source: src/Concurrency/Keywords.cc@ 212c421e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 212c421e was 6b224a52, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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