comparison gen/toobj.cpp @ 988:2667e3a145be

- Fixed LLVM style CL args for D2. - Moved main() into its own file gen/main.cpp - Fixed basic cross compilation - removed the option for setting OS - added support for llc's mattr, mcpu and mtriple switches - added basic ABI abstraction for return value rewrites, it's not perfect and will probably be completely rewritten once I get to handling parameter rewrites as well. - x86-64 extern(C) abi for cfloat returns now match (llvm-)gcc.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Thu, 26 Feb 2009 14:11:49 +0100
parents a8cb25d478c4
children 27956b440c0a
comparison
equal deleted inserted replaced
987:73ff89728d85 988:2667e3a145be
12 #include <fstream> 12 #include <fstream>
13 13
14 #include "gen/llvm.h" 14 #include "gen/llvm.h"
15 #include "llvm/Analysis/Verifier.h" 15 #include "llvm/Analysis/Verifier.h"
16 #include "llvm/Bitcode/ReaderWriter.h" 16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Target/SubtargetFeature.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetMachineRegistry.h"
20 #include "llvm/Module.h" 17 #include "llvm/Module.h"
21 #include "llvm/ModuleProvider.h" 18 #include "llvm/ModuleProvider.h"
22 #include "llvm/PassManager.h" 19 #include "llvm/PassManager.h"
23 #include "llvm/LinkAllPasses.h" 20 #include "llvm/LinkAllPasses.h"
24 #include "llvm/System/Program.h" 21 #include "llvm/System/Program.h"
25 #include "llvm/System/Path.h" 22 #include "llvm/System/Path.h"
26 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Target/TargetMachine.h"
28 26
29 #include "mars.h" 27 #include "mars.h"
30 #include "module.h" 28 #include "module.h"
31 #include "mtype.h" 29 #include "mtype.h"
32 #include "declaration.h" 30 #include "declaration.h"
48 #include "gen/structs.h" 46 #include "gen/structs.h"
49 #include "gen/classes.h" 47 #include "gen/classes.h"
50 #include "gen/functions.h" 48 #include "gen/functions.h"
51 #include "gen/todebug.h" 49 #include "gen/todebug.h"
52 #include "gen/runtime.h" 50 #include "gen/runtime.h"
51 #include "gen/abi.h"
52 #include "gen/cl_options.h"
53 53
54 #include "ir/irvar.h" 54 #include "ir/irvar.h"
55 #include "ir/irmodule.h" 55 #include "ir/irmodule.h"
56 56
57 ////////////////////////////////////////////////////////////////////////////////////////// 57 //////////////////////////////////////////////////////////////////////////////////////////
108 // might already exist via import, just overwrite since 108 // might already exist via import, just overwrite since
109 // the global created for the filename must belong to the right llvm module 109 // the global created for the filename must belong to the right llvm module
110 // FIXME: but shouldn't this always get reset between modules? like other IrSymbols 110 // FIXME: but shouldn't this always get reset between modules? like other IrSymbols
111 this->ir.irModule = new IrModule(this, srcfile->toChars()); 111 this->ir.irModule = new IrModule(this, srcfile->toChars());
112 112
113 // set target stuff 113 // set target triple
114
115 ir.module->setTargetTriple(global.params.targetTriple); 114 ir.module->setTargetTriple(global.params.targetTriple);
115
116 // set final data layout
116 ir.module->setDataLayout(global.params.dataLayout); 117 ir.module->setDataLayout(global.params.dataLayout);
117
118 // get the target machine
119 const llvm::TargetMachineRegistry::entry* MArch;
120
121 std::string Err;
122 MArch = llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, Err);
123 if (MArch == 0) {
124 error("error auto-selecting target for module '%s'", Err.c_str());
125 fatal();
126 }
127
128 llvm::SubtargetFeatures Features;
129 //TODO: Features?
130 // Features.setCPU(MCPU);
131 // for (unsigned i = 0; i != MAttrs.size(); ++i)
132 // Features.AddFeature(MAttrs[i]);
133
134 // allocate the target machine
135 std::auto_ptr<llvm::TargetMachine> target(MArch->CtorFn(*ir.module, Features.getString()));
136 assert(target.get() && "Could not allocate target machine!");
137 llvm::TargetMachine &Target = *target.get();
138
139 gTargetData = Target.getTargetData();
140
141 // set final data layout
142 std::string datalayout = gTargetData->getStringRepresentation();
143 ir.module->setDataLayout(datalayout);
144 if (Logger::enabled()) 118 if (Logger::enabled())
145 Logger::cout() << "Final data layout: " << datalayout << '\n'; 119 Logger::cout() << "Final data layout: " << global.params.dataLayout << '\n';
146 assert(memcmp(global.params.dataLayout, datalayout.c_str(), 9) == 0); // "E-p:xx:xx" 120
121 // allocate the target abi
122 gABI = TargetABI::getTarget();
147 123
148 // debug info 124 // debug info
149 if (global.params.symdebug) { 125 if (global.params.symdebug) {
150 RegisterDwarfSymbols(ir.module); 126 RegisterDwarfSymbols(ir.module);
151 DtoDwarfCompileUnit(this); 127 DtoDwarfCompileUnit(this);
250 } 226 }
251 Logger::println("Writing native asm to: %s\n", spath.c_str()); 227 Logger::println("Writing native asm to: %s\n", spath.c_str());
252 std::string err; 228 std::string err;
253 { 229 {
254 llvm::raw_fd_ostream out(spath.c_str(), false, err); 230 llvm::raw_fd_ostream out(spath.c_str(), false, err);
255 write_asm_to_file(Target, *ir.module, out); 231 write_asm_to_file(*gTargetMachine, *ir.module, out);
256 } 232 }
257 233
258 // call gcc to convert assembly to object file 234 // call gcc to convert assembly to object file
259 if (global.params.output_o) { 235 if (global.params.output_o) {
260 LLPath objpath = LLPath(objfile->name->toChars()); 236 LLPath objpath = LLPath(objfile->name->toChars());