[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 {
|
---|
[1c01c58] | 44 | inline static std::string getVTableName( std::string const & exception_name ) {
|
---|
| 45 | return exception_name.empty() ? std::string() : Virtual::vtableTypeName(exception_name);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[64adb03] | 48 | //=============================================================================================
|
---|
[2065609] | 49 | // Pass declarations
|
---|
[64adb03] | 50 | //=============================================================================================
|
---|
| 51 |
|
---|
[bcda04c] | 52 | //-----------------------------------------------------------------------------
|
---|
| 53 | //Handles sue type declarations :
|
---|
| 54 | // sue MyType { struct MyType {
|
---|
| 55 | // int data; int data;
|
---|
| 56 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 57 | // => NewField_t newField;
|
---|
| 58 | // }; };
|
---|
| 59 | // static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
|
---|
| 60 | //
|
---|
[2065609] | 61 | class ConcurrentSueKeyword : public WithDeclsToAdd {
|
---|
[bcda04c] | 62 | public:
|
---|
| 63 |
|
---|
[1c01c58] | 64 | ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name,
|
---|
| 65 | std::string&& getter_name, std::string&& context_error, std::string&& exception_name,
|
---|
| 66 | bool needs_main, AggregateDecl::Aggregate cast_target ) :
|
---|
| 67 | type_name( type_name ), field_name( field_name ), getter_name( getter_name ),
|
---|
| 68 | context_error( context_error ), vtable_name( getVTableName( exception_name ) ),
|
---|
| 69 | needs_main( needs_main ), cast_target( cast_target ) {}
|
---|
[bcda04c] | 70 |
|
---|
| 71 | virtual ~ConcurrentSueKeyword() {}
|
---|
| 72 |
|
---|
[9a705dc8] | 73 | Declaration * postmutate( StructDecl * decl );
|
---|
[6c3a5ac1] | 74 | DeclarationWithType * postmutate( FunctionDecl * decl );
|
---|
[bcda04c] | 75 |
|
---|
| 76 | void handle( StructDecl * );
|
---|
[1c01c58] | 77 | void addVtableForward( StructDecl * );
|
---|
[bcda04c] | 78 | FunctionDecl * forwardDeclare( StructDecl * );
|
---|
| 79 | ObjectDecl * addField( StructDecl * );
|
---|
[2f9a722] | 80 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
---|
[bcda04c] | 81 |
|
---|
| 82 | virtual bool is_target( StructDecl * decl ) = 0;
|
---|
| 83 |
|
---|
[9a705dc8] | 84 | Expression * postmutate( KeywordCastExpr * cast );
|
---|
| 85 |
|
---|
[bcda04c] | 86 | private:
|
---|
| 87 | const std::string type_name;
|
---|
| 88 | const std::string field_name;
|
---|
| 89 | const std::string getter_name;
|
---|
| 90 | const std::string context_error;
|
---|
[1c01c58] | 91 | const std::string vtable_name;
|
---|
[bd4d011] | 92 | bool needs_main;
|
---|
[312029a] | 93 | AggregateDecl::Aggregate cast_target;
|
---|
[bcda04c] | 94 |
|
---|
[6c3a5ac1] | 95 | StructDecl * type_decl = nullptr;
|
---|
| 96 | FunctionDecl * dtor_decl = nullptr;
|
---|
[1c01c58] | 97 | StructDecl * vtable_decl = nullptr;
|
---|
[bcda04c] | 98 | };
|
---|
| 99 |
|
---|
| 100 |
|
---|
[64adb03] | 101 | //-----------------------------------------------------------------------------
|
---|
| 102 | //Handles thread type declarations :
|
---|
| 103 | // thread Mythread { struct MyThread {
|
---|
| 104 | // int data; int data;
|
---|
| 105 | // a_struct_t more_data; a_struct_t more_data;
|
---|
[ac2b598] | 106 | // => $thread __thrd_d;
|
---|
[64adb03] | 107 | // }; };
|
---|
[ac2b598] | 108 | // static inline $thread * get_thread( MyThread * this ) { return &this->__thrd_d; }
|
---|
[64adb03] | 109 | //
|
---|
[bcda04c] | 110 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 111 | public:
|
---|
| 112 |
|
---|
[bcda04c] | 113 | ThreadKeyword() : ConcurrentSueKeyword(
|
---|
[ac2b598] | 114 | "$thread",
|
---|
[bcda04c] | 115 | "__thrd",
|
---|
| 116 | "get_thread",
|
---|
[6c3a5ac1] | 117 | "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
|
---|
[1c01c58] | 118 | "",
|
---|
[9a705dc8] | 119 | true,
|
---|
[312029a] | 120 | AggregateDecl::Thread
|
---|
[bcda04c] | 121 | )
|
---|
| 122 | {}
|
---|
| 123 |
|
---|
| 124 | virtual ~ThreadKeyword() {}
|
---|
| 125 |
|
---|
| 126 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
|
---|
| 127 |
|
---|
| 128 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 129 | PassVisitor< ThreadKeyword > impl;
|
---|
[9a705dc8] | 130 | mutateAll( translationUnit, impl );
|
---|
[bcda04c] | 131 | }
|
---|
[64adb03] | 132 | };
|
---|
| 133 |
|
---|
| 134 | //-----------------------------------------------------------------------------
|
---|
| 135 | //Handles coroutine type declarations :
|
---|
| 136 | // coroutine MyCoroutine { struct MyCoroutine {
|
---|
| 137 | // int data; int data;
|
---|
| 138 | // a_struct_t more_data; a_struct_t more_data;
|
---|
[ac2b598] | 139 | // => $coroutine __cor_d;
|
---|
[64adb03] | 140 | // }; };
|
---|
[ac2b598] | 141 | // static inline $coroutine * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
|
---|
[64adb03] | 142 | //
|
---|
[bcda04c] | 143 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 144 | public:
|
---|
| 145 |
|
---|
[bcda04c] | 146 | CoroutineKeyword() : ConcurrentSueKeyword(
|
---|
[ac2b598] | 147 | "$coroutine",
|
---|
[bcda04c] | 148 | "__cor",
|
---|
| 149 | "get_coroutine",
|
---|
[6c3a5ac1] | 150 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n",
|
---|
[1c01c58] | 151 | "CoroutineCancelled",
|
---|
[9a705dc8] | 152 | true,
|
---|
[312029a] | 153 | AggregateDecl::Coroutine
|
---|
[bcda04c] | 154 | )
|
---|
| 155 | {}
|
---|
[b32ada31] | 156 |
|
---|
[bcda04c] | 157 | virtual ~CoroutineKeyword() {}
|
---|
| 158 |
|
---|
| 159 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
---|
[b32ada31] | 160 |
|
---|
| 161 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 162 | PassVisitor< CoroutineKeyword > impl;
|
---|
[9a705dc8] | 163 | mutateAll( translationUnit, impl );
|
---|
[b32ada31] | 164 | }
|
---|
[64adb03] | 165 | };
|
---|
| 166 |
|
---|
[427854b] | 167 |
|
---|
| 168 |
|
---|
[64adb03] | 169 | //-----------------------------------------------------------------------------
|
---|
| 170 | //Handles monitor type declarations :
|
---|
| 171 | // monitor MyMonitor { struct MyMonitor {
|
---|
| 172 | // int data; int data;
|
---|
| 173 | // a_struct_t more_data; a_struct_t more_data;
|
---|
[ac2b598] | 174 | // => $monitor __mon_d;
|
---|
[64adb03] | 175 | // }; };
|
---|
[ac2b598] | 176 | // static inline $monitor * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
|
---|
[64adb03] | 177 | //
|
---|
[bcda04c] | 178 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 179 | public:
|
---|
| 180 |
|
---|
[bcda04c] | 181 | MonitorKeyword() : ConcurrentSueKeyword(
|
---|
[ac2b598] | 182 | "$monitor",
|
---|
[bcda04c] | 183 | "__mon",
|
---|
| 184 | "get_monitor",
|
---|
[6c3a5ac1] | 185 | "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n",
|
---|
[1c01c58] | 186 | "",
|
---|
[9a705dc8] | 187 | false,
|
---|
[312029a] | 188 | AggregateDecl::Monitor
|
---|
[bcda04c] | 189 | )
|
---|
| 190 | {}
|
---|
| 191 |
|
---|
| 192 | virtual ~MonitorKeyword() {}
|
---|
| 193 |
|
---|
| 194 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
|
---|
| 195 |
|
---|
| 196 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 197 | PassVisitor< MonitorKeyword > impl;
|
---|
[9a705dc8] | 198 | mutateAll( translationUnit, impl );
|
---|
[bcda04c] | 199 | }
|
---|
[64adb03] | 200 | };
|
---|
| 201 |
|
---|
[427854b] | 202 | //-----------------------------------------------------------------------------
|
---|
| 203 | //Handles generator type declarations :
|
---|
| 204 | // generator MyGenerator { struct MyGenerator {
|
---|
| 205 | // int data; int data;
|
---|
| 206 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 207 | // => int __gen_next;
|
---|
| 208 | // }; };
|
---|
| 209 | //
|
---|
| 210 | class GeneratorKeyword final : public ConcurrentSueKeyword {
|
---|
| 211 | public:
|
---|
| 212 |
|
---|
| 213 | GeneratorKeyword() : ConcurrentSueKeyword(
|
---|
| 214 | "$generator",
|
---|
| 215 | "__generator_state",
|
---|
| 216 | "get_generator",
|
---|
| 217 | "Unable to find builtin type $generator\n",
|
---|
[1c01c58] | 218 | "",
|
---|
[427854b] | 219 | true,
|
---|
| 220 | AggregateDecl::Generator
|
---|
| 221 | )
|
---|
| 222 | {}
|
---|
| 223 |
|
---|
| 224 | virtual ~GeneratorKeyword() {}
|
---|
| 225 |
|
---|
| 226 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_generator(); }
|
---|
| 227 |
|
---|
| 228 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
| 229 | PassVisitor< GeneratorKeyword > impl;
|
---|
| 230 | mutateAll( translationUnit, impl );
|
---|
| 231 | }
|
---|
| 232 | };
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 | //-----------------------------------------------------------------------------
|
---|
| 236 | class SuspendKeyword final : public WithStmtsToAdd, public WithGuards {
|
---|
| 237 | public:
|
---|
| 238 | SuspendKeyword() = default;
|
---|
| 239 | virtual ~SuspendKeyword() = default;
|
---|
| 240 |
|
---|
| 241 | void premutate( FunctionDecl * );
|
---|
| 242 | DeclarationWithType * postmutate( FunctionDecl * );
|
---|
| 243 |
|
---|
| 244 | Statement * postmutate( SuspendStmt * );
|
---|
| 245 |
|
---|
| 246 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
| 247 | PassVisitor< SuspendKeyword > impl;
|
---|
| 248 | mutateAll( translationUnit, impl );
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | private:
|
---|
| 252 | bool is_real_suspend( FunctionDecl * );
|
---|
| 253 |
|
---|
| 254 | Statement * make_generator_suspend( SuspendStmt * );
|
---|
| 255 | Statement * make_coroutine_suspend( SuspendStmt * );
|
---|
| 256 |
|
---|
| 257 | struct LabelPair {
|
---|
| 258 | Label obj;
|
---|
| 259 | int idx;
|
---|
| 260 | };
|
---|
| 261 |
|
---|
| 262 | LabelPair make_label() {
|
---|
| 263 | labels.push_back( gen.newLabel("generator") );
|
---|
| 264 | return { labels.back(), int(labels.size()) };
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | DeclarationWithType * in_generator = nullptr;
|
---|
| 268 | FunctionDecl * decl_suspend = nullptr;
|
---|
| 269 | std::vector<Label> labels;
|
---|
| 270 | ControlStruct::LabelGenerator & gen = *ControlStruct::LabelGenerator::getGenerator();
|
---|
| 271 | };
|
---|
| 272 |
|
---|
[64adb03] | 273 | //-----------------------------------------------------------------------------
|
---|
| 274 | //Handles mutex routines definitions :
|
---|
| 275 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
[ac2b598] | 276 | // $monitor * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[64adb03] | 277 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
| 278 | // /*Some code*/ => /*Some code*/
|
---|
| 279 | // } }
|
---|
| 280 | //
|
---|
[2065609] | 281 | class MutexKeyword final {
|
---|
[64adb03] | 282 | public:
|
---|
| 283 |
|
---|
[2065609] | 284 | void postvisit( FunctionDecl * decl );
|
---|
| 285 | void postvisit( StructDecl * decl );
|
---|
[64adb03] | 286 |
|
---|
[db4d8e3] | 287 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
|
---|
[64adb03] | 288 | void validate( DeclarationWithType * );
|
---|
[549c006] | 289 | void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
[97e3296] | 290 | void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
[64adb03] | 291 |
|
---|
| 292 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 293 | PassVisitor< MutexKeyword > impl;
|
---|
[64adb03] | 294 | acceptAll( translationUnit, impl );
|
---|
| 295 | }
|
---|
[9243cc91] | 296 |
|
---|
| 297 | private:
|
---|
| 298 | StructDecl* monitor_decl = nullptr;
|
---|
[ef42b143] | 299 | StructDecl* guard_decl = nullptr;
|
---|
[549c006] | 300 | StructDecl* dtor_guard_decl = nullptr;
|
---|
[97e3296] | 301 |
|
---|
| 302 | static std::unique_ptr< Type > generic_func;
|
---|
[64adb03] | 303 | };
|
---|
| 304 |
|
---|
[97e3296] | 305 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
---|
| 306 | new FunctionType(
|
---|
| 307 | noQualifiers,
|
---|
| 308 | true
|
---|
| 309 | )
|
---|
| 310 | );
|
---|
| 311 |
|
---|
[bd4d011] | 312 | //-----------------------------------------------------------------------------
|
---|
| 313 | //Handles mutex routines definitions :
|
---|
| 314 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
[ac2b598] | 315 | // $monitor * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[bd4d011] | 316 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
| 317 | // /*Some code*/ => /*Some code*/
|
---|
| 318 | // } }
|
---|
| 319 | //
|
---|
[2065609] | 320 | class ThreadStarter final {
|
---|
[bd4d011] | 321 | public:
|
---|
| 322 |
|
---|
[2065609] | 323 | void postvisit( FunctionDecl * decl );
|
---|
[549c006] | 324 | void previsit ( StructDecl * decl );
|
---|
[bd4d011] | 325 |
|
---|
| 326 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
|
---|
| 327 |
|
---|
| 328 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 329 | PassVisitor< ThreadStarter > impl;
|
---|
[bd4d011] | 330 | acceptAll( translationUnit, impl );
|
---|
| 331 | }
|
---|
[549c006] | 332 |
|
---|
| 333 | private :
|
---|
| 334 | bool thread_ctor_seen = false;
|
---|
| 335 | StructDecl * thread_decl = nullptr;
|
---|
[bd4d011] | 336 | };
|
---|
| 337 |
|
---|
[64adb03] | 338 | //=============================================================================================
|
---|
| 339 | // General entry routine
|
---|
| 340 | //=============================================================================================
|
---|
| 341 | void applyKeywords( std::list< Declaration * > & translationUnit ) {
|
---|
| 342 | ThreadKeyword ::implement( translationUnit );
|
---|
| 343 | CoroutineKeyword ::implement( translationUnit );
|
---|
| 344 | MonitorKeyword ::implement( translationUnit );
|
---|
[427854b] | 345 | GeneratorKeyword ::implement( translationUnit );
|
---|
| 346 | SuspendKeyword ::implement( translationUnit );
|
---|
[bcda04c] | 347 | }
|
---|
| 348 |
|
---|
| 349 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
---|
[64adb03] | 350 | MutexKeyword ::implement( translationUnit );
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[bcda04c] | 353 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
---|
[bd4d011] | 354 | ThreadStarter ::implement( translationUnit );
|
---|
[bcda04c] | 355 | }
|
---|
| 356 |
|
---|
[b32ada31] | 357 | //=============================================================================================
|
---|
[bcda04c] | 358 | // Generic keyword implementation
|
---|
[b32ada31] | 359 | //=============================================================================================
|
---|
[2db79e5] | 360 | void fixupGenerics(FunctionType * func, StructDecl * decl) {
|
---|
| 361 | cloneAll(decl->parameters, func->forall);
|
---|
| 362 | for ( TypeDecl * td : func->forall ) {
|
---|
| 363 | strict_dynamic_cast<StructInstType*>(
|
---|
| 364 | func->parameters.front()->get_type()->stripReferences()
|
---|
| 365 | )->parameters.push_back(
|
---|
| 366 | new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) )
|
---|
| 367 | );
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[9a705dc8] | 371 | Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
|
---|
[9f5ecf5] | 372 | if( decl->name == type_name && decl->body ) {
|
---|
[bcda04c] | 373 | assert( !type_decl );
|
---|
| 374 | type_decl = decl;
|
---|
[b32ada31] | 375 | }
|
---|
[bcda04c] | 376 | else if ( is_target(decl) ) {
|
---|
[b32ada31] | 377 | handle( decl );
|
---|
| 378 | }
|
---|
[1c01c58] | 379 | else if ( !vtable_decl && vtable_name == decl->name && decl->body ) {
|
---|
| 380 | vtable_decl = decl;
|
---|
| 381 | }
|
---|
| 382 | // Might be able to get ride of is target.
|
---|
| 383 | assert( is_target(decl) == (cast_target == decl->kind) );
|
---|
[9a705dc8] | 384 | return decl;
|
---|
[b32ada31] | 385 | }
|
---|
| 386 |
|
---|
[6c3a5ac1] | 387 | DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) {
|
---|
[1c01c58] | 388 | if ( type_decl && isDestructorFor( decl, type_decl ) )
|
---|
| 389 | dtor_decl = decl;
|
---|
| 390 | else if ( vtable_name.empty() )
|
---|
| 391 | ;
|
---|
| 392 | else if ( auto param = isMainFor( decl, cast_target ) ) {
|
---|
| 393 | // This should never trigger.
|
---|
| 394 | assert( vtable_decl );
|
---|
| 395 | // Should be safe because of isMainFor.
|
---|
| 396 | StructInstType * struct_type = static_cast<StructInstType *>(
|
---|
| 397 | static_cast<ReferenceType *>( param->get_type() )->base );
|
---|
| 398 | assert( struct_type );
|
---|
| 399 |
|
---|
| 400 | declsToAddAfter.push_back( Virtual::makeVtableInstance( vtable_decl, {
|
---|
| 401 | new TypeExpr( struct_type->clone() ),
|
---|
| 402 | }, struct_type, nullptr ) );
|
---|
| 403 | }
|
---|
[6c3a5ac1] | 404 |
|
---|
| 405 | return decl;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[9a705dc8] | 408 | Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
|
---|
| 409 | if ( cast_target == cast->target ) {
|
---|
[ac2b598] | 410 | // convert (thread &)t to ($thread &)*get_thread(t), etc.
|
---|
[9a705dc8] | 411 | if( !type_decl ) SemanticError( cast, context_error );
|
---|
[6c3a5ac1] | 412 | if( !dtor_decl ) SemanticError( cast, context_error );
|
---|
[3b0c8cb] | 413 | assert( cast->result == nullptr );
|
---|
| 414 | cast->set_result( new ReferenceType( noQualifiers, new StructInstType( noQualifiers, type_decl ) ) );
|
---|
| 415 | cast->concrete_target.field = field_name;
|
---|
| 416 | cast->concrete_target.getter = getter_name;
|
---|
[9a705dc8] | 417 | }
|
---|
| 418 | return cast;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 |
|
---|
[bcda04c] | 422 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
---|
[9f5ecf5] | 423 | if( ! decl->body ) return;
|
---|
[b32ada31] | 424 |
|
---|
[a16764a6] | 425 | if( !type_decl ) SemanticError( decl, context_error );
|
---|
[6c3a5ac1] | 426 | if( !dtor_decl ) SemanticError( decl, context_error );
|
---|
[b32ada31] | 427 |
|
---|
[1c01c58] | 428 | addVtableForward( decl );
|
---|
[bcda04c] | 429 | FunctionDecl * func = forwardDeclare( decl );
|
---|
| 430 | ObjectDecl * field = addField( decl );
|
---|
[2f9a722] | 431 | addRoutines( field, func );
|
---|
[b32ada31] | 432 | }
|
---|
| 433 |
|
---|
[1c01c58] | 434 | void ConcurrentSueKeyword::addVtableForward( StructDecl * decl ) {
|
---|
| 435 | if ( vtable_decl ) {
|
---|
| 436 | declsToAddBefore.push_back( Virtual::makeVtableForward( vtable_decl, {
|
---|
| 437 | new TypeExpr( new StructInstType( noQualifiers, decl ) ),
|
---|
| 438 | } ) );
|
---|
| 439 | // Its only an error if we want a vtable and don't have one.
|
---|
| 440 | } else if ( ! vtable_name.empty() ) {
|
---|
| 441 | SemanticError( decl, context_error );
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[bcda04c] | 445 | FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
|
---|
| 446 |
|
---|
| 447 | StructDecl * forward = decl->clone();
|
---|
| 448 | forward->set_body( false );
|
---|
| 449 | deleteAll( forward->get_members() );
|
---|
| 450 | forward->get_members().clear();
|
---|
| 451 |
|
---|
[bd4d011] | 452 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
---|
[bcda04c] | 453 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
| 454 | "this",
|
---|
[ba3706f] | 455 | noStorageClasses,
|
---|
[b32ada31] | 456 | LinkageSpec::Cforall,
|
---|
| 457 | nullptr,
|
---|
[83a071f9] | 458 | new ReferenceType(
|
---|
[b32ada31] | 459 | noQualifiers,
|
---|
[bcda04c] | 460 | new StructInstType(
|
---|
| 461 | noQualifiers,
|
---|
| 462 | decl
|
---|
| 463 | )
|
---|
[b32ada31] | 464 | ),
|
---|
| 465 | nullptr
|
---|
| 466 | );
|
---|
| 467 |
|
---|
[2db79e5] | 468 | get_type->get_parameters().push_back( this_decl->clone() );
|
---|
[bd4d011] | 469 | get_type->get_returnVals().push_back(
|
---|
[e04b636] | 470 | new ObjectDecl(
|
---|
| 471 | "ret",
|
---|
[ba3706f] | 472 | noStorageClasses,
|
---|
[e04b636] | 473 | LinkageSpec::Cforall,
|
---|
| 474 | nullptr,
|
---|
| 475 | new PointerType(
|
---|
| 476 | noQualifiers,
|
---|
| 477 | new StructInstType(
|
---|
| 478 | noQualifiers,
|
---|
[bcda04c] | 479 | type_decl
|
---|
[e04b636] | 480 | )
|
---|
| 481 | ),
|
---|
| 482 | nullptr
|
---|
| 483 | )
|
---|
| 484 | );
|
---|
[2db79e5] | 485 | fixupGenerics(get_type, decl);
|
---|
[b32ada31] | 486 |
|
---|
[bcda04c] | 487 | FunctionDecl * get_decl = new FunctionDecl(
|
---|
| 488 | getter_name,
|
---|
| 489 | Type::Static,
|
---|
| 490 | LinkageSpec::Cforall,
|
---|
[bd4d011] | 491 | get_type,
|
---|
[bcda04c] | 492 | nullptr,
|
---|
[a8078eef] | 493 | { new Attribute("const") },
|
---|
[bcda04c] | 494 | Type::Inline
|
---|
| 495 | );
|
---|
| 496 |
|
---|
[bd4d011] | 497 | FunctionDecl * main_decl = nullptr;
|
---|
| 498 |
|
---|
| 499 | if( needs_main ) {
|
---|
| 500 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
---|
[bf2438c] | 501 |
|
---|
[bd4d011] | 502 | main_type->get_parameters().push_back( this_decl->clone() );
|
---|
| 503 |
|
---|
| 504 | main_decl = new FunctionDecl(
|
---|
| 505 | "main",
|
---|
[ba3706f] | 506 | noStorageClasses,
|
---|
[bd4d011] | 507 | LinkageSpec::Cforall,
|
---|
| 508 | main_type,
|
---|
| 509 | nullptr
|
---|
| 510 | );
|
---|
[2db79e5] | 511 | fixupGenerics(main_type, decl);
|
---|
[bd4d011] | 512 | }
|
---|
| 513 |
|
---|
[2db79e5] | 514 | delete this_decl;
|
---|
| 515 |
|
---|
[2065609] | 516 | declsToAddBefore.push_back( forward );
|
---|
| 517 | if( needs_main ) declsToAddBefore.push_back( main_decl );
|
---|
| 518 | declsToAddBefore.push_back( get_decl );
|
---|
[bcda04c] | 519 |
|
---|
| 520 | return get_decl;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
|
---|
| 524 | ObjectDecl * field = new ObjectDecl(
|
---|
| 525 | field_name,
|
---|
[ba3706f] | 526 | noStorageClasses,
|
---|
[bcda04c] | 527 | LinkageSpec::Cforall,
|
---|
| 528 | nullptr,
|
---|
| 529 | new StructInstType(
|
---|
| 530 | noQualifiers,
|
---|
| 531 | type_decl
|
---|
| 532 | ),
|
---|
| 533 | nullptr
|
---|
| 534 | );
|
---|
| 535 |
|
---|
| 536 | decl->get_members().push_back( field );
|
---|
| 537 |
|
---|
| 538 | return field;
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[2f9a722] | 541 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
---|
[ba3706f] | 542 | CompoundStmt * statement = new CompoundStmt();
|
---|
[bf2438c] | 543 | statement->push_back(
|
---|
[b32ada31] | 544 | new ReturnStmt(
|
---|
[e04b636] | 545 | new AddressExpr(
|
---|
[bcda04c] | 546 | new MemberExpr(
|
---|
| 547 | field,
|
---|
[2db79e5] | 548 | new CastExpr(
|
---|
| 549 | new VariableExpr( func->get_functionType()->get_parameters().front() ),
|
---|
[b81fd95] | 550 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone(),
|
---|
| 551 | false
|
---|
[2db79e5] | 552 | )
|
---|
[e04b636] | 553 | )
|
---|
[b32ada31] | 554 | )
|
---|
| 555 | )
|
---|
| 556 | );
|
---|
| 557 |
|
---|
[bcda04c] | 558 | FunctionDecl * get_decl = func->clone();
|
---|
| 559 |
|
---|
| 560 | get_decl->set_statements( statement );
|
---|
[e04b636] | 561 |
|
---|
| 562 | declsToAddAfter.push_back( get_decl );
|
---|
[427854b] | 563 | }
|
---|
| 564 |
|
---|
| 565 | //=============================================================================================
|
---|
| 566 | // Suspend keyword implementation
|
---|
| 567 | //=============================================================================================
|
---|
| 568 | bool SuspendKeyword::is_real_suspend( FunctionDecl * func ) {
|
---|
| 569 | if(isMangled(func->linkage)) return false; // the real suspend isn't mangled
|
---|
| 570 | if(func->name != "__cfactx_suspend") return false; // the real suspend has a specific name
|
---|
| 571 | if(func->type->parameters.size() != 0) return false; // Too many parameters
|
---|
| 572 | if(func->type->returnVals.size() != 0) return false; // Too many return values
|
---|
| 573 |
|
---|
| 574 | return true;
|
---|
[b32ada31] | 575 | }
|
---|
| 576 |
|
---|
[427854b] | 577 | void SuspendKeyword::premutate( FunctionDecl * func ) {
|
---|
| 578 | GuardValue(in_generator);
|
---|
| 579 | in_generator = nullptr;
|
---|
| 580 |
|
---|
| 581 | // Is this the real suspend?
|
---|
| 582 | if(is_real_suspend(func)) {
|
---|
| 583 | decl_suspend = decl_suspend ? decl_suspend : func;
|
---|
| 584 | return;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | // Is this the main of a generator?
|
---|
[1c01c58] | 588 | auto param = isMainFor( func, AggregateDecl::Aggregate::Generator );
|
---|
[427854b] | 589 | if(!param) return;
|
---|
| 590 |
|
---|
| 591 | if(func->type->returnVals.size() != 0) SemanticError(func->location, "Generator main must return void");
|
---|
| 592 |
|
---|
| 593 | in_generator = param;
|
---|
| 594 | GuardValue(labels);
|
---|
| 595 | labels.clear();
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | DeclarationWithType * SuspendKeyword::postmutate( FunctionDecl * func ) {
|
---|
| 599 | if( !func->statements ) return func; // Not the actual definition, don't do anything
|
---|
| 600 | if( !in_generator ) return func; // Not in a generator, don't do anything
|
---|
| 601 | if( labels.empty() ) return func; // Generator has no states, nothing to do, could throw a warning
|
---|
| 602 |
|
---|
| 603 | // This is a generator main, we need to add the following code to the top
|
---|
| 604 | // static void * __generator_labels[] = {&&s0, &&s1, ...};
|
---|
| 605 | // goto * __generator_labels[gen.__generator_state];
|
---|
| 606 | const auto & loc = func->location;
|
---|
| 607 |
|
---|
| 608 | const auto first_label = gen.newLabel("generator");
|
---|
| 609 |
|
---|
| 610 | // for each label add to declaration
|
---|
| 611 | std::list<Initializer*> inits = { new SingleInit( new LabelAddressExpr( first_label ) ) };
|
---|
| 612 | for(const auto & label : labels) {
|
---|
| 613 | inits.push_back(
|
---|
| 614 | new SingleInit(
|
---|
| 615 | new LabelAddressExpr( label )
|
---|
| 616 | )
|
---|
| 617 | );
|
---|
| 618 | }
|
---|
| 619 | auto init = new ListInit(std::move(inits), noDesignators, true);
|
---|
| 620 | labels.clear();
|
---|
| 621 |
|
---|
| 622 | // create decl
|
---|
| 623 | auto decl = new ObjectDecl(
|
---|
| 624 | "__generator_labels",
|
---|
| 625 | Type::StorageClasses( Type::Static ),
|
---|
| 626 | LinkageSpec::AutoGen,
|
---|
| 627 | nullptr,
|
---|
| 628 | new ArrayType(
|
---|
| 629 | Type::Qualifiers(),
|
---|
| 630 | new PointerType(
|
---|
| 631 | Type::Qualifiers(),
|
---|
| 632 | new VoidType( Type::Qualifiers() )
|
---|
| 633 | ),
|
---|
| 634 | nullptr,
|
---|
| 635 | false, false
|
---|
| 636 | ),
|
---|
| 637 | init
|
---|
| 638 | );
|
---|
| 639 |
|
---|
| 640 | // create the goto
|
---|
| 641 | assert(in_generator);
|
---|
| 642 |
|
---|
| 643 | auto go_decl = new ObjectDecl(
|
---|
| 644 | "__generator_label",
|
---|
| 645 | noStorageClasses,
|
---|
| 646 | LinkageSpec::AutoGen,
|
---|
| 647 | nullptr,
|
---|
| 648 | new PointerType(
|
---|
| 649 | Type::Qualifiers(),
|
---|
| 650 | new VoidType( Type::Qualifiers() )
|
---|
| 651 | ),
|
---|
| 652 | new SingleInit(
|
---|
| 653 | new UntypedExpr(
|
---|
| 654 | new NameExpr("?[?]"),
|
---|
| 655 | {
|
---|
| 656 | new NameExpr("__generator_labels"),
|
---|
| 657 | new UntypedMemberExpr(
|
---|
| 658 | new NameExpr("__generator_state"),
|
---|
| 659 | new VariableExpr( in_generator )
|
---|
| 660 | )
|
---|
| 661 | }
|
---|
| 662 | )
|
---|
| 663 | )
|
---|
| 664 | );
|
---|
| 665 | go_decl->location = loc;
|
---|
| 666 |
|
---|
| 667 | auto go = new BranchStmt(
|
---|
| 668 | new VariableExpr( go_decl ),
|
---|
| 669 | BranchStmt::Goto
|
---|
| 670 | );
|
---|
| 671 | go->location = loc;
|
---|
| 672 | go->computedTarget->location = loc;
|
---|
| 673 |
|
---|
| 674 | auto noop = new NullStmt({ first_label });
|
---|
| 675 | noop->location = loc;
|
---|
| 676 |
|
---|
| 677 | // wrap everything in a nice compound
|
---|
| 678 | auto body = new CompoundStmt({
|
---|
| 679 | new DeclStmt( decl ),
|
---|
| 680 | new DeclStmt( go_decl ),
|
---|
| 681 | go,
|
---|
| 682 | noop,
|
---|
| 683 | func->statements
|
---|
| 684 | });
|
---|
| 685 | body->location = loc;
|
---|
| 686 | func->statements = body;
|
---|
| 687 |
|
---|
| 688 | return func;
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | Statement * SuspendKeyword::postmutate( SuspendStmt * stmt ) {
|
---|
| 692 | SuspendStmt::Type type = stmt->type;
|
---|
| 693 | if(type == SuspendStmt::None) {
|
---|
| 694 | // This suspend has a implicit target, find it
|
---|
| 695 | type = in_generator ? SuspendStmt::Generator : SuspendStmt::Coroutine;
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | // Check that the target makes sense
|
---|
| 699 | if(!in_generator && type == SuspendStmt::Generator) SemanticError( stmt->location, "'suspend generator' must be used inside main of generator type.");
|
---|
| 700 |
|
---|
| 701 | // Act appropriately
|
---|
| 702 | switch(type) {
|
---|
| 703 | case SuspendStmt::Generator: return make_generator_suspend(stmt);
|
---|
| 704 | case SuspendStmt::Coroutine: return make_coroutine_suspend(stmt);
|
---|
| 705 | default: abort();
|
---|
| 706 | }
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | Statement * SuspendKeyword::make_generator_suspend( SuspendStmt * stmt ) {
|
---|
| 710 | assert(in_generator);
|
---|
| 711 | // Target code is :
|
---|
| 712 | // gen.__generator_state = X;
|
---|
| 713 | // { THEN }
|
---|
| 714 | // return;
|
---|
| 715 | // __gen_X:;
|
---|
| 716 |
|
---|
| 717 | // Save the location and delete the old statement, we only need the location from this point on
|
---|
| 718 | auto loc = stmt->location;
|
---|
| 719 |
|
---|
| 720 | // Build the label and get its index
|
---|
| 721 | auto label = make_label();
|
---|
| 722 |
|
---|
| 723 | // Create the context saving statement
|
---|
| 724 | auto save = new ExprStmt( new UntypedExpr(
|
---|
| 725 | new NameExpr( "?=?" ),
|
---|
| 726 | {
|
---|
| 727 | new UntypedMemberExpr(
|
---|
| 728 | new NameExpr("__generator_state"),
|
---|
| 729 | new VariableExpr( in_generator )
|
---|
| 730 | ),
|
---|
| 731 | new ConstantExpr(
|
---|
| 732 | Constant::from_int( label.idx )
|
---|
| 733 | )
|
---|
| 734 | }
|
---|
| 735 | ));
|
---|
| 736 | assert(save->expr);
|
---|
| 737 | save->location = loc;
|
---|
| 738 | stmtsToAddBefore.push_back( save );
|
---|
| 739 |
|
---|
| 740 | // if we have a then add it here
|
---|
| 741 | auto then = stmt->then;
|
---|
| 742 | stmt->then = nullptr;
|
---|
| 743 | delete stmt;
|
---|
| 744 | if(then) stmtsToAddBefore.push_back( then );
|
---|
| 745 |
|
---|
| 746 | // Create the return statement
|
---|
| 747 | auto ret = new ReturnStmt( nullptr );
|
---|
| 748 | ret->location = loc;
|
---|
| 749 | stmtsToAddBefore.push_back( ret );
|
---|
| 750 |
|
---|
| 751 | // Create the null statement with the created label
|
---|
| 752 | auto noop = new NullStmt({ label.obj });
|
---|
| 753 | noop->location = loc;
|
---|
| 754 |
|
---|
| 755 | // Return the null statement to take the place of the previous statement
|
---|
| 756 | return noop;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | Statement * SuspendKeyword::make_coroutine_suspend( SuspendStmt * stmt ) {
|
---|
| 760 | if(stmt->then) SemanticError( stmt->location, "Compound statement following coroutines is not implemented.");
|
---|
| 761 |
|
---|
| 762 | // Save the location and delete the old statement, we only need the location from this point on
|
---|
| 763 | auto loc = stmt->location;
|
---|
| 764 | delete stmt;
|
---|
| 765 |
|
---|
| 766 | // Create the call expression
|
---|
| 767 | if(!decl_suspend) SemanticError( loc, "suspend keyword applied to coroutines requires coroutines to be in scope, add #include <coroutine.hfa>\n");
|
---|
| 768 | auto expr = new UntypedExpr( VariableExpr::functionPointer( decl_suspend ) );
|
---|
[e6cfa8ff] | 769 | expr->location = loc;
|
---|
[427854b] | 770 |
|
---|
| 771 | // Change this statement into a regular expr
|
---|
| 772 | assert(expr);
|
---|
| 773 | auto nstmt = new ExprStmt( expr );
|
---|
[e6cfa8ff] | 774 | nstmt->location = loc;
|
---|
[427854b] | 775 | return nstmt;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 |
|
---|
[64adb03] | 779 | //=============================================================================================
|
---|
| 780 | // Mutex keyword implementation
|
---|
| 781 | //=============================================================================================
|
---|
[97e3296] | 782 |
|
---|
[2065609] | 783 | void MutexKeyword::postvisit(FunctionDecl* decl) {
|
---|
[102a58b] | 784 |
|
---|
[db4d8e3] | 785 | bool first = false;
|
---|
| 786 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
|
---|
[ceedde6] | 787 | bool isDtor = CodeGen::isDestructor( decl->name );
|
---|
[64adb03] | 788 |
|
---|
[ceedde6] | 789 | // Is this function relevant to monitors
|
---|
| 790 | if( mutexArgs.empty() ) {
|
---|
| 791 | // If this is the destructor for a monitor it must be mutex
|
---|
| 792 | if(isDtor) {
|
---|
| 793 | Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
|
---|
| 794 |
|
---|
| 795 | // If it's a copy, it's not a mutex
|
---|
| 796 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
| 797 | if( ! rty ) return;
|
---|
| 798 |
|
---|
| 799 | // If we are not pointing directly to a type, it's not a mutex
|
---|
| 800 | Type* base = rty->get_base();
|
---|
| 801 | if( dynamic_cast< ReferenceType * >( base ) ) return;
|
---|
| 802 | if( dynamic_cast< PointerType * >( base ) ) return;
|
---|
| 803 |
|
---|
| 804 | // Check if its a struct
|
---|
| 805 | StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
|
---|
| 806 | if( !baseStruct ) return;
|
---|
| 807 |
|
---|
| 808 | // Check if its a monitor
|
---|
| 809 | if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
|
---|
| 810 | SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
|
---|
| 811 | }
|
---|
| 812 | return;
|
---|
| 813 | }
|
---|
[549c006] | 814 |
|
---|
[ceedde6] | 815 | // Monitors can't be constructed with mutual exclusion
|
---|
| 816 | if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
|
---|
[549c006] | 817 |
|
---|
[ceedde6] | 818 | // It makes no sense to have multiple mutex parameters for the destructor
|
---|
[a16764a6] | 819 | if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
|
---|
[549c006] | 820 |
|
---|
[ceedde6] | 821 | // Make sure all the mutex arguments are monitors
|
---|
[64adb03] | 822 | for(auto arg : mutexArgs) {
|
---|
| 823 | validate( arg );
|
---|
| 824 | }
|
---|
| 825 |
|
---|
[ceedde6] | 826 | // Check if we need to instrument the body
|
---|
[64adb03] | 827 | CompoundStmt* body = decl->get_statements();
|
---|
| 828 | if( ! body ) return;
|
---|
| 829 |
|
---|
[ceedde6] | 830 | // Do we have the required headers
|
---|
[a16764a6] | 831 | if( !monitor_decl || !guard_decl || !dtor_guard_decl )
|
---|
[73abe95] | 832 | SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>\n" );
|
---|
[b32ada31] | 833 |
|
---|
[ceedde6] | 834 | // Instrument the body
|
---|
[549c006] | 835 | if( isDtor ) {
|
---|
| 836 | addDtorStatments( decl, body, mutexArgs );
|
---|
| 837 | }
|
---|
| 838 | else {
|
---|
| 839 | addStatments( decl, body, mutexArgs );
|
---|
| 840 | }
|
---|
[64adb03] | 841 | }
|
---|
| 842 |
|
---|
[2065609] | 843 | void MutexKeyword::postvisit(StructDecl* decl) {
|
---|
[102a58b] | 844 |
|
---|
[ac2b598] | 845 | if( decl->name == "$monitor" && decl->body ) {
|
---|
[9243cc91] | 846 | assert( !monitor_decl );
|
---|
| 847 | monitor_decl = decl;
|
---|
| 848 | }
|
---|
[88d955f] | 849 | else if( decl->name == "monitor_guard_t" && decl->body ) {
|
---|
[ef42b143] | 850 | assert( !guard_decl );
|
---|
| 851 | guard_decl = decl;
|
---|
| 852 | }
|
---|
[88d955f] | 853 | else if( decl->name == "monitor_dtor_guard_t" && decl->body ) {
|
---|
[549c006] | 854 | assert( !dtor_guard_decl );
|
---|
| 855 | dtor_guard_decl = decl;
|
---|
| 856 | }
|
---|
[9243cc91] | 857 | }
|
---|
| 858 |
|
---|
[db4d8e3] | 859 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
|
---|
[64adb03] | 860 | std::list<DeclarationWithType*> mutexArgs;
|
---|
| 861 |
|
---|
[db4d8e3] | 862 | bool once = true;
|
---|
[64adb03] | 863 | for( auto arg : decl->get_functionType()->get_parameters()) {
|
---|
| 864 | //Find mutex arguments
|
---|
| 865 | Type* ty = arg->get_type();
|
---|
[615a096] | 866 | if( ! ty->get_mutex() ) continue;
|
---|
[64adb03] | 867 |
|
---|
[db4d8e3] | 868 | if(once) {first = true;}
|
---|
| 869 | once = false;
|
---|
| 870 |
|
---|
[64adb03] | 871 | //Append it to the list
|
---|
| 872 | mutexArgs.push_back( arg );
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | return mutexArgs;
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | void MutexKeyword::validate( DeclarationWithType * arg ) {
|
---|
| 879 | Type* ty = arg->get_type();
|
---|
| 880 |
|
---|
| 881 | //Makes sure it's not a copy
|
---|
[870d1f0] | 882 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
[a16764a6] | 883 | if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " );
|
---|
[64adb03] | 884 |
|
---|
| 885 | //Make sure the we are pointing directly to a type
|
---|
[83a071f9] | 886 | Type* base = rty->get_base();
|
---|
[a16764a6] | 887 | if( dynamic_cast< ReferenceType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
| 888 | if( dynamic_cast< PointerType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
[64adb03] | 889 |
|
---|
| 890 | //Make sure that typed isn't mutex
|
---|
[a16764a6] | 891 | if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " );
|
---|
[64adb03] | 892 | }
|
---|
| 893 |
|
---|
[549c006] | 894 | void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
| 895 | Type * arg_type = args.front()->get_type()->clone();
|
---|
| 896 | arg_type->set_mutex( false );
|
---|
| 897 |
|
---|
| 898 | ObjectDecl * monitors = new ObjectDecl(
|
---|
| 899 | "__monitor",
|
---|
[ba3706f] | 900 | noStorageClasses,
|
---|
[549c006] | 901 | LinkageSpec::Cforall,
|
---|
| 902 | nullptr,
|
---|
| 903 | new PointerType(
|
---|
| 904 | noQualifiers,
|
---|
| 905 | new StructInstType(
|
---|
| 906 | noQualifiers,
|
---|
| 907 | monitor_decl
|
---|
| 908 | )
|
---|
| 909 | ),
|
---|
| 910 | new SingleInit( new UntypedExpr(
|
---|
| 911 | new NameExpr( "get_monitor" ),
|
---|
[b81fd95] | 912 | { new CastExpr( new VariableExpr( args.front() ), arg_type, false ) }
|
---|
[549c006] | 913 | ))
|
---|
| 914 | );
|
---|
| 915 |
|
---|
| 916 | assert(generic_func);
|
---|
| 917 |
|
---|
| 918 | //in reverse order :
|
---|
[2bf7ef6] | 919 | // monitor_dtor_guard_t __guard = { __monitors, func };
|
---|
[549c006] | 920 | body->push_front(
|
---|
[ba3706f] | 921 | new DeclStmt( new ObjectDecl(
|
---|
[549c006] | 922 | "__guard",
|
---|
[ba3706f] | 923 | noStorageClasses,
|
---|
[549c006] | 924 | LinkageSpec::Cforall,
|
---|
| 925 | nullptr,
|
---|
| 926 | new StructInstType(
|
---|
| 927 | noQualifiers,
|
---|
| 928 | dtor_guard_decl
|
---|
| 929 | ),
|
---|
| 930 | new ListInit(
|
---|
| 931 | {
|
---|
| 932 | new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ),
|
---|
[77a2994] | 933 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) ),
|
---|
| 934 | new SingleInit( new ConstantExpr( Constant::from_bool( false ) ) )
|
---|
[549c006] | 935 | },
|
---|
| 936 | noDesignators,
|
---|
| 937 | true
|
---|
| 938 | )
|
---|
| 939 | ))
|
---|
| 940 | );
|
---|
| 941 |
|
---|
[ac2b598] | 942 | //$monitor * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[ba3706f] | 943 | body->push_front( new DeclStmt( monitors) );
|
---|
[549c006] | 944 | }
|
---|
| 945 |
|
---|
[97e3296] | 946 | void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
[9243cc91] | 947 | ObjectDecl * monitors = new ObjectDecl(
|
---|
| 948 | "__monitors",
|
---|
[ba3706f] | 949 | noStorageClasses,
|
---|
[9243cc91] | 950 | LinkageSpec::Cforall,
|
---|
| 951 | nullptr,
|
---|
| 952 | new ArrayType(
|
---|
| 953 | noQualifiers,
|
---|
| 954 | new PointerType(
|
---|
| 955 | noQualifiers,
|
---|
| 956 | new StructInstType(
|
---|
| 957 | noQualifiers,
|
---|
| 958 | monitor_decl
|
---|
| 959 | )
|
---|
| 960 | ),
|
---|
| 961 | new ConstantExpr( Constant::from_ulong( args.size() ) ),
|
---|
| 962 | false,
|
---|
| 963 | false
|
---|
| 964 | ),
|
---|
| 965 | new ListInit(
|
---|
[bd41764] | 966 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
|
---|
[cb0e6de] | 967 | Type * type = var->get_type()->clone();
|
---|
| 968 | type->set_mutex( false );
|
---|
[9243cc91] | 969 | return new SingleInit( new UntypedExpr(
|
---|
| 970 | new NameExpr( "get_monitor" ),
|
---|
[b81fd95] | 971 | { new CastExpr( new VariableExpr( var ), type, false ) }
|
---|
[9243cc91] | 972 | ) );
|
---|
| 973 | })
|
---|
| 974 | )
|
---|
| 975 | );
|
---|
| 976 |
|
---|
[97e3296] | 977 | assert(generic_func);
|
---|
| 978 |
|
---|
[2bf7ef6] | 979 | // in reverse order :
|
---|
[97e3296] | 980 | // monitor_guard_t __guard = { __monitors, #, func };
|
---|
[64adb03] | 981 | body->push_front(
|
---|
[ba3706f] | 982 | new DeclStmt( new ObjectDecl(
|
---|
[64adb03] | 983 | "__guard",
|
---|
[ba3706f] | 984 | noStorageClasses,
|
---|
[64adb03] | 985 | LinkageSpec::Cforall,
|
---|
| 986 | nullptr,
|
---|
| 987 | new StructInstType(
|
---|
| 988 | noQualifiers,
|
---|
[ef42b143] | 989 | guard_decl
|
---|
[64adb03] | 990 | ),
|
---|
| 991 | new ListInit(
|
---|
| 992 | {
|
---|
[9243cc91] | 993 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
[97e3296] | 994 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
---|
[b81fd95] | 995 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone(), false ) )
|
---|
[ef42b143] | 996 | },
|
---|
| 997 | noDesignators,
|
---|
| 998 | true
|
---|
[64adb03] | 999 | )
|
---|
| 1000 | ))
|
---|
| 1001 | );
|
---|
| 1002 |
|
---|
[ac2b598] | 1003 | //$monitor * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[ba3706f] | 1004 | body->push_front( new DeclStmt( monitors) );
|
---|
[64adb03] | 1005 | }
|
---|
[bd4d011] | 1006 |
|
---|
| 1007 | //=============================================================================================
|
---|
| 1008 | // General entry routine
|
---|
| 1009 | //=============================================================================================
|
---|
[549c006] | 1010 | void ThreadStarter::previsit( StructDecl * decl ) {
|
---|
[ac2b598] | 1011 | if( decl->name == "$thread" && decl->body ) {
|
---|
[549c006] | 1012 | assert( !thread_decl );
|
---|
| 1013 | thread_decl = decl;
|
---|
| 1014 | }
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
[2065609] | 1017 | void ThreadStarter::postvisit(FunctionDecl * decl) {
|
---|
[9f5ecf5] | 1018 | if( ! CodeGen::isConstructor(decl->name) ) return;
|
---|
[bd4d011] | 1019 |
|
---|
[549c006] | 1020 | Type * typeof_this = InitTweak::getTypeofThis(decl->type);
|
---|
| 1021 | StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this );
|
---|
| 1022 | if( ctored_type && ctored_type->baseStruct == thread_decl ) {
|
---|
| 1023 | thread_ctor_seen = true;
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
[bd4d011] | 1026 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
---|
[ce8c12f] | 1027 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
---|
[bd4d011] | 1028 | if( type && type->get_baseStruct()->is_thread() ) {
|
---|
[549c006] | 1029 | if( !thread_decl || !thread_ctor_seen ) {
|
---|
[73abe95] | 1030 | SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>");
|
---|
[549c006] | 1031 | }
|
---|
| 1032 |
|
---|
[bd4d011] | 1033 | addStartStatement( decl, param );
|
---|
| 1034 | }
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
| 1037 | void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
|
---|
| 1038 | CompoundStmt * stmt = decl->get_statements();
|
---|
| 1039 |
|
---|
| 1040 | if( ! stmt ) return;
|
---|
| 1041 |
|
---|
[bf2438c] | 1042 | stmt->push_back(
|
---|
[bd4d011] | 1043 | new ExprStmt(
|
---|
| 1044 | new UntypedExpr(
|
---|
| 1045 | new NameExpr( "__thrd_start" ),
|
---|
[09f357ec] | 1046 | { new VariableExpr( param ), new NameExpr("main") }
|
---|
[bd4d011] | 1047 | )
|
---|
| 1048 | )
|
---|
| 1049 | );
|
---|
| 1050 | }
|
---|
[68fe077a] | 1051 | };
|
---|
[6b0b624] | 1052 |
|
---|
| 1053 | // Local Variables: //
|
---|
| 1054 | // mode: c //
|
---|
| 1055 | // tab-width: 4 //
|
---|
| 1056 | // End: //
|
---|
[1c01c58] | 1057 |
|
---|