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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
|
%{
/*
* Copyright(c) 2019-2023 rev.ng Labs Srl. All Rights Reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "idef-parser.h"
#include "parser-helpers.h"
#include "idef-parser.tab.h"
#include "idef-parser.yy.h"
/* Uncomment this to disable yyasserts */
/* #define NDEBUG */
#define ERR_LINE_CONTEXT 40
%}
%lex-param {void *scanner}
%parse-param {void *scanner}
%parse-param {Context *c}
%define parse.error verbose
%define parse.lac full
%define api.pure full
%locations
%union {
GString *string;
HexValue rvalue;
HexSat sat;
HexCast cast;
HexExtract extract;
HexMpy mpy;
HexSignedness signedness;
int index;
}
/* Tokens */
%start input
%expect 1
%token IN INAME VAR
%token ABS CROUND ROUND CIRCADD COUNTONES INC DEC ANDA ORA XORA PLUSPLUS ASL
%token ASR LSR EQ NEQ LTE GTE MIN MAX ANDL FOR ICIRC IF MUN FSCR FCHK SXT
%token ZXT CONSTEXT LOCNT BREV SIGN LOAD STORE PC LPCFG
%token LOAD_CANCEL STORE_CANCEL CANCEL IDENTITY ROTL INSBITS SETBITS EXTRANGE
%token CAST4_8U FAIL CARRY_FROM_ADD ADDSAT64 LSBNEW
%token TYPE_SIZE_T TYPE_INT TYPE_SIGNED TYPE_UNSIGNED TYPE_LONG
%token <rvalue> REG IMM PRED
%token <index> ELSE
%token <mpy> MPY
%token <sat> SAT
%token <cast> CAST DEPOSIT SETHALF
%token <extract> EXTRACT
%type <string> INAME
%type <rvalue> rvalue lvalue VAR assign_statement var var_decl var_type
%type <rvalue> FAIL
%type <rvalue> TYPE_SIGNED TYPE_UNSIGNED TYPE_INT TYPE_LONG TYPE_SIZE_T
%type <index> if_stmt IF
%type <signedness> SIGN
/* Operator Precedences */
%left MIN MAX
%left '('
%left ','
%left '='
%right CIRCADD
%right INC DEC ANDA ORA XORA
%left '?' ':'
%left ANDL
%left '|'
%left '^' ANDOR
%left '&'
%left EQ NEQ
%left '<' '>' LTE GTE
%left ASL ASR LSR
%right ABS
%left '-' '+'
%left '*' '/' '%' MPY
%right '~' '!'
%left '['
%right CAST
%right LOCNT BREV
/* Bison Grammar */
%%
/* Input file containing the description of each hexagon instruction */
input : instructions
{
/* Suppress warning about unused yynerrs */
(void) yynerrs;
YYACCEPT;
}
;
instructions : instruction instructions
| %empty
;
instruction : INAME
{
gen_inst(c, $1);
}
arguments
{
EMIT_SIG(c, ")");
EMIT_HEAD(c, "{\n");
}
code
{
gen_inst_code(c, &@1);
}
| error /* Recover gracefully after instruction compilation error */
{
free_instruction(c);
}
;
arguments : '(' ')'
| '(' argument_list ')';
argument_list : argument_decl ',' argument_list
| argument_decl
;
var : VAR
{
track_string(c, $1.var.name);
$$ = $1;
}
;
/*
* Here the integer types are defined from valid combinations of
* `signed`, `unsigned`, `int`, and `long` tokens. The `signed`
* and `unsigned` tokens are here assumed to always be placed
* first in the type declaration, which is not the case in
* normal C. Similarly, `int` is assumed to always be placed
* last in the type.
*/
type_int : TYPE_INT
| TYPE_SIGNED
| TYPE_SIGNED TYPE_INT;
type_uint : TYPE_UNSIGNED
| TYPE_UNSIGNED TYPE_INT;
type_ulonglong : TYPE_UNSIGNED TYPE_LONG TYPE_LONG
| TYPE_UNSIGNED TYPE_LONG TYPE_LONG TYPE_INT;
/*
* Here the various valid int types defined above specify
* their `signedness` and `bit_width`. The LP64 convention
* is assumed where longs are 64-bit, long longs are then
* assumed to also be 64-bit.
*/
var_type : TYPE_SIZE_T
{
yyassert(c, &@1, $1.bit_width <= 64,
"Variables with size > 64-bit are not supported!");
$$ = $1;
}
| type_int
{
$$.signedness = SIGNED;
$$.bit_width = 32;
}
| type_uint
{
$$.signedness = UNSIGNED;
$$.bit_width = 32;
}
| type_ulonglong
{
$$.signedness = UNSIGNED;
$$.bit_width = 64;
}
;
/* Rule to capture declarations of VARs */
var_decl : var_type IMM
{
/*
* Rule to capture "int i;" declarations since "i" is special
* and assumed to be always be IMM. Moreover, "i" is only
* assumed to be used in for-loops.
*
* Therefore we want to NOP these declarations.
*/
yyassert(c, &@2, $2.imm.type == I,
"Variable declaration with immedaties only allowed"
" for the loop induction variable \"i\"");
$$ = $2;
}
| var_type var
{
/*
* Allocate new variable, this checks that it hasn't already
* been declared.
*/
gen_varid_allocate(c, &@1, &$2, $1.bit_width, $1.signedness);
/* Copy var for variable name */
$$ = $2;
/* Copy type info from var_type */
$$.signedness = $1.signedness;
$$.bit_width = $1.bit_width;
}
;
/* Return the modified registers list */
code : '{' statements '}'
{
c->inst.code_begin = c->input_buffer + @2.first_column - 1;
c->inst.code_end = c->input_buffer + @2.last_column - 1;
}
| '{'
{
/* Nop */
}
'}'
;
argument_decl : REG
{
emit_arg(c, &@1, &$1);
/* Enqueue register into initialization list */
g_array_append_val(c->inst.init_list, $1);
}
| PRED
{
emit_arg(c, &@1, &$1);
/* Enqueue predicate into initialization list */
g_array_append_val(c->inst.init_list, $1);
}
| IN REG
{
emit_arg(c, &@2, &$2);
}
| IN PRED
{
emit_arg(c, &@2, &$2);
}
| IMM
{
EMIT_SIG(c, ", int %ciV", $1.imm.id);
}
;
code_block : '{' statements '}'
| '{' '}'
;
/* A list of one or more statements */
statements : statements statement
| statement
;
/* Statements can be assignment (rvalue ';'), control or memory statements */
statement : control_statement
| var_decl ';'
| rvalue ';'
| code_block
| ';'
;
assign_statement : lvalue '=' rvalue
{
@1.last_column = @3.last_column;
gen_assign(c, &@1, &$1, &$3);
$$ = $1;
}
| var_decl '=' rvalue
{
@1.last_column = @3.last_column;
gen_assign(c, &@1, &$1, &$3);
$$ = $1;
}
| lvalue INC rvalue
{
@1.last_column = @3.last_column;
HexValue tmp = gen_bin_op(c, &@1, ADD_OP, &$1, &$3);
gen_assign(c, &@1, &$1, &tmp);
$$ = $1;
}
| lvalue DEC rvalue
{
@1.last_column = @3.last_column;
HexValue tmp = gen_bin_op(c, &@1, SUB_OP, &$1, &$3);
gen_assign(c, &@1, &$1, &tmp);
$$ = $1;
}
| lvalue ANDA rvalue
{
@1.last_column = @3.last_column;
HexValue tmp = gen_bin_op(c, &@1, ANDB_OP, &$1, &$3);
gen_assign(c, &@1, &$1, &tmp);
$$ = $1;
}
| lvalue ORA rvalue
{
@1.last_column = @3.last_column;
HexValue tmp = gen_bin_op(c, &@1, ORB_OP, &$1, &$3);
gen_assign(c, &@1, &$1, &tmp);
$$ = $1;
}
| lvalue XORA rvalue
{
@1.last_column = @3.last_column;
HexValue tmp = gen_bin_op(c, &@1, XORB_OP, &$1, &$3);
gen_assign(c, &@1, &$1, &tmp);
$$ = $1;
}
| PRED '=' rvalue
{
@1.last_column = @3.last_column;
gen_pred_assign(c, &@1, &$1, &$3);
}
| IMM '=' rvalue
{
@1.last_column = @3.last_column;
yyassert(c, &@1, $3.type == IMMEDIATE,
"Cannot assign non-immediate to immediate!");
yyassert(c, &@1, $1.imm.type == VARIABLE,
"Cannot assign to non-variable!");
/* Assign to the function argument */
OUT(c, &@1, &$1, " = ", &$3, ";\n");
$$ = $1;
}
| LOAD '(' IMM ',' IMM ',' SIGN ',' var ',' lvalue ')'
{
@1.last_column = @12.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
yyassert(c, &@1, $3.imm.value == 1,
"LOAD of arrays not supported!");
gen_load(c, &@1, &$5, $7, &$9, &$11);
}
| STORE '(' IMM ',' IMM ',' var ',' rvalue ')'
/* Store primitive */
{
@1.last_column = @10.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
yyassert(c, &@1, $3.imm.value == 1,
"STORE of arrays not supported!");
gen_store(c, &@1, &$5, &$7, &$9);
}
| LPCFG '=' rvalue
{
@1.last_column = @3.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
$3 = gen_rvalue_truncate(c, &@1, &$3);
$3 = rvalue_materialize(c, &@1, &$3);
OUT(c, &@1, "gen_set_usr_field(ctx, USR_LPCFG, ", &$3, ");\n");
}
| DEPOSIT '(' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
gen_deposit_op(c, &@1, &$5, &$7, &$3, &$1);
}
| SETHALF '(' rvalue ',' lvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
gen_sethalf(c, &@1, &$1, &$3, &$5, &$7);
}
| SETBITS '(' rvalue ',' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @10.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
gen_setbits(c, &@1, &$3, &$5, &$7, &$9);
}
| INSBITS '(' lvalue ',' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @10.last_column;
yyassert(c, &@1, !is_inside_ternary(c),
"Assignment side-effect not modeled!");
gen_rdeposit_op(c, &@1, &$3, &$9, &$7, &$5);
}
| IDENTITY '(' rvalue ')'
{
@1.last_column = @4.last_column;
$$ = $3;
}
;
control_statement : frame_check
| cancel_statement
| if_statement
| for_statement
;
frame_check : FCHK '(' rvalue ',' rvalue ')' ';'
;
cancel_statement : LOAD_CANCEL
{
gen_load_cancel(c, &@1);
}
| STORE_CANCEL
{
gen_cancel(c, &@1);
}
| CANCEL
;
if_statement : if_stmt
{
/* Fix else label */
OUT(c, &@1, "gen_set_label(if_label_", &$1, ");\n");
}
| if_stmt ELSE
{
@1.last_column = @2.last_column;
$2 = gen_if_else(c, &@1, $1);
}
statement
{
OUT(c, &@1, "gen_set_label(if_label_", &$2, ");\n");
}
;
for_statement : FOR '(' IMM '=' IMM ';' IMM '<' IMM ';' IMM PLUSPLUS ')'
{
yyassert(c, &@3,
$3.imm.type == I &&
$7.imm.type == I &&
$11.imm.type == I,
"Loop induction variable must be \"i\"");
@1.last_column = @13.last_column;
OUT(c, &@1, "for (int ", &$3, " = ", &$5, "; ",
&$7, " < ", &$9);
OUT(c, &@1, "; ", &$11, "++) {\n");
}
code_block
{
OUT(c, &@1, "}\n");
}
;
if_stmt : IF '(' rvalue ')'
{
@1.last_column = @3.last_column;
$1 = gen_if_cond(c, &@1, &$3);
}
statement
{
$$ = $1;
}
;
rvalue : FAIL
{
yyassert(c, &@1, false, "Encountered a FAIL token as rvalue.\n");
}
| assign_statement
| REG
{
$$ = $1;
}
| IMM
{
$$ = $1;
}
| PRED
{
$$ = gen_rvalue_pred(c, &@1, &$1);
}
| PC
{
/* Read PC from the CR */
HexValue rvalue;
memset(&rvalue, 0, sizeof(HexValue));
rvalue.type = IMMEDIATE;
rvalue.imm.type = IMM_PC;
rvalue.bit_width = 32;
rvalue.signedness = UNSIGNED;
$$ = rvalue;
}
| CONSTEXT
{
HexValue rvalue;
memset(&rvalue, 0, sizeof(HexValue));
rvalue.type = IMMEDIATE;
rvalue.imm.type = IMM_CONSTEXT;
rvalue.signedness = UNSIGNED;
rvalue.is_dotnew = false;
$$ = rvalue;
}
| var
{
$$ = gen_rvalue_var(c, &@1, &$1);
}
| MPY '(' rvalue ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_rvalue_mpy(c, &@1, &$1, &$3, &$5);
}
| rvalue '+' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, ADD_OP, &$1, &$3);
}
| rvalue '-' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, SUB_OP, &$1, &$3);
}
| rvalue '*' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, MUL_OP, &$1, &$3);
}
| rvalue ASL rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, ASL_OP, &$1, &$3);
}
| rvalue ASR rvalue
{
@1.last_column = @3.last_column;
assert_signedness(c, &@1, $1.signedness);
if ($1.signedness == UNSIGNED) {
$$ = gen_bin_op(c, &@1, LSR_OP, &$1, &$3);
} else if ($1.signedness == SIGNED) {
$$ = gen_bin_op(c, &@1, ASR_OP, &$1, &$3);
}
}
| rvalue LSR rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, LSR_OP, &$1, &$3);
}
| rvalue '&' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, ANDB_OP, &$1, &$3);
}
| rvalue '|' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, ORB_OP, &$1, &$3);
}
| rvalue '^' rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, XORB_OP, &$1, &$3);
}
| rvalue ANDL rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, ANDL_OP, &$1, &$3);
}
| MIN '(' rvalue ',' rvalue ')'
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, MINI_OP, &$3, &$5);
}
| MAX '(' rvalue ',' rvalue ')'
{
@1.last_column = @3.last_column;
$$ = gen_bin_op(c, &@1, MAXI_OP, &$3, &$5);
}
| '~' rvalue
{
@1.last_column = @2.last_column;
$$ = gen_rvalue_not(c, &@1, &$2);
}
| '!' rvalue
{
@1.last_column = @2.last_column;
$$ = gen_rvalue_notl(c, &@1, &$2);
}
| SAT '(' IMM ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_rvalue_sat(c, &@1, &$1, &$3, &$5);
}
| CAST rvalue
{
@1.last_column = @2.last_column;
$$ = gen_cast_op(c, &@1, &$2, $1.bit_width, $1.signedness);
}
| rvalue EQ rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_cmp(c, &@1, TCG_COND_EQ, &$1, &$3);
}
| rvalue NEQ rvalue
{
@1.last_column = @3.last_column;
$$ = gen_bin_cmp(c, &@1, TCG_COND_NE, &$1, &$3);
}
| rvalue '<' rvalue
{
@1.last_column = @3.last_column;
assert_signedness(c, &@1, $1.signedness);
assert_signedness(c, &@1, $3.signedness);
if ($1.signedness == UNSIGNED || $3.signedness == UNSIGNED) {
$$ = gen_bin_cmp(c, &@1, TCG_COND_LTU, &$1, &$3);
} else {
$$ = gen_bin_cmp(c, &@1, TCG_COND_LT, &$1, &$3);
}
}
| rvalue '>' rvalue
{
@1.last_column = @3.last_column;
assert_signedness(c, &@1, $1.signedness);
assert_signedness(c, &@1, $3.signedness);
if ($1.signedness == UNSIGNED || $3.signedness == UNSIGNED) {
$$ = gen_bin_cmp(c, &@1, TCG_COND_GTU, &$1, &$3);
} else {
$$ = gen_bin_cmp(c, &@1, TCG_COND_GT, &$1, &$3);
}
}
| rvalue LTE rvalue
{
@1.last_column = @3.last_column;
assert_signedness(c, &@1, $1.signedness);
assert_signedness(c, &@1, $3.signedness);
if ($1.signedness == UNSIGNED || $3.signedness == UNSIGNED) {
$$ = gen_bin_cmp(c, &@1, TCG_COND_LEU, &$1, &$3);
} else {
$$ = gen_bin_cmp(c, &@1, TCG_COND_LE, &$1, &$3);
}
}
| rvalue GTE rvalue
{
@1.last_column = @3.last_column;
assert_signedness(c, &@1, $1.signedness);
assert_signedness(c, &@1, $3.signedness);
if ($1.signedness == UNSIGNED || $3.signedness == UNSIGNED) {
$$ = gen_bin_cmp(c, &@1, TCG_COND_GEU, &$1, &$3);
} else {
$$ = gen_bin_cmp(c, &@1, TCG_COND_GE, &$1, &$3);
}
}
| rvalue '?'
{
Ternary t = { 0 };
t.state = IN_LEFT;
t.cond = $1;
g_array_append_val(c->ternary, t);
}
rvalue ':'
{
Ternary *t = &g_array_index(c->ternary, Ternary,
c->ternary->len - 1);
t->state = IN_RIGHT;
}
rvalue
{
@1.last_column = @5.last_column;
$$ = gen_rvalue_ternary(c, &@1, &$1, &$4, &$7);
}
| FSCR '(' rvalue ')'
{
@1.last_column = @4.last_column;
$$ = gen_rvalue_fscr(c, &@1, &$3);
}
| SXT '(' rvalue ',' IMM ',' rvalue ')'
{
@1.last_column = @8.last_column;
yyassert(c, &@1, $5.type == IMMEDIATE &&
$5.imm.type == VALUE,
"SXT expects immediate values\n");
$$ = gen_extend_op(c, &@1, &$3, 64, &$7, SIGNED);
}
| ZXT '(' rvalue ',' IMM ',' rvalue ')'
{
@1.last_column = @8.last_column;
yyassert(c, &@1, $5.type == IMMEDIATE &&
$5.imm.type == VALUE,
"ZXT expects immediate values\n");
$$ = gen_extend_op(c, &@1, &$3, 64, &$7, UNSIGNED);
}
| '(' rvalue ')'
{
$$ = $2;
}
| ABS rvalue
{
@1.last_column = @2.last_column;
$$ = gen_rvalue_abs(c, &@1, &$2);
}
| CROUND '(' rvalue ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_convround_n(c, &@1, &$3, &$5);
}
| CROUND '(' rvalue ')'
{
@1.last_column = @4.last_column;
$$ = gen_convround(c, &@1, &$3);
}
| ROUND '(' rvalue ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_round(c, &@1, &$3, &$5);
}
| '-' rvalue
{
@1.last_column = @2.last_column;
$$ = gen_rvalue_neg(c, &@1, &$2);
}
| ICIRC '(' rvalue ')' ASL IMM
{
@1.last_column = @6.last_column;
$$ = gen_tmp(c, &@1, 32, UNSIGNED);
OUT(c, &@1, "gen_read_ireg(", &$$, ", ", &$3, ", ", &$6, ");\n");
}
| CIRCADD '(' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
gen_circ_op(c, &@1, &$3, &$5, &$7);
}
| LOCNT '(' rvalue ')'
{
@1.last_column = @4.last_column;
/* Leading ones count */
$$ = gen_locnt_op(c, &@1, &$3);
}
| COUNTONES '(' rvalue ')'
{
@1.last_column = @4.last_column;
/* Ones count */
$$ = gen_ctpop_op(c, &@1, &$3);
}
| EXTRACT '(' rvalue ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_extract_op(c, &@1, &$5, &$3, &$1);
}
| EXTRANGE '(' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
yyassert(c, &@1, $5.type == IMMEDIATE &&
$5.imm.type == VALUE &&
$7.type == IMMEDIATE &&
$7.imm.type == VALUE,
"Range extract needs immediate values!\n");
$$ = gen_rextract_op(c,
&@1,
&$3,
$7.imm.value,
$5.imm.value - $7.imm.value + 1);
}
| CAST4_8U '(' rvalue ')'
{
@1.last_column = @4.last_column;
$$ = gen_rvalue_truncate(c, &@1, &$3);
$$.signedness = UNSIGNED;
$$ = rvalue_materialize(c, &@1, &$$);
$$ = gen_rvalue_extend(c, &@1, &$$);
}
| BREV '(' rvalue ')'
{
@1.last_column = @4.last_column;
$$ = gen_rvalue_brev(c, &@1, &$3);
}
| ROTL '(' rvalue ',' rvalue ')'
{
@1.last_column = @6.last_column;
$$ = gen_rotl(c, &@1, &$3, &$5);
}
| ADDSAT64 '(' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
gen_addsat64(c, &@1, &$3, &$5, &$7);
}
| CARRY_FROM_ADD '(' rvalue ',' rvalue ',' rvalue ')'
{
@1.last_column = @8.last_column;
$$ = gen_carry_from_add(c, &@1, &$3, &$5, &$7);
}
| LSBNEW '(' rvalue ')'
{
@1.last_column = @4.last_column;
HexValue one = gen_imm_value(c, &@1, 1, 32, UNSIGNED);
$$ = gen_bin_op(c, &@1, ANDB_OP, &$3, &one);
}
;
lvalue : FAIL
{
@1.last_column = @1.last_column;
yyassert(c, &@1, false, "Encountered a FAIL token as lvalue.\n");
}
| REG
{
$$ = $1;
}
| var
{
$$ = $1;
}
;
%%
int main(int argc, char **argv)
{
if (argc != 5) {
fprintf(stderr,
"Semantics: Hexagon ISA to tinycode generator compiler\n\n");
fprintf(stderr,
"Usage: ./semantics IDEFS EMITTER_C EMITTER_H "
"ENABLED_INSTRUCTIONS_LIST\n");
return 1;
}
enum {
ARG_INDEX_ARGV0 = 0,
ARG_INDEX_IDEFS,
ARG_INDEX_EMITTER_C,
ARG_INDEX_EMITTER_H,
ARG_INDEX_ENABLED_INSTRUCTIONS_LIST
};
FILE *enabled_file = fopen(argv[ARG_INDEX_ENABLED_INSTRUCTIONS_LIST], "w");
FILE *output_file = fopen(argv[ARG_INDEX_EMITTER_C], "w");
fputs("#include \"qemu/osdep.h\"\n", output_file);
fputs("#include \"qemu/log.h\"\n", output_file);
fputs("#include \"cpu.h\"\n", output_file);
fputs("#include \"internal.h\"\n", output_file);
fputs("#include \"tcg/tcg-op.h\"\n", output_file);
fputs("#include \"insn.h\"\n", output_file);
fputs("#include \"opcodes.h\"\n", output_file);
fputs("#include \"translate.h\"\n", output_file);
fputs("#define QEMU_GENERATE\n", output_file);
fputs("#include \"genptr.h\"\n", output_file);
fputs("#include \"tcg/tcg.h\"\n", output_file);
fputs("#include \"macros.h\"\n", output_file);
fprintf(output_file, "#include \"%s\"\n", argv[ARG_INDEX_EMITTER_H]);
FILE *defines_file = fopen(argv[ARG_INDEX_EMITTER_H], "w");
assert(defines_file != NULL);
fputs("#ifndef HEX_EMITTER_H\n", defines_file);
fputs("#define HEX_EMITTER_H\n", defines_file);
fputs("\n", defines_file);
fputs("#include \"insn.h\"\n\n", defines_file);
/* Parser input file */
Context context = { 0 };
context.defines_file = defines_file;
context.output_file = output_file;
context.enabled_file = enabled_file;
/* Initialize buffers */
context.out_str = g_string_new(NULL);
context.signature_str = g_string_new(NULL);
context.header_str = g_string_new(NULL);
context.ternary = g_array_new(FALSE, TRUE, sizeof(Ternary));
/* Read input file */
FILE *input_file = fopen(argv[ARG_INDEX_IDEFS], "r");
fseek(input_file, 0L, SEEK_END);
long input_size = ftell(input_file);
context.input_buffer = (char *) calloc(input_size + 1, sizeof(char));
fseek(input_file, 0L, SEEK_SET);
size_t read_chars = fread(context.input_buffer,
sizeof(char),
input_size,
input_file);
if (read_chars != (size_t) input_size) {
fprintf(stderr, "Error: an error occurred while reading input file!\n");
return -1;
}
yylex_init(&context.scanner);
YY_BUFFER_STATE buffer;
buffer = yy_scan_string(context.input_buffer, context.scanner);
/* Start the parsing procedure */
yyparse(context.scanner, &context);
if (context.implemented_insn != context.total_insn) {
fprintf(stderr,
"Warning: %d/%d meta instructions have been implemented!\n",
context.implemented_insn,
context.total_insn);
}
fputs("#endif " START_COMMENT " HEX_EMITTER_h " END_COMMENT "\n",
defines_file);
/* Cleanup */
yy_delete_buffer(buffer, context.scanner);
yylex_destroy(context.scanner);
free(context.input_buffer);
g_string_free(context.out_str, TRUE);
g_string_free(context.signature_str, TRUE);
g_string_free(context.header_str, TRUE);
g_array_free(context.ternary, TRUE);
fclose(output_file);
fclose(input_file);
fclose(defines_file);
fclose(enabled_file);
return 0;
}
|