-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.h
290 lines (232 loc) · 6.84 KB
/
compiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2018 LPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <vector>
#include <stdarg.h>
#include <memory>
#include <assert.h>
#include "common.h"
#include "frontend.h"
#include "symbols.h"
#include "stringTable.h"
#include "compilationContext.h"
#include "commandLine.h"
#include "backend.h"
#include "cppBackend.h"
#include "clrBackend.h"
#include "heapManager.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////////
//
class Compiler : public CompilationContext
{
public:
Compiler() :
m_errorsCount(0),
m_warningsCount(0),
m_enableWarnings(false),
m_enableLogging(false),
m_pTarget(nullptr)
{
m_backends.push_back(make_unique<cpp::CppBackend>());
m_backends.push_back(make_unique<clr::ClrBackend>());
}
~Compiler() override
{
#if 0
m_symbolTable.print();
#endif
}
public:
void compile(int argc, const char* argv[])
{
// parse the command line
//
if(!_parseCommandLine(argc, argv))
return;
if(!m_cmdLine.getFlag("-nologo"))
{
_printBanner();
printf("compiling %s ...\n", m_cmdLine.getInputName());
printf("\n");
}
// reset the state
//
m_errorsCount = 0;
m_warningsCount = 0;
setContext(this);
// set up the initial context
// (predefined symbols, ...)
//
_initContext();
// parsing (constructs the AST and the symbol table)
//
m_frontEnd.parse(m_cmdLine.getInputName());
// done parsing
//
_finishContext();
// code generation
//
if(m_errorsCount == 0)
_codeGeneration();
// print the compilation summary
//
if(m_errorsCount != 0 || (m_warningsCount != 0 && m_enableWarnings) || m_enableLogging)
printf("\n");
printf("%d error(s), %d warning(s)\n", m_errorsCount, m_warningsCount);
#ifdef LPC_VERBOSE
printf("done.\n");
#endif
printf("\n");
setContext(nullptr);
}
int errorsCount() const { return m_errorsCount; }
// implementation of the CompilationContext interface
//
public:
virtual void error(int line, const char* msg, ...)
{
printf("! error on line %d: ", line);
va_list argptr;
va_start(argptr, msg);
vprintf(msg, argptr);
va_end(argptr);
printf("\n");
++m_errorsCount;
}
virtual void warning(int line, const char* msg, ...)
{
if(m_enableWarnings)
{
printf(" warning on line %d: ", line);
va_list argptr;
va_start(argptr, msg);
vprintf(msg, argptr);
va_end(argptr);
printf("\n");
}
++m_warningsCount;
}
virtual void info(const char* msg, ...)
{
if(m_enableLogging)
{
printf(". ");
va_list argptr;
va_start(argptr, msg);
vprintf(msg, argptr);
va_end(argptr);
printf("\n");
}
}
virtual SymbolTable* symbolTable()
{
return &m_symbolTable;
}
virtual CommandLine* commandLine()
{
return &m_cmdLine;
}
virtual HeapManager* heapManager()
{
return &m_heapManager;
}
private:
void _initContext()
{
// global scope
//
symbolTable()->beginGlobalScope();
ts::predefineTypes();
obj::predefineObjects();
}
void _finishContext()
{
symbolTable()->endGlobalScope();
}
void _printBanner() const
{
printf("\n%s\n\n", BUILD_STRING);
}
bool _parseCommandLine(int argc, const char* argv[])
{
m_cmdLine.addFlag("-?", "print the command line help");
m_cmdLine.addFlag("-nologo", "supresses the compiler banner");
m_cmdLine.addFlag("-warnings", "enables the warning messages");
m_cmdLine.addFlag("-logging", "enables the informational messages");
m_cmdLine.addFlag("-debugInfo", "generates debug information");
m_cmdLine.addOption("-outPath=", "changes the output location");
m_cmdLine.addOption("-target=", "selects the code generation target");
for (const auto& backend : m_backends)
{
m_cmdLine.addTarget(backend->targetName());
}
try
{
m_cmdLine.parse(argc, argv);
if(m_cmdLine.getFlag("-?"))
{
_printBanner();
m_cmdLine.printHelp();
return false;
}
// just to make sure we have the input name
//
m_cmdLine.getInputName();
// select the target backend
//
assert(nullptr == m_pTarget);
string targetName = m_cmdLine.getString("-target=");
if(targetName.empty())
throw Exception("target name was not specified");
for (const auto& backend : m_backends)
{
if(targetName == backend->targetName())
{
m_pTarget = backend.get();
break;
}
}
if(nullptr == m_pTarget)
throw Exception("invalid target name (%s)", targetName.c_str());
}
catch(const Exception& e)
{
_printBanner();
printf("COMMAND LINE ERROR: %s\n\n", e.message());
m_cmdLine.printHelp();
return false;
}
m_enableWarnings = m_cmdLine.getFlag("-warnings");
m_enableLogging = m_cmdLine.getFlag("-logging");
return true;
}
void _codeGeneration()
{
assert(m_errorsCount == 0);
m_pTarget->generateCode(m_symbolTable.globalScope());
}
private:
CommandLine m_cmdLine;
HeapManager m_heapManager;
FrontEnd m_frontEnd;
SymbolTable m_symbolTable;
int m_errorsCount;
int m_warningsCount;
bool m_enableWarnings;
bool m_enableLogging;
string m_outputName;
vector<unique_ptr<Backend>> m_backends;
Backend* m_pTarget;
};