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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
%{
/* get access to fileno to avoid warnings with flex */
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <tasm/assembler.h>
#include <tasm/parser.h>
#include <tasm/bits.h>
#include <tasm/regs.h>
#include <tri.h>
%}
%locations
%define parse.trace
%define parse.error verbose
%define api.pure full
%define lr.type ielr
%lex-param {void *scanner} {struct parser *parser}
%parse-param {void *scanner} {struct parser *parser} {struct asm_ctx *ctx}
%union {
const char *str;
enum gpr_num gpr;
tri_t tri;
};
%token <tri> imm
%token <str> label
%token <str> str
%token <str> id
%token COMMA ","
%token LPAREN "("
%token RPAREN ")"
%token set
%token include
%nterm <gpr> gpr;
%nterm <str> width addr csr
/* regs */
%token x0 x1 x2 x3 x4 x5 x6 x7 x8 x9
%token x10 x11 x12 x13 x14 x15 x16 x17 x18 x19
%token x20 x21 x22 x23 x24 x25 x26 x27 x28 x29
%token x30 x31 x32 x33 x34 x35 x36 x37 x38 x39
%token x40 x41 x42 x43 x44 x45 x46 x47 x48 x49
%token x50 x51 x52 x53 x54 x55 x56 x57 x58 x59
%token x60 x61 x62 x63 x64 x65 x66 x67 x68 x69
%token x70 x71 x72 x73 x74 x75 x76 x77 x78 x79
%token x80
/* i */
%token addi slti sgei seqi snei
%token unop diop
%token slli srli sll srl
%token add sub
%token slt sge seq sne
%token lui auipc
%token jal jalr
%token beq bne blt bge
%token ld st
%token ecall ebreak pcall
%token fence
/* meta */
%token li la nop
/* m (avoid name clash with stdlib div) */
%token mul diV rem
/* a */
%token stt cst ent cat
/* Zcsr */
%token csrrw csrrs csrrc
%{
struct file_ctx {
const char *fname;
const char *fbuf;
};
struct src_loc {
int first_line;
int last_line;
int first_col;
int last_col;
};
struct src_issue {
struct src_loc loc;
struct file_ctx fctx;
};
/** Modifies the signature of yylex to fit our parser better. */
#define YY_DECL int yylex(YYSTYPE *yylval, YYLTYPE *yylloc, \
void *yyscanner, struct parser *parser)
/**
* Declare yylex.
*
* @param yylval Bison current value.
* @param yylloc Bison location info.
* @param yyscanner Flex scanner.
* @param parser Current parser state.
* @return \c 0 when succesful, \c 1 otherwise.
* More info on yylex() can be found in the flex manual.
*/
YY_DECL;
/**
* Convert bison location info to our own source location info.
*
* @param yylloc Bison location info.
* @return Internal location info.
*/
static struct src_loc to_src_loc(YYLTYPE *yylloc);
/**
* Print parsing error.
* Automatically called by bison.
*
* @param yylloc Location of error.
* @param lexer Lexer.
* @param parser Parser state.
* @param msg Message to print.
*/
static void yyerror(YYLTYPE *yylloc, void *lexer,
struct parser *parser, struct asm_ctx *ctx, const char *msg);
%}
%start input;
%%
width
: id
/* eventually we might allow jumping to immediate addresses, but for now require
* that a label always be used */
addr
: id
/* lexer makes sure the abi names are correct */
gpr
: x0 {$$ = X0_NUM;}
| x1 {$$ = X1_NUM;}
| x2 {$$ = X2_NUM;}
| x3 {$$ = X3_NUM;}
| x4 {$$ = X4_NUM;}
| x5 {$$ = X5_NUM;}
| x6 {$$ = X6_NUM;}
| x7 {$$ = X7_NUM;}
| x8 {$$ = X8_NUM;}
| x9 {$$ = X9_NUM;}
| x10 {$$ = X10_NUM;}
| x11 {$$ = X11_NUM;}
| x12 {$$ = X12_NUM;}
| x13 {$$ = X13_NUM;}
| x14 {$$ = X14_NUM;}
| x15 {$$ = X15_NUM;}
| x16 {$$ = X16_NUM;}
| x17 {$$ = X17_NUM;}
| x18 {$$ = X18_NUM;}
| x19 {$$ = X19_NUM;}
| x20 {$$ = X20_NUM;}
| x21 {$$ = X21_NUM;}
| x22 {$$ = X22_NUM;}
| x23 {$$ = X23_NUM;}
| x24 {$$ = X24_NUM;}
| x25 {$$ = X25_NUM;}
| x26 {$$ = X26_NUM;}
| x27 {$$ = X27_NUM;}
| x28 {$$ = X28_NUM;}
| x29 {$$ = X29_NUM;}
| x30 {$$ = X30_NUM;}
| x31 {$$ = X31_NUM;}
| x32 {$$ = X32_NUM;}
| x33 {$$ = X33_NUM;}
| x34 {$$ = X34_NUM;}
| x35 {$$ = X35_NUM;}
| x36 {$$ = X36_NUM;}
| x37 {$$ = X37_NUM;}
| x38 {$$ = X38_NUM;}
| x39 {$$ = X39_NUM;}
| x40 {$$ = X40_NUM;}
| x41 {$$ = X41_NUM;}
| x42 {$$ = X42_NUM;}
| x43 {$$ = X43_NUM;}
| x44 {$$ = X44_NUM;}
| x45 {$$ = X45_NUM;}
| x46 {$$ = X46_NUM;}
| x47 {$$ = X47_NUM;}
| x48 {$$ = X48_NUM;}
| x49 {$$ = X49_NUM;}
| x50 {$$ = X50_NUM;}
| x51 {$$ = X51_NUM;}
| x52 {$$ = X52_NUM;}
| x53 {$$ = X53_NUM;}
| x54 {$$ = X54_NUM;}
| x55 {$$ = X55_NUM;}
| x56 {$$ = X56_NUM;}
| x57 {$$ = X57_NUM;}
| x58 {$$ = X58_NUM;}
| x59 {$$ = X59_NUM;}
| x60 {$$ = X60_NUM;}
| x61 {$$ = X61_NUM;}
| x62 {$$ = X62_NUM;}
| x63 {$$ = X63_NUM;}
| x64 {$$ = X64_NUM;}
| x65 {$$ = X65_NUM;}
| x66 {$$ = X66_NUM;}
| x67 {$$ = X67_NUM;}
| x68 {$$ = X68_NUM;}
| x69 {$$ = X69_NUM;}
| x70 {$$ = X70_NUM;}
| x71 {$$ = X71_NUM;}
| x72 {$$ = X72_NUM;}
| x73 {$$ = X73_NUM;}
| x74 {$$ = X74_NUM;}
| x75 {$$ = X75_NUM;}
| x76 {$$ = X76_NUM;}
| x77 {$$ = X77_NUM;}
| x78 {$$ = X78_NUM;}
| x79 {$$ = X79_NUM;}
| x80 {$$ = X80_NUM;}
i
: addi gpr "," gpr "," imm
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_ADDI, $4, $6);}
| slti gpr "," gpr "," imm
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SLTI, $4, $6);}
| sgei gpr "," gpr "," imm
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SGEI, $4, $6);}
| seqi gpr "," gpr "," imm
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SEQI, $4, $6);}
| snei gpr "," gpr "," imm
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SNEI, $4, $6);}
| slli gpr "," gpr "," imm
{check_shift($6); emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SLLI, $4, $6);}
| srli gpr "," gpr "," imm
{check_shift($6); emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_SRLI, $4, $6);}
| add gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_ADD, $4, $6, 0);}
| sub gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SUB, $4, $6, 0);}
| slt gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SLT, $4, $6, 0);}
| sge gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SGE, $4, $6, 0);}
| seq gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SEQ, $4, $6, 0);}
| sne gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SNE, $4, $6, 0);}
| sll gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SLL, $4, $6, 0);}
| srl gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_SRL, $4, $6, 0);}
| lui gpr "," imm
{emit_u(ctx, OPCODE_LUI, $2, $4);}
| auipc gpr "," imm
{emit_u(ctx, OPCODE_AUIPC, $2, $4);}
| jal gpr "," addr
{emit_u(ctx, OPCODE_JAL, $2, 0);
emit_reloc(ctx, RELOC_J, $4);}
| jalr gpr "," gpr "," imm
{emit_i(ctx, OPCODE_JALR, $2, 0, $4, $6);}
| beq gpr "," gpr "," addr
{emit_s(ctx, OPCODE_BRANCH, BRANCH_BEQ, $2, $4, 0);
emit_reloc(ctx, RELOC_B, $6);}
| bne gpr "," gpr "," addr
{emit_s(ctx, OPCODE_BRANCH, BRANCH_BNE, $2, $4, 0);
emit_reloc(ctx, RELOC_B, $6);}
| blt gpr "," gpr "," addr
{emit_s(ctx, OPCODE_BRANCH, BRANCH_BLT, $2, $4, 0);
emit_reloc(ctx, RELOC_B, $6);}
| bge gpr "," gpr "," addr
{emit_s(ctx, OPCODE_BRANCH, BRANCH_BGE, $2, $4, 0);
emit_reloc(ctx, RELOC_B, $6);}
| ld width "," gpr "," imm "(" gpr ")"
{emit_i(ctx, OPCODE_LOAD, check_width($2), $4, $8, $6);}
| st width "," gpr "," imm "(" gpr ")"
{emit_s(ctx, OPCODE_STORE, check_width($2), $4, $8, $6);}
| ecall
{emit_i(ctx, OPCODE_SYSTEM, X0_NUM, SYSTEM_ECALL, X0_NUM, 0);}
| ebreak
{emit_i(ctx, OPCODE_SYSTEM, X0_NUM, SYSTEM_EBREAK, X0_NUM, 0);}
| pcall
{emit_i(ctx, OPCODE_SYSTEM, X0_NUM, SYSTEM_PCALL, X0_NUM, 0);}
| fence
{emit_i(ctx, OPCODE_MEM, X0_NUM, MEM_FENCE, X0_NUM, 0); /* still todo */}
| unop gpr "," gpr "," str
{emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_UNOP, $4, check_nop($6));}
| diop gpr "," gpr "," gpr "," str
{emit_d(ctx, OPCODE_DIOP, $2, $4, $6, check_nop3($8));}
/* meta */
| nop
{emit_i(ctx, OPCODE_OP_IMM, X0_NUM, OP_IMM_ADDI, X0_NUM, 0);}
| la gpr "," addr
{emit_u(ctx, OPCODE_LUI, $2, 0);
/* important that the reloc is in the middle here */
emit_reloc(ctx, RELOC_LA, $4);
emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_ADDI, $2, 0);}
| li gpr "," imm
{emit_u(ctx, OPCODE_LUI, $2, 0);
emit_i(ctx, OPCODE_OP_IMM, $2, OP_IMM_ADDI, $2, 0);}
/* stuff like call and ret TBD once I've come up with a proper register
* calling convention so we know which register to use as ra */
m
: mul gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_MUL, $4, $6, 0);}
| diV gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_DIV, $4, $6, 0);}
| rem gpr "," gpr "," gpr
{emit_r(ctx, OPCODE_OP, $2, OP_DIV, $4, $6, 0);}
a
: stt
{emit_i(ctx, OPCODE_MEM, X0_NUM, MEM_STT, X0_NUM, 0);}
| cst width "," gpr "," imm "(" gpr ")"
{emit_s(ctx, OPCODE_STORE, STORE_CST | check_width($2), $4, $8, $6);}
| ent gpr
{emit_i(ctx, OPCODE_MEM, $2, MEM_ENT, X0_NUM, 0);}
| cat
{emit_i(ctx, OPCODE_MEM, X0_NUM, MEM_CAT, X0_NUM, 0);}
Zcsr
: csrrw csr "," gpr "," gpr
{emit_i(ctx, OPCODE_SYSTEM, $4, SYSTEM_CSRRW, $6, check_csr($2));}
| csrrs csr "," gpr "," gpr
{emit_i(ctx, OPCODE_SYSTEM, $4, SYSTEM_CSRRS, $6, check_csr($2));}
| csrrc csr "," gpr "," gpr
{emit_i(ctx, OPCODE_SYSTEM, $4, SYSTEM_CSRRC, $6, check_csr($2));}
csr
: id
statement
: i
| a
| m
| Zcsr
directive
: set id imm
| include str {process_file(ctx, $2);}
top
: label {emit_label(ctx, $1);}
| statement
| directive
unit
: top
| top unit
input
: unit
| /* empty */
%%
static const char *find_lineno(const char *buf, size_t no)
{
if (no == 0 || no == 1)
return buf;
char c;
while ((c = *buf)) {
buf++;
if (c == '\n')
no--;
if (no == 1)
break;
}
return buf;
}
static void _issue(struct src_issue issue, const char *fmt, va_list args)
{
/* get start and end of current line in buffer */
const char *line_start = find_lineno(issue.fctx.fbuf,
issue.loc.first_line);
const char *line_end = strchr(line_start, '\n');
if (!line_end)
line_end = strchr(line_start, 0);
const int line_len = line_end - line_start;
fprintf(stderr, "%s:%i:%i: ",
issue.fctx.fname,
issue.loc.first_line,
issue.loc.first_col);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
int lineno_len = snprintf(NULL, 0, "%i", issue.loc.first_line);
fputc(' ', stderr);
fprintf(stderr, "%i | ", issue.loc.first_line);
fprintf(stderr, "%.*s\n", line_len, line_start);
for (int i = 0; i < lineno_len + 2; ++i)
fputc(' ', stderr);
fprintf(stderr, "| ");
for (int i = 0; i < issue.loc.first_col - 1; ++i)
fputc(line_start[i] == '\t' ? '\t' : ' ', stderr);
for (int i = issue.loc.first_col; i < issue.loc.last_col; ++i) {
if (i == issue.loc.first_col)
fputc('^', stderr);
else
fputc('~', stderr);
}
fputc('\n', stderr);
}
void src_issue(struct src_issue issue, const char *err_msg, ...)
{
va_list args;
va_start(args, err_msg);
_issue(issue, err_msg, args);
va_end(args);
}
#include "gen_lexer.inc"
static struct src_loc to_src_loc(YYLTYPE *yylloc)
{
struct src_loc loc;
loc.first_line = yylloc->first_line;
loc.last_line = yylloc->last_line;
loc.first_col = yylloc->first_column;
loc.last_col = yylloc->last_column;
return loc;
}
static void yyerror(YYLTYPE *yylloc, void *lexer,
struct parser *parser, struct asm_ctx *ctx, const char *msg)
{
(void)lexer;
(void)ctx;
struct src_issue issue;
issue.loc = to_src_loc(yylloc);
issue.fctx.fbuf = parser->buf;
issue.fctx.fname = parser->fname;
src_issue(issue, msg);
}
struct parser *create_parser()
{
return calloc(1, sizeof(struct parser));
}
void destroy_parser(struct parser *p)
{
yylex_destroy(p->lexer);
free(p);
}
void parse(struct parser *p, struct asm_ctx *ctx, const char *fname, const char *buf)
{
p->fname = fname;
p->buf = buf;
p->comment_nesting = 0;
p->failed = false;
yylex_init(&p->lexer);
yy_scan_string(buf, p->lexer);
yyparse(p->lexer, p, ctx);
}
|