-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcgen.h
More file actions
404 lines (362 loc) · 12.4 KB
/
cgen.h
File metadata and controls
404 lines (362 loc) · 12.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
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
//
// Project: clibparser
// Created by bajdcc
//
#ifndef CLIBPARSER_CGEN_H
#define CLIBPARSER_CGEN_H
#include <vector>
#include <memory>
#include "cast.h"
#include "cparser.h"
#include "cvm.h"
namespace clib {
enum symbol_t {
s_sym,
s_type,
s_type_base,
s_type_typedef,
s_id,
s_struct,
s_function,
s_var,
s_var_id,
s_expression,
s_cast,
s_unop,
s_sinop,
s_binop,
s_triop,
s_list,
s_ctrl,
s_statement,
};
enum cast_t {
t_char,
t_uchar,
t_short,
t_ushort,
t_int,
t_uint,
t_long,
t_ulong,
t_float,
t_double,
t_ptr,
t_struct,
t_error,
};
enum gen_t {
g_ok,
g_error,
g_no_load,
};
class igen {
public:
virtual void emit(ins_t) = 0;
virtual void emit(ins_t, int) = 0;
virtual void emit(ins_t, int, int) = 0;
virtual void emit(keyword_t) = 0;
virtual int current() const = 0;
virtual void edit(int, int) = 0;
virtual int load_string(const string_t &) = 0;
virtual void error(const string_t &) const = 0;
};
enum sym_size_t {
x_load,
x_size,
x_inc,
x_matrix,
};
class sym_t {
public:
using ref = std::shared_ptr<sym_t>;
using weak_ref = std::weak_ptr<sym_t>;
virtual symbol_t get_type() const;
virtual symbol_t get_base_type() const;
virtual int size(sym_size_t t) const;
virtual string_t get_name() const;
virtual string_t to_string() const;
virtual gen_t gen_lvalue(igen &gen);
virtual gen_t gen_rvalue(igen &gen);
virtual gen_t gen_invoke(igen &gen, ref &list);
virtual cast_t get_cast() const;
int line{0}, column{0};
};
class type_t : public sym_t {
public:
using ref = std::shared_ptr<type_t>;
explicit type_t(int ptr = 0);
symbol_t get_type() const override;
symbol_t get_base_type() const override;
virtual ref clone() const;
int ptr;
std::vector<int> matrix;
};
class type_base_t : public type_t {
public:
explicit type_base_t(lexer_t type, int ptr = 0);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
cast_t get_cast() const override;
type_t::ref clone() const override;
lexer_t type;
};
class type_typedef_t : public type_t {
public:
explicit type_typedef_t(const sym_t::ref &refer, int ptr = 0);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t to_string() const override;
cast_t get_cast() const override;
ref clone() const override;
sym_t::weak_ref refer;
};
enum sym_class_t {
z_undefined,
z_global_var,
z_local_var,
z_param_var,
z_struct_var,
z_function,
z_end,
};
const string_t &sym_class_string(sym_class_t);
class type_exp_t : public sym_t {
public:
using ref = std::shared_ptr<type_exp_t>;
explicit type_exp_t(const type_t::ref &base);
symbol_t get_type() const override;
symbol_t get_base_type() const override;
int size(sym_size_t t) const override;
gen_t gen_invoke(igen& gen, sym_t::ref& list) override;
cast_t get_cast() const override;
type_t::ref base;
};
class sym_id_t : public sym_t {
public:
using ref = std::shared_ptr<sym_id_t>;
explicit sym_id_t(const type_t::ref &base, const string_t &id);
symbol_t get_type() const override;
symbol_t get_base_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
cast_t get_cast() const override;
type_t::ref base;
type_exp_t::ref init;
string_t id;
sym_class_t clazz{z_undefined};
int addr{0};
int addr_end{0};
};
class sym_struct_t : public sym_t {
public:
using ref = std::shared_ptr<sym_id_t>;
explicit sym_struct_t(bool _struct, const string_t &id);
symbol_t get_type() const override;
symbol_t get_base_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
cast_t get_cast() const override;
bool _struct{true};
string_t id;
int _size{0};
std::vector<sym_id_t::ref> decls;
};
class sym_func_t : public sym_id_t {
public:
explicit sym_func_t(const type_t::ref &base, const string_t &id);
symbol_t get_type() const override;
symbol_t get_base_type() const override;
int size(sym_size_t t) const override;
string_t to_string() const override;
gen_t gen_invoke(igen &gen, sym_t::ref &list) override;
cast_t get_cast() const override;
std::vector<sym_id_t::ref> params;
int ebp{0}, ebp_local{0};
int entry{0};
};
class sym_var_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_var_t>;
explicit sym_var_t(const type_t::ref &base, ast_node *node);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
cast_t get_cast() const override;
ast_node *node{nullptr};
};
class sym_var_id_t : public sym_var_t {
public:
using ref = std::shared_ptr<sym_var_id_t>;
explicit sym_var_id_t(const type_t::ref &base, ast_node *node, const sym_t::ref &symbol);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
gen_t gen_invoke(igen &gen, sym_t::ref &list) override;
cast_t get_cast() const override;
sym_t::weak_ref id;
};
class sym_cast_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_cast_t>;
explicit sym_cast_t(const type_exp_t::ref &exp, const type_t::ref &base);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp;
};
class sym_unop_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_unop_t>;
explicit sym_unop_t(const type_exp_t::ref &exp, ast_node *op);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp;
ast_node *op{nullptr};
};
class sym_sinop_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_sinop_t>;
explicit sym_sinop_t(const type_exp_t::ref &exp, ast_node *op);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp;
ast_node *op{nullptr};
};
class sym_binop_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_binop_t>;
explicit sym_binop_t(const type_exp_t::ref &exp1, const type_exp_t::ref &exp2, ast_node *op);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp1, exp2;
ast_node *op{nullptr};
};
class sym_triop_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_triop_t>;
explicit sym_triop_t(const type_exp_t::ref &exp1, const type_exp_t::ref &exp2,
const type_exp_t::ref &exp3, ast_node *op1, ast_node *op2);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp1, exp2, exp3;
ast_node *op1{nullptr}, *op2{nullptr};
};
class sym_list_t : public type_exp_t {
public:
using ref = std::shared_ptr<sym_list_t>;
sym_list_t();
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
std::vector<type_exp_t::ref> exps;
};
class sym_ctrl_t : public sym_t {
public:
using ref = std::shared_ptr<sym_ctrl_t>;
explicit sym_ctrl_t(ast_node *op);
symbol_t get_type() const override;
int size(sym_size_t t) const override;
string_t get_name() const override;
string_t to_string() const override;
gen_t gen_lvalue(igen &gen) override;
gen_t gen_rvalue(igen &gen) override;
type_exp_t::ref exp;
ast_node *op{nullptr};
};
struct cycle_t {
int _break;
int _continue;
};
struct switch_t {
type_exp_t::ref _case;
int addr;
};
struct PE {
char magic[4];
uint entry;
uint data_len;
uint text_len;
byte data;
// byte *data;
// byte *text;
};
// 生成虚拟机指令
class cgen : public csemantic, public igen {
public:
cgen();
~cgen() = default;
cgen(const cgen &) = delete;
cgen &operator=(const cgen &) = delete;
backtrace_direction check(pda_edge_t, ast_node *) override;
void gen(ast_node *node);
void reset();
std::vector<byte> file() const;
void emit(ins_t) override;
void emit(ins_t, int) override;
void emit(ins_t, int, int) override;
void emit(keyword_t) override;
int current() const override;
void edit(int, int) override;
int load_string(const string_t &) override;
void error(const string_t &) const override;
private:
void gen_rec(ast_node *node, int level);
void gen_coll(const std::vector<ast_node *> &nodes, int level, ast_node *node);
void gen_stmt(const std::vector<ast_node *> &nodes, int level, ast_node *node);
void allocate(sym_id_t::ref id, const type_exp_t::ref &init, int delta = 0);
sym_id_t::ref add_id(const type_base_t::ref &, sym_class_t, ast_node *, const type_exp_t::ref &, int = 0);
sym_t::ref find_symbol(const string_t &name);
sym_var_t::ref primary_node(ast_node *node);
void error(ast_node *, const string_t &, bool info = false) const;
void error(sym_t::ref s, const string_t &) const;
sym_list_t::ref exp_list(const std::vector<sym_t::ref> &exps);
type_exp_t::ref to_exp(sym_t::ref s);
private:
std::vector<LEX_T(int)> text; // 代码
std::vector<LEX_T(char)> data; // 数据
std::vector<std::unordered_map<LEX_T(string), std::shared_ptr<sym_t>>> symbols; // 符号表
std::vector<std::vector<ast_node *>> ast;
std::vector<std::vector<sym_t::ref>> tmp;
std::vector<cycle_t> cycle;
std::vector<std::vector<switch_t>> cases;
sym_t::weak_ref ctx;
std::vector<sym_t::ref> ctx_stack;
int global_id{0};
};
}
#endif //CLIBPARSER_CGEN_H