source: src/Concurrency/Keywords.cc@ b0440b7

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 b0440b7 was bff227f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Refactor operator predicates into OperatorTable.cc

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