source: src/Concurrency/Corun.cpp @ 8cbe732

Last change on this file since 8cbe732 was cf3da24, checked in by Andrew Beach <ajbeach@…>, 13 months ago

Fixed up some whitespace. Pretty minor stuff mostly.

  • Property mode set to 100644
File size: 3.0 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        if ( !runnerBlockDecl )
45            SemanticError( stmt->location, "To use corun statements add #include <cofor.hfa>\n" );
46
47        if ( !stmt->stmt )
48            return nullptr;
49
50        const CodeLocation & loc = stmt->location;
51        const string fnName = CorunFnNamer.newName();
52        const string objName = RunnerBlockNamer.newName();
53
54        // Generates:
55        // void __CFA_corun_lambda_() { ... stmt->stmt ... }
56        Stmt * runnerLambda = new DeclStmt( loc,
57            new FunctionDecl( loc,
58                fnName,                                             // name
59                {},                                                 // forall
60                {},                                                 // params
61                {},                                                 // return
62                new CompoundStmt( loc, { deepCopy(stmt->stmt) } )   // body
63            )
64        );
65
66        // Generates:
67        // runner_block __CFA_corun_block_;
68        Stmt * objDecl = new DeclStmt( loc,
69            new ObjectDecl( loc,
70                objName,
71                new StructInstType( runnerBlockDecl )
72            )
73        );
74
75        // Generates:
76        // __CFA_corun_block_{ __CFA_corun_lambda_ };
77        Stmt * threadStart = new ExprStmt( loc,
78            new UntypedExpr ( loc,
79                new NameExpr( loc, "?{}" ),
80                {
81                    new NameExpr( loc, objName ),
82                    new NameExpr( loc, fnName )
83                }
84            )
85        );
86
87        stmtsToAddBefore.push_back( runnerLambda );
88        stmtsToAddBefore.push_back( objDecl );
89
90        return threadStart;
91    }
92};
93
94void implementCorun( TranslationUnit & translationUnit ) {
95    Pass<CorunKeyword>::run( translationUnit );
96}
97
98} // namespace Concurrency
Note: See TracBrowser for help on using the repository browser.