source: src/Concurrency/Keywords.cc@ 6a36975

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 6a36975 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 17.2 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 * );
202 void addStatments( CompoundStmt *, const std::list<DeclarationWithType * > &);
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;
[64adb03]212 };
213
[bd4d011]214 //-----------------------------------------------------------------------------
215 //Handles mutex routines definitions :
216 // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
217 // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
218 // monitor_guard_t __guard = { __monitors, 2 };
219 // /*Some code*/ => /*Some code*/
220 // } }
221 //
222 class ThreadStarter final : public Visitor {
223 public:
224
225 using Visitor::visit;
226 virtual void visit( FunctionDecl * decl ) override final;
227
228 void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
229
230 static void implement( std::list< Declaration * > & translationUnit ) {
231 ThreadStarter impl;
232 acceptAll( translationUnit, impl );
233 }
234 };
235
[64adb03]236 //=============================================================================================
237 // General entry routine
238 //=============================================================================================
239 void applyKeywords( std::list< Declaration * > & translationUnit ) {
240 ThreadKeyword ::implement( translationUnit );
241 CoroutineKeyword ::implement( translationUnit );
242 MonitorKeyword ::implement( translationUnit );
[bcda04c]243 }
244
245 void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
[64adb03]246 MutexKeyword ::implement( translationUnit );
247 }
248
[bcda04c]249 void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
[bd4d011]250 ThreadStarter ::implement( translationUnit );
[bcda04c]251 }
252
[b32ada31]253 //=============================================================================================
[bcda04c]254 // Generic keyword implementation
[b32ada31]255 //=============================================================================================
[bcda04c]256 void ConcurrentSueKeyword::visit(StructDecl * decl) {
[102a58b]257 Visitor::visit(decl);
[2f9a722]258 if( decl->get_name() == type_name && decl->has_body() ) {
[bcda04c]259 assert( !type_decl );
260 type_decl = decl;
[b32ada31]261 }
[bcda04c]262 else if ( is_target(decl) ) {
[b32ada31]263 handle( decl );
264 }
265
266 }
267
[bcda04c]268 void ConcurrentSueKeyword::handle( StructDecl * decl ) {
[b32ada31]269 if( ! decl->has_body() ) return;
270
[bcda04c]271 if( !type_decl ) throw SemanticError( context_error, decl );
[b32ada31]272
[bcda04c]273 FunctionDecl * func = forwardDeclare( decl );
274 ObjectDecl * field = addField( decl );
[2f9a722]275 addRoutines( field, func );
[b32ada31]276 }
277
[bcda04c]278 FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
279
280 StructDecl * forward = decl->clone();
281 forward->set_body( false );
282 deleteAll( forward->get_members() );
283 forward->get_members().clear();
284
[bd4d011]285 FunctionType * get_type = new FunctionType( noQualifiers, false );
[bcda04c]286 ObjectDecl * this_decl = new ObjectDecl(
287 "this",
[b32ada31]288 noStorage,
289 LinkageSpec::Cforall,
290 nullptr,
[bcda04c]291 new PointerType(
[b32ada31]292 noQualifiers,
[bcda04c]293 new StructInstType(
294 noQualifiers,
295 decl
296 )
[b32ada31]297 ),
298 nullptr
299 );
300
[bd4d011]301 get_type->get_parameters().push_back( this_decl );
302 get_type->get_returnVals().push_back(
[e04b636]303 new ObjectDecl(
304 "ret",
305 noStorage,
306 LinkageSpec::Cforall,
307 nullptr,
308 new PointerType(
309 noQualifiers,
310 new StructInstType(
311 noQualifiers,
[bcda04c]312 type_decl
[e04b636]313 )
314 ),
315 nullptr
316 )
317 );
[b32ada31]318
[bcda04c]319 FunctionDecl * get_decl = new FunctionDecl(
320 getter_name,
321 Type::Static,
322 LinkageSpec::Cforall,
[bd4d011]323 get_type,
[bcda04c]324 nullptr,
325 noAttributes,
326 Type::Inline
327 );
328
[bd4d011]329 FunctionDecl * main_decl = nullptr;
330
331 if( needs_main ) {
332 FunctionType * main_type = new FunctionType( noQualifiers, false );
[bf2438c]333
[bd4d011]334 main_type->get_parameters().push_back( this_decl->clone() );
335
336 main_decl = new FunctionDecl(
337 "main",
338 noStorage,
339 LinkageSpec::Cforall,
340 main_type,
341 nullptr
342 );
343 }
344
[bcda04c]345 declsToAdd.push_back( forward );
[bd4d011]346 if( needs_main ) declsToAdd.push_back( main_decl );
[bcda04c]347 declsToAdd.push_back( get_decl );
348
349 return get_decl;
350 }
351
352 ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
353 ObjectDecl * field = new ObjectDecl(
354 field_name,
355 noStorage,
356 LinkageSpec::Cforall,
357 nullptr,
358 new StructInstType(
359 noQualifiers,
360 type_decl
361 ),
362 nullptr
363 );
364
365 decl->get_members().push_back( field );
366
367 return field;
368 }
369
[2f9a722]370 void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
[b32ada31]371 CompoundStmt * statement = new CompoundStmt( noLabels );
[bf2438c]372 statement->push_back(
[b32ada31]373 new ReturnStmt(
374 noLabels,
[e04b636]375 new AddressExpr(
[bcda04c]376 new MemberExpr(
377 field,
378 UntypedExpr::createDeref( new VariableExpr( func->get_functionType()->get_parameters().front() ) )
[e04b636]379 )
[b32ada31]380 )
381 )
382 );
383
[bcda04c]384 FunctionDecl * get_decl = func->clone();
385
386 get_decl->set_statements( statement );
[e04b636]387
388 declsToAddAfter.push_back( get_decl );
389
[bcda04c]390 // get_decl->fixUniqueId();
[b32ada31]391 }
392
[64adb03]393 //=============================================================================================
394 // Mutex keyword implementation
395 //=============================================================================================
396 void MutexKeyword::visit(FunctionDecl* decl) {
[bf2438c]397 Visitor::visit(decl);
[102a58b]398
[64adb03]399 std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
400 if( mutexArgs.empty() ) return;
401
402 for(auto arg : mutexArgs) {
403 validate( arg );
404 }
405
406 CompoundStmt* body = decl->get_statements();
407 if( ! body ) return;
408
[b32ada31]409 if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
410 if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
411
[64adb03]412 addStatments( body, mutexArgs );
413 }
414
[9243cc91]415 void MutexKeyword::visit(StructDecl* decl) {
[102a58b]416 Visitor::visit(decl);
417
[9243cc91]418 if( decl->get_name() == "monitor_desc" ) {
419 assert( !monitor_decl );
420 monitor_decl = decl;
421 }
[ef42b143]422 else if( decl->get_name() == "monitor_guard_t" ) {
423 assert( !guard_decl );
424 guard_decl = decl;
425 }
[9243cc91]426 }
427
[64adb03]428 std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl ) {
429 std::list<DeclarationWithType*> mutexArgs;
430
431 for( auto arg : decl->get_functionType()->get_parameters()) {
432 //Find mutex arguments
433 Type* ty = arg->get_type();
[615a096]434 if( ! ty->get_mutex() ) continue;
[64adb03]435
436 //Append it to the list
437 mutexArgs.push_back( arg );
438 }
439
440 return mutexArgs;
441 }
442
443 void MutexKeyword::validate( DeclarationWithType * arg ) {
444 Type* ty = arg->get_type();
445
446 //Makes sure it's not a copy
447 PointerType* pty = dynamic_cast< PointerType * >( ty );
448 if( ! pty ) throw SemanticError( "Mutex argument must be of pointer/reference type ", arg );
449
450 //Make sure the we are pointing directly to a type
451 Type* base = pty->get_base();
452 if( dynamic_cast< PointerType * >( base ) ) throw SemanticError( "Mutex argument have exactly one level of indirection ", arg );
453
454 //Make sure that typed isn't mutex
[e04b636]455 if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
[64adb03]456 }
457
458 void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
[9243cc91]459 ObjectDecl * monitors = new ObjectDecl(
460 "__monitors",
461 noStorage,
462 LinkageSpec::Cforall,
463 nullptr,
464 new ArrayType(
465 noQualifiers,
466 new PointerType(
467 noQualifiers,
468 new StructInstType(
469 noQualifiers,
470 monitor_decl
471 )
472 ),
473 new ConstantExpr( Constant::from_ulong( args.size() ) ),
474 false,
475 false
476 ),
477 new ListInit(
[cb0e6de]478 map_range < std::list<Initializer*> > ( args, [this](DeclarationWithType * var ){
479 Type * type = var->get_type()->clone();
480 type->set_mutex( false );
[9243cc91]481 return new SingleInit( new UntypedExpr(
482 new NameExpr( "get_monitor" ),
[cb0e6de]483 { new CastExpr( new VariableExpr( var ), type ) }
[9243cc91]484 ) );
485 })
486 )
487 );
488
[64adb03]489 //in reverse order :
490 // monitor_guard_t __guard = { __monitors, # };
491 body->push_front(
492 new DeclStmt( noLabels, new ObjectDecl(
493 "__guard",
494 noStorage,
495 LinkageSpec::Cforall,
496 nullptr,
497 new StructInstType(
498 noQualifiers,
[ef42b143]499 guard_decl
[64adb03]500 ),
501 new ListInit(
502 {
[9243cc91]503 new SingleInit( new VariableExpr( monitors ) ),
[64adb03]504 new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
[ef42b143]505 },
506 noDesignators,
507 true
[64adb03]508 )
509 ))
510 );
511
[ef42b143]512 //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[9243cc91]513 body->push_front( new DeclStmt( noLabels, monitors) );
[64adb03]514 }
[bd4d011]515
516 //=============================================================================================
517 // General entry routine
518 //=============================================================================================
519 void ThreadStarter::visit(FunctionDecl * decl) {
[102a58b]520 Visitor::visit(decl);
[bf2438c]521
[bd4d011]522 if( ! InitTweak::isConstructor(decl->get_name()) ) return;
523
524 DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
525 auto ptr = dynamic_cast< PointerType * >( param->get_type() );
526 // if( ptr ) std::cerr << "FRED1" << std::endl;
527 auto type = dynamic_cast< StructInstType * >( ptr->get_base() );
528 // if( type ) std::cerr << "FRED2" << std::endl;
529 if( type && type->get_baseStruct()->is_thread() ) {
530 addStartStatement( decl, param );
531 }
532 }
533
534 void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
535 CompoundStmt * stmt = decl->get_statements();
536
537 if( ! stmt ) return;
538
[bf2438c]539 stmt->push_back(
[bd4d011]540 new ExprStmt(
541 noLabels,
542 new UntypedExpr(
543 new NameExpr( "__thrd_start" ),
544 { new VariableExpr( param ) }
545 )
546 )
547 );
548 }
[68fe077a]549};
[6b0b624]550
551// Local Variables: //
552// mode: c //
553// tab-width: 4 //
554// End: //
Note: See TracBrowser for help on using the repository browser.