zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #include "slang.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame^] | 5 | // Force linking all passes/vmcore stuffs to libslang.so |
| 6 | #include "llvm/LinkAllPasses.h" |
| 7 | #include "llvm/LinkAllVMCore.h" |
| 8 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 9 | #include "llvm/Target/TargetSelect.h" |
| 10 | |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 11 | #include "llvm/System/Path.h" |
| 12 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 14 | #include "llvm/Support/MemoryBuffer.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include "llvm/Support/ManagedStatic.h" |
| 17 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 18 | #include "clang/Basic/LangOptions.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
| 20 | #include "clang/Basic/TargetInfo.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 21 | #include "clang/Basic/TargetOptions.h" |
| 22 | |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 23 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 25 | #include "clang/Frontend/Utils.h" |
| 26 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
| 28 | #include "clang/Lex/HeaderSearch.h" |
| 29 | |
| 30 | #include "clang/AST/ASTConsumer.h" |
| 31 | #include "clang/AST/ASTContext.h" |
| 32 | |
| 33 | #include "clang/Basic/FileManager.h" |
| 34 | |
| 35 | #include "clang/Frontend/CodeGenOptions.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 36 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 37 | |
| 38 | #include "clang/Parse/ParseAST.h" |
| 39 | |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 40 | #include "slang_utils.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 41 | #include "slang_backend.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 42 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame^] | 43 | // More force linking |
| 44 | #include "llvm/Linker.h" |
| 45 | #include "llvm/Bitcode/ReaderWriter.h" |
| 46 | |
| 47 | namespace { |
| 48 | struct ForceSlangLinking { |
| 49 | ForceSlangLinking() { |
| 50 | // We must reference the functions in such a way that compilers will not |
| 51 | // delete it all as dead code, even with whole program optimization, |
| 52 | // yet is effectively a NO-OP. As the compiler isn't smart enough |
| 53 | // to know that getenv() never returns -1, this will do the job. |
| 54 | if (std::getenv("bar") != reinterpret_cast<char*>(-1)) |
| 55 | return; |
| 56 | |
| 57 | // llvm-rs-link needs following functions existing in libslang. |
| 58 | llvm::ParseBitcodeFile(NULL, llvm::getGlobalContext(), NULL); |
| 59 | llvm::Linker::LinkModules(NULL, NULL, NULL); |
| 60 | } |
| 61 | } ForceSlangLinking; |
| 62 | } |
| 63 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 64 | using namespace slang; |
| 65 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 66 | #if defined(__arm__) |
| 67 | # define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi" |
| 68 | #elif defined(__x86_64__) |
| 69 | # define DEFAULT_TARGET_TRIPLE_STRING "x86_64-unknown-linux" |
| 70 | #else |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 71 | // let's use x86 as default target |
| 72 | # define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 | bool Slang::GlobalInitialized = false; |
| 76 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 77 | // Language option (define the language feature for compiler such as C99) |
| 78 | clang::LangOptions Slang::LangOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 79 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 80 | // Code generation option for the compiler |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 81 | clang::CodeGenOptions Slang::CodeGenOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 82 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 83 | const std::string Slang::TargetDescription = |
| 84 | "e-" // little-endian |
| 85 | "p:32:32:32-" // 32-bit pointer |
| 86 | "i1:8:8-" |
| 87 | "i8:8:8-" |
| 88 | "i16:16:16-" |
| 89 | "i32:32:32-" |
| 90 | "i64:64:64-" |
| 91 | "f32:32:32-" |
| 92 | "f64:64:64-" |
| 93 | "v64:64:64-" // 64-bit vector (e.g. float2, int2, short4) |
| 94 | "v128:128:128-" |
| 95 | "a0:0:64-" |
| 96 | "n32"; // native CPU only support 32-bit integer width. |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 97 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 98 | // The named of metadata node that pragma resides (should be synced with |
| 99 | // bcc.cpp) |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 100 | const llvm::StringRef Slang::PragmaMetadataName = "#pragma"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 101 | |
| 102 | void Slang::GlobalInitialization() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 103 | if (!GlobalInitialized) { |
| 104 | // We only support x86, x64 and ARM target |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 105 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 106 | // For ARM |
| 107 | LLVMInitializeARMTargetInfo(); |
| 108 | LLVMInitializeARMTarget(); |
| 109 | LLVMInitializeARMAsmPrinter(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 110 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 111 | // For x86 and x64 |
| 112 | LLVMInitializeX86TargetInfo(); |
| 113 | LLVMInitializeX86Target(); |
| 114 | LLVMInitializeX86AsmPrinter(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 116 | // Please refer to include/clang/Basic/LangOptions.h to setup |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 117 | // the options. |
| 118 | LangOpts.RTTI = 0; // Turn off the RTTI information support |
| 119 | LangOpts.NeXTRuntime = 0; // Turn off the NeXT runtime uses |
| 120 | LangOpts.Bool = 1; // Turn on 'bool', 'true', 'false' keywords |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 121 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 122 | CodeGenOpts.OptimizationLevel = 3; /* -O3 */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 123 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | GlobalInitialized = true; |
| 125 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 126 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 127 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void Slang::LLVMErrorHandler(void *UserData, const std::string &Message) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 131 | clang::Diagnostic* Diags = static_cast<clang::Diagnostic*>(UserData); |
| 132 | Diags->Report(clang::diag::err_fe_error_backend) << Message; |
| 133 | exit(1); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 136 | void Slang::createDiagnostic() { |
| 137 | mDiagnostics = |
| 138 | llvm::IntrusiveRefCntPtr<clang::Diagnostic>(new clang::Diagnostic()); |
| 139 | mDiagClient = new DiagnosticBuffer(); |
| 140 | // This takes the ownership of mDiagClient. |
| 141 | mDiagnostics->setClient(mDiagClient); |
| 142 | return; |
| 143 | } |
| 144 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 145 | void Slang::createTarget(const char* Triple, const char* CPU, |
| 146 | const char** Features) { |
| 147 | if (Triple != NULL) |
| 148 | mTargetOpts.Triple = Triple; |
| 149 | else |
| 150 | mTargetOpts.Triple = DEFAULT_TARGET_TRIPLE_STRING; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 151 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 152 | if (CPU != NULL) |
| 153 | mTargetOpts.CPU = CPU; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 155 | mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagnostics, |
| 156 | mTargetOpts)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 157 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 158 | if (Features != NULL) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 159 | for (int i = 0; Features[i] != NULL; i++) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 160 | mTargetOpts.Features.push_back(Features[i]); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 161 | |
| 162 | return; |
| 163 | } |
| 164 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 165 | void Slang::createFileManager() { |
| 166 | mFileMgr.reset(new clang::FileManager()); |
| 167 | } |
| 168 | |
| 169 | void Slang::createSourceManager() { |
| 170 | mSourceMgr.reset(new clang::SourceManager(*mDiagnostics)); |
| 171 | return; |
| 172 | } |
| 173 | |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 174 | void Slang::createPreprocessor() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 175 | // Default only search header file in current dir |
| 176 | clang::HeaderSearch *HS = new clang::HeaderSearch(*mFileMgr); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 177 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 178 | mPP.reset(new clang::Preprocessor(*mDiagnostics, |
| 179 | LangOpts, |
| 180 | *mTarget, |
| 181 | *mSourceMgr, |
| 182 | *HS, |
| 183 | NULL, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 184 | /* OwnsHeaderSearch = */true)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 185 | // Initialize the prepocessor |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 186 | mPragmas.clear(); |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 187 | mPP->AddPragmaHandler(new PragmaRecorder(mPragmas)); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 188 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 189 | std::vector<clang::DirectoryLookup> SearchList; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 190 | for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 191 | if (const clang::DirectoryEntry *DE = |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 192 | mFileMgr->getDirectory(mIncludePaths[i])) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 193 | SearchList.push_back(clang::DirectoryLookup(DE, |
| 194 | clang::SrcMgr::C_System, |
| 195 | false, |
| 196 | false)); |
| 197 | } |
Shih-wei Liao | 9089828 | 2010-07-19 18:38:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | HS->SetSearchPaths(SearchList, 1, false); |
| 201 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 202 | initPreprocessor(); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 206 | void Slang::createASTContext() { |
| 207 | mASTContext.reset(new clang::ASTContext(LangOpts, |
| 208 | *mSourceMgr, |
| 209 | *mTarget, |
| 210 | mPP->getIdentifierTable(), |
| 211 | mPP->getSelectorTable(), |
| 212 | mPP->getBuiltinInfo(), |
| 213 | /* size_reserve = */0)); |
| 214 | initASTContext(); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | clang::ASTConsumer |
| 219 | *Slang::createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| 220 | llvm::raw_ostream *OS, |
| 221 | OutputType OT) { |
| 222 | return new Backend(*mDiagnostics, |
| 223 | CodeGenOpts, |
| 224 | mTargetOpts, |
| 225 | mPragmas, |
| 226 | OS, |
| 227 | OT); |
| 228 | } |
| 229 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 230 | Slang::Slang(const char *Triple, const char *CPU, const char **Features) |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 231 | : mDiagClient(NULL), |
| 232 | mOT(OT_Default) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 233 | GlobalInitialization(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 234 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 235 | createDiagnostic(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 236 | llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagnostics.getPtr()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 237 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 238 | createTarget(Triple, CPU, Features); |
| 239 | createFileManager(); |
| 240 | createSourceManager(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 241 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 242 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 245 | bool Slang::setInputSource(llvm::StringRef InputFile, |
| 246 | const char *Text, |
| 247 | size_t TextLength) { |
| 248 | mInputFileName = InputFile.str(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 249 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 250 | // Reset the ID tables if we are reusing the SourceManager |
| 251 | mSourceMgr->clearIDTables(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 252 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 253 | // Load the source |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 254 | llvm::MemoryBuffer *SB = |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 255 | llvm::MemoryBuffer::getMemBuffer(Text, Text + TextLength); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 256 | mSourceMgr->createMainFileIDForMemBuffer(SB); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 257 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 258 | if (mSourceMgr->getMainFileID().isInvalid()) { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 259 | mDiagnostics->Report(clang::diag::err_fe_error_reading) << InputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 265 | bool Slang::setInputSource(llvm::StringRef InputFile) { |
| 266 | mInputFileName = InputFile.str(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 267 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 268 | mSourceMgr->clearIDTables(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 269 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 270 | const clang::FileEntry *File = mFileMgr->getFile(InputFile); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 271 | if (File) |
| 272 | mSourceMgr->createMainFileID(File); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 273 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 274 | if (mSourceMgr->getMainFileID().isInvalid()) { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 275 | mDiagnostics->Report(clang::diag::err_fe_error_reading) << InputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 276 | return false; |
| 277 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 278 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 279 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 282 | bool Slang::setOutput(const char *OutputFile) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 283 | llvm::sys::Path OutputFilePath(OutputFile); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 284 | std::string Error; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 285 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 286 | switch (mOT) { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 287 | case OT_Dependency: |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 288 | case OT_Assembly: |
| 289 | case OT_LLVMAssembly: { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 290 | if (!SlangUtils::CreateDirectoryWithParents(OutputFilePath.getDirname(), |
| 291 | &Error)) |
| 292 | mDiagnostics->Report(clang::diag::err_fe_error_opening) << OutputFile |
| 293 | << Error; |
| 294 | mOS.reset(new llvm::tool_output_file(OutputFile, Error, 0)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 295 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 296 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 297 | case OT_Nothing: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 298 | mOS.reset(); |
| 299 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 300 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 301 | case OT_Object: |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 302 | case OT_Bitcode: { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 303 | if (!SlangUtils::CreateDirectoryWithParents(OutputFilePath.getDirname(), |
| 304 | &Error)) |
| 305 | mDiagnostics->Report(clang::diag::err_fe_error_opening) << OutputFile |
| 306 | << Error; |
| 307 | mOS.reset(new llvm::tool_output_file(OutputFile, |
| 308 | Error, |
| 309 | llvm::raw_fd_ostream::F_Binary)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 310 | break; |
| 311 | } |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 312 | default: { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 313 | llvm_unreachable("Unknown compiler output type"); |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 314 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 315 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 316 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 317 | if (!Error.empty()) { |
| 318 | mOS.reset(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 319 | mDiagnostics->Report(clang::diag::err_fe_error_opening) << OutputFile |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 320 | << Error; |
| 321 | return false; |
| 322 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 323 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 324 | mOutputFileName = OutputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 325 | |
| 326 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 329 | bool Slang::setDepOutput(const char *OutputFile) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 330 | llvm::sys::Path OutputFilePath(OutputFile); |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 331 | std::string Error; |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 332 | |
| 333 | if (!SlangUtils::CreateDirectoryWithParents(OutputFilePath.getDirname(), |
| 334 | &Error)) |
| 335 | mDiagnostics->Report(clang::diag::err_fe_error_opening) << OutputFile |
| 336 | << Error; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 337 | mDOS.reset(new llvm::raw_fd_ostream(OutputFile, Error, 0)); |
| 338 | |
| 339 | if (!Error.empty()) { |
| 340 | mDOS.reset(); |
| 341 | mDiagnostics->Report(clang::diag::err_fe_error_opening) << OutputFile |
| 342 | << Error; |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | mDepOutputFileName = OutputFile; |
| 347 | |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | bool Slang::setDepTargetBC(const char *TargetBCFile) { |
| 352 | mDepTargetBCFileName = TargetBCFile; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Ying Wang | 5457231 | 2010-10-07 18:27:09 -0700 | [diff] [blame] | 357 | bool Slang::setAdditionalDepTarget(const char* AdditionalDepTargetFileName) { |
| 358 | mAdditionalDepTargetFileName = AdditionalDepTargetFileName; |
| 359 | } |
| 360 | |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 361 | int Slang::generateDepFile() { |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 362 | if(mDiagnostics->getNumErrors() > 0) |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 363 | return mDiagnostics->getNumErrors(); |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 364 | if (mDOS.get() == NULL) |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 365 | return 1; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 366 | |
| 367 | /* Initialize options for generating dependency file */ |
| 368 | clang::DependencyOutputOptions DepOpts; |
| 369 | DepOpts.IncludeSystemHeaders = 1; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 370 | DepOpts.OutputFile = mDepOutputFileName; |
Ying Wang | 5457231 | 2010-10-07 18:27:09 -0700 | [diff] [blame] | 371 | if (!mAdditionalDepTargetFileName.empty()) { |
| 372 | DepOpts.Targets.push_back(mAdditionalDepTargetFileName); |
| 373 | } |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 374 | DepOpts.Targets.push_back(mDepTargetBCFileName); |
| 375 | |
| 376 | /* Per-compilation needed initialization */ |
| 377 | createPreprocessor(); |
| 378 | AttachDependencyFileGen(*mPP.get(), DepOpts); |
| 379 | |
| 380 | /* Inform the diagnostic client we are processing a source file */ |
| 381 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 382 | |
| 383 | /* Go through the source file (no operations necessary) */ |
| 384 | clang::Token Tok; |
| 385 | mPP->EnterMainSourceFile(); |
| 386 | do { |
| 387 | mPP->Lex(Tok); |
| 388 | } while (Tok.isNot(clang::tok::eof)); |
| 389 | |
| 390 | mPP->EndSourceFile(); |
| 391 | |
| 392 | /* Clean up after compilation */ |
| 393 | mPP.reset(); |
| 394 | |
| 395 | return mDiagnostics->getNumErrors(); |
| 396 | } |
| 397 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 398 | int Slang::compile() { |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 399 | if (mDiagnostics->getNumErrors() > 0) |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 400 | return mDiagnostics->getNumErrors(); |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 401 | if (mOS.get() == NULL) |
| 402 | return 1; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 403 | |
| 404 | // Here is per-compilation needed initialization |
| 405 | createPreprocessor(); |
| 406 | createASTContext(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 407 | |
| 408 | mBackend.reset(createBackend(CodeGenOpts, mOS.take(), mOT)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 409 | |
| 410 | // Inform the diagnostic client we are processing a source file |
| 411 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 412 | |
| 413 | // The core of the slang compiler |
| 414 | ParseAST(*mPP, mBackend.get(), *mASTContext); |
| 415 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 416 | // The compilation ended, clear |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 417 | mBackend.reset(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 418 | mASTContext.reset(); |
| 419 | mPP.reset(); |
| 420 | |
| 421 | // Inform the diagnostic client we are done with previous source file |
| 422 | mDiagClient->EndSourceFile(); |
| 423 | |
| 424 | return mDiagnostics->getNumErrors(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 427 | void Slang::reset() { |
| 428 | mDiagnostics->Reset(); |
| 429 | mDiagClient->reset(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 430 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | Slang::~Slang() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 434 | llvm::llvm_shutdown(); |
| 435 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 436 | } |