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