slang_backend.cpp revision fa6ef56a6ca3dc3061218a75a7e68e5357fcb82c
1c383a500aa59423264811be3874461bf8adbfea0Zonr Chang/*
2c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Copyright 2010, The Android Open Source Project
3c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
4c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
5c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * you may not use this file except in compliance with the License.
6c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * You may obtain a copy of the License at
7c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
8c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
9c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
10c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Unless required by applicable law or agreed to in writing, software
11c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
12c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * See the License for the specific language governing permissions and
14c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * limitations under the License.
15c383a500aa59423264811be3874461bf8adbfea0Zonr Chang */
16c383a500aa59423264811be3874461bf8adbfea0Zonr Chang
176315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "slang_backend.h"
18462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
19e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <string>
20e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <vector>
21462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
225e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines#include "bcinfo/BitcodeWrapper.h"
235e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines
24e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/ASTContext.h"
259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/AST/Decl.h"
269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/AST/DeclGroup.h"
27462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/Basic/Diagnostic.h"
29e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/Basic/TargetInfo.h"
309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/Basic/TargetOptions.h"
31462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
32e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/CodeGen/ModuleBuilder.h"
33e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
343a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang#include "clang/Frontend/CodeGenOptions.h"
359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/Frontend/FrontendDiagnostic.h"
36462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
37e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Assembly/PrintModulePass.h"
38e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
39e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Bitcode/ReaderWriter.h"
40e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
41e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/CodeGen/RegAllocRegistry.h"
42e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/CodeGen/SchedulerRegistry.h"
43e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
44e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/LLVMContext.h"
45e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Module.h"
46e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Metadata.h"
47e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
489207a2e495c8363606861e4f034504ec5c153dabLogan Chien#include "llvm/Transforms/IPO/PassManagerBuilder.h"
49fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao
50e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Target/TargetData.h"
51e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Target/TargetMachine.h"
52e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Target/TargetOptions.h"
539207a2e495c8363606861e4f034504ec5c153dabLogan Chien#include "llvm/Support/TargetRegistry.h"
5477703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao
5577703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao#include "llvm/MC/SubtargetFeature.h"
56462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
576e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines#include "slang_assert.h"
584cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "BitWriter_2_9/ReaderWriter_2_9.h"
596315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr
60e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hinesnamespace slang {
61462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
623a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Backend::CreateFunctionPasses() {
633a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  if (!mPerFunctionPasses) {
643a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    mPerFunctionPasses = new llvm::FunctionPassManager(mpModule);
6541ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang    mPerFunctionPasses->add(new llvm::TargetData(mpModule));
663a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang
67fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    llvm::PassManagerBuilder PMBuilder;
68fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
69fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.populateFunctionPassManager(*mPerFunctionPasses);
703a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  }
713a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  return;
723a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang}
733a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang
743a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Backend::CreateModulePasses() {
753a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  if (!mPerModulePasses) {
763a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    mPerModulePasses = new llvm::PassManager();
7741ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang    mPerModulePasses->add(new llvm::TargetData(mpModule));
783a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang
79fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    llvm::PassManagerBuilder PMBuilder;
80fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.OptLevel = mCodeGenOpts.OptimizationLevel;
81fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize;
82fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.SizeLevel = mCodeGenOpts.OptimizeSize;
83fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    if (mCodeGenOpts.UnitAtATime) {
84fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao      PMBuilder.DisableUnitAtATime = 0;
85fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    } else {
86fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao      PMBuilder.DisableUnitAtATime = 1;
87fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    }
88fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao
89fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    if (mCodeGenOpts.UnrollLoops) {
90fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao      PMBuilder.DisableUnrollLoops = 0;
91fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    } else {
92fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao      PMBuilder.DisableUnrollLoops = 1;
93fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    }
94fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao
95fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.DisableSimplifyLibCalls = false;
96fcc654a1f4034978f7534479f9c5c33a92ca8546Shih-wei Liao    PMBuilder.populateModulePassManager(*mPerModulePasses);
973a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  }
983a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  return;
993a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang}
1003a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang
101462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaobool Backend::CreateCodeGenPasses() {
1023a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  if ((mOT != Slang::OT_Assembly) && (mOT != Slang::OT_Object))
1039ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return true;
104462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Now we add passes for code emitting
1069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (mCodeGenPasses) {
107462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    return true;
1089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  } else {
1099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    mCodeGenPasses = new llvm::FunctionPassManager(mpModule);
11041ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang    mCodeGenPasses->add(new llvm::TargetData(mpModule));
1119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Create the TargetMachine for generating code.
1149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::string Triple = mpModule->getTargetTriple();
1159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::string Error;
1179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  const llvm::Target* TargetInfo =
1189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      llvm::TargetRegistry::lookupTarget(Triple, Error);
1196315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  if (TargetInfo == NULL) {
1209207a2e495c8363606861e4f034504ec5c153dabLogan Chien    mDiagEngine.Report(clang::diag::err_fe_unable_to_create_target) << Error;
1219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return false;
1229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::NoFramePointerElim = mCodeGenOpts.DisableFPElim;
1259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Use hardware FPU.
1279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //
1289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // FIXME: Need to detect the CPU capability and decide whether to use softfp.
1299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // To use softfp, change following 2 lines to
1309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //
1319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //  llvm::FloatABIType = llvm::FloatABI::Soft;
1329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //  llvm::UseSoftFloat = true;
1339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::FloatABIType = llvm::FloatABI::Hard;
1349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::UseSoftFloat = false;
1359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // BCC needs all unknown symbols resolved at compilation time. So we don't
1379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // need any relocation model.
138ab992e59a36a18df49bf4878968ef0598299afd3Logan Chien  llvm::Reloc::Model RM = llvm::Reloc::Static;
1399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
14041ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang  // This is set for the linker (specify how large of the virtual addresses we
14141ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang  // can access for all unknown symbols.)
1429207a2e495c8363606861e4f034504ec5c153dabLogan Chien  llvm::CodeModel::Model CM;
1439207a2e495c8363606861e4f034504ec5c153dabLogan Chien  if (mpModule->getPointerSize() == llvm::Module::Pointer32) {
1449207a2e495c8363606861e4f034504ec5c153dabLogan Chien    CM = llvm::CodeModel::Small;
1459207a2e495c8363606861e4f034504ec5c153dabLogan Chien  } else {
14641ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang    // The target may have pointer size greater than 32 (e.g. x86_64
14741ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang    // architecture) may need large data address model
1489207a2e495c8363606861e4f034504ec5c153dabLogan Chien    CM = llvm::CodeModel::Medium;
1499207a2e495c8363606861e4f034504ec5c153dabLogan Chien  }
1509ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1519ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Setup feature string
1529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::string FeaturesStr;
1539ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (mTargetOpts.CPU.size() || mTargetOpts.Features.size()) {
1549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    llvm::SubtargetFeatures Features;
1559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    for (std::vector<std::string>::const_iterator
1579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao             I = mTargetOpts.Features.begin(), E = mTargetOpts.Features.end();
1589ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I != E;
1599ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I++)
1609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      Features.AddFeature(*I);
1619ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    FeaturesStr = Features.getString();
1639ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1649207a2e495c8363606861e4f034504ec5c153dabLogan Chien
1659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::TargetMachine *TM =
1669207a2e495c8363606861e4f034504ec5c153dabLogan Chien    TargetInfo->createTargetMachine(Triple, mTargetOpts.CPU, FeaturesStr,
1679207a2e495c8363606861e4f034504ec5c153dabLogan Chien                                    RM, CM);
1689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Register scheduler
1709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
1719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Register allocation policy:
1739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //  createFastRegisterAllocator: fast but bad quality
1742c6bad5038d8509466b9e0f9aa6a8ae533c0e029Logan Chien  //  createGreedyRegisterAllocator: not so fast but good quality
1759ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::RegisterRegAlloc::setDefault((mCodeGenOpts.OptimizationLevel == 0) ?
1769ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                     llvm::createFastRegisterAllocator :
1772c6bad5038d8509466b9e0f9aa6a8ae533c0e029Logan Chien                                     llvm::createGreedyRegisterAllocator);
1789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::CodeGenOpt::Level OptLevel = llvm::CodeGenOpt::Default;
18077703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao  if (mCodeGenOpts.OptimizationLevel == 0) {
1819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    OptLevel = llvm::CodeGenOpt::None;
18277703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao  } else if (mCodeGenOpts.OptimizationLevel == 3) {
1839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    OptLevel = llvm::CodeGenOpt::Aggressive;
18477703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao  }
1859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::TargetMachine::CodeGenFileType CGFT =
1876315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      llvm::TargetMachine::CGFT_AssemblyFile;
18877703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao  if (mOT == Slang::OT_Object) {
1899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    CGFT = llvm::TargetMachine::CGFT_ObjectFile;
19077703262b9c2fecc22287ca236daa72b2cea7d27Shih-wei Liao  }
1919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (TM->addPassesToEmitFile(*mCodeGenPasses, FormattedOutStream,
1929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                              CGFT, OptLevel)) {
1939207a2e495c8363606861e4f034504ec5c153dabLogan Chien    mDiagEngine.Report(clang::diag::err_fe_unable_to_interface_with_target);
1949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return false;
1959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
1969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1979ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return true;
198462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
199462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2009207a2e495c8363606861e4f034504ec5c153dabLogan ChienBackend::Backend(clang::DiagnosticsEngine *DiagEngine,
2019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                 const clang::CodeGenOptions &CodeGenOpts,
2029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                 const clang::TargetOptions &TargetOpts,
2033fd0a94a5cf1656569b1aea07043cc63939dcb46Stephen Hines                 PragmaList *Pragmas,
2049ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                 llvm::raw_ostream *OS,
2053a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang                 Slang::OutputType OT)
2066315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr    : ASTConsumer(),
2076315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mCodeGenOpts(CodeGenOpts),
2086315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mTargetOpts(TargetOpts),
20968fc02ca4a7235e2981be5eee4ad968a9d3928c0Zonr Chang      mpModule(NULL),
2106315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mpOS(OS),
2113a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang      mOT(OT),
2126315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mGen(NULL),
2136315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mPerFunctionPasses(NULL),
2146315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mPerModulePasses(NULL),
2156315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mCodeGenPasses(NULL),
2166315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mLLVMContext(llvm::getGlobalContext()),
2179207a2e495c8363606861e4f034504ec5c153dabLogan Chien      mDiagEngine(*DiagEngine),
2186315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      mPragmas(Pragmas) {
2196315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  FormattedOutStream.setStream(*mpOS,
2206315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr                               llvm::formatted_raw_ostream::PRESERVE_STREAM);
2219207a2e495c8363606861e4f034504ec5c153dabLogan Chien  mGen = CreateLLVMCodeGen(mDiagEngine, "", mCodeGenOpts, mLLVMContext);
2229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
223462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
224462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaovoid Backend::Initialize(clang::ASTContext &Ctx) {
2269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mGen->Initialize(Ctx);
227462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mpModule = mGen->GetModule();
229462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
231462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
232462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2335e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines// Encase the Bitcode in a wrapper containing RS version information.
2345e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hinesvoid Backend::WrapBitcode(llvm::raw_string_ostream &Bitcode) {
2355e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  struct bcinfo::BCWrapperHeader header;
2365e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.Magic = 0x0B17C0DE;
2375e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.Version = 0;
2385e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.BitcodeOffset = sizeof(header);
2395e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.BitcodeSize = Bitcode.str().length();
2405e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.HeaderVersion = 0;
2415e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  header.TargetAPI = getTargetAPI();
2425e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines
2435e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  // Write out the bitcode wrapper.
2445e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  FormattedOutStream.write((const char*) &header, sizeof(header));
2455e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines
2465e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  // Write out the actual encoded bitcode.
2475e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  FormattedOutStream << Bitcode.str();
2485e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines  return;
2495e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines}
2505e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines
251fa6ef56a6ca3dc3061218a75a7e68e5357fcb82cLogan Chienbool Backend::HandleTopLevelDecl(clang::DeclGroupRef D) {
2529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mGen->HandleTopLevelDecl(D);
253fa6ef56a6ca3dc3061218a75a7e68e5357fcb82cLogan Chien  return true;
254462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
255462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaovoid Backend::HandleTranslationUnit(clang::ASTContext &Ctx) {
25768fc02ca4a7235e2981be5eee4ad968a9d3928c0Zonr Chang  HandleTranslationUnitPre(Ctx);
25868fc02ca4a7235e2981be5eee4ad968a9d3928c0Zonr Chang
2599ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mGen->HandleTranslationUnit(Ctx);
260462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2619ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Here, we complete a translation unit (whole translation unit is now in LLVM
2629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // IR). Now, interact with LLVM backend to generate actual machine code (asm
2639ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // or machine code, whatever.)
264462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Silently ignore if we weren't initialized for some reason.
26641ebf534161bb67f6207a070c1f6a895dc853408Zonr Chang  if (!mpModule)
2679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
268462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  llvm::Module *M = mGen->ReleaseModule();
2709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (!M) {
2719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // The module has been released by IR gen on failures, do not double free.
2729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    mpModule = NULL;
2739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
2749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
2759ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2766e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines  slangAssert(mpModule == M &&
2776e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines              "Unexpected module change during LLVM IR generation");
2789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Insert #pragma information into metadata section of module
2803fd0a94a5cf1656569b1aea07043cc63939dcb46Stephen Hines  if (!mPragmas->empty()) {
2819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    llvm::NamedMDNode *PragmaMetadata =
2829ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        mpModule->getOrInsertNamedMetadata(Slang::PragmaMetadataName);
2833fd0a94a5cf1656569b1aea07043cc63939dcb46Stephen Hines    for (PragmaList::const_iterator I = mPragmas->begin(), E = mPragmas->end();
2849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I != E;
2859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I++) {
2869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      llvm::SmallVector<llvm::Value*, 2> Pragma;
2879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Name goes first
2889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      Pragma.push_back(llvm::MDString::get(mLLVMContext, I->first));
2899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // And then value
2909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      Pragma.push_back(llvm::MDString::get(mLLVMContext, I->second));
29183f0c6261efc8f397fc2509e3862bc6d0eb1e1c4Shih-wei Liao
2929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      // Create MDNode and insert into PragmaMetadata
2939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      PragmaMetadata->addOperand(
29418c8829f2bd3cbe0d02471588c6643c0a8c6ca3cStephen Hines          llvm::MDNode::get(mLLVMContext, Pragma));
295462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
2969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
297462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
29868fc02ca4a7235e2981be5eee4ad968a9d3928c0Zonr Chang  HandleTranslationUnitPost(mpModule);
299462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Create passes for optimization and code emission
301462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Create and run per-function passes
3039ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  CreateFunctionPasses();
3049ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (mPerFunctionPasses) {
3059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    mPerFunctionPasses->doInitialization();
306462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
3089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I != E;
3099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I++)
3109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if (!I->isDeclaration())
3119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        mPerFunctionPasses->run(*I);
312462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    mPerFunctionPasses->doFinalization();
3149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
315462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Create and run module passes
3179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  CreateModulePasses();
3189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  if (mPerModulePasses)
3199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    mPerModulePasses->run(*mpModule);
3209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3213a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang  switch (mOT) {
3223a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    case Slang::OT_Assembly:
3233a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    case Slang::OT_Object: {
3246315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr      if (!CreateCodeGenPasses())
3259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        return;
326462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      mCodeGenPasses->doInitialization();
328462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      for (llvm::Module::iterator I = mpModule->begin(), E = mpModule->end();
3309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          I != E;
3319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          I++)
3326315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        if (!I->isDeclaration())
3339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          mCodeGenPasses->run(*I);
3349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      mCodeGenPasses->doFinalization();
3369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
3379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3383a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    case Slang::OT_LLVMAssembly: {
3399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      llvm::PassManager *LLEmitPM = new llvm::PassManager();
3409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      LLEmitPM->add(llvm::createPrintModulePass(&FormattedOutStream));
3419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      LLEmitPM->run(*mpModule);
3429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
343462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
3443a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    case Slang::OT_Bitcode: {
3459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      llvm::PassManager *BCEmitPM = new llvm::PassManager();
3465e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines      std::string BCStr;
3475e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines      llvm::raw_string_ostream Bitcode(BCStr);
3484cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (getTargetAPI() < SLANG_ICS_TARGET_API) {
3494cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter
3505e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines        BCEmitPM->add(llvm_2_9::createBitcodeWriterPass(Bitcode));
3514cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else {
3525e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines        BCEmitPM->add(llvm::createBitcodeWriterPass(Bitcode));
3534cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
3545e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines
3559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      BCEmitPM->run(*mpModule);
3565e6d0d5a911fff0b7b4bce216a213a4cf8a811f5Stephen Hines      WrapBitcode(Bitcode);
3579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      break;
3589ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3593a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang    case Slang::OT_Nothing: {
3609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return;
3619ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    default: {
3636e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines      slangAssert(false && "Unknown output type");
3649ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    }
3659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
366462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  FormattedOutStream.flush();
3689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
370462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
371462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaovoid Backend::HandleTagDeclDefinition(clang::TagDecl *D) {
3739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mGen->HandleTagDeclDefinition(D);
3749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
375462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
376462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3779ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaovoid Backend::CompleteTentativeDefinition(clang::VarDecl *D) {
3789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  mGen->CompleteTentativeDefinition(D);
3799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
380462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
381462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
382462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei LiaoBackend::~Backend() {
3839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  delete mpModule;
3849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  delete mGen;
3859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  delete mPerFunctionPasses;
3869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  delete mPerModulePasses;
3879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  delete mCodeGenPasses;
3889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  return;
389462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao}
390e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines
391e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines}  // namespace slang
392