[64adb03] | 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 :
|
---|
[615a096] | 14 | // Update Count : 3
|
---|
[64adb03] | 15 | //
|
---|
| 16 |
|
---|
| 17 | #include "Concurrency/Keywords.h"
|
---|
| 18 |
|
---|
[bf2438c] | 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
|
---|
[bff227f] | 24 | #include "CodeGen/OperatorTable.h" // for isConstructor
|
---|
| 25 | #include "InitTweak/InitTweak.h" // for getPointerBase
|
---|
[bf2438c] | 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 |
|
---|
| 37 | class Attribute;
|
---|
[64adb03] | 38 |
|
---|
| 39 | namespace Concurrency {
|
---|
| 40 |
|
---|
| 41 | namespace {
|
---|
| 42 | const std::list<Label> noLabels;
|
---|
[b32ada31] | 43 | const std::list< Attribute * > noAttributes;
|
---|
[68fe077a] | 44 | Type::StorageClasses noStorage;
|
---|
[64adb03] | 45 | Type::Qualifiers noQualifiers;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | //=============================================================================================
|
---|
| 49 | // Visitors declaration
|
---|
| 50 | //=============================================================================================
|
---|
| 51 |
|
---|
[bcda04c] | 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 |
|
---|
[bd4d011] | 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 ) {}
|
---|
[bcda04c] | 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 * );
|
---|
[2f9a722] | 78 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
---|
[bcda04c] | 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;
|
---|
[bd4d011] | 87 | bool needs_main;
|
---|
[bcda04c] | 88 |
|
---|
| 89 | std::list< Declaration * > declsToAdd, declsToAddAfter;
|
---|
| 90 | StructDecl* type_decl = nullptr;
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 |
|
---|
[64adb03] | 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 | //
|
---|
[bcda04c] | 103 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 104 | public:
|
---|
| 105 |
|
---|
[bcda04c] | 106 | ThreadKeyword() : ConcurrentSueKeyword(
|
---|
| 107 | "thread_desc",
|
---|
| 108 | "__thrd",
|
---|
| 109 | "get_thread",
|
---|
[bd4d011] | 110 | "thread keyword requires threads to be in scope, add #include <thread>",
|
---|
| 111 | true
|
---|
[bcda04c] | 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 | }
|
---|
[64adb03] | 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 | //
|
---|
[bcda04c] | 134 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 135 | public:
|
---|
| 136 |
|
---|
[bcda04c] | 137 | CoroutineKeyword() : ConcurrentSueKeyword(
|
---|
| 138 | "coroutine_desc",
|
---|
| 139 | "__cor",
|
---|
| 140 | "get_coroutine",
|
---|
[bd4d011] | 141 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
|
---|
| 142 | true
|
---|
[bcda04c] | 143 | )
|
---|
| 144 | {}
|
---|
[b32ada31] | 145 |
|
---|
[bcda04c] | 146 | virtual ~CoroutineKeyword() {}
|
---|
| 147 |
|
---|
| 148 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
---|
[b32ada31] | 149 |
|
---|
| 150 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
| 151 | CoroutineKeyword impl;
|
---|
| 152 | SymTab::acceptAndAdd( translationUnit, impl );
|
---|
| 153 | }
|
---|
[64adb03] | 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 | //
|
---|
[bcda04c] | 165 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 166 | public:
|
---|
| 167 |
|
---|
[bcda04c] | 168 | MonitorKeyword() : ConcurrentSueKeyword(
|
---|
| 169 | "monitor_desc",
|
---|
| 170 | "__mon",
|
---|
| 171 | "get_monitor",
|
---|
[bd4d011] | 172 | "monitor keyword requires monitors to be in scope, add #include <monitor>",
|
---|
| 173 | false
|
---|
[bcda04c] | 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 | }
|
---|
[64adb03] | 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 ) {
|
---|
[9243cc91] | 190 | // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[64adb03] | 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;
|
---|
[b32ada31] | 199 | virtual void visit( FunctionDecl * decl ) override final;
|
---|
| 200 | virtual void visit( StructDecl * decl ) override final;
|
---|
[64adb03] | 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 | }
|
---|
[9243cc91] | 210 |
|
---|
| 211 | private:
|
---|
| 212 | StructDecl* monitor_decl = nullptr;
|
---|
[ef42b143] | 213 | StructDecl* guard_decl = nullptr;
|
---|
[64adb03] | 214 | };
|
---|
| 215 |
|
---|
[bd4d011] | 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 |
|
---|
[64adb03] | 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 );
|
---|
[bcda04c] | 245 | }
|
---|
| 246 |
|
---|
| 247 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
---|
[64adb03] | 248 | MutexKeyword ::implement( translationUnit );
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[bcda04c] | 251 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
---|
[bd4d011] | 252 | ThreadStarter ::implement( translationUnit );
|
---|
[bcda04c] | 253 | }
|
---|
| 254 |
|
---|
[b32ada31] | 255 | //=============================================================================================
|
---|
[bcda04c] | 256 | // Generic keyword implementation
|
---|
[b32ada31] | 257 | //=============================================================================================
|
---|
[bcda04c] | 258 | void ConcurrentSueKeyword::visit(StructDecl * decl) {
|
---|
[102a58b] | 259 | Visitor::visit(decl);
|
---|
[2f9a722] | 260 | if( decl->get_name() == type_name && decl->has_body() ) {
|
---|
[bcda04c] | 261 | assert( !type_decl );
|
---|
| 262 | type_decl = decl;
|
---|
[b32ada31] | 263 | }
|
---|
[bcda04c] | 264 | else if ( is_target(decl) ) {
|
---|
[b32ada31] | 265 | handle( decl );
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[bcda04c] | 270 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
---|
[b32ada31] | 271 | if( ! decl->has_body() ) return;
|
---|
| 272 |
|
---|
[bcda04c] | 273 | if( !type_decl ) throw SemanticError( context_error, decl );
|
---|
[b32ada31] | 274 |
|
---|
[bcda04c] | 275 | FunctionDecl * func = forwardDeclare( decl );
|
---|
| 276 | ObjectDecl * field = addField( decl );
|
---|
[2f9a722] | 277 | addRoutines( field, func );
|
---|
[b32ada31] | 278 | }
|
---|
| 279 |
|
---|
[bcda04c] | 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 |
|
---|
[bd4d011] | 287 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
---|
[bcda04c] | 288 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
| 289 | "this",
|
---|
[b32ada31] | 290 | noStorage,
|
---|
| 291 | LinkageSpec::Cforall,
|
---|
| 292 | nullptr,
|
---|
[bcda04c] | 293 | new PointerType(
|
---|
[b32ada31] | 294 | noQualifiers,
|
---|
[bcda04c] | 295 | new StructInstType(
|
---|
| 296 | noQualifiers,
|
---|
| 297 | decl
|
---|
| 298 | )
|
---|
[b32ada31] | 299 | ),
|
---|
| 300 | nullptr
|
---|
| 301 | );
|
---|
| 302 |
|
---|
[bd4d011] | 303 | get_type->get_parameters().push_back( this_decl );
|
---|
| 304 | get_type->get_returnVals().push_back(
|
---|
[e04b636] | 305 | new ObjectDecl(
|
---|
| 306 | "ret",
|
---|
| 307 | noStorage,
|
---|
| 308 | LinkageSpec::Cforall,
|
---|
| 309 | nullptr,
|
---|
| 310 | new PointerType(
|
---|
| 311 | noQualifiers,
|
---|
| 312 | new StructInstType(
|
---|
| 313 | noQualifiers,
|
---|
[bcda04c] | 314 | type_decl
|
---|
[e04b636] | 315 | )
|
---|
| 316 | ),
|
---|
| 317 | nullptr
|
---|
| 318 | )
|
---|
| 319 | );
|
---|
[b32ada31] | 320 |
|
---|
[bcda04c] | 321 | FunctionDecl * get_decl = new FunctionDecl(
|
---|
| 322 | getter_name,
|
---|
| 323 | Type::Static,
|
---|
| 324 | LinkageSpec::Cforall,
|
---|
[bd4d011] | 325 | get_type,
|
---|
[bcda04c] | 326 | nullptr,
|
---|
| 327 | noAttributes,
|
---|
| 328 | Type::Inline
|
---|
| 329 | );
|
---|
| 330 |
|
---|
[bd4d011] | 331 | FunctionDecl * main_decl = nullptr;
|
---|
| 332 |
|
---|
| 333 | if( needs_main ) {
|
---|
| 334 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
---|
[ce8c12f] | 335 |
|
---|
[bd4d011] | 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 |
|
---|
[bcda04c] | 347 | declsToAdd.push_back( forward );
|
---|
[bd4d011] | 348 | if( needs_main ) declsToAdd.push_back( main_decl );
|
---|
[bcda04c] | 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 |
|
---|
[2f9a722] | 372 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
---|
[b32ada31] | 373 | CompoundStmt * statement = new CompoundStmt( noLabels );
|
---|
[ce8c12f] | 374 | statement->push_back(
|
---|
[b32ada31] | 375 | new ReturnStmt(
|
---|
| 376 | noLabels,
|
---|
[e04b636] | 377 | new AddressExpr(
|
---|
[bcda04c] | 378 | new MemberExpr(
|
---|
| 379 | field,
|
---|
| 380 | UntypedExpr::createDeref( new VariableExpr( func->get_functionType()->get_parameters().front() ) )
|
---|
[e04b636] | 381 | )
|
---|
[b32ada31] | 382 | )
|
---|
| 383 | )
|
---|
| 384 | );
|
---|
| 385 |
|
---|
[bcda04c] | 386 | FunctionDecl * get_decl = func->clone();
|
---|
| 387 |
|
---|
| 388 | get_decl->set_statements( statement );
|
---|
[e04b636] | 389 |
|
---|
| 390 | declsToAddAfter.push_back( get_decl );
|
---|
| 391 |
|
---|
[bcda04c] | 392 | // get_decl->fixUniqueId();
|
---|
[b32ada31] | 393 | }
|
---|
| 394 |
|
---|
[64adb03] | 395 | //=============================================================================================
|
---|
| 396 | // Mutex keyword implementation
|
---|
| 397 | //=============================================================================================
|
---|
| 398 | void MutexKeyword::visit(FunctionDecl* decl) {
|
---|
[ce8c12f] | 399 | Visitor::visit(decl);
|
---|
[102a58b] | 400 |
|
---|
[64adb03] | 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 |
|
---|
[b32ada31] | 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 |
|
---|
[64adb03] | 414 | addStatments( body, mutexArgs );
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[9243cc91] | 417 | void MutexKeyword::visit(StructDecl* decl) {
|
---|
[102a58b] | 418 | Visitor::visit(decl);
|
---|
| 419 |
|
---|
[9243cc91] | 420 | if( decl->get_name() == "monitor_desc" ) {
|
---|
| 421 | assert( !monitor_decl );
|
---|
| 422 | monitor_decl = decl;
|
---|
| 423 | }
|
---|
[ef42b143] | 424 | else if( decl->get_name() == "monitor_guard_t" ) {
|
---|
| 425 | assert( !guard_decl );
|
---|
| 426 | guard_decl = decl;
|
---|
| 427 | }
|
---|
[9243cc91] | 428 | }
|
---|
| 429 |
|
---|
[64adb03] | 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();
|
---|
[615a096] | 436 | if( ! ty->get_mutex() ) continue;
|
---|
[64adb03] | 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 );
|
---|
[870d1f0] | 450 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
| 451 | if( ! pty && ! rty ) throw SemanticError( "Mutex argument must be of pointer/reference type ", arg );
|
---|
[64adb03] | 452 |
|
---|
| 453 | //Make sure the we are pointing directly to a type
|
---|
[870d1f0] | 454 | Type* base = pty ? pty->get_base() : rty->get_base();
|
---|
[64adb03] | 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
|
---|
[e04b636] | 458 | if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
|
---|
[64adb03] | 459 | }
|
---|
| 460 |
|
---|
| 461 | void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
[9243cc91] | 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(
|
---|
[cb0e6de] | 481 | map_range < std::list<Initializer*> > ( args, [this](DeclarationWithType * var ){
|
---|
| 482 | Type * type = var->get_type()->clone();
|
---|
| 483 | type->set_mutex( false );
|
---|
[9243cc91] | 484 | return new SingleInit( new UntypedExpr(
|
---|
| 485 | new NameExpr( "get_monitor" ),
|
---|
[cb0e6de] | 486 | { new CastExpr( new VariableExpr( var ), type ) }
|
---|
[9243cc91] | 487 | ) );
|
---|
| 488 | })
|
---|
| 489 | )
|
---|
| 490 | );
|
---|
| 491 |
|
---|
[64adb03] | 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,
|
---|
[ef42b143] | 502 | guard_decl
|
---|
[64adb03] | 503 | ),
|
---|
| 504 | new ListInit(
|
---|
| 505 | {
|
---|
[9243cc91] | 506 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
[64adb03] | 507 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
|
---|
[ef42b143] | 508 | },
|
---|
| 509 | noDesignators,
|
---|
| 510 | true
|
---|
[64adb03] | 511 | )
|
---|
| 512 | ))
|
---|
| 513 | );
|
---|
| 514 |
|
---|
[ef42b143] | 515 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[9243cc91] | 516 | body->push_front( new DeclStmt( noLabels, monitors) );
|
---|
[64adb03] | 517 | }
|
---|
[bd4d011] | 518 |
|
---|
| 519 | //=============================================================================================
|
---|
| 520 | // General entry routine
|
---|
| 521 | //=============================================================================================
|
---|
| 522 | void ThreadStarter::visit(FunctionDecl * decl) {
|
---|
[102a58b] | 523 | Visitor::visit(decl);
|
---|
[ce8c12f] | 524 |
|
---|
[bff227f] | 525 | if( ! CodeGen::isConstructor(decl->get_name()) ) return;
|
---|
[bd4d011] | 526 |
|
---|
| 527 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
---|
[ce8c12f] | 528 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
---|
[bd4d011] | 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 |
|
---|
[ce8c12f] | 540 | stmt->push_back(
|
---|
[bd4d011] | 541 | new ExprStmt(
|
---|
| 542 | noLabels,
|
---|
| 543 | new UntypedExpr(
|
---|
| 544 | new NameExpr( "__thrd_start" ),
|
---|
| 545 | { new VariableExpr( param ) }
|
---|
| 546 | )
|
---|
| 547 | )
|
---|
| 548 | );
|
---|
| 549 | }
|
---|
[68fe077a] | 550 | };
|
---|