-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-generator.cpp
232 lines (195 loc) · 3.45 KB
/
code-generator.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
#include "code-generator.h"
#include "bit-stream.h"
void
ADD::text( ostream& oss ) const
{
oss << "ADD" << endl;
}
void
ADD::bytecode( ostream& oss ) const
{
oss << U16( OP_ADD ) << endl;
}
void
AND::text( ostream& oss ) const
{
oss << "AND" << endl;
}
void
AND::bytecode( ostream& oss ) const
{
oss << U16( OP_ADD ) << endl;
}
void
ASSIGN::text( ostream& oss ) const
{
oss << "ASSIGN" << endl;
}
void
ASSIGN::bytecode( ostream& oss ) const
{
oss << U16( OP_ASSIGN );
}
void
CALL::text( ostream& oss ) const
{
oss << "CALL " << ident << " " << argc << endl;
}
void
CALL::bytecode( ostream& oss ) const
{
oss << U16( OP_CALL ) << STR( ident ) << U16( argc );
}
void
CMP::text( ostream& oss ) const
{
oss << "CMP" << endl;
}
void
CMP::bytecode( ostream& oss ) const
{
oss << U16( OP_CMP );
}
void
CMPGT::text( ostream& oss ) const
{
oss << "CMPGT" << endl;
}
void
CMPGT::bytecode( ostream& oss ) const
{
oss << U16( OP_CMPGT );
}
void
CMPGTE::text( ostream& oss ) const
{
oss << "CMPGTE" << endl;
}
void
CMPGTE::bytecode( ostream& oss ) const
{
oss << U16( OP_CMPGTE );
}
void
CMPLT::text( ostream& oss ) const
{
oss << "CMPLT" << endl;
}
void
CMPLT::bytecode( ostream& oss ) const
{
oss << U16( OP_CMPLT );
}
void
CMPLTE::text( ostream& oss ) const
{
oss << "CMPLTE" << endl;
}
void
CMPLTE::bytecode( ostream& oss ) const
{
oss << U16( OP_CMPLTE );
}
void
CMPNE::text( ostream& oss ) const
{
oss << "CMPNE" << endl;
}
void
CMPNE::bytecode( ostream& oss ) const
{
oss << U16( OP_CMPNE );
}
void
FUNCDEF::text( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
oss << ref << " " << TFUNC << " " << argc << endl;
}
void
FUNCDEF::bytecode( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
oss << U16( OP_FUNC );
}
void
JZ::text( ostream& oss ) const
{
oss << "JZ " << ref << endl;
}
void
JZ::bytecode( ostream& oss ) const
{
oss << U16( OP_CMP ) << REF( ref );
}
void
OR::text( ostream& oss ) const
{
oss << "OR" << endl;
}
void
OR::bytecode( ostream& oss ) const
{
oss << U16( OP_OR );
}
void
PUSH::text( ostream& oss ) const
{
oss << "PUSH " << type << " " << value << endl;
}
void
PUSH::bytecode( ostream& oss ) const
{
switch( type ) {
case TSTR: oss << U16( OP_PUSH | OP_STR ) << REF( value ); break;
case TFUNC: oss << U16( OP_PUSH | OP_FUNC ) << REF( value ); break;
case TIDENT: oss << U16( OP_PUSH | OP_IDENT ) << STR( value ); break;
case TINT: oss << U16( OP_PUSH | OP_INT ) << U64( value ); break;
case TDOUBLE: oss << U16( OP_PUSH | OP_DBL ) << DBL( value ); break;
case TNIL: oss << U16( OP_PUSH | OP_NIL ); break;
default:
printf( "Unknown type for PUSH: %d\n", type );
exit( -1 );
break;
}
}
void
POP::text( ostream& oss ) const
{
oss << "POP " << ident << endl;
}
void
POP::bytecode( ostream& oss ) const
{
}
void
REFERENCEDEF::text( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
oss << ref << endl;
}
void
REFERENCEDEF::bytecode( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
}
void
RET::text( ostream& oss ) const
{
oss << "RET" << endl;
}
void
RET::bytecode( ostream& oss ) const
{
}
void
STRINGDEF::text( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
oss << ref << " " << TSTR << " " << ( str.size() ) << " " << str << endl;
}
void
STRINGDEF::bytecode( ostream& oss ) const
{
REF::found_references[ ref ] = oss.tellp();
}