[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 : |
---|
[07de76b] | 13 | // Update Count : 10 |
---|
[64adb03] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Concurrency/Keywords.h" |
---|
| 17 | |
---|
[427854b] | 18 | #include <cassert> // for assert |
---|
| 19 | #include <string> // for string, operator== |
---|
| 20 | |
---|
[1c01c58] | 21 | #include <iostream> |
---|
| 22 | |
---|
| 23 | #include "Common/Examine.h" // for isMainFor |
---|
[427854b] | 24 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
| 25 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 26 | #include "Common/utility.h" // for deleteAll, map_range |
---|
| 27 | #include "CodeGen/OperatorTable.h" // for isConstructor |
---|
| 28 | #include "ControlStruct/LabelGenerator.h" // for LebelGenerator |
---|
| 29 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 30 | #include "SynTree/LinkageSpec.h" // for Cforall |
---|
| 31 | #include "SynTree/Constant.h" // for Constant |
---|
| 32 | #include "SynTree/Declaration.h" // for StructDecl, FunctionDecl, ObjectDecl |
---|
| 33 | #include "SynTree/Expression.h" // for VariableExpr, ConstantExpr, Untype... |
---|
| 34 | #include "SynTree/Initializer.h" // for SingleInit, ListInit, Initializer ... |
---|
| 35 | #include "SynTree/Label.h" // for Label |
---|
| 36 | #include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt |
---|
| 37 | #include "SynTree/Type.h" // for StructInstType, Type, PointerType |
---|
| 38 | #include "SynTree/Visitor.h" // for Visitor, acceptAll |
---|
[1c01c58] | 39 | #include "Virtual/Tables.h" |
---|
[bf2438c] | 40 | |
---|
| 41 | class Attribute; |
---|
[64adb03] | 42 | |
---|
| 43 | namespace Concurrency { |
---|
[ecfd758] | 44 | inline static std::string getTypeIdName( std::string const & exception_name ) { |
---|
| 45 | return exception_name.empty() ? std::string() : Virtual::typeIdType( exception_name ); |
---|
| 46 | } |
---|
[1c01c58] | 47 | inline static std::string getVTableName( std::string const & exception_name ) { |
---|
[ecfd758] | 48 | return exception_name.empty() ? std::string() : Virtual::vtableTypeName( exception_name ); |
---|
[1c01c58] | 49 | } |
---|
| 50 | |
---|
[ab8c6a6] | 51 | // Only detects threads constructed with the keyword thread. |
---|
| 52 | inline static bool isThread( DeclarationWithType * decl ) { |
---|
| 53 | Type * baseType = decl->get_type()->stripDeclarator(); |
---|
| 54 | StructInstType * instType = dynamic_cast<StructInstType *>( baseType ); |
---|
| 55 | if ( nullptr == instType ) { return false; } |
---|
| 56 | return instType->baseStruct->is_thread(); |
---|
| 57 | } |
---|
| 58 | |
---|
[64adb03] | 59 | //============================================================================================= |
---|
[2065609] | 60 | // Pass declarations |
---|
[64adb03] | 61 | //============================================================================================= |
---|
| 62 | |
---|
[bcda04c] | 63 | //----------------------------------------------------------------------------- |
---|
| 64 | //Handles sue type declarations : |
---|
| 65 | // sue MyType { struct MyType { |
---|
| 66 | // int data; int data; |
---|
| 67 | // a_struct_t more_data; a_struct_t more_data; |
---|
| 68 | // => NewField_t newField; |
---|
| 69 | // }; }; |
---|
| 70 | // static inline NewField_t * getter_name( MyType * this ) { return &this->newField; } |
---|
| 71 | // |
---|
[2065609] | 72 | class ConcurrentSueKeyword : public WithDeclsToAdd { |
---|
[bcda04c] | 73 | public: |
---|
| 74 | |
---|
[1c01c58] | 75 | ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, |
---|
| 76 | std::string&& getter_name, std::string&& context_error, std::string&& exception_name, |
---|
| 77 | bool needs_main, AggregateDecl::Aggregate cast_target ) : |
---|
| 78 | type_name( type_name ), field_name( field_name ), getter_name( getter_name ), |
---|
[69c5c00] | 79 | context_error( context_error ), exception_name( exception_name ), |
---|
[ecfd758] | 80 | typeid_name( getTypeIdName( exception_name ) ), |
---|
[69c5c00] | 81 | vtable_name( getVTableName( exception_name ) ), |
---|
[1c01c58] | 82 | needs_main( needs_main ), cast_target( cast_target ) {} |
---|
[bcda04c] | 83 | |
---|
| 84 | virtual ~ConcurrentSueKeyword() {} |
---|
| 85 | |
---|
[9a705dc8] | 86 | Declaration * postmutate( StructDecl * decl ); |
---|
[6c3a5ac1] | 87 | DeclarationWithType * postmutate( FunctionDecl * decl ); |
---|
[bcda04c] | 88 | |
---|
| 89 | void handle( StructDecl * ); |
---|
[ecfd758] | 90 | void addTypeId( StructDecl * ); |
---|
[1c01c58] | 91 | void addVtableForward( StructDecl * ); |
---|
[bcda04c] | 92 | FunctionDecl * forwardDeclare( StructDecl * ); |
---|
| 93 | ObjectDecl * addField( StructDecl * ); |
---|
[2f9a722] | 94 | void addRoutines( ObjectDecl *, FunctionDecl * ); |
---|
[af67ee1] | 95 | void addLockUnlockRoutines( StructDecl * ); |
---|
[bcda04c] | 96 | |
---|
| 97 | virtual bool is_target( StructDecl * decl ) = 0; |
---|
| 98 | |
---|
[9a705dc8] | 99 | Expression * postmutate( KeywordCastExpr * cast ); |
---|
| 100 | |
---|
[bcda04c] | 101 | private: |
---|
| 102 | const std::string type_name; |
---|
| 103 | const std::string field_name; |
---|
| 104 | const std::string getter_name; |
---|
| 105 | const std::string context_error; |
---|
[69c5c00] | 106 | const std::string exception_name; |
---|
[ecfd758] | 107 | const std::string typeid_name; |
---|
[1c01c58] | 108 | const std::string vtable_name; |
---|
[bd4d011] | 109 | bool needs_main; |
---|
[312029a] | 110 | AggregateDecl::Aggregate cast_target; |
---|
[bcda04c] | 111 | |
---|
[6c3a5ac1] | 112 | StructDecl * type_decl = nullptr; |
---|
| 113 | FunctionDecl * dtor_decl = nullptr; |
---|
[69c5c00] | 114 | StructDecl * except_decl = nullptr; |
---|
[ecfd758] | 115 | StructDecl * typeid_decl = nullptr; |
---|
[1c01c58] | 116 | StructDecl * vtable_decl = nullptr; |
---|
[bcda04c] | 117 | }; |
---|
| 118 | |
---|
| 119 | |
---|
[64adb03] | 120 | //----------------------------------------------------------------------------- |
---|
| 121 | //Handles thread type declarations : |
---|
| 122 | // thread Mythread { struct MyThread { |
---|
| 123 | // int data; int data; |
---|
| 124 | // a_struct_t more_data; a_struct_t more_data; |
---|
[e84ab3d] | 125 | // => thread$ __thrd_d; |
---|
[64adb03] | 126 | // }; }; |
---|
[e84ab3d] | 127 | // static inline thread$ * get_thread( MyThread * this ) { return &this->__thrd_d; } |
---|
[64adb03] | 128 | // |
---|
[bcda04c] | 129 | class ThreadKeyword final : public ConcurrentSueKeyword { |
---|
[64adb03] | 130 | public: |
---|
| 131 | |
---|
[bcda04c] | 132 | ThreadKeyword() : ConcurrentSueKeyword( |
---|
[e84ab3d] | 133 | "thread$", |
---|
[bcda04c] | 134 | "__thrd", |
---|
| 135 | "get_thread", |
---|
[6c3a5ac1] | 136 | "thread keyword requires threads to be in scope, add #include <thread.hfa>\n", |
---|
[ab8c6a6] | 137 | "ThreadCancelled", |
---|
[9a705dc8] | 138 | true, |
---|
[312029a] | 139 | AggregateDecl::Thread |
---|
[bcda04c] | 140 | ) |
---|
| 141 | {} |
---|
| 142 | |
---|
| 143 | virtual ~ThreadKeyword() {} |
---|
| 144 | |
---|
| 145 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); } |
---|
| 146 | |
---|
| 147 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
[2065609] | 148 | PassVisitor< ThreadKeyword > impl; |
---|
[9a705dc8] | 149 | mutateAll( translationUnit, impl ); |
---|
[bcda04c] | 150 | } |
---|
[64adb03] | 151 | }; |
---|
| 152 | |
---|
| 153 | //----------------------------------------------------------------------------- |
---|
| 154 | //Handles coroutine type declarations : |
---|
| 155 | // coroutine MyCoroutine { struct MyCoroutine { |
---|
| 156 | // int data; int data; |
---|
| 157 | // a_struct_t more_data; a_struct_t more_data; |
---|
[e84ab3d] | 158 | // => coroutine$ __cor_d; |
---|
[64adb03] | 159 | // }; }; |
---|
[e84ab3d] | 160 | // static inline coroutine$ * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; } |
---|
[64adb03] | 161 | // |
---|
[bcda04c] | 162 | class CoroutineKeyword final : public ConcurrentSueKeyword { |
---|
[64adb03] | 163 | public: |
---|
| 164 | |
---|
[bcda04c] | 165 | CoroutineKeyword() : ConcurrentSueKeyword( |
---|
[e84ab3d] | 166 | "coroutine$", |
---|
[bcda04c] | 167 | "__cor", |
---|
| 168 | "get_coroutine", |
---|
[6c3a5ac1] | 169 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n", |
---|
[1c01c58] | 170 | "CoroutineCancelled", |
---|
[9a705dc8] | 171 | true, |
---|
[312029a] | 172 | AggregateDecl::Coroutine |
---|
[bcda04c] | 173 | ) |
---|
| 174 | {} |
---|
[b32ada31] | 175 | |
---|
[bcda04c] | 176 | virtual ~CoroutineKeyword() {} |
---|
| 177 | |
---|
| 178 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); } |
---|
[b32ada31] | 179 | |
---|
| 180 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
[2065609] | 181 | PassVisitor< CoroutineKeyword > impl; |
---|
[9a705dc8] | 182 | mutateAll( translationUnit, impl ); |
---|
[b32ada31] | 183 | } |
---|
[64adb03] | 184 | }; |
---|
| 185 | |
---|
[427854b] | 186 | |
---|
| 187 | |
---|
[64adb03] | 188 | //----------------------------------------------------------------------------- |
---|
| 189 | //Handles monitor type declarations : |
---|
| 190 | // monitor MyMonitor { struct MyMonitor { |
---|
| 191 | // int data; int data; |
---|
| 192 | // a_struct_t more_data; a_struct_t more_data; |
---|
[e84ab3d] | 193 | // => monitor$ __mon_d; |
---|
[64adb03] | 194 | // }; }; |
---|
[e84ab3d] | 195 | // static inline monitor$ * get_coroutine( MyMonitor * this ) { return &this->__cor_d; } |
---|
[64adb03] | 196 | // |
---|
[bcda04c] | 197 | class MonitorKeyword final : public ConcurrentSueKeyword { |
---|
[64adb03] | 198 | public: |
---|
| 199 | |
---|
[bcda04c] | 200 | MonitorKeyword() : ConcurrentSueKeyword( |
---|
[e84ab3d] | 201 | "monitor$", |
---|
[bcda04c] | 202 | "__mon", |
---|
| 203 | "get_monitor", |
---|
[6c3a5ac1] | 204 | "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n", |
---|
[1c01c58] | 205 | "", |
---|
[9a705dc8] | 206 | false, |
---|
[312029a] | 207 | AggregateDecl::Monitor |
---|
[bcda04c] | 208 | ) |
---|
| 209 | {} |
---|
| 210 | |
---|
| 211 | virtual ~MonitorKeyword() {} |
---|
| 212 | |
---|
| 213 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); } |
---|
| 214 | |
---|
| 215 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
[2065609] | 216 | PassVisitor< MonitorKeyword > impl; |
---|
[9a705dc8] | 217 | mutateAll( translationUnit, impl ); |
---|
[bcda04c] | 218 | } |
---|
[64adb03] | 219 | }; |
---|
| 220 | |
---|
[427854b] | 221 | //----------------------------------------------------------------------------- |
---|
| 222 | //Handles generator type declarations : |
---|
| 223 | // generator MyGenerator { struct MyGenerator { |
---|
| 224 | // int data; int data; |
---|
| 225 | // a_struct_t more_data; a_struct_t more_data; |
---|
| 226 | // => int __gen_next; |
---|
| 227 | // }; }; |
---|
| 228 | // |
---|
| 229 | class GeneratorKeyword final : public ConcurrentSueKeyword { |
---|
| 230 | public: |
---|
| 231 | |
---|
| 232 | GeneratorKeyword() : ConcurrentSueKeyword( |
---|
[e84ab3d] | 233 | "generator$", |
---|
[427854b] | 234 | "__generator_state", |
---|
| 235 | "get_generator", |
---|
[e84ab3d] | 236 | "Unable to find builtin type generator$\n", |
---|
[1c01c58] | 237 | "", |
---|
[427854b] | 238 | true, |
---|
| 239 | AggregateDecl::Generator |
---|
| 240 | ) |
---|
| 241 | {} |
---|
| 242 | |
---|
| 243 | virtual ~GeneratorKeyword() {} |
---|
| 244 | |
---|
| 245 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_generator(); } |
---|
| 246 | |
---|
| 247 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
| 248 | PassVisitor< GeneratorKeyword > impl; |
---|
| 249 | mutateAll( translationUnit, impl ); |
---|
| 250 | } |
---|
| 251 | }; |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | //----------------------------------------------------------------------------- |
---|
| 255 | class SuspendKeyword final : public WithStmtsToAdd, public WithGuards { |
---|
| 256 | public: |
---|
| 257 | SuspendKeyword() = default; |
---|
| 258 | virtual ~SuspendKeyword() = default; |
---|
| 259 | |
---|
| 260 | void premutate( FunctionDecl * ); |
---|
| 261 | DeclarationWithType * postmutate( FunctionDecl * ); |
---|
| 262 | |
---|
| 263 | Statement * postmutate( SuspendStmt * ); |
---|
| 264 | |
---|
| 265 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
| 266 | PassVisitor< SuspendKeyword > impl; |
---|
| 267 | mutateAll( translationUnit, impl ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | private: |
---|
| 271 | bool is_real_suspend( FunctionDecl * ); |
---|
| 272 | |
---|
| 273 | Statement * make_generator_suspend( SuspendStmt * ); |
---|
| 274 | Statement * make_coroutine_suspend( SuspendStmt * ); |
---|
| 275 | |
---|
| 276 | struct LabelPair { |
---|
| 277 | Label obj; |
---|
| 278 | int idx; |
---|
| 279 | }; |
---|
| 280 | |
---|
| 281 | LabelPair make_label() { |
---|
| 282 | labels.push_back( gen.newLabel("generator") ); |
---|
| 283 | return { labels.back(), int(labels.size()) }; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | DeclarationWithType * in_generator = nullptr; |
---|
| 287 | FunctionDecl * decl_suspend = nullptr; |
---|
| 288 | std::vector<Label> labels; |
---|
| 289 | ControlStruct::LabelGenerator & gen = *ControlStruct::LabelGenerator::getGenerator(); |
---|
| 290 | }; |
---|
| 291 | |
---|
[64adb03] | 292 | //----------------------------------------------------------------------------- |
---|
| 293 | //Handles mutex routines definitions : |
---|
| 294 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) { |
---|
[e84ab3d] | 295 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) }; |
---|
[64adb03] | 296 | // monitor_guard_t __guard = { __monitors, 2 }; |
---|
| 297 | // /*Some code*/ => /*Some code*/ |
---|
| 298 | // } } |
---|
| 299 | // |
---|
[2065609] | 300 | class MutexKeyword final { |
---|
[64adb03] | 301 | public: |
---|
| 302 | |
---|
[2065609] | 303 | void postvisit( FunctionDecl * decl ); |
---|
| 304 | void postvisit( StructDecl * decl ); |
---|
[6cebfef] | 305 | Statement * postmutate( MutexStmt * stmt ); |
---|
[64adb03] | 306 | |
---|
[db4d8e3] | 307 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first ); |
---|
[64adb03] | 308 | void validate( DeclarationWithType * ); |
---|
[ab8c6a6] | 309 | void addDtorStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); |
---|
| 310 | void addStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &); |
---|
[6cebfef] | 311 | void addStatements( CompoundStmt * body, const std::list<Expression * > & args ); |
---|
[ab8c6a6] | 312 | void addThreadDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ); |
---|
[64adb03] | 313 | |
---|
| 314 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
[2065609] | 315 | PassVisitor< MutexKeyword > impl; |
---|
[64adb03] | 316 | acceptAll( translationUnit, impl ); |
---|
[6cebfef] | 317 | mutateAll( translationUnit, impl ); |
---|
[64adb03] | 318 | } |
---|
[9243cc91] | 319 | |
---|
| 320 | private: |
---|
| 321 | StructDecl* monitor_decl = nullptr; |
---|
[ef42b143] | 322 | StructDecl* guard_decl = nullptr; |
---|
[549c006] | 323 | StructDecl* dtor_guard_decl = nullptr; |
---|
[ab8c6a6] | 324 | StructDecl* thread_guard_decl = nullptr; |
---|
[af67ee1] | 325 | StructDecl* lock_guard_decl = nullptr; |
---|
[97e3296] | 326 | |
---|
| 327 | static std::unique_ptr< Type > generic_func; |
---|
[64adb03] | 328 | }; |
---|
| 329 | |
---|
[97e3296] | 330 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >( |
---|
| 331 | new FunctionType( |
---|
| 332 | noQualifiers, |
---|
| 333 | true |
---|
| 334 | ) |
---|
| 335 | ); |
---|
| 336 | |
---|
[bd4d011] | 337 | //----------------------------------------------------------------------------- |
---|
| 338 | //Handles mutex routines definitions : |
---|
| 339 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) { |
---|
[e84ab3d] | 340 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) }; |
---|
[bd4d011] | 341 | // monitor_guard_t __guard = { __monitors, 2 }; |
---|
| 342 | // /*Some code*/ => /*Some code*/ |
---|
| 343 | // } } |
---|
| 344 | // |
---|
[2065609] | 345 | class ThreadStarter final { |
---|
[bd4d011] | 346 | public: |
---|
| 347 | |
---|
[2065609] | 348 | void postvisit( FunctionDecl * decl ); |
---|
[549c006] | 349 | void previsit ( StructDecl * decl ); |
---|
[bd4d011] | 350 | |
---|
| 351 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param ); |
---|
| 352 | |
---|
| 353 | static void implement( std::list< Declaration * > & translationUnit ) { |
---|
[2065609] | 354 | PassVisitor< ThreadStarter > impl; |
---|
[bd4d011] | 355 | acceptAll( translationUnit, impl ); |
---|
| 356 | } |
---|
[549c006] | 357 | |
---|
| 358 | private : |
---|
| 359 | bool thread_ctor_seen = false; |
---|
| 360 | StructDecl * thread_decl = nullptr; |
---|
[bd4d011] | 361 | }; |
---|
| 362 | |
---|
[64adb03] | 363 | //============================================================================================= |
---|
| 364 | // General entry routine |
---|
| 365 | //============================================================================================= |
---|
| 366 | void applyKeywords( std::list< Declaration * > & translationUnit ) { |
---|
| 367 | ThreadKeyword ::implement( translationUnit ); |
---|
| 368 | CoroutineKeyword ::implement( translationUnit ); |
---|
| 369 | MonitorKeyword ::implement( translationUnit ); |
---|
[427854b] | 370 | GeneratorKeyword ::implement( translationUnit ); |
---|
| 371 | SuspendKeyword ::implement( translationUnit ); |
---|
[bcda04c] | 372 | } |
---|
| 373 | |
---|
| 374 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) { |
---|
[64adb03] | 375 | MutexKeyword ::implement( translationUnit ); |
---|
| 376 | } |
---|
| 377 | |
---|
[bcda04c] | 378 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) { |
---|
[bd4d011] | 379 | ThreadStarter ::implement( translationUnit ); |
---|
[bcda04c] | 380 | } |
---|
| 381 | |
---|
[b32ada31] | 382 | //============================================================================================= |
---|
[bcda04c] | 383 | // Generic keyword implementation |
---|
[b32ada31] | 384 | //============================================================================================= |
---|
[2db79e5] | 385 | void fixupGenerics(FunctionType * func, StructDecl * decl) { |
---|
| 386 | cloneAll(decl->parameters, func->forall); |
---|
| 387 | for ( TypeDecl * td : func->forall ) { |
---|
| 388 | strict_dynamic_cast<StructInstType*>( |
---|
| 389 | func->parameters.front()->get_type()->stripReferences() |
---|
| 390 | )->parameters.push_back( |
---|
| 391 | new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) ) |
---|
| 392 | ); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | |
---|
[9a705dc8] | 396 | Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) { |
---|
[9f5ecf5] | 397 | if( decl->name == type_name && decl->body ) { |
---|
[bcda04c] | 398 | assert( !type_decl ); |
---|
| 399 | type_decl = decl; |
---|
[b32ada31] | 400 | } |
---|
[bcda04c] | 401 | else if ( is_target(decl) ) { |
---|
[b32ada31] | 402 | handle( decl ); |
---|
| 403 | } |
---|
[69c5c00] | 404 | else if ( !except_decl && exception_name == decl->name && decl->body ) { |
---|
| 405 | except_decl = decl; |
---|
| 406 | } |
---|
[ecfd758] | 407 | else if ( !typeid_decl && typeid_name == decl->name && decl->body ) { |
---|
| 408 | typeid_decl = decl; |
---|
| 409 | } |
---|
[1c01c58] | 410 | else if ( !vtable_decl && vtable_name == decl->name && decl->body ) { |
---|
| 411 | vtable_decl = decl; |
---|
| 412 | } |
---|
| 413 | // Might be able to get ride of is target. |
---|
| 414 | assert( is_target(decl) == (cast_target == decl->kind) ); |
---|
[9a705dc8] | 415 | return decl; |
---|
[b32ada31] | 416 | } |
---|
| 417 | |
---|
[6c3a5ac1] | 418 | DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) { |
---|
[1c01c58] | 419 | if ( type_decl && isDestructorFor( decl, type_decl ) ) |
---|
| 420 | dtor_decl = decl; |
---|
[b583113] | 421 | else if ( vtable_name.empty() || !decl->has_body() ) |
---|
[43cedfb1] | 422 | ; |
---|
[1c01c58] | 423 | else if ( auto param = isMainFor( decl, cast_target ) ) { |
---|
[a494d105] | 424 | if ( !vtable_decl ) { |
---|
| 425 | SemanticError( decl, context_error ); |
---|
| 426 | } |
---|
[1c01c58] | 427 | // Should be safe because of isMainFor. |
---|
| 428 | StructInstType * struct_type = static_cast<StructInstType *>( |
---|
| 429 | static_cast<ReferenceType *>( param->get_type() )->base ); |
---|
| 430 | assert( struct_type ); |
---|
| 431 | |
---|
[69c5c00] | 432 | std::list< Expression * > poly_args = { new TypeExpr( struct_type->clone() ) }; |
---|
| 433 | ObjectDecl * vtable_object = Virtual::makeVtableInstance( |
---|
[b583113] | 434 | "_default_vtable_object_declaration", |
---|
[69c5c00] | 435 | vtable_decl->makeInst( poly_args ), struct_type, nullptr ); |
---|
| 436 | declsToAddAfter.push_back( vtable_object ); |
---|
[b583113] | 437 | declsToAddAfter.push_back( |
---|
| 438 | new ObjectDecl( |
---|
| 439 | Virtual::concurrentDefaultVTableName(), |
---|
[67b421c] | 440 | noStorageClasses, |
---|
[b583113] | 441 | LinkageSpec::Cforall, |
---|
| 442 | /* bitfieldWidth */ nullptr, |
---|
| 443 | new ReferenceType( Type::Const, vtable_object->type->clone() ), |
---|
| 444 | new SingleInit( new VariableExpr( vtable_object ) ) |
---|
| 445 | ) |
---|
| 446 | ); |
---|
[69c5c00] | 447 | declsToAddAfter.push_back( Virtual::makeGetExceptionFunction( |
---|
| 448 | vtable_object, except_decl->makeInst( std::move( poly_args ) ) |
---|
| 449 | ) ); |
---|
[1c01c58] | 450 | } |
---|
[6c3a5ac1] | 451 | |
---|
| 452 | return decl; |
---|
| 453 | } |
---|
| 454 | |
---|
[9a705dc8] | 455 | Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) { |
---|
| 456 | if ( cast_target == cast->target ) { |
---|
[e84ab3d] | 457 | // convert (thread &)t to (thread$ &)*get_thread(t), etc. |
---|
[9a705dc8] | 458 | if( !type_decl ) SemanticError( cast, context_error ); |
---|
[6c3a5ac1] | 459 | if( !dtor_decl ) SemanticError( cast, context_error ); |
---|
[3b0c8cb] | 460 | assert( cast->result == nullptr ); |
---|
| 461 | cast->set_result( new ReferenceType( noQualifiers, new StructInstType( noQualifiers, type_decl ) ) ); |
---|
| 462 | cast->concrete_target.field = field_name; |
---|
| 463 | cast->concrete_target.getter = getter_name; |
---|
[9a705dc8] | 464 | } |
---|
| 465 | return cast; |
---|
| 466 | } |
---|
| 467 | |
---|
[bcda04c] | 468 | void ConcurrentSueKeyword::handle( StructDecl * decl ) { |
---|
[9f5ecf5] | 469 | if( ! decl->body ) return; |
---|
[b32ada31] | 470 | |
---|
[a16764a6] | 471 | if( !type_decl ) SemanticError( decl, context_error ); |
---|
[6c3a5ac1] | 472 | if( !dtor_decl ) SemanticError( decl, context_error ); |
---|
[b32ada31] | 473 | |
---|
[ecfd758] | 474 | if ( !exception_name.empty() ) { |
---|
| 475 | if( !typeid_decl ) SemanticError( decl, context_error ); |
---|
| 476 | if( !vtable_decl ) SemanticError( decl, context_error ); |
---|
| 477 | |
---|
| 478 | addTypeId( decl ); |
---|
| 479 | addVtableForward( decl ); |
---|
| 480 | } |
---|
[bcda04c] | 481 | FunctionDecl * func = forwardDeclare( decl ); |
---|
| 482 | ObjectDecl * field = addField( decl ); |
---|
[af67ee1] | 483 | |
---|
| 484 | // add get_.* routine |
---|
[2f9a722] | 485 | addRoutines( field, func ); |
---|
[af67ee1] | 486 | // add lock/unlock routines to monitors for use by mutex stmt |
---|
| 487 | addLockUnlockRoutines( decl ); |
---|
[b32ada31] | 488 | } |
---|
| 489 | |
---|
[ecfd758] | 490 | void ConcurrentSueKeyword::addTypeId( StructDecl * decl ) { |
---|
| 491 | assert( typeid_decl ); |
---|
| 492 | StructInstType typeid_type( Type::Const, typeid_decl ); |
---|
| 493 | typeid_type.parameters.push_back( new TypeExpr( |
---|
| 494 | new StructInstType( noQualifiers, decl ) |
---|
[69c5c00] | 495 | ) ); |
---|
[ecfd758] | 496 | declsToAddBefore.push_back( Virtual::makeTypeIdInstance( &typeid_type ) ); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | void ConcurrentSueKeyword::addVtableForward( StructDecl * decl ) { |
---|
| 500 | assert( vtable_decl ); |
---|
| 501 | std::list< Expression * > poly_args = { |
---|
| 502 | new TypeExpr( new StructInstType( noQualifiers, decl ) ), |
---|
| 503 | }; |
---|
| 504 | declsToAddBefore.push_back( Virtual::makeGetExceptionForward( |
---|
| 505 | vtable_decl->makeInst( poly_args ), |
---|
| 506 | except_decl->makeInst( poly_args ) |
---|
| 507 | ) ); |
---|
[b583113] | 508 | ObjectDecl * vtable_object = Virtual::makeVtableForward( |
---|
| 509 | "_default_vtable_object_declaration", |
---|
[09f34a84] | 510 | vtable_decl->makeInst( std::move( poly_args ) ) ); |
---|
[b583113] | 511 | declsToAddBefore.push_back( vtable_object ); |
---|
[8edbe40] | 512 | declsToAddBefore.push_back( |
---|
[b583113] | 513 | new ObjectDecl( |
---|
| 514 | Virtual::concurrentDefaultVTableName(), |
---|
[67b421c] | 515 | Type::StorageClasses( Type::Extern ), |
---|
[b583113] | 516 | LinkageSpec::Cforall, |
---|
| 517 | /* bitfieldWidth */ nullptr, |
---|
| 518 | new ReferenceType( Type::Const, vtable_object->type->clone() ), |
---|
| 519 | /* init */ nullptr |
---|
| 520 | ) |
---|
| 521 | ); |
---|
[1c01c58] | 522 | } |
---|
| 523 | |
---|
[bcda04c] | 524 | FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) { |
---|
| 525 | |
---|
| 526 | StructDecl * forward = decl->clone(); |
---|
| 527 | forward->set_body( false ); |
---|
| 528 | deleteAll( forward->get_members() ); |
---|
| 529 | forward->get_members().clear(); |
---|
| 530 | |
---|
[bd4d011] | 531 | FunctionType * get_type = new FunctionType( noQualifiers, false ); |
---|
[bcda04c] | 532 | ObjectDecl * this_decl = new ObjectDecl( |
---|
| 533 | "this", |
---|
[ba3706f] | 534 | noStorageClasses, |
---|
[b32ada31] | 535 | LinkageSpec::Cforall, |
---|
| 536 | nullptr, |
---|
[83a071f9] | 537 | new ReferenceType( |
---|
[b32ada31] | 538 | noQualifiers, |
---|
[bcda04c] | 539 | new StructInstType( |
---|
| 540 | noQualifiers, |
---|
| 541 | decl |
---|
| 542 | ) |
---|
[b32ada31] | 543 | ), |
---|
| 544 | nullptr |
---|
| 545 | ); |
---|
| 546 | |
---|
[2db79e5] | 547 | get_type->get_parameters().push_back( this_decl->clone() ); |
---|
[bd4d011] | 548 | get_type->get_returnVals().push_back( |
---|
[e04b636] | 549 | new ObjectDecl( |
---|
| 550 | "ret", |
---|
[ba3706f] | 551 | noStorageClasses, |
---|
[e04b636] | 552 | LinkageSpec::Cforall, |
---|
| 553 | nullptr, |
---|
| 554 | new PointerType( |
---|
| 555 | noQualifiers, |
---|
| 556 | new StructInstType( |
---|
| 557 | noQualifiers, |
---|
[bcda04c] | 558 | type_decl |
---|
[e04b636] | 559 | ) |
---|
| 560 | ), |
---|
| 561 | nullptr |
---|
| 562 | ) |
---|
| 563 | ); |
---|
[2db79e5] | 564 | fixupGenerics(get_type, decl); |
---|
[b32ada31] | 565 | |
---|
[bcda04c] | 566 | FunctionDecl * get_decl = new FunctionDecl( |
---|
| 567 | getter_name, |
---|
| 568 | Type::Static, |
---|
| 569 | LinkageSpec::Cforall, |
---|
[bd4d011] | 570 | get_type, |
---|
[bcda04c] | 571 | nullptr, |
---|
[a8078ee] | 572 | { new Attribute("const") }, |
---|
[bcda04c] | 573 | Type::Inline |
---|
| 574 | ); |
---|
| 575 | |
---|
[bd4d011] | 576 | FunctionDecl * main_decl = nullptr; |
---|
| 577 | |
---|
| 578 | if( needs_main ) { |
---|
| 579 | FunctionType * main_type = new FunctionType( noQualifiers, false ); |
---|
[bf2438c] | 580 | |
---|
[bd4d011] | 581 | main_type->get_parameters().push_back( this_decl->clone() ); |
---|
| 582 | |
---|
| 583 | main_decl = new FunctionDecl( |
---|
| 584 | "main", |
---|
[ba3706f] | 585 | noStorageClasses, |
---|
[bd4d011] | 586 | LinkageSpec::Cforall, |
---|
| 587 | main_type, |
---|
| 588 | nullptr |
---|
| 589 | ); |
---|
[2db79e5] | 590 | fixupGenerics(main_type, decl); |
---|
[bd4d011] | 591 | } |
---|
| 592 | |
---|
[2db79e5] | 593 | delete this_decl; |
---|
| 594 | |
---|
[2065609] | 595 | declsToAddBefore.push_back( forward ); |
---|
| 596 | if( needs_main ) declsToAddBefore.push_back( main_decl ); |
---|
| 597 | declsToAddBefore.push_back( get_decl ); |
---|
[bcda04c] | 598 | |
---|
| 599 | return get_decl; |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) { |
---|
| 603 | ObjectDecl * field = new ObjectDecl( |
---|
| 604 | field_name, |
---|
[ba3706f] | 605 | noStorageClasses, |
---|
[bcda04c] | 606 | LinkageSpec::Cforall, |
---|
| 607 | nullptr, |
---|
| 608 | new StructInstType( |
---|
| 609 | noQualifiers, |
---|
| 610 | type_decl |
---|
| 611 | ), |
---|
| 612 | nullptr |
---|
| 613 | ); |
---|
| 614 | |
---|
| 615 | decl->get_members().push_back( field ); |
---|
| 616 | |
---|
| 617 | return field; |
---|
| 618 | } |
---|
| 619 | |
---|
[af67ee1] | 620 | // This function adds the get_.* routine body for coroutines, monitors etc |
---|
| 621 | // after their corresponding struct has been made |
---|
[2f9a722] | 622 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) { |
---|
[ba3706f] | 623 | CompoundStmt * statement = new CompoundStmt(); |
---|
[bf2438c] | 624 | statement->push_back( |
---|
[b32ada31] | 625 | new ReturnStmt( |
---|
[e04b636] | 626 | new AddressExpr( |
---|
[bcda04c] | 627 | new MemberExpr( |
---|
| 628 | field, |
---|
[2db79e5] | 629 | new CastExpr( |
---|
| 630 | new VariableExpr( func->get_functionType()->get_parameters().front() ), |
---|
[b81fd95] | 631 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone(), |
---|
| 632 | false |
---|
[2db79e5] | 633 | ) |
---|
[e04b636] | 634 | ) |
---|
[b32ada31] | 635 | ) |
---|
| 636 | ) |
---|
| 637 | ); |
---|
| 638 | |
---|
[bcda04c] | 639 | FunctionDecl * get_decl = func->clone(); |
---|
| 640 | |
---|
| 641 | get_decl->set_statements( statement ); |
---|
[e04b636] | 642 | |
---|
| 643 | declsToAddAfter.push_back( get_decl ); |
---|
[427854b] | 644 | } |
---|
| 645 | |
---|
[af67ee1] | 646 | // Generates lock/unlock routines for monitors to be used by mutex stmts |
---|
| 647 | void ConcurrentSueKeyword::addLockUnlockRoutines( StructDecl * decl ) { |
---|
| 648 | // this routine will be called for all ConcurrentSueKeyword children so only continue if we are a monitor |
---|
| 649 | if ( !decl->is_monitor() ) return; |
---|
| 650 | |
---|
| 651 | FunctionType * lock_fn_type = new FunctionType( noQualifiers, false ); |
---|
| 652 | FunctionType * unlock_fn_type = new FunctionType( noQualifiers, false ); |
---|
| 653 | |
---|
| 654 | // create this ptr parameter for both routines |
---|
| 655 | ObjectDecl * this_decl = new ObjectDecl( |
---|
| 656 | "this", |
---|
| 657 | noStorageClasses, |
---|
| 658 | LinkageSpec::Cforall, |
---|
| 659 | nullptr, |
---|
| 660 | new ReferenceType( |
---|
| 661 | noQualifiers, |
---|
| 662 | new StructInstType( |
---|
| 663 | noQualifiers, |
---|
| 664 | decl |
---|
| 665 | ) |
---|
| 666 | ), |
---|
| 667 | nullptr |
---|
| 668 | ); |
---|
| 669 | |
---|
| 670 | lock_fn_type->get_parameters().push_back( this_decl->clone() ); |
---|
| 671 | unlock_fn_type->get_parameters().push_back( this_decl->clone() ); |
---|
[5b7c8b5] | 672 | fixupGenerics(lock_fn_type, decl); |
---|
| 673 | fixupGenerics(unlock_fn_type, decl); |
---|
[af67ee1] | 674 | |
---|
| 675 | delete this_decl; |
---|
| 676 | |
---|
| 677 | |
---|
| 678 | ////////////////////////////////////////////////////////////////////// |
---|
| 679 | // The following generates this lock routine for all monitors |
---|
| 680 | /* |
---|
| 681 | void lock (monitor_t & this) { |
---|
| 682 | lock(get_monitor(this)); |
---|
[09f34a84] | 683 | } |
---|
[af67ee1] | 684 | */ |
---|
| 685 | FunctionDecl * lock_decl = new FunctionDecl( |
---|
| 686 | "lock", |
---|
| 687 | Type::Static, |
---|
| 688 | LinkageSpec::Cforall, |
---|
| 689 | lock_fn_type, |
---|
| 690 | nullptr, |
---|
| 691 | { }, |
---|
| 692 | Type::Inline |
---|
| 693 | ); |
---|
| 694 | |
---|
| 695 | UntypedExpr * get_monitor_lock = new UntypedExpr ( |
---|
| 696 | new NameExpr( "get_monitor" ), |
---|
| 697 | { new VariableExpr( lock_fn_type->get_parameters().front() ) } |
---|
| 698 | ); |
---|
| 699 | |
---|
| 700 | CompoundStmt * lock_statement = new CompoundStmt(); |
---|
| 701 | lock_statement->push_back( |
---|
[09f34a84] | 702 | new ExprStmt( |
---|
[af67ee1] | 703 | new UntypedExpr ( |
---|
| 704 | new NameExpr( "lock" ), |
---|
| 705 | { |
---|
| 706 | get_monitor_lock |
---|
| 707 | } |
---|
| 708 | ) |
---|
| 709 | ) |
---|
| 710 | ); |
---|
| 711 | lock_decl->set_statements( lock_statement ); |
---|
| 712 | |
---|
| 713 | ////////////////////////////////////////////////////////////////// |
---|
| 714 | // The following generates this routine for all monitors |
---|
| 715 | /* |
---|
| 716 | void unlock (monitor_t & this) { |
---|
| 717 | unlock(get_monitor(this)); |
---|
[09f34a84] | 718 | } |
---|
[af67ee1] | 719 | */ |
---|
| 720 | FunctionDecl * unlock_decl = new FunctionDecl( |
---|
| 721 | "unlock", |
---|
| 722 | Type::Static, |
---|
| 723 | LinkageSpec::Cforall, |
---|
| 724 | unlock_fn_type, |
---|
| 725 | nullptr, |
---|
| 726 | { }, |
---|
| 727 | Type::Inline |
---|
| 728 | ); |
---|
| 729 | |
---|
| 730 | CompoundStmt * unlock_statement = new CompoundStmt(); |
---|
| 731 | |
---|
| 732 | UntypedExpr * get_monitor_unlock = new UntypedExpr ( |
---|
| 733 | new NameExpr( "get_monitor" ), |
---|
| 734 | { new VariableExpr( unlock_fn_type->get_parameters().front() ) } |
---|
| 735 | ); |
---|
| 736 | |
---|
| 737 | unlock_statement->push_back( |
---|
[09f34a84] | 738 | new ExprStmt( |
---|
[af67ee1] | 739 | new UntypedExpr( |
---|
| 740 | new NameExpr( "unlock" ), |
---|
| 741 | { |
---|
| 742 | get_monitor_unlock |
---|
| 743 | } |
---|
| 744 | ) |
---|
| 745 | ) |
---|
| 746 | ); |
---|
| 747 | unlock_decl->set_statements( unlock_statement ); |
---|
[09f34a84] | 748 | |
---|
[af67ee1] | 749 | // pushes routines to declsToAddAfter to add at a later time |
---|
| 750 | declsToAddAfter.push_back( lock_decl ); |
---|
| 751 | declsToAddAfter.push_back( unlock_decl ); |
---|
| 752 | } |
---|
| 753 | |
---|
[427854b] | 754 | //============================================================================================= |
---|
| 755 | // Suspend keyword implementation |
---|
| 756 | //============================================================================================= |
---|
| 757 | bool SuspendKeyword::is_real_suspend( FunctionDecl * func ) { |
---|
| 758 | if(isMangled(func->linkage)) return false; // the real suspend isn't mangled |
---|
| 759 | if(func->name != "__cfactx_suspend") return false; // the real suspend has a specific name |
---|
| 760 | if(func->type->parameters.size() != 0) return false; // Too many parameters |
---|
| 761 | if(func->type->returnVals.size() != 0) return false; // Too many return values |
---|
| 762 | |
---|
| 763 | return true; |
---|
[b32ada31] | 764 | } |
---|
| 765 | |
---|
[427854b] | 766 | void SuspendKeyword::premutate( FunctionDecl * func ) { |
---|
| 767 | GuardValue(in_generator); |
---|
| 768 | in_generator = nullptr; |
---|
| 769 | |
---|
| 770 | // Is this the real suspend? |
---|
| 771 | if(is_real_suspend(func)) { |
---|
| 772 | decl_suspend = decl_suspend ? decl_suspend : func; |
---|
| 773 | return; |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | // Is this the main of a generator? |
---|
[1c01c58] | 777 | auto param = isMainFor( func, AggregateDecl::Aggregate::Generator ); |
---|
[427854b] | 778 | if(!param) return; |
---|
| 779 | |
---|
| 780 | if(func->type->returnVals.size() != 0) SemanticError(func->location, "Generator main must return void"); |
---|
| 781 | |
---|
| 782 | in_generator = param; |
---|
| 783 | GuardValue(labels); |
---|
| 784 | labels.clear(); |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | DeclarationWithType * SuspendKeyword::postmutate( FunctionDecl * func ) { |
---|
| 788 | if( !func->statements ) return func; // Not the actual definition, don't do anything |
---|
| 789 | if( !in_generator ) return func; // Not in a generator, don't do anything |
---|
| 790 | if( labels.empty() ) return func; // Generator has no states, nothing to do, could throw a warning |
---|
| 791 | |
---|
| 792 | // This is a generator main, we need to add the following code to the top |
---|
| 793 | // static void * __generator_labels[] = {&&s0, &&s1, ...}; |
---|
| 794 | // goto * __generator_labels[gen.__generator_state]; |
---|
| 795 | const auto & loc = func->location; |
---|
| 796 | |
---|
| 797 | const auto first_label = gen.newLabel("generator"); |
---|
| 798 | |
---|
| 799 | // for each label add to declaration |
---|
| 800 | std::list<Initializer*> inits = { new SingleInit( new LabelAddressExpr( first_label ) ) }; |
---|
| 801 | for(const auto & label : labels) { |
---|
| 802 | inits.push_back( |
---|
| 803 | new SingleInit( |
---|
| 804 | new LabelAddressExpr( label ) |
---|
| 805 | ) |
---|
| 806 | ); |
---|
| 807 | } |
---|
| 808 | auto init = new ListInit(std::move(inits), noDesignators, true); |
---|
| 809 | labels.clear(); |
---|
| 810 | |
---|
| 811 | // create decl |
---|
| 812 | auto decl = new ObjectDecl( |
---|
| 813 | "__generator_labels", |
---|
| 814 | Type::StorageClasses( Type::Static ), |
---|
| 815 | LinkageSpec::AutoGen, |
---|
| 816 | nullptr, |
---|
| 817 | new ArrayType( |
---|
| 818 | Type::Qualifiers(), |
---|
| 819 | new PointerType( |
---|
| 820 | Type::Qualifiers(), |
---|
| 821 | new VoidType( Type::Qualifiers() ) |
---|
| 822 | ), |
---|
| 823 | nullptr, |
---|
| 824 | false, false |
---|
| 825 | ), |
---|
| 826 | init |
---|
| 827 | ); |
---|
| 828 | |
---|
| 829 | // create the goto |
---|
| 830 | assert(in_generator); |
---|
| 831 | |
---|
| 832 | auto go_decl = new ObjectDecl( |
---|
| 833 | "__generator_label", |
---|
| 834 | noStorageClasses, |
---|
| 835 | LinkageSpec::AutoGen, |
---|
| 836 | nullptr, |
---|
| 837 | new PointerType( |
---|
| 838 | Type::Qualifiers(), |
---|
| 839 | new VoidType( Type::Qualifiers() ) |
---|
| 840 | ), |
---|
| 841 | new SingleInit( |
---|
| 842 | new UntypedExpr( |
---|
| 843 | new NameExpr("?[?]"), |
---|
| 844 | { |
---|
| 845 | new NameExpr("__generator_labels"), |
---|
| 846 | new UntypedMemberExpr( |
---|
| 847 | new NameExpr("__generator_state"), |
---|
| 848 | new VariableExpr( in_generator ) |
---|
| 849 | ) |
---|
| 850 | } |
---|
| 851 | ) |
---|
| 852 | ) |
---|
| 853 | ); |
---|
| 854 | go_decl->location = loc; |
---|
| 855 | |
---|
| 856 | auto go = new BranchStmt( |
---|
| 857 | new VariableExpr( go_decl ), |
---|
| 858 | BranchStmt::Goto |
---|
| 859 | ); |
---|
| 860 | go->location = loc; |
---|
| 861 | go->computedTarget->location = loc; |
---|
| 862 | |
---|
| 863 | auto noop = new NullStmt({ first_label }); |
---|
| 864 | noop->location = loc; |
---|
| 865 | |
---|
| 866 | // wrap everything in a nice compound |
---|
| 867 | auto body = new CompoundStmt({ |
---|
| 868 | new DeclStmt( decl ), |
---|
| 869 | new DeclStmt( go_decl ), |
---|
| 870 | go, |
---|
| 871 | noop, |
---|
| 872 | func->statements |
---|
| 873 | }); |
---|
| 874 | body->location = loc; |
---|
| 875 | func->statements = body; |
---|
| 876 | |
---|
| 877 | return func; |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | Statement * SuspendKeyword::postmutate( SuspendStmt * stmt ) { |
---|
| 881 | SuspendStmt::Type type = stmt->type; |
---|
| 882 | if(type == SuspendStmt::None) { |
---|
| 883 | // This suspend has a implicit target, find it |
---|
| 884 | type = in_generator ? SuspendStmt::Generator : SuspendStmt::Coroutine; |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | // Check that the target makes sense |
---|
| 888 | if(!in_generator && type == SuspendStmt::Generator) SemanticError( stmt->location, "'suspend generator' must be used inside main of generator type."); |
---|
| 889 | |
---|
| 890 | // Act appropriately |
---|
| 891 | switch(type) { |
---|
| 892 | case SuspendStmt::Generator: return make_generator_suspend(stmt); |
---|
| 893 | case SuspendStmt::Coroutine: return make_coroutine_suspend(stmt); |
---|
| 894 | default: abort(); |
---|
| 895 | } |
---|
| 896 | } |
---|
| 897 | |
---|
| 898 | Statement * SuspendKeyword::make_generator_suspend( SuspendStmt * stmt ) { |
---|
| 899 | assert(in_generator); |
---|
| 900 | // Target code is : |
---|
| 901 | // gen.__generator_state = X; |
---|
| 902 | // { THEN } |
---|
| 903 | // return; |
---|
| 904 | // __gen_X:; |
---|
| 905 | |
---|
| 906 | // Save the location and delete the old statement, we only need the location from this point on |
---|
| 907 | auto loc = stmt->location; |
---|
| 908 | |
---|
| 909 | // Build the label and get its index |
---|
| 910 | auto label = make_label(); |
---|
| 911 | |
---|
| 912 | // Create the context saving statement |
---|
| 913 | auto save = new ExprStmt( new UntypedExpr( |
---|
| 914 | new NameExpr( "?=?" ), |
---|
| 915 | { |
---|
| 916 | new UntypedMemberExpr( |
---|
| 917 | new NameExpr("__generator_state"), |
---|
| 918 | new VariableExpr( in_generator ) |
---|
| 919 | ), |
---|
| 920 | new ConstantExpr( |
---|
| 921 | Constant::from_int( label.idx ) |
---|
| 922 | ) |
---|
| 923 | } |
---|
| 924 | )); |
---|
| 925 | assert(save->expr); |
---|
| 926 | save->location = loc; |
---|
| 927 | stmtsToAddBefore.push_back( save ); |
---|
| 928 | |
---|
| 929 | // if we have a then add it here |
---|
| 930 | auto then = stmt->then; |
---|
| 931 | stmt->then = nullptr; |
---|
| 932 | delete stmt; |
---|
| 933 | if(then) stmtsToAddBefore.push_back( then ); |
---|
| 934 | |
---|
| 935 | // Create the return statement |
---|
| 936 | auto ret = new ReturnStmt( nullptr ); |
---|
| 937 | ret->location = loc; |
---|
| 938 | stmtsToAddBefore.push_back( ret ); |
---|
| 939 | |
---|
| 940 | // Create the null statement with the created label |
---|
| 941 | auto noop = new NullStmt({ label.obj }); |
---|
| 942 | noop->location = loc; |
---|
| 943 | |
---|
| 944 | // Return the null statement to take the place of the previous statement |
---|
| 945 | return noop; |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | Statement * SuspendKeyword::make_coroutine_suspend( SuspendStmt * stmt ) { |
---|
| 949 | if(stmt->then) SemanticError( stmt->location, "Compound statement following coroutines is not implemented."); |
---|
| 950 | |
---|
| 951 | // Save the location and delete the old statement, we only need the location from this point on |
---|
| 952 | auto loc = stmt->location; |
---|
| 953 | delete stmt; |
---|
| 954 | |
---|
| 955 | // Create the call expression |
---|
| 956 | if(!decl_suspend) SemanticError( loc, "suspend keyword applied to coroutines requires coroutines to be in scope, add #include <coroutine.hfa>\n"); |
---|
| 957 | auto expr = new UntypedExpr( VariableExpr::functionPointer( decl_suspend ) ); |
---|
[e6cfa8ff] | 958 | expr->location = loc; |
---|
[427854b] | 959 | |
---|
| 960 | // Change this statement into a regular expr |
---|
| 961 | assert(expr); |
---|
| 962 | auto nstmt = new ExprStmt( expr ); |
---|
[e6cfa8ff] | 963 | nstmt->location = loc; |
---|
[427854b] | 964 | return nstmt; |
---|
| 965 | } |
---|
| 966 | |
---|
| 967 | |
---|
[64adb03] | 968 | //============================================================================================= |
---|
| 969 | // Mutex keyword implementation |
---|
| 970 | //============================================================================================= |
---|
[97e3296] | 971 | |
---|
[2065609] | 972 | void MutexKeyword::postvisit(FunctionDecl* decl) { |
---|
[102a58b] | 973 | |
---|
[db4d8e3] | 974 | bool first = false; |
---|
| 975 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first ); |
---|
[ab8c6a6] | 976 | bool const isDtor = CodeGen::isDestructor( decl->name ); |
---|
[64adb03] | 977 | |
---|
[ceedde6] | 978 | // Is this function relevant to monitors |
---|
| 979 | if( mutexArgs.empty() ) { |
---|
| 980 | // If this is the destructor for a monitor it must be mutex |
---|
| 981 | if(isDtor) { |
---|
[3cc1111] | 982 | // This reflects MutexKeyword::validate, except does not produce an error. |
---|
[ceedde6] | 983 | Type* ty = decl->get_functionType()->get_parameters().front()->get_type(); |
---|
| 984 | |
---|
| 985 | // If it's a copy, it's not a mutex |
---|
| 986 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty ); |
---|
| 987 | if( ! rty ) return; |
---|
| 988 | |
---|
| 989 | // If we are not pointing directly to a type, it's not a mutex |
---|
| 990 | Type* base = rty->get_base(); |
---|
| 991 | if( dynamic_cast< ReferenceType * >( base ) ) return; |
---|
| 992 | if( dynamic_cast< PointerType * >( base ) ) return; |
---|
| 993 | |
---|
| 994 | // Check if its a struct |
---|
| 995 | StructInstType * baseStruct = dynamic_cast< StructInstType * >( base ); |
---|
| 996 | if( !baseStruct ) return; |
---|
| 997 | |
---|
| 998 | // Check if its a monitor |
---|
| 999 | if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread()) |
---|
| 1000 | SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" ); |
---|
| 1001 | } |
---|
| 1002 | return; |
---|
| 1003 | } |
---|
[549c006] | 1004 | |
---|
[ceedde6] | 1005 | // Monitors can't be constructed with mutual exclusion |
---|
| 1006 | if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" ); |
---|
[549c006] | 1007 | |
---|
[ceedde6] | 1008 | // It makes no sense to have multiple mutex parameters for the destructor |
---|
[a16764a6] | 1009 | if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" ); |
---|
[549c006] | 1010 | |
---|
[ceedde6] | 1011 | // Make sure all the mutex arguments are monitors |
---|
[64adb03] | 1012 | for(auto arg : mutexArgs) { |
---|
| 1013 | validate( arg ); |
---|
| 1014 | } |
---|
| 1015 | |
---|
[ceedde6] | 1016 | // Check if we need to instrument the body |
---|
[64adb03] | 1017 | CompoundStmt* body = decl->get_statements(); |
---|
| 1018 | if( ! body ) return; |
---|
| 1019 | |
---|
[ceedde6] | 1020 | // Do we have the required headers |
---|
[a16764a6] | 1021 | if( !monitor_decl || !guard_decl || !dtor_guard_decl ) |
---|
[73abe95] | 1022 | SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>\n" ); |
---|
[b32ada31] | 1023 | |
---|
[ceedde6] | 1024 | // Instrument the body |
---|
[ab8c6a6] | 1025 | if ( isDtor && isThread( mutexArgs.front() ) ) { |
---|
| 1026 | if( !thread_guard_decl ) { |
---|
| 1027 | SemanticError( decl, "thread destructor requires threads to be in scope, add #include <thread.hfa>\n" ); |
---|
| 1028 | } |
---|
| 1029 | addThreadDtorStatements( decl, body, mutexArgs ); |
---|
| 1030 | } |
---|
| 1031 | else if ( isDtor ) { |
---|
| 1032 | addDtorStatements( decl, body, mutexArgs ); |
---|
[549c006] | 1033 | } |
---|
| 1034 | else { |
---|
[ab8c6a6] | 1035 | addStatements( decl, body, mutexArgs ); |
---|
[549c006] | 1036 | } |
---|
[64adb03] | 1037 | } |
---|
| 1038 | |
---|
[2065609] | 1039 | void MutexKeyword::postvisit(StructDecl* decl) { |
---|
[102a58b] | 1040 | |
---|
[e84ab3d] | 1041 | if( decl->name == "monitor$" && decl->body ) { |
---|
[9243cc91] | 1042 | assert( !monitor_decl ); |
---|
| 1043 | monitor_decl = decl; |
---|
| 1044 | } |
---|
[88d955f] | 1045 | else if( decl->name == "monitor_guard_t" && decl->body ) { |
---|
[ef42b143] | 1046 | assert( !guard_decl ); |
---|
| 1047 | guard_decl = decl; |
---|
| 1048 | } |
---|
[88d955f] | 1049 | else if( decl->name == "monitor_dtor_guard_t" && decl->body ) { |
---|
[549c006] | 1050 | assert( !dtor_guard_decl ); |
---|
| 1051 | dtor_guard_decl = decl; |
---|
| 1052 | } |
---|
[ab8c6a6] | 1053 | else if( decl->name == "thread_dtor_guard_t" && decl->body ) { |
---|
| 1054 | assert( !thread_guard_decl ); |
---|
| 1055 | thread_guard_decl = decl; |
---|
[09f34a84] | 1056 | } |
---|
[af67ee1] | 1057 | else if ( decl->name == "__mutex_stmt_lock_guard" && decl->body ) { |
---|
| 1058 | assert( !lock_guard_decl ); |
---|
| 1059 | lock_guard_decl = decl; |
---|
[ab8c6a6] | 1060 | } |
---|
[9243cc91] | 1061 | } |
---|
| 1062 | |
---|
[6cebfef] | 1063 | Statement * MutexKeyword::postmutate( MutexStmt * stmt ) { |
---|
| 1064 | std::list<Statement *> stmtsForCtor; |
---|
| 1065 | stmtsForCtor.push_back(stmt->stmt); |
---|
| 1066 | CompoundStmt * body = new CompoundStmt( stmtsForCtor ); |
---|
| 1067 | addStatements( body, stmt->mutexObjs); |
---|
| 1068 | return body; |
---|
| 1069 | } |
---|
| 1070 | |
---|
[db4d8e3] | 1071 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) { |
---|
[64adb03] | 1072 | std::list<DeclarationWithType*> mutexArgs; |
---|
| 1073 | |
---|
[db4d8e3] | 1074 | bool once = true; |
---|
[64adb03] | 1075 | for( auto arg : decl->get_functionType()->get_parameters()) { |
---|
| 1076 | //Find mutex arguments |
---|
| 1077 | Type* ty = arg->get_type(); |
---|
[615a096] | 1078 | if( ! ty->get_mutex() ) continue; |
---|
[64adb03] | 1079 | |
---|
[db4d8e3] | 1080 | if(once) {first = true;} |
---|
| 1081 | once = false; |
---|
| 1082 | |
---|
[64adb03] | 1083 | //Append it to the list |
---|
| 1084 | mutexArgs.push_back( arg ); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | return mutexArgs; |
---|
| 1088 | } |
---|
| 1089 | |
---|
| 1090 | void MutexKeyword::validate( DeclarationWithType * arg ) { |
---|
| 1091 | Type* ty = arg->get_type(); |
---|
| 1092 | |
---|
| 1093 | //Makes sure it's not a copy |
---|
[870d1f0] | 1094 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty ); |
---|
[a16764a6] | 1095 | if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " ); |
---|
[64adb03] | 1096 | |
---|
| 1097 | //Make sure the we are pointing directly to a type |
---|
[83a071f9] | 1098 | Type* base = rty->get_base(); |
---|
[a16764a6] | 1099 | if( dynamic_cast< ReferenceType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " ); |
---|
| 1100 | if( dynamic_cast< PointerType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " ); |
---|
[64adb03] | 1101 | |
---|
| 1102 | //Make sure that typed isn't mutex |
---|
[a16764a6] | 1103 | if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " ); |
---|
[64adb03] | 1104 | } |
---|
| 1105 | |
---|
[ab8c6a6] | 1106 | void MutexKeyword::addDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { |
---|
[549c006] | 1107 | Type * arg_type = args.front()->get_type()->clone(); |
---|
| 1108 | arg_type->set_mutex( false ); |
---|
| 1109 | |
---|
| 1110 | ObjectDecl * monitors = new ObjectDecl( |
---|
| 1111 | "__monitor", |
---|
[ba3706f] | 1112 | noStorageClasses, |
---|
[549c006] | 1113 | LinkageSpec::Cforall, |
---|
| 1114 | nullptr, |
---|
| 1115 | new PointerType( |
---|
| 1116 | noQualifiers, |
---|
| 1117 | new StructInstType( |
---|
| 1118 | noQualifiers, |
---|
| 1119 | monitor_decl |
---|
| 1120 | ) |
---|
| 1121 | ), |
---|
| 1122 | new SingleInit( new UntypedExpr( |
---|
| 1123 | new NameExpr( "get_monitor" ), |
---|
[b81fd95] | 1124 | { new CastExpr( new VariableExpr( args.front() ), arg_type, false ) } |
---|
[549c006] | 1125 | )) |
---|
| 1126 | ); |
---|
| 1127 | |
---|
| 1128 | assert(generic_func); |
---|
| 1129 | |
---|
| 1130 | //in reverse order : |
---|
[2bf7ef6] | 1131 | // monitor_dtor_guard_t __guard = { __monitors, func }; |
---|
[549c006] | 1132 | body->push_front( |
---|
[ba3706f] | 1133 | new DeclStmt( new ObjectDecl( |
---|
[549c006] | 1134 | "__guard", |
---|
[ba3706f] | 1135 | noStorageClasses, |
---|
[549c006] | 1136 | LinkageSpec::Cforall, |
---|
| 1137 | nullptr, |
---|
| 1138 | new StructInstType( |
---|
| 1139 | noQualifiers, |
---|
| 1140 | dtor_guard_decl |
---|
| 1141 | ), |
---|
| 1142 | new ListInit( |
---|
| 1143 | { |
---|
| 1144 | new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ), |
---|
[77a2994] | 1145 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ), |
---|
| 1146 | new SingleInit( new ConstantExpr( Constant::from_bool( false ) ) ) |
---|
[549c006] | 1147 | }, |
---|
| 1148 | noDesignators, |
---|
| 1149 | true |
---|
| 1150 | ) |
---|
| 1151 | )) |
---|
| 1152 | ); |
---|
| 1153 | |
---|
[e84ab3d] | 1154 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) }; |
---|
[ab8c6a6] | 1155 | body->push_front( new DeclStmt( monitors ) ); |
---|
| 1156 | } |
---|
| 1157 | |
---|
| 1158 | void MutexKeyword::addThreadDtorStatements( |
---|
| 1159 | FunctionDecl*, CompoundStmt * body, |
---|
| 1160 | const std::list<DeclarationWithType * > & args ) { |
---|
| 1161 | assert( args.size() == 1 ); |
---|
| 1162 | DeclarationWithType * arg = args.front(); |
---|
| 1163 | Type * arg_type = arg->get_type()->clone(); |
---|
| 1164 | assert( arg_type->get_mutex() ); |
---|
| 1165 | arg_type->set_mutex( false ); |
---|
| 1166 | |
---|
| 1167 | // thread_dtor_guard_t __guard = { this, intptr( 0 ) }; |
---|
| 1168 | body->push_front( |
---|
| 1169 | new DeclStmt( new ObjectDecl( |
---|
| 1170 | "__guard", |
---|
| 1171 | noStorageClasses, |
---|
| 1172 | LinkageSpec::Cforall, |
---|
| 1173 | nullptr, |
---|
| 1174 | new StructInstType( |
---|
| 1175 | noQualifiers, |
---|
| 1176 | thread_guard_decl |
---|
| 1177 | ), |
---|
| 1178 | new ListInit( |
---|
| 1179 | { |
---|
| 1180 | new SingleInit( new CastExpr( new VariableExpr( arg ), arg_type ) ), |
---|
| 1181 | new SingleInit( new UntypedExpr( |
---|
| 1182 | new NameExpr( "intptr" ), { |
---|
| 1183 | new ConstantExpr( Constant::from_int( 0 ) ), |
---|
| 1184 | } |
---|
| 1185 | ) ), |
---|
| 1186 | }, |
---|
| 1187 | noDesignators, |
---|
| 1188 | true |
---|
| 1189 | ) |
---|
| 1190 | )) |
---|
| 1191 | ); |
---|
[549c006] | 1192 | } |
---|
| 1193 | |
---|
[6cebfef] | 1194 | void MutexKeyword::addStatements( CompoundStmt * body, const std::list<Expression * > & args ) { |
---|
| 1195 | ObjectDecl * monitors = new ObjectDecl( |
---|
| 1196 | "__monitors", |
---|
| 1197 | noStorageClasses, |
---|
| 1198 | LinkageSpec::Cforall, |
---|
| 1199 | nullptr, |
---|
| 1200 | new ArrayType( |
---|
| 1201 | noQualifiers, |
---|
| 1202 | new PointerType( |
---|
| 1203 | noQualifiers, |
---|
[a8367eb] | 1204 | //new TypeofType( noQualifiers, args.front()->clone() ) |
---|
| 1205 | new TypeofType( noQualifiers, new UntypedExpr( |
---|
[080d2d7] | 1206 | new NameExpr( "__get_mutexstmt_lock_type" ), |
---|
[a8367eb] | 1207 | { args.front()->clone() } |
---|
[09f34a84] | 1208 | ) |
---|
[a8367eb] | 1209 | ) |
---|
[6cebfef] | 1210 | ), |
---|
| 1211 | new ConstantExpr( Constant::from_ulong( args.size() ) ), |
---|
| 1212 | false, |
---|
| 1213 | false |
---|
| 1214 | ), |
---|
| 1215 | new ListInit( |
---|
| 1216 | map_range < std::list<Initializer*> > ( args, [](Expression * var ){ |
---|
[a8367eb] | 1217 | return new SingleInit( new UntypedExpr( |
---|
[080d2d7] | 1218 | new NameExpr( "__get_mutexstmt_lock_ptr" ), |
---|
[a8367eb] | 1219 | { var } |
---|
| 1220 | ) ); |
---|
| 1221 | //return new SingleInit( new AddressExpr( var ) ); |
---|
[6cebfef] | 1222 | }) |
---|
| 1223 | ) |
---|
| 1224 | ); |
---|
| 1225 | |
---|
[af67ee1] | 1226 | StructInstType * lock_guard_struct = new StructInstType( noQualifiers, lock_guard_decl ); |
---|
[09f34a84] | 1227 | TypeExpr * lock_type_expr = new TypeExpr( |
---|
[a8367eb] | 1228 | new TypeofType( noQualifiers, new UntypedExpr( |
---|
[080d2d7] | 1229 | new NameExpr( "__get_mutexstmt_lock_type" ), |
---|
[a8367eb] | 1230 | { args.front()->clone() } |
---|
[09f34a84] | 1231 | ) |
---|
| 1232 | ) |
---|
[a8367eb] | 1233 | ); |
---|
[af67ee1] | 1234 | |
---|
| 1235 | lock_guard_struct->parameters.push_back( lock_type_expr ) ; |
---|
| 1236 | |
---|
[6cebfef] | 1237 | // in reverse order : |
---|
| 1238 | // monitor_guard_t __guard = { __monitors, # }; |
---|
| 1239 | body->push_front( |
---|
| 1240 | new DeclStmt( new ObjectDecl( |
---|
| 1241 | "__guard", |
---|
| 1242 | noStorageClasses, |
---|
| 1243 | LinkageSpec::Cforall, |
---|
| 1244 | nullptr, |
---|
[af67ee1] | 1245 | lock_guard_struct, |
---|
[6cebfef] | 1246 | new ListInit( |
---|
| 1247 | { |
---|
| 1248 | new SingleInit( new VariableExpr( monitors ) ), |
---|
| 1249 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ) |
---|
| 1250 | }, |
---|
| 1251 | noDesignators, |
---|
| 1252 | true |
---|
| 1253 | ) |
---|
| 1254 | )) |
---|
| 1255 | ); |
---|
| 1256 | |
---|
| 1257 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) }; |
---|
| 1258 | body->push_front( new DeclStmt( monitors) ); |
---|
| 1259 | } |
---|
| 1260 | |
---|
[ab8c6a6] | 1261 | void MutexKeyword::addStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) { |
---|
[9243cc91] | 1262 | ObjectDecl * monitors = new ObjectDecl( |
---|
| 1263 | "__monitors", |
---|
[ba3706f] | 1264 | noStorageClasses, |
---|
[9243cc91] | 1265 | LinkageSpec::Cforall, |
---|
| 1266 | nullptr, |
---|
| 1267 | new ArrayType( |
---|
| 1268 | noQualifiers, |
---|
| 1269 | new PointerType( |
---|
| 1270 | noQualifiers, |
---|
| 1271 | new StructInstType( |
---|
| 1272 | noQualifiers, |
---|
| 1273 | monitor_decl |
---|
| 1274 | ) |
---|
| 1275 | ), |
---|
| 1276 | new ConstantExpr( Constant::from_ulong( args.size() ) ), |
---|
| 1277 | false, |
---|
| 1278 | false |
---|
| 1279 | ), |
---|
| 1280 | new ListInit( |
---|
[bd41764] | 1281 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){ |
---|
[cb0e6de] | 1282 | Type * type = var->get_type()->clone(); |
---|
| 1283 | type->set_mutex( false ); |
---|
[9243cc91] | 1284 | return new SingleInit( new UntypedExpr( |
---|
| 1285 | new NameExpr( "get_monitor" ), |
---|
[b81fd95] | 1286 | { new CastExpr( new VariableExpr( var ), type, false ) } |
---|
[9243cc91] | 1287 | ) ); |
---|
| 1288 | }) |
---|
| 1289 | ) |
---|
| 1290 | ); |
---|
| 1291 | |
---|
[97e3296] | 1292 | assert(generic_func); |
---|
| 1293 | |
---|
[2bf7ef6] | 1294 | // in reverse order : |
---|
[97e3296] | 1295 | // monitor_guard_t __guard = { __monitors, #, func }; |
---|
[64adb03] | 1296 | body->push_front( |
---|
[ba3706f] | 1297 | new DeclStmt( new ObjectDecl( |
---|
[64adb03] | 1298 | "__guard", |
---|
[ba3706f] | 1299 | noStorageClasses, |
---|
[64adb03] | 1300 | LinkageSpec::Cforall, |
---|
| 1301 | nullptr, |
---|
| 1302 | new StructInstType( |
---|
| 1303 | noQualifiers, |
---|
[ef42b143] | 1304 | guard_decl |
---|
[64adb03] | 1305 | ), |
---|
| 1306 | new ListInit( |
---|
| 1307 | { |
---|
[9243cc91] | 1308 | new SingleInit( new VariableExpr( monitors ) ), |
---|
[97e3296] | 1309 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ), |
---|
[b81fd95] | 1310 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ) |
---|
[ef42b143] | 1311 | }, |
---|
| 1312 | noDesignators, |
---|
| 1313 | true |
---|
[64adb03] | 1314 | ) |
---|
| 1315 | )) |
---|
| 1316 | ); |
---|
| 1317 | |
---|
[e84ab3d] | 1318 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) }; |
---|
[ba3706f] | 1319 | body->push_front( new DeclStmt( monitors) ); |
---|
[64adb03] | 1320 | } |
---|
[bd4d011] | 1321 | |
---|
| 1322 | //============================================================================================= |
---|
| 1323 | // General entry routine |
---|
| 1324 | //============================================================================================= |
---|
[549c006] | 1325 | void ThreadStarter::previsit( StructDecl * decl ) { |
---|
[e84ab3d] | 1326 | if( decl->name == "thread$" && decl->body ) { |
---|
[549c006] | 1327 | assert( !thread_decl ); |
---|
| 1328 | thread_decl = decl; |
---|
| 1329 | } |
---|
| 1330 | } |
---|
| 1331 | |
---|
[2065609] | 1332 | void ThreadStarter::postvisit(FunctionDecl * decl) { |
---|
[9f5ecf5] | 1333 | if( ! CodeGen::isConstructor(decl->name) ) return; |
---|
[bd4d011] | 1334 | |
---|
[549c006] | 1335 | Type * typeof_this = InitTweak::getTypeofThis(decl->type); |
---|
| 1336 | StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this ); |
---|
| 1337 | if( ctored_type && ctored_type->baseStruct == thread_decl ) { |
---|
| 1338 | thread_ctor_seen = true; |
---|
| 1339 | } |
---|
| 1340 | |
---|
[bd4d011] | 1341 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front(); |
---|
[ce8c12f] | 1342 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) ); |
---|
[bd4d011] | 1343 | if( type && type->get_baseStruct()->is_thread() ) { |
---|
[549c006] | 1344 | if( !thread_decl || !thread_ctor_seen ) { |
---|
[73abe95] | 1345 | SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>"); |
---|
[549c006] | 1346 | } |
---|
| 1347 | |
---|
[bd4d011] | 1348 | addStartStatement( decl, param ); |
---|
| 1349 | } |
---|
| 1350 | } |
---|
| 1351 | |
---|
| 1352 | void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) { |
---|
| 1353 | CompoundStmt * stmt = decl->get_statements(); |
---|
| 1354 | |
---|
| 1355 | if( ! stmt ) return; |
---|
| 1356 | |
---|
[bf2438c] | 1357 | stmt->push_back( |
---|
[bd4d011] | 1358 | new ExprStmt( |
---|
| 1359 | new UntypedExpr( |
---|
| 1360 | new NameExpr( "__thrd_start" ), |
---|
[09f357ec] | 1361 | { new VariableExpr( param ), new NameExpr("main") } |
---|
[bd4d011] | 1362 | ) |
---|
| 1363 | ) |
---|
| 1364 | ); |
---|
| 1365 | } |
---|
[68fe077a] | 1366 | }; |
---|
[6b0b624] | 1367 | |
---|
| 1368 | // Local Variables: // |
---|
| 1369 | // mode: c // |
---|
| 1370 | // tab-width: 4 // |
---|
| 1371 | // End: // |
---|
[1c01c58] | 1372 | |
---|