Skip to content

Commit

Permalink
don't malloc string literals in output, free formatted strings in output
Browse files Browse the repository at this point in the history
  • Loading branch information
f0xeri committed Mar 10, 2024
1 parent 6766f41 commit 553ef57
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 25 deletions.
41 changes: 27 additions & 14 deletions codegen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace Slangc {

auto needed = context.builder->CreateCall(snprintfFunc, args);
auto neededPlusOne = context.builder->CreateAdd(needed, ConstantInt::get(Type::getInt32Ty(*context.llvmContext), 1));
auto buffer = context.builder->CreateMalloc(Type::getInt32Ty(*context.llvmContext), Type::getInt8Ty(*context.llvmContext), neededPlusOne, context.mallocFunc);
auto buffer = context.builder->CreateMalloc(Type::getInt32Ty(*context.llvmContext), Type::getInt8Ty(*context.llvmContext), neededPlusOne, nullptr, context.mallocFunc);
args[0] = buffer;
args[1] = context.builder->CreateIntCast(neededPlusOne, Type::getInt64Ty(*context.llvmContext), false);
auto call = context.builder->CreateCall(snprintfFunc, args);
Expand Down Expand Up @@ -157,7 +157,7 @@ namespace Slangc {
bool isFunc = false;

if (auto localVar = context.localsDeclsLookup(name)) {
if (context.refferencing) {
if (context.referencing) {
context.setReferenced(name, true);
}
declType = getDeclType(localVar.value(), context.context, errors);
Expand Down Expand Up @@ -463,9 +463,9 @@ namespace Slangc {
context.loadValue = false;
context.currentFuncSignature = std::get<FuncExprPtr>(getExprType(left, context.context, errors).value());
}
context.refferencing = true;
context.referencing = true;
auto rightVal = processNode(right, context, errors);
context.refferencing = false;
context.referencing = false;
// if right is new, clean up left
/*if (auto newExpr = std::get_if<NewExprPtr>(&right)) {
if (auto arr = std::get_if<ArrayExprPtr>(&newExpr->get()->type)) {
Expand Down Expand Up @@ -496,10 +496,10 @@ namespace Slangc {
if (std::holds_alternative<TypeExprPtr>(expr) && std::get<TypeExprPtr>(expr)->type == "void")
return context.builder->CreateRetVoid();
context.loadValue = true;
context.refferencing = true;
context.referencing = true;
auto type = getExprType(expr, context.context, errors).value();
auto val = processNode(expr, context, errors);
context.refferencing = false;
context.referencing = false;
context.loadValue = false;
val = typeCast(val, context.currentReturnType, context, errors, getExprLoc(expr));
// cleanupCurrentScope(context, errors);
Expand All @@ -510,9 +510,6 @@ namespace Slangc {
if (context.debug) context.debugBuilder->emitLocation(loc);
auto printfFunc = context.module->getOrInsertFunction("printf", FunctionType::get(Type::getInt32Ty(*context.llvmContext), PointerType::get(Type::getInt8Ty(*context.llvmContext), 0), true));
auto temp = context.loadValue;
context.loadValue = true;
auto val = processNode(expr, context, errors);
context.loadValue = temp;
bool charArray = false;
auto exprType = getExprType(expr, context.context, errors).value();
if (auto arr = std::get_if<ArrayExprPtr>(&exprType)) {
Expand All @@ -522,6 +519,15 @@ namespace Slangc {
}
}
}
Value* val = nullptr;
// we don't want to malloc string in output statement
if (auto strLiteral = std::get_if<StringExprPtr>(&expr))
val = context.builder->CreateGlobalStringPtr(strLiteral->get()->value, "", 0, context.module.get());
else {
context.loadValue = true;
val = processNode(expr, context, errors);
context.loadValue = temp;
}
if (auto arr = std::get_if<StringExprPtr>(&expr)) charArray = true;

std::vector<Value*> printArgs;
Expand Down Expand Up @@ -551,7 +557,14 @@ namespace Slangc {
}
printArgs.push_back(formatStr);
printArgs.push_back(val);
return context.builder->CreateCall(printfFunc, printArgs);
auto printfCall = context.builder->CreateCall(printfFunc, printArgs);

// deallocate formatted string
if (auto fmtString = std::get_if<FormattedStringExprPtr>(&expr)) {
context.builder->CreateCall(context.freeFunc, val);
}

return printfCall;
}

auto InputStatementNode::codegen(CodeGenContext &context, std::vector<ErrorMessage>& errors) -> Value* {
Expand Down Expand Up @@ -614,9 +627,9 @@ namespace Slangc {
var = context.builder->CreateAlloca(type, nullptr, name);
if (expr.has_value()) {
context.loadValue = true;
context.refferencing = true;
context.referencing = true;
rightVal = processNode(expr.value(), context, errors);
context.refferencing = false;
context.referencing = false;
context.loadValue = false;
// TODO: do type cast
if (rightVal->getType() != type) {
Expand Down Expand Up @@ -747,9 +760,9 @@ namespace Slangc {
}
if (assignExpr.has_value()) {
context.loadValue = true;
context.refferencing = true;
context.referencing = true;
auto assignVal = processNode(assignExpr.value(), context, errors);
context.refferencing = false;
context.referencing = false;
context.loadValue = false;
context.builder->CreateStore(assignVal, var);
//context.referenced()[name] = true; // if string literal assigned, we should not try to clear this variable
Expand Down
2 changes: 1 addition & 1 deletion codegen/CodeGenContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace Slangc {
LoadInst* currentTypeLoad = nullptr;
std::optional<FuncExprPtr> currentFuncSignature = std::nullopt;
Type* currentReturnType = nullptr;
bool refferencing = false;
bool referencing = false;
bool loadValue = false;
bool currentDeclImported = false;
bool debug = true;
Expand Down
1 change: 1 addition & 0 deletions examples/StdString.sl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ module StdString
end while;
let newCharArray[j] := "\0";
call result.init(newCharArray);
delete newCharArray;
return result;
end substr;

Expand Down
1 change: 1 addition & 0 deletions tests/StdString.sl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ module StdString
end while;
let newCharArray[j] := "\0";
call result.init(newCharArray);
delete newCharArray;
return result;
end substr;

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(364,35): warning: Implicit conversion from 'character' to 'integer'.\n"
f"StdString.sl(364,12): warning: Implicit conversion from 'integer' to 'character'.\n"
f"StdString.sl(377,8): warning: Implicit conversion from 'real' to 'integer'.\n"
f"StdString.sl(378,37): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(386,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(398,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(402,46): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(402,51): warning: Implicit conversion from 'character' to 'real'.\n"
f"StdString.sl(402,20): warning: Implicit conversion from 'real' to 'character'.\n"
f"StdString.sl(406,45): warning: Implicit conversion from 'integer' to 'real'.\n",
f"StdString.sl(365,35): warning: Implicit conversion from 'character' to 'integer'.\n"
f"StdString.sl(365,12): warning: Implicit conversion from 'integer' to 'character'.\n"
f"StdString.sl(378,8): warning: Implicit conversion from 'real' to 'integer'.\n"
f"StdString.sl(379,37): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(387,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(399,41): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(403,46): warning: Implicit conversion from 'integer' to 'real'.\n"
f"StdString.sl(403,51): warning: Implicit conversion from 'character' to 'real'.\n"
f"StdString.sl(403,20): warning: Implicit conversion from 'real' to 'character'.\n"
f"StdString.sl(407,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 553ef57

Please sign in to comment.