OpenCores
URL https://opencores.org/ocsvn/diogenes/diogenes/trunk

Subversion Repositories diogenes

[/] [diogenes/] [trunk/] [asm/] [yacc.in] - Blame information for rev 236

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 140 fellnhofer
 
2
 
3
%{
4
 
5
 
6
#include 
7
#include 
8
#include "tools.h"
9
 
10
#define YYERROR_VERBOSE
11
 
12
int temp;
13
int firstpass=1;
14
 
15
void yyerror(const char *str)
16
{
17
        fprintf(stderr,"error: %s\n",str);
18
}
19
 
20
int yywrap()
21
{
22
        return 1;
23
}
24
 
25
int main(int argc, char **argv)
26
{
27
        if(argc>=2) {
28
                if(!strcmp(argv[1], "-h")) hex = 1;
29
                if(!strcmp(argv[1], "-b")) hex = 2;
30
        }
31
        if(!hash_init()) {
32
                exit(1);
33
                printf("could not create hash table\n");
34
        }
35
 
36
        if(hex && argc==3) stdin = fopen(argv[2], "r");
37
        if(!hex && argc==2) stdin = fopen(argv[1], "r");
38
 
39
        firstpass = 1;
40
        address = 0;
41
    if(yyparse()) exit(1);
42
        firstpass = 0;
43
        address = 0;
44
        rewind(stdin);
45
        (void) PROC_FLUSH_BUF (stdin );
46
    yyparse();
47
    return 0;
48
}
49
 
50
%}
51
 
52
%token C OBRA CBRA OSBRA CSBRA NUM
53
 
54
%union
55
{
56
        int num;
57
        char *str;
58
}
59
 
60
%token  IMM LABEL USELAB
61
%token  REG
62
%token  ARI LDI LSI CMP BR SHA JUMP CALL MOV MEM SEL SELI LDL NOP
63
 
64
%type   asms asm nop ari ldsi cmp br sha jmpcall mov mem sel seli ldl
65
%type   imm6b imm8b imm8bs imm2b imm32
66
 
67
 
68
 
69
%%
70
asms: asm | asms asm
71
 
72
asm:    LABEL                   {if(firstpass)add_label($1, address);}
73
        |       nop                             {if(!firstpass)xprintf($1);address++;}
74
        |       ari                             {if(!firstpass)xprintf($1);address++;}
75
        |       ldsi                    {if(!firstpass)xprintf($1);address++;}
76
        |       cmp                     {if(!firstpass)xprintf($1);address++;}
77
        |       br                              {if(!firstpass)xprintf($1);address++;}
78
        |       sha                     {if(!firstpass)xprintf($1);address++;}
79
        |       jmpcall                 {if(!firstpass)xprintf($1);address++;}
80
        |       mov                     {if(!firstpass)xprintf($1);address++;}
81
        |       mem                     {if(!firstpass)xprintf($1);address++;}
82
        |       sel                     {if(!firstpass)xprintf($1);address++;}
83
        |       seli                    {if(!firstpass)xprintf($1);address++;}
84
        |       ldl                             {;}
85
 
86
//      |                                       { yyerror("unknown instruction"); YYERROR; }
87
 
88
 
89
nop:    NOP                                                             { $$ = $1; }
90
 
91
ari:    ARI REG C REG C REG                             { $$ = ($1<<12) | ($2<<8) | ($4<<4) | $6; }
92
 
93
ldsi:   LDI REG C imm8b                                 { $$ = (0x6000) | ($2<<8) | ($4&0xff); }
94
        |       LSI     REG C imm8b                                     { $$ = (0x7000) | ($2<<8) | ($4&0xff); }
95
 
96
cmp:    CMP REG C REG C REG                             { $$ = (0x8000) | ($1<<12) | ($2<<8) | ($4<<4) | $6;}
97
 
98
br:             BR      REG C USELAB                            { $$ = (0xc000) | ($1<<12) | (((get_label($4)-address)&0xff) << 4) | $2;
99
                                                                                //      if(!firstpass) printf(" @%d - %d: %d",  address, $4, get_label($4));
100
                                                                                }
101
        |       BR      REG C imm8bs                            { $$ = (0xc000) | ($1<<12) | (($4&0xff) << 4) | $2; }
102
 
