aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CodeGen.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-07-26 17:13:15 +0000
committer_ov4 <ov4@ov4.org>2026-07-26 17:13:15 +0000
commit0fef2986daa2945c9e3d050c0f750d56792b30bd (patch)
tree4db469725cc64bb0c45978951e071dacaf58a687 /lib/CodeGen/CodeGen.cc
parent344206d57e834b2394cbe2ec0df50b04a8b3c171 (diff)
refactor: groundwork for TYPE — sizeof constants, string guard, array scoping
- Element sizes now flow as target-folded null-GEP sizeof constants (TypeSystem::getSizeOfConstant); the module has no DataLayout string, so host-side size math would diverge from the backend on padded structs. - Extract the triplicated STRING null->"" select into CodeGen::emitStringNullGuard. - Scope ArrayTable per function via ArrayHandler::exchangeTable, mirroring the Symbols save/clear/restore discipline. - Promote checked element addressing to public ArrayHandler::emitElementAddress for the upcoming lvalue layer. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/CodeGen.cc')
-rw-r--r--lib/CodeGen/CodeGen.cc25
1 files changed, 14 insertions, 11 deletions
diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc
index ae82914..4b9ebfb 100644
--- a/lib/CodeGen/CodeGen.cc
+++ b/lib/CodeGen/CodeGen.cc
@@ -363,6 +363,14 @@ void CodeGen::emitDeclareStmt(DeclareStmtAST *Stmt) {
registerSymbol(Stmt->getName(), Alloca, Info->Name, false);
}
+Value *CodeGen::emitStringNullGuard(Value *StrVal) {
+ if (!StrVal || !StrVal->getType()->isPointerTy()) return StrVal;
+ Value *IsNull = Builder->CreateICmpEQ(StrVal,
+ ConstantPointerNull::get(cast<PointerType>(StrVal->getType())),
+ "str_is_null");
+ return Builder->CreateSelect(IsNull, EmptyStringStr, StrVal, "safe_str");
+}
+
void CodeGen::emitOutputValue(Value *Val, const TypeInfo *Info, bool AppendNewline) {
if (!Val || !Info) return;
if (!Info->Printable && Info->Kind == TypeKind::Custom) {
@@ -387,13 +395,7 @@ void CodeGen::emitOutputValue(Value *Val, const TypeInfo *Info, bool AppendNewli
Args.push_back(Fmt);
Args.push_back(Builder->CreateSelect(BoolVal, TrueStr, FalseStr));
} else if (Info->isString()) {
- Value *StrVal = Val;
- if (StrVal->getType()->isPointerTy()) {
- Value *IsNull = Builder->CreateICmpEQ(StrVal,
- ConstantPointerNull::get(cast<PointerType>(StrVal->getType())),
- "str_is_null");
- StrVal = Builder->CreateSelect(IsNull, EmptyStringStr, StrVal, "safe_str");
- }
+ Value *StrVal = emitStringNullGuard(Val);
Fmt = AppendNewline
? PrintfStringFormatStr
: Builder->CreateGlobalStringPtr("%s", "fmt_str_inline", 0, TheModule.get());
@@ -491,10 +493,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) {
Value *Loaded = Builder->CreateLoad(TypeInfo->LLVMType, Info->Storage, Var->getName().c_str());
if (TypeInfo->isString()) {
- Value *IsNull = Builder->CreateICmpEQ(Loaded,
- ConstantPointerNull::get(cast<PointerType>(Loaded->getType())),
- "str_is_null");
- Loaded = Builder->CreateSelect(IsNull, EmptyStringStr, Loaded, "safe_var_str");
+ Loaded = emitStringNullGuard(Loaded);
}
return Loaded;
}
@@ -836,11 +835,15 @@ void CodeGen::emitStmt(StmtAST *Stmt) {
if (auto *FuncDef = dynamic_cast<FunctionDefAST*>(Stmt)) {
BasicBlock *SavedBlock = Builder->GetInsertBlock();
+ // Function bodies get a fresh array scope (metadata SSA values belong
+ // to the emitting function), mirroring FunctionGen's Symbols handling.
+ auto SavedArrays = Arrays->exchangeTable({});
FuncGen->emitFunctionDef(FuncDef, [this](StmtAST *S) {
this->emitStmt(S);
});
+ Arrays->exchangeTable(std::move(SavedArrays));
if (SavedBlock) Builder->SetInsertPoint(SavedBlock);
return;
}