aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/ArrayHandler.cc27
-rw-r--r--lib/CodeGen/CodeGen.cc31
-rw-r--r--lib/CodeGen/RuntimeCheck.cc2
3 files changed, 30 insertions, 30 deletions
diff --git a/lib/CodeGen/ArrayHandler.cc b/lib/CodeGen/ArrayHandler.cc
index 499fd2d..18b9253 100644
--- a/lib/CodeGen/ArrayHandler.cc
+++ b/lib/CodeGen/ArrayHandler.cc
@@ -53,25 +53,24 @@ void ArrayHandler::emitArrayDeclare(ArrayDeclareStmtAST *Stmt, CodeGen &CG) {
}
std::vector<Value*> Multipliers(Rank);
- Multipliers[Rank - 1] = ConstantInt::get(*TheContext, APInt(32, 1));
+ Multipliers[Rank - 1] = ConstantInt::get(*TheContext, APInt(64, 1));
for (int i = Rank - 2; i >= 0; --i) {
Value *NextMult = Multipliers[i+1];
Value *NextDimSize = Dims[i+1];
- Value *NextDimSize32 = Builder->CreateTrunc(NextDimSize, Type::getInt32Ty(*TheContext));
- Multipliers[i] = Builder->CreateMul(NextMult, NextDimSize32);
+ Multipliers[i] = Builder->CreateMul(NextMult, NextDimSize);
}
ArrayMetadata Meta;
Meta.Rank = Rank;
- Meta.ElementType = Type::getInt32Ty(*TheContext);
+ Meta.ElementType = Type::getInt64Ty(*TheContext);
Meta.LowerBounds = Lows;
Meta.UpperBounds = Highs;
Meta.Multipliers = Multipliers;
ArrayTable[Name] = Meta;
- Value *ElemSize = ConstantInt::get(*TheContext, APInt(64, 4));
+ Value *ElemSize = ConstantInt::get(*TheContext, APInt(64, 8));
Value *TotalBytes = Builder->CreateMul(TotalElements, ElemSize);
CallInst *Ptr = Builder->CreateCall(MallocFunc, TotalBytes);
@@ -89,7 +88,7 @@ Value* ArrayHandler::computeFlatIndex(const std::string &Name, const std::vector
if (ArrayTable.find(Name) == ArrayTable.end()) return nullptr;
const auto &Meta = ArrayTable[Name];
- Value *Offset = ConstantInt::get(*TheContext, APInt(32, 0));
+ Value *Offset = ConstantInt::get(*TheContext, APInt(64, 0));
for (size_t i = 0; i < Indices.size(); ++i) {
Value *Idx = Indices[i];
@@ -129,9 +128,9 @@ Value* ArrayHandler::emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG) {
AllocaInst *Alloca = (*NamedValues)[Name];
Value *RawPtr = Builder->CreateLoad(PointerType::getUnqual(*TheContext), Alloca);
Value *IntPtr = Builder->CreateBitCast(RawPtr, PointerType::getUnqual(*TheContext));
- Value *ElemPtr = Builder->CreateGEP(Type::getInt32Ty(*TheContext), IntPtr, Offset);
+ Value *ElemPtr = Builder->CreateGEP(Type::getInt64Ty(*TheContext), IntPtr, Offset);
- return Builder->CreateLoad(Type::getInt32Ty(*TheContext), ElemPtr);
+ return Builder->CreateLoad(Type::getInt64Ty(*TheContext), ElemPtr);
}
void ArrayHandler::emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG) {
@@ -158,7 +157,7 @@ void ArrayHandler::emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG) {
AllocaInst *Alloca = (*NamedValues)[Name];
Value *RawPtr = Builder->CreateLoad(PointerType::getUnqual(*TheContext), Alloca);
Value *IntPtr = Builder->CreateBitCast(RawPtr, PointerType::getUnqual(*TheContext));
- Value *ElemPtr = Builder->CreateGEP(Type::getInt32Ty(*TheContext), IntPtr, Offset);
+ Value *ElemPtr = Builder->CreateGEP(Type::getInt64Ty(*TheContext), IntPtr, Offset);
Builder->CreateStore(Val, ElemPtr);
}
@@ -201,8 +200,8 @@ void ArrayHandler::emitPrintLoop(const std::string &Name, int CurrentDim, std::v
AllocaInst *Alloca = (*NamedValues)[Name];
Value *RawPtr = Builder->CreateLoad(PointerType::getUnqual(*TheContext), Alloca);
Value *IntPtr = Builder->CreateBitCast(RawPtr, PointerType::getUnqual(*TheContext));
- Value *ElemPtr = Builder->CreateGEP(Type::getInt32Ty(*TheContext), IntPtr, Offset);
- Value *Val = Builder->CreateLoad(Type::getInt32Ty(*TheContext), ElemPtr);
+ Value *ElemPtr = Builder->CreateGEP(Type::getInt64Ty(*TheContext), IntPtr, Offset);
+ Value *Val = Builder->CreateLoad(Type::getInt64Ty(*TheContext), ElemPtr);
std::vector<Value*> Args;
Args.push_back(FmtStr);
@@ -216,7 +215,7 @@ void ArrayHandler::emitPrintLoop(const std::string &Name, int CurrentDim, std::v
BasicBlock *AfterBB = BasicBlock::Create(*TheContext, "arr_after", TheFunction);
IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin());
- AllocaInst *LoopVar = TmpB.CreateAlloca(Type::getInt32Ty(*TheContext), nullptr, "idx");
+ AllocaInst *LoopVar = TmpB.CreateAlloca(Type::getInt64Ty(*TheContext), nullptr, "idx");
Value *Start = Meta.LowerBounds[CurrentDim];
Value *End = Meta.UpperBounds[CurrentDim];
@@ -225,13 +224,13 @@ void ArrayHandler::emitPrintLoop(const std::string &Name, int CurrentDim, std::v
Builder->CreateBr(LoopBB);
Builder->SetInsertPoint(LoopBB);
- Value *CurVal = Builder->CreateLoad(Type::getInt32Ty(*TheContext), LoopVar);
+ Value *CurVal = Builder->CreateLoad(Type::getInt64Ty(*TheContext), LoopVar);
std::vector<Value*> NextIndices = CurrentIndices;
NextIndices.push_back(CurVal);
emitPrintLoop(Name, CurrentDim + 1, NextIndices, PrintfFunc, FmtStr);
- Value *NextVal = Builder->CreateAdd(CurVal, ConstantInt::get(*TheContext, APInt(32, 1)));
+ Value *NextVal = Builder->CreateAdd(CurVal, ConstantInt::get(*TheContext, APInt(64, 1)));
Builder->CreateStore(NextVal, LoopVar);
Value *Cond = Builder->CreateICmpSLE(NextVal, End);
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc
index e2c959e..9dc88af 100644
--- a/lib/CodeGen/CodeGen.cc
+++ b/lib/CodeGen/CodeGen.cc
@@ -29,15 +29,15 @@ void CodeGen::SetupExternalFunctions() {
FunctionType *ScanfType = FunctionType::get(Type::getInt32Ty(*TheContext), PrintfArgs, true);
ScanfFunc = TheModule->getOrInsertFunction("scanf", ScanfType);
- PrintfFormatStr = Builder->CreateGlobalStringPtr("%d\n", "fmt_nl", 0, TheModule.get());
- ScanfFormatStr = Builder->CreateGlobalStringPtr("%d", "fmt_in", 0, TheModule.get());
+ PrintfFormatStr = Builder->CreateGlobalStringPtr("%lld\n", "fmt_nl", 0, TheModule.get());
+ ScanfFormatStr = Builder->CreateGlobalStringPtr("%lld", "fmt_in", 0, TheModule.get());
Arrays->setupExternalFunctions();
}
AllocaInst *CodeGen::CreateEntryBlockAlloca(Function *TheFunction, const std::string &VarName) {
IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin());
- return TmpB.CreateAlloca(Type::getInt32Ty(*TheContext), nullptr, VarName);
+ return TmpB.CreateAlloca(Type::getInt64Ty(*TheContext), nullptr, VarName);
}
void CodeGen::compile(const std::vector<std::unique_ptr<StmtAST>> &Statements) {
@@ -64,7 +64,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) {
if (!Expr) return nullptr;
if (auto *Num = dynamic_cast<NumberExprAST*>(Expr)) {
- return ConstantInt::get(*TheContext, APInt(32, Num->getVal(), true));
+ return ConstantInt::get(*TheContext, APInt(64, Num->getVal(), true));
}
if (auto *Var = dynamic_cast<VariableExprAST*>(Expr)) {
@@ -74,7 +74,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) {
return nullptr;
}
- return Builder->CreateLoad(Type::getInt32Ty(*TheContext), A, Var->getName().c_str());
+ return Builder->CreateLoad(Type::getInt64Ty(*TheContext), A, Var->getName().c_str());
}
@@ -112,8 +112,8 @@ void CodeGen::emitIfStmt(IfStmtAST *Stmt) {
Value *CondV = emitExpr(Stmt->getCond());
if (!CondV) return;
- if (CondV->getType()->isIntegerTy(32))
- CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(32, 0)), "ifcond");
+ if (CondV->getType()->isIntegerTy(64))
+ CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(64, 0)), "ifcond");
Function *TheFunction = Builder->GetInsertBlock()->getParent();
BasicBlock *ThenBB = BasicBlock::Create(*TheContext, "then", TheFunction);
@@ -145,8 +145,8 @@ void CodeGen::emitWhileStmt(WhileStmtAST *Stmt) {
Builder->SetInsertPoint(CondBB);
Value *CondV = emitExpr(Stmt->getCond());
if (!CondV) return;
- if (CondV->getType()->isIntegerTy(32))
- CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(32, 0)), "loopcond");
+ if (CondV->getType()->isIntegerTy(64))
+ CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(64, 0)), "loopcond");
Builder->CreateCondBr(CondV, LoopBB, AfterBB);
@@ -177,8 +177,8 @@ void CodeGen::emitRepeatStmt(RepeatStmtAST *Stmt) {
Value *CondV = emitExpr(Stmt->getCond());
if (!CondV) return;
- if (CondV->getType()->isIntegerTy(32))
- CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(32, 0)), "untilcond");
+ if (CondV->getType()->isIntegerTy(64))
+ CondV = Builder->CreateICmpNE(CondV, ConstantInt::get(*TheContext, APInt(64, 0)), "untilcond");
Builder->CreateCondBr(CondV, AfterBB, LoopBB);
@@ -207,7 +207,7 @@ void CodeGen::emitForStmt(ForStmtAST *Stmt) {
Builder->CreateBr(CondBB);
Builder->SetInsertPoint(CondBB);
- Value *CurVar = Builder->CreateLoad(Type::getInt32Ty(*TheContext), Alloca, VarName.c_str());
+ Value *CurVar = Builder->CreateLoad(Type::getInt64Ty(*TheContext), Alloca, VarName.c_str());
Value *EndVal = emitExpr(Stmt->getEnd());
if (!EndVal) return;
@@ -237,10 +237,10 @@ void CodeGen::emitForStmt(ForStmtAST *Stmt) {
if (Stmt->getStep()) {
StepVal = emitExpr(Stmt->getStep());
} else {
- StepVal = ConstantInt::get(*TheContext, APInt(32, 1));
+ StepVal = ConstantInt::get(*TheContext, APInt(64, 1));
}
- Value *CurValForInc = Builder->CreateLoad(Type::getInt32Ty(*TheContext), Alloca, VarName.c_str());
+ Value *CurValForInc = Builder->CreateLoad(Type::getInt64Ty(*TheContext), Alloca, VarName.c_str());
Value *NextVal = Builder->CreateAdd(CurValForInc, StepVal, "nextval");
Builder->CreateStore(NextVal, Alloca);
Builder->CreateBr(CondBB);
@@ -265,7 +265,7 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
Function *TheFunction = Builder->GetInsertBlock()->getParent();
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Decl->getName());
NamedValues[Decl->getName()] = Alloca;
- Builder->CreateStore(ConstantInt::get(*TheContext, APInt(32, 0)), Alloca);
+ Builder->CreateStore(ConstantInt::get(*TheContext, APInt(64, 0)), Alloca);
}
else if (auto *Assign = dynamic_cast<AssignStmtAST*>(Stmt)) {
Value *Val = emitExpr(Assign->getExpr());
@@ -313,3 +313,4 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
emitForStmt(ForStmt);
}
}
+
diff --git a/lib/CodeGen/RuntimeCheck.cc b/lib/CodeGen/RuntimeCheck.cc
index cab50d5..d51d78a 100644
--- a/lib/CodeGen/RuntimeCheck.cc
+++ b/lib/CodeGen/RuntimeCheck.cc
@@ -47,7 +47,7 @@ void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, int Line) {
}
void RuntimeCheck::emitDivZeroCheck(Value *Divisor, int Line) {
- Value *IsZero = Builder.CreateICmpEQ(Divisor, ConstantInt::get(TheContext, APInt(32, 0)), "is_zero");
+ Value *IsZero = Builder.CreateICmpEQ(Divisor, ConstantInt::get(TheContext, APInt(64, 0)), "is_zero");
emitErrorAndExit(IsZero, DivZeroMsg, Line);
}