aboutsummaryrefslogtreecommitdiffstats
path: root/ast.cpp
blob: e3a3e68f040eba9c249d4209fb418ad7adab59bc (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * lisp-compiler: Compiles LISP using LLVM.
 * Copyright (C) 2022  Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @file ast.cpp
 * @brief Abstract Syntax Tree (AST) implementation.
 */

#include "ast.hpp"
#include "state.hpp"

#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Type.h"

#include <algorithm>
#include <any>
#include <cctype>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <optional>
#include <string>
#include <typeinfo>
#include <variant>
#include <vector>

AST::Value AST::Identifier::codegen(CompilerState& state)
{
    if (state.namedValues.contains(name))
        return state.namedValues[name];
    else
        return nullptr;
}

AST::Value AST::Literal::codegen(CompilerState& state)
{
    if (type == AST::Literal::Type::Number) {
        if (std::holds_alternative<int>(value))
            return llvm::ConstantInt::get(state.context, llvm::APInt(sizeof(int) * 8, std::get<int>(value)));
        else
            return llvm::ConstantFP::get(state.context, llvm::APFloat(std::get<double>(value)));
    } else {
        return nullptr;
    }
}

AST::Value AST::ProcedureCall::codegen(CompilerState& state)
{
    if (auto id = dynamic_cast<AST::Identifier *>(callee); id) {
        if (state.namedValues.contains(id->name)) {
            auto ptrToFuncPtr = state.namedValues[id->name];
            auto funcPtr = state.builder.CreateLoad(
                ptrToFuncPtr->getType()->getContainedType(0),
                ptrToFuncPtr);
            auto funcType = (llvm::FunctionType *)funcPtr->getType()->getContainedType(0);
            return state.builder.CreateCall(funcType, funcPtr, llvm::None, "calltmp");
        } else {
            std::vector<llvm::Value *> args;
            std::vector<llvm::Type *> argtypes;

            for (auto& a : operands) {
                auto gen = a->codegen(state);
                args.push_back(gen);
                argtypes.push_back(gen ? gen->getType() : llvm::Type::getVoidTy(state.context));
            }


            auto func = state.module.getOrInsertFunction(id->name,
                llvm::FunctionType::get(
                    llvm::Type::getVoidTy(state.context),
                    argtypes,
                    false));
            return state.builder.CreateCall(func, args);
        }

        // work off of id's name
        // builtin?
        // named value? (i.e. is defined)
        return nullptr;
    } else if (auto v = callee->codegen(state); v) {
        // v needs to be a callable procedure
        //std::vector<Node *> operands;
        return nullptr;
    } else {
        return nullptr;
    }
}

AST::Value AST::LambdaExpression::codegen(CompilerState& state)
{
    std::vector<llvm::Type *> args;
    std::vector<std::string> argnames;

    for (auto& op : operands) {
        args.push_back(llvm::Type::getDoubleTy(state.context));
        argnames.push_back(op->name);
    }

    auto ftype = llvm::FunctionType::get(
        llvm::Type::getDoubleTy(state.context), args, false);
    auto func = llvm::Function::Create(
        ftype, llvm::Function::ExternalLinkage, "lambda", &state.module);

    auto n = argnames.cbegin();
    for (auto& a : func->args()) {
        a.setName(*n);
        state.namedValues[*n] = &a;
        ++n;
    }

    auto block = llvm::BasicBlock::Create(state.context, "entry", func);

    auto ip = state.builder.saveIP();
    state.builder.SetInsertPoint(block);
    ++state.scope;

    llvm::Value *ret;
    for (auto& b : body)
        ret = b->codegen(state);

    if (ret)
        state.builder.CreateRet(ret);
    else
        state.builder.CreateRetVoid();

    --state.scope;
    state.builder.restoreIP(ip);

    for (auto& a : argnames)
        state.namedValues.erase(a);

    return func;
}

AST::Value AST::Conditional::codegen(CompilerState& state)
{
    auto cval = state.builder.CreateFCmpONE(
        cond->codegen(state),
        llvm::ConstantFP::get(state.context, llvm::APFloat(0.0)),
        "ifcond");

    auto func = state.builder.GetInsertBlock()->getParent();

    auto bthen = llvm::BasicBlock::Create(state.context, "then", func);
    auto belse = llvm::BasicBlock::Create(state.context, "else", func);
    auto bcont = llvm::BasicBlock::Create(state.context, "cont", func);
    state.builder.CreateCondBr(cval, bthen, belse);

    state.builder.SetInsertPoint(bthen);
    auto vthen = iftrue->codegen(state);
    if (!vthen || vthen->getType() != llvm::Type::getDoubleTy(state.context))
        vthen = llvm::ConstantFP::get(state.context, llvm::APFloat(0.0));
    state.builder.CreateBr(bcont);

    state.builder.SetInsertPoint(belse);
    auto velse = iffalse->codegen(state);
    if (!velse || velse->getType() != llvm::Type::getDoubleTy(state.context))
        velse = llvm::ConstantFP::get(state.context, llvm::APFloat(0.0));
    state.builder.CreateBr(bcont);

    state.builder.SetInsertPoint(bcont);
    auto PN = state.builder.CreatePHI(
        llvm::Type::getDoubleTy(state.context), 2, "iftmp");

    PN->addIncoming(vthen, bthen);
    PN->addIncoming(velse, belse);
    return PN;
}

AST::Value AST::Definition::codegen(CompilerState& state)
{
    if (!state.namedValues.contains(ident->name)) {
        if (state.scope == 0) {
            auto val = (llvm::Constant *)value->codegen(state);

            state.module.getOrInsertGlobal(ident->name, val->getType());

            auto var = state.module.getNamedGlobal(ident->name);
            var->setLinkage(llvm::Function::ExternalLinkage);
            var->setInitializer(val);
            state.namedValues[ident->name] = var;
            return var;
        } else {
            auto alloc = state.builder.CreateAlloca(
                llvm::Type::getDoubleTy(state.context),
                nullptr,
                ident->name);
            state.builder.CreateStore(value->codegen(state), alloc);
            state.namedValues[ident->name] = alloc;
            return alloc;
        }
    } else {
        return nullptr;
    }
}

AST::Value AST::Assignment::codegen(CompilerState& state)
{
    if (state.scope > 0) {
        if (state.namedValues.contains(ident->name)) {
            return state.builder.CreateStore(value->codegen(state), state.namedValues[ident->name]);
        }
    }

    return nullptr;
}