blob: cd3cf9354074371232b431aed025cc118f4e1820 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
zonr6315f762010-10-05 15:35:14 +080017#include "slang.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019#include <stdlib.h>
20
Zonr Chang08df36e2010-10-07 18:50:42 +080021// Force linking all passes/vmcore stuffs to libslang.so
22#include "llvm/LinkAllPasses.h"
23#include "llvm/LinkAllVMCore.h"
24
zonr6315f762010-10-05 15:35:14 +080025#include "llvm/Target/TargetSelect.h"
26
Zonr Chang8c6d9b22010-10-07 18:01:19 +080027#include "llvm/System/Path.h"
28
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080029#include "llvm/Support/raw_ostream.h"
zonr6315f762010-10-05 15:35:14 +080030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/ManagedStatic.h"
33
zonr6315f762010-10-05 15:35:14 +080034#include "clang/Basic/LangOptions.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080035#include "clang/Basic/SourceManager.h"
36#include "clang/Basic/TargetInfo.h"
zonr6315f762010-10-05 15:35:14 +080037#include "clang/Basic/TargetOptions.h"
38
Stephen Hinescc0efad2010-10-04 16:13:02 -070039#include "clang/Frontend/DependencyOutputOptions.h"
40#include "clang/Frontend/FrontendDiagnostic.h"
41#include "clang/Frontend/Utils.h"
42
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080043#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"
zonr6315f762010-10-05 15:35:14 +080052#include "clang/Frontend/FrontendDiagnostic.h"
53
54#include "clang/Parse/ParseAST.h"
55
Zonr Chang8c6d9b22010-10-07 18:01:19 +080056#include "slang_utils.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080057#include "slang_backend.h"
zonr6315f762010-10-05 15:35:14 +080058
Zonr Chang08df36e2010-10-07 18:50:42 +080059// More force linking
60#include "llvm/Linker.h"
61#include "llvm/Bitcode/ReaderWriter.h"
62
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070063#include "clang/Frontend/DiagnosticOptions.h"
64#include "clang/Frontend/TextDiagnosticPrinter.h"
65
Zonr Chang08df36e2010-10-07 18:50:42 +080066namespace {
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 Liaob81c6a42010-10-10 14:15:00 -070079
80 // llvm-rs-cc need this.
81 new clang::TextDiagnosticPrinter(llvm::errs(),
82 clang::DiagnosticOptions());
Zonr Chang08df36e2010-10-07 18:50:42 +080083 }
84 } ForceSlangLinking;
85}
86
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070087using namespace slang;
88
Shih-wei Liao462aefd2010-06-04 15:32:04 -070089#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 Liao9ef2f782010-10-01 12:31:37 -070094// let's use x86 as default target
95# define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070096#endif
97
Shih-wei Liao462aefd2010-06-04 15:32:04 -070098bool Slang::GlobalInitialized = false;
99
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100// Language option (define the language feature for compiler such as C99)
101clang::LangOptions Slang::LangOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700102
zonr6315f762010-10-05 15:35:14 +0800103// Code generation option for the compiler
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104clang::CodeGenOptions Slang::CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700105
zonr6315f762010-10-05 15:35:14 +0800106const 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 Liao462aefd2010-06-04 15:32:04 -0700120
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700121// The named of metadata node that pragma resides (should be synced with
122// bcc.cpp)
Shih-wei Liaof52a6202010-09-10 17:40:53 -0700123const llvm::StringRef Slang::PragmaMetadataName = "#pragma";
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Zonr Change8c263a2010-10-12 00:35:29 +0800125static 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 Liao462aefd2010-06-04 15:32:04 -0700148void Slang::GlobalInitialization() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 if (!GlobalInitialized) {
150 // We only support x86, x64 and ARM target
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700151
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 // For ARM
153 LLVMInitializeARMTargetInfo();
154 LLVMInitializeARMTarget();
155 LLVMInitializeARMAsmPrinter();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700157 // For x86 and x64
158 LLVMInitializeX86TargetInfo();
159 LLVMInitializeX86Target();
160 LLVMInitializeX86AsmPrinter();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700161
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800162 // Please refer to include/clang/Basic/LangOptions.h to setup
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700163 // 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 Liao462aefd2010-06-04 15:32:04 -0700167
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700168 CodeGenOpts.OptimizationLevel = 3; /* -O3 */
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700169
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700170 GlobalInitialized = true;
171 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700172
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174}
175
176void Slang::LLVMErrorHandler(void *UserData, const std::string &Message) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700177 clang::Diagnostic* Diags = static_cast<clang::Diagnostic*>(UserData);
178 Diags->Report(clang::diag::err_fe_error_backend) << Message;
179 exit(1);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700180}
181
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800182void 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);
188 return;
189}
190
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700191void Slang::createTarget(const std::string &Triple, const std::string &CPU,
192 const std::vector<std::string> &Features) {
193 if (!Triple.empty())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 mTargetOpts.Triple = Triple;
195 else
196 mTargetOpts.Triple = DEFAULT_TARGET_TRIPLE_STRING;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700197
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700198 if (!CPU.empty())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199 mTargetOpts.CPU = CPU;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700201 if (!Features.empty())
202 mTargetOpts.Features = Features;
203
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700204 mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagnostics,
205 mTargetOpts));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700207 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700208}
209
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800210void Slang::createFileManager() {
211 mFileMgr.reset(new clang::FileManager());
212}
213
214void Slang::createSourceManager() {
215 mSourceMgr.reset(new clang::SourceManager(*mDiagnostics));
216 return;
217}
218
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700219void Slang::createPreprocessor() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700220 // Default only search header file in current dir
221 clang::HeaderSearch *HS = new clang::HeaderSearch(*mFileMgr);
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700222
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700223 mPP.reset(new clang::Preprocessor(*mDiagnostics,
224 LangOpts,
225 *mTarget,
226 *mSourceMgr,
227 *HS,
228 NULL,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800229 /* OwnsHeaderSearch = */true));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700230 // Initialize the prepocessor
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700231 mPragmas.clear();
Shih-wei Liaof52a6202010-09-10 17:40:53 -0700232 mPP->AddPragmaHandler(new PragmaRecorder(mPragmas));
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700233
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700234 std::vector<clang::DirectoryLookup> SearchList;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800235 for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700236 if (const clang::DirectoryEntry *DE =
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800237 mFileMgr->getDirectory(mIncludePaths[i])) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700238 SearchList.push_back(clang::DirectoryLookup(DE,
239 clang::SrcMgr::C_System,
240 false,
241 false));
242 }
Shih-wei Liao90898282010-07-19 18:38:57 -0700243 }
244
245 HS->SetSearchPaths(SearchList, 1, false);
246
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800247 initPreprocessor();
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700248 return;
249}
250
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800251void Slang::createASTContext() {
252 mASTContext.reset(new clang::ASTContext(LangOpts,
253 *mSourceMgr,
254 *mTarget,
255 mPP->getIdentifierTable(),
256 mPP->getSelectorTable(),
257 mPP->getBuiltinInfo(),
258 /* size_reserve = */0));
259 initASTContext();
260 return;
261}
262
263clang::ASTConsumer
264*Slang::createBackend(const clang::CodeGenOptions& CodeGenOpts,
265 llvm::raw_ostream *OS,
266 OutputType OT) {
267 return new Backend(*mDiagnostics,
268 CodeGenOpts,
269 mTargetOpts,
270 mPragmas,
271 OS,
272 OT);
273}
274
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700275Slang::Slang(const std::string &Triple, const std::string &CPU,
276 const std::vector<std::string> &Features)
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800277 : mDiagClient(NULL),
278 mOT(OT_Default) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700279 GlobalInitialization();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 createDiagnostic();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800282 llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagnostics.getPtr());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700283
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700284 createTarget(Triple, CPU, Features);
285 createFileManager();
286 createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700287
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700289}
290
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800291bool Slang::setInputSource(llvm::StringRef InputFile,
292 const char *Text,
293 size_t TextLength) {
294 mInputFileName = InputFile.str();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700295
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 // Reset the ID tables if we are reusing the SourceManager
297 mSourceMgr->clearIDTables();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700298
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700299 // Load the source
zonr6315f762010-10-05 15:35:14 +0800300 llvm::MemoryBuffer *SB =
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800301 llvm::MemoryBuffer::getMemBuffer(Text, Text + TextLength);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700302 mSourceMgr->createMainFileIDForMemBuffer(SB);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700303
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700304 if (mSourceMgr->getMainFileID().isInvalid()) {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800305 mDiagnostics->Report(clang::diag::err_fe_error_reading) << InputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700306 return false;
307 }
308 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700309}
310
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800311bool Slang::setInputSource(llvm::StringRef InputFile) {
312 mInputFileName = InputFile.str();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700313
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700314 mSourceMgr->clearIDTables();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700315
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800316 const clang::FileEntry *File = mFileMgr->getFile(InputFile);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700317 if (File)
318 mSourceMgr->createMainFileID(File);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700319
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700320 if (mSourceMgr->getMainFileID().isInvalid()) {
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800321 mDiagnostics->Report(clang::diag::err_fe_error_reading) << InputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700322 return false;
323 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700324
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700325 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700326}
327
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800328bool Slang::setOutput(const char *OutputFile) {
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800329 llvm::sys::Path OutputFilePath(OutputFile);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700330 std::string Error;
Zonr Change8c263a2010-10-12 00:35:29 +0800331 llvm::tool_output_file *OS = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700332
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800333 switch (mOT) {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700334 case OT_Dependency:
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800335 case OT_Assembly:
336 case OT_LLVMAssembly: {
Zonr Change8c263a2010-10-12 00:35:29 +0800337 OS = OpenOutputFile(OutputFile, 0, &Error, mDiagnostics.getPtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700338 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700339 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800340 case OT_Nothing: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700341 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700342 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800343 case OT_Object:
Stephen Hinescc0efad2010-10-04 16:13:02 -0700344 case OT_Bitcode: {
Zonr Change8c263a2010-10-12 00:35:29 +0800345 OS = OpenOutputFile(OutputFile,
346 llvm::raw_fd_ostream::F_Binary,
347 &Error,
348 mDiagnostics.getPtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700349 break;
350 }
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800351 default: {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700352 llvm_unreachable("Unknown compiler output type");
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800353 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700354 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700355
Zonr Change8c263a2010-10-12 00:35:29 +0800356 if (!Error.empty())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700357 return false;
Zonr Change8c263a2010-10-12 00:35:29 +0800358
359 mOS.reset(OS);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700360
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800361 mOutputFileName = OutputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700362
363 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700364}
365
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700366bool Slang::setDepOutput(const char *OutputFile) {
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800367 llvm::sys::Path OutputFilePath(OutputFile);
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700368 std::string Error;
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800369
Zonr Change8c263a2010-10-12 00:35:29 +0800370 mDOS.reset(OpenOutputFile(OutputFile, 0, &Error, mDiagnostics.getPtr()));
371 if (!Error.empty() || (mDOS.get() == NULL))
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700372 return false;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700373
374 mDepOutputFileName = OutputFile;
375
376 return true;
377}
378
Stephen Hinescc0efad2010-10-04 16:13:02 -0700379int Slang::generateDepFile() {
Zonr Changb2573012010-10-07 19:35:21 +0800380 if (mDiagnostics->getNumErrors() > 0)
Stephen Hinescc0efad2010-10-04 16:13:02 -0700381 return mDiagnostics->getNumErrors();
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700382 if (mDOS.get() == NULL)
Stephen Hinesf7de8522010-10-06 11:46:18 -0700383 return 1;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700384
Zonr Change8c263a2010-10-12 00:35:29 +0800385 // Initialize options for generating dependency file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700386 clang::DependencyOutputOptions DepOpts;
387 DepOpts.IncludeSystemHeaders = 1;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700388 DepOpts.OutputFile = mDepOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700389 DepOpts.Targets = mAdditionalDepTargets;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700390 DepOpts.Targets.push_back(mDepTargetBCFileName);
391
Zonr Change8c263a2010-10-12 00:35:29 +0800392 // Per-compilation needed initialization
Stephen Hinescc0efad2010-10-04 16:13:02 -0700393 createPreprocessor();
394 AttachDependencyFileGen(*mPP.get(), DepOpts);
395
Zonr Change8c263a2010-10-12 00:35:29 +0800396 // Inform the diagnostic client we are processing a source file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700397 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
398
Zonr Change8c263a2010-10-12 00:35:29 +0800399 // Go through the source file (no operations necessary)
Stephen Hinescc0efad2010-10-04 16:13:02 -0700400 clang::Token Tok;
401 mPP->EnterMainSourceFile();
402 do {
403 mPP->Lex(Tok);
404 } while (Tok.isNot(clang::tok::eof));
405
406 mPP->EndSourceFile();
407
Zonr Change8c263a2010-10-12 00:35:29 +0800408 // Declare success if no error
409 if (mDiagnostics->getNumErrors() == 0)
410 mDOS->keep();
411
412 // Clean up after compilation
Stephen Hinescc0efad2010-10-04 16:13:02 -0700413 mPP.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800414 mDOS.reset();
Stephen Hinescc0efad2010-10-04 16:13:02 -0700415
416 return mDiagnostics->getNumErrors();
417}
418
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700419int Slang::compile() {
Stephen Hinesf7de8522010-10-06 11:46:18 -0700420 if (mDiagnostics->getNumErrors() > 0)
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700421 return mDiagnostics->getNumErrors();
Stephen Hinesf7de8522010-10-06 11:46:18 -0700422 if (mOS.get() == NULL)
423 return 1;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700424
425 // Here is per-compilation needed initialization
426 createPreprocessor();
427 createASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800428
Zonr Change8c263a2010-10-12 00:35:29 +0800429 mBackend.reset(createBackend(CodeGenOpts, mOS.get(), mOT));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700430
431 // Inform the diagnostic client we are processing a source file
432 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
433
434 // The core of the slang compiler
435 ParseAST(*mPP, mBackend.get(), *mASTContext);
436
Zonr Change8c263a2010-10-12 00:35:29 +0800437 // Inform the diagnostic client we are done with previous source file
438 mDiagClient->EndSourceFile();
439
440 // Declare success if no error
441 if (mDiagnostics->getNumErrors() == 0)
442 mOS->keep();
443
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800444 // The compilation ended, clear
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700445 mBackend.reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700446 mASTContext.reset();
447 mPP.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800448 mOS.reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700449
450 return mDiagnostics->getNumErrors();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700451}
452
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800453void Slang::reset() {
454 mDiagnostics->Reset();
455 mDiagClient->reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700456 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700457}
458
459Slang::~Slang() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700460 llvm::llvm_shutdown();
461 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700462}