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