Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 5e3b677 | 2012-03-21 08:02:10 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_backend.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 22 | #include "bcinfo/BitcodeWrapper.h" |
| 23 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 24 | #include "clang/AST/ASTContext.h" |
| 25 | #include "clang/AST/Decl.h" |
| 26 | #include "clang/AST/DeclGroup.h" |
| 27 | |
| 28 | #include "clang/Basic/Diagnostic.h" |
| 29 | #include "clang/Basic/TargetInfo.h" |
| 30 | #include "clang/Basic/TargetOptions.h" |
| 31 | |
| 32 | #include "clang/CodeGen/ModuleBuilder.h" |
| 33 | |
| 34 | #include "clang/Frontend/CodeGenOptions.h" |
| 35 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 36 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 37 | #include "llvm/IR/IRPrintingPasses.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 38 | |
| 39 | #include "llvm/Bitcode/ReaderWriter.h" |
| 40 | |
| 41 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 42 | #include "llvm/CodeGen/SchedulerRegistry.h" |
| 43 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 44 | #include "llvm/IR/LLVMContext.h" |
| 45 | #include "llvm/IR/Module.h" |
| 46 | #include "llvm/IR/Metadata.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 47 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 48 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 49 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 50 | #include "llvm/IR/DataLayout.h" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 51 | #include "llvm/Target/TargetMachine.h" |
| 52 | #include "llvm/Target/TargetOptions.h" |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 53 | #include "llvm/Support/TargetRegistry.h" |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 54 | |
| 55 | #include "llvm/MC/SubtargetFeature.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 56 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 57 | #include "slang_assert.h" |
Stephen Hines | 552d872 | 2013-12-18 12:21:50 -0800 | [diff] [blame] | 58 | #include "strip_unknown_attributes.h" |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 59 | #include "BitWriter_2_9/ReaderWriter_2_9.h" |
Stephen Hines | 9b044ec | 2011-11-23 16:30:16 -0800 | [diff] [blame] | 60 | #include "BitWriter_2_9_func/ReaderWriter_2_9_func.h" |
Stephen Hines | d711dec | 2013-01-08 16:23:30 -0800 | [diff] [blame] | 61 | #include "BitWriter_3_2/ReaderWriter_3_2.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 62 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 63 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 65 | void Backend::CreateFunctionPasses() { |
| 66 | if (!mPerFunctionPasses) { |
| 67 | mPerFunctionPasses = new llvm::FunctionPassManager(mpModule); |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 68 | mPerFunctionPasses->add(new llvm::DataLayoutPass(mpModule)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 69 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 70 | llvm::PassManagerBuilder PMBuilder; |
| 71 | PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel; |
| 72 | PMBuilder.populateFunctionPassManager(*mPerFunctionPasses); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 73 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void Backend::CreateModulePasses() { |
| 77 | if (!mPerModulePasses) { |
| 78 | mPerModulePasses = new llvm::PassManager(); |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 79 | mPerModulePasses->add(new llvm::DataLayoutPass(mpModule)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 80 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 81 | llvm::PassManagerBuilder PMBuilder; |
| 82 | PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel; |
| 83 | PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize; |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 84 | if (mCodeGenOpts.UnitAtATime) { |
| 85 | PMBuilder.DisableUnitAtATime = 0; |
| 86 | } else { |
| 87 | PMBuilder.DisableUnitAtATime = 1; |
| 88 | } |
| 89 | |
| 90 | if (mCodeGenOpts.UnrollLoops) { |
| 91 | PMBuilder.DisableUnrollLoops = 0; |
| 92 | } else { |
| 93 | PMBuilder.DisableUnrollLoops = 1; |
| 94 | } |
| 95 | |
Shih-wei Liao | fcc654a | 2011-06-22 04:47:33 -0700 | [diff] [blame] | 96 | PMBuilder.populateModulePassManager(*mPerModulePasses); |
Stephen Hines | 552d872 | 2013-12-18 12:21:50 -0800 | [diff] [blame] | 97 | // Add a pass to strip off unknown/unsupported attributes. |
| 98 | mPerModulePasses->add(createStripUnknownAttributesPass()); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 99 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 100 | } |
| 101 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 102 | bool Backend::CreateCodeGenPasses() { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 103 | if ((mOT != Slang::OT_Assembly) && (mOT != Slang::OT_Object)) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 104 | return true; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | |
| 106 | // Now we add passes for code emitting |
| 107 | if (mCodeGenPasses) { |
| 108 | return true; |
| 109 | } else { |
| 110 | mCodeGenPasses = new llvm::FunctionPassManager(mpModule); |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 111 | mCodeGenPasses->add(new llvm::DataLayoutPass(mpModule)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Create the TargetMachine for generating code. |
| 115 | std::string Triple = mpModule->getTargetTriple(); |
| 116 | |
| 117 | std::string Error; |
| 118 | const llvm::Target* TargetInfo = |
| 119 | llvm::TargetRegistry::lookupTarget(Triple, Error); |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 120 | if (TargetInfo == NULL) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 121 | mDiagEngine.Report(clang::diag::err_fe_unable_to_create_target) << Error; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Logan Chien | ac4e185 | 2011-12-16 13:37:10 +0800 | [diff] [blame] | 125 | // Target Machine Options |
| 126 | llvm::TargetOptions Options; |
| 127 | |
| 128 | Options.NoFramePointerElim = mCodeGenOpts.DisableFPElim; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 129 | |
| 130 | // Use hardware FPU. |
| 131 | // |
| 132 | // FIXME: Need to detect the CPU capability and decide whether to use softfp. |
| 133 | // To use softfp, change following 2 lines to |
| 134 | // |
Logan Chien | ac4e185 | 2011-12-16 13:37:10 +0800 | [diff] [blame] | 135 | // Options.FloatABIType = llvm::FloatABI::Soft; |
| 136 | // Options.UseSoftFloat = true; |
| 137 | Options.FloatABIType = llvm::FloatABI::Hard; |
| 138 | Options.UseSoftFloat = false; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 139 | |
| 140 | // BCC needs all unknown symbols resolved at compilation time. So we don't |
| 141 | // need any relocation model. |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 142 | llvm::Reloc::Model RM = llvm::Reloc::Static; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 143 | |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 144 | // This is set for the linker (specify how large of the virtual addresses we |
| 145 | // can access for all unknown symbols.) |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 146 | llvm::CodeModel::Model CM; |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 147 | if (mpModule->getDataLayout()->getPointerSize() == 4) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 148 | CM = llvm::CodeModel::Small; |
| 149 | } else { |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 150 | // The target may have pointer size greater than 32 (e.g. x86_64 |
| 151 | // architecture) may need large data address model |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 152 | CM = llvm::CodeModel::Medium; |
| 153 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 154 | |
| 155 | // Setup feature string |
| 156 | std::string FeaturesStr; |
| 157 | if (mTargetOpts.CPU.size() || mTargetOpts.Features.size()) { |
| 158 | llvm::SubtargetFeatures Features; |
| 159 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 160 | for (std::vector<std::string>::const_iterator |
| 161 | I = mTargetOpts.Features.begin(), E = mTargetOpts.Features.end(); |
| 162 | I != E; |
| 163 | I++) |
| 164 | Features.AddFeature(*I); |
| 165 | |
| 166 | FeaturesStr = Features.getString(); |
| 167 | } |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 168 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 169 | llvm::TargetMachine *TM = |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 170 | TargetInfo->createTargetMachine(Triple, mTargetOpts.CPU, FeaturesStr, |
Logan Chien | ac4e185 | 2011-12-16 13:37:10 +0800 | [diff] [blame] | 171 | Options, RM, CM); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 172 | |
| 173 | // Register scheduler |
| 174 | llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler); |
| 175 | |
| 176 | // Register allocation policy: |
| 177 | // createFastRegisterAllocator: fast but bad quality |
Logan Chien | 2c6bad5 | 2011-11-15 15:57:33 +0800 | [diff] [blame] | 178 | // createGreedyRegisterAllocator: not so fast but good quality |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 179 | llvm::RegisterRegAlloc::setDefault((mCodeGenOpts.OptimizationLevel == 0) ? |
| 180 | llvm::createFastRegisterAllocator : |
Logan Chien | 2c6bad5 | 2011-11-15 15:57:33 +0800 | [diff] [blame] | 181 | llvm::createGreedyRegisterAllocator); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 182 | |
| 183 | llvm::CodeGenOpt::Level OptLevel = llvm::CodeGenOpt::Default; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 184 | if (mCodeGenOpts.OptimizationLevel == 0) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 185 | OptLevel = llvm::CodeGenOpt::None; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 186 | } else if (mCodeGenOpts.OptimizationLevel == 3) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 187 | OptLevel = llvm::CodeGenOpt::Aggressive; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 188 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 189 | |
| 190 | llvm::TargetMachine::CodeGenFileType CGFT = |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 191 | llvm::TargetMachine::CGFT_AssemblyFile; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 192 | if (mOT == Slang::OT_Object) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 193 | CGFT = llvm::TargetMachine::CGFT_ObjectFile; |
Shih-wei Liao | 7770326 | 2011-07-02 11:30:32 -0700 | [diff] [blame] | 194 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 195 | if (TM->addPassesToEmitFile(*mCodeGenPasses, FormattedOutStream, |
| 196 | CGFT, OptLevel)) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 197 | mDiagEngine.Report(clang::diag::err_fe_unable_to_interface_with_target); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 204 | Backend::Backend(clang::DiagnosticsEngine *DiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 205 | const clang::CodeGenOptions &CodeGenOpts, |
| 206 | const clang::TargetOptions &TargetOpts, |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 207 | PragmaList *Pragmas, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 208 | llvm::raw_ostream *OS, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 209 | Slang::OutputType OT) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 210 | : ASTConsumer(), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 211 | mTargetOpts(TargetOpts), |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 212 | mpModule(NULL), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 213 | mpOS(OS), |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 214 | mOT(OT), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 215 | mGen(NULL), |
| 216 | mPerFunctionPasses(NULL), |
| 217 | mPerModulePasses(NULL), |
| 218 | mCodeGenPasses(NULL), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 219 | mLLVMContext(llvm::getGlobalContext()), |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 220 | mDiagEngine(*DiagEngine), |
mkopec1 | c460b37 | 2012-01-09 11:21:50 -0500 | [diff] [blame] | 221 | mCodeGenOpts(CodeGenOpts), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 222 | mPragmas(Pragmas) { |
| 223 | FormattedOutStream.setStream(*mpOS, |
| 224 | llvm::formatted_raw_ostream::PRESERVE_STREAM); |
Stephen Hines | 0da7f6c | 2013-03-05 15:19:02 -0800 | [diff] [blame] | 225 | mGen = CreateLLVMCodeGen(mDiagEngine, "", mCodeGenOpts, |
| 226 | mTargetOpts, mLLVMContext); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 229 | void Backend::Initialize(clang::ASTContext &Ctx) { |
| 230 | mGen->Initialize(Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 231 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 232 | mpModule = mGen->GetModule(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 235 | // Encase the Bitcode in a wrapper containing RS version information. |
| 236 | void Backend::WrapBitcode(llvm::raw_string_ostream &Bitcode) { |
Stephen Hines | 5e3b677 | 2012-03-21 08:02:10 -0700 | [diff] [blame] | 237 | bcinfo::AndroidBitcodeWrapper wrapper; |
| 238 | size_t actualWrapperLen = bcinfo::writeAndroidBitcodeWrapper( |
| 239 | &wrapper, Bitcode.str().length(), getTargetAPI(), |
| 240 | SlangVersion::CURRENT, mCodeGenOpts.OptimizationLevel); |
| 241 | |
| 242 | slangAssert(actualWrapperLen > 0); |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 243 | |
| 244 | // Write out the bitcode wrapper. |
Stephen Hines | 5e3b677 | 2012-03-21 08:02:10 -0700 | [diff] [blame] | 245 | FormattedOutStream.write(reinterpret_cast<char*>(&wrapper), actualWrapperLen); |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 246 | |
| 247 | // Write out the actual encoded bitcode. |
| 248 | FormattedOutStream << Bitcode.str(); |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Logan Chien | fa6ef56 | 2011-11-25 13:50:02 +0800 | [diff] [blame] | 251 | bool Backend::HandleTopLevelDecl(clang::DeclGroupRef D) { |
Stephen Hines | de7ac19 | 2011-11-28 15:57:01 -0800 | [diff] [blame] | 252 | return mGen->HandleTopLevelDecl(D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 255 | void Backend::HandleTranslationUnit(clang::ASTContext &Ctx) { |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 256 | HandleTranslationUnitPre(Ctx); |
| 257 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 258 | mGen->HandleTranslationUnit(Ctx); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 259 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 260 | // Here, we complete a translation unit (whole translation unit is now in LLVM |
| 261 | // IR). Now, interact with LLVM backend to generate actual machine code (asm |
| 262 | // or machine code, whatever.) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 263 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 264 | // Silently ignore if we weren't initialized for some reason. |
Zonr Chang | 41ebf53 | 2010-10-13 18:29:18 +0800 | [diff] [blame] | 265 | if (!mpModule) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 266 | return; |
| 267 | |
| 268 | llvm::Module *M = mGen->ReleaseModule(); |
| 269 | if (!M) { |
| 270 | // The module has been released by IR gen on failures, do not double free. |
| 271 | mpModule = NULL; |
| 272 | return; |
| 273 | } |
| 274 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 275 | slangAssert(mpModule == M && |
| 276 | "Unexpected module change during LLVM IR generation"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 277 | |
| 278 | // Insert #pragma information into metadata section of module |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 279 | if (!mPragmas->empty()) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 280 | llvm::NamedMDNode *PragmaMetadata = |
| 281 | mpModule->getOrInsertNamedMetadata(Slang::PragmaMetadataName); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 282 | for (PragmaList::const_iterator I = mPragmas->begin(), E = mPragmas->end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 283 | I != E; |
| 284 | I++) { |
| 285 | llvm::SmallVector<llvm::Value*, 2> Pragma; |
| 286 | // Name goes first |
| 287 | Pragma.push_back(llvm::MDString::get(mLLVMContext, I->first)); |
| 288 | // And then value |
| 289 | Pragma.push_back(llvm::MDString::get(mLLVMContext, I->second)); |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 290 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 291 | // Create MDNode and insert into PragmaMetadata |
| 292 | PragmaMetadata->addOperand( |
Stephen Hines | 18c8829 | 2011-07-14 22:46:15 -0700 | [diff] [blame] | 293 | llvm::MDNode::get(mLLVMContext, Pragma)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Zonr Chang | 68fc02c | 2010-10-13 19:09:19 +0800 | [diff] [blame] | 297 | HandleTranslationUnitPost(mpModule); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 298 | |
| 299 | // Create passes for optimization and code emission |
| 300 | |
| 301 | // Create and run per-function passes |
| 302 | CreateFunctionPasses(); |
| 303 | if (mPerFunctionPasses) { |
| 304 | mPerFunctionPasses->doInitialization(); |
| 305 | |
| 306 | for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end(); |
| 307 | I != E; |
| 308 | I++) |
| 309 | if (!I->isDeclaration()) |
| 310 | mPerFunctionPasses->run(*I); |
| 311 | |
| 312 | mPerFunctionPasses->doFinalization(); |
| 313 | } |
| 314 | |
| 315 | // Create and run module passes |
| 316 | CreateModulePasses(); |
| 317 | if (mPerModulePasses) |
| 318 | mPerModulePasses->run(*mpModule); |
| 319 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 320 | switch (mOT) { |
| 321 | case Slang::OT_Assembly: |
| 322 | case Slang::OT_Object: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 323 | if (!CreateCodeGenPasses()) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 324 | return; |
| 325 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 326 | mCodeGenPasses->doInitialization(); |
| 327 | |
| 328 | for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end(); |
| 329 | I != E; |
| 330 | I++) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 331 | if (!I->isDeclaration()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 332 | mCodeGenPasses->run(*I); |
| 333 | |
| 334 | mCodeGenPasses->doFinalization(); |
| 335 | break; |
| 336 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 337 | case Slang::OT_LLVMAssembly: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 338 | llvm::PassManager *LLEmitPM = new llvm::PassManager(); |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 339 | LLEmitPM->add(llvm::createPrintModulePass(FormattedOutStream)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 340 | LLEmitPM->run(*mpModule); |
| 341 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 342 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 343 | case Slang::OT_Bitcode: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 344 | llvm::PassManager *BCEmitPM = new llvm::PassManager(); |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 345 | std::string BCStr; |
| 346 | llvm::raw_string_ostream Bitcode(BCStr); |
Chris Wailes | c9454af | 2014-06-13 17:25:40 -0700 | [diff] [blame] | 347 | unsigned int TargetAPI = getTargetAPI(); |
Stephen Hines | 9b044ec | 2011-11-23 16:30:16 -0800 | [diff] [blame] | 348 | switch (TargetAPI) { |
| 349 | case SLANG_HC_TARGET_API: |
| 350 | case SLANG_HC_MR1_TARGET_API: |
| 351 | case SLANG_HC_MR2_TARGET_API: { |
| 352 | // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter |
| 353 | BCEmitPM->add(llvm_2_9::createBitcodeWriterPass(Bitcode)); |
| 354 | break; |
| 355 | } |
| 356 | case SLANG_ICS_TARGET_API: |
| 357 | case SLANG_ICS_MR1_TARGET_API: { |
| 358 | // ICS targets must use the LLVM 2.9_func BitcodeWriter |
| 359 | BCEmitPM->add(llvm_2_9_func::createBitcodeWriterPass(Bitcode)); |
| 360 | break; |
| 361 | } |
| 362 | default: { |
Chris Wailes | 9e3aa07 | 2014-06-16 18:52:02 -0700 | [diff] [blame] | 363 | if (TargetAPI != SLANG_DEVELOPMENT_TARGET_API && |
| 364 | (TargetAPI < SLANG_MINIMUM_TARGET_API || |
| 365 | TargetAPI > SLANG_MAXIMUM_TARGET_API)) { |
Stephen Hines | 9b044ec | 2011-11-23 16:30:16 -0800 | [diff] [blame] | 366 | slangAssert(false && "Invalid target API value"); |
| 367 | } |
Stephen Hines | d711dec | 2013-01-08 16:23:30 -0800 | [diff] [blame] | 368 | // Switch to the 3.2 BitcodeWriter by default, and don't use |
| 369 | // LLVM's included BitcodeWriter at all (for now). |
| 370 | BCEmitPM->add(llvm_3_2::createBitcodeWriterPass(Bitcode)); |
| 371 | //BCEmitPM->add(llvm::createBitcodeWriterPass(Bitcode)); |
Stephen Hines | 9b044ec | 2011-11-23 16:30:16 -0800 | [diff] [blame] | 372 | break; |
| 373 | } |
Stephen Hines | 4cc499d | 2011-08-24 19:06:17 -0700 | [diff] [blame] | 374 | } |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 375 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 376 | BCEmitPM->run(*mpModule); |
Stephen Hines | 5e6d0d5 | 2011-11-22 19:42:41 -0800 | [diff] [blame] | 377 | WrapBitcode(Bitcode); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 378 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 379 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 380 | case Slang::OT_Nothing: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 381 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 382 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 383 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 384 | slangAssert(false && "Unknown output type"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 385 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 386 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 387 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 388 | FormattedOutStream.flush(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 391 | void Backend::HandleTagDeclDefinition(clang::TagDecl *D) { |
| 392 | mGen->HandleTagDeclDefinition(D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 395 | void Backend::CompleteTentativeDefinition(clang::VarDecl *D) { |
| 396 | mGen->CompleteTentativeDefinition(D); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | Backend::~Backend() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 400 | delete mpModule; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 401 | delete mGen; |
| 402 | delete mPerFunctionPasses; |
| 403 | delete mPerModulePasses; |
| 404 | delete mCodeGenPasses; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 405 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 406 | |
| 407 | } // namespace slang |