source: src/Concurrency/Keywords.cc@ 90c4df0

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 stuck-waitfor-destruct with_gc
Last change on this file since 90c4df0 was 97e3296, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

First working implementation of external scheduling... Still lots of testing to do

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