Skip to content

Commit

Permalink
new/delete fixes, removed use of default destructors and code for aut…
Browse files Browse the repository at this point in the history
…o cleanup (it's just not working in some cases, better write fully functional gc)
  • Loading branch information
f0xeri committed Mar 10, 2024
1 parent 0fab71e commit aece62f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
5 changes: 4 additions & 1 deletion check/CheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ namespace Slangc::Check {
}

bool checkStmt(const ReturnStatementPtr &stmt, Context &context, std::vector<ErrorMessage> &errors) {
auto result = checkExpr(stmt->expr, context, errors);
bool result = true;
if (!(std::holds_alternative<TypeExprPtr>(stmt->expr) && std::get<TypeExprPtr>(stmt->expr)->type == "void")) {
result = checkExpr(stmt->expr, context, errors);
}
if (!result) return result;
auto type = getExprType(stmt->expr, context, errors).value();
context.currFuncReturnTypes.emplace_back(type, stmt->loc);
Expand Down
20 changes: 11 additions & 9 deletions codegen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ namespace Slangc {

auto DeleteStmtNode::codegen(CodeGenContext &context, std::vector<ErrorMessage>& errors) -> Value* {
if (context.debug) context.debugBuilder->emitLocation(loc);
context.loadValue = true; // TODO: not sure if it's correct
context.loadValue = false; // TODO: not sure if it's correct
auto var = processNode(expr, context, errors);
context.loadValue = false;
// call destructor

auto type = getExprType(expr, context.context, errors).value();
if (auto typeExpr = std::get_if<TypeExprPtr>(&type)) {
cleanupVar(typeExpr->get()->type, var, context, errors);
Expand All @@ -442,9 +442,9 @@ namespace Slangc {
return var;
}
else {
// TODO: check if it's correct and how we can set var to null
return context.builder->CreateCall(context.freeFunc, var);
//return context.builder->CreateStore(Constant::getNullValue(var->getType()), var);
context.builder->CreateCall(context.freeFunc, var);
auto notLoadedVar = processNode(expr, context, errors);
return context.builder->CreateStore(Constant::getNullValue(notLoadedVar->getType()), notLoadedVar);
}
}

Expand All @@ -467,14 +467,14 @@ namespace Slangc {
auto rightVal = processNode(right, context, errors);
context.refferencing = false;
// if right is new, clean up left
if (auto newExpr = std::get_if<NewExprPtr>(&right)) {
/*if (auto newExpr = std::get_if<NewExprPtr>(&right)) {
if (auto arr = std::get_if<ArrayExprPtr>(&newExpr->get()->type)) {
createArrayFree(*arr, leftVal, context, errors);
}
else if (auto typeExpr = std::get_if<TypeExprPtr>(&newExpr->get()->type)) {
cleanupVar(typeExpr->get()->type, leftVal, context, errors);
}
}
}*/
context.currentFuncSignature = tempSig;
context.loadValue = false;
if (ltype != rightVal->getType()) {
Expand All @@ -493,14 +493,16 @@ namespace Slangc {

auto ReturnStatementNode::codegen(CodeGenContext &context, std::vector<ErrorMessage>& errors) -> Value* {
if (context.debug) context.debugBuilder->emitLocation(loc);
if (std::holds_alternative<TypeExprPtr>(expr) && std::get<TypeExprPtr>(expr)->type == "void")
return context.builder->CreateRetVoid();
context.loadValue = true;
context.refferencing = true;
auto type = getExprType(expr, context.context, errors).value();
auto val = processNode(expr, context, errors);
context.refferencing = false;
context.loadValue = false;
val = typeCast(val, context.currentReturnType, context, errors, getExprLoc(expr));
cleanupCurrentScope(context, errors);
// cleanupCurrentScope(context, errors);
return context.builder->CreateRet(val);
}

Expand Down Expand Up @@ -848,7 +850,7 @@ namespace Slangc {
}
context.currentReturnType = nullptr;
if (!hasReturn) {
cleanupCurrentScope(context, errors);
// cleanupCurrentScope(context, errors);
}
if (!isFunction) context.builder->CreateRetVoid();
else {
Expand Down
2 changes: 1 addition & 1 deletion codegen/CodeGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace Slangc {
for (auto&stmt: moduleAST->block->statements) {
processNode(stmt, codeGenContext, errors);
}
cleanupCurrentScope(codeGenContext, errors);
// cleanupCurrentScope(codeGenContext, errors);
codeGenContext.endMainFunc();
}
llvm::verifyModule(*codeGenContext.module, &llvm::errs());
Expand Down
19 changes: 10 additions & 9 deletions codegen/CodeGenHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ namespace Slangc {
context.ret(whileEnd);
context.builder->SetInsertPoint(whileEnd);
if (i == 0) {
auto arrLoad = context.builder->CreateLoad(getIRType(array, context), var);
context.builder->CreateCall(context.freeFunc, arrLoad);
auto arrLoadZero = context.builder->CreateLoad(getIRType(array, context), var);
context.builder->CreateCall(context.freeFunc, arrLoadZero);
context.builder->CreateStore(Constant::getNullValue(getIRType(array->type, context)), var);
}
}
Expand All @@ -380,7 +380,7 @@ namespace Slangc {
if (indicesCount == 1) {
auto arrLoad = context.builder->CreateLoad(getIRType(array, context), var);
context.builder->CreateCall(context.freeFunc, arrLoad);
context.builder->CreateStore(Constant::getNullValue(getIRType(array->type, context)), var);
context.builder->CreateStore(Constant::getNullValue(var->getType()), var);
return;
}
std::vector<Value *> sizes;
Expand Down Expand Up @@ -647,6 +647,7 @@ namespace Slangc {
return nullptr;
}

// don't use this
void cleanupCurrentScope(CodeGenContext &context, std::vector<ErrorMessage> &errors) {
auto block = context.blocks.back();
for (auto&& local : block->locals) {
Expand All @@ -670,30 +671,30 @@ namespace Slangc {
if (!Context::isBuiltInType(type)) {
auto arg = context.builder->CreateLoad(getIRType(type, context), var);
// make sure arg is not null
auto cmp = context.builder->CreateICmpNE(arg, Constant::getNullValue(getIRType(type, context)));
/*auto cmp = context.builder->CreateICmpNE(arg, Constant::getNullValue(getIRType(type, context)));
auto block = context.builder->GetInsertBlock();
auto func = block->getParent();
auto thenBlock = BasicBlock::Create(*context.llvmContext, "then", func);
auto elseBlock = BasicBlock::Create(*context.llvmContext, "else", func);
auto endBlock = BasicBlock::Create(*context.llvmContext, "end", func);
context.builder->CreateCondBr(cmp, thenBlock, elseBlock);
context.pushBlock(thenBlock);
context.builder->SetInsertPoint(thenBlock);
context.builder->SetInsertPoint(thenBlock);*/

auto destructor = context.module->getFunction(type + "._default_destructor");
if (destructor) context.builder->CreateCall(destructor, arg);
// auto destructor = context.module->getFunction(type + "._default_destructor");
// if (destructor) context.builder->CreateCall(destructor, arg);
context.builder->CreateCall(context.freeFunc, arg);
context.builder->CreateStore(Constant::getNullValue(getIRType(type, context)), var);

context.builder->CreateBr(endBlock);
/*context.builder->CreateBr(endBlock);
context.popBlock();
context.pushBlock(elseBlock);
context.builder->SetInsertPoint(elseBlock);
context.builder->CreateBr(endBlock);
context.popBlock();
context.builder->SetInsertPoint(endBlock);
context.builder->SetInsertPoint(endBlock);*/
}
}
}
5 changes: 4 additions & 1 deletion tests/StdString.sl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module StdString
public method init(String this)(in array[] character str)
//delete this.arrayOfChars;
variable-integer capacity := strlen(str);
delete this.arrayOfChars;
let this.arrayOfChars := new array[capacity + 1] character;
let this.arrayOfChars := strcpy(this.arrayOfChars, str);
//let this.capacity := capacity;
Expand All @@ -104,7 +105,7 @@ module StdString
variable-array[totalLength + 1] character newCharArray;
let newCharArray := strcpy(newCharArray, this.arrayOfChars);
let newCharArray := strcat(newCharArray, str);

delete this.arrayOfChars;
let this.arrayOfChars := new array[totalLength + 1] character;
let this.arrayOfChars := strcpy(this.arrayOfChars, newCharArray);

Expand Down Expand Up @@ -221,6 +222,7 @@ module StdString
let j := j + 1;
end while;
let newCharArray[j] := "\0";
delete this.arrayOfChars;
let this.arrayOfChars := new array[newLength + 1] character;
let this.arrayOfChars := strcpy(this.arrayOfChars, newCharArray);
let this.length := newLength;
Expand Down Expand Up @@ -287,6 +289,7 @@ module StdString
let j := j + 1;
end while;
let newCharArray[j] := "\0";
delete this.arrayOfChars;
let this.arrayOfChars := new array[newLength + 2] character;
let this.arrayOfChars := strcpy(this.arrayOfChars, newCharArray);
let this.length := newLength;
Expand Down
20 changes: 10 additions & 10 deletions tests/runTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ def runTest(self):
f"Building StdStringTests.sl...\n"
f"Building StdString.sl...\n"
f"Building StdMath.sl...\n"
f"StdString.sl(359,35): warning: Implicit conversion from 'character' to 'integer'.\n"
f"StdString.sl(359,12): warning: Implicit conversion from 'integer' to 'character'.\n"
f"StdString.sl(372,8): warning: Implicit conversion from 'real' to 'integer'.\n"
f"StdString.sl(373,37): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(381,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(392,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(396,46): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(396,51): warning: Implicit conversion from 'character' to 'real'.\n"
f"StdString.sl(396,20): warning: Implicit conversion from 'real' to 'character'.\n"
f"StdString.sl(400,45): warning: Implicit conversion from 'integer' to 'real'.\n",
f"StdString.sl(362,35): warning: Implicit conversion from 'character' to 'integer'.\n"
f"StdString.sl(362,12): warning: Implicit conversion from 'integer' to 'character'.\n"
f"StdString.sl(375,8): warning: Implicit conversion from 'real' to 'integer'.\n"
f"StdString.sl(376,37): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(384,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(395,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(399,46): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(399,51): warning: Implicit conversion from 'character' to 'real'.\n"
f"StdString.sl(399,20): warning: Implicit conversion from 'real' to 'character'.\n"
f"StdString.sl(403,45): warning: Implicit conversion from 'integer' to 'real'.\n",
expected_program_output)
test.compile_test()
self.assertEqual(test.check_outs(), True)
Expand Down

0 comments on commit aece62f

Please sign in to comment.