Skip to content

Commit

Permalink
boehm gc
Browse files Browse the repository at this point in the history
  • Loading branch information
f0xeri committed Mar 7, 2024
1 parent 44a1a55 commit 25db653
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ include_directories(${CMAKE_SOURCE_DIR})
if(CMAKE_BUILD_TYPE MATCHES "Debug")
message("Building in DEBUG mode.")
#set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(LLVM_DIR "D:\\llvm17debug\\lib\\cmake\\llvm")
#set(LLVM_DIR "C:\\llvm-18-debug\\lib\\cmake\\llvm")
#set(LLVM_DIR "D:\\llvm17debug\\lib\\cmake\\llvm")
set(LLVM_DIR "C:\\llvm-18-debug\\lib\\cmake\\llvm")
find_package(LLVM REQUIRED CONFIG)
message(${LLVM_INCLUDE_DIRS})
endif()
Expand All @@ -23,8 +23,8 @@ if(CMAKE_BUILD_TYPE MATCHES "Release")
message("Building in RELEASE mode.")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(CMAKE_BUILD_TYPE Release)
set(LLVM_DIR "D:\\Program Files (x86)\\LLVM\\lib\\cmake\\llvm")
#set(LLVM_DIR "C:\\llvm-18-release\\lib\\cmake\\llvm")
#set(LLVM_DIR "D:\\Program Files (x86)\\LLVM\\lib\\cmake\\llvm")
set(LLVM_DIR "C:\\llvm-18-release\\lib\\cmake\\llvm")
find_package(LLVM REQUIRED CONFIG)
message(${LLVM_INCLUDE_DIRS})
#add_definitions(-DDEBUG=0)
Expand Down
15 changes: 5 additions & 10 deletions codegen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,7 @@ namespace Slangc {

auto needed = context.builder->CreateCall(snprintfFunc, args);
auto neededPlusOne = context.builder->CreateAdd(needed, ConstantInt::get(Type::getInt32Ty(*context.llvmContext), 1));
auto mallocFunc = context.module->getOrInsertFunction(
"malloc",
FunctionType::get(
PointerType::get(Type::getInt8Ty(*context.llvmContext), 0),
Type::getInt32Ty(*context.llvmContext),
false
)
);
auto buffer = context.builder->CreateCall(mallocFunc, neededPlusOne);
auto buffer = context.builder->CreateMalloc(Type::getInt32Ty(*context.llvmContext), Type::getInt8Ty(*context.llvmContext), neededPlusOne, 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 @@ -410,7 +402,10 @@ namespace Slangc {
context.loadValue = true; // TODO: not sure if it's correct
auto var = processNode(expr, context, errors);
context.loadValue = false;
return context.builder->CreateFree(var);
if (context.gcEnabled)
return context.builder->CreateCall(context.freeFunc, var);
else
return context.builder->CreateFree(var);
}

auto BlockStmtNode::codegen(CodeGenContext &context, std::vector<ErrorMessage>& errors) -> Value* {
Expand Down
4 changes: 2 additions & 2 deletions codegen/CodeGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Slangc {
}
class CodeGen {
public:
CodeGen(Context& context, ModuleDeclPtr&& moduleAST, bool isMainModule) : context(context),
CodeGen(Context& context, ModuleDeclPtr&& moduleAST, bool isMainModule, bool debug, bool gcEnabled) : context(context),
moduleAST(std::move(moduleAST)), isMainModule(isMainModule),
codeGenContext(context) {}
codeGenContext(context, debug, gcEnabled) {}

void process(std::vector<ErrorMessage>& errors) {

Expand Down
32 changes: 29 additions & 3 deletions codegen/CodeGenContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ namespace Slangc {
bool loadValue = false;
bool currentDeclImported = false;
bool debug = true;

explicit CodeGenContext(Context &context) : context(context) {
bool gcEnabled = true;
Function* mallocFunc = nullptr;
Function* freeFunc = nullptr;
explicit CodeGenContext(Context &context, bool debug, bool gcEnabled) : context(context), debug(debug), gcEnabled(gcEnabled) {
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllTargetMCs();
Expand Down Expand Up @@ -109,12 +111,31 @@ namespace Slangc {
module->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);
}

std::string mallocName = "malloc";
std::string freeName = "free";
if (gcEnabled) {
mallocName = "GC_malloc";
freeName = "GC_free";
}
mallocFunc = Function::Create(FunctionType::get(
PointerType::get(Type::getInt8Ty(*llvmContext), 0),
Type::getInt32Ty(*llvmContext),
false
), GlobalValue::ExternalLinkage, mallocName, module.get());
mallocFunc->setCallingConv(CallingConv::C);
freeFunc = Function::Create(FunctionType::get(
Type::getVoidTy(*llvmContext),
PointerType::get(Type::getInt8Ty(*llvmContext), 0),
false
), GlobalValue::ExternalLinkage, freeName, module.get());
freeFunc->setCallingConv(CallingConv::C);

}
~CodeGenContext() = default;

auto startMainFunc() -> llvm::Function* {
auto mainFuncType = llvm::FunctionType::get(Type::getInt32Ty(*llvmContext), false);
auto mainFunc = llvm::Function::Create(mainFuncType, Function::ExternalLinkage, "main", module.get());
auto mainFunc = Function::Create(mainFuncType, Function::ExternalLinkage, "main", module.get());
auto mainBlock = BasicBlock::Create(*llvmContext, "entry", mainFunc);
builder->SetInsertPoint(mainBlock);
pushBlock(mainBlock);
Expand All @@ -123,6 +144,11 @@ namespace Slangc {
mainFunc->setSubprogram(dbgFunc);
debugBuilder->lexicalBlocks.push_back(dbgFunc);
}
if (gcEnabled) {
if (debug) debugBuilder->emitLocation();
auto gcInitFunc = module->getOrInsertFunction("GC_init", FunctionType::get(Type::getVoidTy(*llvmContext), false));
builder->CreateCall(gcInitFunc, {});
}
return mainFunc;
}

Expand Down
6 changes: 3 additions & 3 deletions codegen/CodeGenHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ namespace Slangc {
auto mallocCall = context.builder->CreateMalloc(intType, structType, ConstantInt::get(intType,
context.module->getDataLayout().getTypeAllocSize(
structType)),
nullptr);
nullptr, context.mallocFunc);
context.builder->CreateStore(mallocCall, var);
return var;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ namespace Slangc {
context.module->getDataLayout().getTypeAllocSize(
getIRType(array->type,
context))));
auto mallocCall = context.builder->CreateMalloc(intType, arrayType, allocSize, nullptr);
auto mallocCall = context.builder->CreateMalloc(intType, arrayType, allocSize, nullptr, context.mallocFunc);

auto arrLoad = context.builder->CreateLoad(loadArrType, var);
Value *arrPtr = nullptr;
Expand Down Expand Up @@ -279,7 +279,7 @@ namespace Slangc {
auto arraySize = processNode(array->size, context, errors);
context.loadValue = temp;
auto allocSize = context.builder->CreateMul(arraySize,ConstantInt::get(intType,context.module->getDataLayout().getTypeAllocSize(structType)));
auto mallocCall = context.builder->CreateMalloc(intType, structType, allocSize, nullptr);
auto mallocCall = context.builder->CreateMalloc(intType, structType, allocSize, nullptr, context.mallocFunc);
auto indicesCount = array->getIndicesCount();
context.builder->CreateStore(mallocCall, var);
callArrayElementsConstructors(array, var, arraySize, context, errors);
Expand Down
6 changes: 4 additions & 2 deletions driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ namespace Slangc {
#endif
}
clangCallStream << " -o " << (options.getOutputFilePath().empty() ? outputFilename : options.getOutputFilePath().string());
system(clangCallStream.str().c_str());
if (options.isGCEnabled())
clangCallStream << " -lgc";
system(clangCallStream.str().c_str());
}

std::unique_ptr<Context> Driver::processUnit(std::filesystem::path &filepath, bool isMainModule) {
Expand All @@ -53,7 +55,7 @@ namespace Slangc {
parser.parse();

Check::checkAST(parser.moduleAST, *context, errors);
auto codeGen = CodeGen(*context, std::move(parser.moduleAST), isMainModule);
auto codeGen = CodeGen(*context, std::move(parser.moduleAST), isMainModule, options.isDebug(), options.isGCEnabled());
if (!containsErrors(errors))
codeGen.process(errors);
else
Expand Down

0 comments on commit 25db653

Please sign in to comment.