source: src/Concurrency/Keywords.cc@ b947fb2

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 b947fb2 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
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 "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;
36
37namespace Concurrency {
38
39 namespace {
40 const std::list<Label> noLabels;
41 const std::list< Attribute * > noAttributes;
42 Type::StorageClasses noStorage;
43 Type::Qualifiers noQualifiers;
44 }
45
46 //=============================================================================================
47 // Visitors declaration
48 //=============================================================================================
49
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
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 ) {}
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 * );
76 void addRoutines( ObjectDecl *, FunctionDecl * );
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;
85 bool needs_main;
86
87 std::list< Declaration * > declsToAdd, declsToAddAfter;
88 StructDecl* type_decl = nullptr;
89 };
90
91
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 //
101 class ThreadKeyword final : public ConcurrentSueKeyword {
102 public:
103
104 ThreadKeyword() : ConcurrentSueKeyword(
105 "thread_desc",
106 "__thrd",
107 "get_thread",
108 "thread keyword requires threads to be in scope, add #include <thread>",
109 true
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 }
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 //
132 class CoroutineKeyword final : public ConcurrentSueKeyword {
133 public:
134
135 CoroutineKeyword() : ConcurrentSueKeyword(
136 "coroutine_desc",
137 "__cor",
138 "get_coroutine",
139 "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
140 true
141 )
142 {}
143
144 virtual ~CoroutineKeyword() {}
145
146 virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
147
148 static void implement( std::list< Declaration * > & translationUnit ) {
149 CoroutineKeyword impl;
150 SymTab::acceptAndAdd( translationUnit, impl );
151 }
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 //
163 class MonitorKeyword final : public ConcurrentSueKeyword {
164 public:
165
166 MonitorKeyword() : ConcurrentSueKeyword(
167 "monitor_desc",
168 "__mon",
169 "get_monitor",
170 "monitor keyword requires monitors to be in scope, add #include <monitor>",
171 false
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 }
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 ) {
188 // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
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;
197 virtual void visit( FunctionDecl * decl ) override final;
198 virtual void visit( StructDecl * decl ) override final;
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 }
208
209 private:
210 StructDecl* monitor_decl = nullptr;
211 StructDecl* guard_decl = nullptr;
212 };
213
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
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 );
243 }
244
245 void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
246 MutexKeyword ::implement( translationUnit );
247 }
248
249 void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
250 ThreadStarter ::implement( translationUnit );
251 }
252
253 //=============================================================================================
254 // Generic keyword implementation
255 //=============================================================================================
256 void ConcurrentSueKeyword::visit(StructDecl * decl) {
257 Visitor::visit(decl);
258 if( decl->get_name() == type_name && decl->has_body() ) {
259 assert( !type_decl );
260 type_decl = decl;
261 }
262 else if ( is_target(decl) ) {
263 handle( decl );
264 }
265
266 }
267
268 void ConcurrentSueKeyword::handle( StructDecl * decl ) {
269 if( ! decl->has_body() ) return;
270
271 if( !type_decl ) throw SemanticError( context_error, decl );
272
273 FunctionDecl * func = forwardDeclare( decl );
274 ObjectDecl * field = addField( decl );
275 addRoutines( field, func );
276 }
277
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
285 FunctionType * get_type = new FunctionType( noQualifiers, false );
286 ObjectDecl * this_decl = new ObjectDecl(
287 "this",
288 noStorage,
289 LinkageSpec::Cforall,
290 nullptr,
291 new PointerType(
292 noQualifiers,
293 new StructInstType(
294 noQualifiers,
295 decl
296 )
297 ),
298 nullptr
299 );
300
301 get_type->get_parameters().push_back( this_decl );
302 get_type->get_returnVals().push_back(
303 new ObjectDecl(
304 "ret",
305 noStorage,
306 LinkageSpec::Cforall,
307 nullptr,
308 new PointerType(
309 noQualifiers,
310 new StructInstType(
311 noQualifiers,
312 type_decl
313 )
314 ),
315 nullptr
316 )
317 );
318
319 FunctionDecl * get_decl = new FunctionDecl(
320 getter_name,
321 Type::Static,
322 LinkageSpec::Cforall,
323 get_type,
324 nullptr,
325 noAttributes,
326 Type::Inline
327 );
328
329 FunctionDecl * main_decl = nullptr;
330
331 if( needs_main ) {
332 FunctionType * main_type = new FunctionType( noQualifiers, false );
333
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
345 declsToAdd.push_back( forward );
346 if( needs_main ) declsToAdd.push_back( main_decl );
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
370 void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
371 CompoundStmt * statement = new CompoundStmt( noLabels );
372 statement->push_back(
373 new ReturnStmt(
374 noLabels,
375 new AddressExpr(
376 new MemberExpr(
377 field,
378 UntypedExpr::createDeref( new VariableExpr( func->get_functionType()->get_parameters().front() ) )
379 )
380 )
381 )
382 );
383
384 FunctionDecl * get_decl = func->clone();
385
386 get_decl->set_statements( statement );
387
388 declsToAddAfter.push_back( get_decl );
389
390 // get_decl->fixUniqueId();
391 }
392
393 //=============================================================================================
394 // Mutex keyword implementation
395 //=============================================================================================
396 void MutexKeyword::visit(FunctionDecl* decl) {
397 Visitor::visit(decl);
398
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
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
412 addStatments( body, mutexArgs );
413 }
414
415 void MutexKeyword::visit(StructDecl* decl) {
416 Visitor::visit(decl);
417
418 if( decl->get_name() == "monitor_desc" ) {
419 assert( !monitor_decl );
420 monitor_decl = decl;
421 }
422 else if( decl->get_name() == "monitor_guard_t" ) {
423 assert( !guard_decl );
424 guard_decl = decl;
425 }
426 }
427
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();
434 if( ! ty->get_mutex() ) continue;
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
455 if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
456 }
457
458 void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
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(
478 map_range < std::list<Initializer*> > ( args, [this](DeclarationWithType * var ){
479 Type * type = var->get_type()->clone();
480 type->set_mutex( false );
481 return new SingleInit( new UntypedExpr(
482 new NameExpr( "get_monitor" ),
483 { new CastExpr( new VariableExpr( var ), type ) }
484 ) );
485 })
486 )
487 );
488
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,
499 guard_decl
500 ),
501 new ListInit(
502 {
503 new SingleInit( new VariableExpr( monitors ) ),
504 new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
505 },
506 noDesignators,
507 true
508 )
509 ))
510 );
511
512 //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
513 body->push_front( new DeclStmt( noLabels, monitors) );
514 }
515
516 //=============================================================================================
517 // General entry routine
518 //=============================================================================================
519 void ThreadStarter::visit(FunctionDecl * decl) {
520 Visitor::visit(decl);
521
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
539 stmt->push_back(
540 new ExprStmt(
541 noLabels,
542 new UntypedExpr(
543 new NameExpr( "__thrd_start" ),
544 { new VariableExpr( param ) }
545 )
546 )
547 );
548 }
549};
550
551// Local Variables: //
552// mode: c //
553// tab-width: 4 //
554// End: //
Note: See TracBrowser for help on using the repository browser.