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

Subversion Repositories marca

[/] [marca/] [trunk/] [spar/] [exprs.y] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jeunes2
/* This file is part of the assembler "spar" for marca.
2
   Copyright (C) 2007 Wolfgang Puffitsch
3
 
4
   This program is free software; you can redistribute it and/or modify it
5
   under the terms of the GNU Library General Public License as published
6
   by the Free Software Foundation; either version 2, or (at your option)
7
   any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Library General Public License for more details.
13
 
14
   You should have received a copy of the GNU Library General Public
15
   License along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
17
 
18
%{
19
 
20
#include 
21
#include 
22
 
23
#include "exprs.h"
24
 
25
int exprslex(void);
26
void yyerror(int64_t *, const char *);
27
 
28
%}
29
 
30
%union {
31
  int64_t intval;
32
}
33
 
34
%parse-param {
35
  int64_t *retval, int64_t *symcount
36
}
37
 
38
%token  NUM IDENT
39
 
40
%token CUMOD CEQU CUGT CSHR CUMIN CLE CNEQ CMIN CLAND
41
%token CGE CUDIV CUGE CULE CLOR CULT CSHL CMAX CUMAX CSLR
42
%token HI LO
43
 
44
%type  PrimaryExpr UnaryExpr MultExpr AddExpr ShiftExpr
45
%type  RelatExpr EqualExpr AndExpr XorExpr OrExpr
46
%type  LAndExpr LOrExpr CondExpr MinMaxExpr HiLoExpr Expr
47
 
48
%start Expr
49
 
50
%%
51
 
52
PrimaryExpr : NUM
53
            { $$ = $1;
54
            }
55
            | IDENT
56
            { (*symcount)++;
57
              $$ = $1;
58
            }
59
            | '(' Expr ')'
60
            { $$ = $2;
61
            }
62
;
63
 
64
UnaryExpr : PrimaryExpr
65
          | '+' UnaryExpr
66
          { $$ = $2;
67
          }
68
          | '-' UnaryExpr
69
          { $$ = -$2;
70
          }
71
          | '~' UnaryExpr
72
          { $$ = ~$2;
73
          }
74
          | '!' UnaryExpr
75
          { $$ = !$2;
76
          }
77
;
78
 
79
MultExpr : UnaryExpr
80
         | MultExpr '*' UnaryExpr
81
         { $$ = $1 * $3;
82
         }
83
         | MultExpr '/' UnaryExpr
84
         {
85
           if ($3 == 0)
86
             {
87
               fprintf(stderr, "division by zero");
88
               $$ = 0;
89
             }
90
           else
91
             {
92
               $$ = $1 / $3;
93
             }
94
         }
95
         | MultExpr CUDIV UnaryExpr
96
         {
97
           if ($3 == 0)
98
             {
99
               fprintf(stderr, "division by zero");
100
               $$ = 0;
101
             }
102
           else
103
             {
104
               $$ = (uint64_t)$1 / (uint64_t)$3;
105
             }
106
         }
107
         | MultExpr '%' UnaryExpr
108
         {
109
           if ($3 == 0)
110
             {
111
               fprintf(stderr, "modulo zero");
112
               $$ = 0;
113
             }
114
           else
115
             {
116
               $$ = $1 % $3;
117
             }
118
         }
119
         | MultExpr CUMOD UnaryExpr
120
         {
121
           if ($3 == 0)
122
             {
123
               fprintf(stderr, "modulo zero");
124
               $$ = 0;
125
             }
126
           else
127
             {
128
               $$ = (uint64_t)$1 % (uint64_t)$3;
129
             }
130
         }
131
;
132
 
133
AddExpr : MultExpr
134
        | AddExpr '+' MultExpr
135
        { $$ = $1 + $3;
136
        }
137
        | AddExpr '-' MultExpr
138
        { $$ = $1 - $3;
139
        }
140
;
141
 
142
ShiftExpr : AddExpr
143
          | ShiftExpr CSHR AddExpr
144
          { $$ = $1 >> $3;
145
          }
146
          | ShiftExpr CSHL AddExpr
147
          { $$ = $1 << $3;
148
          }
149
          | ShiftExpr CSLR AddExpr
150
          { $$ = (uint64_t)$1 >> $3;
151
          }
152
;
153
 
154
RelatExpr : ShiftExpr
155
          | RelatExpr '<' ShiftExpr
156
          { $$ = $1 < $3;
157
          }
158
          | RelatExpr '>' ShiftExpr
159
          { $$ = $1 > $3;
160
          }
161
          | RelatExpr CLE ShiftExpr
162
          { $$ = $1 <= $3;
163
          }
164
          | RelatExpr CGE ShiftExpr
165
          { $$ = $1 >= $3;
166
          }
167
          | RelatExpr CULT ShiftExpr
168
          { $$ = (uint64_t)$1 < (uint64_t)$3;
169
          }
170
          | RelatExpr CUGT ShiftExpr
171
          { $$ = (uint64_t)$1 > (uint64_t)$3;
172
          }
173
          | RelatExpr CULE ShiftExpr
174
          { $$ = (uint64_t)$1 <= (uint64_t)$3;
175
          }
176
          | RelatExpr CUGE ShiftExpr
177
          { $$ = (uint64_t)$1 >= (uint64_t)$3;
178
          }
179
;
180
 
181
EqualExpr : RelatExpr
182
          | EqualExpr CEQU RelatExpr
183
          { $$ = $1 == $3;
184
          }
185
          | EqualExpr CNEQ RelatExpr
186
          { $$ = $1 != $3;
187
          }
188
;
189
 
190
AndExpr : EqualExpr
191
        | AndExpr '&' EqualExpr
192
        { $$ = $1 & $3;
193
        }
194
;
195
 
196
XorExpr : AndExpr
197
        | XorExpr '^' AndExpr
198
        { $$ = $1 ^ $3;
199
        }
200
;
201
 
202
OrExpr : XorExpr
203
       | OrExpr '|' XorExpr
204
       { $$ = $1 | $3;
205
       }
206
;
207
 
208
LAndExpr : OrExpr
209
         | LAndExpr CLAND OrExpr
210
         { $$ = $1 && $3;
211
         }
212
;
213
 
214
LOrExpr : LAndExpr
215
        | LOrExpr CLOR LAndExpr
216
        { $$ = $1 || $3;
217
        }
218
;
219
 
220
CondExpr : LOrExpr
221
         | LOrExpr '?' Expr ':' CondExpr
222
         { $$ = $1 ? $3 : $5;
223
         }
224
;
225
 
226
MinMaxExpr : CondExpr
227
           | MinMaxExpr CMIN CondExpr
228
           { $$ = $1 < $3 ? $1 : $3;
229
           }
230
           | MinMaxExpr CMAX CondExpr
231
           { $$ = $1 > $3 ? $1 : $3;
232
           }
233
           | MinMaxExpr CUMIN CondExpr
234
           { $$ = (uint64_t)$1 < (uint64_t)$3 ? $1 : $3;
235
           }
236
           | MinMaxExpr CUMAX CondExpr
237
           { $$ = (uint64_t)$1 > (uint64_t)$3 ? $1 : $3;
238
           }
239
;
240
 
241
HiLoExpr : MinMaxExpr
242
         | HI '(' MinMaxExpr ')'
243
         { $$ = ($3 >> 8) & 0xFF;
244
         }
245
         | LO '(' MinMaxExpr ')'
246
         { $$ = $3 & 0xFF;
247
         }
248
 
249
Expr: HiLoExpr
250
    { *retval = $1;
251
    }
252
;
253
 
254
%%
255
 
256
void yyerror(int64_t *r, const char *msg)
257
{
258
  fprintf(stderr, "within expression: %s\n", msg);
259
}
260
 

powered by: WebSVN 2.1.0

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