#include "BrainF.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include <cstdlib>
#include <iostream>
using namespace llvm;
const char *BrainF::tapereg = "tape";
const char *BrainF::headreg = "head";
const char *BrainF::label = "brainf";
const char *BrainF::testreg = "test";
Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf,
LLVMContext& Context) {
in = in1;
memtotal = mem;
comflag = cf;
header(Context);
readloop(nullptr, nullptr, nullptr, Context);
delete builder;
return module;
}
void BrainF::header(LLVMContext& C) {
module = new Module("BrainF", C);
Type *Tys[] = { Type::getInt8PtrTy(C), Type::getInt32Ty(C) };
Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset,
Tys);
getchar_func =
module->getOrInsertFunction("getchar", IntegerType::getInt32Ty(C));
putchar_func = module->getOrInsertFunction(
"putchar", IntegerType::getInt32Ty(C), IntegerType::getInt32Ty(C));
brainf_func = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
Function::ExternalLinkage, "brainf", module);
builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
BasicBlock* BB = builder->GetInsertBlock();
Type* IntPtrTy = IntegerType::getInt32Ty(C);
Type* Int8Ty = IntegerType::getInt8Ty(C);
Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty);
allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy);
ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem,
nullptr, "arr");
BB->getInstList().push_back(cast<Instruction>(ptr_arr));
{
Value *memset_params[] = {
ptr_arr,
ConstantInt::get(C, APInt(8, 0)),
val_mem,
ConstantInt::get(C, APInt(1, 0))
};
CallInst *memset_call = builder->
CreateCall(memset_func, memset_params);
memset_call->setTailCall(false);
}
if (comflag & flag_arraybounds) {
ptr_arrmax = builder->CreateGEP(
Int8Ty, ptr_arr, ConstantInt::get(C, APInt(32, memtotal)), "arrmax");
}
curhead = builder->CreateGEP(
Int8Ty, ptr_arr, ConstantInt::get(C, APInt(32, memtotal / 2)), headreg);
endbb = BasicBlock::Create(C, label, brainf_func);
endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb));
ReturnInst::Create(C, endbb);
if (comflag & flag_arraybounds)
{
Constant *msg_0 =
ConstantDataArray::getString(C, "Error: The head has left the tape.",
true);
GlobalVariable *aberrormsg = new GlobalVariable(
*module,
msg_0->getType(),
true,
GlobalValue::InternalLinkage,
msg_0,
"aberrormsg");
FunctionCallee puts_func = module->getOrInsertFunction(
"puts", IntegerType::getInt32Ty(C),
PointerType::getUnqual(IntegerType::getInt8Ty(C)));
aberrorbb = BasicBlock::Create(C, label, brainf_func);
{
Constant *zero_32 = Constant::getNullValue(IntegerType::getInt32Ty(C));
Constant *gep_params[] = {
zero_32,
zero_32
};
Constant *msgptr = ConstantExpr::
getGetElementPtr(aberrormsg->getValueType(), aberrormsg, gep_params);
Value *puts_params[] = {
msgptr
};
CallInst *puts_call =
CallInst::Create(puts_func,
puts_params,
"", aberrorbb);
puts_call->setTailCall(false);
}
BranchInst::Create(endbb, aberrorbb);
}
}
void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
LLVMContext &C) {
Symbol cursym = SYM_NONE;
int curvalue = 0;
Symbol nextsym = SYM_NONE;
int nextvalue = 0;
char c;
int loop;
int direction;
Type *Int8Ty = IntegerType::getInt8Ty(C);
while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
switch(cursym) {
case SYM_NONE:
break;
case SYM_READ:
{
CallInst *getchar_call =
builder->CreateCall(getchar_func, {}, tapereg);
getchar_call->setTailCall(false);
Value *tape_0 = getchar_call;
Value *tape_1 = builder->
CreateTrunc(tape_0, IntegerType::getInt8Ty(C), tapereg);
builder->CreateStore(tape_1, curhead);
}
break;
case SYM_WRITE:
{
LoadInst *tape_0 = builder->CreateLoad(Int8Ty, curhead, tapereg);
Value *tape_1 = builder->
CreateSExt(tape_0, IntegerType::getInt32Ty(C), tapereg);
Value *putchar_params[] = {
tape_1
};
CallInst *putchar_call = builder->
CreateCall(putchar_func,
putchar_params);
putchar_call->setTailCall(false);
}
break;
case SYM_MOVE:
{
curhead = builder->CreateGEP(Int8Ty, curhead,
ConstantInt::get(C, APInt(32, curvalue)),
headreg);
if (comflag & flag_arraybounds)
{
Value *test_0 = builder->
CreateICmpUGE(curhead, ptr_arrmax, testreg);
Value *test_1 = builder->
CreateICmpULT(curhead, ptr_arr, testreg);
Value *test_2 = builder->
CreateOr(test_0, test_1, testreg);
BasicBlock *nextbb = BasicBlock::Create(C, label, brainf_func);
builder->CreateCondBr(test_2, aberrorbb, nextbb);
builder->SetInsertPoint(nextbb);
}
}
break;
case SYM_CHANGE:
{
LoadInst *tape_0 = builder->CreateLoad(Int8Ty, curhead, tapereg);
Value *tape_1 = builder->
CreateAdd(tape_0, ConstantInt::get(C, APInt(8, curvalue)), tapereg);
builder->CreateStore(tape_1, curhead);
}
break;
case SYM_LOOP:
{
BasicBlock *testbb = BasicBlock::Create(C, label, brainf_func);
builder->CreateBr(testbb);
BasicBlock *bb_0 = builder->GetInsertBlock();
BasicBlock *bb_1 = BasicBlock::Create(C, label, brainf_func);
builder->SetInsertPoint(bb_1);
PHINode *phi_0 = PHINode::Create(PointerType::getUnqual(Int8Ty), 2,
headreg, testbb);
phi_0->addIncoming(curhead, bb_0);
curhead = phi_0;
readloop(phi_0, bb_1, testbb, C);
}
break;
default:
std::cerr << "Error: Unknown symbol.\n";
abort();
break;
}
cursym = nextsym;
curvalue = nextvalue;
nextsym = SYM_NONE;
loop = (cursym == SYM_NONE)
|| (cursym == SYM_MOVE)
|| (cursym == SYM_CHANGE);
while(loop) {
*in>>c;
if (in->eof()) {
if (cursym == SYM_NONE) {
cursym = SYM_EOF;
} else {
nextsym = SYM_EOF;
}
loop = 0;
} else {
direction = 1;
switch(c) {
case '-':
direction = -1;
LLVM_FALLTHROUGH;
case '+':
if (cursym == SYM_CHANGE) {
curvalue += direction;
} else {
if (cursym == SYM_NONE) {
cursym = SYM_CHANGE;
curvalue = direction;
} else {
nextsym = SYM_CHANGE;
nextvalue = direction;
loop = 0;
}
}
break;
case '<':
direction = -1;
LLVM_FALLTHROUGH;
case '>':
if (cursym == SYM_MOVE) {
curvalue += direction;
} else {
if (cursym == SYM_NONE) {
cursym = SYM_MOVE;
curvalue = direction;
} else {
nextsym = SYM_MOVE;
nextvalue = direction;
loop = 0;
}
}
break;
case ',':
if (cursym == SYM_NONE) {
cursym = SYM_READ;
} else {
nextsym = SYM_READ;
}
loop = 0;
break;
case '.':
if (cursym == SYM_NONE) {
cursym = SYM_WRITE;
} else {
nextsym = SYM_WRITE;
}
loop = 0;
break;
case '[':
if (cursym == SYM_NONE) {
cursym = SYM_LOOP;
} else {
nextsym = SYM_LOOP;
}
loop = 0;
break;
case ']':
if (cursym == SYM_NONE) {
cursym = SYM_ENDLOOP;
} else {
nextsym = SYM_ENDLOOP;
}
loop = 0;
break;
default:
break;
}
}
}
}
if (cursym == SYM_ENDLOOP) {
if (!phi) {
std::cerr << "Error: Extra ']'\n";
abort();
}
{
builder->CreateBr(testbb);
phi->addIncoming(curhead, builder->GetInsertBlock());
Value *head_0 = phi;
LoadInst *tape_0 = new LoadInst(Int8Ty, head_0, tapereg, testbb);
ICmpInst *test_0 = new ICmpInst(*testbb, ICmpInst::ICMP_EQ, tape_0,
ConstantInt::get(C, APInt(8, 0)), testreg);
BasicBlock *bb_0 = BasicBlock::Create(C, label, brainf_func);
BranchInst::Create(bb_0, oldbb, test_0, testbb);
builder->SetInsertPoint(bb_0);
PHINode *phi_1 =
builder->CreatePHI(PointerType::getUnqual(Int8Ty), 1, headreg);
phi_1->addIncoming(head_0, testbb);
curhead = phi_1;
}
return;
}
builder->CreateBr(endbb);
if (phi) {
std::cerr << "Error: Missing ']'\n";
abort();
}
}