-
Notifications
You must be signed in to change notification settings - Fork 1
/
language.h
executable file
·518 lines (420 loc) · 13.1 KB
/
language.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//[of]:Copyright statement
//[c](C) Copyright 2005, 2012 Kristian Dupont Knudsen. MIT license -- see LICENSE.md for details
//[c]This file is most easily read with Code Browser: http://tibleiz.net/code-browser/
//[cf]
//[of]:header
#pragma once
#include <stack>
#include "assembler.h"
#include <boost/function.hpp>
#include <boost/type_traits/function_traits.hpp>
#pragma warning(disable: 4355)
namespace rtasm
{
// using a namespace in a header file is note pretty but since the using statement is
// present within the rtcg namespace, it's somewhat okay..
using namespace IA32;
//using namespace boost;
//[cf]
//[c]
//[of]:class scope_proxy
class scope_proxy
{
public:
enum scope_type
{
PLAIN,
IF,
ELSE,
WHILE,
};
scope_proxy(assembler* asmb) : asm_(asmb), scope_type_(PLAIN) {}
assembler* get_assembler() { return asm_; }
void tag(scope_type _scope_type, std::string const& label_1, std::string const& label_2)
{
scope_type_ = _scope_type;
label_1_ = label_1;
label_2_ = label_2;
}
scope_type get_scope_type() const { return scope_type_; }
std::string const& get_label_1() const { return label_1_; }
std::string const& get_label_2() const { return label_2_; }
private:
assembler* asm_;
scope_type scope_type_;
std::string label_1_;
std::string label_2_;
};
//[cf]
//[of]:class environment
class environment
{
public:
environment() : next_scope_type_(scope_proxy::PLAIN) {}
assembler* get_assembler() { return scopes_.top()->get_assembler(); }
void push_scope(scope_proxy* scope)
{
scope->tag(next_scope_type_, next_label_1_, next_label_2_);
scopes_.push(scope);
next_label_1_ = "";
next_label_2_ = "";
}
void pop_scope()
{
scope_proxy* oldscope = scopes_.top();
scopes_.pop();
if(oldscope->get_scope_type() == scope_proxy::IF)
{
// If this was an "if" scope, we'll register begin by jumping to the "endif" label,
// and then register both the endif and the else label.
std::string else_label = oldscope->get_label_1();
current_endif_label_ = oldscope->get_label_2();
get_assembler()->add_instruction(new Jmp(current_endif_label_));
get_assembler()->register_label(current_endif_label_);
get_assembler()->register_label(else_label);
}
else if(oldscope->get_scope_type() == scope_proxy::ELSE)
{
// It was an "else" scope. Move the "endif" label down to the end of this scope then.
current_endif_label_ = oldscope->get_label_1();
get_assembler()->register_label(current_endif_label_);
}
}
// Tells the environment that the proceeding scope is an if scope and may we
// please have an else label from it.
std::string create_if_scope()
{
next_scope_type_ = scope_proxy::IF;
next_label_1_ = create_label(); // "else" label
next_label_2_ = create_label(); // "endif" label
return next_label_1_;
}
std::string create_else_scope()
{
next_scope_type_ = scope_proxy::ELSE;
next_label_1_ = current_endif_label_; // The "endif" label moves along to the end of the else scope.
return next_label_1_;
}
private:
std::stack<scope_proxy*> scopes_;
scope_proxy::scope_type next_scope_type_;
std::string next_label_1_;
std::string next_label_2_;
std::string current_endif_label_;
std::string create_label()
{
static int counter(0);
std::string result = "env_generated_label_" + counter++;
return result;
}
};
//[cf]
//[of]:environment g_environment;
environment g_environment;
//[cf]
//[c]
//[of]:class functor_base
template<typename Signature>
class functor_base
{
public:
typedef typename boost::function_traits<Signature>::result_type ResultType;
Signature* get_function_get_ptr()
{
assembler* a = g_environment.get_assembler();
return reinterpret_cast<Signature*>(a->get_function_pointer());
}
protected:
//[of]: class variable_d
template<typename T>
class variable_d
{
public:
variable_d() : var_(new T(0)), ownership_(true)
{
}
virtual ~variable_d()
{
// if(ownership_)
// delete var_;
}
explicit variable_d(T const& var) : var_(new T(var)), ownership_(true)
{
assembler* a = g_environment.get_assembler();
// Initialization is required because we don't know
// if this function has been called before.
a->add_instruction(new Mov(EAX, i32(var)));
a->add_instruction(new Mov(get_ptr(), EAX));
}
// Copy constructor. Make a dynamic copy.
variable_d(variable_d<T>& var) : var_(new T(*(var.var_))), ownership_(true)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, var.get_ptr()));
a->add_instruction(new Mov(get_ptr(), EAX));
}
ptr<T> get_ptr()
{
return ptr<T>(var_);
}
variable_d<T>& operator = (variable_d<T>& v)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, v.get_ptr()));
a->add_instruction(new Mov(get_ptr(), EAX));
return *this;
}
// Used for parameters. Assign to the value currently present in EAX.
void from_EAX()
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(get_ptr(), EAX));
}
private:
T* var_;
bool ownership_;
};
//[cf]
//[of]: class bool_d \: public variable_d<bool>
class bool_d : public variable_d<bool>
{
public:
bool_d() {}
explicit bool_d(bool const& var) : variable_d<bool>(var) {}
bool_d(bool_d& var) : variable_d<bool>(var) {}
bool_d operator == (bool_d& v)
{
assembler* a = g_environment.get_assembler();
bool_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Cmp(EAX, EBX));
a->add_instruction(new Mov(EAX, imm<32>(0)));
a->add_instruction(new Set(AL, EQUAL));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
};
//[cf]
//[of]: class int_d \: public variable_d<int>
class int_d : public variable_d<int>
{
public:
int_d() {}
explicit int_d(int const& var) : variable_d<int>(var) {}
int_d(int_d& var) : variable_d<int>(var) {}
int_d& operator += (i32 const& i)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Add(EAX, i));
a->add_instruction(new Mov(get_ptr(), EAX));
return *this;
}
int_d& operator += (int_d& v)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Add(EAX, EBX));
a->add_instruction(new Mov(get_ptr(), EAX));
return *this;
}
int_d& operator -= (i32 const& i)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Sub(EAX, i.value));
a->add_instruction(new Mov(get_ptr(), EAX));
return *this;
}
int_d& operator -= (int_d& v)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Sub(EAX, EBX));
a->add_instruction(new Mov(get_ptr(), EAX));
return *this;
}
int_d operator + (int_d& v)
{
assembler* a = g_environment.get_assembler();
int_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Add(EAX, EBX));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
int_d operator - (int_d& v)
{
assembler* a = g_environment.get_assembler();
int_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Sub(EAX, EBX));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
bool_d operator == (int_d& v)
{
assembler* a = g_environment.get_assembler();
bool_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Cmp(EAX, EBX));
a->add_instruction(new Mov(EAX, imm<32>(0)));
a->add_instruction(new Set(AL, EQUAL));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
bool_d operator == (i32 const& i)
{
assembler* a = g_environment.get_assembler();
bool_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, i));
a->add_instruction(new Cmp(EAX, EBX));
a->add_instruction(new Mov(EAX, imm<32>(0)));
a->add_instruction(new Set(AL, EQUAL));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
bool_d operator != (int_d& v)
{
assembler* a = g_environment.get_assembler();
bool_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, v.get_ptr()));
a->add_instruction(new Cmp(EAX, EBX));
a->add_instruction(new Mov(EAX, imm<32>(0)));
a->add_instruction(new Set(AL, NOT_EQUAL));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
bool_d operator != (i32 const& i)
{
assembler* a = g_environment.get_assembler();
bool_d temporary;
a->add_instruction(new Mov(EAX, get_ptr()));
a->add_instruction(new Mov(EBX, i));
a->add_instruction(new Cmp(EAX, EBX));
a->add_instruction(new Mov(EAX, imm<32>(0)));
a->add_instruction(new Set(AL, NOT_EQUAL));
a->add_instruction(new Mov(temporary.get_ptr(), EAX));
return temporary;
}
};
//[cf]
//[of]: class scope \: public scope_proxy
//[c]Hvordan skal scope fungere?
//[c] - Den skal kende alle variable, og hvilke registre de befinder sig i..
//[c] - Den skal instansieres med parent scopets variable og registre
//[c] - Den skal holde styr på temporaries og registrere disse hos sin functor, der så kan tilbyde hukommelsesplads til dem (om nødvendigt).
//[c] - Hvad med bools? Men skal kunne optimere bools væk fra variable.
//[c]
class scope : public scope_proxy
{
public:
scope(functor_base<Signature>* owner) : scope_proxy(&owner->asm_), owner_(owner)
{
g_environment.push_scope(this);
};
~scope()
{
g_environment.pop_scope();
}
template<typename T>
void register_variable(variable_d<T>& var)
{
}
private:
functor_base<Signature>* owner_;
};
// Allow scopes to access our internal assembler.
friend class scope;
//[cf]
// Parameter container. Currently, only ints are allowed .
std::vector<int_d*> param_;
// The outermost scope ensures that we have an environment right away.
scope outer_scope_;
public:
functor_base() : outer_scope_(this)
{
assembler* a = g_environment.get_assembler();
// Add code for function entry.
// Read arguments from the stack etc.
a->add_instruction(new Push(EDI));
a->add_instruction(new Push(ESI));
a->add_instruction(new Push(EBX));
a->add_instruction(new Push(EBP));
a->add_instruction(new Mov(EBX, ESP));
a->add_instruction(new Add(EBX, 20));
// The following code stores every parameter as a dynamic variable.
// The type system is broken here because we don't know the types of the actual
// parameters. Hence, we just store dynamic ints for now which in effect
// means that you can only use ints as parameters presently.
for(int i = 0; i != boost::function_traits<Signature>::arity; ++i)
{
int_d* parameter = new int_d;
a->add_instruction(new MovIndex(EAX, EBX));
parameter->from_EAX();
a->add_instruction(new Add(EBX, 4));
// Parameters are pushed on the stack in reverse order.
param_.push_back(parameter);
}
}
virtual ~functor_base() {}
void if_d(bool_d& condition)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, condition.get_ptr()));
a->add_instruction(new Cmp(EAX, i32(0)));
std::string else_label = g_environment.create_if_scope();
a->add_instruction(new Jmp(else_label, EQUAL));
}
void else_d()
{
std::string endif_label = g_environment.create_else_scope();
}
void return_d(ResultType i)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, i32(i)));
call_return();
}
void return_d(variable_d<ResultType>& var)
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Mov(EAX, var.get_ptr()));
call_return();
}
private:
assembler asm_;
void call_return()
{
assembler* a = g_environment.get_assembler();
a->add_instruction(new Pop(EBP));
a->add_instruction(new Pop(EBX));
a->add_instruction(new Pop(ESI));
a->add_instruction(new Pop(EDI));
a->add_instruction(new Ret());
}
};
//[cf]
//[of]:class functor
template<typename Signature>
class functor : public boost::function<Signature>
{
public:
functor(functor_base<Signature>& base)
{
*((boost::function<Signature>*)this) = base.get_function_get_ptr();
}
};
//[cf]
//[c]
//[of]:footer
} // namespace rtc
//[cf]