| | 198 | |
|---|
| | 199 | //TODO: Move all of this into a helper function? |
|---|
| | 200 | if(false) |
|---|
| | 201 | { |
|---|
| | 202 | using namespace llvm; |
|---|
| | 203 | |
|---|
| | 204 | //FIXME: Proper out file name. |
|---|
| | 205 | std::string Err; |
|---|
| | 206 | raw_fd_ostream Out("test.s", Err); |
|---|
| | 207 | if(!Err.empty()) {} |
|---|
| | 208 | |
|---|
| | 209 | const TargetMachineRegistry::entry* MArch; |
|---|
| | 210 | MArch = TargetMachineRegistry::getClosestStaticTargetForModule(*ir.module, Err); |
|---|
| | 211 | if (MArch == 0) { |
|---|
| | 212 | error("error auto-selecting target for module '%s'", Err.c_str()); |
|---|
| | 213 | fatal(); |
|---|
| | 214 | } |
|---|
| | 215 | |
|---|
| | 216 | SubtargetFeatures Features; |
|---|
| | 217 | //TODO: Features? |
|---|
| | 218 | // Features.setCPU(MCPU); |
|---|
| | 219 | // for (unsigned i = 0; i != MAttrs.size(); ++i) |
|---|
| | 220 | // Features.AddFeature(MAttrs[i]); |
|---|
| | 221 | |
|---|
| | 222 | //TODO: Set PIC if shared (or just do it always?) |
|---|
| | 223 | // TargetMachine::setRelocationModel(...); |
|---|
| | 224 | |
|---|
| | 225 | std::auto_ptr<TargetMachine> target(MArch->CtorFn(*ir.module, Features.getString())); |
|---|
| | 226 | assert(target.get() && "Could not allocate target machine!"); |
|---|
| | 227 | TargetMachine &Target = *target.get(); |
|---|
| | 228 | |
|---|
| | 229 | // Build up all of the passes that we want to do to the module. |
|---|
| | 230 | ExistingModuleProvider Provider(ir.module); |
|---|
| | 231 | FunctionPassManager Passes(&Provider); |
|---|
| | 232 | //FIXME does this TargetData match gTargetData? |
|---|
| | 233 | Passes.add(new TargetData(*Target.getTargetData())); |
|---|
| | 234 | |
|---|
| | 235 | // Ask the target to add backend passes as necessary. |
|---|
| | 236 | MachineCodeEmitter *MCE = 0; |
|---|
| | 237 | |
|---|
| | 238 | //TODO: May want to switch it on for -O0? |
|---|
| | 239 | bool Fast = false; |
|---|
| | 240 | FileModel::Model mod = Target.addPassesToEmitFile(Passes, Out, TargetMachine::AssemblyFile, Fast); |
|---|
| | 241 | assert(mod == FileModel::AsmFile); |
|---|
| | 242 | |
|---|
| | 243 | bool err = Target.addPassesToEmitFileFinish(Passes, MCE, Fast); |
|---|
| | 244 | assert(!err); |
|---|
| | 245 | |
|---|
| | 246 | Passes.doInitialization(); |
|---|
| | 247 | |
|---|
| | 248 | // Run our queue of passes all at once now, efficiently. |
|---|
| | 249 | for (llvm::Module::iterator I = ir.module->begin(), E = ir.module->end(); I != E; ++I) |
|---|
| | 250 | if (!I->isDeclaration()) |
|---|
| | 251 | Passes.run(*I); |
|---|
| | 252 | |
|---|
| | 253 | Passes.doFinalization(); |
|---|
| | 254 | |
|---|
| | 255 | // release module from module provider so we can delete it ourselves |
|---|
| | 256 | llvm::Module* rmod = Provider.releaseModule(&Err); |
|---|
| | 257 | assert(rmod); |
|---|
| | 258 | } |
|---|
| | 259 | |
|---|