Mercurial > projects > ldc
comparison gen/naked.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 | 474d7dd54d43 |
children | dbe4af57b240 |
comparison
equal
deleted
inserted
replaced
1046:cc6489f32519 | 1047:6bb04dbee21f |
---|---|
166 gIR->functions.pop_back(); | 166 gIR->functions.pop_back(); |
167 } | 167 } |
168 | 168 |
169 ////////////////////////////////////////////////////////////////////////////////////////// | 169 ////////////////////////////////////////////////////////////////////////////////////////// |
170 | 170 |
171 static LLValue* x86_64_cfloatRetFixup(IRBuilderHelper b, LLValue* orig) { | |
172 assert(orig->getType() == LLType::DoubleTy); | |
173 LLType* retty = LLStructType::get(LLType::DoubleTy, NULL); | |
174 LLValue* undef = llvm::UndefValue::get(retty); | |
175 return b->CreateInsertValue(undef, orig, 0, "asm.ret"); | |
176 } | |
177 | |
178 void emitABIReturnAsmStmt(IRAsmBlock* asmblock, Loc loc, FuncDeclaration* fdecl) | 171 void emitABIReturnAsmStmt(IRAsmBlock* asmblock, Loc loc, FuncDeclaration* fdecl) |
179 { | 172 { |
180 Logger::println("emitABIReturnAsmStmt(%s)", fdecl->mangle()); | 173 Logger::println("emitABIReturnAsmStmt(%s)", fdecl->mangle()); |
181 LOG_SCOPE; | 174 LOG_SCOPE; |
182 | 175 |
183 IRAsmStmt* as = new IRAsmStmt; | 176 IRAsmStmt* as = new IRAsmStmt; |
184 | 177 |
185 const LLType* llretTy = DtoType(fdecl->type->nextOf()); | 178 const LLType* llretTy = DtoType(fdecl->type->nextOf()); |
186 asmblock->retty = llretTy; | 179 asmblock->retty = llretTy; |
187 asmblock->retn = 1; | 180 asmblock->retn = 1; |
181 | |
182 // FIXME: This should probably be handled by the TargetABI somehow. | |
183 // It should be able to do this for a greater variety of types. | |
188 | 184 |
189 // x86 | 185 // x86 |
190 if (global.params.cpu == ARCHx86) | 186 if (global.params.cpu == ARCHx86) |
191 { | 187 { |
192 LINK l = fdecl->linkage; | 188 LINK l = fdecl->linkage; |
291 // LLVM and GCC disagree on how to return {float, float}. | 287 // LLVM and GCC disagree on how to return {float, float}. |
292 // For compatibility, use the GCC/LLVM-GCC way for extern(C/Windows) | 288 // For compatibility, use the GCC/LLVM-GCC way for extern(C/Windows) |
293 // extern(C) cfloat -> %xmm0 (extract two floats) | 289 // extern(C) cfloat -> %xmm0 (extract two floats) |
294 as->out_c = "={xmm0},"; | 290 as->out_c = "={xmm0},"; |
295 asmblock->retty = LLType::DoubleTy; | 291 asmblock->retty = LLType::DoubleTy; |
296 asmblock->retfixup = &x86_64_cfloatRetFixup; | |
297 } else if (rt->iscomplex()) { | 292 } else if (rt->iscomplex()) { |
298 // cdouble and extern(D) cfloat -> re=%xmm0, im=%xmm1 | 293 // cdouble and extern(D) cfloat -> re=%xmm0, im=%xmm1 |
299 as->out_c = "={xmm0},={xmm1},"; | 294 as->out_c = "={xmm0},={xmm1},"; |
300 asmblock->retn = 2; | 295 asmblock->retn = 2; |
301 } else { | 296 } else { |