slang.cpp revision 7ac9d0de26d325071ad3f26f8d34514efca2d3d6
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.h" 18462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 196315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include <stdlib.h> 20462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 21e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <string> 22e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <vector> 238c6d9b2d36ed2d6d811279fd9bddc05fffe16803Zonr Chang 24e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/ASTConsumer.h" 25e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/ASTContext.h" 26462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 27be27482cdeaf08576bc39b72a15d35d13014a636Logan#include "clang/Basic/DiagnosticIDs.h" 2823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "clang/Basic/DiagnosticOptions.h" 29e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/Basic/FileManager.h" 30df5bcce1582d839eead432a5e24435236c90fb05Shih-wei Liao#include "clang/Basic/FileSystemOptions.h" 316315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "clang/Basic/LangOptions.h" 323a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang#include "clang/Basic/SourceManager.h" 333a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang#include "clang/Basic/TargetInfo.h" 346315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "clang/Basic/TargetOptions.h" 35462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 36e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/Frontend/CodeGenOptions.h" 37cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines#include "clang/Frontend/DependencyOutputOptions.h" 38cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines#include "clang/Frontend/FrontendDiagnostic.h" 390e4ee65a2ba6b32fcd16c5d4a57fc7542d4032b4Stephen Hines#include "clang/Frontend/FrontendOptions.h" 40e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/Frontend/TextDiagnosticPrinter.h" 41cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines#include "clang/Frontend/Utils.h" 42cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 433a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang#include "clang/Lex/Preprocessor.h" 4423c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "clang/Lex/PreprocessorOptions.h" 453a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang#include "clang/Lex/HeaderSearch.h" 4623c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "clang/Lex/HeaderSearchOptions.h" 473a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 486315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "clang/Parse/ParseAST.h" 496315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr 50be27482cdeaf08576bc39b72a15d35d13014a636Logan#include "llvm/ADT/IntrusiveRefCntPtr.h" 51be27482cdeaf08576bc39b72a15d35d13014a636Logan 52e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Bitcode/ReaderWriter.h" 539ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao 5408df36e5c0a990306d958c9f6d59a5de23b748f6Zonr Chang// More force linking 55ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray#include "llvm/Linker/Linker.h" 5608df36e5c0a990306d958c9f6d59a5de23b748f6Zonr Chang 57e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines// Force linking all passes/vmcore stuffs to libslang.so 580da7f6c8201b27938d3b9f048d71fd784cd1df9aStephen Hines#include "llvm/LinkAllIR.h" 59e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/LinkAllPasses.h" 60e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 61e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/raw_ostream.h" 62e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/MemoryBuffer.h" 63e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/ErrorHandling.h" 64e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/ManagedStatic.h" 65be27482cdeaf08576bc39b72a15d35d13014a636Logan#include "llvm/Support/Path.h" 669207a2e495c8363606861e4f034504ec5c153dabLogan Chien#include "llvm/Support/TargetSelect.h" 679207a2e495c8363606861e4f034504ec5c153dabLogan Chien#include "llvm/Support/ToolOutputFile.h" 68e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 696e6578a360497f78a181e63d7783422a9c9bfb15Stephen Hines#include "slang_assert.h" 70e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "slang_backend.h" 71e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "slang_utils.h" 72b81c6a4cbd9c08e0b20ea4fbc615b416ac1bc9ecShih-wei Liao 7308df36e5c0a990306d958c9f6d59a5de23b748f6Zonr Changnamespace { 7408df36e5c0a990306d958c9f6d59a5de23b748f6Zonr Chang 759ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hinesstatic const char *kRSTriple32 = "armv7-none-linux-gnueabi"; 769ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hinesstatic const char *kRSTriple64 = "aarch64-none-linux-gnueabi"; 779ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines 78e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hinesstruct ForceSlangLinking { 79e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines ForceSlangLinking() { 80e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // We must reference the functions in such a way that compilers will not 81e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // delete it all as dead code, even with whole program optimization, 82e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // yet is effectively a NO-OP. As the compiler isn't smart enough 83e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // to know that getenv() never returns -1, this will do the job. 84e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines if (std::getenv("bar") != reinterpret_cast<char*>(-1)) 85e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines return; 86e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 87e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // llvm-rs-link needs following functions existing in libslang. 88ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray llvm::parseBitcodeFile(NULL, llvm::getGlobalContext()); 899207a2e495c8363606861e4f034504ec5c153dabLogan Chien llvm::Linker::LinkModules(NULL, NULL, 0, NULL); 90e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 91e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines // llvm-rs-cc need this. 92e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines new clang::TextDiagnosticPrinter(llvm::errs(), 9323c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines new clang::DiagnosticOptions()); 94e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines } 95e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines} ForceSlangLinking; 96e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 97e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines} // namespace 98e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 99e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hinesnamespace slang { 1009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao 101462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaobool Slang::GlobalInitialized = false; 102462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1039ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao// Language option (define the language feature for compiler such as C99) 1049ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaoclang::LangOptions Slang::LangOpts; 105462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1066315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr// Code generation option for the compiler 1079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liaoclang::CodeGenOptions Slang::CodeGenOpts; 108462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao// The named of metadata node that pragma resides (should be synced with 1109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao// bcc.cpp) 111f52a620440fa62257dfdcf2583f0f9df5b855c76Shih-wei Liaoconst llvm::StringRef Slang::PragmaMetadataName = "#pragma"; 112462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1139207a2e495c8363606861e4f034504ec5c153dabLogan Chienstatic inline llvm::tool_output_file * 1149207a2e495c8363606861e4f034504ec5c153dabLogan ChienOpenOutputFile(const char *OutputFile, 115a1f95ee8df425089100148a42954cf61dc285015Stephen Hines llvm::sys::fs::OpenFlags Flags, 1169207a2e495c8363606861e4f034504ec5c153dabLogan Chien std::string* Error, 117e67239de8d94975e7e2216ee6860ae2e6cb8b15aStephen Hines clang::DiagnosticsEngine *DiagEngine) { 1189207a2e495c8363606861e4f034504ec5c153dabLogan Chien slangAssert((OutputFile != NULL) && (Error != NULL) && 1199207a2e495c8363606861e4f034504ec5c153dabLogan Chien (DiagEngine != NULL) && "Invalid parameter!"); 120e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 1216f4e0a9955a53a6f715af7e674e68ed15270a47cLogan Chien if (SlangUtils::CreateDirectoryWithParents( 1226f4e0a9955a53a6f715af7e674e68ed15270a47cLogan Chien llvm::sys::path::parent_path(OutputFile), Error)) { 123e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang llvm::tool_output_file *F = 124e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang new llvm::tool_output_file(OutputFile, *Error, Flags); 125e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang if (F != NULL) 126e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang return F; 127e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang } 128e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 129e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Report error here. 1309207a2e495c8363606861e4f034504ec5c153dabLogan Chien DiagEngine->Report(clang::diag::err_fe_error_opening) 1319207a2e495c8363606861e4f034504ec5c153dabLogan Chien << OutputFile << *Error; 132e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 133e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang return NULL; 134e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang} 135e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 136462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaovoid Slang::GlobalInitialization() { 1379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao if (!GlobalInitialized) { 1389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // We only support x86, x64 and ARM target 139462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // For ARM 1419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeARMTargetInfo(); 1429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeARMTarget(); 1439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeARMAsmPrinter(); 144462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // For x86 and x64 1469ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeX86TargetInfo(); 1479ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeX86Target(); 1489ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LLVMInitializeX86AsmPrinter(); 149462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1503a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang // Please refer to include/clang/Basic/LangOptions.h to setup 1519ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // the options. 1529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LangOpts.RTTI = 0; // Turn off the RTTI information support 153245eba133628ef5b52c78d6f538c9268c9bc5828Shih-wei Liao LangOpts.C99 = 1; 15452d132c53a57c3bb4b517f87ec4f0148ef8a4216Stephen Hines LangOpts.Renderscript = 1; 155f64e76657a98f66c0f3f26014c0a51f55204dc1cStephen Hines LangOpts.LaxVectorConversions = 0; // Do not bitcast vectors! 156194d4039a37861368d0774682b6d104f10bcc262Stephen Hines LangOpts.CharIsSigned = 1; // Signed char is our default. 157462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 158c460b37ffb50819a32c2a8967754b6f784b28263mkopec CodeGenOpts.OptimizationLevel = 3; 159462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao GlobalInitialized = true; 1619ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 162462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 163462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1644b3f3bada7155de983e7d92fa8b20091629b3bb3Stephen Hinesvoid Slang::LLVMErrorHandler(void *UserData, const std::string &Message, 1654b3f3bada7155de983e7d92fa8b20091629b3bb3Stephen Hines bool GenCrashDialog) { 1669207a2e495c8363606861e4f034504ec5c153dabLogan Chien clang::DiagnosticsEngine* DiagEngine = 1679207a2e495c8363606861e4f034504ec5c153dabLogan Chien static_cast<clang::DiagnosticsEngine *>(UserData); 1689207a2e495c8363606861e4f034504ec5c153dabLogan Chien 1699207a2e495c8363606861e4f034504ec5c153dabLogan Chien DiagEngine->Report(clang::diag::err_fe_error_backend) << Message; 1709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao exit(1); 171462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 172462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1739ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hinesvoid Slang::createTarget(uint32_t BitWidth) { 1749ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines std::vector<std::string> features; 175462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1769ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines if (BitWidth == 64) { 1779ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines mTargetOpts->Triple = kRSTriple64; 1789ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines } else { 1799ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines mTargetOpts->Triple = kRSTriple32; 1809ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines // Treat long as a 64-bit type for our 32-bit RS code. 1819ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines features.push_back("+long64"); 1829ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines mTargetOpts->FeaturesAsWritten = features; 1839ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines } 184b81c6a4cbd9c08e0b20ea4fbc615b416ac1bc9ecShih-wei Liao 1859207a2e495c8363606861e4f034504ec5c153dabLogan Chien mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagEngine, 1867ac9d0de26d325071ad3f26f8d34514efca2d3d6Stephen Hines mTargetOpts)); 187462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 188462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 1893a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Slang::createFileManager() { 190df5bcce1582d839eead432a5e24435236c90fb05Shih-wei Liao mFileSysOpt.reset(new clang::FileSystemOptions()); 191be27482cdeaf08576bc39b72a15d35d13014a636Logan mFileMgr.reset(new clang::FileManager(*mFileSysOpt)); 1923a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang} 1933a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 1943a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Slang::createSourceManager() { 1959207a2e495c8363606861e4f034504ec5c153dabLogan Chien mSourceMgr.reset(new clang::SourceManager(*mDiagEngine, *mFileMgr)); 1963a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang} 1973a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 19868e8e9f98730b3672fb96d49e33b9db70a90ed3fShih-wei Liaovoid Slang::createPreprocessor() { 1999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // Default only search header file in current dir 20023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HSOpts = 20123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines new clang::HeaderSearchOptions(); 20223c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines clang::HeaderSearch *HeaderInfo = new clang::HeaderSearch(HSOpts, 203eeb251e8db802b836af7461d0f6eb1252c527ab8Stephen Hines *mSourceMgr, 2040444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines *mDiagEngine, 2050444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines LangOpts, 2060444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines mTarget.get()); 2079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao 20823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> PPOpts = 20923c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines new clang::PreprocessorOptions(); 21023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines mPP.reset(new clang::Preprocessor(PPOpts, 21123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines *mDiagEngine, 2129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao LangOpts, 2139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao *mSourceMgr, 2149207a2e495c8363606861e4f034504ec5c153dabLogan Chien *HeaderInfo, 2159207a2e495c8363606861e4f034504ec5c153dabLogan Chien *this, 2169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao NULL, 2173a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang /* OwnsHeaderSearch = */true)); 2183fd0a94a5cf1656569b1aea07043cc63939dcb46Stephen Hines // Initialize the preprocessor 21975d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines mPP->Initialize(getTargetInfo()); 2200e4ee65a2ba6b32fcd16c5d4a57fc7542d4032b4Stephen Hines clang::FrontendOptions FEOpts; 2217ac9d0de26d325071ad3f26f8d34514efca2d3d6Stephen Hines clang::InitializePreprocessor(*mPP, *PPOpts, FEOpts); 2220e4ee65a2ba6b32fcd16c5d4a57fc7542d4032b4Stephen Hines 22368e8e9f98730b3672fb96d49e33b9db70a90ed3fShih-wei Liao mPragmas.clear(); 2243fd0a94a5cf1656569b1aea07043cc63939dcb46Stephen Hines mPP->AddPragmaHandler(new PragmaRecorder(&mPragmas)); 22568e8e9f98730b3672fb96d49e33b9db70a90ed3fShih-wei Liao 2269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao std::vector<clang::DirectoryLookup> SearchList; 2273a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) { 2289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao if (const clang::DirectoryEntry *DE = 229be27482cdeaf08576bc39b72a15d35d13014a636Logan mFileMgr->getDirectory(mIncludePaths[i])) { 2309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao SearchList.push_back(clang::DirectoryLookup(DE, 2319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao clang::SrcMgr::C_System, 2329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao false)); 2339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 2349089828a995422a5ed3beeed0890b755c4abe188Shih-wei Liao } 2359089828a995422a5ed3beeed0890b755c4abe188Shih-wei Liao 2369207a2e495c8363606861e4f034504ec5c153dabLogan Chien HeaderInfo->SetSearchPaths(SearchList, 2379207a2e495c8363606861e4f034504ec5c153dabLogan Chien /* angledDirIdx = */1, 2389207a2e495c8363606861e4f034504ec5c153dabLogan Chien /* systemDixIdx = */1, 2399207a2e495c8363606861e4f034504ec5c153dabLogan Chien /* noCurDirSearch = */false); 2409089828a995422a5ed3beeed0890b755c4abe188Shih-wei Liao 2413a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang initPreprocessor(); 24268e8e9f98730b3672fb96d49e33b9db70a90ed3fShih-wei Liao} 24368e8e9f98730b3672fb96d49e33b9db70a90ed3fShih-wei Liao 2443a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Slang::createASTContext() { 2453a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mASTContext.reset(new clang::ASTContext(LangOpts, 2463a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang *mSourceMgr, 2473a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mPP->getIdentifierTable(), 2483a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mPP->getSelectorTable(), 24975d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines mPP->getBuiltinInfo())); 25075d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines mASTContext->InitBuiltinTypes(getTargetInfo()); 2513a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang initASTContext(); 2523a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang} 2533a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 2549207a2e495c8363606861e4f034504ec5c153dabLogan Chienclang::ASTConsumer * 2559207a2e495c8363606861e4f034504ec5c153dabLogan ChienSlang::createBackend(const clang::CodeGenOptions& CodeGenOpts, 2569207a2e495c8363606861e4f034504ec5c153dabLogan Chien llvm::raw_ostream *OS, OutputType OT) { 25723c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines return new Backend(mDiagEngine, CodeGenOpts, getTargetOptions(), 2589207a2e495c8363606861e4f034504ec5c153dabLogan Chien &mPragmas, OS, OT); 2593a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang} 2603a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 2617ac9d0de26d325071ad3f26f8d34514efca2d3d6Stephen HinesSlang::Slang() : mInitialized(false), mDiagClient(NULL), 2627ac9d0de26d325071ad3f26f8d34514efca2d3d6Stephen Hines mTargetOpts(new clang::TargetOptions()), mOT(OT_Default) { 2639ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao GlobalInitialization(); 264641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang} 265641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang 2669ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hinesvoid Slang::init(uint32_t BitWidth, clang::DiagnosticsEngine *DiagEngine, 2678f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines DiagnosticBuffer *DiagClient) { 268641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang if (mInitialized) 269641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang return; 270462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 2718f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines mDiagEngine = DiagEngine; 2728f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines mDiagClient = DiagClient; 2738f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines mDiag.reset(new clang::Diagnostic(mDiagEngine)); 2748f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines initDiagnostic(); 2758f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagEngine); 276462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 2779ae18b2bbee0b08afd400542e863dd665ff76059Stephen Hines createTarget(BitWidth); 2789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao createFileManager(); 2799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao createSourceManager(); 280462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 281641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang mInitialized = true; 2829207a2e495c8363606861e4f034504ec5c153dabLogan Chien} 283641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang 28423c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hinesclang::ModuleLoadResult Slang::loadModule( 28523c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines clang::SourceLocation ImportLoc, 28623c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines clang::ModuleIdPath Path, 28723c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines clang::Module::NameVisibilityKind Visibility, 28823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines bool IsInclusionDirective) { 2899207a2e495c8363606861e4f034504ec5c153dabLogan Chien slangAssert(0 && "Not implemented"); 29023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines return clang::ModuleLoadResult(); 291462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 292462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 2933a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changbool Slang::setInputSource(llvm::StringRef InputFile, 2943a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang const char *Text, 2953a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang size_t TextLength) { 2963a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mInputFileName = InputFile.str(); 297462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 2989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // Reset the ID tables if we are reusing the SourceManager 2999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mSourceMgr->clearIDTables(); 300462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // Load the source 3026315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr llvm::MemoryBuffer *SB = 3033a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang llvm::MemoryBuffer::getMemBuffer(Text, Text + TextLength); 30475d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines mSourceMgr->setMainFileID(mSourceMgr->createFileID(SB)); 305462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao if (mSourceMgr->getMainFileID().isInvalid()) { 3079207a2e495c8363606861e4f034504ec5c153dabLogan Chien mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile; 3089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return false; 3099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 3109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return true; 311462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 312462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3133a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changbool Slang::setInputSource(llvm::StringRef InputFile) { 3143a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mInputFileName = InputFile.str(); 315462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mSourceMgr->clearIDTables(); 317462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 318be27482cdeaf08576bc39b72a15d35d13014a636Logan const clang::FileEntry *File = mFileMgr->getFile(InputFile); 31975d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines if (File) { 32075d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines mSourceMgr->setMainFileID(mSourceMgr->createFileID(File, 32175d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines clang::SourceLocation(), clang::SrcMgr::C_User)); 32275d471819f1810e0e65224d6e57fe1106af0d340Stephen Hines } 323462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao if (mSourceMgr->getMainFileID().isInvalid()) { 3259207a2e495c8363606861e4f034504ec5c153dabLogan Chien mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile; 3269ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return false; 3279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 328462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return true; 330462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 331462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3323a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changbool Slang::setOutput(const char *OutputFile) { 3339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao std::string Error; 334e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang llvm::tool_output_file *OS = NULL; 335462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3363a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang switch (mOT) { 337cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines case OT_Dependency: 3383a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang case OT_Assembly: 3393a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang case OT_LLVMAssembly: { 340ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, &Error, 341a1f95ee8df425089100148a42954cf61dc285015Stephen Hines mDiagEngine); 3429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao break; 343462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao } 3443a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang case OT_Nothing: { 3459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao break; 346462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao } 3473a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang case OT_Object: 348cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines case OT_Bitcode: { 349ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray OS = OpenOutputFile(OutputFile, llvm::sys::fs::F_None, 3508f4d972ef6c6796ffdde603e456979a7004d1e20Stephen Hines &Error, mDiagEngine); 3519ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao break; 3529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 3538c6d9b2d36ed2d6d811279fd9bddc05fffe16803Zonr Chang default: { 354cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines llvm_unreachable("Unknown compiler output type"); 3558c6d9b2d36ed2d6d811279fd9bddc05fffe16803Zonr Chang } 3569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao } 357462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 358e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang if (!Error.empty()) 3599ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return false; 360e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 361e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mOS.reset(OS); 3629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao 3633a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mOutputFileName = OutputFile; 364462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao return true; 366462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 367462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 3680b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hinesbool Slang::setDepOutput(const char *OutputFile) { 3690b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines std::string Error; 3708c6d9b2d36ed2d6d811279fd9bddc05fffe16803Zonr Chang 371a1f95ee8df425089100148a42954cf61dc285015Stephen Hines mDOS.reset( 372ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, &Error, mDiagEngine)); 373e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang if (!Error.empty() || (mDOS.get() == NULL)) 3740b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines return false; 3750b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines 3760b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines mDepOutputFileName = OutputFile; 3770b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines 3780b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines return true; 3790b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines} 3800b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines 381cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hinesint Slang::generateDepFile() { 3829207a2e495c8363606861e4f034504ec5c153dabLogan Chien if (mDiagEngine->hasErrorOccurred()) 383be27482cdeaf08576bc39b72a15d35d13014a636Logan return 1; 3840b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines if (mDOS.get() == NULL) 385f7de85223c8728d5dc147e8a19ddf039da72b431Stephen Hines return 1; 386cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 387e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Initialize options for generating dependency file 388cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines clang::DependencyOutputOptions DepOpts; 389cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines DepOpts.IncludeSystemHeaders = 1; 3900b7ef1a176b9ddd1e0d437efdce20743373fd7dbStephen Hines DepOpts.OutputFile = mDepOutputFileName; 391b81c6a4cbd9c08e0b20ea4fbc615b416ac1bc9ecShih-wei Liao DepOpts.Targets = mAdditionalDepTargets; 392cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines DepOpts.Targets.push_back(mDepTargetBCFileName); 3934cc67fce91f43215d61b2695746eab102a3db516Stephen Hines for (std::vector<std::string>::const_iterator 3944cc67fce91f43215d61b2695746eab102a3db516Stephen Hines I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end(); 3954cc67fce91f43215d61b2695746eab102a3db516Stephen Hines I != E; 3964cc67fce91f43215d61b2695746eab102a3db516Stephen Hines I++) { 3974cc67fce91f43215d61b2695746eab102a3db516Stephen Hines DepOpts.Targets.push_back(*I); 3984cc67fce91f43215d61b2695746eab102a3db516Stephen Hines } 3994cc67fce91f43215d61b2695746eab102a3db516Stephen Hines mGeneratedFileNames.clear(); 400cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 401e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Per-compilation needed initialization 402cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines createPreprocessor(); 403ee4016d1247d3fbe50822de279d3da273d8aef4cTim Murray clang::DependencyFileGenerator::CreateAndAttachToPreprocessor(*mPP.get(), DepOpts); 404cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 405e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Inform the diagnostic client we are processing a source file 406cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines mDiagClient->BeginSourceFile(LangOpts, mPP.get()); 407cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 408e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Go through the source file (no operations necessary) 409cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines clang::Token Tok; 410cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines mPP->EnterMainSourceFile(); 411cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines do { 412cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines mPP->Lex(Tok); 413cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines } while (Tok.isNot(clang::tok::eof)); 414cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 415cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines mPP->EndSourceFile(); 416cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 417e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Declare success if no error 4189207a2e495c8363606861e4f034504ec5c153dabLogan Chien if (!mDiagEngine->hasErrorOccurred()) 419e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mDOS->keep(); 420e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 421e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Clean up after compilation 422cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines mPP.reset(); 423e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mDOS.reset(); 424cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 4259207a2e495c8363606861e4f034504ec5c153dabLogan Chien return mDiagEngine->hasErrorOccurred() ? 1 : 0; 426cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines} 427cc0efad052e944f64bb71cfdaa8a825e30192e78Stephen Hines 428462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaoint Slang::compile() { 4299207a2e495c8363606861e4f034504ec5c153dabLogan Chien if (mDiagEngine->hasErrorOccurred()) 430be27482cdeaf08576bc39b72a15d35d13014a636Logan return 1; 431f7de85223c8728d5dc147e8a19ddf039da72b431Stephen Hines if (mOS.get() == NULL) 432f7de85223c8728d5dc147e8a19ddf039da72b431Stephen Hines return 1; 433462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 4349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // Here is per-compilation needed initialization 4359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao createPreprocessor(); 4369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao createASTContext(); 4373a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang 438df5bcce1582d839eead432a5e24435236c90fb05Shih-wei Liao mBackend.reset(createBackend(CodeGenOpts, &mOS->os(), mOT)); 439462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 4409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // Inform the diagnostic client we are processing a source file 4419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mDiagClient->BeginSourceFile(LangOpts, mPP.get()); 442462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 4439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao // The core of the slang compiler 4449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao ParseAST(*mPP, mBackend.get(), *mASTContext); 445462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 446e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Inform the diagnostic client we are done with previous source file 447e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mDiagClient->EndSourceFile(); 448e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 449e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang // Declare success if no error 4509207a2e495c8363606861e4f034504ec5c153dabLogan Chien if (!mDiagEngine->hasErrorOccurred()) 451e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mOS->keep(); 452e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang 4533a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang // The compilation ended, clear 4549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mBackend.reset(); 4559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mASTContext.reset(); 4569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao mPP.reset(); 457e8c263a1c5df81594cf302ecadd813909c894487Zonr Chang mOS.reset(); 458462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 4599207a2e495c8363606861e4f034504ec5c153dabLogan Chien return mDiagEngine->hasErrorOccurred() ? 1 : 0; 460462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 461462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 462c460b37ffb50819a32c2a8967754b6f784b28263mkopecvoid Slang::setDebugMetadataEmission(bool EmitDebug) { 46343730fe3c839af391efe6bdf56b0479860121924Shih-wei Liao if (EmitDebug) 46423c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines CodeGenOpts.setDebugInfo(clang::CodeGenOptions::FullDebugInfo); 46543730fe3c839af391efe6bdf56b0479860121924Shih-wei Liao else 46623c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines CodeGenOpts.setDebugInfo(clang::CodeGenOptions::NoDebugInfo); 467c460b37ffb50819a32c2a8967754b6f784b28263mkopec} 468c460b37ffb50819a32c2a8967754b6f784b28263mkopec 469c460b37ffb50819a32c2a8967754b6f784b28263mkopecvoid Slang::setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel) { 470c460b37ffb50819a32c2a8967754b6f784b28263mkopec CodeGenOpts.OptimizationLevel = OptimizationLevel; 471c460b37ffb50819a32c2a8967754b6f784b28263mkopec} 472c460b37ffb50819a32c2a8967754b6f784b28263mkopec 4733a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Changvoid Slang::reset() { 474c632be206ac4fe49a5db05cfa54942d774329dbeStephen Hines llvm::errs() << mDiagClient->str(); 4759207a2e495c8363606861e4f034504ec5c153dabLogan Chien mDiagEngine->Reset(); 4763a9ca1f0d6bd8f12c2bb2adea51f95c255996180Zonr Chang mDiagClient->reset(); 477462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 478462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao 479462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei LiaoSlang::~Slang() { 480462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao} 481e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines 482e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines} // namespace slang 483