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