aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CodeGen.cc
diff options
context:
space:
mode:
author_ov4 <hausemasterstream@gmail.com>2026-01-30 13:11:20 +0000
committer_ov4 <hausemasterstream@gmail.com>2026-01-30 13:11:20 +0000
commitbd799e42b40613983c6e3cb9aedf58866ac68605 (patch)
tree03fdd0dffba45fa2a1d9b8d4b0e78f49140c07e5 /lib/CodeGen/CodeGen.cc
parent80420b0f92f42edc0a3cac4490a08adb8b2457dc (diff)
Added FUNCTION and PROCEDURE
Diffstat (limited to 'lib/CodeGen/CodeGen.cc')
-rw-r--r--lib/CodeGen/CodeGen.cc42
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc
index 9dc88af..6358699 100644
--- a/lib/CodeGen/CodeGen.cc
+++ b/lib/CodeGen/CodeGen.cc
@@ -17,6 +17,8 @@ CodeGen::CodeGen() {
Arrays = std::make_unique<ArrayHandler>(*TheContext, *Builder, *TheModule, NamedValues, *RuntimeChecker);
+ FuncGen = std::make_unique<FunctionGen>(*TheContext, *TheModule, *Builder, NamedValues);
+
SetupExternalFunctions();
}
@@ -105,6 +107,15 @@ Value *CodeGen::emitExpr(ExprAST *Expr) {
return nullptr;
}
}
+
+ if (auto *Call = dynamic_cast<CallExprAST*>(Expr)) {
+ std::vector<Value*> Args;
+ for (const auto &Arg : Call->getArgs()) {
+ Args.push_back(emitExpr(Arg.get()));
+ }
+ return FuncGen->emitCallExpr(Call, Args);
+ }
+
return nullptr;
}
@@ -261,6 +272,36 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
return;
}
+ if (auto *FuncDef = dynamic_cast<FunctionDefAST*>(Stmt)) {
+ BasicBlock *SavedBlock = Builder->GetInsertBlock();
+
+ FuncGen->emitFunctionDef(FuncDef, [this](StmtAST *S) {
+ this->emitStmt(S);
+ });
+
+ if (SavedBlock) Builder->SetInsertPoint(SavedBlock);
+
+ return;
+ }
+
+ else if (auto *Call = dynamic_cast<CallStmtAST*>(Stmt)) {
+ std::vector<Value*> Args;
+ for (const auto &ArgExpr : Call->getArgs()) {
+ Args.push_back(emitExpr(ArgExpr.get()));
+ }
+ FuncGen->emitCallStmt(Call, Args);
+ return;
+ }
+
+ else if (auto *Ret = dynamic_cast<ReturnStmtAST*>(Stmt)) {
+ Value *RetVal = nullptr;
+ if (Ret->getRetVal()) {
+ RetVal = emitExpr(Ret->getRetVal());
+ }
+ FuncGen->emitReturn(Ret, RetVal);
+ return;
+ }
+
if (auto *Decl = dynamic_cast<DeclareStmtAST*>(Stmt)) {
Function *TheFunction = Builder->GetInsertBlock()->getParent();
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Decl->getName());
@@ -313,4 +354,3 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
emitForStmt(ForStmt);
}
}
-