Skip to content

Commit

Permalink
fixed some things about strings and more
Browse files Browse the repository at this point in the history
  • Loading branch information
lehlud committed Jun 2, 2021
1 parent f05d894 commit 9955946
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 69 deletions.
10 changes: 6 additions & 4 deletions examples/second.adscript
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(defn puts [char* s] int)

(defn setHW [char** s] int
(setptr s "Hello World")
42)

(defn main [int a] i8
(var hw "41")
(set (hw 1) \2)
(puts hw)
(setptr (ref hw) "Hello World")
(var hw "42");;#[\4\20])
(setHW (ref hw))
(puts hw))
44 changes: 24 additions & 20 deletions src/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,31 @@ llvm::Value* AST::Identifier::llvmValue(Compiler::Context& ctx) {
}

llvm::Value* AST::String::llvmValue(Compiler::Context& ctx) {
auto elementT = Utils::isAscii(val)
auto charT = Utils::isAscii(val)
? llvm::Type::getInt8Ty(ctx.mod->getContext())
: llvm::Type::getInt32Ty(ctx.mod->getContext());

auto arrayT = llvm::ArrayType::get(elementT, val.size() + 1);

auto array = (llvm::Value*) ctx.builder->CreateAlloca(arrayT);

auto zero = Compiler::constInt(ctx, 0);
array = ctx.builder->CreateGEP(array, { zero, zero });

for (size_t i = 0; i < val.size(); i++) {
auto idx = Compiler::constInt(ctx, i);
auto llvmVal = llvm::ConstantInt::get(elementT, val[i]);
// create llvm value vector for array elements
std::vector<llvm::Constant*> chars;

auto ptr = ctx.builder->CreateGEP(array, idx);
// add characters to the 'elements' vector
for (auto& c : val) chars.push_back(llvm::ConstantInt::get(charT, c));

// add NULL terminator to chars
chars.push_back(llvm::ConstantInt::get(charT, 0));

ctx.builder->CreateStore(llvmVal, ptr);
}
// create array type using the element type
auto arrT = llvm::ArrayType::get(charT, chars.size());

auto ptr = ctx.builder->CreateGEP(array, Compiler::constInt(ctx, val.size()));
auto initializer = llvm::ConstantArray::get(arrT, chars);

ctx.builder->CreateStore(llvm::ConstantInt::get(elementT, 0), ptr);
auto arr = new llvm::GlobalVariable(
*(ctx.mod), arrT, false,
llvm::GlobalValue::PrivateLinkage, initializer);

return array;
// assign 'arr' to the pointer to the first element of the string
auto zero = Compiler::constInt(ctx, 0);
return ctx.builder->CreateGEP(arr, { zero, zero });
}

