comparison gen/tocall.cpp @ 1047:6bb04dbee21f

Some calling convention work for x86-64: - Implement x86-64 extern(C), hopefully correctly. - Tried to be a bit smarter about extern(D) while I was there. Interestingly, this code seems to be generating more efficient code than gcc and llvm-gcc in some edge cases, like returning a `{ [7 x i8] }` loaded from a stack slot from an extern(C) function. (gcc generates 7 1-byte loads, while this code generates a 4-byte, a 2-byte and a 1-byte load) I also added some changes to make sure structs being returned from functions or passed in as parameters are stored in memory where the rest of the backend seems to expect them to be. These should be removed when support for first-class aggregates improves.
author Frits van Bommel <fvbommel wxs.nl>
date Fri, 06 Mar 2009 16:00:47 +0100
parents 45af482e3832
children 32ead42679d1
comparison
equal deleted inserted replaced
1046:cc6489f32519 1047:6bb04dbee21f
215 215
216 // FIXME: this function is a mess ! 216 // FIXME: this function is a mess !
217 217
218 DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* arguments) 218 DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* arguments)
219 { 219 {
220 if (Logger::enabled()) {
221 Logger::println("DtoCallFunction()");
222 }
223 LOG_SCOPE
224
220 // the callee D type 225 // the callee D type
221 Type* calleeType = fnval->getType(); 226 Type* calleeType = fnval->getType();
222 227
223 // if the type has not yet been processed, do so now 228 // if the type has not yet been processed, do so now
224 if (calleeType->ir.type == NULL) 229 if (calleeType->ir.type == NULL)
383 388
384 // give the ABI a say 389 // give the ABI a say
385 LLValue* arg = tf->fty->putParam(argval->getType(), i, argval); 390 LLValue* arg = tf->fty->putParam(argval->getType(), i, argval);
386 391
387 int j = tf->fty->reverseParams ? beg + n - i - 1 : beg + i; 392 int j = tf->fty->reverseParams ? beg + n - i - 1 : beg + i;
393
394 // Hack around LDC assuming structs are in memory:
395 // If the function wants a struct, and the argument value is a
396 // pointer to a struct, load from it before passing it in.
397 if (argval->getType()->ty == Tstruct
398 && isaPointer(arg) && !isaPointer(callableTy->getParamType(j))) {
399 Logger::println("Loading struct type for function argument");
400 arg = DtoLoad(arg);
401 }
388 402
389 // parameter type mismatch, this is hard to get rid of 403 // parameter type mismatch, this is hard to get rid of
390 if (arg->getType() != callableTy->getParamType(j)) 404 if (arg->getType() != callableTy->getParamType(j))
391 { 405 {
392 #if 1 406 #if 1
466 LLCallSite call = gIR->CreateCallOrInvoke(callable, args.begin(), args.end(), varname); 480 LLCallSite call = gIR->CreateCallOrInvoke(callable, args.begin(), args.end(), varname);
467 481
468 // get return value 482 // get return value
469 LLValue* retllval = (retinptr) ? args[0] : call.getInstruction(); 483 LLValue* retllval = (retinptr) ? args[0] : call.getInstruction();
470 484
471 if (tf->linkage == LINKintrinsic) 485 // Ignore ABI for intrinsics
472 { 486 if (tf->linkage != LINKintrinsic && !retinptr)
473 // Ignore ABI for intrinsics
474 Type* rettype = tf->next;
475 if (rettype->ty == Tstruct) {
476 // LDC assumes structs are in memory, so put it there.
477 LLValue* mem = DtoAlloca(retllval->getType());
478 DtoStore(retllval, mem);
479 retllval = mem;
480 }
481 }
482 else if (!retinptr)
483 { 487 {
484 // do abi specific return value fixups 488 // do abi specific return value fixups
485 DImValue dretval(tf->next, retllval); 489 DImValue dretval(tf->next, retllval);
486 retllval = tf->fty->getRet(tf->next, &dretval); 490 retllval = tf->fty->getRet(tf->next, &dretval);
491 }
492
493 // Hack around LDC assuming structs are in memory:
494 // If the function returns a struct, and the return value is not a
495 // pointer to a struct, store it to a stack slot before continuing.
496 if (tf->next->ty == Tstruct && !isaPointer(retllval)) {
497 Logger::println("Storing return value to stack slot");
498 LLValue* mem = DtoAlloca(retllval->getType());
499 DtoStore(retllval, mem);
500 retllval = mem;
487 } 501 }
488 502
489 // repaint the type if necessary 503 // repaint the type if necessary
490 if (resulttype) 504 if (resulttype)
491 { 505 {