aboutsummaryrefslogtreecommitdiffstats
path: root/llvm.cpp
blob: 51d47061631fe3277d3ee0cf739d8c20ad5c96b4 (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
/**
 * forspll - LLVM-based Forsp compiler
 * Copyright (C) 2024  Clyne Sullivan <clyne@bitgloo.com>
 *
 * 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/>.
 */
#include "llvm.hpp"

LLVMState::LLVMState():
    ctx(),
    modul("forsp", ctx),
    builder(ctx),
    inttype(llvm::Type::getInt32Ty(ctx)),
    stacktype(llvm::VectorType::get(inttype, 16, false)),
    ftype(llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), llvm::ArrayRef<llvm::Type *> {inttype->getPointerTo()}, false)),
    one(llvm::ConstantInt::get(inttype, 1)),
    zero(llvm::ConstantInt::get(inttype, 0))
{
    auto zerovec = llvm::ConstantVector::get(llvm::ArrayRef(zero));
    llvmSp = new llvm::GlobalVariable(modul, inttype, false,
        llvm::GlobalValue::ExternalLinkage, zero, "sp");
    llvmStack = new llvm::GlobalVariable(modul, stacktype, false,
        llvm::GlobalValue::ExternalLinkage, zerovec, "stack");
}

llvm::Value *LLVMState::createPush(llvm::Value *var)
{
    auto dspval = builder.CreateLoad(inttype, llvmSp);
    auto inc = builder.CreateAdd(dspval, one);
    builder.CreateStore(inc, llvmSp, false);

    auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dspval});
    return builder.CreateStore(var, gep);
}

llvm::Value *LLVMState::createPop()
{
    auto dspval = builder.CreateLoad(inttype, llvmSp);
    auto dec = builder.CreateSub(dspval, one);
    builder.CreateStore(dec, llvmSp, false);

    auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dec});
    return builder.CreateLoad(inttype, gep);
}

llvm::Function *LLVMState::createFunction(const std::string& name)
{
    auto func = llvm::Function::Create(ftype, llvm::Function::ExternalLinkage,
        name.c_str(), modul);
    func->getArg(0)->setName("penv");
    return func;
}

llvm::BasicBlock *LLVMState::createEntry(llvm::Function *func)
{
    return llvm::BasicBlock::Create(ctx, "entry", func);
}

llvm::Value *LLVMState::createVariable(const std::string& name)
{
    return new llvm::GlobalVariable(modul, inttype, false,
        llvm::GlobalValue::InternalLinkage, zero, name);
}

llvm::Constant *LLVMState::createInt(int n)
{
    return llvm::ConstantInt::get(ctx, llvm::APInt(32, n, true));
}

void LLVMState::output()
{
    modul.print(llvm::outs(), nullptr);
}