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
|
/* $Id$ */
/*
* Copyright (c) 2012 Dimitri Sokolyuk <demon@dim13.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, dATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
%{
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylineno;
extern FILE *yyin;
int yylex(void);
int yyparse(void);
void yyerror(const char *);
void push(int, char *);
void popop(int);
void popall(void);
void addref(char *);
#if YYDEBUG
extern int yydebug;
#endif
struct {
unsigned short val;
char *label;
} stack[0x100], ref[0x100];
int sp = 0;
int rp = 0;
int pc = 0;
unsigned short *buffer;
char **label;
%}
%union {
int ival;
char *sval;
};
%token A B C X Y Z I J
%token POP PEEK PUSH SP PC O
%token SET ADD SUB MUL DIV MOD SHL SHR AND BOR XOR IFE IFN IFG IFB
%token DAT JSR BRK ORG JMP
%token LBR RBR LBRACE RBRACE LPAR RPAR
%token COMMA DP PLUS MINUS MULT
%token DOT HASH MACRO INCLUDE
%token <ival> NUMBER
%token <sval> STRING QSTRING
%type <ival> register opcode extended value expr
%left PLUS MINUS
%left MULT
%%
prog
: /* empty */
| prog statement
;
statement
: opcode value COMMA value
{
popop(($4 << 10) | ($2 << 4) | $1);
popall();
}
| opcode value
{
popop(($2 << 10) | $1);
popall();
}
| opcode { popop($1); }
| DP STRING { addref($2); }
| DAT data { popall(); }
| ORG expr { pc = $2; }
;
data
: /* empty */
| data block
| data COMMA block
;
expr
: NUMBER { $$ = $1; }
| expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr MULT expr { $$ = $1 * $3; }
| LPAR expr RPAR { $$ = $2; }
;
block
: QSTRING
{
char *s = $1;
while (*s)
push(*s++, NULL);
}
| STRING { push(0, $1); }
| expr { push($1, NULL); }
;
value
: register { $$ = $1; }
| LBR register RBR { $$ = 0x08 + $2; }
| POP { $$ = 0x18; }
| PEEK { $$ = 0x19; }
| PUSH { $$ = 0x1a; }
| SP { $$ = 0x1b; }
| PC { $$ = 0x1c; }
| O { $$ = 0x1d; }
| LBR expr RBR
{
$$ = 0x1e;
push($2, NULL);
}
| LBR STRING RBR
{
$$ = 0x1e;
push(0, $2);
}
| LBR expr PLUS register RBR
{
$$ = 0x10 + $4;
push($2, NULL);
}
| LBR register PLUS expr RBR
{
$$ = 0x10 + $2;
push($4, NULL);
}
| LBR STRING PLUS register RBR
{
$$ = 0x10 + $4;
push(0, $2);
}
| LBR register PLUS STRING RBR
{
$$ = 0x10 + $2;
push(0, $4);
}
| expr
{
if ($1 < 0x20)
$$ = 0x20 + $1;
else {
$$ = 0x1f;
push($1, NULL);
}
}
| STRING
{
$$ = 0x1f;
push(0, $1);
}
;
register
: A { $$ = 0x00; }
| B { $$ = 0x01; }
| C { $$ = 0x02; }
| X { $$ = 0x03; }
| Y { $$ = 0x04; }
| Z { $$ = 0x05; }
| I { $$ = 0x06; }
| J { $$ = 0x07; }
;
opcode
: extended { $$ = $1 << 4; }
| SET { $$ = 0x01; }
| ADD { $$ = 0x02; }
| SUB { $$ = 0x03; }
| MUL { $$ = 0x04; }
| DIV { $$ = 0x05; }
| MOD { $$ = 0x06; }
| SHL { $$ = 0x07; }
| SHR { $$ = 0x08; }
| AND { $$ = 0x09; }
| BOR { $$ = 0x0a; }
| XOR { $$ = 0x0b; }
| IFE { $$ = 0x0c; }
| IFN { $$ = 0x0d; }
| IFG { $$ = 0x0e; }
| IFB { $$ = 0x0f; }
;
extended
: JSR { $$ = 0x01; }
| BRK { $$ = 0x02; }
;
%%
void
yyerror(const char *s)
{
fprintf(stderr, "Line %d: %s\n", yylineno, s);
exit(1);
}
void
push(int i, char *s)
{
stack[sp].val = i;
stack[sp++].label = s;
}
void
popop(int n)
{
buffer[pc++] = n;
}
void
popall(void)
{
int n = sp;
while (sp > 0) {
buffer[pc] = stack[n - sp].val;
label[pc++] = stack[n - sp--].label;
}
}
void
addref(char *s)
{
ref[rp].label = s;
ref[rp++].val = pc;
}
int
findref(char *s)
{
int i;
for (i = 0; i < rp; i++)
if (strcmp(ref[i].label, s) == 0)
return ref[i].val;
return 0;
}
void
restorerefs(void)
{
int i;
for (i = 0; i < pc; i++)
if (label[i])
buffer[i] = findref(label[i]);
}
unsigned short *
compile(FILE *fd, size_t sz)
{
buffer = calloc(sz, sizeof(unsigned short));
label = calloc(sz, sizeof(char *));
#if YYDEBUG
yydebug = 1;
#endif
yyin = fd;
yyparse();
restorerefs();
free(label);
return buffer;
}
|