123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- %{
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include "../6c/6.out.h"
- #include "a.h"
- %}
- %union {
- Sym *sym;
- long lval;
- double dval;
- char sval[8];
- Gen gen;
- Gen2 gen2;
- }
- %left '|'
- %left '^'
- %left '&'
- %left '<' '>'
- %left '+' '-'
- %left '*' '/' '%'
- %token <lval> LTYPES LTYPED LTYPEB LTYPE1 LTYPE2
- %token <lval> LCONST LSP LFP LPC LSB LTYPEX
- %token <lval> LRREG LFREG
- %token <dval> LFCONST
- %token <sval> LSCONST
- %token <sym> LNAME LLAB LVAR
- %type <lval> con expr pointer offset reg
- %type <gen> addr
- %type <gen> imm nam
- %type <gen2> regs srrr2 drrr2 spec1 spec2
- %%
- prog:
- | prog line
- line:
- LLAB ':'
- {
- if($1->value != pc)
- yyerror("redeclaration of %s", $1->name);
- $1->value = pc;
- }
- line
- | LNAME ':'
- {
- $1->type = LLAB;
- $1->value = pc;
- }
- line
- | ';'
- | inst ';'
- | error ';'
- inst:
- LNAME '=' expr
- {
- $1->type = LVAR;
- $1->value = $3;
- }
- | LVAR '=' expr
- {
- if($1->value != $3)
- yyerror("redeclaration of %s", $1->name);
- $1->value = $3;
- }
- | LTYPEB regs { outcode($1, &$2); }
- | LTYPES srrr2 { outcode($1, &$2); }
- | LTYPED drrr2 { outcode($1, &$2); }
- | LTYPE1 spec1 { outcode($1, &$2); }
- | LTYPE2 spec2 { outcode($1, &$2); }
- srrr2:
- regs
- | addr
- {
- $$.from = $1;
- $$.to = nullgen;
- $$.type = D_NONE;
- }
- drrr2:
- regs
- | addr
- {
- $$.from = nullgen;
- $$.to = $1;
- $$.type = D_NONE;
- }
- regs:
- comma
- {
- $$.from = nullgen;
- $$.to = nullgen;
- $$.type = D_NONE;
- }
- | addr ','
- {
- $$.from = $1;
- $$.to = nullgen;
- $$.type = D_NONE;
- }
- | ',' addr
- {
- $$.from = nullgen;
- $$.to = $2;
- $$.type = D_NONE;
- }
- | addr ',' addr
- {
- $$.from = $1;
- $$.to = $3;
- $$.type = D_NONE;
- }
- | addr ',' reg ',' addr
- {
- $$.from = $1;
- $$.to = $5;
- $$.type = $3;
- }
- spec1: /* DATA */
- addr '/' con ',' imm
- {
- $$.from = $1;
- $$.from.scale = $3;
- $$.to = $5;
- $$.type = D_NONE;
- }
- spec2: /* SHL/SHR/MOV */
- regs
- | regs ':' LRREG
- {
- $$ = $1;
- if($$.from.index != D_NONE)
- yyerror("dp shift with lhs index");
- $$.from.index = $3;
- }
- addr:
- reg
- {
- $$ = nullgen;
- $$.type = $1;
- }
- | con
- {
- $$ = nullgen;
- $$.type = D_INDIR+D_NONE;
- $$.offset = $1;
- }
- | con '(' LRREG ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+$3;
- $$.offset = $1;
- }
- | con '(' LSP ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+REGSP;
- $$.offset = $1;
- }
- | con '(' LRREG '*' con ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+D_NONE;
- $$.offset = $1;
- $$.index = $3;
- $$.scale = $5;
- checkscale($$.scale);
- }
- | con '(' LRREG ')' '(' LRREG '*' con ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+$3;
- $$.offset = $1;
- $$.index = $6;
- $$.scale = $8;
- checkscale($$.scale);
- }
- | '(' LRREG ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+$2;
- }
- | '(' LSP ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+REGSP;
- }
- | '(' LRREG '*' con ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+D_NONE;
- $$.index = $2;
- $$.scale = $4;
- checkscale($$.scale);
- }
- | '(' LRREG ')' '(' LRREG '*' con ')'
- {
- $$ = nullgen;
- $$.type = D_INDIR+$2;
- $$.index = $5;
- $$.scale = $7;
- checkscale($$.scale);
- }
- | nam
- {
- $$ = $1;
- }
- | nam '(' LRREG '*' con ')'
- {
- $$ = $1;
- $$.index = $3;
- $$.scale = $5;
- checkscale($$.scale);
- }
- | con '(' LPC ')'
- {
- $$ = nullgen;
- $$.type = D_BRANCH;
- $$.offset = $1 + pc;
- }
- | LNAME offset
- {
- $$ = nullgen;
- if(pass == 2)
- yyerror("undefined label: %s", $1->name);
- $$.type = D_BRANCH;
- $$.sym = $1;
- $$.offset = $2;
- }
- | LLAB offset
- {
- $$ = nullgen;
- $$.type = D_BRANCH;
- $$.sym = $1;
- $$.offset = $1->value + $2;
- }
- | imm
- imm:
- '$' con
- {
- $$ = nullgen;
- $$.type = D_CONST;
- $$.offset = $2;
- }
- | '$' nam
- {
- $$ = $2;
- $$.index = $2.type;
- $$.type = D_ADDR;
- if($2.type == D_AUTO || $2.type == D_PARAM)
- yyerror("constant cannot be automatic: %s",
- $2.sym->name);
- }
- | '$' LSCONST
- {
- $$ = nullgen;
- $$.type = D_SCONST;
- memcpy($$.sval, $2, sizeof($$.sval));
- }
- | '$' LFCONST
- {
- $$ = nullgen;
- $$.type = D_FCONST;
- $$.dval = $2;
- }
- | '$' '-' LFCONST
- {
- $$ = nullgen;
- $$.type = D_FCONST;
- $$.dval = -$3;
- }
- reg:
- LRREG
- | LFREG
- nam:
- LNAME offset '(' pointer ')'
- {
- $$ = nullgen;
- $$.type = $4;
- $$.sym = $1;
- $$.offset = $2;
- }
- | LNAME '<' '>' offset '(' LSB ')'
- {
- $$ = nullgen;
- $$.type = D_STATIC;
- $$.sym = $1;
- $$.offset = $4;
- }
- offset:
- {
- $$ = 0;
- }
- | '+' con
- {
- $$ = $2;
- }
- | '-' con
- {
- $$ = -$2;
- }
- pointer:
- LSB
- | LSP
- | LFP
- con:
- LCONST
- | LVAR
- {
- $$ = $1->value;
- }
- | '-' con
- {
- $$ = -$2;
- }
- | '+' con
- {
- $$ = $2;
- }
- | '~' con
- {
- $$ = ~$2;
- }
- | '(' expr ')'
- {
- $$ = $2;
- }
- expr:
- con
- | expr '+' expr
- {
- $$ = $1 + $3;
- }
- | expr '-' expr
- {
- $$ = $1 - $3;
- }
- | expr '*' expr
- {
- $$ = $1 * $3;
- }
- | expr '/' expr
- {
- $$ = $1 / $3;
- }
- | expr '%' expr
- {
- $$ = $1 % $3;
- }
- | expr '<' '<' expr
- {
- $$ = $1 << $4;
- }
- | expr '>' '>' expr
- {
- $$ = $1 >> $4;
- }
- | expr '&' expr
- {
- $$ = $1 & $3;
- }
- | expr '^' expr
- {
- $$ = $1 ^ $3;
- }
- | expr '|' expr
- {
- $$ = $1 | $3;
- }
- comma:
- | ','
|