diff options
| author | ov-4 <hausemasterstream@gmail.com> | 2026-01-30 02:06:37 +0000 |
|---|---|---|
| committer | ov-4 <hausemasterstream@gmail.com> | 2026-01-30 02:06:37 +0000 |
| commit | 766a4b8e5236bffb40984439a79186c5ee823ff8 (patch) | |
| tree | a4226690b4b95c916908afa0047899ff60e63c81 /lib/CodeGen | |
| parent | 943ed04b775949e8bc4948bcb8d762a32ab14f76 (diff) | |
Added runtime check for array index and division
Diffstat (limited to 'lib/CodeGen')
| -rw-r--r-- | lib/CodeGen/ArrayHandler.cc | 23 | ||||
| -rw-r--r-- | lib/CodeGen/CodeGen.cc | 8 | ||||
| -rw-r--r-- | lib/CodeGen/RuntimeCheck.cc | 60 |
3 files changed, 83 insertions, 8 deletions
diff --git a/lib/CodeGen/ArrayHandler.cc b/lib/CodeGen/ArrayHandler.cc index 470cffa..499fd2d 100644 --- a/lib/CodeGen/ArrayHandler.cc +++ b/lib/CodeGen/ArrayHandler.cc @@ -8,8 +8,8 @@ using namespace llvm; using namespace cps; -ArrayHandler::ArrayHandler(LLVMContext &C, IRBuilder<> &B, Module &M, std::map<std::string, AllocaInst*> &NV) - : TheContext(&C), Builder(&B), TheModule(&M), NamedValues(&NV) { +ArrayHandler::ArrayHandler(LLVMContext &C, IRBuilder<> &B, Module &M, std::map<std::string, AllocaInst*> &NV, RuntimeCheck &RC) + : TheContext(&C), Builder(&B), TheModule(&M), NamedValues(&NV), RuntimeChecker(RC) { setupExternalFunctions(); } @@ -115,8 +115,13 @@ Value* ArrayHandler::emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG) { } std::vector<Value*> Indices; - for (const auto &IdxExpr : Expr->getIndices()) { - Indices.push_back(CG.emitExpr(IdxExpr.get())); + for (size_t i = 0; i < Expr->getIndices().size(); ++i) { + Value *Idx = CG.emitExpr(Expr->getIndices()[i].get()); + Indices.push_back(Idx); + + Value *Lower = Meta.LowerBounds[i]; + Value *Upper = Meta.UpperBounds[i]; + RuntimeChecker.emitIndexCheck(Idx, Lower, Upper, Expr->getLine()); } Value *Offset = computeFlatIndex(Name, Indices); @@ -137,8 +142,14 @@ void ArrayHandler::emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG) { } std::vector<Value*> Indices; - for (const auto &IdxExpr : Stmt->getIndices()) { - Indices.push_back(CG.emitExpr(IdxExpr.get())); + for (size_t i = 0; i < Stmt->getIndices().size(); ++i) { + Value *Idx = CG.emitExpr(Stmt->getIndices()[i].get()); + Indices.push_back(Idx); + + const auto &Meta = ArrayTable[Name]; + Value *Lower = Meta.LowerBounds[i]; + Value *Upper = Meta.UpperBounds[i]; + RuntimeChecker.emitIndexCheck(Idx, Lower, Upper, Stmt->getLine()); } Value *Val = CG.emitExpr(Stmt->getExpr()); diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 57d76eb..e2c959e 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -13,7 +13,9 @@ CodeGen::CodeGen() { TheModule = std::make_unique<Module>("cps_module", *TheContext); Builder = std::make_unique<IRBuilder<>>(*TheContext); - Arrays = std::make_unique<ArrayHandler>(*TheContext, *Builder, *TheModule, NamedValues); + RuntimeChecker = std::make_unique<RuntimeCheck>(*TheModule, *TheContext, *Builder); + + Arrays = std::make_unique<ArrayHandler>(*TheContext, *Builder, *TheModule, NamedValues, *RuntimeChecker); SetupExternalFunctions(); } @@ -89,7 +91,9 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { case '+': return Builder->CreateAdd(L, R, "addtmp"); case '-': return Builder->CreateSub(L, R, "subtmp"); case '*': return Builder->CreateMul(L, R, "multmp"); - case '/': return Builder->CreateSDiv(L, R, "divtmp"); + case '/': + RuntimeChecker->emitDivZeroCheck(R, Bin->getLine()); + return Builder->CreateSDiv(L, R, "divtmp"); case tok_eq: return Builder->CreateICmpEQ(L, R, "eqtmp"); case tok_ne: return Builder->CreateICmpNE(L, R, "netmp"); case '<': return Builder->CreateICmpSLT(L, R, "slttmp"); diff --git a/lib/CodeGen/RuntimeCheck.cc b/lib/CodeGen/RuntimeCheck.cc new file mode 100644 index 0000000..cab50d5 --- /dev/null +++ b/lib/CodeGen/RuntimeCheck.cc @@ -0,0 +1,60 @@ +#include "cps/RuntimeCheck.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/Constants.h" + +using namespace llvm; +using namespace cps; + +RuntimeCheck::RuntimeCheck(Module &M, LLVMContext &C, IRBuilder<> &B) + : TheModule(M), TheContext(C), Builder(B) { + setupExternalFunctions(); +} + +void RuntimeCheck::setupExternalFunctions() { + std::vector<Type*> PrintfArgs; + PrintfArgs.push_back(PointerType::getUnqual(TheContext)); + FunctionType *PrintfType = FunctionType::get(Type::getInt32Ty(TheContext), PrintfArgs, true); + PrintfFunc = TheModule.getOrInsertFunction("printf", PrintfType); + + std::vector<Type*> ExitArgs; + ExitArgs.push_back(Type::getInt32Ty(TheContext)); + FunctionType *ExitType = FunctionType::get(Type::getVoidTy(TheContext), ExitArgs, false); + ExitFunc = TheModule.getOrInsertFunction("exit", ExitType); + + DivZeroMsg = Builder.CreateGlobalStringPtr("[Fatal] line %d: Division by zero\n", "err_div_zero", 0, &TheModule); + OutOfBoundsMsg = Builder.CreateGlobalStringPtr("[Fatal] line %d: Array index out of bounds\n", "err_bounds", 0, &TheModule); +} + +void RuntimeCheck::emitErrorAndExit(Value *Condition, Value *Msg, int Line) { + Function *TheFunction = Builder.GetInsertBlock()->getParent(); + + BasicBlock *FailBB = BasicBlock::Create(TheContext, "check_fail", TheFunction); + BasicBlock *ContBB = BasicBlock::Create(TheContext, "check_cont", TheFunction); + + Builder.CreateCondBr(Condition, FailBB, ContBB); + + Builder.SetInsertPoint(FailBB); + std::vector<Value*> PrintArgs; + PrintArgs.push_back(Msg); + PrintArgs.push_back(ConstantInt::get(TheContext, APInt(32, Line))); + Builder.CreateCall(PrintfFunc, PrintArgs); + + Builder.CreateCall(ExitFunc, ConstantInt::get(TheContext, APInt(32, 1))); + Builder.CreateUnreachable(); + + Builder.SetInsertPoint(ContBB); +} + +void RuntimeCheck::emitDivZeroCheck(Value *Divisor, int Line) { + Value *IsZero = Builder.CreateICmpEQ(Divisor, ConstantInt::get(TheContext, APInt(32, 0)), "is_zero"); + emitErrorAndExit(IsZero, DivZeroMsg, Line); +} + +void RuntimeCheck::emitIndexCheck(Value *Index, Value *Lower, Value *Upper, int Line) { + Value *TooLow = Builder.CreateICmpSLT(Index, Lower, "too_low"); + Value *TooHigh = Builder.CreateICmpSGT(Index, Upper, "too_high"); + Value *OutOfBounds = Builder.CreateOr(TooLow, TooHigh, "out_of_bounds"); + + emitErrorAndExit(OutOfBounds, OutOfBoundsMsg, Line); +} |