103
sha:    SHA     REG C imm6b                                     { $$ = (0xe000) | ($2<<8) | ($1<<6) | ($4&0x3f); }
104
        |       SHA REG C REG C imm6b                   { if(($2^0x8) != $4) { yyerror("wrong sister register"); YYERROR; }
105
                                                                                  $$ = (0xe000) | ($2<<8) | (1<<7) | ($1<<6) | ($6&0x3f); }
106
 
107
jmpcall:JUMP OSBRA REG CSBRA                    { $$ = (0xf000) | ($3<<4) | $1; }
108
        |       CALL REG C OSBRA REG CSBRA      { $$ = (0xf000) | ($2<<8) | ($5<<4) | $1; }
109
 
110
mov:    MOV REG C REG                                   { $$ = (0xf000) | ($2<<8) | ($4<<4) | 1; }
111
 
112
mem:    MEM REG C OBRA REG CBRA                 { $$ = (0xf000) | ($2<<8) | ($5<<4) | $1; }
113
 
114
sel:    SEL REG C OBRA REG CBRA                 { $$ = (0xf000) | ($2<<8) | ($5<<4) | $1; }
115
        |       SEL REG C REG C OBRA REG CBRA   { if(($2^0x8) != $4) { yyerror("wrong sister register"); YYERROR; }
116
                                                                                  $$ = (0xf000) | ($2<<8) | ($7<<4) | (1<<3) | $1; }
117
 
118
seli:   SELI REG C imm2b                                { $$ = (0xf000) | ($2<<8) | ($4<<6) | $1; }
119
        |       SELI REG C REG C imm2b                  { if(($2^0x8) != $4) { yyerror("wrong sister register"); YYERROR; }
120
                                                                                  $$ = (0xf000) | ($2<<8) | ($6<<4) | (1<<3) | $1; }
121
 
122
ldl:    LDL     REG C imm32
123
                                { if(!firstpass)xprintf(0x6000|($2<<8)|(($4>>24)&0xff));address++;
124
                                  if(!firstpass)xprintf(0x7000|($2<<8)|(($4>>18)&0xff));address++;
125
                                  if(!firstpass)xprintf(0x7000|($2<<8)|(($4>>8)&0xff));address++;
126
                                  if(!firstpass)xprintf(0x7000|($2<<8)|(($4>>0)&0xff));address++;
127
                                }
128
        |       LDL REG C USELAB
129
                                { temp = get_abs_label($4);
130
                                  if(!firstpass)xprintf(0x6000|($2<<8)|((temp>>24)&0xff));address++;
131
                                  if(!firstpass)xprintf(0x7000|($2<<8)|((temp>>18)&0xff));address++;
132
                                  if(!firstpass)xprintf(0x7000|($2<<8)|((temp>>8)&0xff));address++;
133
                                  if(!firstpass)xprintf(0x7000|($2<<8)|((temp>>0)&0xff));address++;
134
                                }
135
 
136
 
137
imm2b:  IMM             {
138
                                        temp = check_imm($1, &$$, 0, 3);
139
                                        if(temp==1) { yyerror("2 Bit immediate expected"); YYERROR; }
140
                                        if(temp==2) { yyerror("Immediate syntax"); YYERROR; }
141
                                }
142
 
143
imm8b:  IMM             {
144
                                        temp = check_imm($1, &$$, 0, 255);
145
                                        if(temp==1) { yyerror("8 Bit unsigned immediate expected"); YYERROR; }
146
                                        if(temp==2) { yyerror("Immediate syntax"); YYERROR; }
147
                                }
148
imm8bs: IMM             {
149
                                        temp = check_imm($1, &$$, -128, 127);
150
                                        if(temp==1) { yyerror("8 bit signed immediate expected"); YYERROR; }
151
                                        if(temp==2) { yyerror("Immediate syntax"); YYERROR; }
152
                                }
153
//      |       LABEL   {       $$ = get_label($1) - address - 1; }
154
 
155
imm6b:  IMM             {
156
                                        temp = check_imm($1, &$$, -32, 31);
157
                                        if(temp==1) { printf("Immediate Value %d out of range: -32..31 allowed\n", $$); YYERROR; }
158
                                        if(temp==2) { yyerror("Immediate syntax"); YYERROR; }
159
                                }
160
 
161
imm32:  IMM             {
162
                                        temp = check_imm($1, &$$, 0x1000000, 0x7fffffff);
163
                                        if(temp==1) { printf("Immediate Value %d out of range: -32b..31b allowed\n", $$); YYERROR; }
164
                                        if(temp==2) { yyerror("Immediate syntax"); YYERROR; }
165
                                }
166
 
167
%%

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.