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