forked from rochus-keller/Micron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicCilGen.cpp
331 lines (291 loc) · 9.46 KB
/
MicCilGen.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
** Copyright (C) 2024 Rochus Keller (me@rochus-keller.ch)
**
** This file is part of the Micron language project.
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
*/
#include "MicCilGen.h"
#include <PeLib/PublicApi.h>
#include <PeLib/PEMetaTables.h>
using namespace Mic;
using namespace DotNetPELib;
struct Thing
{
enum Type { Module, Struct, Union, ProcType, Pointer, Array, Field, Var, Const, Proc } type;
Thing(Type t, Resource* r):type(t),me(r){}
~Thing()
{
QMap<const char*,Thing*>::const_iterator i;
for( i = subs.begin(); i != subs.end(); ++i )
delete i.value();
}
Resource* me;
QMap<const char*,Thing*> subs;
};
class CilGen::Imp
{
public:
MilLoader* loader;
InMemRenderer imr;
PELib* out;
typedef QMap<const char*,Thing*> Imports;
Imports imports;
Thing* module;
QList<Thing*> current;
Imp(MilLoader*l):loader(l),imr(l),out(0),module(0)
{
}
~Imp()
{
if( module )
delete module;
}
void instantiate( const MilType& type, DataContainer* dc)
{
Type* t;
Thing* base = 0;
if( !type.base.second.isEmpty() )
base = find(type.base);
switch(type.kind)
{
// TODO
case MilEmitter::Struct:
case MilEmitter::Union:
{
Class* cls = new Class(type.name.constData(),Qualifiers::Public | Qualifiers::Value,-1,-1);
dc->Add(cls);
current.back()->subs.insert(type.name, new Thing(type.kind == MilEmitter::Struct ?
Thing::Struct : Thing::Union,t));
}
break;
case MilEmitter::ProcType:
break;
case MilEmitter::Alias:
// TODO: call recursively for resolved base
break;
case MilEmitter::Pointer:
case MilEmitter::Array:
if( Type* primitive = dynamic_cast<Type*>(base->me) )
t = new Type(primitive->GetBasicType());
else
t = new Type(dynamic_cast<DataContainer*>(base->me));
if( type.kind == MilEmitter::Array )
t->ArrayLevel(1);
else
t->PointerLevel(1);
// TODO: use plain memory for array instead of built-in array type
//dc->Add(t);
current.back()->subs.insert(type.name, new Thing(Thing::Array,t));
break;
default:
Q_ASSERT(false);
}
}
void instantiate( const MilProcedure& p, DataContainer* dc)
{
// TODO
}
void instantiate( const MilVariable& v, DataContainer* dc, bool module)
{
Thing* type = find(v.type);
Qualifiers q = Qualifiers::Public;
if( module )
q |= Qualifiers::Static;
Field* f = new Field(v.name.constData(),dynamic_cast<Type*>(type->me), q);
dc->Add(f);
current.back()->subs.insert(v.name, new Thing(Thing::Var,f));
}
void instantiate( const MilConst& c, DataContainer* dc)
{
// NOP ?
}
void instantiate( const QByteArray& module)
{
fetchImport(module);
}
void instantiate(const MilModule* m, DataContainer* dc )
{
for( int i = 0; i < m->order.size(); i++ )
{
const MilModule::Order& o = m->order[i];
switch( o.first )
{
case MilModule::Type:
instantiate(m->types[o.second],dc);
break;
case MilModule::Variable:
instantiate(m->vars[o.second],dc,true);
break;
case MilModule::Proc:
instantiate(m->procs[o.second],dc);
break;
case MilModule::Import:
instantiate(m->imports[o.second]);
break;
case MilModule::Const:
instantiate(m->consts[o.second],dc);
break;
}
}
}
Thing* fetchImport( const QByteArray& module )
{
const MilModule* m = loader->getModule(module);
if( m == 0 )
return 0;
AssemblyDef* a = out->AddExternalAssembly(module.constData());
Thing* res = new Thing(Thing::Module, a);
imports.insert(module,res);
current.push_back(res);
instantiate(m,a);
current.pop_back();
return res;
}
static void cannotResolve( const QString& what )
{
throw QString("cannot resolve '%1'").arg(what);
}
Thing* find(const MilTrident& what)
{
Thing* type = find(what.first);
Thing* res = type->subs.value(what.second.constData());
if( res == 0 )
cannotResolve(what.second);
return res;
}
Thing* find(const MilQuali& what)
{
Thing* m = module;
if( !what.first.isEmpty() )
{
Thing* t = imports.value(what.first.constData());
if( t == 0 )
{
t = fetchImport(what.first);
if( t == 0 )
cannotResolve(what.first);
imports.insert(what.first.constData(), t );
}
m = t;
}
Thing* res = m->subs.value(what.second.constData());
if( res == 0 )
cannotResolve(what.second);
return res;
}
void addLabelOp( Method* m, quint8 op, const QByteArray& label )
{
m->AddInstruction(new Instruction((Instruction::iop)op, new Operand( label.constData() ) ) );
}
Thing* addTypeOp( Method* m, quint8 op, const MilQuali& typeRef )
{
Thing* type = find(typeRef);
Type* t = dynamic_cast<Type*>(type->me);
Q_ASSERT( t );
m->AddInstruction(new Instruction((Instruction::iop)op, new Operand( new Value(t))));
return type;
}
void addMethodOp( Method* m, quint8 op, const MilQuali& methodRef )
{
Thing* node = find(methodRef);
Q_ASSERT(node);
MethodSignature* sig = 0;
if( Method* meth = dynamic_cast<Method*>(node->me) )
sig = meth->Signature();
else
sig = dynamic_cast<MethodSignature*>(node->me); // happens only for vararg signatures
Q_ASSERT( sig );
m->AddInstruction(new Instruction((Instruction::iop)op, new Operand( new MethodName( sig ))));
}
void addFieldOp( Method* m, quint8 op, const MilTrident& fieldRef )
{
Thing* node = find(fieldRef);
Q_ASSERT(node);
Field* f = dynamic_cast<Field*>(node->me);
Q_ASSERT( f );
m->AddInstruction( new Instruction((Instruction::iop)op, new Operand( new FieldName(f))));
}
void addOperand( Method* m, quint8 op, Operand* v )
{
m->AddInstruction( new Instruction((Instruction::iop)op, v));
}
void addLocalOp( Method* m, quint8 op, const QByteArray& local )
{
m->AddInstruction( new Instruction((Instruction::iop)op,
new Operand( m->getLocal(local.toInt() ))));
}
void addArgOp( Method* m, quint8 op, const QByteArray& arg )
{
int i = arg.toInt();
MethodSignature* sig = m->Signature();
m->AddInstruction( new Instruction((Instruction::iop)op,new Operand(sig->getParam(i))));
}
};
CilGen::CilGen(MilLoader* l)
{
imp = new Imp(l);
}
CilGen::~CilGen()
{
delete imp;
}
void CilGen::beginModule(const QByteArray& moduleName, const QString& sourceFile, const QByteArrayList& mp)
{
imp->imr.beginModule(moduleName, sourceFile, mp);
imp->out = new PELib(moduleName.constData(), PELib::ilonly);
if( imp->module )
delete imp->module;
// PELib implicitly associates a CIL assembly with exactly one CIL module. The WorkingAssembly is
// automatically created by PELib; we use it as a MIC module. CIL supports creating fields and
// methods directly in the CIL module.
imp->module = new Thing(Thing::Module,imp->out->WorkingAssembly());
imp->current.append(imp->module);
}
void CilGen::endModule()
{
imp->imr.endModule();
MetaBase::dump();
delete imp->out;
imp->out = 0;
}
void CilGen::addImport(const QByteArray& path)
{
imp->imr.addImport(path);
}
void CilGen::addVariable(const MilQuali& typeRef, QByteArray name, bool isPublic)
{
imp->imr.addVariable(typeRef, name, isPublic);
}
void CilGen::addConst(const MilQuali& typeRef, const QByteArray& name, const QVariant& val)
{
imp->imr.addConst(typeRef, name, val);
}
void CilGen::addProcedure(const MilProcedure& method)
{
imp->imr.addProcedure(method);
}
void CilGen::beginType(const QByteArray& name, bool isPublic, quint8 typeKind)
{
imp->imr.beginType(name, isPublic, typeKind);
}
void CilGen::endType()
{
imp->imr.endType();
}
void CilGen::addType(const QByteArray& name, bool isPublic, const MilQuali& baseType, quint8 typeKind, quint32 len)
{
imp->imr.addType(name, isPublic, baseType, typeKind, len);
}
void CilGen::addField(const QByteArray& fieldName, const MilQuali& typeRef, bool isPublic)
{
imp->imr.addField(fieldName, typeRef, isPublic);
}