| 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"
|
|---|
| 23 | #include <algorithm>
|
|---|
| 24 | using namespace ast;
|
|---|
| 25 | using namespace std;
|
|---|
| 26 |
|
|---|
| 27 | namespace Concurrency {
|
|---|
| 28 |
|
|---|
| 29 | struct CollectactorStructDecls : public ast::WithGuards {
|
|---|
| 30 | unordered_set<const StructDecl *> & actorStructDecls;
|
|---|
| 31 | unordered_set<const StructDecl *> & messageStructDecls;
|
|---|
| 32 | const StructDecl *& requestDecl;
|
|---|
| 33 | const EnumDecl *& allocationDecl;
|
|---|
| 34 | const StructDecl *& actorDecl;
|
|---|
| 35 | const StructDecl *& msgDecl;
|
|---|
| 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 ) {
|
|---|
| 42 | if( decl->name == "allocation" ) allocationDecl = decl;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 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
|
|---|
| 50 | actorDecl = decl;
|
|---|
| 51 | } else if( decl->name == "message" ) {
|
|---|
| 52 | messageStructDecls.insert( decl ); // skip inserting fwd decl
|
|---|
| 53 | msgDecl = decl;
|
|---|
| 54 | } else if( decl->name == "request" ) {
|
|---|
| 55 | requestDecl = decl;
|
|---|
| 56 | } else {
|
|---|
| 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 ) {
|
|---|
| 76 | if ( !actorDecl || !msgDecl ) return;
|
|---|
| 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 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public:
|
|---|
| 91 | CollectactorStructDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
|---|
| 92 | const StructDecl *& requestDecl, const EnumDecl *& allocationDecl, const StructDecl *& actorDecl, const StructDecl *& msgDecl )
|
|---|
| 93 | : actorStructDecls( actorStructDecls ), messageStructDecls( messageStructDecls ), requestDecl( requestDecl ),
|
|---|
| 94 | allocationDecl( allocationDecl ), actorDecl(actorDecl), msgDecl(msgDecl) {}
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 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 |
|
|---|
| 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 | }
|
|---|
| 132 |
|
|---|
| 133 | public:
|
|---|
| 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 | }
|
|---|
| 192 | };
|
|---|
| 193 |
|
|---|
| 194 | // generates the definitions of send operators for actors
|
|---|
| 195 | // collects data needed for next pass that does the circular defn resolution
|
|---|
| 196 | // for message send operators (via table above)
|
|---|
| 197 | struct GenFuncsCreateTables : public ast::WithDeclsToAdd {
|
|---|
| 198 | unordered_set<const StructDecl *> & actorStructDecls;
|
|---|
| 199 | unordered_set<const StructDecl *> & messageStructDecls;
|
|---|
| 200 | const StructDecl *& requestDecl;
|
|---|
| 201 | const EnumDecl *& allocationDecl;
|
|---|
| 202 | const StructDecl *& actorDecl;
|
|---|
| 203 | const StructDecl *& msgDecl;
|
|---|
| 204 | FwdDeclTable & forwardDecls;
|
|---|
| 205 |
|
|---|
| 206 | // generates the operator for actor message sends
|
|---|
| 207 | void postvisit( const FunctionDecl * decl ) {
|
|---|
| 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",
|
|---|
| 282 | new PointerType( new PointerType( new StructInstType( actorDecl ) ) )
|
|---|
| 283 | ),
|
|---|
| 284 | new ObjectDecl(
|
|---|
| 285 | decl->location,
|
|---|
| 286 | "base_msg",
|
|---|
| 287 | new PointerType( new PointerType( new StructInstType( msgDecl ) ) )
|
|---|
| 288 | )
|
|---|
| 289 | }, // params
|
|---|
| 290 | {
|
|---|
| 291 | new ObjectDecl(
|
|---|
| 292 | decl->location,
|
|---|
| 293 | "__CFA_receive_wrap_ret",
|
|---|
| 294 | new EnumInstType( allocationDecl )
|
|---|
| 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",
|
|---|
| 326 | new StructInstType( requestDecl )
|
|---|
| 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 ) );
|
|---|
| 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 ) );
|
|---|
| 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();
|
|---|
| 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 ) );
|
|---|
| 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,
|
|---|
| 376 | new UntypedExpr (
|
|---|
| 377 | decl->location,
|
|---|
| 378 | new NameExpr( decl->location, "?{}" ),
|
|---|
| 379 | {
|
|---|
| 380 | new NameExpr( decl->location, "new_req" ),
|
|---|
| 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 ),
|
|---|
| 383 | new NameExpr( decl->location, "fn" )
|
|---|
| 384 | }
|
|---|
| 385 | )
|
|---|
| 386 | ));
|
|---|
| 387 |
|
|---|
| 388 | // Generates: send( receiver, new_req );
|
|---|
| 389 | sendBody->push_back( new ExprStmt(
|
|---|
| 390 | decl->location,
|
|---|
| 391 | new UntypedExpr (
|
|---|
| 392 | decl->location,
|
|---|
| 393 | new NameExpr( decl->location, "send" ),
|
|---|
| 394 | {
|
|---|
| 395 | {
|
|---|
| 396 | new NameExpr( decl->location, "receiver" ),
|
|---|
| 397 | new NameExpr( decl->location, "new_req" )
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 | )
|
|---|
| 401 | ));
|
|---|
| 402 |
|
|---|
| 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" ) ) );
|
|---|
| 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 | }
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | public:
|
|---|
| 453 | GenFuncsCreateTables( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
|---|
| 454 | const StructDecl *& requestDecl, const EnumDecl *& allocationDecl, const StructDecl *& actorDecl, const StructDecl *& msgDecl,
|
|---|
| 455 | FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
|
|---|
| 456 | requestDecl(requestDecl), allocationDecl(allocationDecl), actorDecl(actorDecl), msgDecl(msgDecl), forwardDecls(forwardDecls) {}
|
|---|
| 457 | };
|
|---|
| 458 |
|
|---|
| 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
|
|---|
| 462 | struct FwdDeclOperator : public ast::WithDeclsToAdd {
|
|---|
| 463 | unordered_set<const StructDecl *> & actorStructDecls;
|
|---|
| 464 | unordered_set<const StructDecl *> & messageStructDecls;
|
|---|
| 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 | }
|
|---|
| 492 |
|
|---|
| 493 | public:
|
|---|
| 494 | FwdDeclOperator( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
|
|---|
| 495 | FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls), forwardDecls(forwardDecls) {}
|
|---|
| 496 | };
|
|---|
| 497 |
|
|---|
| 498 | void implementActors( TranslationUnit & translationUnit ) {
|
|---|
| 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
|
|---|
| 506 | const StructDecl * requestDecl = nullptr;
|
|---|
| 507 | const EnumDecl * allocationDecl = nullptr;
|
|---|
| 508 | const StructDecl * actorDecl = nullptr;
|
|---|
| 509 | const StructDecl * msgDecl = nullptr;
|
|---|
| 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
|
|---|
| 513 | Pass<CollectactorStructDecls>::run( translationUnit, actorStructDecls, messageStructDecls,
|
|---|
| 514 | requestDecl, allocationDecl, actorDecl, msgDecl );
|
|---|
| 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
|
|---|
| 517 | if ( !allocationDecl || !requestDecl || !actorDecl || !msgDecl )
|
|---|
| 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
|
|---|
| 522 | Pass<GenFuncsCreateTables>::run( translationUnit, actorStructDecls, messageStructDecls,
|
|---|
| 523 | requestDecl, allocationDecl, actorDecl, msgDecl, forwardDecls );
|
|---|
| 524 |
|
|---|
| 525 | // The third pass forward declares operator '|' send routines
|
|---|
| 526 | Pass<FwdDeclOperator>::run( translationUnit, actorStructDecls, messageStructDecls, forwardDecls );
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | } // namespace Concurrency
|
|---|
| 530 |
|
|---|
| 531 | // Local Variables: //
|
|---|
| 532 | // tab-width: 4 //
|
|---|
| 533 | // mode: c++ //
|
|---|
| 534 | // compile-command: "make install" //
|
|---|
| 535 | // End: //
|
|---|
| 536 |
|
|---|