1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // Keywords.cc --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Mon Mar 13 12:41:22 2017
|
---|
11 | // Last Modified By :
|
---|
12 | // Last Modified On :
|
---|
13 | // Update Count : 10
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "Concurrency/Keywords.h"
|
---|
17 |
|
---|
18 | #include <cassert> // for assert
|
---|
19 | #include <string> // for string, operator==
|
---|
20 |
|
---|
21 | #include <iostream>
|
---|
22 |
|
---|
23 | #include "Common/Examine.h" // for isMainFor
|
---|
24 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
25 | #include "Common/SemanticError.h" // for SemanticError
|
---|
26 | #include "Common/utility.h" // for deleteAll, map_range
|
---|
27 | #include "CodeGen/OperatorTable.h" // for isConstructor
|
---|
28 | #include "ControlStruct/LabelGenerator.h" // for LebelGenerator
|
---|
29 | #include "InitTweak/InitTweak.h" // for getPointerBase
|
---|
30 | #include "SynTree/LinkageSpec.h" // for Cforall
|
---|
31 | #include "SynTree/Constant.h" // for Constant
|
---|
32 | #include "SynTree/Declaration.h" // for StructDecl, FunctionDecl, ObjectDecl
|
---|
33 | #include "SynTree/Expression.h" // for VariableExpr, ConstantExpr, Untype...
|
---|
34 | #include "SynTree/Initializer.h" // for SingleInit, ListInit, Initializer ...
|
---|
35 | #include "SynTree/Label.h" // for Label
|
---|
36 | #include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt
|
---|
37 | #include "SynTree/Type.h" // for StructInstType, Type, PointerType
|
---|
38 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
39 | #include "Virtual/Tables.h"
|
---|
40 |
|
---|
41 | class Attribute;
|
---|
42 |
|
---|
43 | namespace Concurrency {
|
---|
44 | inline static std::string getTypeIdName( std::string const & exception_name ) {
|
---|
45 | return exception_name.empty() ? std::string() : Virtual::typeIdType( exception_name );
|
---|
46 | }
|
---|
47 | inline static std::string getVTableName( std::string const & exception_name ) {
|
---|
48 | return exception_name.empty() ? std::string() : Virtual::vtableTypeName( exception_name );
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Only detects threads constructed with the keyword thread.
|
---|
52 | inline static bool isThread( DeclarationWithType * decl ) {
|
---|
53 | Type * baseType = decl->get_type()->stripDeclarator();
|
---|
54 | StructInstType * instType = dynamic_cast<StructInstType *>( baseType );
|
---|
55 | if ( nullptr == instType ) { return false; }
|
---|
56 | return instType->baseStruct->is_thread();
|
---|
57 | }
|
---|
58 |
|
---|
59 | //=============================================================================================
|
---|
60 | // Pass declarations
|
---|
61 | //=============================================================================================
|
---|
62 |
|
---|
63 | //-----------------------------------------------------------------------------
|
---|
64 | //Handles sue type declarations :
|
---|
65 | // sue MyType { struct MyType {
|
---|
66 | // int data; int data;
|
---|
67 | // a_struct_t more_data; a_struct_t more_data;
|
---|
68 | // => NewField_t newField;
|
---|
69 | // }; };
|
---|
70 | // static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
|
---|
71 | //
|
---|
72 | class ConcurrentSueKeyword : public WithDeclsToAdd {
|
---|
73 | public:
|
---|
74 |
|
---|
75 | ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name,
|
---|
76 | std::string&& getter_name, std::string&& context_error, std::string&& exception_name,
|
---|
77 | bool needs_main, AggregateDecl::Aggregate cast_target ) :
|
---|
78 | type_name( type_name ), field_name( field_name ), getter_name( getter_name ),
|
---|
79 | context_error( context_error ), exception_name( exception_name ),
|
---|
80 | typeid_name( getTypeIdName( exception_name ) ),
|
---|
81 | vtable_name( getVTableName( exception_name ) ),
|
---|
82 | needs_main( needs_main ), cast_target( cast_target ) {}
|
---|
83 |
|
---|
84 | virtual ~ConcurrentSueKeyword() {}
|
---|
85 |
|
---|
86 | Declaration * postmutate( StructDecl * decl );
|
---|
87 | DeclarationWithType * postmutate( FunctionDecl * decl );
|
---|
88 |
|
---|
89 | void handle( StructDecl * );
|
---|
90 | void addTypeId( StructDecl * );
|
---|
91 | void addVtableForward( StructDecl * );
|
---|
92 | FunctionDecl * forwardDeclare( StructDecl * );
|
---|
93 | ObjectDecl * addField( StructDecl * );
|
---|
94 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
---|
95 | void addLockUnlockRoutines( StructDecl * );
|
---|
96 |
|
---|
97 | virtual bool is_target( StructDecl * decl ) = 0;
|
---|
98 |
|
---|
99 | Expression * postmutate( KeywordCastExpr * cast );
|
---|
100 |
|
---|
101 | private:
|
---|
102 | const std::string type_name;
|
---|
103 | const std::string field_name;
|
---|
104 | const std::string getter_name;
|
---|
105 | const std::string context_error;
|
---|
106 | const std::string exception_name;
|
---|
107 | const std::string typeid_name;
|
---|
108 | const std::string vtable_name;
|
---|
109 | bool needs_main;
|
---|
110 | AggregateDecl::Aggregate cast_target;
|
---|
111 |
|
---|
112 | StructDecl * type_decl = nullptr;
|
---|
113 | FunctionDecl * dtor_decl = nullptr;
|
---|
114 | StructDecl * except_decl = nullptr;
|
---|
115 | StructDecl * typeid_decl = nullptr;
|
---|
116 | StructDecl * vtable_decl = nullptr;
|
---|
117 | };
|
---|
118 |
|
---|
119 |
|
---|
120 | //-----------------------------------------------------------------------------
|
---|
121 | //Handles thread type declarations :
|
---|
122 | // thread Mythread { struct MyThread {
|
---|
123 | // int data; int data;
|
---|
124 | // a_struct_t more_data; a_struct_t more_data;
|
---|
125 | // => thread$ __thrd_d;
|
---|
126 | // }; };
|
---|
127 | // static inline thread$ * get_thread( MyThread * this ) { return &this->__thrd_d; }
|
---|
128 | //
|
---|
129 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
---|
130 | public:
|
---|
131 |
|
---|
132 | ThreadKeyword() : ConcurrentSueKeyword(
|
---|
133 | "thread$",
|
---|
134 | "__thrd",
|
---|
135 | "get_thread",
|
---|
136 | "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
|
---|
137 | "ThreadCancelled",
|
---|
138 | true,
|
---|
139 | AggregateDecl::Thread
|
---|
140 | )
|
---|
141 | {}
|
---|
142 |
|
---|
143 | virtual ~ThreadKeyword() {}
|
---|
144 |
|
---|
145 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
|
---|
146 |
|
---|
147 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
148 | PassVisitor< ThreadKeyword > impl;
|
---|
149 | mutateAll( translationUnit, impl );
|
---|
150 | }
|
---|
151 | };
|
---|
152 |
|
---|
153 | //-----------------------------------------------------------------------------
|
---|
154 | //Handles coroutine type declarations :
|
---|
155 | // coroutine MyCoroutine { struct MyCoroutine {
|
---|
156 | // int data; int data;
|
---|
157 | // a_struct_t more_data; a_struct_t more_data;
|
---|
158 | // => coroutine$ __cor_d;
|
---|
159 | // }; };
|
---|
160 | // static inline coroutine$ * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
|
---|
161 | //
|
---|
162 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
---|
163 | public:
|
---|
164 |
|
---|
165 | CoroutineKeyword() : ConcurrentSueKeyword(
|
---|
166 | "coroutine$",
|
---|
167 | "__cor",
|
---|
168 | "get_coroutine",
|
---|
169 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n",
|
---|
170 | "CoroutineCancelled",
|
---|
171 | true,
|
---|
172 | AggregateDecl::Coroutine
|
---|
173 | )
|
---|
174 | {}
|
---|
175 |
|
---|
176 | virtual ~CoroutineKeyword() {}
|
---|
177 |
|
---|
178 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
---|
179 |
|
---|
180 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
181 | PassVisitor< CoroutineKeyword > impl;
|
---|
182 | mutateAll( translationUnit, impl );
|
---|
183 | }
|
---|
184 | };
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | //-----------------------------------------------------------------------------
|
---|
189 | //Handles monitor type declarations :
|
---|
190 | // monitor MyMonitor { struct MyMonitor {
|
---|
191 | // int data; int data;
|
---|
192 | // a_struct_t more_data; a_struct_t more_data;
|
---|
193 | // => monitor$ __mon_d;
|
---|
194 | // }; };
|
---|
195 | // static inline monitor$ * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
|
---|
196 | //
|
---|
197 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
---|
198 | public:
|
---|
199 |
|
---|
200 | MonitorKeyword() : ConcurrentSueKeyword(
|
---|
201 | "monitor$",
|
---|
202 | "__mon",
|
---|
203 | "get_monitor",
|
---|
204 | "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n",
|
---|
205 | "",
|
---|
206 | false,
|
---|
207 | AggregateDecl::Monitor
|
---|
208 | )
|
---|
209 | {}
|
---|
210 |
|
---|
211 | virtual ~MonitorKeyword() {}
|
---|
212 |
|
---|
213 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
|
---|
214 |
|
---|
215 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
216 | PassVisitor< MonitorKeyword > impl;
|
---|
217 | mutateAll( translationUnit, impl );
|
---|
218 | }
|
---|
219 | };
|
---|
220 |
|
---|
221 | //-----------------------------------------------------------------------------
|
---|
222 | //Handles generator type declarations :
|
---|
223 | // generator MyGenerator { struct MyGenerator {
|
---|
224 | // int data; int data;
|
---|
225 | // a_struct_t more_data; a_struct_t more_data;
|
---|
226 | // => int __gen_next;
|
---|
227 | // }; };
|
---|
228 | //
|
---|
229 | class GeneratorKeyword final : public ConcurrentSueKeyword {
|
---|
230 | public:
|
---|
231 |
|
---|
232 | GeneratorKeyword() : ConcurrentSueKeyword(
|
---|
233 | "generator$",
|
---|
234 | "__generator_state",
|
---|
235 | "get_generator",
|
---|
236 | "Unable to find builtin type generator$\n",
|
---|
237 | "",
|
---|
238 | true,
|
---|
239 | AggregateDecl::Generator
|
---|
240 | )
|
---|
241 | {}
|
---|
242 |
|
---|
243 | virtual ~GeneratorKeyword() {}
|
---|
244 |
|
---|
245 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_generator(); }
|
---|
246 |
|
---|
247 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
248 | PassVisitor< GeneratorKeyword > impl;
|
---|
249 | mutateAll( translationUnit, impl );
|
---|
250 | }
|
---|
251 | };
|
---|
252 |
|
---|
253 |
|
---|
254 | //-----------------------------------------------------------------------------
|
---|
255 | class SuspendKeyword final : public WithStmtsToAdd, public WithGuards {
|
---|
256 | public:
|
---|
257 | SuspendKeyword() = default;
|
---|
258 | virtual ~SuspendKeyword() = default;
|
---|
259 |
|
---|
260 | void premutate( FunctionDecl * );
|
---|
261 | DeclarationWithType * postmutate( FunctionDecl * );
|
---|
262 |
|
---|
263 | Statement * postmutate( SuspendStmt * );
|
---|
264 |
|
---|
265 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
266 | PassVisitor< SuspendKeyword > impl;
|
---|
267 | mutateAll( translationUnit, impl );
|
---|
268 | }
|
---|
269 |
|
---|
270 | private:
|
---|
271 | bool is_real_suspend( FunctionDecl * );
|
---|
272 |
|
---|
273 | Statement * make_generator_suspend( SuspendStmt * );
|
---|
274 | Statement * make_coroutine_suspend( SuspendStmt * );
|
---|
275 |
|
---|
276 | struct LabelPair {
|
---|
277 | Label obj;
|
---|
278 | int idx;
|
---|
279 | };
|
---|
280 |
|
---|
281 | LabelPair make_label() {
|
---|
282 | labels.push_back( gen.newLabel("generator") );
|
---|
283 | return { labels.back(), int(labels.size()) };
|
---|
284 | }
|
---|
285 |
|
---|
286 | DeclarationWithType * in_generator = nullptr;
|
---|
287 | FunctionDecl * decl_suspend = nullptr;
|
---|
288 | std::vector<Label> labels;
|
---|
289 | ControlStruct::LabelGenerator & gen = *ControlStruct::LabelGenerator::getGenerator();
|
---|
290 | };
|
---|
291 |
|
---|
292 | //-----------------------------------------------------------------------------
|
---|
293 | //Handles mutex routines definitions :
|
---|
294 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
295 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
296 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
297 | // /*Some code*/ => /*Some code*/
|
---|
298 | // } }
|
---|
299 | //
|
---|
300 | class MutexKeyword final {
|
---|
301 | public:
|
---|
302 |
|
---|
303 | void postvisit( FunctionDecl * decl );
|
---|
304 | void postvisit( StructDecl * decl );
|
---|
305 | Statement * postmutate( MutexStmt * stmt );
|
---|
306 |
|
---|
307 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
|
---|
308 | void validate( DeclarationWithType * );
|
---|
309 | void addDtorStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
310 | void addStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
311 | void addStatements( CompoundStmt * body, const std::list<Expression * > & args );
|
---|
312 | void addThreadDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args );
|
---|
313 |
|
---|
314 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
315 | PassVisitor< MutexKeyword > impl;
|
---|
316 | acceptAll( translationUnit, impl );
|
---|
317 | mutateAll( translationUnit, impl );
|
---|
318 | }
|
---|
319 |
|
---|
320 | private:
|
---|
321 | StructDecl* monitor_decl = nullptr;
|
---|
322 | StructDecl* guard_decl = nullptr;
|
---|
323 | StructDecl* dtor_guard_decl = nullptr;
|
---|
324 | StructDecl* thread_guard_decl = nullptr;
|
---|
325 | StructDecl* lock_guard_decl = nullptr;
|
---|
326 |
|
---|
327 | static std::unique_ptr< Type > generic_func;
|
---|
328 | };
|
---|
329 |
|
---|
330 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
---|
331 | new FunctionType(
|
---|
332 | noQualifiers,
|
---|
333 | true
|
---|
334 | )
|
---|
335 | );
|
---|
336 |
|
---|
337 | //-----------------------------------------------------------------------------
|
---|
338 | //Handles mutex routines definitions :
|
---|
339 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
340 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
341 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
342 | // /*Some code*/ => /*Some code*/
|
---|
343 | // } }
|
---|
344 | //
|
---|
345 | class ThreadStarter final {
|
---|
346 | public:
|
---|
347 |
|
---|
348 | void postvisit( FunctionDecl * decl );
|
---|
349 | void previsit ( StructDecl * decl );
|
---|
350 |
|
---|
351 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
|
---|
352 |
|
---|
353 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
354 | PassVisitor< ThreadStarter > impl;
|
---|
355 | acceptAll( translationUnit, impl );
|
---|
356 | }
|
---|
357 |
|
---|
358 | private :
|
---|
359 | bool thread_ctor_seen = false;
|
---|
360 | StructDecl * thread_decl = nullptr;
|
---|
361 | };
|
---|
362 |
|
---|
363 | //=============================================================================================
|
---|
364 | // General entry routine
|
---|
365 | //=============================================================================================
|
---|
366 | void applyKeywords( std::list< Declaration * > & translationUnit ) {
|
---|
367 | ThreadKeyword ::implement( translationUnit );
|
---|
368 | CoroutineKeyword ::implement( translationUnit );
|
---|
369 | MonitorKeyword ::implement( translationUnit );
|
---|
370 | GeneratorKeyword ::implement( translationUnit );
|
---|
371 | SuspendKeyword ::implement( translationUnit );
|
---|
372 | }
|
---|
373 |
|
---|
374 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
---|
375 | MutexKeyword ::implement( translationUnit );
|
---|
376 | }
|
---|
377 |
|
---|
378 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
---|
379 | ThreadStarter ::implement( translationUnit );
|
---|
380 | }
|
---|
381 |
|
---|
382 | //=============================================================================================
|
---|
383 | // Generic keyword implementation
|
---|
384 | //=============================================================================================
|
---|
385 | void fixupGenerics(FunctionType * func, StructDecl * decl) {
|
---|
386 | cloneAll(decl->parameters, func->forall);
|
---|
387 | for ( TypeDecl * td : func->forall ) {
|
---|
388 | strict_dynamic_cast<StructInstType*>(
|
---|
389 | func->parameters.front()->get_type()->stripReferences()
|
---|
390 | )->parameters.push_back(
|
---|
391 | new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) )
|
---|
392 | );
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
|
---|
397 | if( decl->name == type_name && decl->body ) {
|
---|
398 | assert( !type_decl );
|
---|
399 | type_decl = decl;
|
---|
400 | }
|
---|
401 | else if ( is_target(decl) ) {
|
---|
402 | handle( decl );
|
---|
403 | }
|
---|
404 | else if ( !except_decl && exception_name == decl->name && decl->body ) {
|
---|
405 | except_decl = decl;
|
---|
406 | }
|
---|
407 | else if ( !typeid_decl && typeid_name == decl->name && decl->body ) {
|
---|
408 | typeid_decl = decl;
|
---|
409 | }
|
---|
410 | else if ( !vtable_decl && vtable_name == decl->name && decl->body ) {
|
---|
411 | vtable_decl = decl;
|
---|
412 | }
|
---|
413 | // Might be able to get ride of is target.
|
---|
414 | assert( is_target(decl) == (cast_target == decl->kind) );
|
---|
415 | return decl;
|
---|
416 | }
|
---|
417 |
|
---|
418 | DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) {
|
---|
419 | if ( type_decl && isDestructorFor( decl, type_decl ) )
|
---|
420 | dtor_decl = decl;
|
---|
421 | else if ( vtable_name.empty() || !decl->has_body() )
|
---|
422 | ;
|
---|
423 | else if ( auto param = isMainFor( decl, cast_target ) ) {
|
---|
424 | // This should never trigger.
|
---|
425 | assert( vtable_decl );
|
---|
426 | // Should be safe because of isMainFor.
|
---|
427 | StructInstType * struct_type = static_cast<StructInstType *>(
|
---|
428 | static_cast<ReferenceType *>( param->get_type() )->base );
|
---|
429 | assert( struct_type );
|
---|
430 |
|
---|
431 | std::list< Expression * > poly_args = { new TypeExpr( struct_type->clone() ) };
|
---|
432 | ObjectDecl * vtable_object = Virtual::makeVtableInstance(
|
---|
433 | "_default_vtable_object_declaration",
|
---|
434 | vtable_decl->makeInst( poly_args ), struct_type, nullptr );
|
---|
435 | declsToAddAfter.push_back( vtable_object );
|
---|
436 | declsToAddAfter.push_back(
|
---|
437 | new ObjectDecl(
|
---|
438 | Virtual::concurrentDefaultVTableName(),
|
---|
439 | noStorageClasses,
|
---|
440 | LinkageSpec::Cforall,
|
---|
441 | /* bitfieldWidth */ nullptr,
|
---|
442 | new ReferenceType( Type::Const, vtable_object->type->clone() ),
|
---|
443 | new SingleInit( new VariableExpr( vtable_object ) )
|
---|
444 | )
|
---|
445 | );
|
---|
446 | declsToAddAfter.push_back( Virtual::makeGetExceptionFunction(
|
---|
447 | vtable_object, except_decl->makeInst( std::move( poly_args ) )
|
---|
448 | ) );
|
---|
449 | }
|
---|
450 |
|
---|
451 | return decl;
|
---|
452 | }
|
---|
453 |
|
---|
454 | Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
|
---|
455 | if ( cast_target == cast->target ) {
|
---|
456 | // convert (thread &)t to (thread$ &)*get_thread(t), etc.
|
---|
457 | if( !type_decl ) SemanticError( cast, context_error );
|
---|
458 | if( !dtor_decl ) SemanticError( cast, context_error );
|
---|
459 | assert( cast->result == nullptr );
|
---|
460 | cast->set_result( new ReferenceType( noQualifiers, new StructInstType( noQualifiers, type_decl ) ) );
|
---|
461 | cast->concrete_target.field = field_name;
|
---|
462 | cast->concrete_target.getter = getter_name;
|
---|
463 | }
|
---|
464 | return cast;
|
---|
465 | }
|
---|
466 |
|
---|
467 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
---|
468 | if( ! decl->body ) return;
|
---|
469 |
|
---|
470 | if( !type_decl ) SemanticError( decl, context_error );
|
---|
471 | if( !dtor_decl ) SemanticError( decl, context_error );
|
---|
472 |
|
---|
473 | if ( !exception_name.empty() ) {
|
---|
474 | if( !typeid_decl ) SemanticError( decl, context_error );
|
---|
475 | if( !vtable_decl ) SemanticError( decl, context_error );
|
---|
476 |
|
---|
477 | addTypeId( decl );
|
---|
478 | addVtableForward( decl );
|
---|
479 | }
|
---|
480 | FunctionDecl * func = forwardDeclare( decl );
|
---|
481 | ObjectDecl * field = addField( decl );
|
---|
482 |
|
---|
483 | // add get_.* routine
|
---|
484 | addRoutines( field, func );
|
---|
485 | // add lock/unlock routines to monitors for use by mutex stmt
|
---|
486 | addLockUnlockRoutines( decl );
|
---|
487 | }
|
---|
488 |
|
---|
489 | void ConcurrentSueKeyword::addTypeId( StructDecl * decl ) {
|
---|
490 | assert( typeid_decl );
|
---|
491 | StructInstType typeid_type( Type::Const, typeid_decl );
|
---|
492 | typeid_type.parameters.push_back( new TypeExpr(
|
---|
493 | new StructInstType( noQualifiers, decl )
|
---|
494 | ) );
|
---|
495 | declsToAddBefore.push_back( Virtual::makeTypeIdInstance( &typeid_type ) );
|
---|
496 | }
|
---|
497 |
|
---|
498 | void ConcurrentSueKeyword::addVtableForward( StructDecl * decl ) {
|
---|
499 | assert( vtable_decl );
|
---|
500 | std::list< Expression * > poly_args = {
|
---|
501 | new TypeExpr( new StructInstType( noQualifiers, decl ) ),
|
---|
502 | };
|
---|
503 | declsToAddBefore.push_back( Virtual::makeGetExceptionForward(
|
---|
504 | vtable_decl->makeInst( poly_args ),
|
---|
505 | except_decl->makeInst( poly_args )
|
---|
506 | ) );
|
---|
507 | ObjectDecl * vtable_object = Virtual::makeVtableForward(
|
---|
508 | "_default_vtable_object_declaration",
|
---|
509 | vtable_decl->makeInst( move( poly_args ) ) );
|
---|
510 | declsToAddBefore.push_back( vtable_object );
|
---|
511 | declsToAddBefore.push_back(
|
---|
512 | new ObjectDecl(
|
---|
513 | Virtual::concurrentDefaultVTableName(),
|
---|
514 | Type::StorageClasses( Type::Extern ),
|
---|
515 | LinkageSpec::Cforall,
|
---|
516 | /* bitfieldWidth */ nullptr,
|
---|
517 | new ReferenceType( Type::Const, vtable_object->type->clone() ),
|
---|
518 | /* init */ nullptr
|
---|
519 | )
|
---|
520 | );
|
---|
521 | }
|
---|
522 |
|
---|
523 | FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
|
---|
524 |
|
---|
525 | StructDecl * forward = decl->clone();
|
---|
526 | forward->set_body( false );
|
---|
527 | deleteAll( forward->get_members() );
|
---|
528 | forward->get_members().clear();
|
---|
529 |
|
---|
530 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
---|
531 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
532 | "this",
|
---|
533 | noStorageClasses,
|
---|
534 | LinkageSpec::Cforall,
|
---|
535 | nullptr,
|
---|
536 | new ReferenceType(
|
---|
537 | noQualifiers,
|
---|
538 | new StructInstType(
|
---|
539 | noQualifiers,
|
---|
540 | decl
|
---|
541 | )
|
---|
542 | ),
|
---|
543 | nullptr
|
---|
544 | );
|
---|
545 |
|
---|
546 | get_type->get_parameters().push_back( this_decl->clone() );
|
---|
547 | get_type->get_returnVals().push_back(
|
---|
548 | new ObjectDecl(
|
---|
549 | "ret",
|
---|
550 | noStorageClasses,
|
---|
551 | LinkageSpec::Cforall,
|
---|
552 | nullptr,
|
---|
553 | new PointerType(
|
---|
554 | noQualifiers,
|
---|
555 | new StructInstType(
|
---|
556 | noQualifiers,
|
---|
557 | type_decl
|
---|
558 | )
|
---|
559 | ),
|
---|
560 | nullptr
|
---|
561 | )
|
---|
562 | );
|
---|
563 | fixupGenerics(get_type, decl);
|
---|
564 |
|
---|
565 | FunctionDecl * get_decl = new FunctionDecl(
|
---|
566 | getter_name,
|
---|
567 | Type::Static,
|
---|
568 | LinkageSpec::Cforall,
|
---|
569 | get_type,
|
---|
570 | nullptr,
|
---|
571 | { new Attribute("const") },
|
---|
572 | Type::Inline
|
---|
573 | );
|
---|
574 |
|
---|
575 | FunctionDecl * main_decl = nullptr;
|
---|
576 |
|
---|
577 | if( needs_main ) {
|
---|
578 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
---|
579 |
|
---|
580 | main_type->get_parameters().push_back( this_decl->clone() );
|
---|
581 |
|
---|
582 | main_decl = new FunctionDecl(
|
---|
583 | "main",
|
---|
584 | noStorageClasses,
|
---|
585 | LinkageSpec::Cforall,
|
---|
586 | main_type,
|
---|
587 | nullptr
|
---|
588 | );
|
---|
589 | fixupGenerics(main_type, decl);
|
---|
590 | }
|
---|
591 |
|
---|
592 | delete this_decl;
|
---|
593 |
|
---|
594 | declsToAddBefore.push_back( forward );
|
---|
595 | if( needs_main ) declsToAddBefore.push_back( main_decl );
|
---|
596 | declsToAddBefore.push_back( get_decl );
|
---|
597 |
|
---|
598 | return get_decl;
|
---|
599 | }
|
---|
600 |
|
---|
601 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
|
---|
602 | ObjectDecl * field = new ObjectDecl(
|
---|
603 | field_name,
|
---|
604 | noStorageClasses,
|
---|
605 | LinkageSpec::Cforall,
|
---|
606 | nullptr,
|
---|
607 | new StructInstType(
|
---|
608 | noQualifiers,
|
---|
609 | type_decl
|
---|
610 | ),
|
---|
611 | nullptr
|
---|
612 | );
|
---|
613 |
|
---|
614 | decl->get_members().push_back( field );
|
---|
615 |
|
---|
616 | return field;
|
---|
617 | }
|
---|
618 |
|
---|
619 | // This function adds the get_.* routine body for coroutines, monitors etc
|
---|
620 | // after their corresponding struct has been made
|
---|
621 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
---|
622 | CompoundStmt * statement = new CompoundStmt();
|
---|
623 | statement->push_back(
|
---|
624 | new ReturnStmt(
|
---|
625 | new AddressExpr(
|
---|
626 | new MemberExpr(
|
---|
627 | field,
|
---|
628 | new CastExpr(
|
---|
629 | new VariableExpr( func->get_functionType()->get_parameters().front() ),
|
---|
630 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone(),
|
---|
631 | false
|
---|
632 | )
|
---|
633 | )
|
---|
634 | )
|
---|
635 | )
|
---|
636 | );
|
---|
637 |
|
---|
638 | FunctionDecl * get_decl = func->clone();
|
---|
639 |
|
---|
640 | get_decl->set_statements( statement );
|
---|
641 |
|
---|
642 | declsToAddAfter.push_back( get_decl );
|
---|
643 | }
|
---|
644 |
|
---|
645 | // Generates lock/unlock routines for monitors to be used by mutex stmts
|
---|
646 | void ConcurrentSueKeyword::addLockUnlockRoutines( StructDecl * decl ) {
|
---|
647 | // this routine will be called for all ConcurrentSueKeyword children so only continue if we are a monitor
|
---|
648 | if ( !decl->is_monitor() ) return;
|
---|
649 |
|
---|
650 | FunctionType * lock_fn_type = new FunctionType( noQualifiers, false );
|
---|
651 | FunctionType * unlock_fn_type = new FunctionType( noQualifiers, false );
|
---|
652 |
|
---|
653 | // create this ptr parameter for both routines
|
---|
654 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
655 | "this",
|
---|
656 | noStorageClasses,
|
---|
657 | LinkageSpec::Cforall,
|
---|
658 | nullptr,
|
---|
659 | new ReferenceType(
|
---|
660 | noQualifiers,
|
---|
661 | new StructInstType(
|
---|
662 | noQualifiers,
|
---|
663 | decl
|
---|
664 | )
|
---|
665 | ),
|
---|
666 | nullptr
|
---|
667 | );
|
---|
668 |
|
---|
669 | lock_fn_type->get_parameters().push_back( this_decl->clone() );
|
---|
670 | unlock_fn_type->get_parameters().push_back( this_decl->clone() );
|
---|
671 | fixupGenerics(lock_fn_type, decl);
|
---|
672 | fixupGenerics(unlock_fn_type, decl);
|
---|
673 |
|
---|
674 | delete this_decl;
|
---|
675 |
|
---|
676 |
|
---|
677 | //////////////////////////////////////////////////////////////////////
|
---|
678 | // The following generates this lock routine for all monitors
|
---|
679 | /*
|
---|
680 | void lock (monitor_t & this) {
|
---|
681 | lock(get_monitor(this));
|
---|
682 | }
|
---|
683 | */
|
---|
684 | FunctionDecl * lock_decl = new FunctionDecl(
|
---|
685 | "lock",
|
---|
686 | Type::Static,
|
---|
687 | LinkageSpec::Cforall,
|
---|
688 | lock_fn_type,
|
---|
689 | nullptr,
|
---|
690 | { },
|
---|
691 | Type::Inline
|
---|
692 | );
|
---|
693 |
|
---|
694 | UntypedExpr * get_monitor_lock = new UntypedExpr (
|
---|
695 | new NameExpr( "get_monitor" ),
|
---|
696 | { new VariableExpr( lock_fn_type->get_parameters().front() ) }
|
---|
697 | );
|
---|
698 |
|
---|
699 | CompoundStmt * lock_statement = new CompoundStmt();
|
---|
700 | lock_statement->push_back(
|
---|
701 | new ExprStmt(
|
---|
702 | new UntypedExpr (
|
---|
703 | new NameExpr( "lock" ),
|
---|
704 | {
|
---|
705 | get_monitor_lock
|
---|
706 | }
|
---|
707 | )
|
---|
708 | )
|
---|
709 | );
|
---|
710 | lock_decl->set_statements( lock_statement );
|
---|
711 |
|
---|
712 | //////////////////////////////////////////////////////////////////
|
---|
713 | // The following generates this routine for all monitors
|
---|
714 | /*
|
---|
715 | void unlock (monitor_t & this) {
|
---|
716 | unlock(get_monitor(this));
|
---|
717 | }
|
---|
718 | */
|
---|
719 | FunctionDecl * unlock_decl = new FunctionDecl(
|
---|
720 | "unlock",
|
---|
721 | Type::Static,
|
---|
722 | LinkageSpec::Cforall,
|
---|
723 | unlock_fn_type,
|
---|
724 | nullptr,
|
---|
725 | { },
|
---|
726 | Type::Inline
|
---|
727 | );
|
---|
728 |
|
---|
729 | CompoundStmt * unlock_statement = new CompoundStmt();
|
---|
730 |
|
---|
731 | UntypedExpr * get_monitor_unlock = new UntypedExpr (
|
---|
732 | new NameExpr( "get_monitor" ),
|
---|
733 | { new VariableExpr( unlock_fn_type->get_parameters().front() ) }
|
---|
734 | );
|
---|
735 |
|
---|
736 | unlock_statement->push_back(
|
---|
737 | new ExprStmt(
|
---|
738 | new UntypedExpr(
|
---|
739 | new NameExpr( "unlock" ),
|
---|
740 | {
|
---|
741 | get_monitor_unlock
|
---|
742 | }
|
---|
743 | )
|
---|
744 | )
|
---|
745 | );
|
---|
746 | unlock_decl->set_statements( unlock_statement );
|
---|
747 |
|
---|
748 | // pushes routines to declsToAddAfter to add at a later time
|
---|
749 | declsToAddAfter.push_back( lock_decl );
|
---|
750 | declsToAddAfter.push_back( unlock_decl );
|
---|
751 | }
|
---|
752 |
|
---|
753 | //=============================================================================================
|
---|
754 | // Suspend keyword implementation
|
---|
755 | //=============================================================================================
|
---|
756 | bool SuspendKeyword::is_real_suspend( FunctionDecl * func ) {
|
---|
757 | if(isMangled(func->linkage)) return false; // the real suspend isn't mangled
|
---|
758 | if(func->name != "__cfactx_suspend") return false; // the real suspend has a specific name
|
---|
759 | if(func->type->parameters.size() != 0) return false; // Too many parameters
|
---|
760 | if(func->type->returnVals.size() != 0) return false; // Too many return values
|
---|
761 |
|
---|
762 | return true;
|
---|
763 | }
|
---|
764 |
|
---|
765 | void SuspendKeyword::premutate( FunctionDecl * func ) {
|
---|
766 | GuardValue(in_generator);
|
---|
767 | in_generator = nullptr;
|
---|
768 |
|
---|
769 | // Is this the real suspend?
|
---|
770 | if(is_real_suspend(func)) {
|
---|
771 | decl_suspend = decl_suspend ? decl_suspend : func;
|
---|
772 | return;
|
---|
773 | }
|
---|
774 |
|
---|
775 | // Is this the main of a generator?
|
---|
776 | auto param = isMainFor( func, AggregateDecl::Aggregate::Generator );
|
---|
777 | if(!param) return;
|
---|
778 |
|
---|
779 | if(func->type->returnVals.size() != 0) SemanticError(func->location, "Generator main must return void");
|
---|
780 |
|
---|
781 | in_generator = param;
|
---|
782 | GuardValue(labels);
|
---|
783 | labels.clear();
|
---|
784 | }
|
---|
785 |
|
---|
786 | DeclarationWithType * SuspendKeyword::postmutate( FunctionDecl * func ) {
|
---|
787 | if( !func->statements ) return func; // Not the actual definition, don't do anything
|
---|
788 | if( !in_generator ) return func; // Not in a generator, don't do anything
|
---|
789 | if( labels.empty() ) return func; // Generator has no states, nothing to do, could throw a warning
|
---|
790 |
|
---|
791 | // This is a generator main, we need to add the following code to the top
|
---|
792 | // static void * __generator_labels[] = {&&s0, &&s1, ...};
|
---|
793 | // goto * __generator_labels[gen.__generator_state];
|
---|
794 | const auto & loc = func->location;
|
---|
795 |
|
---|
796 | const auto first_label = gen.newLabel("generator");
|
---|
797 |
|
---|
798 | // for each label add to declaration
|
---|
799 | std::list<Initializer*> inits = { new SingleInit( new LabelAddressExpr( first_label ) ) };
|
---|
800 | for(const auto & label : labels) {
|
---|
801 | inits.push_back(
|
---|
802 | new SingleInit(
|
---|
803 | new LabelAddressExpr( label )
|
---|
804 | )
|
---|
805 | );
|
---|
806 | }
|
---|
807 | auto init = new ListInit(std::move(inits), noDesignators, true);
|
---|
808 | labels.clear();
|
---|
809 |
|
---|
810 | // create decl
|
---|
811 | auto decl = new ObjectDecl(
|
---|
812 | "__generator_labels",
|
---|
813 | Type::StorageClasses( Type::Static ),
|
---|
814 | LinkageSpec::AutoGen,
|
---|
815 | nullptr,
|
---|
816 | new ArrayType(
|
---|
817 | Type::Qualifiers(),
|
---|
818 | new PointerType(
|
---|
819 | Type::Qualifiers(),
|
---|
820 | new VoidType( Type::Qualifiers() )
|
---|
821 | ),
|
---|
822 | nullptr,
|
---|
823 | false, false
|
---|
824 | ),
|
---|
825 | init
|
---|
826 | );
|
---|
827 |
|
---|
828 | // create the goto
|
---|
829 | assert(in_generator);
|
---|
830 |
|
---|
831 | auto go_decl = new ObjectDecl(
|
---|
832 | "__generator_label",
|
---|
833 | noStorageClasses,
|
---|
834 | LinkageSpec::AutoGen,
|
---|
835 | nullptr,
|
---|
836 | new PointerType(
|
---|
837 | Type::Qualifiers(),
|
---|
838 | new VoidType( Type::Qualifiers() )
|
---|
839 | ),
|
---|
840 | new SingleInit(
|
---|
841 | new UntypedExpr(
|
---|
842 | new NameExpr("?[?]"),
|
---|
843 | {
|
---|
844 | new NameExpr("__generator_labels"),
|
---|
845 | new UntypedMemberExpr(
|
---|
846 | new NameExpr("__generator_state"),
|
---|
847 | new VariableExpr( in_generator )
|
---|
848 | )
|
---|
849 | }
|
---|
850 | )
|
---|
851 | )
|
---|
852 | );
|
---|
853 | go_decl->location = loc;
|
---|
854 |
|
---|
855 | auto go = new BranchStmt(
|
---|
856 | new VariableExpr( go_decl ),
|
---|
857 | BranchStmt::Goto
|
---|
858 | );
|
---|
859 | go->location = loc;
|
---|
860 | go->computedTarget->location = loc;
|
---|
861 |
|
---|
862 | auto noop = new NullStmt({ first_label });
|
---|
863 | noop->location = loc;
|
---|
864 |
|
---|
865 | // wrap everything in a nice compound
|
---|
866 | auto body = new CompoundStmt({
|
---|
867 | new DeclStmt( decl ),
|
---|
868 | new DeclStmt( go_decl ),
|
---|
869 | go,
|
---|
870 | noop,
|
---|
871 | func->statements
|
---|
872 | });
|
---|
873 | body->location = loc;
|
---|
874 | func->statements = body;
|
---|
875 |
|
---|
876 | return func;
|
---|
877 | }
|
---|
878 |
|
---|
879 | Statement * SuspendKeyword::postmutate( SuspendStmt * stmt ) {
|
---|
880 | SuspendStmt::Type type = stmt->type;
|
---|
881 | if(type == SuspendStmt::None) {
|
---|
882 | // This suspend has a implicit target, find it
|
---|
883 | type = in_generator ? SuspendStmt::Generator : SuspendStmt::Coroutine;
|
---|
884 | }
|
---|
885 |
|
---|
886 | // Check that the target makes sense
|
---|
887 | if(!in_generator && type == SuspendStmt::Generator) SemanticError( stmt->location, "'suspend generator' must be used inside main of generator type.");
|
---|
888 |
|
---|
889 | // Act appropriately
|
---|
890 | switch(type) {
|
---|
891 | case SuspendStmt::Generator: return make_generator_suspend(stmt);
|
---|
892 | case SuspendStmt::Coroutine: return make_coroutine_suspend(stmt);
|
---|
893 | default: abort();
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | Statement * SuspendKeyword::make_generator_suspend( SuspendStmt * stmt ) {
|
---|
898 | assert(in_generator);
|
---|
899 | // Target code is :
|
---|
900 | // gen.__generator_state = X;
|
---|
901 | // { THEN }
|
---|
902 | // return;
|
---|
903 | // __gen_X:;
|
---|
904 |
|
---|
905 | // Save the location and delete the old statement, we only need the location from this point on
|
---|
906 | auto loc = stmt->location;
|
---|
907 |
|
---|
908 | // Build the label and get its index
|
---|
909 | auto label = make_label();
|
---|
910 |
|
---|
911 | // Create the context saving statement
|
---|
912 | auto save = new ExprStmt( new UntypedExpr(
|
---|
913 | new NameExpr( "?=?" ),
|
---|
914 | {
|
---|
915 | new UntypedMemberExpr(
|
---|
916 | new NameExpr("__generator_state"),
|
---|
917 | new VariableExpr( in_generator )
|
---|
918 | ),
|
---|
919 | new ConstantExpr(
|
---|
920 | Constant::from_int( label.idx )
|
---|
921 | )
|
---|
922 | }
|
---|
923 | ));
|
---|
924 | assert(save->expr);
|
---|
925 | save->location = loc;
|
---|
926 | stmtsToAddBefore.push_back( save );
|
---|
927 |
|
---|
928 | // if we have a then add it here
|
---|
929 | auto then = stmt->then;
|
---|
930 | stmt->then = nullptr;
|
---|
931 | delete stmt;
|
---|
932 | if(then) stmtsToAddBefore.push_back( then );
|
---|
933 |
|
---|
934 | // Create the return statement
|
---|
935 | auto ret = new ReturnStmt( nullptr );
|
---|
936 | ret->location = loc;
|
---|
937 | stmtsToAddBefore.push_back( ret );
|
---|
938 |
|
---|
939 | // Create the null statement with the created label
|
---|
940 | auto noop = new NullStmt({ label.obj });
|
---|
941 | noop->location = loc;
|
---|
942 |
|
---|
943 | // Return the null statement to take the place of the previous statement
|
---|
944 | return noop;
|
---|
945 | }
|
---|
946 |
|
---|
947 | Statement * SuspendKeyword::make_coroutine_suspend( SuspendStmt * stmt ) {
|
---|
948 | if(stmt->then) SemanticError( stmt->location, "Compound statement following coroutines is not implemented.");
|
---|
949 |
|
---|
950 | // Save the location and delete the old statement, we only need the location from this point on
|
---|
951 | auto loc = stmt->location;
|
---|
952 | delete stmt;
|
---|
953 |
|
---|
954 | // Create the call expression
|
---|
955 | if(!decl_suspend) SemanticError( loc, "suspend keyword applied to coroutines requires coroutines to be in scope, add #include <coroutine.hfa>\n");
|
---|
956 | auto expr = new UntypedExpr( VariableExpr::functionPointer( decl_suspend ) );
|
---|
957 | expr->location = loc;
|
---|
958 |
|
---|
959 | // Change this statement into a regular expr
|
---|
960 | assert(expr);
|
---|
961 | auto nstmt = new ExprStmt( expr );
|
---|
962 | nstmt->location = loc;
|
---|
963 | return nstmt;
|
---|
964 | }
|
---|
965 |
|
---|
966 |
|
---|
967 | //=============================================================================================
|
---|
968 | // Mutex keyword implementation
|
---|
969 | //=============================================================================================
|
---|
970 |
|
---|
971 | void MutexKeyword::postvisit(FunctionDecl* decl) {
|
---|
972 |
|
---|
973 | bool first = false;
|
---|
974 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
|
---|
975 | bool const isDtor = CodeGen::isDestructor( decl->name );
|
---|
976 |
|
---|
977 | // Is this function relevant to monitors
|
---|
978 | if( mutexArgs.empty() ) {
|
---|
979 | // If this is the destructor for a monitor it must be mutex
|
---|
980 | if(isDtor) {
|
---|
981 | Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
|
---|
982 |
|
---|
983 | // If it's a copy, it's not a mutex
|
---|
984 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
985 | if( ! rty ) return;
|
---|
986 |
|
---|
987 | // If we are not pointing directly to a type, it's not a mutex
|
---|
988 | Type* base = rty->get_base();
|
---|
989 | if( dynamic_cast< ReferenceType * >( base ) ) return;
|
---|
990 | if( dynamic_cast< PointerType * >( base ) ) return;
|
---|
991 |
|
---|
992 | // Check if its a struct
|
---|
993 | StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
|
---|
994 | if( !baseStruct ) return;
|
---|
995 |
|
---|
996 | // Check if its a monitor
|
---|
997 | if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
|
---|
998 | SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
|
---|
999 | }
|
---|
1000 | return;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | // Monitors can't be constructed with mutual exclusion
|
---|
1004 | if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
|
---|
1005 |
|
---|
1006 | // It makes no sense to have multiple mutex parameters for the destructor
|
---|
1007 | if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
|
---|
1008 |
|
---|
1009 | // Make sure all the mutex arguments are monitors
|
---|
1010 | for(auto arg : mutexArgs) {
|
---|
1011 | validate( arg );
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | // Check if we need to instrument the body
|
---|
1015 | CompoundStmt* body = decl->get_statements();
|
---|
1016 | if( ! body ) return;
|
---|
1017 |
|
---|
1018 | // Do we have the required headers
|
---|
1019 | if( !monitor_decl || !guard_decl || !dtor_guard_decl )
|
---|
1020 | SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>\n" );
|
---|
1021 |
|
---|
1022 | // Instrument the body
|
---|
1023 | if ( isDtor && isThread( mutexArgs.front() ) ) {
|
---|
1024 | if( !thread_guard_decl ) {
|
---|
1025 | SemanticError( decl, "thread destructor requires threads to be in scope, add #include <thread.hfa>\n" );
|
---|
1026 | }
|
---|
1027 | addThreadDtorStatements( decl, body, mutexArgs );
|
---|
1028 | }
|
---|
1029 | else if ( isDtor ) {
|
---|
1030 | addDtorStatements( decl, body, mutexArgs );
|
---|
1031 | }
|
---|
1032 | else {
|
---|
1033 | addStatements( decl, body, mutexArgs );
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | void MutexKeyword::postvisit(StructDecl* decl) {
|
---|
1038 |
|
---|
1039 | if( decl->name == "monitor$" && decl->body ) {
|
---|
1040 | assert( !monitor_decl );
|
---|
1041 | monitor_decl = decl;
|
---|
1042 | }
|
---|
1043 | else if( decl->name == "monitor_guard_t" && decl->body ) {
|
---|
1044 | assert( !guard_decl );
|
---|
1045 | guard_decl = decl;
|
---|
1046 | }
|
---|
1047 | else if( decl->name == "monitor_dtor_guard_t" && decl->body ) {
|
---|
1048 | assert( !dtor_guard_decl );
|
---|
1049 | dtor_guard_decl = decl;
|
---|
1050 | }
|
---|
1051 | else if( decl->name == "thread_dtor_guard_t" && decl->body ) {
|
---|
1052 | assert( !thread_guard_decl );
|
---|
1053 | thread_guard_decl = decl;
|
---|
1054 | }
|
---|
1055 | else if ( decl->name == "__mutex_stmt_lock_guard" && decl->body ) {
|
---|
1056 | assert( !lock_guard_decl );
|
---|
1057 | lock_guard_decl = decl;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | Statement * MutexKeyword::postmutate( MutexStmt * stmt ) {
|
---|
1062 | std::list<Statement *> stmtsForCtor;
|
---|
1063 | stmtsForCtor.push_back(stmt->stmt);
|
---|
1064 | CompoundStmt * body = new CompoundStmt( stmtsForCtor );
|
---|
1065 | addStatements( body, stmt->mutexObjs);
|
---|
1066 | return body;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
|
---|
1070 | std::list<DeclarationWithType*> mutexArgs;
|
---|
1071 |
|
---|
1072 | bool once = true;
|
---|
1073 | for( auto arg : decl->get_functionType()->get_parameters()) {
|
---|
1074 | //Find mutex arguments
|
---|
1075 | Type* ty = arg->get_type();
|
---|
1076 | if( ! ty->get_mutex() ) continue;
|
---|
1077 |
|
---|
1078 | if(once) {first = true;}
|
---|
1079 | once = false;
|
---|
1080 |
|
---|
1081 | //Append it to the list
|
---|
1082 | mutexArgs.push_back( arg );
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | return mutexArgs;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | void MutexKeyword::validate( DeclarationWithType * arg ) {
|
---|
1089 | Type* ty = arg->get_type();
|
---|
1090 |
|
---|
1091 | //Makes sure it's not a copy
|
---|
1092 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
1093 | if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " );
|
---|
1094 |
|
---|
1095 | //Make sure the we are pointing directly to a type
|
---|
1096 | Type* base = rty->get_base();
|
---|
1097 | if( dynamic_cast< ReferenceType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
1098 | if( dynamic_cast< PointerType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
1099 |
|
---|
1100 | //Make sure that typed isn't mutex
|
---|
1101 | if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " );
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | void MutexKeyword::addDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
1105 | Type * arg_type = args.front()->get_type()->clone();
|
---|
1106 | arg_type->set_mutex( false );
|
---|
1107 |
|
---|
1108 | ObjectDecl * monitors = new ObjectDecl(
|
---|
1109 | "__monitor",
|
---|
1110 | noStorageClasses,
|
---|
1111 | LinkageSpec::Cforall,
|
---|
1112 | nullptr,
|
---|
1113 | new PointerType(
|
---|
1114 | noQualifiers,
|
---|
1115 | new StructInstType(
|
---|
1116 | noQualifiers,
|
---|
1117 | monitor_decl
|
---|
1118 | )
|
---|
1119 | ),
|
---|
1120 | new SingleInit( new UntypedExpr(
|
---|
1121 | new NameExpr( "get_monitor" ),
|
---|
1122 | { new CastExpr( new VariableExpr( args.front() ), arg_type, false ) }
|
---|
1123 | ))
|
---|
1124 | );
|
---|
1125 |
|
---|
1126 | assert(generic_func);
|
---|
1127 |
|
---|
1128 | //in reverse order :
|
---|
1129 | // monitor_dtor_guard_t __guard = { __monitors, func };
|
---|
1130 | body->push_front(
|
---|
1131 | new DeclStmt( new ObjectDecl(
|
---|
1132 | "__guard",
|
---|
1133 | noStorageClasses,
|
---|
1134 | LinkageSpec::Cforall,
|
---|
1135 | nullptr,
|
---|
1136 | new StructInstType(
|
---|
1137 | noQualifiers,
|
---|
1138 | dtor_guard_decl
|
---|
1139 | ),
|
---|
1140 | new ListInit(
|
---|
1141 | {
|
---|
1142 | new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ),
|
---|
1143 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ),
|
---|
1144 | new SingleInit( new ConstantExpr( Constant::from_bool( false ) ) )
|
---|
1145 | },
|
---|
1146 | noDesignators,
|
---|
1147 | true
|
---|
1148 | )
|
---|
1149 | ))
|
---|
1150 | );
|
---|
1151 |
|
---|
1152 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
1153 | body->push_front( new DeclStmt( monitors ) );
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | void MutexKeyword::addThreadDtorStatements(
|
---|
1157 | FunctionDecl*, CompoundStmt * body,
|
---|
1158 | const std::list<DeclarationWithType * > & args ) {
|
---|
1159 | assert( args.size() == 1 );
|
---|
1160 | DeclarationWithType * arg = args.front();
|
---|
1161 | Type * arg_type = arg->get_type()->clone();
|
---|
1162 | assert( arg_type->get_mutex() );
|
---|
1163 | arg_type->set_mutex( false );
|
---|
1164 |
|
---|
1165 | // thread_dtor_guard_t __guard = { this, intptr( 0 ) };
|
---|
1166 | body->push_front(
|
---|
1167 | new DeclStmt( new ObjectDecl(
|
---|
1168 | "__guard",
|
---|
1169 | noStorageClasses,
|
---|
1170 | LinkageSpec::Cforall,
|
---|
1171 | nullptr,
|
---|
1172 | new StructInstType(
|
---|
1173 | noQualifiers,
|
---|
1174 | thread_guard_decl
|
---|
1175 | ),
|
---|
1176 | new ListInit(
|
---|
1177 | {
|
---|
1178 | new SingleInit( new CastExpr( new VariableExpr( arg ), arg_type ) ),
|
---|
1179 | new SingleInit( new UntypedExpr(
|
---|
1180 | new NameExpr( "intptr" ), {
|
---|
1181 | new ConstantExpr( Constant::from_int( 0 ) ),
|
---|
1182 | }
|
---|
1183 | ) ),
|
---|
1184 | },
|
---|
1185 | noDesignators,
|
---|
1186 | true
|
---|
1187 | )
|
---|
1188 | ))
|
---|
1189 | );
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | void MutexKeyword::addStatements( CompoundStmt * body, const std::list<Expression * > & args ) {
|
---|
1193 | ObjectDecl * monitors = new ObjectDecl(
|
---|
1194 | "__monitors",
|
---|
1195 | noStorageClasses,
|
---|
1196 | LinkageSpec::Cforall,
|
---|
1197 | nullptr,
|
---|
1198 | new ArrayType(
|
---|
1199 | noQualifiers,
|
---|
1200 | new PointerType(
|
---|
1201 | noQualifiers,
|
---|
1202 | //new TypeofType( noQualifiers, args.front()->clone() )
|
---|
1203 | new TypeofType( noQualifiers, new UntypedExpr(
|
---|
1204 | new NameExpr( "__get_type" ),
|
---|
1205 | { args.front()->clone() }
|
---|
1206 | )
|
---|
1207 | )
|
---|
1208 | ),
|
---|
1209 | new ConstantExpr( Constant::from_ulong( args.size() ) ),
|
---|
1210 | false,
|
---|
1211 | false
|
---|
1212 | ),
|
---|
1213 | new ListInit(
|
---|
1214 | map_range < std::list<Initializer*> > ( args, [](Expression * var ){
|
---|
1215 | return new SingleInit( new UntypedExpr(
|
---|
1216 | new NameExpr( "__get_ptr" ),
|
---|
1217 | { var }
|
---|
1218 | ) );
|
---|
1219 | //return new SingleInit( new AddressExpr( var ) );
|
---|
1220 | })
|
---|
1221 | )
|
---|
1222 | );
|
---|
1223 |
|
---|
1224 | StructInstType * lock_guard_struct = new StructInstType( noQualifiers, lock_guard_decl );
|
---|
1225 | TypeExpr * lock_type_expr = new TypeExpr(
|
---|
1226 | new TypeofType( noQualifiers, new UntypedExpr(
|
---|
1227 | new NameExpr( "__get_type" ),
|
---|
1228 | { args.front()->clone() }
|
---|
1229 | )
|
---|
1230 | )
|
---|
1231 | );
|
---|
1232 |
|
---|
1233 | lock_guard_struct->parameters.push_back( lock_type_expr ) ;
|
---|
1234 |
|
---|
1235 | // in reverse order :
|
---|
1236 | // monitor_guard_t __guard = { __monitors, # };
|
---|
1237 | body->push_front(
|
---|
1238 | new DeclStmt( new ObjectDecl(
|
---|
1239 | "__guard",
|
---|
1240 | noStorageClasses,
|
---|
1241 | LinkageSpec::Cforall,
|
---|
1242 | nullptr,
|
---|
1243 | lock_guard_struct,
|
---|
1244 | new ListInit(
|
---|
1245 | {
|
---|
1246 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
1247 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
|
---|
1248 | },
|
---|
1249 | noDesignators,
|
---|
1250 | true
|
---|
1251 | )
|
---|
1252 | ))
|
---|
1253 | );
|
---|
1254 |
|
---|
1255 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
1256 | body->push_front( new DeclStmt( monitors) );
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | void MutexKeyword::addStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
1260 | ObjectDecl * monitors = new ObjectDecl(
|
---|
1261 | "__monitors",
|
---|
1262 | noStorageClasses,
|
---|
1263 | LinkageSpec::Cforall,
|
---|
1264 | nullptr,
|
---|
1265 | new ArrayType(
|
---|
1266 | noQualifiers,
|
---|
1267 | new PointerType(
|
---|
1268 | noQualifiers,
|
---|
1269 | new StructInstType(
|
---|
1270 | noQualifiers,
|
---|
1271 | monitor_decl
|
---|
1272 | )
|
---|
1273 | ),
|
---|
1274 | new ConstantExpr( Constant::from_ulong( args.size() ) ),
|
---|
1275 | false,
|
---|
1276 | false
|
---|
1277 | ),
|
---|
1278 | new ListInit(
|
---|
1279 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
|
---|
1280 | Type * type = var->get_type()->clone();
|
---|
1281 | type->set_mutex( false );
|
---|
1282 | return new SingleInit( new UntypedExpr(
|
---|
1283 | new NameExpr( "get_monitor" ),
|
---|
1284 | { new CastExpr( new VariableExpr( var ), type, false ) }
|
---|
1285 | ) );
|
---|
1286 | })
|
---|
1287 | )
|
---|
1288 | );
|
---|
1289 |
|
---|
1290 | assert(generic_func);
|
---|
1291 |
|
---|
1292 | // in reverse order :
|
---|
1293 | // monitor_guard_t __guard = { __monitors, #, func };
|
---|
1294 | body->push_front(
|
---|
1295 | new DeclStmt( new ObjectDecl(
|
---|
1296 | "__guard",
|
---|
1297 | noStorageClasses,
|
---|
1298 | LinkageSpec::Cforall,
|
---|
1299 | nullptr,
|
---|
1300 | new StructInstType(
|
---|
1301 | noQualifiers,
|
---|
1302 | guard_decl
|
---|
1303 | ),
|
---|
1304 | new ListInit(
|
---|
1305 | {
|
---|
1306 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
1307 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
---|
1308 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) )
|
---|
1309 | },
|
---|
1310 | noDesignators,
|
---|
1311 | true
|
---|
1312 | )
|
---|
1313 | ))
|
---|
1314 | );
|
---|
1315 |
|
---|
1316 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
1317 | body->push_front( new DeclStmt( monitors) );
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | //=============================================================================================
|
---|
1321 | // General entry routine
|
---|
1322 | //=============================================================================================
|
---|
1323 | void ThreadStarter::previsit( StructDecl * decl ) {
|
---|
1324 | if( decl->name == "thread$" && decl->body ) {
|
---|
1325 | assert( !thread_decl );
|
---|
1326 | thread_decl = decl;
|
---|
1327 | }
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | void ThreadStarter::postvisit(FunctionDecl * decl) {
|
---|
1331 | if( ! CodeGen::isConstructor(decl->name) ) return;
|
---|
1332 |
|
---|
1333 | Type * typeof_this = InitTweak::getTypeofThis(decl->type);
|
---|
1334 | StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this );
|
---|
1335 | if( ctored_type && ctored_type->baseStruct == thread_decl ) {
|
---|
1336 | thread_ctor_seen = true;
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
---|
1340 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
---|
1341 | if( type && type->get_baseStruct()->is_thread() ) {
|
---|
1342 | if( !thread_decl || !thread_ctor_seen ) {
|
---|
1343 | SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>");
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | addStartStatement( decl, param );
|
---|
1347 | }
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
|
---|
1351 | CompoundStmt * stmt = decl->get_statements();
|
---|
1352 |
|
---|
1353 | if( ! stmt ) return;
|
---|
1354 |
|
---|
1355 | stmt->push_back(
|
---|
1356 | new ExprStmt(
|
---|
1357 | new UntypedExpr(
|
---|
1358 | new NameExpr( "__thrd_start" ),
|
---|
1359 | { new VariableExpr( param ), new NameExpr("main") }
|
---|
1360 | )
|
---|
1361 | )
|
---|
1362 | );
|
---|
1363 | }
|
---|
1364 | };
|
---|
1365 |
|
---|
1366 | // Local Variables: //
|
---|
1367 | // mode: c //
|
---|
1368 | // tab-width: 4 //
|
---|
1369 | // End: //
|
---|
1370 |
|
---|