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