[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 :
|
---|
[6b0b624] | 13 | // Update Count : 5
|
---|
[64adb03] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "Concurrency/Keywords.h"
|
---|
| 17 |
|
---|
[bf2438c] | 18 | #include <cassert> // for assert
|
---|
| 19 | #include <string> // for string, operator==
|
---|
| 20 |
|
---|
[2065609] | 21 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
[bf2438c] | 22 | #include "Common/SemanticError.h" // for SemanticError
|
---|
| 23 | #include "Common/utility.h" // for deleteAll, map_range
|
---|
[bff227f] | 24 | #include "CodeGen/OperatorTable.h" // for isConstructor
|
---|
| 25 | #include "InitTweak/InitTweak.h" // for getPointerBase
|
---|
[bf2438c] | 26 | #include "Parser/LinkageSpec.h" // for Cforall
|
---|
| 27 | #include "SynTree/Constant.h" // for Constant
|
---|
| 28 | #include "SynTree/Declaration.h" // for StructDecl, FunctionDecl, ObjectDecl
|
---|
| 29 | #include "SynTree/Expression.h" // for VariableExpr, ConstantExpr, Untype...
|
---|
| 30 | #include "SynTree/Initializer.h" // for SingleInit, ListInit, Initializer ...
|
---|
| 31 | #include "SynTree/Label.h" // for Label
|
---|
| 32 | #include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt
|
---|
| 33 | #include "SynTree/Type.h" // for StructInstType, Type, PointerType
|
---|
| 34 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
| 35 |
|
---|
| 36 | class Attribute;
|
---|
[64adb03] | 37 |
|
---|
| 38 | namespace Concurrency {
|
---|
| 39 | //=============================================================================================
|
---|
[2065609] | 40 | // Pass declarations
|
---|
[64adb03] | 41 | //=============================================================================================
|
---|
| 42 |
|
---|
[bcda04c] | 43 | //-----------------------------------------------------------------------------
|
---|
| 44 | //Handles sue type declarations :
|
---|
| 45 | // sue MyType { struct MyType {
|
---|
| 46 | // int data; int data;
|
---|
| 47 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 48 | // => NewField_t newField;
|
---|
| 49 | // }; };
|
---|
| 50 | // static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
|
---|
| 51 | //
|
---|
[2065609] | 52 | class ConcurrentSueKeyword : public WithDeclsToAdd {
|
---|
[bcda04c] | 53 | public:
|
---|
| 54 |
|
---|
[9a705dc8] | 55 | ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main, KeywordCastExpr::Target cast_target ) :
|
---|
| 56 | type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ), cast_target( cast_target ) {}
|
---|
[bcda04c] | 57 |
|
---|
| 58 | virtual ~ConcurrentSueKeyword() {}
|
---|
| 59 |
|
---|
[9a705dc8] | 60 | Declaration * postmutate( StructDecl * decl );
|
---|
[6c3a5ac1] | 61 | DeclarationWithType * postmutate( FunctionDecl * decl );
|
---|
[bcda04c] | 62 |
|
---|
| 63 | void handle( StructDecl * );
|
---|
| 64 | FunctionDecl * forwardDeclare( StructDecl * );
|
---|
| 65 | ObjectDecl * addField( StructDecl * );
|
---|
[2f9a722] | 66 | void addRoutines( ObjectDecl *, FunctionDecl * );
|
---|
[bcda04c] | 67 |
|
---|
| 68 | virtual bool is_target( StructDecl * decl ) = 0;
|
---|
| 69 |
|
---|
[9a705dc8] | 70 | Expression * postmutate( KeywordCastExpr * cast );
|
---|
| 71 |
|
---|
[bcda04c] | 72 | private:
|
---|
| 73 | const std::string type_name;
|
---|
| 74 | const std::string field_name;
|
---|
| 75 | const std::string getter_name;
|
---|
| 76 | const std::string context_error;
|
---|
[bd4d011] | 77 | bool needs_main;
|
---|
[9a705dc8] | 78 | KeywordCastExpr::Target cast_target;
|
---|
[bcda04c] | 79 |
|
---|
[6c3a5ac1] | 80 | StructDecl * type_decl = nullptr;
|
---|
| 81 | FunctionDecl * dtor_decl = nullptr;
|
---|
[bcda04c] | 82 | };
|
---|
| 83 |
|
---|
| 84 |
|
---|
[64adb03] | 85 | //-----------------------------------------------------------------------------
|
---|
| 86 | //Handles thread type declarations :
|
---|
| 87 | // thread Mythread { struct MyThread {
|
---|
| 88 | // int data; int data;
|
---|
| 89 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 90 | // => thread_desc __thrd_d;
|
---|
| 91 | // }; };
|
---|
| 92 | // static inline thread_desc * get_thread( MyThread * this ) { return &this->__thrd_d; }
|
---|
| 93 | //
|
---|
[bcda04c] | 94 | class ThreadKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 95 | public:
|
---|
| 96 |
|
---|
[bcda04c] | 97 | ThreadKeyword() : ConcurrentSueKeyword(
|
---|
| 98 | "thread_desc",
|
---|
| 99 | "__thrd",
|
---|
| 100 | "get_thread",
|
---|
[6c3a5ac1] | 101 | "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
|
---|
[9a705dc8] | 102 | true,
|
---|
| 103 | KeywordCastExpr::Thread
|
---|
[bcda04c] | 104 | )
|
---|
| 105 | {}
|
---|
| 106 |
|
---|
| 107 | virtual ~ThreadKeyword() {}
|
---|
| 108 |
|
---|
| 109 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
|
---|
| 110 |
|
---|
| 111 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 112 | PassVisitor< ThreadKeyword > impl;
|
---|
[9a705dc8] | 113 | mutateAll( translationUnit, impl );
|
---|
[bcda04c] | 114 | }
|
---|
[64adb03] | 115 | };
|
---|
| 116 |
|
---|
| 117 | //-----------------------------------------------------------------------------
|
---|
| 118 | //Handles coroutine type declarations :
|
---|
| 119 | // coroutine MyCoroutine { struct MyCoroutine {
|
---|
| 120 | // int data; int data;
|
---|
| 121 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 122 | // => coroutine_desc __cor_d;
|
---|
| 123 | // }; };
|
---|
| 124 | // static inline coroutine_desc * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
|
---|
| 125 | //
|
---|
[bcda04c] | 126 | class CoroutineKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 127 | public:
|
---|
| 128 |
|
---|
[bcda04c] | 129 | CoroutineKeyword() : ConcurrentSueKeyword(
|
---|
| 130 | "coroutine_desc",
|
---|
| 131 | "__cor",
|
---|
| 132 | "get_coroutine",
|
---|
[6c3a5ac1] | 133 | "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n",
|
---|
[9a705dc8] | 134 | true,
|
---|
| 135 | KeywordCastExpr::Coroutine
|
---|
[bcda04c] | 136 | )
|
---|
| 137 | {}
|
---|
[b32ada31] | 138 |
|
---|
[bcda04c] | 139 | virtual ~CoroutineKeyword() {}
|
---|
| 140 |
|
---|
| 141 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
|
---|
[b32ada31] | 142 |
|
---|
| 143 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 144 | PassVisitor< CoroutineKeyword > impl;
|
---|
[9a705dc8] | 145 | mutateAll( translationUnit, impl );
|
---|
[b32ada31] | 146 | }
|
---|
[64adb03] | 147 | };
|
---|
| 148 |
|
---|
| 149 | //-----------------------------------------------------------------------------
|
---|
| 150 | //Handles monitor type declarations :
|
---|
| 151 | // monitor MyMonitor { struct MyMonitor {
|
---|
| 152 | // int data; int data;
|
---|
| 153 | // a_struct_t more_data; a_struct_t more_data;
|
---|
| 154 | // => monitor_desc __mon_d;
|
---|
| 155 | // }; };
|
---|
| 156 | // static inline monitor_desc * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
|
---|
| 157 | //
|
---|
[bcda04c] | 158 | class MonitorKeyword final : public ConcurrentSueKeyword {
|
---|
[64adb03] | 159 | public:
|
---|
| 160 |
|
---|
[bcda04c] | 161 | MonitorKeyword() : ConcurrentSueKeyword(
|
---|
| 162 | "monitor_desc",
|
---|
| 163 | "__mon",
|
---|
| 164 | "get_monitor",
|
---|
[6c3a5ac1] | 165 | "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n",
|
---|
[9a705dc8] | 166 | false,
|
---|
| 167 | KeywordCastExpr::Monitor
|
---|
[bcda04c] | 168 | )
|
---|
| 169 | {}
|
---|
| 170 |
|
---|
| 171 | virtual ~MonitorKeyword() {}
|
---|
| 172 |
|
---|
| 173 | virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
|
---|
| 174 |
|
---|
| 175 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 176 | PassVisitor< MonitorKeyword > impl;
|
---|
[9a705dc8] | 177 | mutateAll( translationUnit, impl );
|
---|
[bcda04c] | 178 | }
|
---|
[64adb03] | 179 | };
|
---|
| 180 |
|
---|
| 181 | //-----------------------------------------------------------------------------
|
---|
| 182 | //Handles mutex routines definitions :
|
---|
| 183 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
[9243cc91] | 184 | // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[64adb03] | 185 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
| 186 | // /*Some code*/ => /*Some code*/
|
---|
| 187 | // } }
|
---|
| 188 | //
|
---|
[2065609] | 189 | class MutexKeyword final {
|
---|
[64adb03] | 190 | public:
|
---|
| 191 |
|
---|
[2065609] | 192 | void postvisit( FunctionDecl * decl );
|
---|
| 193 | void postvisit( StructDecl * decl );
|
---|
[64adb03] | 194 |
|
---|
[db4d8e3] | 195 | std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
|
---|
[64adb03] | 196 | void validate( DeclarationWithType * );
|
---|
[549c006] | 197 | void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
[97e3296] | 198 | void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
|
---|
[64adb03] | 199 |
|
---|
| 200 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 201 | PassVisitor< MutexKeyword > impl;
|
---|
[64adb03] | 202 | acceptAll( translationUnit, impl );
|
---|
| 203 | }
|
---|
[9243cc91] | 204 |
|
---|
| 205 | private:
|
---|
| 206 | StructDecl* monitor_decl = nullptr;
|
---|
[ef42b143] | 207 | StructDecl* guard_decl = nullptr;
|
---|
[549c006] | 208 | StructDecl* dtor_guard_decl = nullptr;
|
---|
[97e3296] | 209 |
|
---|
| 210 | static std::unique_ptr< Type > generic_func;
|
---|
[64adb03] | 211 | };
|
---|
| 212 |
|
---|
[97e3296] | 213 | std::unique_ptr< Type > MutexKeyword::generic_func = std::unique_ptr< Type >(
|
---|
| 214 | new FunctionType(
|
---|
| 215 | noQualifiers,
|
---|
| 216 | true
|
---|
| 217 | )
|
---|
| 218 | );
|
---|
| 219 |
|
---|
[bd4d011] | 220 | //-----------------------------------------------------------------------------
|
---|
| 221 | //Handles mutex routines definitions :
|
---|
| 222 | // void foo( A * mutex a, B * mutex b, int i ) { void foo( A * a, B * b, int i ) {
|
---|
| 223 | // monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
| 224 | // monitor_guard_t __guard = { __monitors, 2 };
|
---|
| 225 | // /*Some code*/ => /*Some code*/
|
---|
| 226 | // } }
|
---|
| 227 | //
|
---|
[2065609] | 228 | class ThreadStarter final {
|
---|
[bd4d011] | 229 | public:
|
---|
| 230 |
|
---|
[2065609] | 231 | void postvisit( FunctionDecl * decl );
|
---|
[549c006] | 232 | void previsit ( StructDecl * decl );
|
---|
[bd4d011] | 233 |
|
---|
| 234 | void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
|
---|
| 235 |
|
---|
| 236 | static void implement( std::list< Declaration * > & translationUnit ) {
|
---|
[2065609] | 237 | PassVisitor< ThreadStarter > impl;
|
---|
[bd4d011] | 238 | acceptAll( translationUnit, impl );
|
---|
| 239 | }
|
---|
[549c006] | 240 |
|
---|
| 241 | private :
|
---|
| 242 | bool thread_ctor_seen = false;
|
---|
| 243 | StructDecl * thread_decl = nullptr;
|
---|
[bd4d011] | 244 | };
|
---|
| 245 |
|
---|
[64adb03] | 246 | //=============================================================================================
|
---|
| 247 | // General entry routine
|
---|
| 248 | //=============================================================================================
|
---|
| 249 | void applyKeywords( std::list< Declaration * > & translationUnit ) {
|
---|
| 250 | ThreadKeyword ::implement( translationUnit );
|
---|
| 251 | CoroutineKeyword ::implement( translationUnit );
|
---|
| 252 | MonitorKeyword ::implement( translationUnit );
|
---|
[bcda04c] | 253 | }
|
---|
| 254 |
|
---|
| 255 | void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
|
---|
[64adb03] | 256 | MutexKeyword ::implement( translationUnit );
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[bcda04c] | 259 | void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
|
---|
[bd4d011] | 260 | ThreadStarter ::implement( translationUnit );
|
---|
[bcda04c] | 261 | }
|
---|
| 262 |
|
---|
[b32ada31] | 263 | //=============================================================================================
|
---|
[bcda04c] | 264 | // Generic keyword implementation
|
---|
[b32ada31] | 265 | //=============================================================================================
|
---|
[2db79e5] | 266 | void fixupGenerics(FunctionType * func, StructDecl * decl) {
|
---|
| 267 | cloneAll(decl->parameters, func->forall);
|
---|
| 268 | for ( TypeDecl * td : func->forall ) {
|
---|
| 269 | strict_dynamic_cast<StructInstType*>(
|
---|
| 270 | func->parameters.front()->get_type()->stripReferences()
|
---|
| 271 | )->parameters.push_back(
|
---|
| 272 | new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) )
|
---|
| 273 | );
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[9a705dc8] | 277 | Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
|
---|
[9f5ecf5] | 278 | if( decl->name == type_name && decl->body ) {
|
---|
[bcda04c] | 279 | assert( !type_decl );
|
---|
| 280 | type_decl = decl;
|
---|
[b32ada31] | 281 | }
|
---|
[bcda04c] | 282 | else if ( is_target(decl) ) {
|
---|
[b32ada31] | 283 | handle( decl );
|
---|
| 284 | }
|
---|
[9a705dc8] | 285 | return decl;
|
---|
[b32ada31] | 286 | }
|
---|
| 287 |
|
---|
[6c3a5ac1] | 288 | DeclarationWithType * ConcurrentSueKeyword::postmutate( FunctionDecl * decl ) {
|
---|
| 289 | if( !type_decl ) return decl;
|
---|
| 290 | if( !CodeGen::isDestructor( decl->name ) ) return decl;
|
---|
| 291 |
|
---|
| 292 | auto params = decl->type->parameters;
|
---|
| 293 | if( params.size() != 1 ) return decl;
|
---|
| 294 |
|
---|
| 295 | auto type = dynamic_cast<ReferenceType*>( params.front()->get_type() );
|
---|
| 296 | if( !type ) return decl;
|
---|
| 297 |
|
---|
| 298 | auto stype = dynamic_cast<StructInstType*>( type->base );
|
---|
| 299 | if( !stype ) return decl;
|
---|
| 300 | if( stype->baseStruct != type_decl ) return decl;
|
---|
| 301 |
|
---|
| 302 | if( !dtor_decl ) dtor_decl = decl;
|
---|
| 303 | return decl;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[9a705dc8] | 306 | Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
|
---|
| 307 | if ( cast_target == cast->target ) {
|
---|
| 308 | // convert (thread &)t to (thread_desc &)*get_thread(t), etc.
|
---|
| 309 | if( !type_decl ) SemanticError( cast, context_error );
|
---|
[6c3a5ac1] | 310 | if( !dtor_decl ) SemanticError( cast, context_error );
|
---|
[9a705dc8] | 311 | Expression * arg = cast->arg;
|
---|
| 312 | cast->arg = nullptr;
|
---|
| 313 | delete cast;
|
---|
| 314 | return new CastExpr(
|
---|
| 315 | UntypedExpr::createDeref(
|
---|
| 316 | new UntypedExpr( new NameExpr( getter_name ), { arg } )
|
---|
| 317 | ),
|
---|
| 318 | new ReferenceType(
|
---|
| 319 | noQualifiers,
|
---|
| 320 | new StructInstType( noQualifiers, type_decl ) )
|
---|
| 321 | );
|
---|
| 322 | }
|
---|
| 323 | return cast;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 |
|
---|
[bcda04c] | 327 | void ConcurrentSueKeyword::handle( StructDecl * decl ) {
|
---|
[9f5ecf5] | 328 | if( ! decl->body ) return;
|
---|
[b32ada31] | 329 |
|
---|
[a16764a6] | 330 | if( !type_decl ) SemanticError( decl, context_error );
|
---|
[6c3a5ac1] | 331 | if( !dtor_decl ) SemanticError( decl, context_error );
|
---|
[b32ada31] | 332 |
|
---|
[bcda04c] | 333 | FunctionDecl * func = forwardDeclare( decl );
|
---|
| 334 | ObjectDecl * field = addField( decl );
|
---|
[2f9a722] | 335 | addRoutines( field, func );
|
---|
[b32ada31] | 336 | }
|
---|
| 337 |
|
---|
[bcda04c] | 338 | FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
|
---|
| 339 |
|
---|
| 340 | StructDecl * forward = decl->clone();
|
---|
| 341 | forward->set_body( false );
|
---|
| 342 | deleteAll( forward->get_members() );
|
---|
| 343 | forward->get_members().clear();
|
---|
| 344 |
|
---|
[bd4d011] | 345 | FunctionType * get_type = new FunctionType( noQualifiers, false );
|
---|
[bcda04c] | 346 | ObjectDecl * this_decl = new ObjectDecl(
|
---|
| 347 | "this",
|
---|
[ba3706f] | 348 | noStorageClasses,
|
---|
[b32ada31] | 349 | LinkageSpec::Cforall,
|
---|
| 350 | nullptr,
|
---|
[83a071f9] | 351 | new ReferenceType(
|
---|
[b32ada31] | 352 | noQualifiers,
|
---|
[bcda04c] | 353 | new StructInstType(
|
---|
| 354 | noQualifiers,
|
---|
| 355 | decl
|
---|
| 356 | )
|
---|
[b32ada31] | 357 | ),
|
---|
| 358 | nullptr
|
---|
| 359 | );
|
---|
| 360 |
|
---|
[2db79e5] | 361 | get_type->get_parameters().push_back( this_decl->clone() );
|
---|
[bd4d011] | 362 | get_type->get_returnVals().push_back(
|
---|
[e04b636] | 363 | new ObjectDecl(
|
---|
| 364 | "ret",
|
---|
[ba3706f] | 365 | noStorageClasses,
|
---|
[e04b636] | 366 | LinkageSpec::Cforall,
|
---|
| 367 | nullptr,
|
---|
| 368 | new PointerType(
|
---|
| 369 | noQualifiers,
|
---|
| 370 | new StructInstType(
|
---|
| 371 | noQualifiers,
|
---|
[bcda04c] | 372 | type_decl
|
---|
[e04b636] | 373 | )
|
---|
| 374 | ),
|
---|
| 375 | nullptr
|
---|
| 376 | )
|
---|
| 377 | );
|
---|
[2db79e5] | 378 | fixupGenerics(get_type, decl);
|
---|
[b32ada31] | 379 |
|
---|
[bcda04c] | 380 | FunctionDecl * get_decl = new FunctionDecl(
|
---|
| 381 | getter_name,
|
---|
| 382 | Type::Static,
|
---|
| 383 | LinkageSpec::Cforall,
|
---|
[bd4d011] | 384 | get_type,
|
---|
[bcda04c] | 385 | nullptr,
|
---|
| 386 | noAttributes,
|
---|
| 387 | Type::Inline
|
---|
| 388 | );
|
---|
| 389 |
|
---|
[bd4d011] | 390 | FunctionDecl * main_decl = nullptr;
|
---|
| 391 |
|
---|
| 392 | if( needs_main ) {
|
---|
| 393 | FunctionType * main_type = new FunctionType( noQualifiers, false );
|
---|
[bf2438c] | 394 |
|
---|
[bd4d011] | 395 | main_type->get_parameters().push_back( this_decl->clone() );
|
---|
| 396 |
|
---|
| 397 | main_decl = new FunctionDecl(
|
---|
| 398 | "main",
|
---|
[ba3706f] | 399 | noStorageClasses,
|
---|
[bd4d011] | 400 | LinkageSpec::Cforall,
|
---|
| 401 | main_type,
|
---|
| 402 | nullptr
|
---|
| 403 | );
|
---|
[2db79e5] | 404 | fixupGenerics(main_type, decl);
|
---|
[bd4d011] | 405 | }
|
---|
| 406 |
|
---|
[2db79e5] | 407 | delete this_decl;
|
---|
| 408 |
|
---|
[2065609] | 409 | declsToAddBefore.push_back( forward );
|
---|
| 410 | if( needs_main ) declsToAddBefore.push_back( main_decl );
|
---|
| 411 | declsToAddBefore.push_back( get_decl );
|
---|
[bcda04c] | 412 |
|
---|
| 413 | return get_decl;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
|
---|
| 417 | ObjectDecl * field = new ObjectDecl(
|
---|
| 418 | field_name,
|
---|
[ba3706f] | 419 | noStorageClasses,
|
---|
[bcda04c] | 420 | LinkageSpec::Cforall,
|
---|
| 421 | nullptr,
|
---|
| 422 | new StructInstType(
|
---|
| 423 | noQualifiers,
|
---|
| 424 | type_decl
|
---|
| 425 | ),
|
---|
| 426 | nullptr
|
---|
| 427 | );
|
---|
| 428 |
|
---|
| 429 | decl->get_members().push_back( field );
|
---|
| 430 |
|
---|
| 431 | return field;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[2f9a722] | 434 | void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
|
---|
[ba3706f] | 435 | CompoundStmt * statement = new CompoundStmt();
|
---|
[bf2438c] | 436 | statement->push_back(
|
---|
[b32ada31] | 437 | new ReturnStmt(
|
---|
[e04b636] | 438 | new AddressExpr(
|
---|
[bcda04c] | 439 | new MemberExpr(
|
---|
| 440 | field,
|
---|
[2db79e5] | 441 | new CastExpr(
|
---|
| 442 | new VariableExpr( func->get_functionType()->get_parameters().front() ),
|
---|
| 443 | func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone()
|
---|
| 444 | )
|
---|
[e04b636] | 445 | )
|
---|
[b32ada31] | 446 | )
|
---|
| 447 | )
|
---|
| 448 | );
|
---|
| 449 |
|
---|
[bcda04c] | 450 | FunctionDecl * get_decl = func->clone();
|
---|
| 451 |
|
---|
| 452 | get_decl->set_statements( statement );
|
---|
[e04b636] | 453 |
|
---|
| 454 | declsToAddAfter.push_back( get_decl );
|
---|
| 455 |
|
---|
[bcda04c] | 456 | // get_decl->fixUniqueId();
|
---|
[b32ada31] | 457 | }
|
---|
| 458 |
|
---|
[64adb03] | 459 | //=============================================================================================
|
---|
| 460 | // Mutex keyword implementation
|
---|
| 461 | //=============================================================================================
|
---|
[97e3296] | 462 |
|
---|
[2065609] | 463 | void MutexKeyword::postvisit(FunctionDecl* decl) {
|
---|
[102a58b] | 464 |
|
---|
[db4d8e3] | 465 | bool first = false;
|
---|
| 466 | std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
|
---|
[ceedde6] | 467 | bool isDtor = CodeGen::isDestructor( decl->name );
|
---|
[64adb03] | 468 |
|
---|
[ceedde6] | 469 | // Is this function relevant to monitors
|
---|
| 470 | if( mutexArgs.empty() ) {
|
---|
| 471 | // If this is the destructor for a monitor it must be mutex
|
---|
| 472 | if(isDtor) {
|
---|
| 473 | Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
|
---|
| 474 |
|
---|
| 475 | // If it's a copy, it's not a mutex
|
---|
| 476 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
| 477 | if( ! rty ) return;
|
---|
| 478 |
|
---|
| 479 | // If we are not pointing directly to a type, it's not a mutex
|
---|
| 480 | Type* base = rty->get_base();
|
---|
| 481 | if( dynamic_cast< ReferenceType * >( base ) ) return;
|
---|
| 482 | if( dynamic_cast< PointerType * >( base ) ) return;
|
---|
| 483 |
|
---|
| 484 | // Check if its a struct
|
---|
| 485 | StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
|
---|
| 486 | if( !baseStruct ) return;
|
---|
| 487 |
|
---|
| 488 | // Check if its a monitor
|
---|
| 489 | if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
|
---|
| 490 | SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
|
---|
| 491 | }
|
---|
| 492 | return;
|
---|
| 493 | }
|
---|
[549c006] | 494 |
|
---|
[ceedde6] | 495 | // Monitors can't be constructed with mutual exclusion
|
---|
| 496 | if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
|
---|
[549c006] | 497 |
|
---|
[ceedde6] | 498 | // It makes no sense to have multiple mutex parameters for the destructor
|
---|
[a16764a6] | 499 | if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
|
---|
[549c006] | 500 |
|
---|
[ceedde6] | 501 | // Make sure all the mutex arguments are monitors
|
---|
[64adb03] | 502 | for(auto arg : mutexArgs) {
|
---|
| 503 | validate( arg );
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[ceedde6] | 506 | // Check if we need to instrument the body
|
---|
[64adb03] | 507 | CompoundStmt* body = decl->get_statements();
|
---|
| 508 | if( ! body ) return;
|
---|
| 509 |
|
---|
[ceedde6] | 510 | // Do we have the required headers
|
---|
[a16764a6] | 511 | if( !monitor_decl || !guard_decl || !dtor_guard_decl )
|
---|
[73abe95] | 512 | SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>\n" );
|
---|
[b32ada31] | 513 |
|
---|
[ceedde6] | 514 | // Instrument the body
|
---|
[549c006] | 515 | if( isDtor ) {
|
---|
| 516 | addDtorStatments( decl, body, mutexArgs );
|
---|
| 517 | }
|
---|
| 518 | else {
|
---|
| 519 | addStatments( decl, body, mutexArgs );
|
---|
| 520 | }
|
---|
[64adb03] | 521 | }
|
---|
| 522 |
|
---|
[2065609] | 523 | void MutexKeyword::postvisit(StructDecl* decl) {
|
---|
[102a58b] | 524 |
|
---|
[88d955f] | 525 | if( decl->name == "monitor_desc" && decl->body ) {
|
---|
[9243cc91] | 526 | assert( !monitor_decl );
|
---|
| 527 | monitor_decl = decl;
|
---|
| 528 | }
|
---|
[88d955f] | 529 | else if( decl->name == "monitor_guard_t" && decl->body ) {
|
---|
[ef42b143] | 530 | assert( !guard_decl );
|
---|
| 531 | guard_decl = decl;
|
---|
| 532 | }
|
---|
[88d955f] | 533 | else if( decl->name == "monitor_dtor_guard_t" && decl->body ) {
|
---|
[549c006] | 534 | assert( !dtor_guard_decl );
|
---|
| 535 | dtor_guard_decl = decl;
|
---|
| 536 | }
|
---|
[9243cc91] | 537 | }
|
---|
| 538 |
|
---|
[db4d8e3] | 539 | std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
|
---|
[64adb03] | 540 | std::list<DeclarationWithType*> mutexArgs;
|
---|
| 541 |
|
---|
[db4d8e3] | 542 | bool once = true;
|
---|
[64adb03] | 543 | for( auto arg : decl->get_functionType()->get_parameters()) {
|
---|
| 544 | //Find mutex arguments
|
---|
| 545 | Type* ty = arg->get_type();
|
---|
[615a096] | 546 | if( ! ty->get_mutex() ) continue;
|
---|
[64adb03] | 547 |
|
---|
[db4d8e3] | 548 | if(once) {first = true;}
|
---|
| 549 | once = false;
|
---|
| 550 |
|
---|
[64adb03] | 551 | //Append it to the list
|
---|
| 552 | mutexArgs.push_back( arg );
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | return mutexArgs;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | void MutexKeyword::validate( DeclarationWithType * arg ) {
|
---|
| 559 | Type* ty = arg->get_type();
|
---|
| 560 |
|
---|
| 561 | //Makes sure it's not a copy
|
---|
[870d1f0] | 562 | ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
|
---|
[a16764a6] | 563 | if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " );
|
---|
[64adb03] | 564 |
|
---|
| 565 | //Make sure the we are pointing directly to a type
|
---|
[83a071f9] | 566 | Type* base = rty->get_base();
|
---|
[a16764a6] | 567 | if( dynamic_cast< ReferenceType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
| 568 | if( dynamic_cast< PointerType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
|
---|
[64adb03] | 569 |
|
---|
| 570 | //Make sure that typed isn't mutex
|
---|
[a16764a6] | 571 | if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " );
|
---|
[64adb03] | 572 | }
|
---|
| 573 |
|
---|
[549c006] | 574 | void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
| 575 | Type * arg_type = args.front()->get_type()->clone();
|
---|
| 576 | arg_type->set_mutex( false );
|
---|
| 577 |
|
---|
| 578 | ObjectDecl * monitors = new ObjectDecl(
|
---|
| 579 | "__monitor",
|
---|
[ba3706f] | 580 | noStorageClasses,
|
---|
[549c006] | 581 | LinkageSpec::Cforall,
|
---|
| 582 | nullptr,
|
---|
| 583 | new PointerType(
|
---|
| 584 | noQualifiers,
|
---|
| 585 | new StructInstType(
|
---|
| 586 | noQualifiers,
|
---|
| 587 | monitor_decl
|
---|
| 588 | )
|
---|
| 589 | ),
|
---|
| 590 | new SingleInit( new UntypedExpr(
|
---|
| 591 | new NameExpr( "get_monitor" ),
|
---|
| 592 | { new CastExpr( new VariableExpr( args.front() ), arg_type ) }
|
---|
| 593 | ))
|
---|
| 594 | );
|
---|
| 595 |
|
---|
| 596 | assert(generic_func);
|
---|
| 597 |
|
---|
| 598 | //in reverse order :
|
---|
[2bf7ef6] | 599 | // monitor_dtor_guard_t __guard = { __monitors, func };
|
---|
[549c006] | 600 | body->push_front(
|
---|
[ba3706f] | 601 | new DeclStmt( new ObjectDecl(
|
---|
[549c006] | 602 | "__guard",
|
---|
[ba3706f] | 603 | noStorageClasses,
|
---|
[549c006] | 604 | LinkageSpec::Cforall,
|
---|
| 605 | nullptr,
|
---|
| 606 | new StructInstType(
|
---|
| 607 | noQualifiers,
|
---|
| 608 | dtor_guard_decl
|
---|
| 609 | ),
|
---|
| 610 | new ListInit(
|
---|
| 611 | {
|
---|
| 612 | new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ),
|
---|
| 613 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
|
---|
| 614 | },
|
---|
| 615 | noDesignators,
|
---|
| 616 | true
|
---|
| 617 | )
|
---|
| 618 | ))
|
---|
| 619 | );
|
---|
| 620 |
|
---|
| 621 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[ba3706f] | 622 | body->push_front( new DeclStmt( monitors) );
|
---|
[549c006] | 623 | }
|
---|
| 624 |
|
---|
[97e3296] | 625 | void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
|
---|
[9243cc91] | 626 | ObjectDecl * monitors = new ObjectDecl(
|
---|
| 627 | "__monitors",
|
---|
[ba3706f] | 628 | noStorageClasses,
|
---|
[9243cc91] | 629 | LinkageSpec::Cforall,
|
---|
| 630 | nullptr,
|
---|
| 631 | new ArrayType(
|
---|
| 632 | noQualifiers,
|
---|
| 633 | new PointerType(
|
---|
| 634 | noQualifiers,
|
---|
| 635 | new StructInstType(
|
---|
| 636 | noQualifiers,
|
---|
| 637 | monitor_decl
|
---|
| 638 | )
|
---|
| 639 | ),
|
---|
| 640 | new ConstantExpr( Constant::from_ulong( args.size() ) ),
|
---|
| 641 | false,
|
---|
| 642 | false
|
---|
| 643 | ),
|
---|
| 644 | new ListInit(
|
---|
[bd41764] | 645 | map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
|
---|
[cb0e6de] | 646 | Type * type = var->get_type()->clone();
|
---|
| 647 | type->set_mutex( false );
|
---|
[9243cc91] | 648 | return new SingleInit( new UntypedExpr(
|
---|
| 649 | new NameExpr( "get_monitor" ),
|
---|
[cb0e6de] | 650 | { new CastExpr( new VariableExpr( var ), type ) }
|
---|
[9243cc91] | 651 | ) );
|
---|
| 652 | })
|
---|
| 653 | )
|
---|
| 654 | );
|
---|
| 655 |
|
---|
[97e3296] | 656 | assert(generic_func);
|
---|
| 657 |
|
---|
[2bf7ef6] | 658 | // in reverse order :
|
---|
[97e3296] | 659 | // monitor_guard_t __guard = { __monitors, #, func };
|
---|
[64adb03] | 660 | body->push_front(
|
---|
[ba3706f] | 661 | new DeclStmt( new ObjectDecl(
|
---|
[64adb03] | 662 | "__guard",
|
---|
[ba3706f] | 663 | noStorageClasses,
|
---|
[64adb03] | 664 | LinkageSpec::Cforall,
|
---|
| 665 | nullptr,
|
---|
| 666 | new StructInstType(
|
---|
| 667 | noQualifiers,
|
---|
[ef42b143] | 668 | guard_decl
|
---|
[64adb03] | 669 | ),
|
---|
| 670 | new ListInit(
|
---|
| 671 | {
|
---|
[9243cc91] | 672 | new SingleInit( new VariableExpr( monitors ) ),
|
---|
[97e3296] | 673 | new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
|
---|
| 674 | new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
|
---|
[ef42b143] | 675 | },
|
---|
| 676 | noDesignators,
|
---|
| 677 | true
|
---|
[64adb03] | 678 | )
|
---|
| 679 | ))
|
---|
| 680 | );
|
---|
| 681 |
|
---|
[ef42b143] | 682 | //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
|
---|
[ba3706f] | 683 | body->push_front( new DeclStmt( monitors) );
|
---|
[64adb03] | 684 | }
|
---|
[bd4d011] | 685 |
|
---|
| 686 | //=============================================================================================
|
---|
| 687 | // General entry routine
|
---|
| 688 | //=============================================================================================
|
---|
[549c006] | 689 | void ThreadStarter::previsit( StructDecl * decl ) {
|
---|
| 690 | if( decl->name == "thread_desc" && decl->body ) {
|
---|
| 691 | assert( !thread_decl );
|
---|
| 692 | thread_decl = decl;
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[2065609] | 696 | void ThreadStarter::postvisit(FunctionDecl * decl) {
|
---|
[9f5ecf5] | 697 | if( ! CodeGen::isConstructor(decl->name) ) return;
|
---|
[bd4d011] | 698 |
|
---|
[549c006] | 699 | Type * typeof_this = InitTweak::getTypeofThis(decl->type);
|
---|
| 700 | StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this );
|
---|
| 701 | if( ctored_type && ctored_type->baseStruct == thread_decl ) {
|
---|
| 702 | thread_ctor_seen = true;
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[bd4d011] | 705 | DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
|
---|
[ce8c12f] | 706 | auto type = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
|
---|
[bd4d011] | 707 | if( type && type->get_baseStruct()->is_thread() ) {
|
---|
[549c006] | 708 | if( !thread_decl || !thread_ctor_seen ) {
|
---|
[73abe95] | 709 | SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>");
|
---|
[549c006] | 710 | }
|
---|
| 711 |
|
---|
[bd4d011] | 712 | addStartStatement( decl, param );
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
|
---|
| 717 | CompoundStmt * stmt = decl->get_statements();
|
---|
| 718 |
|
---|
| 719 | if( ! stmt ) return;
|
---|
| 720 |
|
---|
[bf2438c] | 721 | stmt->push_back(
|
---|
[bd4d011] | 722 | new ExprStmt(
|
---|
| 723 | new UntypedExpr(
|
---|
| 724 | new NameExpr( "__thrd_start" ),
|
---|
| 725 | { new VariableExpr( param ) }
|
---|
| 726 | )
|
---|
| 727 | )
|
---|
| 728 | );
|
---|
| 729 | }
|
---|
[68fe077a] | 730 | };
|
---|
[6b0b624] | 731 |
|
---|
| 732 | // Local Variables: //
|
---|
| 733 | // mode: c //
|
---|
| 734 | // tab-width: 4 //
|
---|
| 735 | // End: //
|
---|