blob: dd194963ef7d189f6959e538a61d3c024622e719 [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
Stephen Hinese639eb52010-11-08 19:27:20 -080021#include <string>
22#include <vector>
23
24#include "clang/AST/ASTConsumer.h"
25#include "clang/AST/ASTContext.h"
26
Loganbe274822011-02-16 22:02:54 +080027#include "clang/Basic/DiagnosticIDs.h"
Stephen Hines23c43582013-01-09 20:02:04 -080028#include "clang/Basic/DiagnosticOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080029#include "clang/Basic/FileManager.h"
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -080030#include "clang/Basic/FileSystemOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080031#include "clang/Basic/LangOptions.h"
32#include "clang/Basic/SourceManager.h"
33#include "clang/Basic/TargetInfo.h"
34#include "clang/Basic/TargetOptions.h"
35
36#include "clang/Frontend/CodeGenOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080037#include "clang/Frontend/DependencyOutputOptions.h"
38#include "clang/Frontend/FrontendDiagnostic.h"
39#include "clang/Frontend/TextDiagnosticPrinter.h"
40#include "clang/Frontend/Utils.h"
41
42#include "clang/Lex/Preprocessor.h"
Stephen Hines23c43582013-01-09 20:02:04 -080043#include "clang/Lex/PreprocessorOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080044#include "clang/Lex/HeaderSearch.h"
Stephen Hines23c43582013-01-09 20:02:04 -080045#include "clang/Lex/HeaderSearchOptions.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080046
47#include "clang/Parse/ParseAST.h"
48
Loganbe274822011-02-16 22:02:54 +080049#include "llvm/ADT/IntrusiveRefCntPtr.h"
50
Stephen Hinese639eb52010-11-08 19:27:20 -080051#include "llvm/Bitcode/ReaderWriter.h"
52
53// More force linking
54#include "llvm/Linker.h"
55
Zonr Chang08df36e2010-10-07 18:50:42 +080056// Force linking all passes/vmcore stuffs to libslang.so
Stephen Hines0da7f6c2013-03-05 15:19:02 -080057#include "llvm/LinkAllIR.h"
Zonr Chang08df36e2010-10-07 18:50:42 +080058#include "llvm/LinkAllPasses.h"
Zonr Chang08df36e2010-10-07 18:50:42 +080059
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080060#include "llvm/Support/raw_ostream.h"
zonr6315f762010-10-05 15:35:14 +080061#include "llvm/Support/MemoryBuffer.h"
62#include "llvm/Support/ErrorHandling.h"
63#include "llvm/Support/ManagedStatic.h"
Loganbe274822011-02-16 22:02:54 +080064#include "llvm/Support/Path.h"
Logan Chien9207a2e2011-10-21 15:39:28 +080065#include "llvm/Support/TargetSelect.h"
66#include "llvm/Support/ToolOutputFile.h"
Stephen Hinescc0efad2010-10-04 16:13:02 -070067
Stephen Hines6e6578a2011-02-07 18:05:48 -080068#include "slang_assert.h"
Zonr Chang3a9ca1f2010-10-06 17:52:56 +080069#include "slang_backend.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080070#include "slang_utils.h"
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070071
Zonr Chang08df36e2010-10-07 18:50:42 +080072namespace {
Zonr Chang08df36e2010-10-07 18:50:42 +080073
Stephen Hinese639eb52010-11-08 19:27:20 -080074struct ForceSlangLinking {
75 ForceSlangLinking() {
76 // We must reference the functions in such a way that compilers will not
77 // delete it all as dead code, even with whole program optimization,
78 // yet is effectively a NO-OP. As the compiler isn't smart enough
79 // to know that getenv() never returns -1, this will do the job.
80 if (std::getenv("bar") != reinterpret_cast<char*>(-1))
81 return;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -070082
Stephen Hinese639eb52010-11-08 19:27:20 -080083 // llvm-rs-link needs following functions existing in libslang.
84 llvm::ParseBitcodeFile(NULL, llvm::getGlobalContext(), NULL);
Logan Chien9207a2e2011-10-21 15:39:28 +080085 llvm::Linker::LinkModules(NULL, NULL, 0, NULL);
Zonr Chang08df36e2010-10-07 18:50:42 +080086
Stephen Hinese639eb52010-11-08 19:27:20 -080087 // llvm-rs-cc need this.
88 new clang::TextDiagnosticPrinter(llvm::errs(),
Stephen Hines23c43582013-01-09 20:02:04 -080089 new clang::DiagnosticOptions());
Stephen Hinese639eb52010-11-08 19:27:20 -080090 }
91} ForceSlangLinking;
92
93} // namespace
94
95namespace slang {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070096
Shih-wei Liao462aefd2010-06-04 15:32:04 -070097#if defined(__arm__)
98# define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"
99#elif defined(__x86_64__)
100# define DEFAULT_TARGET_TRIPLE_STRING "x86_64-unknown-linux"
101#else
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102// let's use x86 as default target
103# define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux"
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700104#endif
105
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106bool Slang::GlobalInitialized = false;
107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108// Language option (define the language feature for compiler such as C99)
109clang::LangOptions Slang::LangOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
zonr6315f762010-10-05 15:35:14 +0800111// Code generation option for the compiler
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700112clang::CodeGenOptions Slang::CodeGenOpts;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114// The named of metadata node that pragma resides (should be synced with
115// bcc.cpp)
Shih-wei Liaof52a6202010-09-10 17:40:53 -0700116const llvm::StringRef Slang::PragmaMetadataName = "#pragma";
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700117
Logan Chien9207a2e2011-10-21 15:39:28 +0800118static inline llvm::tool_output_file *
119OpenOutputFile(const char *OutputFile,
120 unsigned Flags,
121 std::string* Error,
Stephen Hinese67239d2012-02-24 15:08:36 -0800122 clang::DiagnosticsEngine *DiagEngine) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800123 slangAssert((OutputFile != NULL) && (Error != NULL) &&
124 (DiagEngine != NULL) && "Invalid parameter!");
Zonr Change8c263a2010-10-12 00:35:29 +0800125
Logan Chien6f4e0a92011-03-10 01:10:25 +0800126 if (SlangUtils::CreateDirectoryWithParents(
127 llvm::sys::path::parent_path(OutputFile), Error)) {
Zonr Change8c263a2010-10-12 00:35:29 +0800128 llvm::tool_output_file *F =
129 new llvm::tool_output_file(OutputFile, *Error, Flags);
130 if (F != NULL)
131 return F;
132 }
133
134 // Report error here.
Logan Chien9207a2e2011-10-21 15:39:28 +0800135 DiagEngine->Report(clang::diag::err_fe_error_opening)
136 << OutputFile << *Error;
Zonr Change8c263a2010-10-12 00:35:29 +0800137
138 return NULL;
139}
140
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700141void Slang::GlobalInitialization() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700142 if (!GlobalInitialized) {
143 // We only support x86, x64 and ARM target
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 // For ARM
146 LLVMInitializeARMTargetInfo();
147 LLVMInitializeARMTarget();
148 LLVMInitializeARMAsmPrinter();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 // For x86 and x64
151 LLVMInitializeX86TargetInfo();
152 LLVMInitializeX86Target();
153 LLVMInitializeX86AsmPrinter();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800155 // Please refer to include/clang/Basic/LangOptions.h to setup
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700156 // the options.
157 LangOpts.RTTI = 0; // Turn off the RTTI information support
Shih-wei Liao245eba12011-01-19 02:14:07 -0800158 LangOpts.C99 = 1;
Stephen Hines52d132c2012-08-01 18:48:06 -0700159 LangOpts.Renderscript = 1;
Stephen Hines194d4032012-02-27 18:01:09 -0800160 LangOpts.CharIsSigned = 1; // Signed char is our default.
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700161
mkopec1c460b372012-01-09 11:21:50 -0500162 CodeGenOpts.OptimizationLevel = 3;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164 GlobalInitialized = true;
165 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166}
167
168void Slang::LLVMErrorHandler(void *UserData, const std::string &Message) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800169 clang::DiagnosticsEngine* DiagEngine =
170 static_cast<clang::DiagnosticsEngine *>(UserData);
171
172 DiagEngine->Report(clang::diag::err_fe_error_backend) << Message;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 exit(1);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700174}
175
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700176void Slang::createTarget(const std::string &Triple, const std::string &CPU,
177 const std::vector<std::string> &Features) {
178 if (!Triple.empty())
Stephen Hines23c43582013-01-09 20:02:04 -0800179 mTargetOpts->Triple = Triple;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700180 else
Stephen Hines23c43582013-01-09 20:02:04 -0800181 mTargetOpts->Triple = DEFAULT_TARGET_TRIPLE_STRING;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700182
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700183 if (!CPU.empty())
Stephen Hines23c43582013-01-09 20:02:04 -0800184 mTargetOpts->CPU = CPU;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700185
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700186 if (!Features.empty())
Stephen Hines23c43582013-01-09 20:02:04 -0800187 mTargetOpts->FeaturesAsWritten = Features;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700188
Logan Chien9207a2e2011-10-21 15:39:28 +0800189 mTarget.reset(clang::TargetInfo::CreateTargetInfo(*mDiagEngine,
Stephen Hines23c43582013-01-09 20:02:04 -0800190 mTargetOpts.getPtr()));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700191}
192
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800193void Slang::createFileManager() {
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -0800194 mFileSysOpt.reset(new clang::FileSystemOptions());
Loganbe274822011-02-16 22:02:54 +0800195 mFileMgr.reset(new clang::FileManager(*mFileSysOpt));
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800196}
197
198void Slang::createSourceManager() {
Logan Chien9207a2e2011-10-21 15:39:28 +0800199 mSourceMgr.reset(new clang::SourceManager(*mDiagEngine, *mFileMgr));
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800200}
201
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700202void Slang::createPreprocessor() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700203 // Default only search header file in current dir
Stephen Hines23c43582013-01-09 20:02:04 -0800204 llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> HSOpts =
205 new clang::HeaderSearchOptions();
206 clang::HeaderSearch *HeaderInfo = new clang::HeaderSearch(HSOpts,
207 *mFileMgr,
Stephen Hines0444de02012-03-02 23:19:06 -0800208 *mDiagEngine,
209 LangOpts,
210 mTarget.get());
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700211
Stephen Hines23c43582013-01-09 20:02:04 -0800212 llvm::IntrusiveRefCntPtr<clang::PreprocessorOptions> PPOpts =
213 new clang::PreprocessorOptions();
214 mPP.reset(new clang::Preprocessor(PPOpts,
215 *mDiagEngine,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700216 LangOpts,
Logan Chien9207a2e2011-10-21 15:39:28 +0800217 mTarget.get(),
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700218 *mSourceMgr,
Logan Chien9207a2e2011-10-21 15:39:28 +0800219 *HeaderInfo,
220 *this,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 NULL,
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800222 /* OwnsHeaderSearch = */true));
Stephen Hines3fd0a942011-01-18 12:27:39 -0800223 // Initialize the preprocessor
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700224 mPragmas.clear();
Stephen Hines3fd0a942011-01-18 12:27:39 -0800225 mPP->AddPragmaHandler(new PragmaRecorder(&mPragmas));
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700226
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700227 std::vector<clang::DirectoryLookup> SearchList;
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800228 for (unsigned i = 0, e = mIncludePaths.size(); i != e; i++) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700229 if (const clang::DirectoryEntry *DE =
Loganbe274822011-02-16 22:02:54 +0800230 mFileMgr->getDirectory(mIncludePaths[i])) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700231 SearchList.push_back(clang::DirectoryLookup(DE,
232 clang::SrcMgr::C_System,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700233 false));
234 }
Shih-wei Liao90898282010-07-19 18:38:57 -0700235 }
236
Logan Chien9207a2e2011-10-21 15:39:28 +0800237 HeaderInfo->SetSearchPaths(SearchList,
238 /* angledDirIdx = */1,
239 /* systemDixIdx = */1,
240 /* noCurDirSearch = */false);
Shih-wei Liao90898282010-07-19 18:38:57 -0700241
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800242 initPreprocessor();
Shih-wei Liao68e8e9f2010-07-18 18:46:49 -0700243}
244
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800245void Slang::createASTContext() {
246 mASTContext.reset(new clang::ASTContext(LangOpts,
247 *mSourceMgr,
Logan Chien9207a2e2011-10-21 15:39:28 +0800248 mTarget.get(),
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800249 mPP->getIdentifierTable(),
250 mPP->getSelectorTable(),
251 mPP->getBuiltinInfo(),
252 /* size_reserve = */0));
253 initASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800254}
255
Logan Chien9207a2e2011-10-21 15:39:28 +0800256clang::ASTConsumer *
257Slang::createBackend(const clang::CodeGenOptions& CodeGenOpts,
258 llvm::raw_ostream *OS, OutputType OT) {
Stephen Hines23c43582013-01-09 20:02:04 -0800259 return new Backend(mDiagEngine, CodeGenOpts, getTargetOptions(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800260 &mPragmas, OS, OT);
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800261}
262
Zonr Chang641558f2010-10-12 21:07:06 +0800263Slang::Slang() : mInitialized(false), mDiagClient(NULL), mOT(OT_Default) {
Stephen Hines23c43582013-01-09 20:02:04 -0800264 mTargetOpts = new clang::TargetOptions();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700265 GlobalInitialization();
Zonr Chang641558f2010-10-12 21:07:06 +0800266}
267
268void Slang::init(const std::string &Triple, const std::string &CPU,
Stephen Hines8f4d9722011-12-05 14:14:42 -0800269 const std::vector<std::string> &Features,
270 clang::DiagnosticsEngine *DiagEngine,
271 DiagnosticBuffer *DiagClient) {
Zonr Chang641558f2010-10-12 21:07:06 +0800272 if (mInitialized)
273 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700274
Stephen Hines8f4d9722011-12-05 14:14:42 -0800275 mDiagEngine = DiagEngine;
276 mDiagClient = DiagClient;
277 mDiag.reset(new clang::Diagnostic(mDiagEngine));
278 initDiagnostic();
279 llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagEngine);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 createTarget(Triple, CPU, Features);
282 createFileManager();
283 createSourceManager();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700284
Zonr Chang641558f2010-10-12 21:07:06 +0800285 mInitialized = true;
Logan Chien9207a2e2011-10-21 15:39:28 +0800286}
Zonr Chang641558f2010-10-12 21:07:06 +0800287
Stephen Hines23c43582013-01-09 20:02:04 -0800288clang::ModuleLoadResult Slang::loadModule(
289 clang::SourceLocation ImportLoc,
290 clang::ModuleIdPath Path,
291 clang::Module::NameVisibilityKind Visibility,
292 bool IsInclusionDirective) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800293 slangAssert(0 && "Not implemented");
Stephen Hines23c43582013-01-09 20:02:04 -0800294 return clang::ModuleLoadResult();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700295}
296
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800297bool Slang::setInputSource(llvm::StringRef InputFile,
298 const char *Text,
299 size_t TextLength) {
300 mInputFileName = InputFile.str();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700301
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700302 // Reset the ID tables if we are reusing the SourceManager
303 mSourceMgr->clearIDTables();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700304
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700305 // Load the source
zonr6315f762010-10-05 15:35:14 +0800306 llvm::MemoryBuffer *SB =
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800307 llvm::MemoryBuffer::getMemBuffer(Text, Text + TextLength);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700308 mSourceMgr->createMainFileIDForMemBuffer(SB);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700309
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700310 if (mSourceMgr->getMainFileID().isInvalid()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800311 mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700312 return false;
313 }
314 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700315}
316
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800317bool Slang::setInputSource(llvm::StringRef InputFile) {
318 mInputFileName = InputFile.str();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700319
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700320 mSourceMgr->clearIDTables();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700321
Loganbe274822011-02-16 22:02:54 +0800322 const clang::FileEntry *File = mFileMgr->getFile(InputFile);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700323 if (File)
324 mSourceMgr->createMainFileID(File);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700325
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700326 if (mSourceMgr->getMainFileID().isInvalid()) {
Logan Chien9207a2e2011-10-21 15:39:28 +0800327 mDiagEngine->Report(clang::diag::err_fe_error_reading) << InputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700328 return false;
329 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700330
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700331 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700332}
333
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800334bool Slang::setOutput(const char *OutputFile) {
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800335 llvm::sys::Path OutputFilePath(OutputFile);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700336 std::string Error;
Zonr Change8c263a2010-10-12 00:35:29 +0800337 llvm::tool_output_file *OS = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700338
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800339 switch (mOT) {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700340 case OT_Dependency:
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800341 case OT_Assembly:
342 case OT_LLVMAssembly: {
Stephen Hines8f4d9722011-12-05 14:14:42 -0800343 OS = OpenOutputFile(OutputFile, 0, &Error, mDiagEngine);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700344 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700345 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800346 case OT_Nothing: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700347 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700348 }
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800349 case OT_Object:
Stephen Hinescc0efad2010-10-04 16:13:02 -0700350 case OT_Bitcode: {
Logan Chien9207a2e2011-10-21 15:39:28 +0800351 OS = OpenOutputFile(OutputFile, llvm::raw_fd_ostream::F_Binary,
Stephen Hines8f4d9722011-12-05 14:14:42 -0800352 &Error, mDiagEngine);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700353 break;
354 }
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800355 default: {
Stephen Hinescc0efad2010-10-04 16:13:02 -0700356 llvm_unreachable("Unknown compiler output type");
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800357 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700358 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700359
Zonr Change8c263a2010-10-12 00:35:29 +0800360 if (!Error.empty())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700361 return false;
Zonr Change8c263a2010-10-12 00:35:29 +0800362
363 mOS.reset(OS);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700364
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800365 mOutputFileName = OutputFile;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700366
367 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700368}
369
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700370bool Slang::setDepOutput(const char *OutputFile) {
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800371 llvm::sys::Path OutputFilePath(OutputFile);
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700372 std::string Error;
Zonr Chang8c6d9b22010-10-07 18:01:19 +0800373
Stephen Hines8f4d9722011-12-05 14:14:42 -0800374 mDOS.reset(OpenOutputFile(OutputFile, 0, &Error, mDiagEngine));
Zonr Change8c263a2010-10-12 00:35:29 +0800375 if (!Error.empty() || (mDOS.get() == NULL))
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700376 return false;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700377
378 mDepOutputFileName = OutputFile;
379
380 return true;
381}
382
Stephen Hinescc0efad2010-10-04 16:13:02 -0700383int Slang::generateDepFile() {
Logan Chien9207a2e2011-10-21 15:39:28 +0800384 if (mDiagEngine->hasErrorOccurred())
Loganbe274822011-02-16 22:02:54 +0800385 return 1;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700386 if (mDOS.get() == NULL)
Stephen Hinesf7de8522010-10-06 11:46:18 -0700387 return 1;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700388
Zonr Change8c263a2010-10-12 00:35:29 +0800389 // Initialize options for generating dependency file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700390 clang::DependencyOutputOptions DepOpts;
391 DepOpts.IncludeSystemHeaders = 1;
Stephen Hines0b7ef1a2010-10-07 16:36:59 -0700392 DepOpts.OutputFile = mDepOutputFileName;
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700393 DepOpts.Targets = mAdditionalDepTargets;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700394 DepOpts.Targets.push_back(mDepTargetBCFileName);
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800395 for (std::vector<std::string>::const_iterator
396 I = mGeneratedFileNames.begin(), E = mGeneratedFileNames.end();
397 I != E;
398 I++) {
399 DepOpts.Targets.push_back(*I);
400 }
401 mGeneratedFileNames.clear();
Stephen Hinescc0efad2010-10-04 16:13:02 -0700402
Zonr Change8c263a2010-10-12 00:35:29 +0800403 // Per-compilation needed initialization
Stephen Hinescc0efad2010-10-04 16:13:02 -0700404 createPreprocessor();
405 AttachDependencyFileGen(*mPP.get(), DepOpts);
406
Zonr Change8c263a2010-10-12 00:35:29 +0800407 // Inform the diagnostic client we are processing a source file
Stephen Hinescc0efad2010-10-04 16:13:02 -0700408 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
409
Zonr Change8c263a2010-10-12 00:35:29 +0800410 // Go through the source file (no operations necessary)
Stephen Hinescc0efad2010-10-04 16:13:02 -0700411 clang::Token Tok;
412 mPP->EnterMainSourceFile();
413 do {
414 mPP->Lex(Tok);
415 } while (Tok.isNot(clang::tok::eof));
416
417 mPP->EndSourceFile();
418
Zonr Change8c263a2010-10-12 00:35:29 +0800419 // Declare success if no error
Logan Chien9207a2e2011-10-21 15:39:28 +0800420 if (!mDiagEngine->hasErrorOccurred())
Zonr Change8c263a2010-10-12 00:35:29 +0800421 mDOS->keep();
422
423 // Clean up after compilation
Stephen Hinescc0efad2010-10-04 16:13:02 -0700424 mPP.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800425 mDOS.reset();
Stephen Hinescc0efad2010-10-04 16:13:02 -0700426
Logan Chien9207a2e2011-10-21 15:39:28 +0800427 return mDiagEngine->hasErrorOccurred() ? 1 : 0;
Stephen Hinescc0efad2010-10-04 16:13:02 -0700428}
429
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700430int Slang::compile() {
Logan Chien9207a2e2011-10-21 15:39:28 +0800431 if (mDiagEngine->hasErrorOccurred())
Loganbe274822011-02-16 22:02:54 +0800432 return 1;
Stephen Hinesf7de8522010-10-06 11:46:18 -0700433 if (mOS.get() == NULL)
434 return 1;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700435
436 // Here is per-compilation needed initialization
437 createPreprocessor();
438 createASTContext();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800439
Shih-wei Liaodf5bcce2011-02-28 18:39:23 -0800440 mBackend.reset(createBackend(CodeGenOpts, &mOS->os(), mOT));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700441
442 // Inform the diagnostic client we are processing a source file
443 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
444
445 // The core of the slang compiler
446 ParseAST(*mPP, mBackend.get(), *mASTContext);
447
Zonr Change8c263a2010-10-12 00:35:29 +0800448 // Inform the diagnostic client we are done with previous source file
449 mDiagClient->EndSourceFile();
450
451 // Declare success if no error
Logan Chien9207a2e2011-10-21 15:39:28 +0800452 if (!mDiagEngine->hasErrorOccurred())
Zonr Change8c263a2010-10-12 00:35:29 +0800453 mOS->keep();
454
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800455 // The compilation ended, clear
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700456 mBackend.reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700457 mASTContext.reset();
458 mPP.reset();
Zonr Change8c263a2010-10-12 00:35:29 +0800459 mOS.reset();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700460
Logan Chien9207a2e2011-10-21 15:39:28 +0800461 return mDiagEngine->hasErrorOccurred() ? 1 : 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700462}
463
mkopec1c460b372012-01-09 11:21:50 -0500464void Slang::setDebugMetadataEmission(bool EmitDebug) {
Shih-wei Liao43730fe2012-08-02 23:06:18 -0700465 if (EmitDebug)
Stephen Hines23c43582013-01-09 20:02:04 -0800466 CodeGenOpts.setDebugInfo(clang::CodeGenOptions::FullDebugInfo);
Shih-wei Liao43730fe2012-08-02 23:06:18 -0700467 else
Stephen Hines23c43582013-01-09 20:02:04 -0800468 CodeGenOpts.setDebugInfo(clang::CodeGenOptions::NoDebugInfo);
mkopec1c460b372012-01-09 11:21:50 -0500469}
470
471void Slang::setOptimizationLevel(llvm::CodeGenOpt::Level OptimizationLevel) {
472 CodeGenOpts.OptimizationLevel = OptimizationLevel;
473}
474
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800475void Slang::reset() {
Stephen Hinesc632be22011-09-23 15:53:16 -0700476 llvm::errs() << mDiagClient->str();
Logan Chien9207a2e2011-10-21 15:39:28 +0800477 mDiagEngine->Reset();
Zonr Chang3a9ca1f2010-10-06 17:52:56 +0800478 mDiagClient->reset();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700479}
480
481Slang::~Slang() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700482 llvm::llvm_shutdown();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700483}
Stephen Hinese639eb52010-11-08 19:27:20 -0800484
485} // namespace slang