[svn r34] * Fixed passing a struct literal as function argument

This commit is contained in:
Tomas Lindquist Olsen
2007-10-04 14:15:54 +02:00
parent 43d59e151b
commit c188a544de
3 changed files with 56 additions and 27 deletions

View File

@@ -933,6 +933,11 @@ elem* CallExp::toElem(IRState* p)
{
Expression* argexp = (Expression*)arguments->data[i];
elem* arg = argexp->toElem(p);
if (arg->inplace) {
assert(arg->mem);
llargs[j] = arg->mem;
continue;
}
Argument* fnarg = Argument::getNth(tf->parameters, i);
@@ -1385,42 +1390,46 @@ elem* StructLiteralExp::toElem(IRState* p)
LOG_SCOPE;
elem* e = new elem;
// if there is no lval, this must be a static initializer for a global. correct?
llvm::Value* sptr = 0;
// if there is no lval, this is probably a temporary struct literal. correct?
if (p->lvals.empty())
{
// TODO
assert(0);
sptr = new llvm::AllocaInst(LLVM_DtoType(type),"tmpstructliteral",p->topallocapoint());
e->mem = sptr;
e->type = elem::VAR;
}
// otherwise write directly in the lvalue
// already has memory
else
{
llvm::Value* sptr = p->toplval();
assert(sptr);
sptr = p->toplval();
}
llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
assert(sptr);
unsigned n = elements->dim;
for (unsigned i=0; i<n; ++i)
{
llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false);
llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb());
llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
Expression* vx = (Expression*)elements->data[i];
if (vx != 0) {
elem* ve = vx->toElem(p);
llvm::Value* val = ve->getValue();
Logger::cout() << *val << " | " << *arrptr << '\n';
if (vx->type->ty == Tstruct) {
TypeStruct* ts = (TypeStruct*)vx->type;
LLVM_DtoStructCopy(ts,arrptr,val);
}
else
new llvm::StoreInst(val, arrptr, p->scopebb());
delete ve;
}
else {
assert(0);
unsigned n = elements->dim;
for (unsigned i=0; i<n; ++i)
{
llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false);
llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb());
Expression* vx = (Expression*)elements->data[i];
if (vx != 0) {
elem* ve = vx->toElem(p);
llvm::Value* val = ve->getValue();
Logger::cout() << *val << " | " << *arrptr << '\n';
if (vx->type->ty == Tstruct) {
TypeStruct* ts = (TypeStruct*)vx->type;
LLVM_DtoStructCopy(ts,arrptr,val);
}
else
new llvm::StoreInst(val, arrptr, p->scopebb());
delete ve;
}
else {
assert(0);
}
}

15
test/structs6.d Normal file
View File

@@ -0,0 +1,15 @@
module structs6;
struct S
{
float f;
}
void func(S s)
{
}
void main()
{
func(S());
}

5
test/unittest1.d Normal file
View File

@@ -0,0 +1,5 @@
module unittest1;
unittest
{
}