[3dd8f42] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 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 | // Actors.cpp -- generate code needed by the actor system
|
---|
| 8 | //
|
---|
| 9 | // Author : Colby Parsons
|
---|
| 10 | // Created On : Thurs Jan 19 15:34:00 2023
|
---|
| 11 | // Last Modified By : Colby Parsons
|
---|
| 12 | // Last Modified On : Thurs Jan 19 15:34:00 2023
|
---|
| 13 | // Update Count : 0
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "AST/Print.hpp"
|
---|
| 17 | #include "AST/Decl.hpp"
|
---|
| 18 | #include "AST/Pass.hpp"
|
---|
| 19 | #include "AST/Type.hpp"
|
---|
| 20 | #include "AST/Stmt.hpp"
|
---|
| 21 | #include "AST/TranslationUnit.hpp"
|
---|
| 22 | #include "AST/Expr.hpp"
|
---|
[96ddc62] | 23 | #include <algorithm>
|
---|
[3dd8f42] | 24 | using namespace ast;
|
---|
[96ddc62] | 25 | using namespace std;
|
---|
[3dd8f42] | 26 |
|
---|
| 27 | namespace Concurrency {
|
---|
| 28 |
|
---|
| 29 | struct CollectactorStructDecls : public ast::WithGuards {
|
---|
[fc1a3e2] | 30 | unordered_set<const StructDecl *> & actorStructDecls;
|
---|
[1ee74df] | 31 | unordered_set<const StructDecl *> & messageStructDecls;
|
---|
| 32 | const StructDecl *& requestDecl;
|
---|
| 33 | const EnumDecl *& allocationDecl;
|
---|
| 34 | const StructDecl *& actorDecl;
|
---|
| 35 | const StructDecl *& msgDecl;
|
---|
[fc1a3e2] | 36 | StructDecl * parentDecl;
|
---|
| 37 | bool insideStruct = false;
|
---|
| 38 | bool namedDecl = false;
|
---|
| 39 |
|
---|
| 40 | // finds and sets a ptr to the allocation enum, which is needed in the next pass
|
---|
| 41 | void previsit( const EnumDecl * decl ) {
|
---|
[1ee74df] | 42 | if( decl->name == "allocation" ) allocationDecl = decl;
|
---|
[3dd8f42] | 43 | }
|
---|
| 44 |
|
---|
[fc1a3e2] | 45 | // finds and sets a ptr to the actor, message, and request structs, which are needed in the next pass
|
---|
| 46 | void previsit( const StructDecl * decl ) {
|
---|
| 47 | if ( !decl->body ) return;
|
---|
| 48 | if ( decl->name == "actor" ) {
|
---|
| 49 | actorStructDecls.insert( decl ); // skip inserting fwd decl
|
---|
[1ee74df] | 50 | actorDecl = decl;
|
---|
[fc1a3e2] | 51 | } else if( decl->name == "message" ) {
|
---|
| 52 | messageStructDecls.insert( decl ); // skip inserting fwd decl
|
---|
[1ee74df] | 53 | msgDecl = decl;
|
---|
| 54 | } else if( decl->name == "request" ) {
|
---|
| 55 | requestDecl = decl;
|
---|
| 56 | } else {
|
---|
[fc1a3e2] | 57 | GuardValue(insideStruct);
|
---|
| 58 | insideStruct = true;
|
---|
| 59 | parentDecl = mutate( decl );
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | // this catches structs of the form:
|
---|
| 64 | // struct dummy_actor { actor a; };
|
---|
| 65 | // since they should be:
|
---|
| 66 | // struct dummy_actor { inline actor; };
|
---|
| 67 | void previsit ( const ObjectDecl * decl ) {
|
---|
| 68 | if ( insideStruct && ! decl->name.empty() ) {
|
---|
| 69 | GuardValue(namedDecl);
|
---|
| 70 | namedDecl = true;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | // this collects the derived actor and message struct decl ptrs
|
---|
| 75 | void postvisit( const StructInstType * node ) {
|
---|
[1ee74df] | 76 | if ( !actorDecl || !msgDecl ) return;
|
---|
[fc1a3e2] | 77 | if ( insideStruct && !namedDecl ) {
|
---|
| 78 | auto actorIter = actorStructDecls.find( node->aggr() );
|
---|
| 79 | if ( actorIter != actorStructDecls.end() ) {
|
---|
| 80 | actorStructDecls.insert( parentDecl );
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 | auto messageIter = messageStructDecls.find( node->aggr() );
|
---|
| 84 | if ( messageIter != messageStructDecls.end() ) {
|
---|
| 85 | messageStructDecls.insert( parentDecl );
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[3dd8f42] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public:
|
---|
[fc1a3e2] | 91 | CollectactorStructDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
---|
[1ee74df] | 92 | const StructDecl *& requestDecl, const EnumDecl *& allocationDecl, const StructDecl *& actorDecl, const StructDecl *& msgDecl )
|
---|
[fc1a3e2] | 93 | : actorStructDecls( actorStructDecls ), messageStructDecls( messageStructDecls ), requestDecl( requestDecl ),
|
---|
| 94 | allocationDecl( allocationDecl ), actorDecl(actorDecl), msgDecl(msgDecl) {}
|
---|
[3dd8f42] | 95 | };
|
---|
| 96 |
|
---|
[96ddc62] | 97 | // keeps track of all fwdDecls of message routines so that we can hoist them to right after the appropriate decls
|
---|
| 98 | class FwdDeclTable {
|
---|
| 99 |
|
---|
[fc1a3e2] | 100 | // tracks which decls we have seen so that we can hoist the FunctionDecl to the highest point possible
|
---|
| 101 | struct FwdDeclData {
|
---|
| 102 | const StructDecl * actorDecl;
|
---|
| 103 | const StructDecl * msgDecl;
|
---|
| 104 | FunctionDecl * fwdDecl;
|
---|
| 105 | bool actorFound;
|
---|
| 106 | bool msgFound;
|
---|
| 107 |
|
---|
| 108 | bool readyToInsert() { return actorFound && msgFound; }
|
---|
| 109 | bool foundActor() { actorFound = true; return readyToInsert(); }
|
---|
| 110 | bool foundMsg() { msgFound = true; return readyToInsert(); }
|
---|
| 111 |
|
---|
| 112 | FwdDeclData( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) :
|
---|
| 113 | actorDecl(actorDecl), msgDecl(msgDecl), fwdDecl(fwdDecl), actorFound(false), msgFound(false) {}
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | // map indexed by actor struct ptr
|
---|
| 117 | // value is map of all FwdDeclData that contains said actor struct ptr
|
---|
| 118 | // inner map is indexed by the message struct ptr of FwdDeclData
|
---|
| 119 | unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> actorMap;
|
---|
| 120 |
|
---|
| 121 | // this map is the same except the outer map is indexed by message ptr and the inner is indexed by actor ptr
|
---|
| 122 | unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> msgMap;
|
---|
| 123 |
|
---|
| 124 | void insert( const StructDecl * decl, const StructDecl * otherDecl, unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map, FwdDeclData * data ) {
|
---|
| 125 | auto iter = map.find( decl );
|
---|
| 126 | if ( iter != map.end() ) { // if decl exists in map append data to existing inner map
|
---|
| 127 | iter->second.emplace( make_pair( otherDecl, data ) );
|
---|
| 128 | } else { // else create inner map for key
|
---|
| 129 | map.emplace( make_pair( decl, unordered_map<const StructDecl *, FwdDeclData *>( { make_pair( otherDecl, data ) } ) ) );
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[96ddc62] | 132 |
|
---|
| 133 | public:
|
---|
[fc1a3e2] | 134 | // insert decl into table so that we can fwd declare it later (average cost: O(1))
|
---|
| 135 | void insertDecl( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) {
|
---|
| 136 | FwdDeclData * declToInsert = new FwdDeclData( actorDecl, msgDecl, fwdDecl );
|
---|
| 137 | insert( actorDecl, msgDecl, actorMap, declToInsert );
|
---|
| 138 | insert( msgDecl, actorDecl, msgMap, declToInsert );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // returns list of decls to insert after current struct decl
|
---|
| 142 | // Over the entire pass the runtime of this routine is O(r) where r is the # of receive routines
|
---|
| 143 | list<FunctionDecl *> updateDecl( const StructDecl * decl, bool isMsg ) {
|
---|
| 144 | unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map = isMsg ? msgMap : actorMap;
|
---|
| 145 | unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & otherMap = isMsg ? actorMap : msgMap;
|
---|
| 146 | auto iter = map.find( decl );
|
---|
| 147 | list<FunctionDecl *> toInsertAfter; // this is populated with decls that are ready to insert
|
---|
| 148 | if ( iter == map.end() ) return toInsertAfter;
|
---|
| 149 |
|
---|
| 150 | // iterate over inner map
|
---|
| 151 | unordered_map<const StructDecl *, FwdDeclData *> & currInnerMap = iter->second;
|
---|
| 152 | for ( auto innerIter = currInnerMap.begin(); innerIter != currInnerMap.end(); ) {
|
---|
| 153 | FwdDeclData * currentDatum = innerIter->second;
|
---|
| 154 | bool readyToInsert = isMsg ? currentDatum->foundMsg() : currentDatum->foundActor();
|
---|
| 155 | if ( ! readyToInsert ) { ++innerIter; continue; }
|
---|
| 156 |
|
---|
| 157 | // readyToInsert is true so we are good to insert the forward decl of the message fn
|
---|
| 158 | toInsertAfter.push_back( currentDatum->fwdDecl );
|
---|
| 159 |
|
---|
| 160 | // need to remove from other map before deleting
|
---|
| 161 | // find inner map in other map ( other map is actor map if original is msg map and vice versa )
|
---|
| 162 | const StructDecl * otherDecl = isMsg ? currentDatum->actorDecl : currentDatum->msgDecl;
|
---|
| 163 | auto otherMapIter = otherMap.find( otherDecl );
|
---|
| 164 |
|
---|
| 165 | unordered_map<const StructDecl *, FwdDeclData *> & otherInnerMap = otherMapIter->second;
|
---|
| 166 |
|
---|
| 167 | // find the FwdDeclData we need to remove in the other inner map
|
---|
| 168 | auto otherInnerIter = otherInnerMap.find( decl );
|
---|
| 169 |
|
---|
| 170 | // remove references to deleted FwdDeclData from current inner map
|
---|
| 171 | innerIter = currInnerMap.erase( innerIter ); // this does the increment so no explicit inc needed
|
---|
| 172 |
|
---|
| 173 | // remove references to deleted FwdDeclData from other inner map
|
---|
| 174 | otherInnerMap.erase( otherInnerIter );
|
---|
| 175 |
|
---|
| 176 | // if other inner map is now empty, remove key from other outer map
|
---|
| 177 | if ( otherInnerMap.empty() )
|
---|
| 178 | otherMap.erase( otherDecl );
|
---|
| 179 |
|
---|
| 180 | // now we are safe to delete the FwdDeclData since we are done with it
|
---|
| 181 | // and we have removed all references to it from our data structures
|
---|
| 182 | delete currentDatum;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | // if current inner map is now empty, remove key from outer map.
|
---|
| 186 | // Have to do this after iterating for safety
|
---|
| 187 | if ( currInnerMap.empty() )
|
---|
| 188 | map.erase( decl );
|
---|
| 189 |
|
---|
| 190 | return toInsertAfter;
|
---|
| 191 | }
|
---|
[96ddc62] | 192 | };
|
---|
| 193 |
|
---|
[3830c84] | 194 | // generates the definitions of send operators for actors
|
---|
[fc1a3e2] | 195 | // collects data needed for next pass that does the circular defn resolution
|
---|
[3830c84] | 196 | // for message send operators (via table above)
|
---|
[ed96731] | 197 | struct GenFuncsCreateTables : public ast::WithDeclsToAdd {
|
---|
[fc1a3e2] | 198 | unordered_set<const StructDecl *> & actorStructDecls;
|
---|
[1ee74df] | 199 | unordered_set<const StructDecl *> & messageStructDecls;
|
---|
| 200 | const StructDecl *& requestDecl;
|
---|
| 201 | const EnumDecl *& allocationDecl;
|
---|
| 202 | const StructDecl *& actorDecl;
|
---|
| 203 | const StructDecl *& msgDecl;
|
---|
[fc1a3e2] | 204 | FwdDeclTable & forwardDecls;
|
---|
| 205 |
|
---|
| 206 | // generates the operator for actor message sends
|
---|
[3dd8f42] | 207 | void postvisit( const FunctionDecl * decl ) {
|
---|
[fc1a3e2] | 208 | // return if not of the form receive( param1, param2 ) or if it is a forward decl
|
---|
| 209 | if ( decl->name != "receive" || decl->params.size() != 2 || !decl->stmts ) return;
|
---|
| 210 |
|
---|
| 211 | // the params should be references
|
---|
| 212 | const ReferenceType * derivedActorRef = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
|
---|
| 213 | const ReferenceType * derivedMsgRef = dynamic_cast<const ReferenceType *>(decl->params.at(1)->get_type());
|
---|
| 214 | if ( !derivedActorRef || !derivedMsgRef ) return;
|
---|
| 215 |
|
---|
| 216 | // the references should be to struct instances
|
---|
| 217 | const StructInstType * arg1InstType = dynamic_cast<const StructInstType *>(derivedActorRef->base.get());
|
---|
| 218 | const StructInstType * arg2InstType = dynamic_cast<const StructInstType *>(derivedMsgRef->base.get());
|
---|
| 219 | if ( !arg1InstType || !arg2InstType ) return;
|
---|
| 220 |
|
---|
| 221 | // If the struct instances are derived actor and message types then generate the message send routine
|
---|
| 222 | auto actorIter = actorStructDecls.find( arg1InstType->aggr() );
|
---|
| 223 | auto messageIter = messageStructDecls.find( arg2InstType->aggr() );
|
---|
| 224 | if ( actorIter != actorStructDecls.end() && messageIter != messageStructDecls.end() ) {
|
---|
| 225 | //////////////////////////////////////////////////////////////////////
|
---|
| 226 | // The following generates this wrapper for all receive(derived_actor &, derived_msg &) functions
|
---|
| 227 | /* base_actor and base_msg are output params
|
---|
| 228 | static inline allocation __CFA_receive_wrap( derived_actor & receiver, derived_msg & msg, actor ** base_actor, message ** base_msg ) {
|
---|
| 229 | base_actor = &receiver;
|
---|
| 230 | base_msg = &msg;
|
---|
| 231 | return receive( receiver, msg );
|
---|
| 232 | }
|
---|
| 233 | */
|
---|
| 234 | CompoundStmt * wrapBody = new CompoundStmt( decl->location );
|
---|
| 235 |
|
---|
| 236 | // generates: base_actor = &receiver;
|
---|
| 237 | wrapBody->push_back( new ExprStmt( decl->location,
|
---|
| 238 | UntypedExpr::createAssign( decl->location,
|
---|
| 239 | UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "base_actor" ) ),
|
---|
| 240 | new AddressExpr( decl->location, new NameExpr( decl->location, "receiver" ) )
|
---|
| 241 | )
|
---|
| 242 | ));
|
---|
| 243 |
|
---|
| 244 | // generates: base_msg = &msg;
|
---|
| 245 | wrapBody->push_back( new ExprStmt( decl->location,
|
---|
| 246 | UntypedExpr::createAssign( decl->location,
|
---|
| 247 | UntypedExpr::createDeref( decl->location, new NameExpr( decl->location, "base_msg" ) ),
|
---|
| 248 | new AddressExpr( decl->location, new NameExpr( decl->location, "msg" ) )
|
---|
| 249 | )
|
---|
| 250 | ));
|
---|
| 251 |
|
---|
| 252 | // generates: return receive( receiver, msg );
|
---|
| 253 | wrapBody->push_back( new ReturnStmt( decl->location,
|
---|
| 254 | new UntypedExpr ( decl->location,
|
---|
| 255 | new NameExpr( decl->location, "receive" ),
|
---|
| 256 | {
|
---|
| 257 | new NameExpr( decl->location, "receiver" ),
|
---|
| 258 | new NameExpr( decl->location, "msg" )
|
---|
| 259 | }
|
---|
| 260 | )
|
---|
| 261 | ));
|
---|
| 262 |
|
---|
| 263 | // create receive wrapper to extract base message and actor pointer
|
---|
| 264 | // put it all together into the complete function decl from above
|
---|
| 265 | FunctionDecl * receiveWrapper = new FunctionDecl(
|
---|
| 266 | decl->location,
|
---|
| 267 | "__CFA_receive_wrap",
|
---|
| 268 | {
|
---|
| 269 | new ObjectDecl(
|
---|
| 270 | decl->location,
|
---|
| 271 | "receiver",
|
---|
| 272 | ast::deepCopy( derivedActorRef )
|
---|
| 273 | ),
|
---|
| 274 | new ObjectDecl(
|
---|
| 275 | decl->location,
|
---|
| 276 | "msg",
|
---|
| 277 | ast::deepCopy( derivedMsgRef )
|
---|
| 278 | ),
|
---|
| 279 | new ObjectDecl(
|
---|
| 280 | decl->location,
|
---|
| 281 | "base_actor",
|
---|
[1ee74df] | 282 | new PointerType( new PointerType( new StructInstType( actorDecl ) ) )
|
---|
[fc1a3e2] | 283 | ),
|
---|
| 284 | new ObjectDecl(
|
---|
| 285 | decl->location,
|
---|
| 286 | "base_msg",
|
---|
[1ee74df] | 287 | new PointerType( new PointerType( new StructInstType( msgDecl ) ) )
|
---|
[fc1a3e2] | 288 | )
|
---|
| 289 | }, // params
|
---|
| 290 | {
|
---|
| 291 | new ObjectDecl(
|
---|
| 292 | decl->location,
|
---|
| 293 | "__CFA_receive_wrap_ret",
|
---|
[1ee74df] | 294 | new EnumInstType( allocationDecl )
|
---|
[fc1a3e2] | 295 | )
|
---|
| 296 | },
|
---|
| 297 | wrapBody, // body
|
---|
| 298 | { Storage::Static }, // storage
|
---|
| 299 | Linkage::Cforall, // linkage
|
---|
| 300 | {}, // attributes
|
---|
| 301 | { Function::Inline }
|
---|
| 302 | );
|
---|
| 303 |
|
---|
| 304 | declsToAddAfter.push_back( receiveWrapper );
|
---|
| 305 |
|
---|
| 306 | //////////////////////////////////////////////////////////////////////
|
---|
| 307 | // The following generates this send message operator routine for all receive(derived_actor &, derived_msg &) functions
|
---|
| 308 | /*
|
---|
| 309 | static inline derived_actor & ?|?( derived_actor & receiver, derived_msg & msg ) {
|
---|
| 310 | request new_req;
|
---|
| 311 | allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
|
---|
| 312 | __receive_fn fn = (__receive_fn)my_work_fn;
|
---|
| 313 | new_req{ &receiver, &msg, fn };
|
---|
| 314 | send( receiver, new_req );
|
---|
| 315 | return receiver;
|
---|
| 316 | }
|
---|
| 317 | */
|
---|
| 318 | CompoundStmt * sendBody = new CompoundStmt( decl->location );
|
---|
| 319 |
|
---|
| 320 | // Generates: request new_req;
|
---|
| 321 | sendBody->push_back( new DeclStmt(
|
---|
| 322 | decl->location,
|
---|
| 323 | new ObjectDecl(
|
---|
| 324 | decl->location,
|
---|
| 325 | "new_req",
|
---|
[1ee74df] | 326 | new StructInstType( requestDecl )
|
---|
[fc1a3e2] | 327 | )
|
---|
| 328 | ));
|
---|
| 329 |
|
---|
| 330 | // Function type is: allocation (*)( derived_actor &, derived_msg &, actor **, message ** )
|
---|
| 331 | FunctionType * derivedReceive = new FunctionType();
|
---|
| 332 | derivedReceive->params.push_back( ast::deepCopy( derivedActorRef ) );
|
---|
| 333 | derivedReceive->params.push_back( ast::deepCopy( derivedMsgRef ) );
|
---|
[1ee74df] | 334 | derivedReceive->params.push_back( new PointerType( new PointerType( new StructInstType( actorDecl ) ) ) );
|
---|
| 335 | derivedReceive->params.push_back( new PointerType( new PointerType( new StructInstType( msgDecl ) ) ) );
|
---|
| 336 | derivedReceive->returns.push_back( new EnumInstType( allocationDecl ) );
|
---|
[fc1a3e2] | 337 |
|
---|
| 338 | // Generates: allocation (*my_work_fn)( derived_actor &, derived_msg &, actor **, message ** ) = receive;
|
---|
| 339 | sendBody->push_back( new DeclStmt(
|
---|
| 340 | decl->location,
|
---|
| 341 | new ObjectDecl(
|
---|
| 342 | decl->location,
|
---|
| 343 | "my_work_fn",
|
---|
| 344 | new PointerType( derivedReceive ),
|
---|
| 345 | new SingleInit( decl->location, new NameExpr( decl->location, "__CFA_receive_wrap" ) )
|
---|
| 346 | )
|
---|
| 347 | ));
|
---|
| 348 |
|
---|
| 349 | // Function type is: allocation (*)( actor &, message & )
|
---|
| 350 | FunctionType * genericReceive = new FunctionType();
|
---|
[1ee74df] | 351 | genericReceive->params.push_back( new ReferenceType( new StructInstType( actorDecl ) ) );
|
---|
| 352 | genericReceive->params.push_back( new ReferenceType( new StructInstType( msgDecl ) ) );
|
---|
| 353 | genericReceive->params.push_back( new PointerType( new PointerType( new StructInstType( actorDecl ) ) ) );
|
---|
| 354 | genericReceive->params.push_back( new PointerType( new PointerType( new StructInstType( msgDecl ) ) ) );
|
---|
| 355 | genericReceive->returns.push_back( new EnumInstType( allocationDecl ) );
|
---|
[fc1a3e2] | 356 |
|
---|
| 357 | // Generates: allocation (*fn)( actor &, message & ) = (allocation (*)( actor &, message & ))my_work_fn;
|
---|
| 358 | // More readable synonymous code:
|
---|
| 359 | // typedef allocation (*__receive_fn)(actor &, message &);
|
---|
| 360 | // __receive_fn fn = (__receive_fn)my_work_fn;
|
---|
| 361 | sendBody->push_back( new DeclStmt(
|
---|
| 362 | decl->location,
|
---|
| 363 | new ObjectDecl(
|
---|
| 364 | decl->location,
|
---|
| 365 | "fn",
|
---|
| 366 | new PointerType( genericReceive ),
|
---|
| 367 | new SingleInit( decl->location,
|
---|
| 368 | new CastExpr( decl->location, new NameExpr( decl->location, "my_work_fn" ), new PointerType( genericReceive ), ExplicitCast )
|
---|
| 369 | )
|
---|
| 370 | )
|
---|
| 371 | ));
|
---|
| 372 |
|
---|
| 373 | // Generates: new_req{ (actor *)&receiver, (message *)&msg, fn };
|
---|
| 374 | sendBody->push_back( new ExprStmt(
|
---|
| 375 | decl->location,
|
---|
[a64137f] | 376 | new UntypedExpr (
|
---|
[fc1a3e2] | 377 | decl->location,
|
---|
[a64137f] | 378 | new NameExpr( decl->location, "?{}" ),
|
---|
| 379 | {
|
---|
| 380 | new NameExpr( decl->location, "new_req" ),
|
---|
[1ee74df] | 381 | new CastExpr( decl->location, new AddressExpr( new NameExpr( decl->location, "receiver" ) ), new PointerType( new StructInstType( actorDecl ) ), ExplicitCast ),
|
---|
| 382 | new CastExpr( decl->location, new AddressExpr( new NameExpr( decl->location, "msg" ) ), new PointerType( new StructInstType( msgDecl ) ), ExplicitCast ),
|
---|
[fc1a3e2] | 383 | new NameExpr( decl->location, "fn" )
|
---|
[a64137f] | 384 | }
|
---|
| 385 | )
|
---|
| 386 | ));
|
---|
| 387 |
|
---|
[fc1a3e2] | 388 | // Generates: send( receiver, new_req );
|
---|
| 389 | sendBody->push_back( new ExprStmt(
|
---|
| 390 | decl->location,
|
---|
[a64137f] | 391 | new UntypedExpr (
|
---|
[fc1a3e2] | 392 | decl->location,
|
---|
[a64137f] | 393 | new NameExpr( decl->location, "send" ),
|
---|
| 394 | {
|
---|
| 395 | {
|
---|
[fc1a3e2] | 396 | new NameExpr( decl->location, "receiver" ),
|
---|
| 397 | new NameExpr( decl->location, "new_req" )
|
---|
| 398 | }
|
---|
[a64137f] | 399 | }
|
---|
| 400 | )
|
---|
| 401 | ));
|
---|
[37273c8] | 402 |
|
---|
[ab94c37] | 403 | // Generates: &receiver_ret = &receiver; return receiver_ret;
|
---|
| 404 | // Note: This is the general pattern used as a "return convention".
|
---|
| 405 | sendBody->push_back( new ExprStmt( decl->location,
|
---|
| 406 | ast::UntypedExpr::createAssign( decl->location,
|
---|
| 407 | new AddressExpr( new NameExpr( decl->location, "receiver_ret" ) ),
|
---|
| 408 | new AddressExpr( new NameExpr( decl->location, "receiver" ) )
|
---|
| 409 | )
|
---|
| 410 | ) );
|
---|
| 411 | sendBody->push_back( new ReturnStmt( decl->location,
|
---|
| 412 | new NameExpr( decl->location, "receiver_ret" ) ) );
|
---|
[fc1a3e2] | 413 |
|
---|
| 414 | // put it all together into the complete function decl from above
|
---|
| 415 | FunctionDecl * sendOperatorFunction = new FunctionDecl(
|
---|
| 416 | decl->location,
|
---|
| 417 | "?|?",
|
---|
| 418 | {
|
---|
| 419 | new ObjectDecl(
|
---|
| 420 | decl->location,
|
---|
| 421 | "receiver",
|
---|
| 422 | ast::deepCopy( derivedActorRef )
|
---|
| 423 | ),
|
---|
| 424 | new ObjectDecl(
|
---|
| 425 | decl->location,
|
---|
| 426 | "msg",
|
---|
| 427 | ast::deepCopy( derivedMsgRef )
|
---|
| 428 | )
|
---|
| 429 | }, // params
|
---|
| 430 | {
|
---|
| 431 | new ObjectDecl(
|
---|
| 432 | decl->location,
|
---|
| 433 | "receiver_ret",
|
---|
| 434 | ast::deepCopy( derivedActorRef )
|
---|
| 435 | )
|
---|
| 436 | },
|
---|
| 437 | nullptr, // body
|
---|
| 438 | { Storage::Static }, // storage
|
---|
| 439 | Linkage::Cforall, // linkage
|
---|
| 440 | {}, // attributes
|
---|
| 441 | { Function::Inline }
|
---|
| 442 | );
|
---|
| 443 |
|
---|
| 444 | // forward decls to resolve use before decl problem for '|' routines
|
---|
| 445 | forwardDecls.insertDecl( *actorIter, *messageIter , ast::deepCopy( sendOperatorFunction ) );
|
---|
| 446 |
|
---|
| 447 | sendOperatorFunction->stmts = sendBody;
|
---|
| 448 | declsToAddAfter.push_back( sendOperatorFunction );
|
---|
| 449 | }
|
---|
[3dd8f42] | 450 | }
|
---|
| 451 |
|
---|
| 452 | public:
|
---|
[fc1a3e2] | 453 | GenFuncsCreateTables( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
---|
[1ee74df] | 454 | const StructDecl *& requestDecl, const EnumDecl *& allocationDecl, const StructDecl *& actorDecl, const StructDecl *& msgDecl,
|
---|
[fc1a3e2] | 455 | FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
|
---|
| 456 | requestDecl(requestDecl), allocationDecl(allocationDecl), actorDecl(actorDecl), msgDecl(msgDecl), forwardDecls(forwardDecls) {}
|
---|
[34ed17b] | 457 | };
|
---|
| 458 |
|
---|
[3830c84] | 459 |
|
---|
| 460 | // separate pass is needed since this pass resolves circular defn issues
|
---|
| 461 | // generates the forward declarations of the send operator for actor routines
|
---|
[ed96731] | 462 | struct FwdDeclOperator : public ast::WithDeclsToAdd {
|
---|
[fc1a3e2] | 463 | unordered_set<const StructDecl *> & actorStructDecls;
|
---|
[1ee74df] | 464 | unordered_set<const StructDecl *> & messageStructDecls;
|
---|
[fc1a3e2] | 465 | FwdDeclTable & forwardDecls;
|
---|
| 466 |
|
---|
| 467 | // handles forward declaring the message operator
|
---|
| 468 | void postvisit( const StructDecl * decl ) {
|
---|
| 469 | list<FunctionDecl *> toAddAfter;
|
---|
| 470 | auto actorIter = actorStructDecls.find( decl );
|
---|
| 471 | if ( actorIter != actorStructDecls.end() ) { // this is a derived actor decl
|
---|
| 472 | // get list of fwd decls that we can now insert
|
---|
| 473 | toAddAfter = forwardDecls.updateDecl( decl, false );
|
---|
| 474 |
|
---|
| 475 | // get rid of decl from actorStructDecls since we no longer need it
|
---|
| 476 | actorStructDecls.erase( actorIter );
|
---|
| 477 | } else {
|
---|
| 478 | auto messageIter = messageStructDecls.find( decl );
|
---|
| 479 | if ( messageIter == messageStructDecls.end() ) return;
|
---|
| 480 |
|
---|
| 481 | toAddAfter = forwardDecls.updateDecl( decl, true );
|
---|
| 482 |
|
---|
| 483 | // get rid of decl from messageStructDecls since we no longer need it
|
---|
| 484 | messageStructDecls.erase( messageIter );
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 | // add the fwd decls to declsToAddAfter
|
---|
| 488 | for ( FunctionDecl * func : toAddAfter ) {
|
---|
| 489 | declsToAddAfter.push_back( func );
|
---|
| 490 | }
|
---|
| 491 | }
|
---|
[34ed17b] | 492 |
|
---|
| 493 | public:
|
---|
[fc1a3e2] | 494 | FwdDeclOperator( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
---|
| 495 | FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls), forwardDecls(forwardDecls) {}
|
---|
[3dd8f42] | 496 | };
|
---|
| 497 |
|
---|
| 498 | void implementActors( TranslationUnit & translationUnit ) {
|
---|
[fc1a3e2] | 499 | // unordered_maps to collect all derived actor and message types
|
---|
| 500 | unordered_set<const StructDecl *> actorStructDecls;
|
---|
| 501 | unordered_set<const StructDecl *> messageStructDecls;
|
---|
| 502 | FwdDeclTable forwardDecls;
|
---|
| 503 |
|
---|
| 504 | // for storing through the passes
|
---|
| 505 | // these are populated with various important struct decls
|
---|
[1ee74df] | 506 | const StructDecl * requestDecl = nullptr;
|
---|
| 507 | const EnumDecl * allocationDecl = nullptr;
|
---|
| 508 | const StructDecl * actorDecl = nullptr;
|
---|
| 509 | const StructDecl * msgDecl = nullptr;
|
---|
[fc1a3e2] | 510 |
|
---|
| 511 | // first pass collects ptrs to allocation enum, request type, and generic receive fn typedef
|
---|
| 512 | // also populates maps of all derived actors and messages
|
---|
[1ee74df] | 513 | Pass<CollectactorStructDecls>::run( translationUnit, actorStructDecls, messageStructDecls,
|
---|
| 514 | requestDecl, allocationDecl, actorDecl, msgDecl );
|
---|
[fc1a3e2] | 515 |
|
---|
| 516 | // check that we have found all the decls we need from <actor.hfa>, if not no need to run the rest of this pass
|
---|
[1ee74df] | 517 | if ( !allocationDecl || !requestDecl || !actorDecl || !msgDecl )
|
---|
[fc1a3e2] | 518 | return;
|
---|
| 519 |
|
---|
| 520 | // second pass locates all receive() routines that overload the generic receive fn
|
---|
| 521 | // it then generates the appropriate operator '|' send routines for the receive routines
|
---|
[1ee74df] | 522 | Pass<GenFuncsCreateTables>::run( translationUnit, actorStructDecls, messageStructDecls,
|
---|
| 523 | requestDecl, allocationDecl, actorDecl, msgDecl, forwardDecls );
|
---|
[fc1a3e2] | 524 |
|
---|
| 525 | // The third pass forward declares operator '|' send routines
|
---|
| 526 | Pass<FwdDeclOperator>::run( translationUnit, actorStructDecls, messageStructDecls, forwardDecls );
|
---|
[3dd8f42] | 527 | }
|
---|
| 528 |
|
---|
| 529 | } // namespace Concurrency
|
---|
| 530 |
|
---|
| 531 | // Local Variables: //
|
---|
| 532 | // tab-width: 4 //
|
---|
| 533 | // mode: c++ //
|
---|
| 534 | // compile-command: "make install" //
|
---|
| 535 | // End: //
|
---|
| 536 |
|
---|