1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Fixed dynamic Beef libs on Linux/macOS

This commit is contained in:
Brian Fiete 2020-06-30 12:13:20 -07:00
parent 455a0d0b46
commit 6e5b6694a1
10 changed files with 139 additions and 34 deletions

View file

@ -2318,6 +2318,12 @@ void BeIRCodeGen::HandleNextCmd()
else if (attribute == BFIRAttribute_NoRecurse)
{
}
else if (attribute == BFIRAttribute_Constructor)
{
}
else if (attribute == BFIRAttribute_Destructor)
{
}
else
BF_FATAL("Unhandled");
}

View file

@ -735,7 +735,7 @@ BfIRFunction BfCompiler::CreateLoadSharedLibraries(BfVDataModule* bfModule, Arra
bfModule->GetDefaultValue(bfModule->GetPrimitiveType(BfTypeCode_NullPtr)), dllHandleName);
BfIRValue namePtr = bfModule->GetStringCharPtr(strNum);
SmallVector<BfIRValue, 1> args;
SmallVector<BfIRValue, 2> args;
args.push_back(namePtr);
args.push_back(dllHandleVar);
BfIRValue dllHandleValue = bfModule->mBfIRBuilder->CreateCall(loadSharedLibraryProc.mFunc, args);
@ -1624,6 +1624,8 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
if ((targetType == BfTargetType_BeefWindowsApplication) && (mOptions.mPlatformType != BfPlatformType_Windows))
targetType = BfTargetType_BeefConsoleApplication;
bool isPosixDynLib = (targetType == BfTargetType_BeefDynLib) && (mOptions.mPlatformType != BfPlatformType_Windows);
// Generate "main"
if (!IsHotCompile())
{
@ -1677,7 +1679,8 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("entry", true);
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
#ifndef BF_PLATFORM_WINDOWS
if ((mOptions.mPlatformType != BfPlatformType_Windows) &&
((targetType == BfTargetType_BeefConsoleApplication) || (targetType == BfTargetType_BeefTest)))
{
SmallVector<BfIRType, 2> paramTypes;
paramTypes.push_back(int32Type);
@ -1692,7 +1695,6 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
args.push_back(bfModule->mBfIRBuilder->GetArgument(1));
bfModule->mBfIRBuilder->CreateCall(setCmdLineFunc, args);
}
#endif
BfIRBlock initSkipBlock;
if (targetType == BfTargetType_BeefDynLib)
@ -1949,6 +1951,37 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
BfIRMDNode dbgArrayType = bfModule->mBfIRBuilder->DbgCreateArrayType(dataSize * 8, 8, bfModule->mBfIRBuilder->DbgGetType(int8Type), dataSize);
bfModule->mBfIRBuilder->DbgCreateGlobalVariable(bfModule->mDICompileUnit, name, name, NULL, 0, dbgArrayType, false, irVal);
}
if (isPosixDynLib)
{
auto voidType = bfModule->mBfIRBuilder->MapType(bfModule->GetPrimitiveType(BfTypeCode_None));
SizedArray<BfIRType, 4> paramTypes;
BfIRFunctionType funcType = bfModule->mBfIRBuilder->CreateFunctionType(voidType, paramTypes);
BfIRFunction func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Startup");
bfModule->mBfIRBuilder->SetActiveFunction(func);
bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Constructor);
auto entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
SmallVector<BfIRValue, 2> startArgs;
startArgs.push_back(bfModule->mBfIRBuilder->CreateConstNull());
startArgs.push_back(bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 1));
startArgs.push_back(bfModule->mBfIRBuilder->CreateConstNull());
bfModule->mBfIRBuilder->CreateCall(mainFunc, startArgs);
bfModule->mBfIRBuilder->CreateRetVoid();
//////////////////////////////////////////////////////////////////////////
func = bfModule->mBfIRBuilder->CreateFunction(funcType, BfIRLinkageType_Internal, "BfDynLib__Shutdown");
bfModule->mBfIRBuilder->Func_AddAttribute(func, -1, BFIRAttribute_Destructor);
bfModule->mBfIRBuilder->SetActiveFunction(func);
entryBlock = bfModule->mBfIRBuilder->CreateBlock("main", true);
bfModule->mBfIRBuilder->SetInsertPoint(entryBlock);
SmallVector<BfIRValue, 2> stopArgs;
startArgs[1] = bfModule->mBfIRBuilder->CreateConst(BfTypeCode_Int32, 0);
bfModule->mBfIRBuilder->CreateCall(mainFunc, startArgs);
bfModule->mBfIRBuilder->CreateRetVoid();
}
}
// Generate "System.GC.MarkAllStaticMembers"

View file

@ -611,7 +611,9 @@ enum BfIRAttribute
BFIRAttribute_NoFramePointerElim,
BFIRAttribute_DllImport,
BFIRAttribute_DllExport,
BFIRAttribute_NoRecurse
BFIRAttribute_NoRecurse,
BFIRAttribute_Constructor,
BFIRAttribute_Destructor,
};
struct BfIRFunctionType

View file

@ -2737,6 +2737,35 @@ void BfIRCodeGen::HandleNextCmd()
{
func->addFnAttr("no-frame-pointer-elim", "true");
}
else if ((attribute == BFIRAttribute_Constructor) || (attribute == BFIRAttribute_Destructor))
{
CmdParamVec<llvm::Type*> members;
members.push_back(llvm::Type::getInt32Ty(*mLLVMContext));
members.push_back(func->getType());
members.push_back(llvm::Type::getInt8PtrTy(*mLLVMContext));
llvm::StructType* structType = llvm::StructType::get(*mLLVMContext, members);
CmdParamVec<llvm::Constant*> structVals;
structVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(*mLLVMContext), 0x7FFFFF00));
structVals.push_back(func);
structVals.push_back(llvm::ConstantPointerNull::get(llvm::Type::getInt8PtrTy(*mLLVMContext)));
auto constStruct = llvm::ConstantStruct::get(structType, structVals);
CmdParamVec<llvm::Constant*> structArrVals;
structArrVals.push_back(constStruct);
auto arrTy = llvm::ArrayType::get(structType, 1);
auto constArr = llvm::ConstantArray::get(arrTy, structArrVals);
auto globalVariable = new llvm::GlobalVariable(
*mLLVMModule,
arrTy,
false,
llvm::GlobalValue::AppendingLinkage,
constArr,
(attribute == BFIRAttribute_Constructor) ? "llvm.global_ctors" : "llvm.global_dtors",
NULL, llvm::GlobalValue::NotThreadLocal);
}
else
func->addAttribute(argIdx, LLVMMapAttribute(attribute));
}

View file

@ -719,7 +719,12 @@ String BfGNUMangler::Mangle(BfMethodInstance* methodInst)
}
mangleContext.mPrefixObjectPointer = true;
String methodName = methodInst->mMethodDef->mName;
StringT<128> methodName = methodInst->mMethodDef->mName;
for (int i = 0; i < (int)methodName.length(); i++)
{
if (methodName[i] == '@')
methodName[i] = '$';
}
if (methodInst->mMethodDef->mIsOperator)
{