Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "clang/AST/ASTConsumer.h" |
| 25 | #include "clang/AST/ASTContext.h" |
| 26 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 27 | #include "clang/Basic/DiagnosticIDs.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 28 | #include "clang/Basic/FileManager.h" |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 29 | #include "clang/Basic/FileSystemOptions.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 30 | #include "clang/Basic/LangOptions.h" |
| 31 | #include "clang/Basic/SourceManager.h" |
| 32 | #include "clang/Basic/TargetInfo.h" |
| 33 | #include "clang/Basic/TargetOptions.h" |
| 34 | |
| 35 | #include "clang/Frontend/CodeGenOptions.h" |
| 36 | #include "clang/Frontend/DiagnosticOptions.h" |
| 37 | #include "clang/Frontend/DependencyOutputOptions.h" |
| 38 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 39 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 40 | #include "clang/Frontend/Utils.h" |
| 41 | |
| 42 | #include "clang/Lex/Preprocessor.h" |
| 43 | #include "clang/Lex/HeaderSearch.h" |
| 44 | |
| 45 | #include "clang/Parse/ParseAST.h" |
| 46 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 47 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 48 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 49 | #include "llvm/Bitcode/ReaderWriter.h" |
| 50 | |
| 51 | // More force linking |
| 52 | #include "llvm/Linker.h" |
| 53 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 54 | // Force linking all passes/vmcore stuffs to libslang.so |
| 55 | #include "llvm/LinkAllPasses.h" |
| 56 | #include "llvm/LinkAllVMCore.h" |
| 57 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 58 | #include "llvm/Support/raw_ostream.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 59 | #include "llvm/Support/MemoryBuffer.h" |
| 60 | #include "llvm/Support/ErrorHandling.h" |
| 61 | #include "llvm/Support/ManagedStatic.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 62 | #include "llvm/Support/Path.h" |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 63 | #include "llvm/Support/TargetSelect.h" |
| 64 | #include "llvm/Support/ToolOutputFile.h" |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 65 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 66 | #include "slang_assert.h" |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 67 | #include "slang_backend.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 68 | #include "slang_utils.h" |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 69 | |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 70 | namespace { |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 71 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 72 | struct ForceSlangLinking { |
| 73 | ForceSlangLinking() { |
| 74 | // We must reference the functions in such a way that compilers will not |
| 75 | // delete it all as dead code, even with whole program optimization, |
| 76 | // yet is effectively a NO-OP. As the compiler isn't smart enough |
| 77 | // to know that getenv() never returns -1, this will do the job. |
| 78 | if (std::getenv("bar") != reinterpret_cast<char*>(-1)) |
| 79 | return; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 80 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 81 | // llvm-rs-link needs following functions existing in libslang. |
| 82 | llvm::ParseBitcodeFile(NULL, llvm::getGlobalContext(), NULL); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 83 | llvm::Linker::LinkModules(NULL, NULL, 0, NULL); |
Zonr Chang | 08df36e | 2010-10-07 18:50:42 +0800 | [diff] [blame] | 84 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 85 | // llvm-rs-cc need this. |
| 86 | new clang::TextDiagnosticPrinter(llvm::errs(), |
| 87 | clang::DiagnosticOptions()); |
| 88 | } |
| 89 | } ForceSlangLinking; |
| 90 | |
| 91 | } // namespace |
| 92 | |
| 93 | namespace slang { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 94 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 95 | #if defined(__arm__) |
| 96 | # define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi" |
| 97 | #elif defined(__x86_64__) |
| 98 | # define DEFAULT_TARGET_TRIPLE_STRING "x86_64-unknown-linux" |
| 99 | #else |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 100 | // let's use x86 as default target |
| 101 | # define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 102 | #endif |
| 103 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 104 | bool Slang::GlobalInitialized = false; |
| 105 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 106 | // Language option (define the language feature for compiler such as C99) |
| 107 | clang::LangOptions Slang::LangOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 108 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 109 | // Code generation option for the compiler |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 110 | clang::CodeGenOptions Slang::CodeGenOpts; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 111 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 112 | // The named of metadata node that pragma resides (should be synced with |
| 113 | // bcc.cpp) |
Shih-wei Liao | f52a620 | 2010-09-10 17:40:53 -0700 | [diff] [blame] | 114 | const llvm::StringRef Slang::PragmaMetadataName = "#pragma"; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 115 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 116 | static inline llvm::tool_output_file * |
| 117 | OpenOutputFile(const char *OutputFile, |
| 118 | unsigned Flags, |
| 119 | std::string* Error, |
| 120 | clang::DiagnosticsEngine *DiagEngine) |
| 121 | { |
| 122 | slangAssert((OutputFile != NULL) && (Error != NULL) && |
| 123 | (DiagEngine != NULL) && "Invalid parameter!"); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 124 | |
Logan Chien | 6f4e0a9 | 2011-03-10 01:10:25 +0800 | [diff] [blame] | 125 | if (SlangUtils::CreateDirectoryWithParents( |
| 126 | llvm::sys::path::parent_path(OutputFile), Error)) { |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 127 | llvm::tool_output_file *F = |
| 128 | new llvm::tool_output_file(OutputFile, *Error, Flags); |
| 129 | if (F != NULL) |
| 130 | return F; |
| 131 | } |
| 132 | |
| 133 | // Report error here. |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 134 | DiagEngine->Report(clang::diag::err_fe_error_opening) |
| 135 | << OutputFile << *Error; |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 140 | void Slang::GlobalInitialization() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 141 | if (!GlobalInitialized) { |
| 142 | // We only support x86, x64 and ARM target |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 143 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 144 | // For ARM |
| 145 | LLVMInitializeARMTargetInfo(); |
| 146 | LLVMInitializeARMTarget(); |
| 147 | LLVMInitializeARMAsmPrinter(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 148 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 149 | // For x86 and x64 |
| 150 | LLVMInitializeX86TargetInfo(); |
| 151 | LLVMInitializeX86Target(); |
| 152 | LLVMInitializeX86AsmPrinter(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 153 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 154 | // Please refer to include/clang/Basic/LangOptions.h to setup |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 155 | // the options. |
| 156 | LangOpts.RTTI = 0; // Turn off the RTTI information support |
| 157 | LangOpts.NeXTRuntime = 0; // Turn off the NeXT runtime uses |
Shih-wei Liao | 245eba1 | 2011-01-19 02:14:07 -0800 | [diff] [blame] | 158 | LangOpts.C99 = 1; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 159 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 160 | CodeGenOpts.OptimizationLevel = 3; /* -O3 */ |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 161 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 162 | GlobalInitialized = true; |
| 163 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void Slang::LLVMErrorHandler(void *UserData, const std::string &Message) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 167 | clang::DiagnosticsEngine* DiagEngine = |
| 168 | static_cast<clang::DiagnosticsEngine *>(UserData); |
| 169 | |
| 170 | DiagEngine->Report(clang::diag::err_fe_error_backend) << Message; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 171 | exit(1); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 174 | void Slang::createDiagnostic() { |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 175 | mDiagClient = new DiagnosticBuffer(); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 176 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 177 | mDiagIDs = new clang::DiagnosticIDs(); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 178 | mDiagEngine = new clang::DiagnosticsEngine(mDiagIDs, mDiagClient, true); |
| 179 | mDiag.reset(new clang::Diagnostic(mDiagEngine.getPtr())); |
| 180 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 181 | initDiagnostic(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 184 | void Slang::createTarget(const std::string &Triple, const std::string &CPU, |
| 185 | const std::vector<std::string> &Features) { |
| 186 | if (!Triple.empty()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 187 | mTargetOpts.Triple = Triple; |
| 188 | else |
| 189 | mTargetOpts.Triple = DEFAULT_TARGET_TRIPLE_STRING; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 190 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 191 | if (!CPU.empty()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 192 | mTargetOpts.CPU = CPU; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 193 | |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 194 | if (!Features.empty()) |
| 195 | mTargetOpts.Features = Features; |
| 196 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 197 | mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 198 | mTargetOpts)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 201 | void Slang::createFileManager() { |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 202 | mFileSysOpt.reset(new clang::FileSystemOptions()); |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 203 | mFileMgr.reset(new clang::FileManager(*mFileSysOpt)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void Slang::createSourceManager() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 207 | mSourceMgr.reset(new clang::SourceManager(*mDiagEngine, *mFileMgr)); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 210 | void Slang::createPreprocessor() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 211 | // Default only search header file in current dir |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 212 | clang::HeaderSearch *HeaderInfo = new clang::HeaderSearch(*mFileMgr); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 213 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 214 | mPP.reset(new clang::Preprocessor(*mDiagEngine, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 215 | LangOpts, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 216 | mTarget.get(), |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 217 | *mSourceMgr, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 218 | *HeaderInfo, |
| 219 | *this, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 220 | NULL, |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 221 | /* OwnsHeaderSearch = */true)); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 222 | // Initialize the preprocessor |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 223 | mPragmas.clear(); |
Stephen Hines | 3fd0a94 | 2011-01-18 12:27:39 -0800 | [diff] [blame] | 224 | mPP->AddPragmaHandler(new PragmaRecorder(&mPragmas)); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 225 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 226 | std::vector<clang::DirectoryLookup> SearchList; |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 227 | for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 228 | if (const clang::DirectoryEntry *DE = |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 229 | mFileMgr->getDirectory(mIncludePaths[i])) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 230 | SearchList.push_back(clang::DirectoryLookup(DE, |
| 231 | clang::SrcMgr::C_System, |
| 232 | false, |
| 233 | false)); |
| 234 | } |
Shih-wei Liao | 9089828 | 2010-07-19 18:38:57 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 237 | HeaderInfo->SetSearchPaths(SearchList, |
| 238 | /* angledDirIdx = */1, |
| 239 | /* systemDixIdx = */1, |
| 240 | /* noCurDirSearch = */false); |
Shih-wei Liao | 9089828 | 2010-07-19 18:38:57 -0700 | [diff] [blame] | 241 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 242 | initPreprocessor(); |
Shih-wei Liao | 68e8e9f | 2010-07-18 18:46:49 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 245 | void Slang::createASTContext() { |
| 246 | mASTContext.reset(new clang::ASTContext(LangOpts, |
| 247 | *mSourceMgr, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 248 | mTarget.get(), |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 249 | mPP->getIdentifierTable(), |
| 250 | mPP->getSelectorTable(), |
| 251 | mPP->getBuiltinInfo(), |
| 252 | /* size_reserve = */0)); |
| 253 | initASTContext(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 254 | } |
| 255 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 256 | clang::ASTConsumer * |
| 257 | Slang::createBackend(const clang::CodeGenOptions& CodeGenOpts, |
| 258 | llvm::raw_ostream *OS, OutputType OT) { |
| 259 | return new Backend(mDiagEngine.getPtr(), CodeGenOpts, mTargetOpts, |
| 260 | &mPragmas, OS, OT); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 263 | Slang::Slang() : mInitialized(false), mDiagClient(NULL), mOT(OT_Default) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 264 | GlobalInitialization(); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Slang::init(const std::string &Triple, const std::string &CPU, |
| 268 | const std::vector<std::string> &Features) { |
| 269 | if (mInitialized) |
| 270 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 271 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 272 | createDiagnostic(); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 273 | llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagEngine.getPtr()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 274 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 275 | createTarget(Triple, CPU, Features); |
| 276 | createFileManager(); |
| 277 | createSourceManager(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 278 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 279 | mInitialized = true; |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 280 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 281 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 282 | clang::ModuleKey Slang::loadModule(clang::SourceLocation ImportLoc, |
| 283 | clang::IdentifierInfo &ModuleName, |
| 284 | clang::SourceLocation ModuleNameLoc) { |
| 285 | //FIXME: Don't we have to implement this? |
| 286 | slangAssert(0 && "Not implemented"); |
| 287 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 290 | bool Slang::setInputSource(llvm::StringRef InputFile, |
| 291 | const char *Text, |
| 292 | size_t TextLength) { |
| 293 | mInputFileName = InputFile.str(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 294 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 295 | // Reset the ID tables if we are reusing the SourceManager |
| 296 | mSourceMgr->clearIDTables(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 297 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 298 | // Load the source |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 299 | llvm::MemoryBuffer *SB = |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 300 | llvm::MemoryBuffer::getMemBuffer(Text, Text + TextLength); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 301 | mSourceMgr->createMainFileIDForMemBuffer(SB); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 302 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 303 | if (mSourceMgr->getMainFileID().isInvalid()) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 304 | mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 310 | bool Slang::setInputSource(llvm::StringRef InputFile) { |
| 311 | mInputFileName = InputFile.str(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 312 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 313 | mSourceMgr->clearIDTables(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 314 | |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 315 | const clang::FileEntry *File = mFileMgr->getFile(InputFile); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 316 | if (File) |
| 317 | mSourceMgr->createMainFileID(File); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 318 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 319 | if (mSourceMgr->getMainFileID().isInvalid()) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 320 | mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 321 | return false; |
| 322 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 323 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 324 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 327 | bool Slang::setOutput(const char *OutputFile) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 328 | llvm::sys::Path OutputFilePath(OutputFile); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 329 | std::string Error; |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 330 | llvm::tool_output_file *OS = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 331 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 332 | switch (mOT) { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 333 | case OT_Dependency: |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 334 | case OT_Assembly: |
| 335 | case OT_LLVMAssembly: { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 336 | OS = OpenOutputFile(OutputFile, 0, &Error, mDiagEngine.getPtr()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 337 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 338 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 339 | case OT_Nothing: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 340 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 341 | } |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 342 | case OT_Object: |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 343 | case OT_Bitcode: { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 344 | OS = OpenOutputFile(OutputFile, llvm::raw_fd_ostream::F_Binary, |
| 345 | &Error, mDiagEngine.getPtr()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 346 | break; |
| 347 | } |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 348 | default: { |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 349 | llvm_unreachable("Unknown compiler output type"); |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 350 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 351 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 352 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 353 | if (!Error.empty()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 354 | return false; |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 355 | |
| 356 | mOS.reset(OS); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 357 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 358 | mOutputFileName = OutputFile; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 359 | |
| 360 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 363 | bool Slang::setDepOutput(const char *OutputFile) { |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 364 | llvm::sys::Path OutputFilePath(OutputFile); |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 365 | std::string Error; |
Zonr Chang | 8c6d9b2 | 2010-10-07 18:01:19 +0800 | [diff] [blame] | 366 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 367 | mDOS.reset(OpenOutputFile(OutputFile, 0, &Error, mDiagEngine.getPtr())); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 368 | if (!Error.empty() || (mDOS.get() == NULL)) |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 369 | return false; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 370 | |
| 371 | mDepOutputFileName = OutputFile; |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 376 | int Slang::generateDepFile() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 377 | if (mDiagEngine->hasErrorOccurred()) |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 378 | return 1; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 379 | if (mDOS.get() == NULL) |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 380 | return 1; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 381 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 382 | // Initialize options for generating dependency file |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 383 | clang::DependencyOutputOptions DepOpts; |
| 384 | DepOpts.IncludeSystemHeaders = 1; |
Stephen Hines | 0b7ef1a | 2010-10-07 16:36:59 -0700 | [diff] [blame] | 385 | DepOpts.OutputFile = mDepOutputFileName; |
Shih-wei Liao | b81c6a4 | 2010-10-10 14:15:00 -0700 | [diff] [blame] | 386 | DepOpts.Targets = mAdditionalDepTargets; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 387 | DepOpts.Targets.push_back(mDepTargetBCFileName); |
Stephen Hines | 4cc67fc | 2011-01-31 16:48:57 -0800 | [diff] [blame] | 388 | for (std::vector<std::string>::const_iterator |
| 389 | I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end(); |
| 390 | I != E; |
| 391 | I++) { |
| 392 | DepOpts.Targets.push_back(*I); |
| 393 | } |
| 394 | mGeneratedFileNames.clear(); |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 395 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 396 | // Per-compilation needed initialization |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 397 | createPreprocessor(); |
| 398 | AttachDependencyFileGen(*mPP.get(), DepOpts); |
| 399 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 400 | // Inform the diagnostic client we are processing a source file |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 401 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 402 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 403 | // Go through the source file (no operations necessary) |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 404 | clang::Token Tok; |
| 405 | mPP->EnterMainSourceFile(); |
| 406 | do { |
| 407 | mPP->Lex(Tok); |
| 408 | } while (Tok.isNot(clang::tok::eof)); |
| 409 | |
| 410 | mPP->EndSourceFile(); |
| 411 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 412 | // Declare success if no error |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 413 | if (!mDiagEngine->hasErrorOccurred()) |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 414 | mDOS->keep(); |
| 415 | |
| 416 | // Clean up after compilation |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 417 | mPP.reset(); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 418 | mDOS.reset(); |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 419 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 420 | return mDiagEngine->hasErrorOccurred() ? 1 : 0; |
Stephen Hines | cc0efad | 2010-10-04 16:13:02 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 423 | int Slang::compile() { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 424 | if (mDiagEngine->hasErrorOccurred()) |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 425 | return 1; |
Stephen Hines | f7de852 | 2010-10-06 11:46:18 -0700 | [diff] [blame] | 426 | if (mOS.get() == NULL) |
| 427 | return 1; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 428 | |
| 429 | // Here is per-compilation needed initialization |
| 430 | createPreprocessor(); |
| 431 | createASTContext(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 432 | |
Shih-wei Liao | df5bcce | 2011-02-28 18:39:23 -0800 | [diff] [blame] | 433 | mBackend.reset(createBackend(CodeGenOpts, &mOS->os(), mOT)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 434 | |
| 435 | // Inform the diagnostic client we are processing a source file |
| 436 | mDiagClient->BeginSourceFile(LangOpts, mPP.get()); |
| 437 | |
| 438 | // The core of the slang compiler |
| 439 | ParseAST(*mPP, mBackend.get(), *mASTContext); |
| 440 | |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 441 | // Inform the diagnostic client we are done with previous source file |
| 442 | mDiagClient->EndSourceFile(); |
| 443 | |
| 444 | // Declare success if no error |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 445 | if (!mDiagEngine->hasErrorOccurred()) |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 446 | mOS->keep(); |
| 447 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 448 | // The compilation ended, clear |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 449 | mBackend.reset(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 450 | mASTContext.reset(); |
| 451 | mPP.reset(); |
Zonr Chang | e8c263a | 2010-10-12 00:35:29 +0800 | [diff] [blame] | 452 | mOS.reset(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 453 | |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 454 | return mDiagEngine->hasErrorOccurred() ? 1 : 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 457 | void Slang::reset() { |
Stephen Hines | c632be2 | 2011-09-23 15:53:16 -0700 | [diff] [blame] | 458 | llvm::errs() << mDiagClient->str(); |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame^] | 459 | mDiagEngine->Reset(); |
Zonr Chang | 3a9ca1f | 2010-10-06 17:52:56 +0800 | [diff] [blame] | 460 | mDiagClient->reset(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | Slang::~Slang() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 464 | llvm::llvm_shutdown(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 465 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 466 | |
| 467 | } // namespace slang |