Skip to content

Commit

Permalink
Cleanup runtime fwd decls and use ABI functiontype rewriting
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Engelen committed Nov 21, 2015
1 parent 417e3d2 commit 554d0a2
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 565 deletions.
2 changes: 1 addition & 1 deletion driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ int main(int argc, char **argv) {
emitJson(modules);
}

LLVM_D_FreeRuntime();
freeRuntime();
llvm::llvm_shutdown();

if (global.errors) {
Expand Down
10 changes: 5 additions & 5 deletions gen/aa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ DValue *DtoAAIndex(Loc &loc, Type *type, DValue *aa, DValue *key, bool lvalue) {
// extern(C) void* _aaInX(AA aa*, TypeInfo keyti, void* pkey)

// first get the runtime function
llvm::Function *func = LLVM_D_GetRuntimeFunction(
llvm::Function *func = getRuntimeFunction(
loc, gIR->module, lvalue ? "_aaGetY" : "_aaInX");
LLFunctionType *funcTy = func->getFunctionType();

Expand Down Expand Up @@ -92,7 +92,7 @@ DValue *DtoAAIndex(Loc &loc, Type *type, DValue *aa, DValue *key, bool lvalue) {
gIR->scope() = IRScope(failbb);

llvm::Function *errorfn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraybounds");
getRuntimeFunction(loc, gIR->module, "_d_arraybounds");
gIR->CreateCallOrInvoke(
errorfn, DtoModuleFileName(gIR->func()->decl->getModule(), loc),
DtoConstUint(loc.linnum));
Expand All @@ -118,7 +118,7 @@ DValue *DtoAAIn(Loc &loc, Type *type, DValue *aa, DValue *key) {
// extern(C) void* _aaInX(AA aa*, TypeInfo keyti, void* pkey)

// first get the runtime function
llvm::Function *func = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_aaInX");
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaInX");
LLFunctionType *funcTy = func->getFunctionType();

IF_LOG Logger::cout() << "_aaIn = " << *func << '\n';
Expand Down Expand Up @@ -164,7 +164,7 @@ DValue *DtoAARemove(Loc &loc, DValue *aa, DValue *key) {
// extern(C) bool _aaDelX(AA aa, TypeInfo keyti, void* pkey)

// first get the runtime function
llvm::Function *func = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_aaDelX");
llvm::Function *func = getRuntimeFunction(loc, gIR->module, "_aaDelX");
LLFunctionType *funcTy = func->getFunctionType();

IF_LOG Logger::cout() << "_aaDel = " << *func << '\n';
Expand Down Expand Up @@ -198,7 +198,7 @@ LLValue *DtoAAEquals(Loc &loc, TOK op, DValue *l, DValue *r) {
assert(t == r->getType()->toBasetype() &&
"aa equality is only defined for aas of same type");
llvm::Function *func =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_aaEqual");
getRuntimeFunction(loc, gIR->module, "_aaEqual");
LLFunctionType *funcTy = func->getFunctionType();

LLValue *aaval = DtoBitCast(l->getRVal(), funcTy->getParamType(1));
Expand Down
30 changes: 15 additions & 15 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static void copySlice(Loc &loc, LLValue *dstarr, LLValue *sz1, LLValue *srcarr,
global.params.useAssert || gIR->emitArrayBoundsChecks();
if (checksEnabled && !knownInBounds) {
LLValue *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_array_slice_copy");
getRuntimeFunction(loc, gIR->module, "_d_array_slice_copy");
gIR->CreateCallOrInvoke(fn, dstarr, sz1, srcarr, sz2);
} else {
// We might have dstarr == srcarr at compile time, but as long as
Expand Down Expand Up @@ -285,15 +285,15 @@ void DtoArrayAssign(Loc &loc, DValue *lhs, DValue *rhs, int op,
}
} else if (isConstructing) {
LLFunction *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arrayctor");
getRuntimeFunction(loc, gIR->module, "_d_arrayctor");
LLCallSite call = gIR->CreateCallOrInvoke(fn, DtoTypeInfoOf(elemType),
DtoSlice(rhsPtr, rhsLength),
DtoSlice(lhsPtr, lhsLength));
call.setCallingConv(llvm::CallingConv::C);
} else // assigning
{
LLValue *tmpSwap = DtoAlloca(elemType, "arrayAssign.tmpSwap");
LLFunction *fn = LLVM_D_GetRuntimeFunction(
LLFunction *fn = getRuntimeFunction(
loc, gIR->module,
!canSkipPostblit ? "_d_arrayassign_l" : "_d_arrayassign_r");
LLCallSite call = gIR->CreateCallOrInvoke(
Expand All @@ -316,7 +316,7 @@ void DtoArrayAssign(Loc &loc, DValue *lhs, DValue *rhs, int op,
LLValue *actualLength = gIR->ir->CreateExactUDiv(lhsSize, rhsSize);
DtoArrayInit(loc, actualPtr, actualLength, rhs, op);
} else {
LLFunction *fn = LLVM_D_GetRuntimeFunction(
LLFunction *fn = getRuntimeFunction(
loc, gIR->module,
isConstructing ? "_d_arraysetctor" : "_d_arraysetassign");
LLCallSite call = gIR->CreateCallOrInvoke(
Expand Down Expand Up @@ -625,7 +625,7 @@ DSliceValue *DtoNewDynArray(Loc &loc, Type *arrayType, DValue *dim,
const char *fnname = defaultInit
? (zeroInit ? "_d_newarrayT" : "_d_newarrayiT")
: "_d_newarrayU";
LLFunction *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fnname);
LLFunction *fn = getRuntimeFunction(loc, gIR->module, fnname);

// call allocator
LLValue *newArray =
Expand Down Expand Up @@ -653,7 +653,7 @@ DSliceValue *DtoNewMulDimDynArray(Loc &loc, Type *arrayType, DValue **dims,
// get runtime function
const char *fnname =
vtype->isZeroInit() ? "_d_newarraymTX" : "_d_newarraymiTX";
LLFunction *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fnname);
LLFunction *fn = getRuntimeFunction(loc, gIR->module, fnname);

// Check if constant
bool allDimsConst = true;
Expand Down Expand Up @@ -720,7 +720,7 @@ DSliceValue *DtoResizeDynArray(Loc &loc, Type *arrayType, DValue *array,
bool zeroInit = arrayType->toBasetype()->nextOf()->isZeroInit();

// call runtime
LLFunction *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module,
LLFunction *fn = getRuntimeFunction(loc, gIR->module,
zeroInit ? "_d_arraysetlengthT"
: "_d_arraysetlengthiT");

Expand Down Expand Up @@ -750,7 +750,7 @@ void DtoCatAssignElement(Loc &loc, Type *arrayType, DValue *array,
DValue *expVal = toElem(exp);

LLFunction *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arrayappendcTX");
getRuntimeFunction(loc, gIR->module, "_d_arrayappendcTX");
LLValue *appendedArray =
gIR->CreateCallOrInvoke(
fn, DtoTypeInfoOf(arrayType),
Expand All @@ -774,7 +774,7 @@ DSliceValue *DtoCatAssignArray(Loc &loc, DValue *arr, Expression *exp) {
Type *arrayType = arr->getType();

LLFunction *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arrayappendT");
getRuntimeFunction(loc, gIR->module, "_d_arrayappendT");
// Call _d_arrayappendT(TypeInfo ti, byte[] *px, byte[] y)
LLValue *newArray =
gIR->CreateCallOrInvoke(
Expand All @@ -799,7 +799,7 @@ DSliceValue *DtoCatArrays(Loc &loc, Type *arrayType, Expression *exp1,
LLFunction *fn = nullptr;

if (exp1->op == TOKcat) { // handle multiple concat
fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraycatnTX");
fn = getRuntimeFunction(loc, gIR->module, "_d_arraycatnTX");

// Create array of slices
typedef llvm::SmallVector<llvm::Value *, 16> ArgVector;
Expand Down Expand Up @@ -839,7 +839,7 @@ DSliceValue *DtoCatArrays(Loc &loc, Type *arrayType, Expression *exp1,
// byte[][] arrs
args.push_back(val);
} else {
fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_arraycatT");
fn = getRuntimeFunction(loc, gIR->module, "_d_arraycatT");

// TypeInfo ti
args.push_back(DtoTypeInfoOf(arrayType));
Expand Down Expand Up @@ -867,7 +867,7 @@ DSliceValue *DtoAppendDChar(Loc &loc, DValue *arr, Expression *exp,
DValue *valueToAppend = toElem(exp);

// Prepare arguments
LLFunction *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, func);
LLFunction *fn = getRuntimeFunction(loc, gIR->module, func);

// Call function (ref string x, dchar c)
LLValue *newArray =
Expand Down Expand Up @@ -904,7 +904,7 @@ DSliceValue *DtoAppendDCharToUnicodeString(Loc &loc, DValue *arr,
static LLValue *DtoArrayEqCmp_impl(Loc &loc, const char *func, DValue *l,
DValue *r, bool useti) {
IF_LOG Logger::println("comparing arrays");
LLFunction *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, func);
LLFunction *fn = getRuntimeFunction(loc, gIR->module, func);
assert(fn);

// find common dynamic array type
Expand Down Expand Up @@ -981,7 +981,7 @@ LLValue *DtoArrayCastLength(Loc &loc, LLValue *len, LLType *elemty,
}

LLFunction *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_array_cast_len");
getRuntimeFunction(loc, gIR->module, "_d_array_cast_len");
return gIR->CreateCallOrInvoke(fn, len,
LLConstantInt::get(DtoSize_t(), esz, false),
LLConstantInt::get(DtoSize_t(), nsz, false))
Expand Down Expand Up @@ -1213,7 +1213,7 @@ void DtoIndexBoundsCheck(Loc &loc, DValue *arr, DValue *index) {

void DtoBoundsCheckFailCall(IRState *irs, Loc &loc) {
llvm::Function *errorfn =
LLVM_D_GetRuntimeFunction(loc, irs->module, "_d_arraybounds");
getRuntimeFunction(loc, irs->module, "_d_arraybounds");
irs->CreateCallOrInvoke(
errorfn, DtoModuleFileName(irs->func()->decl->getModule(), loc),
DtoConstUint(loc.linnum));
Expand Down
8 changes: 4 additions & 4 deletions gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ DValue *DtoNewClass(Loc &loc, TypeClass *tc, NewExp *newexp) {
// default allocator
else {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_newclass");
getRuntimeFunction(loc, gIR->module, "_d_newclass");
LLConstant *ci = DtoBitCast(getIrAggr(tc->sym)->getClassInfoSymbol(),
DtoType(Type::typeinfoclass->type));
mem =
Expand Down Expand Up @@ -186,7 +186,7 @@ void DtoInitClass(TypeClass *tc, LLValue *dst) {
void DtoFinalizeClass(Loc &loc, LLValue *inst) {
// get runtime function
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_callfinalizer");
getRuntimeFunction(loc, gIR->module, "_d_callfinalizer");

gIR->CreateCallOrInvoke(
fn, DtoBitCast(inst, fn->getFunctionType()->getParamType(0)), "");
Expand Down Expand Up @@ -330,7 +330,7 @@ DValue *DtoDynamicCastObject(Loc &loc, DValue *val, Type *_to) {
DtoResolveClass(Type::typeinfoclass);

llvm::Function *func =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_dynamic_cast");
getRuntimeFunction(loc, gIR->module, "_d_dynamic_cast");
LLFunctionType *funcTy = func->getFunctionType();

// Object o
Expand Down Expand Up @@ -368,7 +368,7 @@ DValue *DtoDynamicCastInterface(Loc &loc, DValue *val, Type *_to) {
DtoResolveClass(Type::typeinfoclass);

llvm::Function *func =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_interface_cast");
getRuntimeFunction(loc, gIR->module, "_d_interface_cast");
LLFunctionType *funcTy = func->getFunctionType();

// void* p
Expand Down
22 changes: 14 additions & 8 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,8 @@ void DtoResolveFunction(FuncDeclaration *fdecl) {

////////////////////////////////////////////////////////////////////////////////

static void set_param_attrs(TypeFunction *f, llvm::Function *func,
FuncDeclaration *fdecl) {
IrFuncTy &irFty = getIrFunc(fdecl)->irFty;
AttrSet newAttrs = AttrSet::extractFunctionAndReturnAttributes(func);
AttrSet getParamAttrs(TypeFunction *f, IrFuncTy &irFty) {
AttrSet newAttrs;

int idx = 0;

Expand Down Expand Up @@ -414,7 +412,13 @@ static void set_param_attrs(TypeFunction *f, llvm::Function *func,
newAttrs.add(i, irFty.args[k]->attrs);
}

// Store the final attribute set
return newAttrs;
}

void applyParamAttrsToLLFunc(TypeFunction *f, IrFuncTy &irFty,
llvm::Function *func) {
AttrSet newAttrs = AttrSet::extractFunctionAndReturnAttributes(func);
newAttrs.merge(getParamAttrs(f, irFty));
func->setAttributes(newAttrs);
}

Expand Down Expand Up @@ -499,7 +503,7 @@ void DtoDeclareFunction(FuncDeclaration *fdecl) {

// parameter attributes
if (!DtoIsIntrinsic(fdecl)) {
set_param_attrs(f, func, fdecl);
applyParamAttrsToLLFunc(f, getIrFunc(fdecl)->irFty, func);
if (global.params.disableRedZone) {
func->addFnAttr(LLAttribute::NoRedZone);
}
Expand Down Expand Up @@ -735,7 +739,8 @@ void DtoDefineFunction(FuncDeclaration *fd) {
}
}

// if this function is naked, we take over right away! no standard processing!
// if this function is naked, we take over right away! no standard
// processing!
if (fd->naked) {
DtoDefineNakedFunction(fd);
return;
Expand Down Expand Up @@ -927,7 +932,8 @@ void DtoDefineFunction(FuncDeclaration *fd) {
// is the last statement in a function)
bb->eraseFromParent();
} else if (!gIR->scopereturned()) {
// llvm requires all basic blocks to end with a TerminatorInst but DMD does
// llvm requires all basic blocks to end with a TerminatorInst but DMD
// does
// not put a return statement
// in automatically, so we do it here.

Expand Down
4 changes: 4 additions & 0 deletions gen/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@

#include "mars.h"

class AttrSet;
class DValue;
class Expression;
class FuncDeclaration;
struct IRAsmBlock;
struct IrFuncTy;
class Parameter;
class Type;
class TypeFunction;
namespace llvm {
class FunctionType;
class Value;
}

AttrSet getParamAttrs(TypeFunction *f, IrFuncTy &irFty);

llvm::FunctionType *DtoFunctionType(Type *t, IrFuncTy &irFty, Type *thistype,
Type *nesttype, bool isMain = false,
bool isCtor = false,
Expand Down
18 changes: 9 additions & 9 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Type *getTypeInfoType(Type *t, Scope *sc);
LLValue *DtoNew(Loc &loc, Type *newtype) {
// get runtime function
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_allocmemoryT");
getRuntimeFunction(loc, gIR->module, "_d_allocmemoryT");
// get type info
LLConstant *ti = DtoTypeInfoOf(newtype);
assert(isaPointer(ti));
Expand All @@ -74,7 +74,7 @@ LLValue *DtoNew(Loc &loc, Type *newtype) {
}

LLValue *DtoNewStruct(Loc &loc, TypeStruct *newtype) {
llvm::Function *fn = LLVM_D_GetRuntimeFunction(
llvm::Function *fn = getRuntimeFunction(
loc, gIR->module,
newtype->isZeroInit(newtype->sym->loc) ? "_d_newitemT" : "_d_newitemiT");
LLConstant *ti = DtoTypeInfoOf(newtype);
Expand All @@ -84,15 +84,15 @@ LLValue *DtoNewStruct(Loc &loc, TypeStruct *newtype) {

void DtoDeleteMemory(Loc &loc, DValue *ptr) {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_delmemory");
getRuntimeFunction(loc, gIR->module, "_d_delmemory");
LLValue *lval = (ptr->isLVal() ? ptr->getLVal() : makeLValue(loc, ptr));
gIR->CreateCallOrInvoke(
fn, DtoBitCast(lval, fn->getFunctionType()->getParamType(0)));
}

void DtoDeleteStruct(Loc &loc, DValue *ptr) {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_delstruct");
getRuntimeFunction(loc, gIR->module, "_d_delstruct");
LLValue *lval = (ptr->isLVal() ? ptr->getLVal() : makeLValue(loc, ptr));
gIR->CreateCallOrInvoke(
fn, DtoBitCast(lval, fn->getFunctionType()->getParamType(0)),
Expand All @@ -102,23 +102,23 @@ void DtoDeleteStruct(Loc &loc, DValue *ptr) {

void DtoDeleteClass(Loc &loc, DValue *inst) {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_delclass");
getRuntimeFunction(loc, gIR->module, "_d_delclass");
LLValue *lval = (inst->isLVal() ? inst->getLVal() : makeLValue(loc, inst));
gIR->CreateCallOrInvoke(
fn, DtoBitCast(lval, fn->getFunctionType()->getParamType(0)));
}

void DtoDeleteInterface(Loc &loc, DValue *inst) {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_delinterface");
getRuntimeFunction(loc, gIR->module, "_d_delinterface");
LLValue *lval = (inst->isLVal() ? inst->getLVal() : makeLValue(loc, inst));
gIR->CreateCallOrInvoke(
fn, DtoBitCast(lval, fn->getFunctionType()->getParamType(0)));
}

void DtoDeleteArray(Loc &loc, DValue *arr) {
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_delarray_t");
getRuntimeFunction(loc, gIR->module, "_d_delarray_t");
llvm::FunctionType *fty = fn->getFunctionType();

// the TypeInfo argument must be null if the type has no dtor
Expand Down Expand Up @@ -183,7 +183,7 @@ llvm::AllocaInst *DtoRawAlloca(LLType *lltype, size_t alignment,
LLValue *DtoGcMalloc(Loc &loc, LLType *lltype, const char *name) {
// get runtime function
llvm::Function *fn =
LLVM_D_GetRuntimeFunction(loc, gIR->module, "_d_allocmemory");
getRuntimeFunction(loc, gIR->module, "_d_allocmemory");
// parameters
LLValue *size = DtoConstSize_t(getTypeAllocSize(lltype));
// call runtime allocator
Expand Down Expand Up @@ -232,7 +232,7 @@ LLValue *DtoAllocaDump(LLValue *val, LLType *asType, int alignment,
void DtoAssert(Module *M, Loc &loc, DValue *msg) {
// func
const char *fname = msg ? "_d_assert_msg" : "_d_assert";
llvm::Function *fn = LLVM_D_GetRuntimeFunction(loc, gIR->module, fname);
llvm::Function *fn = getRuntimeFunction(loc, gIR->module, fname);

// Arguments
llvm::SmallVector<LLValue *, 3> args;
Expand Down
Loading

0 comments on commit 554d0a2

Please sign in to comment.