source: src/Concurrency/Corun.cpp @ eb779d5

Last change on this file since eb779d5 was eb779d5, checked in by caparsons <caparson@…>, 9 months ago

Implemented corun statement

  • Property mode set to 100644
File size: 2.9 KB
Line 
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// Corun.cpp -- generate code needed by the actor system
8//
9// Author           : Colby Parsons
10// Created On       : Monday October 9 15:16:42 2023
11// Last Modified By : Colby Parsons
12// Last Modified On : Monday October 9 15:16:42 2023
13// Update Count     : 0
14//
15
16#include "AST/Decl.hpp"
17#include "AST/Expr.hpp"
18#include "AST/Pass.hpp"
19#include "AST/Stmt.hpp"
20#include "AST/TranslationUnit.hpp"
21#include "Common/UniqueName.h"
22using namespace ast;
23using namespace std;
24
25namespace Concurrency {
26
27struct CorunKeyword : public WithDeclsToAdd<>, public WithStmtsToAdd<> {
28    UniqueName CorunFnNamer = "__CFA_corun_lambda_"s;
29    UniqueName RunnerBlockNamer = "__CFA_corun_block_"s;
30
31    const StructDecl * runnerBlockDecl = nullptr;
32
33    // Finds select_node decl
34    void previsit( const StructDecl * decl ) {
35        if ( !decl->body ) {
36            return;
37        } else if ( "runner_block" == decl->name ) {
38            assert( !runnerBlockDecl );
39            runnerBlockDecl = decl;
40        }
41    }
42
43    Stmt * postvisit( const CorunStmt * stmt ) {
44        assert( runnerBlockDecl );
45        if ( !stmt->stmt )
46            return nullptr;
47       
48        const CodeLocation & loc = stmt->location;
49        const string fnName = CorunFnNamer.newName();
50        const string objName = RunnerBlockNamer.newName();
51
52        // Generates:
53        // void __CFA_corun_lambda_() { ... stmt->stmt ... }
54        Stmt * runnerLambda = new DeclStmt( loc,
55            new FunctionDecl( loc,
56                fnName,                                             // name
57                {},                                                 // forall
58                {},                                                 // params
59                {},                                                 // return
60                new CompoundStmt( loc, { deepCopy(stmt->stmt) } )   // body
61            )
62        );
63
64        // Generates:
65        // runner_block __CFA_corun_block_;
66        Stmt * objDecl = new DeclStmt( loc,
67            new ObjectDecl( loc,
68                objName,
69                new StructInstType( runnerBlockDecl )
70            )
71        );
72
73        // Generates:
74        // __CFA_corun_block_{ __CFA_corun_lambda_ };
75        Stmt * threadStart = new ExprStmt( loc,
76            new UntypedExpr ( loc, 
77                new NameExpr( loc, "?{}" ),
78                {
79                    new NameExpr( loc, objName ),
80                    new NameExpr( loc, fnName )
81                }
82            )
83        );
84
85        stmtsToAddBefore.push_back( runnerLambda );
86        stmtsToAddBefore.push_back( objDecl );
87
88        return threadStart;
89    }
90};
91
92void implementCorun( TranslationUnit & translationUnit ) {
93    Pass<CorunKeyword>::run( translationUnit );
94}
95
96} // namespace Concurrency
Note: See TracBrowser for help on using the repository browser.