| 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 |
|
|---|
| 35 | class Attribute;
|
|---|
| 36 |
|
|---|
| 37 | namespace 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( FunctionDecl* func, 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 | static std::unique_ptr< Type > generic_func;
|
|---|
| 214 | };
|
|---|
| 215 |
|
|---|
| 216 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
|---|
| 217 | new FunctionType(
|
|---|
| 218 | noQualifiers,
|
|---|
| 219 | true
|
|---|
| 220 | )
|
|---|
| 221 | );
|
|---|
| 222 |
|
|---|
| 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 |
|
|---|
| 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 );
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
|---|
| 255 | MutexKeyword ::implement( translationUnit );
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
|---|
| 259 | ThreadStarter ::implement( translationUnit );
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | //=============================================================================================
|
|---|
| 263 | // Generic keyword implementation
|
|---|
| 264 | //=============================================================================================
|
|---|
| 265 | void ConcurrentSueKeyword::visit(StructDecl * decl) {
|
|---|
| 266 | Visitor::visit(decl);
|
|---|
| 267 | if( decl->get_name() == type_name && decl->has_body() ) {
|
|---|
| 268 | assert( !type_decl );
|
|---|
| 269 | type_decl = decl;
|
|---|
| 270 | }
|
|---|
| 271 | else if ( is_target(decl) ) {
|
|---|
| 272 | handle( decl );
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
|---|
| 278 | if( ! decl->has_body() ) return;
|
|---|
| 279 |
|
|---|
| 280 | if( !type_decl ) throw SemanticError( context_error, decl );
|
|---|
| 281 |
|
|---|
| 282 | FunctionDecl * func = forwardDeclare( decl );
|
|---|
| 283 | ObjectDecl * field = addField( decl );
|
|---|
| 284 | addRoutines( field, func );
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 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 |
|
|---|
| 294 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
|---|
| 295 | ObjectDecl * this_decl = new ObjectDecl(
|
|---|
| 296 | "this",
|
|---|
| 297 | noStorage,
|
|---|
| 298 | LinkageSpec::Cforall,
|
|---|
| 299 | nullptr,
|
|---|
| 300 | new PointerType(
|
|---|
| 301 | noQualifiers,
|
|---|
| 302 | new StructInstType(
|
|---|
| 303 | noQualifiers,
|
|---|
| 304 | decl
|
|---|
| 305 | )
|
|---|
| 306 | ),
|
|---|
| 307 | nullptr
|
|---|
| 308 | );
|
|---|
| 309 |
|
|---|
| 310 | get_type->get_parameters().push_back( this_decl );
|
|---|
| 311 | get_type->get_returnVals().push_back(
|
|---|
| 312 | new ObjectDecl(
|
|---|
| 313 | "ret",
|
|---|
| 314 | noStorage,
|
|---|
| 315 | LinkageSpec::Cforall,
|
|---|
| 316 | nullptr,
|
|---|
| 317 | new PointerType(
|
|---|
| 318 | noQualifiers,
|
|---|
| 319 | new StructInstType(
|
|---|
| 320 | noQualifiers,
|
|---|
| 321 | type_decl
|
|---|
| 322 | )
|
|---|
| 323 | ),
|
|---|
| 324 | nullptr
|
|---|
| 325 | )
|
|---|
| 326 | );
|
|---|
| 327 |
|
|---|
| 328 | FunctionDecl * get_decl = new FunctionDecl(
|
|---|
| 329 | getter_name,
|
|---|
| 330 | Type::Static,
|
|---|
| 331 | LinkageSpec::Cforall,
|
|---|
| 332 | get_type,
|
|---|
| 333 | nullptr,
|
|---|
| 334 | noAttributes,
|
|---|
| 335 | Type::Inline
|
|---|
| 336 | );
|
|---|
| 337 |
|
|---|
| 338 | FunctionDecl * main_decl = nullptr;
|
|---|
| 339 |
|
|---|
| 340 | if( needs_main ) {
|
|---|
| 341 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
|---|
| 342 |
|
|---|
| 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 |
|
|---|
| 354 | declsToAdd.push_back( forward );
|
|---|
| 355 | if( needs_main ) declsToAdd.push_back( main_decl );
|
|---|
| 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 |
|
|---|
| 379 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
|---|
| 380 | CompoundStmt * statement = new CompoundStmt( noLabels );
|
|---|
| 381 | statement->push_back(
|
|---|
| 382 | new ReturnStmt(
|
|---|
| 383 | noLabels,
|
|---|
| 384 | new AddressExpr(
|
|---|
| 385 | new MemberExpr(
|
|---|
| 386 | field,
|
|---|
| 387 | UntypedExpr::createDeref( new VariableExpr( func->get_functionType()->get_parameters().front() ) )
|
|---|
| 388 | )
|
|---|
| 389 | )
|
|---|
| 390 | )
|
|---|
| 391 | );
|
|---|
| 392 |
|
|---|
| 393 | FunctionDecl * get_decl = func->clone();
|
|---|
| 394 |
|
|---|
| 395 | get_decl->set_statements( statement );
|
|---|
| 396 |
|
|---|
| 397 | declsToAddAfter.push_back( get_decl );
|
|---|
| 398 |
|
|---|
| 399 | // get_decl->fixUniqueId();
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | //=============================================================================================
|
|---|
| 403 | // Mutex keyword implementation
|
|---|
| 404 | //=============================================================================================
|
|---|
| 405 |
|
|---|
| 406 | void MutexKeyword::visit(FunctionDecl* decl) {
|
|---|
| 407 | Visitor::visit(decl);
|
|---|
| 408 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 422 | addStatments( decl, body, mutexArgs );
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | void MutexKeyword::visit(StructDecl* decl) {
|
|---|
| 426 | Visitor::visit(decl);
|
|---|
| 427 |
|
|---|
| 428 | if( decl->get_name() == "monitor_desc" ) {
|
|---|
| 429 | assert( !monitor_decl );
|
|---|
| 430 | monitor_decl = decl;
|
|---|
| 431 | }
|
|---|
| 432 | else if( decl->get_name() == "monitor_guard_t" ) {
|
|---|
| 433 | assert( !guard_decl );
|
|---|
| 434 | guard_decl = decl;
|
|---|
| 435 | }
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 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();
|
|---|
| 444 | if( ! ty->get_mutex() ) continue;
|
|---|
| 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
|
|---|
| 465 | if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
|---|
| 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(
|
|---|
| 488 | map_range < std::list<Initializer*> > ( args, [this](DeclarationWithType * var ){
|
|---|
| 489 | Type * type = var->get_type()->clone();
|
|---|
| 490 | type->set_mutex( false );
|
|---|
| 491 | return new SingleInit( new UntypedExpr(
|
|---|
| 492 | new NameExpr( "get_monitor" ),
|
|---|
| 493 | { new CastExpr( new VariableExpr( var ), type ) }
|
|---|
| 494 | ) );
|
|---|
| 495 | })
|
|---|
| 496 | )
|
|---|
| 497 | );
|
|---|
| 498 |
|
|---|
| 499 | assert(generic_func);
|
|---|
| 500 |
|
|---|
| 501 | //in reverse order :
|
|---|
| 502 | // monitor_guard_t __guard = { __monitors, #, func };
|
|---|
| 503 | body->push_front(
|
|---|
| 504 | new DeclStmt( noLabels, new ObjectDecl(
|
|---|
| 505 | "__guard",
|
|---|
| 506 | noStorage,
|
|---|
| 507 | LinkageSpec::Cforall,
|
|---|
| 508 | nullptr,
|
|---|
| 509 | new StructInstType(
|
|---|
| 510 | noQualifiers,
|
|---|
| 511 | guard_decl
|
|---|
| 512 | ),
|
|---|
| 513 | new ListInit(
|
|---|
| 514 | {
|
|---|
| 515 | new SingleInit( new VariableExpr( monitors ) ),
|
|---|
| 516 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
|---|
| 517 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
|
|---|
| 518 | },
|
|---|
| 519 | noDesignators,
|
|---|
| 520 | true
|
|---|
| 521 | )
|
|---|
| 522 | ))
|
|---|
| 523 | );
|
|---|
| 524 |
|
|---|
| 525 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
|---|
| 526 | body->push_front( new DeclStmt( noLabels, monitors) );
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | //=============================================================================================
|
|---|
| 530 | // General entry routine
|
|---|
| 531 | //=============================================================================================
|
|---|
| 532 | void ThreadStarter::visit(FunctionDecl * decl) {
|
|---|
| 533 | Visitor::visit(decl);
|
|---|
| 534 |
|
|---|
| 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 |
|
|---|
| 552 | stmt->push_back(
|
|---|
| 553 | new ExprStmt(
|
|---|
| 554 | noLabels,
|
|---|
| 555 | new UntypedExpr(
|
|---|
| 556 | new NameExpr( "__thrd_start" ),
|
|---|
| 557 | { new VariableExpr( param ) }
|
|---|
| 558 | )
|
|---|
| 559 | )
|
|---|
| 560 | );
|
|---|
| 561 | }
|
|---|
| 562 | };
|
|---|
| 563 |
|
|---|
| 564 | // Local Variables: //
|
|---|
| 565 | // mode: c //
|
|---|
| 566 | // tab-width: 4 //
|
|---|
| 567 | // End: //
|
|---|