-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMilVmCode.h
More file actions
324 lines (287 loc) · 8.4 KB
/
MilVmCode.h
File metadata and controls
324 lines (287 loc) · 8.4 KB
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
#ifndef MILVMCODE_H
#define MILVMCODE_H
/*
* Copyright 2025 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Micron language project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <Micron/MilAst.h>
class QTextStream;
namespace Mil
{
namespace Vm
{
enum OpArgCode {
NoOpArgs,
OffArg,
SizeArg,
IntArg,
FloatArg,
StrArg,
ByteArrayArg,
ProcArg,
JumpArg,
VtableArg,
OffSizeArgs
};
enum LL_op
{
LL_invalid,
#define OPDEF(op, x) LL_##op
#include "MilVmOps.h"
#undef OPDEF
LL_NUM_OF_OPS
};
struct Operation
{
uint val : 22;
uint minus : 1;
uint op : 9;
Operation(LL_op op = LL_invalid, quint32 val = 0, bool minus = false):val(val),minus(minus),op(op){}
};
struct Template
{
Type* type;
std::vector<char> mem; // preinitialized memory for type
Template():type(0){}
};
struct Procedure
{
std::vector<Operation> ops; // std::vector because QVector.detach() is very expensive
Declaration* decl;
uint init : 1;
uint called : 1;
uint external : 1;
uint id: 29;
quint32 localsSize;
quint32 argsSize, returnSize;
std::vector<std::pair<int,Template> > locals;
Procedure():decl(0),init(0), called(0), external(0), id(0),
localsSize(0),argsSize(0),returnSize(0),ops(0){}
};
struct Vtable
{
Vtable* parent;
Type* type;
std::vector<Procedure*> methods;
};
struct Interface
{
Type* type;
QList<QPair<Type*, quint32> > mappings; // record/object -> vtable index
Interface(Type* t):type(t) {}
int find(Type* t) const
{
for(int i = 0; i < mappings.size(); i++ )
if( mappings[i].first == t )
return i;
return -1;
}
// NOTE:
// instead of explicit vtables for each class/interface combination, we could use the same approach as
// in TinyGo, where the interface reference only contains a type symbol instead of a pointer to a vtable
// and the compiler generates switch statements based on this type symbol which selects the procedure
// which has O(N) instead of O(1).
};
struct MethRef
{
void* obj;
Procedure* proc;
MethRef(void* o = 0, Procedure* p = 0):obj(o),proc(p){}
};
class Code
{
public:
Code(AstModel*, quint8 pointerWidth, quint8 stackAlignment);
~Code();
void addExternal(const char* module, const char* name, quint32 id);
bool compile(Declaration* procOrModule);
qint64 getInt(quint32 n) const { return ints[n]; }
double getDouble(quint32 n) const { return doubles[n]; }
Procedure* getProc(quint32 n) const { return procs[n]; }
Vtable* getVtable(quint32 n) const { return vtables[n]; }
const char* getString(quint32 n) const { return strings[n].c_str(); }
const std::vector<char>& getObject(quint32 n) const { return objects[n]; }
const Template& getTemplate(quint32 n) const { return templates[n]; }
int findProc(Declaration* proc) const
{
while( proc->kind == Declaration::Procedure && proc->forward )
proc = proc->forwardTo;
if( proc->kind == Declaration::Module )
{
Declaration* init = proc->findInitProc();
if( init )
proc = init;
}
for( int i = 0; i < procs.size(); i++ )
{
if( procs[i]->decl == proc )
return i;
}
return -1;
}
inline int stackAligned(int off)
{
return AstModel::align(off, stackAlignment );
}
void initMemory(char* mem, Type* t, bool doPointerInit );
bool dumpAll(QTextStream& out);
bool dumpModule(QTextStream& out, Declaration* module);
bool dumpProc(QTextStream& out, Declaration* proc);
static const char* op_names[];
protected:
bool translateProc(Declaration* proc);
bool translateModule(Declaration* m);
bool translateProc(Procedure& proc);
bool translateStatSeq(Procedure& proc, Statement* s);
bool translateExprSeq(Procedure& proc, Expression* e);
bool translateInit(Procedure& proc, quint32 id);
void render(char* data, quint32 off, Type* t, Constant* c);
void render(char* data, quint32 start, ComponentList* cl );
Type* deref(Type* t)
{
if( t && t->kind == Type::NameRef )
return deref(t->getType());
else if( t )
return t;
else
return mdl->getBasicType(Type::Undefined);
}
int emitOp(Procedure& proc, LL_op op, quint32 v = 0, bool minus = false )
{
const int res = proc.ops.size();
proc.ops.push_back(Operation(op, v, minus));
return res;
}
void inline branch_here(Procedure& proc, int pc)
{
Q_ASSERT(pc >= 0 && pc < proc.ops.size());
proc.ops[pc].val = proc.ops.size() - pc - 1;
}
void downcopy(Vtable* vt);
template<typename T>
int appendUnique(std::vector<T>& vec, const T& val)
{
for (size_t i = 0; i < vec.size(); ++i) {
if (vec[i] == val)
return i;
}
vec.push_back(val);
return vec.size() - 1;
}
quint32 addInt(qint64 i)
{
return appendUnique(ints,i);
}
quint32 addFloat(double f)
{
return appendUnique(doubles, f);
}
quint32 addString(const char* str)
{
return appendUnique(strings, std::string(str));
}
quint32 addObject(Constant* c)
{
const quint32 id = objects.size();
objects.push_back(std::vector<char>());
std::vector<char>& obj = objects.back();
if( c->kind == Constant::B )
{
obj.resize(c->b->len);
memcpy( obj.data(), (const char*)c->b->b,c->b->len);
}else
{
ComponentList* cl = c->c;
Q_ASSERT( cl->type );
obj.resize(deref(cl->type)->getByteSize(sizeof(void*)));
render(obj.data(), 0, cl);
}
return id;
}
int findVtable(Type* object) const
{
for( int i = 0; i < vtables.size(); i++ )
{
if( vtables[i]->type == object )
return i;
}
return -1;
}
Vtable* getVtable(Type* object) const
{
for( int i = 0; i < vtables.size(); i++ )
{
if( vtables[i]->type == object )
return vtables[i];
}
return 0;
}
int findTemplate(Type* t) const
{
for( int i = 0; i < templates.size(); i++ )
if( templates[i].type == t )
return i;
return -1;
}
int findIface(Type* iface) const
{
for( int i = 0; i < interfaces.size(); i++ )
{
if( interfaces[i].type == iface )
return i;
}
return -1;
}
private:
const quint8 pointerWidth;
const quint8 stackAlignment;
AstModel* mdl;
// accessing std::vector is cheaper than QVector or QByteArray
std::vector<std::string> strings;
std::vector< std::vector<char> > objects;
std::vector<double> doubles;
std::vector<qint64> ints;
std::vector<Procedure*> procs;
std::vector<Vtable*> vtables;
std::vector<Template> templates;
std::vector<Interface> interfaces;
// the following are only used during compilation:
struct Where {
const char* name;
int pc;
Where(const char* name, int pc):name(name),pc(pc){}
};
struct Context {
QList< QList<int> > loopStack;
QList<Where> gotos, labels;
Procedure* curProc;
int findLabel(const char* name) const
{
for( int i = 0; i < labels.size(); i++ )
{
if( labels[i].name == name )
return i;
}
return -1;
}
};
QList<Context> ctxStack; // because translateProc is called recursively
QMap<const char*,QMap<const char*, int> > externals;
};
}
}
#endif // MILVMCODE_H