aboutsummaryrefslogtreecommitdiff
path: root/include/cps/FunctionGen.h
blob: eb4a839a67274b8f6f21f3af44fea538273a0b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#pragma once
#include "cps/Identifier.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "cps/FunctionAST.h"
#include "cps/TypeSystem.h"
#include <cstdint>
#include <map>
#include <utility>
#include <vector>
#include <functional>
#include <string>

namespace cps {

class ExceptionHandler;

// Array-parameter ABI: each rank-R array parameter is flattened into the
// data pointer followed by kBoundArgsPerDim i64 bounds per dimension.
inline constexpr unsigned kBoundArgsPerDim = 2; // lower, upper
inline constexpr const char *kLowerBoundSuffix = ".lb";
inline constexpr const char *kUpperBoundSuffix = ".ub";

// Pseudocode-level signature of one FUNCTION/PROCEDURE parameter; for arrays
// TypeName is the ELEMENT type and DeclaredBounds mirrors ParamDecl.
struct ParamSig {
    std::string TypeName;
    bool IsByRef = false;
    bool IsArray = false;
    int Rank = 0;
    std::vector<std::pair<int64_t, int64_t>> DeclaredBounds;
};

// Pseudocode-level signature of a FUNCTION/PROCEDURE, kept so call sites can
// dispatch on declared parameter types and modes instead of inferring them
// back from LLVM types.
struct FuncSig {
    std::string Name;
    std::string ReturnTypeName;
    std::vector<ParamSig> Params;
    size_t OwnerUnit = 0;
};

class FunctionGen {
public:
    // Injected by CodeGen (StmtEmitter inversion precedent) so array
    // parameters can be bound into ArrayHandler without FunctionGen ever
    // seeing it: (param, incoming data ptr, per-dim lower/upper bounds).
    using ArrayParamBinder =
        std::function<void(const ParamDecl &, llvm::Value *,
                           const std::vector<llvm::Value*> &,
                           const std::vector<llvm::Value*> &)>;

private:
    llvm::LLVMContext &Context;
    llvm::Module &Module;
    llvm::IRBuilder<> &Builder;
    TypeSystem &Types;
    ExceptionHandler &Exceptions;

    IdentifierMap<SymbolInfo> &Symbols;
    bool &HadError;
    int &DiagnosticLine;
    void reportError(const char *Fmt, ...);

    IdentifierMap<FuncSig> Signatures;
    size_t CurrentUnit = 0;
    std::function<bool(size_t)> Visible;
    ArrayParamBinder BindArrayParam;
    std::function<void(llvm::Value *, const std::string &, int)> RegisterCopy;

    void createArgumentAllocas(llvm::Function *F, const std::vector<ParamDecl> &Params, bool IsMethod);

public:
    FunctionGen(llvm::LLVMContext &C,
                llvm::Module &M,
                llvm::IRBuilder<> &B,
                TypeSystem &TS,
                ExceptionHandler &EH,
                IdentifierMap<SymbolInfo> &Sym,
                bool &HadErrorFlag, int &Line)
        : Context(C), Module(M), Builder(B), Types(TS), Exceptions(EH), Symbols(Sym), HadError(HadErrorFlag), DiagnosticLine(Line) {}

    llvm::Type *getLLVMType(const std::string &TypeName);

    void setArrayParamBinder(ArrayParamBinder Binder) { BindArrayParam = std::move(Binder); }

    void setCopyRegistrar(std::function<void(llvm::Value *, const std::string &, int)> Registrar) {
        RegisterCopy = std::move(Registrar);
    }

    void setCurrentUnit(size_t Unit) { CurrentUnit = Unit; }
    void setVisibility(std::function<bool(size_t)> Predicate) { Visible = std::move(Predicate); }
    const FuncSig *getSignature(const std::string &Name) const;

    llvm::Function *emitPrototype(PrototypeAST *Proto);

    llvm::Function *emitFunctionDef(FunctionDefAST *FuncAST, const std::function<void(StmtAST*)> &StmtEmitter, unsigned ScopeID);

    llvm::Value *emitCall(const std::string &Callee, const std::vector<llvm::Value*> &Args);

    void emitReturn(ReturnStmtAST *Ret, llvm::Value *RetVal);
};

}