llvm::Value* AST::UExpr::llvmValue(Compiler::Context& ctx) {
Expand Down Expand Up @@ -429,10 +429,10 @@ llvm::Value* AST::HoArray::llvmValue(Compiler::Context& ctx) {
// add llvm values to the 'elements' vector
for (auto& expr : exprs) elements.push_back(expr->llvmValue(ctx));

// use void type if the size is equal to 0
// use i8 type if the size is equal to 0
// else use the type of the first element
auto elementT = elements.size() == 0
? llvm::Type::getVoidTy(ctx.mod->getContext())
? llvm::Type::getInt8Ty(ctx.mod->getContext())
: elements[0]->getType();

// create array type using the element type
Expand Down Expand Up @@ -627,7 +627,7 @@ llvm::Value* AST::SetPtr::llvmValue(Compiler::Context& ctx) {
auto val1 = tryCast(ctx, val, valT);

// error if casting failed
if (!val1)
if (!val1 || val1->getType() != valT)
Error::compiler(
U"pointer of setptr instruction is unable to store (expected: "
+ Compiler::llvmTypeStr(valT) + U", got: "
Expand Down Expand Up @@ -761,6 +761,8 @@ llvm::Value* AST::Function::llvmValue(Compiler::Context& ctx) {
Error::compiler(U"error in function '" + std::stou32(id) + U"'");
}

ctx.runFPM(f);

return f;
}

Expand Down Expand Up @@ -816,6 +818,8 @@ llvm::Value* AST::Lambda::llvmValue(Compiler::Context& ctx) {
Error::compiler(U"error in lambda expression");
}

ctx.runFPM(f);

return f;
}

Expand Down
38 changes: 6 additions & 32 deletions src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassManager.h>

#include <llvm/Passes/PassBuilder.h>

#include <llvm/Target/TargetMachine.h>
#include <llvm/Target/TargetOptions.h>

Expand Down Expand Up @@ -63,6 +61,11 @@ llvm::Function* Compiler::Context::getFunction(const std::string& id) {
return nullptr;
}

void Compiler::Context::runFPM(llvm::Function *f) {
if (!f) return;
fpm.run(*f, fam);
}

std::string getFileName(const std::string& path) {
auto s = path.find_last_of("/\\");
return s == std::string::npos ? path : path.substr(s + 1);
Expand All @@ -80,33 +83,6 @@ std::string getModuleId(const std::string& filename) {
: filename;
}

void runMPM(llvm::Module *mod) {
llvm::PassBuilder passBuilder;

llvm::ModuleAnalysisManager mam;
llvm::CGSCCAnalysisManager gam;
llvm::FunctionAnalysisManager fam;
llvm::LoopAnalysisManager lam;

passBuilder.registerModuleAnalyses (mam);
passBuilder.registerCGSCCAnalyses (gam);
passBuilder.registerFunctionAnalyses (fam);
passBuilder.registerLoopAnalyses (lam);

passBuilder.crossRegisterProxies(lam, fam, gam, mam);

// TODO: make configurable
auto mpm = passBuilder.buildPerModuleDefaultPipeline(
llvm::PassBuilder::OptimizationLevel::O3);

mpm.run(*mod, mam);

mam.clear();
gam.clear();
fam.clear();
lam.clear();
}

void compileModuleToFile(llvm::Module *mod, const std::string &output, const std::string &target) {
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
Expand Down Expand Up @@ -183,9 +159,7 @@ void Compiler::compile(std::vector<AST::Expr*>& exprs, bool exe, const std::stri
Compiler::Context cctx(&mod, &builder);
for (auto& expr : exprs) expr->llvmValue(cctx);

// mod.print(llvm::errs(), 0);

runMPM(&mod);
cctx.clear();

if (emitLLVM) {
auto idx = output.find_last_of('/');
Expand Down
32 changes: 30 additions & 2 deletions src/compiler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
#include <string>
#include <vector>

#include <llvm/Passes/PassBuilder.h>

namespace Adscript {
namespace Compiler {

typedef std::pair<llvm::Type*, llvm::Value*> ctx_var_t;

class Context {
private:
llvm::ModuleAnalysisManager mam;
llvm::CGSCCAnalysisManager gam;
llvm::FunctionAnalysisManager fam;
llvm::LoopAnalysisManager lam;
llvm::FunctionPassManager fpm;
public:
llvm::Module *mod;
llvm::IRBuilder<> *builder;
Expand All @@ -21,8 +28,20 @@ public:

bool needsRef = false;

Context(llvm::Module *mod, llvm::IRBuilder<> *builder)
: mod(mod), builder(builder) {}
Context(llvm::Module *mod, llvm::IRBuilder<> *builder) : mod(mod), builder(builder) {
llvm::PassBuilder passBuilder;

passBuilder.registerModuleAnalyses (mam);
passBuilder.registerCGSCCAnalyses (gam);
passBuilder.registerFunctionAnalyses (fam);
passBuilder.registerLoopAnalyses (lam);

passBuilder.crossRegisterProxies(lam, fam, gam, mam);

fpm = passBuilder.buildFunctionSimplificationPipeline(
llvm::PassBuilder::OptimizationLevel::O3,
llvm::PassBuilder::ThinLTOPhase::None, false);
}

bool isVar(const std::string& id);
bool isType(const std::string& id);
Expand All @@ -32,6 +51,15 @@ public:
ctx_var_t getVar(const std::string& id);
ctx_var_t getFinal(const std::string& id);
llvm::Function* getFunction(const std::string& id);

void runFPM(llvm::Function *f);

void clear() {
mam.clear();
gam.clear();
fam.clear();
lam.clear();
}
};

void compile(std::vector<AST::Expr*>& exprs, bool exe, const std::string &output, const std::string &target, bool emitLLVM);
Expand Down
14 changes: 8 additions & 6 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ int main(int argc, char **argv) {
int opt, idx;
opterr = 1;

static struct option long_getopt_options[] = {
static const struct option long_getopt_options[] = {
{"executable", no_argument, nullptr, 'e'},
{"llvm-ir", no_argument, nullptr, 'l'},
{"output", required_argument, nullptr, 'o'},

{"help", no_argument, nullptr, 'h'},
{"target", required_argument, nullptr, 't'},
{"version", no_argument, nullptr, 'v'},

{"output", required_argument, nullptr, 'o'},
{"target", required_argument, nullptr, 't'},
{nullptr, 0, nullptr, 0},
};

const char *shortopts = "el:o:t:h:v";
const char *shortopts = "elvho:t:";

while ((opt = getopt_long(argc, argv, shortopts, long_getopt_options, &idx)) != -1) {
switch (opt) {
case 'e': exe = true; break;
case 'h': Error::printUsage(argv, 1);
case 'l': emitLLVM = true; break;
case 'v': std::cout << "0.4" << std::endl; exit(0);
case 'h': Error::printUsage(argv, 1);
case 'o': output = optarg; break;
case 't': target = optarg; break;
case 'v': std::cout << "0.4" << std::endl; exit(0);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@ llvm::Value* Compiler::tryCast(Compiler::Context& ctx, llvm::Value *v, llvm::Typ
} else if (t->isFloatingPointTy()) {
return ctx.builder->CreateFPCast(v, t);
}
} else if (vT->isArrayTy()) {
if (t->isPointerTy()) {
auto zero = Compiler::constInt(ctx, 0);
return ctx.builder->CreateGEP(v, { zero, zero });
}
} else if (vT->isPointerTy()) {
if (t->isIntegerTy()) {
return ctx.builder->CreatePtrToInt(v, t);
} else if (t->isPointerTy()) {
return ctx.builder->CreatePointerCast(v, t);
}
} else if (vT->isArrayTy()) {
if (t->isPointerTy()) {
return ctx.builder->CreateGEP(
v, { Compiler::constInt(ctx, 0), Compiler::constInt(ctx, 0) });
}
}

return nullptr;
Expand Down

0 comments on commit 9955946

Please sign in to comment.