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
|
/* $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 <stdarg.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 pair {
unsigned short val;
int lineno;
char *label;
} *stack, *ref;
static int sp = 0;
static int rp = 0;
static int pc = 0;
static int haserrors = 0;
unsigned short *buffer;
struct label {
char *label;
int lineno;
} *label;
%}
%union {
int ival;
char *sval;
};
%token A B C X Y Z I J
%token PUSH POP PEEK PICK SP PC EX
%token SET ADD SUB MUL MLI DIV DVI MOD AND BOR XOR SHR ASR SHL STI
%token IFB IFC IFE IFN IFG IFA IFL IFU ADX SBX
%token NOP BRK DAT ORG
%token JSR HCF INT IAG IAS HWN HWQ HWI
%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 operand expr extended noop
%left PLUS MINUS
%left MULT
%%
prog
: /* empty */
| prog statement
;
statement
: opcode operand COMMA operand
{
popop(($4 << 10) | ($2 << 5) | $1);
popall();
}
| extended operand
{
popop(($2 << 10) | ($1 << 5));
popall();
}
| noop { popop($1 << 5); }
| DP STRING { addref($2); }
| DAT data { popall(); }
| ORG expr { pc = $2; }
| error
;
data
: /* empty */
| data block
| data COMMA block
;
block
: QSTRING
{
char *s = $1;
while (*s)
push(*s++, NULL);
}
| STRING { push(0, $1); }
| expr { push($1, NULL); }
;
expr
: NUMBER
{
if ($1 > 0xFFFF)
yyerror("integer too big");
$$ = $1;
}
| expr PLUS expr { $$ = $1 + $3; }
| expr MINUS expr { $$ = $1 - $3; }
| expr MULT expr { $$ = $1 * $3; }
| LPAR expr RPAR { $$ = $2; }
;
operand
: register { $$ = $1; }
| LBR register RBR { $$ = 0x08 + $2; }
| PUSH { $$ = 0x18; }
| POP { $$ = 0x18; }
| PEEK { $$ = 0x19; }
| PICK { $$ = 0x1a; }
| SP { $$ = 0x1b; }
| PC { $$ = 0x1c; }
| EX { $$ = 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
: SET { $$ = 0x01; }
| ADD { $$ = 0x02; }
| SUB { $$ = 0x03; }
| MUL { $$ = 0x04; }
| MLI { $$ = 0x05; }
| DIV { $$ = 0x06; }
| DVI { $$ = 0x07; }
| MOD { $$ = 0x08; }
| AND { $$ = 0x09; }
| BOR { $$ = 0x0a; }
| XOR { $$ = 0x0b; }
| SHR { $$ = 0x0c; }
| ASR { $$ = 0x0d; }
| SHL { $$ = 0x0e; }
| STI { $$ = 0x0f; }
| IFB { $$ = 0x10; }
| IFC { $$ = 0x11; }
| IFE { $$ = 0x12; }
| IFN { $$ = 0x13; }
| IFG { $$ = 0x14; }
| IFA { $$ = 0x15; }
| IFL { $$ = 0x16; }
| IFU { $$ = 0x17; }
| ADX { $$ = 0x1a; }
| SBX { $$ = 0x1b; }
;
extended
: JSR { $$ = 0x01; }
| HCF { $$ = 0x07; }
| INT { $$ = 0x08; }
| IAG { $$ = 0x09; }
| IAS { $$ = 0x0a; }
| HWN { $$ = 0x10; }
| HWQ { $$ = 0x11; }
| HWI { $$ = 0x12; }
;
noop
: NOP { $$ = 0x00; }
| BRK { $$ = 0x02; }
;
%%
void
yyerror(const char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "Line %d: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
va_end(ap);
haserrors = 1;
}
void
push(int i, char *s)
{
stack[sp].val = i;
stack[sp].lineno = yylineno;
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].label = stack[n - sp].label;
label[pc++].lineno = stack[n - sp--].lineno;
}
}
void
addref(char *s)
{
ref[rp].label = s;
ref[rp].lineno = yylineno;
ref[rp++].val = pc;
}
int
findref(struct label *l)
{
int i;
for (i = 0; i < rp; i++)
if (strcmp(ref[i].label, l->label) == 0)
return ref[i].val;
yylineno = l->lineno;
yyerror("missing label reference: %s", l->label);
return 0;
}
void
restorerefs(void)
{
int i;
for (i = 0; i < pc; i++)
if (label[i].label)
buffer[i] = findref(&label[i]);
}
unsigned short *
compile(FILE *fd, size_t sz)
{
buffer = calloc(sz, sizeof(unsigned short));
label = calloc(sz, sizeof(struct label));
stack = calloc(sz, sizeof(struct pair));
ref = calloc(sz, sizeof(struct pair));
#if YYDEBUG
yydebug = 1;
#endif
yyin = fd;
yyparse();
restorerefs();
free(ref);
free(stack);
free(label);
if (haserrors) {
free(buffer);
buffer = NULL;
}
return buffer;
}
|