| 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 : 10
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Concurrency/Keywords.h"
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert> // for assert
|
|---|
| 19 | #include <string> // for string, operator==
|
|---|
| 20 |
|
|---|
| 21 | #include <iostream>
|
|---|
| 22 |
|
|---|
| 23 | #include "Common/Examine.h" // for isMainFor
|
|---|
| 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
|
|---|
| 39 | #include "Virtual/Tables.h"
|
|---|
| 40 |
|
|---|
| 41 | class Attribute;
|
|---|
| 42 |
|
|---|
| 43 | namespace Concurrency {
|
|---|
| 44 | inline static std::string getTypeIdName( std::string const & exception_name ) {
|
|---|
| 45 | return exception_name.empty() ? std::string() : Virtual::typeIdType( exception_name );
|
|---|
| 46 | }
|
|---|
| 47 | inline static std::string getVTableName( std::string const & exception_name ) {
|
|---|
| 48 | return exception_name.empty() ? std::string() : Virtual::vtableTypeName( exception_name );
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 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 |
|
|---|
| 59 | //=============================================================================================
|
|---|
| 60 | // Pass declarations
|
|---|
| 61 | //=============================================================================================
|
|---|
| 62 |
|
|---|
| 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 | //
|
|---|
| 72 | class ConcurrentSueKeyword : public WithDeclsToAdd {
|
|---|
| 73 | public:
|
|---|
| 74 |
|
|---|
| 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 ),
|
|---|
| 79 | context_error( context_error ), exception_name( exception_name ),
|
|---|
| 80 | typeid_name( getTypeIdName( exception_name ) ),
|
|---|
| 81 | vtable_name( getVTableName( exception_name ) ),
|
|---|
| 82 | needs_main( needs_main ), cast_target( cast_target ) {}
|
|---|
| 83 |
|
|---|
| 84 | virtual ~ConcurrentSueKeyword() {}
|
|---|
| 85 |
|
|---|
| 86 | Declaration * postmutate( StructDecl * decl );
|
|---|
| 87 | DeclarationWithType * postmutate( FunctionDecl * decl );
|
|---|
| 88 |
|
|---|
| 89 | void handle( StructDecl * );
|
|---|
| 90 | void addTypeId( StructDecl * );
|
|---|
| 91 | void addVtableForward( StructDecl * );
|
|---|
| 92 | FunctionDecl * forwardDeclare( StructDecl * );
|
|---|
| 93 | ObjectDecl * addField( StructDecl * );
|
|---|
| 94 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
|---|
| 95 | void addLockUnlockRoutines( StructDecl * );
|
|---|
| 96 |
|
|---|
| 97 | virtual bool is_target( StructDecl * decl ) = 0;
|
|---|
| 98 |
|
|---|
| 99 | Expression * postmutate( KeywordCastExpr * cast );
|
|---|
| 100 |
|
|---|
| 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;
|
|---|
| 106 | const std::string exception_name;
|
|---|
| 107 | const std::string typeid_name;
|
|---|
| 108 | const std::string vtable_name;
|
|---|
| 109 | bool needs_main;
|
|---|
| 110 | AggregateDecl::Aggregate cast_target;
|
|---|
| 111 |
|
|---|
| 112 | StructDecl * type_decl = nullptr;
|
|---|
| 113 | FunctionDecl * dtor_decl = nullptr;
|
|---|
| 114 | StructDecl * except_decl = nullptr;
|
|---|
| 115 | StructDecl * typeid_decl = nullptr;
|
|---|
| 116 | StructDecl * vtable_decl = nullptr;
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 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;
|
|---|
| 125 | // => thread$ __thrd_d;
|
|---|
| 126 | // }; };
|
|---|
| 127 | // static inline thread$ * get_thread( MyThread * this ) { return &this->__thrd_d; }
|
|---|
| 128 | //
|
|---|
| 129 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
|---|
| 130 | public:
|
|---|
| 131 |
|
|---|
| 132 | ThreadKeyword() : ConcurrentSueKeyword(
|
|---|
| 133 | "thread$",
|
|---|
| 134 | "__thrd",
|
|---|
| 135 | "get_thread",
|
|---|
| 136 | "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
|
|---|
| 137 | "ThreadCancelled",
|
|---|
| 138 | true,
|
|---|
| 139 | AggregateDecl::Thread
|
|---|
| 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 ) {
|
|---|
| 148 | PassVisitor< ThreadKeyword > impl;
|
|---|
| 149 | mutateAll( translationUnit, impl );
|
|---|
| 150 | }
|
|---|
| 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;
|
|---|
| 158 | // => coroutine$ __cor_d;
|
|---|
| 159 | // }; };
|
|---|
| 160 | // static inline coroutine$ * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
|
|---|
| 161 | //
|
|---|
| 162 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
|---|
| 163 | public:
|
|---|
| 164 |
|
|---|
| 165 | CoroutineKeyword() : ConcurrentSueKeyword(
|
|---|
| 166 | "coroutine$",
|
|---|
| 167 | "__cor",
|
|---|
| 168 | "get_coroutine",
|
|---|
| 169 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n",
|
|---|
| 170 | "CoroutineCancelled",
|
|---|
| 171 | true,
|
|---|
| 172 | AggregateDecl::Coroutine
|
|---|
| 173 | )
|
|---|
| 174 | {}
|
|---|
| 175 |
|
|---|
| 176 | virtual ~CoroutineKeyword() {}
|
|---|
| 177 |
|
|---|
| 178 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
|---|
| 179 |
|
|---|
| 180 | static void implement( std::list< Declaration * > & translationUnit ) {
|
|---|
| 181 | PassVisitor< CoroutineKeyword > impl;
|
|---|
| 182 | mutateAll( translationUnit, impl );
|
|---|
| 183 | }
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 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;
|
|---|
| 193 | // => monitor$ __mon_d;
|
|---|
| 194 | // }; };
|
|---|
| 195 | // static inline monitor$ * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
|
|---|
| 196 | //
|
|---|
| 197 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
|---|
| 198 | public:
|
|---|
| 199 |
|
|---|
| 200 | MonitorKeyword() : ConcurrentSueKeyword(
|
|---|
| 201 | "monitor$",
|
|---|
| 202 | "__mon",
|
|---|
| 203 | "get_monitor",
|
|---|
| 204 | "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n",
|
|---|
| 205 | "",
|
|---|
| 206 | false,
|
|---|
| 207 | AggregateDecl::Monitor
|
|---|
| 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 ) {
|
|---|
| 216 | PassVisitor< MonitorKeyword > impl;
|
|---|
| 217 | mutateAll( translationUnit, impl );
|
|---|
| 218 | }
|
|---|
| 219 | };
|
|---|
| 220 |
|
|---|
| 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(
|
|---|
| 233 | "generator$",
|
|---|
| 234 | "__generator_state",
|
|---|
| 235 | "get_generator",
|
|---|
| 236 | "Unable to find builtin type generator$\n",
|
|---|
| 237 | "",
|
|---|
| 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 |
|
|---|
| 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 ) {
|
|---|
| 295 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
|---|
| 296 | // monitor_guard_t __guard = { __monitors, 2 };
|
|---|
| 297 | // /*Some code*/ => /*Some code*/
|
|---|
| 298 | // } }
|
|---|
| 299 | //
|
|---|
| 300 | class MutexKeyword final {
|
|---|
| 301 | public:
|
|---|
| 302 |
|
|---|
| 303 | void postvisit( FunctionDecl * decl );
|
|---|
| 304 | void postvisit( StructDecl * decl );
|
|---|
| 305 | Statement * postmutate( MutexStmt * stmt );
|
|---|
| 306 |
|
|---|
| 307 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
|
|---|
| 308 | void validate( DeclarationWithType * );
|
|---|
| 309 | void addDtorStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
|---|
| 310 | void addStatements( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
|---|
| 311 | void addStatements( CompoundStmt * body, const std::list<Expression * > & args );
|
|---|
| 312 | void addThreadDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args );
|
|---|
| 313 |
|
|---|
| 314 | static void implement( std::list< Declaration * > & translationUnit ) {
|
|---|
| 315 | PassVisitor< MutexKeyword > impl;
|
|---|
| 316 | acceptAll( translationUnit, impl );
|
|---|
| 317 | mutateAll( translationUnit, impl );
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | private:
|
|---|
| 321 | StructDecl* monitor_decl = nullptr;
|
|---|
| 322 | StructDecl* guard_decl = nullptr;
|
|---|
| 323 | StructDecl* dtor_guard_decl = nullptr;
|
|---|
| 324 | StructDecl* thread_guard_decl = nullptr;
|
|---|
| 325 | StructDecl* lock_guard_decl = nullptr;
|
|---|
| 326 |
|
|---|
| 327 | static std::unique_ptr< Type > generic_func;
|
|---|
| 328 | };
|
|---|
| 329 |
|
|---|
| 330 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
|---|
| 331 | new FunctionType(
|
|---|
| 332 | noQualifiers,
|
|---|
| 333 | true
|
|---|
| 334 | )
|
|---|
| 335 | );
|
|---|
| 336 |
|
|---|
| 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 ) {
|
|---|
| 340 | // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
|---|
| 341 | // monitor_guard_t __guard = { __monitors, 2 };
|
|---|
| 342 | // /*Some code*/ => /*Some code*/
|
|---|
| 343 | // } }
|
|---|
| 344 | //
|
|---|
| 345 | class ThreadStarter final {
|
|---|
| 346 | public:
|
|---|
| 347 |
|
|---|
| 348 | void postvisit( FunctionDecl * decl );
|
|---|
| 349 | void previsit ( StructDecl * decl );
|
|---|
| 350 |
|
|---|
| 351 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
|
|---|
| 352 |
|
|---|
| 353 | static void implement( std::list< Declaration * > & translationUnit ) {
|
|---|
| 354 | PassVisitor< ThreadStarter > impl;
|
|---|
| 355 | acceptAll( translationUnit, impl );
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | private :
|
|---|
| 359 | bool thread_ctor_seen = false;
|
|---|
| 360 | StructDecl * thread_decl = nullptr;
|
|---|
| 361 | };
|
|---|
| 362 |
|
|---|
| 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 );
|
|---|
| 370 | GeneratorKeyword ::implement( translationUnit );
|
|---|
| 371 | SuspendKeyword ::implement( translationUnit );
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
|---|
| 375 | MutexKeyword ::implement( translationUnit );
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
|---|
| 379 | ThreadStarter ::implement( translationUnit );
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | //=============================================================================================
|
|---|
| 383 | // Generic keyword implementation
|
|---|
| 384 | //=============================================================================================
|
|---|
| 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 |
|
|---|
| 396 | Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
|
|---|
| 397 | if( decl->name == type_name && decl->body ) {
|
|---|
| 398 | assert( !type_decl );
|
|---|
| 399 | type_decl = decl;
|
|---|
| 400 | }
|
|---|
| 401 | else if ( is_target(decl) ) {
|
|---|
| 402 | handle( decl );
|
|---|
| 403 | }
|
|---|
| 404 | else if ( !except_decl && exception_name == decl->name && decl->body ) {
|
|---|
| 405 | except_decl = decl;
|
|---|
| 406 | }
|
|---|
| 407 | else if ( !typeid_decl && typeid_name == decl->name && decl->body ) {
|
|---|
| 408 | typeid_decl = decl;
|
|---|
| 409 | }
|
|---|
| 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) );
|
|---|
| 415 | return decl;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) {
|
|---|
| 419 | if ( type_decl && isDestructorFor( decl, type_decl ) )
|
|---|
| 420 | dtor_decl = decl;
|
|---|
| 421 | else if ( vtable_name.empty() || !decl->has_body() )
|
|---|
| 422 | ;
|
|---|
| 423 | else if ( auto param = isMainFor( decl, cast_target ) ) {
|
|---|
| 424 | if ( !vtable_decl ) {
|
|---|
| 425 | SemanticError( decl, context_error );
|
|---|
| 426 | }
|
|---|
| 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 |
|
|---|
| 432 | std::list< Expression * > poly_args = { new TypeExpr( struct_type->clone() ) };
|
|---|
| 433 | ObjectDecl * vtable_object = Virtual::makeVtableInstance(
|
|---|
| 434 | "_default_vtable_object_declaration",
|
|---|
| 435 | vtable_decl->makeInst( poly_args ), struct_type, nullptr );
|
|---|
| 436 | declsToAddAfter.push_back( vtable_object );
|
|---|
| 437 | declsToAddAfter.push_back(
|
|---|
| 438 | new ObjectDecl(
|
|---|
| 439 | Virtual::concurrentDefaultVTableName(),
|
|---|
| 440 | noStorageClasses,
|
|---|
| 441 | LinkageSpec::Cforall,
|
|---|
| 442 | /* bitfieldWidth */ nullptr,
|
|---|
| 443 | new ReferenceType( Type::Const, vtable_object->type->clone() ),
|
|---|
| 444 | new SingleInit( new VariableExpr( vtable_object ) )
|
|---|
| 445 | )
|
|---|
| 446 | );
|
|---|
| 447 | declsToAddAfter.push_back( Virtual::makeGetExceptionFunction(
|
|---|
| 448 | vtable_object, except_decl->makeInst( std::move( poly_args ) )
|
|---|
| 449 | ) );
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | return decl;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
|
|---|
| 456 | if ( cast_target == cast->target ) {
|
|---|
| 457 | // convert (thread &)t to (thread$ &)*get_thread(t), etc.
|
|---|
| 458 | if( !type_decl ) SemanticError( cast, context_error );
|
|---|
| 459 | if( !dtor_decl ) SemanticError( cast, context_error );
|
|---|
| 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;
|
|---|
| 464 | }
|
|---|
| 465 | return cast;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
|---|
| 469 | if( ! decl->body ) return;
|
|---|
| 470 |
|
|---|
| 471 | if( !type_decl ) SemanticError( decl, context_error );
|
|---|
| 472 | if( !dtor_decl ) SemanticError( decl, context_error );
|
|---|
| 473 |
|
|---|
| 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 | }
|
|---|
| 481 | FunctionDecl * func = forwardDeclare( decl );
|
|---|
| 482 | ObjectDecl * field = addField( decl );
|
|---|
| 483 |
|
|---|
| 484 | // add get_.* routine
|
|---|
| 485 | addRoutines( field, func );
|
|---|
| 486 | // add lock/unlock routines to monitors for use by mutex stmt
|
|---|
| 487 | addLockUnlockRoutines( decl );
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 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 )
|
|---|
| 495 | ) );
|
|---|
| 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 | ) );
|
|---|
| 508 | ObjectDecl * vtable_object = Virtual::makeVtableForward(
|
|---|
| 509 | "_default_vtable_object_declaration",
|
|---|
| 510 | vtable_decl->makeInst( move( poly_args ) ) );
|
|---|
| 511 | declsToAddBefore.push_back( vtable_object );
|
|---|
| 512 | declsToAddBefore.push_back(
|
|---|
| 513 | new ObjectDecl(
|
|---|
| 514 | Virtual::concurrentDefaultVTableName(),
|
|---|
| 515 | Type::StorageClasses( Type::Extern ),
|
|---|
| 516 | LinkageSpec::Cforall,
|
|---|
| 517 | /* bitfieldWidth */ nullptr,
|
|---|
| 518 | new ReferenceType( Type::Const, vtable_object->type->clone() ),
|
|---|
| 519 | /* init */ nullptr
|
|---|
| 520 | )
|
|---|
| 521 | );
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 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 |
|
|---|
| 531 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
|---|
| 532 | ObjectDecl * this_decl = new ObjectDecl(
|
|---|
| 533 | "this",
|
|---|
| 534 | noStorageClasses,
|
|---|
| 535 | LinkageSpec::Cforall,
|
|---|
| 536 | nullptr,
|
|---|
| 537 | new ReferenceType(
|
|---|
| 538 | noQualifiers,
|
|---|
| 539 | new StructInstType(
|
|---|
| 540 | noQualifiers,
|
|---|
| 541 | decl
|
|---|
| 542 | )
|
|---|
| 543 | ),
|
|---|
| 544 | nullptr
|
|---|
| 545 | );
|
|---|
| 546 |
|
|---|
| 547 | get_type->get_parameters().push_back( this_decl->clone() );
|
|---|
| 548 | get_type->get_returnVals().push_back(
|
|---|
| 549 | new ObjectDecl(
|
|---|
| 550 | "ret",
|
|---|
| 551 | noStorageClasses,
|
|---|
| 552 | LinkageSpec::Cforall,
|
|---|
| 553 | nullptr,
|
|---|
| 554 | new PointerType(
|
|---|
| 555 | noQualifiers,
|
|---|
| 556 | new StructInstType(
|
|---|
| 557 | noQualifiers,
|
|---|
| 558 | type_decl
|
|---|
| 559 | )
|
|---|
| 560 | ),
|
|---|
| 561 | nullptr
|
|---|
| 562 | )
|
|---|
| 563 | );
|
|---|
| 564 | fixupGenerics(get_type, decl);
|
|---|
| 565 |
|
|---|
| 566 | FunctionDecl * get_decl = new FunctionDecl(
|
|---|
| 567 | getter_name,
|
|---|
| 568 | Type::Static,
|
|---|
| 569 | LinkageSpec::Cforall,
|
|---|
| 570 | get_type,
|
|---|
| 571 | nullptr,
|
|---|
| 572 | { new Attribute("const") },
|
|---|
| 573 | Type::Inline
|
|---|
| 574 | );
|
|---|
| 575 |
|
|---|
| 576 | FunctionDecl * main_decl = nullptr;
|
|---|
| 577 |
|
|---|
| 578 | if( needs_main ) {
|
|---|
| 579 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
|---|
| 580 |
|
|---|
| 581 | main_type->get_parameters().push_back( this_decl->clone() );
|
|---|
| 582 |
|
|---|
| 583 | main_decl = new FunctionDecl(
|
|---|
| 584 | "main",
|
|---|
| 585 | noStorageClasses,
|
|---|
| 586 | LinkageSpec::Cforall,
|
|---|
| 587 | main_type,
|
|---|
| 588 | nullptr
|
|---|
| 589 | );
|
|---|
| 590 | fixupGenerics(main_type, decl);
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | delete this_decl;
|
|---|
| 594 |
|
|---|
| 595 | declsToAddBefore.push_back( forward );
|
|---|
| 596 | if( needs_main ) declsToAddBefore.push_back( main_decl );
|
|---|
| 597 | declsToAddBefore.push_back( get_decl );
|
|---|
| 598 |
|
|---|
| 599 | return get_decl;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
|
|---|
| 603 | ObjectDecl * field = new ObjectDecl(
|
|---|
| 604 | field_name,
|
|---|
| 605 | noStorageClasses,
|
|---|
| 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 |
|
|---|
| 620 | // This function adds the get_.* routine body for coroutines, monitors etc
|
|---|
| 621 | // after their corresponding struct has been made
|
|---|
| 622 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
|---|
| 623 | CompoundStmt * statement = new CompoundStmt();
|
|---|
| 624 | statement->push_back(
|
|---|
| 625 | new ReturnStmt(
|
|---|
| 626 | new AddressExpr(
|
|---|
| 627 | new MemberExpr(
|
|---|
| 628 | field,
|
|---|
| 629 | new CastExpr(
|
|---|
| 630 | new VariableExpr( func->get_functionType()->get_parameters().front() ),
|
|---|
| 631 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone(),
|
|---|
| 632 | false
|
|---|
| 633 | )
|
|---|
| 634 | )
|
|---|
| 635 | )
|
|---|
| 636 | )
|
|---|
| 637 | );
|
|---|
| 638 |
|
|---|
| 639 | FunctionDecl * get_decl = func->clone();
|
|---|
| 640 |
|
|---|
| 641 | get_decl->set_statements( statement );
|
|---|
| 642 |
|
|---|
| 643 | declsToAddAfter.push_back( get_decl );
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 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() );
|
|---|
| 672 | fixupGenerics(lock_fn_type, decl);
|
|---|
| 673 | fixupGenerics(unlock_fn_type, decl);
|
|---|
| 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));
|
|---|
| 683 | }
|
|---|
| 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(
|
|---|
| 702 | new ExprStmt(
|
|---|
| 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));
|
|---|
| 718 | }
|
|---|
| 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(
|
|---|
| 738 | new ExprStmt(
|
|---|
| 739 | new UntypedExpr(
|
|---|
| 740 | new NameExpr( "unlock" ),
|
|---|
| 741 | {
|
|---|
| 742 | get_monitor_unlock
|
|---|
| 743 | }
|
|---|
| 744 | )
|
|---|
| 745 | )
|
|---|
| 746 | );
|
|---|
| 747 | unlock_decl->set_statements( unlock_statement );
|
|---|
| 748 |
|
|---|
| 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 |
|
|---|
| 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;
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 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?
|
|---|
| 777 | auto param = isMainFor( func, AggregateDecl::Aggregate::Generator );
|
|---|
| 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 ) );
|
|---|
| 958 | expr->location = loc;
|
|---|
| 959 |
|
|---|
| 960 | // Change this statement into a regular expr
|
|---|
| 961 | assert(expr);
|
|---|
| 962 | auto nstmt = new ExprStmt( expr );
|
|---|
| 963 | nstmt->location = loc;
|
|---|
| 964 | return nstmt;
|
|---|
| 965 | }
|
|---|
| 966 |
|
|---|
| 967 |
|
|---|
| 968 | //=============================================================================================
|
|---|
| 969 | // Mutex keyword implementation
|
|---|
| 970 | //=============================================================================================
|
|---|
| 971 |
|
|---|
| 972 | void MutexKeyword::postvisit(FunctionDecl* decl) {
|
|---|
| 973 |
|
|---|
| 974 | bool first = false;
|
|---|
| 975 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
|
|---|
| 976 | bool const isDtor = CodeGen::isDestructor( decl->name );
|
|---|
| 977 |
|
|---|
| 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) {
|
|---|
| 982 | // This reflects MutexKeyword::validate, except does not produce an error.
|
|---|
| 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 | }
|
|---|
| 1004 |
|
|---|
| 1005 | // Monitors can't be constructed with mutual exclusion
|
|---|
| 1006 | if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
|
|---|
| 1007 |
|
|---|
| 1008 | // It makes no sense to have multiple mutex parameters for the destructor
|
|---|
| 1009 | if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
|
|---|
| 1010 |
|
|---|
| 1011 | // Make sure all the mutex arguments are monitors
|
|---|
| 1012 | for(auto arg : mutexArgs) {
|
|---|
| 1013 | validate( arg );
|
|---|
| 1014 | }
|
|---|
| 1015 |
|
|---|
| 1016 | // Check if we need to instrument the body
|
|---|
| 1017 | CompoundStmt* body = decl->get_statements();
|
|---|
| 1018 | if( ! body ) return;
|
|---|
| 1019 |
|
|---|
| 1020 | // Do we have the required headers
|
|---|
| 1021 | if( !monitor_decl || !guard_decl || !dtor_guard_decl )
|
|---|
| 1022 | SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>\n" );
|
|---|
| 1023 |
|
|---|
| 1024 | // Instrument the body
|
|---|
| 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 );
|
|---|
| 1033 | }
|
|---|
| 1034 | else {
|
|---|
| 1035 | addStatements( decl, body, mutexArgs );
|
|---|
| 1036 | }
|
|---|
| 1037 | }
|
|---|
| 1038 |
|
|---|
| 1039 | void MutexKeyword::postvisit(StructDecl* decl) {
|
|---|
| 1040 |
|
|---|
| 1041 | if( decl->name == "monitor$" && decl->body ) {
|
|---|
| 1042 | assert( !monitor_decl );
|
|---|
| 1043 | monitor_decl = decl;
|
|---|
| 1044 | }
|
|---|
| 1045 | else if( decl->name == "monitor_guard_t" && decl->body ) {
|
|---|
| 1046 | assert( !guard_decl );
|
|---|
| 1047 | guard_decl = decl;
|
|---|
| 1048 | }
|
|---|
| 1049 | else if( decl->name == "monitor_dtor_guard_t" && decl->body ) {
|
|---|
| 1050 | assert( !dtor_guard_decl );
|
|---|
| 1051 | dtor_guard_decl = decl;
|
|---|
| 1052 | }
|
|---|
| 1053 | else if( decl->name == "thread_dtor_guard_t" && decl->body ) {
|
|---|
| 1054 | assert( !thread_guard_decl );
|
|---|
| 1055 | thread_guard_decl = decl;
|
|---|
| 1056 | }
|
|---|
| 1057 | else if ( decl->name == "__mutex_stmt_lock_guard" && decl->body ) {
|
|---|
| 1058 | assert( !lock_guard_decl );
|
|---|
| 1059 | lock_guard_decl = decl;
|
|---|
| 1060 | }
|
|---|
| 1061 | }
|
|---|
| 1062 |
|
|---|
| 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 |
|
|---|
| 1071 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
|
|---|
| 1072 | std::list<DeclarationWithType*> mutexArgs;
|
|---|
| 1073 |
|
|---|
| 1074 | bool once = true;
|
|---|
| 1075 | for( auto arg : decl->get_functionType()->get_parameters()) {
|
|---|
| 1076 | //Find mutex arguments
|
|---|
| 1077 | Type* ty = arg->get_type();
|
|---|
| 1078 | if( ! ty->get_mutex() ) continue;
|
|---|
| 1079 |
|
|---|
| 1080 | if(once) {first = true;}
|
|---|
| 1081 | once = false;
|
|---|
| 1082 |
|
|---|
| 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
|
|---|
| 1094 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
|---|
| 1095 | if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " );
|
|---|
| 1096 |
|
|---|
| 1097 | //Make sure the we are pointing directly to a type
|
|---|
| 1098 | Type* base = rty->get_base();
|
|---|
| 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 " );
|
|---|
| 1101 |
|
|---|
| 1102 | //Make sure that typed isn't mutex
|
|---|
| 1103 | if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " );
|
|---|
| 1104 | }
|
|---|
| 1105 |
|
|---|
| 1106 | void MutexKeyword::addDtorStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
|---|
| 1107 | Type * arg_type = args.front()->get_type()->clone();
|
|---|
| 1108 | arg_type->set_mutex( false );
|
|---|
| 1109 |
|
|---|
| 1110 | ObjectDecl * monitors = new ObjectDecl(
|
|---|
| 1111 | "__monitor",
|
|---|
| 1112 | noStorageClasses,
|
|---|
| 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" ),
|
|---|
| 1124 | { new CastExpr( new VariableExpr( args.front() ), arg_type, false ) }
|
|---|
| 1125 | ))
|
|---|
| 1126 | );
|
|---|
| 1127 |
|
|---|
| 1128 | assert(generic_func);
|
|---|
| 1129 |
|
|---|
| 1130 | //in reverse order :
|
|---|
| 1131 | // monitor_dtor_guard_t __guard = { __monitors, func };
|
|---|
| 1132 | body->push_front(
|
|---|
| 1133 | new DeclStmt( new ObjectDecl(
|
|---|
| 1134 | "__guard",
|
|---|
| 1135 | noStorageClasses,
|
|---|
| 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 ) ) ),
|
|---|
| 1145 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ),
|
|---|
| 1146 | new SingleInit( new ConstantExpr( Constant::from_bool( false ) ) )
|
|---|
| 1147 | },
|
|---|
| 1148 | noDesignators,
|
|---|
| 1149 | true
|
|---|
| 1150 | )
|
|---|
| 1151 | ))
|
|---|
| 1152 | );
|
|---|
| 1153 |
|
|---|
| 1154 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
|---|
| 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 | );
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 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,
|
|---|
| 1204 | //new TypeofType( noQualifiers, args.front()->clone() )
|
|---|
| 1205 | new TypeofType( noQualifiers, new UntypedExpr(
|
|---|
| 1206 | new NameExpr( "__get_mutexstmt_lock_type" ),
|
|---|
| 1207 | { args.front()->clone() }
|
|---|
| 1208 | )
|
|---|
| 1209 | )
|
|---|
| 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 ){
|
|---|
| 1217 | return new SingleInit( new UntypedExpr(
|
|---|
| 1218 | new NameExpr( "__get_mutexstmt_lock_ptr" ),
|
|---|
| 1219 | { var }
|
|---|
| 1220 | ) );
|
|---|
| 1221 | //return new SingleInit( new AddressExpr( var ) );
|
|---|
| 1222 | })
|
|---|
| 1223 | )
|
|---|
| 1224 | );
|
|---|
| 1225 |
|
|---|
| 1226 | StructInstType * lock_guard_struct = new StructInstType( noQualifiers, lock_guard_decl );
|
|---|
| 1227 | TypeExpr * lock_type_expr = new TypeExpr(
|
|---|
| 1228 | new TypeofType( noQualifiers, new UntypedExpr(
|
|---|
| 1229 | new NameExpr( "__get_mutexstmt_lock_type" ),
|
|---|
| 1230 | { args.front()->clone() }
|
|---|
| 1231 | )
|
|---|
| 1232 | )
|
|---|
| 1233 | );
|
|---|
| 1234 |
|
|---|
| 1235 | lock_guard_struct->parameters.push_back( lock_type_expr ) ;
|
|---|
| 1236 |
|
|---|
| 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,
|
|---|
| 1245 | lock_guard_struct,
|
|---|
| 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 |
|
|---|
| 1261 | void MutexKeyword::addStatements( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
|---|
| 1262 | ObjectDecl * monitors = new ObjectDecl(
|
|---|
| 1263 | "__monitors",
|
|---|
| 1264 | noStorageClasses,
|
|---|
| 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(
|
|---|
| 1281 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
|
|---|
| 1282 | Type * type = var->get_type()->clone();
|
|---|
| 1283 | type->set_mutex( false );
|
|---|
| 1284 | return new SingleInit( new UntypedExpr(
|
|---|
| 1285 | new NameExpr( "get_monitor" ),
|
|---|
| 1286 | { new CastExpr( new VariableExpr( var ), type, false ) }
|
|---|
| 1287 | ) );
|
|---|
| 1288 | })
|
|---|
| 1289 | )
|
|---|
| 1290 | );
|
|---|
| 1291 |
|
|---|
| 1292 | assert(generic_func);
|
|---|
| 1293 |
|
|---|
| 1294 | // in reverse order :
|
|---|
| 1295 | // monitor_guard_t __guard = { __monitors, #, func };
|
|---|
| 1296 | body->push_front(
|
|---|
| 1297 | new DeclStmt( new ObjectDecl(
|
|---|
| 1298 | "__guard",
|
|---|
| 1299 | noStorageClasses,
|
|---|
| 1300 | LinkageSpec::Cforall,
|
|---|
| 1301 | nullptr,
|
|---|
| 1302 | new StructInstType(
|
|---|
| 1303 | noQualifiers,
|
|---|
| 1304 | guard_decl
|
|---|
| 1305 | ),
|
|---|
| 1306 | new ListInit(
|
|---|
| 1307 | {
|
|---|
| 1308 | new SingleInit( new VariableExpr( monitors ) ),
|
|---|
| 1309 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
|---|
| 1310 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) )
|
|---|
| 1311 | },
|
|---|
| 1312 | noDesignators,
|
|---|
| 1313 | true
|
|---|
| 1314 | )
|
|---|
| 1315 | ))
|
|---|
| 1316 | );
|
|---|
| 1317 |
|
|---|
| 1318 | //monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
|
|---|
| 1319 | body->push_front( new DeclStmt( monitors) );
|
|---|
| 1320 | }
|
|---|
| 1321 |
|
|---|
| 1322 | //=============================================================================================
|
|---|
| 1323 | // General entry routine
|
|---|
| 1324 | //=============================================================================================
|
|---|
| 1325 | void ThreadStarter::previsit( StructDecl * decl ) {
|
|---|
| 1326 | if( decl->name == "thread$" && decl->body ) {
|
|---|
| 1327 | assert( !thread_decl );
|
|---|
| 1328 | thread_decl = decl;
|
|---|
| 1329 | }
|
|---|
| 1330 | }
|
|---|
| 1331 |
|
|---|
| 1332 | void ThreadStarter::postvisit(FunctionDecl * decl) {
|
|---|
| 1333 | if( ! CodeGen::isConstructor(decl->name) ) return;
|
|---|
| 1334 |
|
|---|
| 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 |
|
|---|
| 1341 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
|---|
| 1342 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
|---|
| 1343 | if( type && type->get_baseStruct()->is_thread() ) {
|
|---|
| 1344 | if( !thread_decl || !thread_ctor_seen ) {
|
|---|
| 1345 | SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>");
|
|---|
| 1346 | }
|
|---|
| 1347 |
|
|---|
| 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 |
|
|---|
| 1357 | stmt->push_back(
|
|---|
| 1358 | new ExprStmt(
|
|---|
| 1359 | new UntypedExpr(
|
|---|
| 1360 | new NameExpr( "__thrd_start" ),
|
|---|
| 1361 | { new VariableExpr( param ), new NameExpr("main") }
|
|---|
| 1362 | )
|
|---|
| 1363 | )
|
|---|
| 1364 | );
|
|---|
| 1365 | }
|
|---|
| 1366 | };
|
|---|
| 1367 |
|
|---|
| 1368 | // Local Variables: //
|
|---|
| 1369 | // mode: c //
|
|---|
| 1370 | // tab-width: 4 //
|
|---|
| 1371 | // End: //
|
|---|
| 1372 |
|
|---|