blob: 4071ada2046680cf9a0867f7fcfd43bda5ccdfb4 [file] [log] [blame]
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001#include "slang.hpp"
2#include "libslang.h"
3
4#include "llvm/ADT/Twine.h" /* for class llvm::Twine */
5
6#include "llvm/Target/TargetSelect.h" /* for function LLVMInitialize[ARM|X86][TargetInfo|Target|AsmPrinter]() */
7
8#include "llvm/Support/MemoryBuffer.h" /* for class llvm::MemoryBuffer */
9#include "llvm/Support/ErrorHandling.h" /* for function llvm::install_fatal_error_handler() */
10#include "llvm/Support/ManagedStatic.h" /* for class llvm::llvm_shutdown */
11
12#include "clang/Basic/TargetInfo.h" /* for class clang::TargetInfo */
13#include "clang/Basic/LangOptions.h" /* for class clang::LangOptions */
14#include "clang/Basic/TargetOptions.h" /* for class clang::TargetOptions */
15
16#include "clang/Frontend/FrontendDiagnostic.h" /* for clang::diag::* */
17
18#include "clang/Sema/ParseAST.h" /* for function clang::ParseAST() */
19
20#if defined(__arm__)
21# define DEFAULT_TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"
22#elif defined(__x86_64__)
23# define DEFAULT_TARGET_TRIPLE_STRING "x86_64-unknown-linux"
24#else
25# define DEFAULT_TARGET_TRIPLE_STRING "i686-unknown-linux" // let's use x86 as default target
26#endif
27
28namespace slang {
29
30bool Slang::GlobalInitialized = false;
31
32/* Language option (define the language feature for compiler such as C99) */
33LangOptions Slang::LangOpts;
34
35/* Code generation option for the compiler */
36CodeGenOptions Slang::CodeGenOpts;
37
38const std::string Slang::TargetDescription = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n32";
39
40/* The named of metadata node that pragma resides (should be synced with bcc.cpp) */
41const llvm::Twine Slang::PragmaMetadataName = "#pragma";
42
43void Slang::GlobalInitialization() {
44 if(!GlobalInitialized) {
45 /* We only support x86, x64 and ARM target */
46
47 /* For ARM */
48 LLVMInitializeARMTargetInfo();
49 LLVMInitializeARMTarget();
50 LLVMInitializeARMAsmPrinter();
51
52 /* For x86 and x64 */
53 LLVMInitializeX86TargetInfo();
54 LLVMInitializeX86Target();
55 LLVMInitializeX86AsmPrinter();
56
57 /* Please refer to clang/include/clang/Basic/LangOptions.h for setting up the options */
58 LangOpts.RTTI = 0; /* turn off the RTTI information support */
59 LangOpts.NeXTRuntime = 0; /* turn off the NeXT runtime uses */
60 LangOpts.Bool = 1; /* turn on 'bool', 'true', 'false' keywords. */
61
62 CodeGenOpts.OptimizationLevel = 3; /* -O3 */
63
64 GlobalInitialized = true;
65 }
66
67 return;
68}
69
70void Slang::LLVMErrorHandler(void *UserData, const std::string &Message) {
71 Diagnostic* Diags = static_cast<Diagnostic*>(UserData);
72 Diags->Report(clang::diag::err_fe_error_backend) << Message;
73 exit(1);
74}
75
76void Slang::createTarget(const char* Triple, const char* CPU, const char** Features) {
77 if(Triple != NULL)
78 mTargetOpts.Triple = Triple;
79 else
80 mTargetOpts.Triple = DEFAULT_TARGET_TRIPLE_STRING;
81
82 if(CPU != NULL)
83 mTargetOpts.CPU = CPU;
84
85 mTarget.reset(TargetInfo::CreateTargetInfo(*mDiagnostics, mTargetOpts));
86
87 if(Features != NULL)
88 for(int i=0;Features[i]!=NULL;i++)
89 mTargetOpts.Features.push_back(Features[i]);
90
91 return;
92}
93
94Slang::Slang(const char* Triple, const char* CPU, const char** Features) :
95 mOutputType(SlangCompilerOutput_Default)
96{
97 GlobalInitialization();
98
99 createDiagnostic();
100 llvm::install_fatal_error_handler(LLVMErrorHandler, mDiagnostics.get());
101
102 createTarget(Triple, CPU, Features);
103 createFileManager();
104 createSourceManager();
105
106 return;
107}
108
109bool Slang::setInputSource(llvm::StringRef inputFile, const char* text, size_t textLength) {
110 mInputFileName = inputFile.str();
111
112 /* Reset the ID tables if we are reusing the SourceManager */
113 mSourceMgr->clearIDTables();
114
115 /* Load the source */
116 llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getMemBuffer(text, text + textLength);
117 mSourceMgr->createMainFileIDForMemBuffer(SB);
118
119 if(mSourceMgr->getMainFileID().isInvalid()) {
120 mDiagnostics->Report(clang::diag::err_fe_error_reading) << inputFile;
121 return false;
122 }
123
124 return true;
125}
126
127bool Slang::setInputSource(llvm::StringRef inputFile) {
128 mInputFileName = inputFile.str();
129
130 mSourceMgr->clearIDTables();
131
132 const FileEntry* File = mFileMgr->getFile(inputFile);
133 if(File)
134 mSourceMgr->createMainFileID(File, SourceLocation());
135
136 if(mSourceMgr->getMainFileID().isInvalid()) {
137 mDiagnostics->Report(clang::diag::err_fe_error_reading) << inputFile;
138 return false;
139 }
140
141 return true;
142}
143
144void Slang::setOutputType(SlangCompilerOutputTy outputType) {
145 mOutputType = outputType;
146 if( mOutputType != SlangCompilerOutput_Assembly &&
147 mOutputType != SlangCompilerOutput_LL &&
148 mOutputType != SlangCompilerOutput_Bitcode &&
149 mOutputType != SlangCompilerOutput_Nothing &&
150 mOutputType != SlangCompilerOutput_Obj)
151 mOutputType = SlangCompilerOutput_Default;
152 return;
153}
154
155bool Slang::setOutput(const char* outputFile) {
156 std::string Error;
157
158 switch(mOutputType) {
159 case SlangCompilerOutput_Assembly:
160 case SlangCompilerOutput_LL:
161 mOS.reset( new llvm::raw_fd_ostream(outputFile, Error, 0) );
162 break;
163
164 case SlangCompilerOutput_Nothing:
165 mOS.reset();
166 break;
167
168 case SlangCompilerOutput_Obj:
169 case SlangCompilerOutput_Bitcode:
170 default:
171 mOS.reset( new llvm::raw_fd_ostream(outputFile, Error, llvm::raw_fd_ostream::F_Binary) );
172 break;
173 }
174
175 if(!Error.empty()) {
176 mOS.reset();
177 mDiagnostics->Report(clang::diag::err_fe_error_opening) << outputFile << Error;
178 return false;
179 }
180
181 mOutputFileName = outputFile;
182
183 return true;
184}
185
186int Slang::compile() {
187 if((mDiagnostics->getNumErrors() > 0) || (mOS.get() == NULL))
188 return mDiagnostics->getNumErrors();
189
190 /* Here is per-compilation needed initialization */
191 createPreprocessor();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700192 createASTContext();
Shih-wei Liao001fb6d2010-06-21 11:17:11 -0700193 createRSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700194 //createBackend();
195 createRSBackend();
196
197 /* Inform the diagnostic client we are processing a source file */
198 mDiagClient->BeginSourceFile(LangOpts, mPP.get());
199
200 /* The core of the slang compiler */
201 ParseAST(*mPP, mBackend.get(), *mASTContext);
202
203 /* The compilation ended, clear up */
204 mBackend.reset();
205 mASTContext.reset();
206 mPP.reset();
207
208 /* Inform the diagnostic client we are done with previous source file */
209 mDiagClient->EndSourceFile();
210
211 return mDiagnostics->getNumErrors();
212}
213
214bool Slang::reflectToJava(const char* outputPackageName) {
215 if(mRSContext.get())
216 return mRSContext->reflectToJava(outputPackageName, mInputFileName, mOutputFileName);
217 else
218 return false;
219}
220
221void Slang::getPragmas(size_t* actualStringCount, size_t maxStringCount, char** strings) {
222 int stringCount = mPragmas.size() * 2;
223
224 if(actualStringCount)
225 *actualStringCount = stringCount;
226 if(stringCount > maxStringCount)
227 stringCount = maxStringCount;
228 if(strings)
229 for(PragmaList::const_iterator it = mPragmas.begin();
230 stringCount > 0;
231 stringCount-=2, it++)
232 {
233 *strings++ = const_cast<char*>(it->first.c_str());
234 *strings++ = const_cast<char*>(it->second.c_str());
235 }
236
237 return;
238}
239
240Slang::~Slang() {
241 llvm::llvm_shutdown();
242 return;
243}
244
245} /* namespace slang */
246