Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function pointers/delegates on Harvard architectures #4465

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,16 @@ class CodegenVisitor : public Visitor {
std::string arg = ("/DEFAULTLIB:\"" + name + "\"").str();
gIR->addLinkerOption(llvm::StringRef(arg));
} else {
bool isStaticLib = name.endswith(".a");
const bool isStaticLib = name.endswith(".a");
const size_t nameLen = name.size();

size_t const nameLen = name.size();
size_t const n = nameLen + 3;
char *arg = nullptr;

if (isStaticLib == false) {
if (!isStaticLib) { // name => -lname
const size_t n = nameLen + 3;
arg = static_cast<char *>(mem.xmalloc(n));
arg[0] = '-';
arg[1] = 'l';
memcpy(arg + 2, name.data(), name.size());
memcpy(arg + 2, name.data(), nameLen);
arg[n - 1] = 0;
} else {
arg = static_cast<char *>((mem.xmalloc(nameLen + 1)));
Expand Down
6 changes: 3 additions & 3 deletions gen/tocall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ class ImplicitArgumentsBuilder {

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

static LLValue *DtoCallableValue(llvm::FunctionType * ft,DValue *fn) {
static LLValue *DtoCallableValue(DValue *fn) {
Type *type = fn->type->toBasetype();
if (type->ty == TY::Tfunction) {
return DtoRVal(fn);
Expand All @@ -829,7 +829,7 @@ static LLValue *DtoCallableValue(llvm::FunctionType * ft,DValue *fn) {
LLValue *dg = DtoLVal(fn);
llvm::StructType *st = isaStruct(DtoType(fn->type));
LLValue *funcptr = DtoGEP(st, dg, 0, 1);
return DtoLoad(ft->getPointerTo(), funcptr, ".funcptr");
return DtoLoad(st->getElementType(1), funcptr, ".funcptr");
}
LLValue *dg = DtoRVal(fn);
assert(isaStruct(dg));
Expand Down Expand Up @@ -862,7 +862,7 @@ DValue *DtoCallFunction(const Loc &loc, Type *resulttype, DValue *fnval,
}

// get callee llvm value
LLValue *callable = DtoCallableValue(irFty.funcType, fnval);
LLValue *callable = DtoCallableValue(fnval);
LLFunctionType *callableTy = irFty.funcType;
if (dfnval && dfnval->func->isCsymbol()) {
// See note in DtoDeclareFunction about K&R foward declared (void) functions
Expand Down
7 changes: 6 additions & 1 deletion ir/irtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
assert(!ctype);

LLType *elemType;
unsigned addressSpace = 0;
if (dt->ty == TY::Tnull) {
elemType = llvm::Type::getInt8Ty(getGlobalContext());
} else {
elemType = DtoMemType(dt->nextOf());
if (dt->nextOf()->ty == TY::Tfunction) {
addressSpace = gDataLayout->getProgramAddressSpace();
}

// DtoType could have already created the same type, e.g. for
// dt == Node* in struct Node { Node* n; }.
Expand All @@ -130,7 +134,8 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
}
}

auto t = new IrTypePointer(dt, llvm::PointerType::get(elemType, 0));
auto t =
new IrTypePointer(dt, llvm::PointerType::get(elemType, addressSpace));
ctype = t;
return t;
}
Expand Down
3 changes: 2 additions & 1 deletion ir/irtypefunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ IrTypeDelegate *IrTypeDelegate::get(Type *t) {
IrFuncTy irFty(tf);
llvm::Type *ltf =
DtoFunctionType(tf, irFty, nullptr, Type::tvoid->pointerTo());
llvm::Type *types[] = {getVoidPtrType(), getPtrToType(ltf)};
llvm::Type *fptr = ltf->getPointerTo(gDataLayout->getProgramAddressSpace());
llvm::Type *types[] = {getVoidPtrType(), fptr};
LLStructType *lt = LLStructType::get(gIR->context(), types, false);

// Could have already built the type as part of a struct forward reference,
Expand Down
24 changes: 24 additions & 0 deletions tests/compilable/funcptr_harvard_gh4432.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// A minimal test for function pointers/delegates on a Harvard architecture,
// with code residing in a separate address space.

// REQUIRES: target_AVR
// RUN: %ldc -mtriple=avr -betterC -c %s

alias FP = void function();
alias DG = void delegate();

void foo(FP fp, DG dg)
{
fp();
dg();
}

void bar()
{
foo(() {}, delegate() {});

FP fp = &bar;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably good to also add testing of (normal) pointers to variables.
No IR test to validate that the address-space is correct? Or do we assume that is already part of LLVM checking?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was a bit lazy and wasn't sure how far we'd get with this. I've extended the test now, incl. storing and loading a function pointer in static data.

DG dg;
dg.funcptr = &bar;
foo(fp, dg);
}