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 : 5
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "Concurrency/Keywords.h"
|
---|
17 |
|
---|
18 | #include <cassert> // for assert
|
---|
19 | #include <string> // for string, operator==
|
---|
20 |
|
---|
21 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
22 | #include "Common/SemanticError.h" // for SemanticError
|
---|
23 | #include "Common/utility.h" // for deleteAll, map_range
|
---|
24 | #include "CodeGen/OperatorTable.h" // for isConstructor
|
---|
25 | #include "InitTweak/InitTweak.h" // for getPointerBase
|
---|
26 | #include "Parser/LinkageSpec.h" // for Cforall
|
---|
27 | #include "SymTab/AddVisit.h" // for acceptAndAdd
|
---|
28 | #include "SynTree/Constant.h" // for Constant
|
---|
29 | #include "SynTree/Declaration.h" // for StructDecl, FunctionDecl, ObjectDecl
|
---|
30 | #include "SynTree/Expression.h" // for VariableExpr, ConstantExpr, Untype...
|
---|
31 | #include "SynTree/Initializer.h" // for SingleInit, ListInit, Initializer ...
|
---|
32 | #include "SynTree/Label.h" // for Label
|
---|
33 | #include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt
|
---|
34 | #include "SynTree/Type.h" // for StructInstType, Type, PointerType
|
---|
35 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
36 |
|
---|
37 | class Attribute;
|
---|
38 |
|
---|
39 | namespace Concurrency {
|
---|
40 | //=============================================================================================
|
---|
41 | // Pass declarations
|
---|
42 | //=============================================================================================
|
---|
43 |
|
---|
44 | //-----------------------------------------------------------------------------
|
---|
45 | //Handles sue type declarations :
|
---|
46 | // sue MyType { struct MyType {
|
---|
47 | // int data; int data;
|
---|
48 | // a_struct_t more_data; a_struct_t more_data;
|
---|
49 | // => NewField_t newField;
|
---|
50 | // }; };
|
---|
51 | // static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
|
---|
52 | //
|
---|
53 | class ConcurrentSueKeyword : public WithDeclsToAdd {
|
---|
54 | public:
|
---|
55 |
|
---|
56 | ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main ) :
|
---|
57 | type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ) {}
|
---|
58 |
|
---|
59 | virtual ~ConcurrentSueKeyword() {}
|
---|
60 |
|
---|
61 | void postvisit( StructDecl * decl );
|
---|
62 |
|
---|
63 | void handle( StructDecl * );
|
---|
64 | FunctionDecl * forwardDeclare( StructDecl * );
|
---|
65 | ObjectDecl * addField( StructDecl * );
|
---|
66 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
---|
67 |
|
---|
68 | virtual bool is_target( StructDecl * decl ) = 0;
|
---|
69 |
|
---|
70 | private:
|
---|
71 | const std::string type_name;
|
---|
72 | const std::string field_name;
|
---|
73 | const std::string getter_name;
|
---|
74 | const std::string context_error;
|
---|
75 | bool needs_main;
|
---|
76 |
|
---|
77 | StructDecl* type_decl = nullptr;
|
---|
78 | };
|
---|
79 |
|
---|
80 |
|
---|
81 | //-----------------------------------------------------------------------------
|
---|
82 | //Handles thread type declarations :
|
---|
83 | // thread Mythread { struct MyThread {
|
---|
84 | // int data; int data;
|
---|
85 | // a_struct_t more_data; a_struct_t more_data;
|
---|
86 | // => thread_desc __thrd_d;
|
---|
87 | // }; };
|
---|
88 | // static inline thread_desc * get_thread( MyThread * this ) { return &this->__thrd_d; }
|
---|
89 | //
|
---|
90 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
---|
91 | public:
|
---|
92 |
|
---|
93 | ThreadKeyword() : ConcurrentSueKeyword(
|
---|
94 | "thread_desc",
|
---|
95 | "__thrd",
|
---|
96 | "get_thread",
|
---|
97 | "thread keyword requires threads to be in scope, add #include <thread>",
|
---|
98 | true
|
---|
99 | )
|
---|
100 | {}
|
---|
101 |
|
---|
102 | virtual ~ThreadKeyword() {}
|
---|
103 |
|
---|
104 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
|
---|
105 |
|
---|
106 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
107 | PassVisitor< ThreadKeyword > impl;
|
---|
108 | acceptAll( translationUnit, impl );
|
---|
109 | }
|
---|
110 | };
|
---|
111 |
|
---|
112 | //-----------------------------------------------------------------------------
|
---|
113 | //Handles coroutine type declarations :
|
---|
114 | // coroutine MyCoroutine { struct MyCoroutine {
|
---|
115 | // int data; int data;
|
---|
116 | // a_struct_t more_data; a_struct_t more_data;
|
---|
117 | // => coroutine_desc __cor_d;
|
---|
118 | // }; };
|
---|
119 | // static inline coroutine_desc * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
|
---|
120 | //
|
---|
121 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
---|
122 | public:
|
---|
123 |
|
---|
124 | CoroutineKeyword() : ConcurrentSueKeyword(
|
---|
125 | "coroutine_desc",
|
---|
126 | "__cor",
|
---|
127 | "get_coroutine",
|
---|
128 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
|
---|
129 | true
|
---|
130 | )
|
---|
131 | {}
|
---|
132 |
|
---|
133 | virtual ~CoroutineKeyword() {}
|
---|
134 |
|
---|
135 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
---|
136 |
|
---|
137 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
138 | PassVisitor< CoroutineKeyword > impl;
|
---|
139 | acceptAll( translationUnit, impl );
|
---|
140 | }
|
---|
141 | };
|
---|
142 |
|
---|
143 | //-----------------------------------------------------------------------------
|
---|
144 | //Handles monitor type declarations :
|
---|
145 | // monitor MyMonitor { struct MyMonitor {
|
---|
146 | // int data; int data;
|
---|
147 | // a_struct_t more_data; a_struct_t more_data;
|
---|
148 | // => monitor_desc __mon_d;
|
---|
149 | // }; };
|
---|
150 | // static inline monitor_desc * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
|
---|
151 | //
|
---|
152 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
---|
153 | public:
|
---|
154 |
|
---|
155 | MonitorKeyword() : ConcurrentSueKeyword(
|
---|
156 | "monitor_desc",
|
---|
157 | "__mon",
|
---|
158 | "get_monitor",
|
---|
159 | "monitor keyword requires monitors to be in scope, add #include <monitor>",
|
---|
160 | false
|
---|
161 | )
|
---|
162 | {}
|
---|
163 |
|
---|
164 | virtual ~MonitorKeyword() {}
|
---|
165 |
|
---|
166 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
|
---|
167 |
|
---|
168 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
169 | PassVisitor< MonitorKeyword > impl;
|
---|
170 | acceptAll( translationUnit, impl );
|
---|
171 | }
|
---|
172 | };
|
---|
173 |
|
---|
174 | //-----------------------------------------------------------------------------
|
---|
175 | //Handles mutex routines definitions :
|
---|
176 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
177 | // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
178 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
179 | // /*Some code*/ => /*Some code*/
|
---|
180 | // } }
|
---|
181 | //
|
---|
182 | class MutexKeyword final {
|
---|
183 | public:
|
---|
184 |
|
---|
185 | void postvisit( FunctionDecl * decl );
|
---|
186 | void postvisit( StructDecl * decl );
|
---|
187 |
|
---|
188 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
|
---|
189 | void validate( DeclarationWithType * );
|
---|
190 | void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
191 | void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
192 |
|
---|
193 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
194 | PassVisitor< MutexKeyword > impl;
|
---|
195 | acceptAll( translationUnit, impl );
|
---|
196 | }
|
---|
197 |
|
---|
198 | private:
|
---|
199 | StructDecl* monitor_decl = nullptr;
|
---|
200 | StructDecl* guard_decl = nullptr;
|
---|
201 | StructDecl* dtor_guard_decl = nullptr;
|
---|
202 |
|
---|
203 | static std::unique_ptr< Type > generic_func;
|
---|
204 | };
|
---|
205 |
|
---|
206 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
---|
207 | new FunctionType(
|
---|
208 | noQualifiers,
|
---|
209 | true
|
---|
210 | )
|
---|
211 | );
|
---|
212 |
|
---|
213 | //-----------------------------------------------------------------------------
|
---|
214 | //Handles mutex routines definitions :
|
---|
215 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
216 | // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
217 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
218 | // /*Some code*/ => /*Some code*/
|
---|
219 | // } }
|
---|
220 | //
|
---|
221 | class ThreadStarter final {
|
---|
222 | public:
|
---|
223 |
|
---|
224 | void postvisit( FunctionDecl * decl );
|
---|
225 | void previsit ( StructDecl * decl );
|
---|
226 |
|
---|
227 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
|
---|
228 |
|
---|
229 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
230 | PassVisitor< ThreadStarter > impl;
|
---|
231 | acceptAll( translationUnit, impl );
|
---|
232 | }
|
---|
233 |
|
---|
234 | private :
|
---|
235 | bool thread_ctor_seen = false;
|
---|
236 | StructDecl * thread_decl = nullptr;
|
---|
237 | };
|
---|
238 |
|
---|
239 | //=============================================================================================
|
---|
240 | // General entry routine
|
---|
241 | //=============================================================================================
|
---|
242 | void applyKeywords( std::list< Declaration * > & translationUnit ) {
|
---|
243 | ThreadKeyword ::implement( translationUnit );
|
---|
244 | CoroutineKeyword ::implement( translationUnit );
|
---|
245 | MonitorKeyword ::implement( translationUnit );
|
---|
246 | }
|
---|
247 |
|
---|
248 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
---|
249 | MutexKeyword ::implement( translationUnit );
|
---|
250 | }
|
---|
251 |
|
---|
252 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
---|
253 | ThreadStarter ::implement( translationUnit );
|
---|
254 | }
|
---|
255 |
|
---|
256 | //=============================================================================================
|
---|
257 | // Generic keyword implementation
|
---|
258 | //=============================================================================================
|
---|
259 | void fixupGenerics(FunctionType * func, StructDecl * decl) {
|
---|
260 | cloneAll(decl->parameters, func->forall);
|
---|
261 | for ( TypeDecl * td : func->forall ) {
|
---|
262 | strict_dynamic_cast<StructInstType*>(
|
---|
263 | func->parameters.front()->get_type()->stripReferences()
|
---|
264 | )->parameters.push_back(
|
---|
265 | new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) )
|
---|
266 | );
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | void ConcurrentSueKeyword::postvisit(StructDecl * decl) {
|
---|
271 | if( decl->name == type_name && decl->body ) {
|
---|
272 | assert( !type_decl );
|
---|
273 | type_decl = decl;
|
---|
274 | }
|
---|
275 | else if ( is_target(decl) ) {
|
---|
276 | handle( decl );
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
---|
281 | if( ! decl->body ) return;
|
---|
282 |
|
---|
283 | if( !type_decl ) throw SemanticError( decl, context_error );
|
---|
284 |
|
---|
285 | FunctionDecl * func = forwardDeclare( decl );
|
---|
286 | ObjectDecl * field = addField( decl );
|
---|
287 | addRoutines( field, func );
|
---|
288 | }
|
---|
289 |
|
---|
290 | FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
|
---|
291 |
|
---|
292 | StructDecl * forward = decl->clone();
|
---|
293 | forward->set_body( false );
|
---|
294 | deleteAll( forward->get_members() );
|
---|
295 | forward->get_members().clear();
|
---|
296 |
|
---|
297 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
---|
298 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
299 | "this",
|
---|
300 | noStorageClasses,
|
---|
301 | LinkageSpec::Cforall,
|
---|
302 | nullptr,
|
---|
303 | new ReferenceType(
|
---|
304 | noQualifiers,
|
---|
305 | new StructInstType(
|
---|
306 | noQualifiers,
|
---|
307 | decl
|
---|
308 | )
|
---|
309 | ),
|
---|
310 | nullptr
|
---|
311 | );
|
---|
312 |
|
---|
313 | get_type->get_parameters().push_back( this_decl->clone() );
|
---|
314 | get_type->get_returnVals().push_back(
|
---|
315 | new ObjectDecl(
|
---|
316 | "ret",
|
---|
317 | noStorageClasses,
|
---|
318 | LinkageSpec::Cforall,
|
---|
319 | nullptr,
|
---|
320 | new PointerType(
|
---|
321 | noQualifiers,
|
---|
322 | new StructInstType(
|
---|
323 | noQualifiers,
|
---|
324 | type_decl
|
---|
325 | )
|
---|
326 | ),
|
---|
327 | nullptr
|
---|
328 | )
|
---|
329 | );
|
---|
330 | fixupGenerics(get_type, decl);
|
---|
331 |
|
---|
332 | FunctionDecl * get_decl = new FunctionDecl(
|
---|
333 | getter_name,
|
---|
334 | Type::Static,
|
---|
335 | LinkageSpec::Cforall,
|
---|
336 | get_type,
|
---|
337 | nullptr,
|
---|
338 | noAttributes,
|
---|
339 | Type::Inline
|
---|
340 | );
|
---|
341 |
|
---|
342 | FunctionDecl * main_decl = nullptr;
|
---|
343 |
|
---|
344 | if( needs_main ) {
|
---|
345 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
---|
346 |
|
---|
347 | main_type->get_parameters().push_back( this_decl->clone() );
|
---|
348 |
|
---|
349 | main_decl = new FunctionDecl(
|
---|
350 | "main",
|
---|
351 | noStorageClasses,
|
---|
352 | LinkageSpec::Cforall,
|
---|
353 | main_type,
|
---|
354 | nullptr
|
---|
355 | );
|
---|
356 | fixupGenerics(main_type, decl);
|
---|
357 | }
|
---|
358 |
|
---|
359 | delete this_decl;
|
---|
360 |
|
---|
361 | declsToAddBefore.push_back( forward );
|
---|
362 | if( needs_main ) declsToAddBefore.push_back( main_decl );
|
---|
363 | declsToAddBefore.push_back( get_decl );
|
---|
364 |
|
---|
365 | return get_decl;
|
---|
366 | }
|
---|
367 |
|
---|
368 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
|
---|
369 | ObjectDecl * field = new ObjectDecl(
|
---|
370 | field_name,
|
---|
371 | noStorageClasses,
|
---|
372 | LinkageSpec::Cforall,
|
---|
373 | nullptr,
|
---|
374 | new StructInstType(
|
---|
375 | noQualifiers,
|
---|
376 | type_decl
|
---|
377 | ),
|
---|
378 | nullptr
|
---|
379 | );
|
---|
380 |
|
---|
381 | decl->get_members().push_back( field );
|
---|
382 |
|
---|
383 | return field;
|
---|
384 | }
|
---|
385 |
|
---|
386 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
---|
387 | CompoundStmt * statement = new CompoundStmt();
|
---|
388 | statement->push_back(
|
---|
389 | new ReturnStmt(
|
---|
390 | new AddressExpr(
|
---|
391 | new MemberExpr(
|
---|
392 | field,
|
---|
393 | new CastExpr(
|
---|
394 | new VariableExpr( func->get_functionType()->get_parameters().front() ),
|
---|
395 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone()
|
---|
396 | )
|
---|
397 | )
|
---|
398 | )
|
---|
399 | )
|
---|
400 | );
|
---|
401 |
|
---|
402 | FunctionDecl * get_decl = func->clone();
|
---|
403 |
|
---|
404 | get_decl->set_statements( statement );
|
---|
405 |
|
---|
406 | declsToAddAfter.push_back( get_decl );
|
---|
407 |
|
---|
408 | // get_decl->fixUniqueId();
|
---|
409 | }
|
---|
410 |
|
---|
411 | //=============================================================================================
|
---|
412 | // Mutex keyword implementation
|
---|
413 | //=============================================================================================
|
---|
414 |
|
---|
415 | void MutexKeyword::postvisit(FunctionDecl* decl) {
|
---|
416 |
|
---|
417 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
|
---|
418 | if( mutexArgs.empty() ) return;
|
---|
419 |
|
---|
420 | if( CodeGen::isConstructor(decl->name) ) throw SemanticError( decl, "constructors cannot have mutex parameters" );
|
---|
421 |
|
---|
422 | bool isDtor = CodeGen::isDestructor( decl->name );
|
---|
423 |
|
---|
424 | if( isDtor && mutexArgs.size() != 1 ) throw SemanticError( decl, "destructors can only have 1 mutex argument" );
|
---|
425 |
|
---|
426 | for(auto arg : mutexArgs) {
|
---|
427 | validate( arg );
|
---|
428 | }
|
---|
429 |
|
---|
430 | CompoundStmt* body = decl->get_statements();
|
---|
431 | if( ! body ) return;
|
---|
432 |
|
---|
433 | if( !monitor_decl ) throw SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>" );
|
---|
434 | if( !guard_decl ) throw SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>" );
|
---|
435 | if( !dtor_guard_decl ) throw SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>" );
|
---|
436 |
|
---|
437 | if( isDtor ) {
|
---|
438 | addDtorStatments( decl, body, mutexArgs );
|
---|
439 | }
|
---|
440 | else {
|
---|
441 | addStatments( decl, body, mutexArgs );
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | void MutexKeyword::postvisit(StructDecl* decl) {
|
---|
446 |
|
---|
447 | if( decl->name == "monitor_desc" ) {
|
---|
448 | assert( !monitor_decl );
|
---|
449 | monitor_decl = decl;
|
---|
450 | }
|
---|
451 | else if( decl->name == "monitor_guard_t" ) {
|
---|
452 | assert( !guard_decl );
|
---|
453 | guard_decl = decl;
|
---|
454 | }
|
---|
455 | else if( decl->name == "monitor_dtor_guard_t" ) {
|
---|
456 | assert( !dtor_guard_decl );
|
---|
457 | dtor_guard_decl = decl;
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl ) {
|
---|
462 | std::list<DeclarationWithType*> mutexArgs;
|
---|
463 |
|
---|
464 | for( auto arg : decl->get_functionType()->get_parameters()) {
|
---|
465 | //Find mutex arguments
|
---|
466 | Type* ty = arg->get_type();
|
---|
467 | if( ! ty->get_mutex() ) continue;
|
---|
468 |
|
---|
469 | //Append it to the list
|
---|
470 | mutexArgs.push_back( arg );
|
---|
471 | }
|
---|
472 |
|
---|
473 | return mutexArgs;
|
---|
474 | }
|
---|
475 |
|
---|
476 | void MutexKeyword::validate( DeclarationWithType * arg ) {
|
---|
477 | Type* ty = arg->get_type();
|
---|
478 |
|
---|
479 | //Makes sure it's not a copy
|
---|
480 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
481 | if( ! rty ) throw SemanticError( arg, "Mutex argument must be of reference type " );
|
---|
482 |
|
---|
483 | //Make sure the we are pointing directly to a type
|
---|
484 | Type* base = rty->get_base();
|
---|
485 | if( dynamic_cast< ReferenceType * >( base ) ) throw SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
486 | if( dynamic_cast< PointerType * >( base ) ) throw SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
487 |
|
---|
488 | //Make sure that typed isn't mutex
|
---|
489 | if( base->get_mutex() ) throw SemanticError( arg, "mutex keyword may only appear once per argument " );
|
---|
490 | }
|
---|
491 |
|
---|
492 | void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
493 | Type * arg_type = args.front()->get_type()->clone();
|
---|
494 | arg_type->set_mutex( false );
|
---|
495 |
|
---|
496 | ObjectDecl * monitors = new ObjectDecl(
|
---|
497 | "__monitor",
|
---|
498 | noStorageClasses,
|
---|
499 | LinkageSpec::Cforall,
|
---|
500 | nullptr,
|
---|
501 | new PointerType(
|
---|
502 | noQualifiers,
|
---|
503 | new StructInstType(
|
---|
504 | noQualifiers,
|
---|
505 | monitor_decl
|
---|
506 | )
|
---|
507 | ),
|
---|
508 | new SingleInit( new UntypedExpr(
|
---|
509 | new NameExpr( "get_monitor" ),
|
---|
510 | { new CastExpr( new VariableExpr( args.front() ), arg_type ) }
|
---|
511 | ))
|
---|
512 | );
|
---|
513 |
|
---|
514 | assert(generic_func);
|
---|
515 |
|
---|
516 | //in reverse order :
|
---|
517 | // monitor_guard_t __guard = { __monitors, #, func };
|
---|
518 | body->push_front(
|
---|
519 | new DeclStmt( new ObjectDecl(
|
---|
520 | "__guard",
|
---|
521 | noStorageClasses,
|
---|
522 | LinkageSpec::Cforall,
|
---|
523 | nullptr,
|
---|
524 | new StructInstType(
|
---|
525 | noQualifiers,
|
---|
526 | dtor_guard_decl
|
---|
527 | ),
|
---|
528 | new ListInit(
|
---|
529 | {
|
---|
530 | new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ),
|
---|
531 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
|
---|
532 | },
|
---|
533 | noDesignators,
|
---|
534 | true
|
---|
535 | )
|
---|
536 | ))
|
---|
537 | );
|
---|
538 |
|
---|
539 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
540 | body->push_front( new DeclStmt( monitors) );
|
---|
541 | }
|
---|
542 |
|
---|
543 | void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
544 | ObjectDecl * monitors = new ObjectDecl(
|
---|
545 | "__monitors",
|
---|
546 | noStorageClasses,
|
---|
547 | LinkageSpec::Cforall,
|
---|
548 | nullptr,
|
---|
549 | new ArrayType(
|
---|
550 | noQualifiers,
|
---|
551 | new PointerType(
|
---|
552 | noQualifiers,
|
---|
553 | new StructInstType(
|
---|
554 | noQualifiers,
|
---|
555 | monitor_decl
|
---|
556 | )
|
---|
557 | ),
|
---|
558 | new ConstantExpr( Constant::from_ulong( args.size() ) ),
|
---|
559 | false,
|
---|
560 | false
|
---|
561 | ),
|
---|
562 | new ListInit(
|
---|
563 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
|
---|
564 | Type * type = var->get_type()->clone();
|
---|
565 | type->set_mutex( false );
|
---|
566 | return new SingleInit( new UntypedExpr(
|
---|
567 | new NameExpr( "get_monitor" ),
|
---|
568 | { new CastExpr( new VariableExpr( var ), type ) }
|
---|
569 | ) );
|
---|
570 | })
|
---|
571 | )
|
---|
572 | );
|
---|
573 |
|
---|
574 | assert(generic_func);
|
---|
575 |
|
---|
576 | //in reverse order :
|
---|
577 | // monitor_guard_t __guard = { __monitors, #, func };
|
---|
578 | body->push_front(
|
---|
579 | new DeclStmt( new ObjectDecl(
|
---|
580 | "__guard",
|
---|
581 | noStorageClasses,
|
---|
582 | LinkageSpec::Cforall,
|
---|
583 | nullptr,
|
---|
584 | new StructInstType(
|
---|
585 | noQualifiers,
|
---|
586 | guard_decl
|
---|
587 | ),
|
---|
588 | new ListInit(
|
---|
589 | {
|
---|
590 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
591 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
---|
592 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
|
---|
593 | },
|
---|
594 | noDesignators,
|
---|
595 | true
|
---|
596 | )
|
---|
597 | ))
|
---|
598 | );
|
---|
599 |
|
---|
600 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
601 | body->push_front( new DeclStmt( monitors) );
|
---|
602 | }
|
---|
603 |
|
---|
604 | //=============================================================================================
|
---|
605 | // General entry routine
|
---|
606 | //=============================================================================================
|
---|
607 | void ThreadStarter::previsit( StructDecl * decl ) {
|
---|
608 | if( decl->name == "thread_desc" && decl->body ) {
|
---|
609 | assert( !thread_decl );
|
---|
610 | thread_decl = decl;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | void ThreadStarter::postvisit(FunctionDecl * decl) {
|
---|
615 | if( ! CodeGen::isConstructor(decl->name) ) return;
|
---|
616 |
|
---|
617 | Type * typeof_this = InitTweak::getTypeofThis(decl->type);
|
---|
618 | StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this );
|
---|
619 | if( ctored_type && ctored_type->baseStruct == thread_decl ) {
|
---|
620 | thread_ctor_seen = true;
|
---|
621 | }
|
---|
622 |
|
---|
623 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
---|
624 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
---|
625 | if( type && type->get_baseStruct()->is_thread() ) {
|
---|
626 | if( !thread_decl || !thread_ctor_seen ) {
|
---|
627 | throw SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread>");
|
---|
628 | }
|
---|
629 |
|
---|
630 | addStartStatement( decl, param );
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
|
---|
635 | CompoundStmt * stmt = decl->get_statements();
|
---|
636 |
|
---|
637 | if( ! stmt ) return;
|
---|
638 |
|
---|
639 | stmt->push_back(
|
---|
640 | new ExprStmt(
|
---|
641 | new UntypedExpr(
|
---|
642 | new NameExpr( "__thrd_start" ),
|
---|
643 | { new VariableExpr( param ) }
|
---|
644 | )
|
---|
645 | )
|
---|
646 | );
|
---|
647 | }
|
---|
648 | };
|
---|
649 |
|
---|
650 | // Local Variables: //
|
---|
651 | // mode: c //
|
---|
652 | // tab-width: 4 //
|
---|
653 | // End: //
|
---|