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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zparser.cpp] - Blame information for rev 89

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    zparser.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU core
6
//
7 13 dgisselq
// Purpose:     This file is really mis-named.  At one time it was going to
8
//              be the parser for the Zip Assembler, zasm.  Since then, I
9
//              discovered Flex and Bison and have written a parser using
10
//              those tools.  The true parser may therefore be found in zasm.y.
11
//              This file, however, still contains some very valuable tools.
12
//              In particular, all of the routines used to build instructions
13
//              from the appropriate fields are kept in this file.  For example,
14
//              op_noop() returns the instruction code for a NOOP  instruction.
15 2 dgisselq
//
16
// Creator:     Dan Gisselquist, Ph.D.
17 69 dgisselq
//              Gisselquist Technology, LLC
18 2 dgisselq
//
19
////////////////////////////////////////////////////////////////////////////////
20
//
21
// Copyright (C) 2015, Gisselquist Technology, LLC
22
//
23
// This program is free software (firmware): you can redistribute it and/or
24
// modify it under the terms of  the GNU General Public License as published
25
// by the Free Software Foundation, either version 3 of the License, or (at
26
// your option) any later version.
27
//
28
// This program is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
// for more details.
32
//
33
// You should have received a copy of the GNU General Public License along
34
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
35
// target there if the PDF file isn't present.)  If not, see
36
// <http://www.gnu.org/licenses/> for a copy.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
////////////////////////////////////////////////////////////////////////////////
43
 
44
#include <stdio.h>
45
#include <stdlib.h>
46
#include <string.h>
47
#include <ctype.h>
48
#include <strings.h>
49 69 dgisselq
#include <assert.h>
50 2 dgisselq
 
51
#include "zparser.h"
52
#include "zopcodes.h"
53
 
54
typedef ZPARSER::ZIPI ZIPI;     // A Zip Instruction (i.e. uint32)
55
 
56 69 dgisselq
#define IMMOP(OP,CND,IMM,A) (((OP&0x01f)<<22)|((A&0x0f)<<27)|((CND&0x07)<<19) \
57
                        | (IMM & 0x03ffff))
58 2 dgisselq
 
59 69 dgisselq
#define DBLREGOP(OP,CND,IMM,B,A) (((OP&0x01f)<<22)|((A&0x0f)<<27)       \
60
                        |((CND&0x07)<<19)|(1<<18)|((B&0x0f)<<14)         \
61
                        | (IMM & 0x03fff))
62 2 dgisselq
 
63
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
64 69 dgisselq
        return DBLREGOP(ZIPO_CMP, cnd, imm, b, a);
65 2 dgisselq
}
66
 
67
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
68 69 dgisselq
        return IMMOP(ZIPO_CMP, cnd, imm, a);
69 2 dgisselq
}
70
 
71
 
72
ZIPI    ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
73 69 dgisselq
        return DBLREGOP(ZIPO_TST, cnd, imm, b, a);
74 2 dgisselq
} ZIPI  ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
75 69 dgisselq
        return IMMOP(ZIPO_TST, cnd, imm, a);
76 2 dgisselq
}
77
 
78
ZIPI    ZPARSER::op_mov(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
79
        ZIPI    in;
80 69 dgisselq
 
81
        in = (ZIPO_MOV)<<22;
82
        in |= ((a  &0x0f)<<27);
83
        in |= ((cnd&0x07)<<19);
84
        in |= ((b  &0x0f)<<14);
85
        in |= ( imm&0x01fff);
86
 
87
 
88
        if (b & 0x10)
89
                in |= (1<<13);
90
        if (a & 0x10)
91
                in |= (1<<18);
92 2 dgisselq
        return in;
93
}
94
 
95
 
96
ZIPI    ZPARSER::op_ldi(ZIPIMM imm, ZIPREG a) const {
97
        ZIPI    in;
98 69 dgisselq
        in = ((a&0x0f)<<27) | (ZIPO_LDI << 22) | (imm & ((1<<23)-1));
99 2 dgisselq
        return in;
100
}
101
 
102
ZIPI    ZPARSER::op_trap(ZIPCOND cnd, ZIPIMM imm) const {
103
        ZIPI    in;
104 13 dgisselq
        if (cnd != ZIPC_ALWAYS)
105
                return op_ldilo(cnd, imm, ZIP_CC);
106
        else
107
                return op_ldi(imm, ZIP_CC);
108
        // in  = ((0x4f)<<24)|((cnd&0x07)<<21)|(1<<20)|((0x0e)<<16);
109
        // in |= (imm & 0x0ffff);
110 2 dgisselq
        return in;
111
}
112
 
113
ZIPI    ZPARSER::op_noop(void) const {
114 69 dgisselq
        return 0x76000000;
115
} ZIPI  ZPARSER::op_break(void) const {
116
        return 0x76400000;
117
} ZIPI  ZPARSER::op_lock(void) const {
118
        return 0x76800000;
119 2 dgisselq
}
120
 
121
ZIPI    ZPARSER::op_ldihi(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
122 69 dgisselq
        ZIPI    in = IMMOP(ZIPO_LDIHI, cnd, (imm & 0x0ffff), a);
123 2 dgisselq
        return in;
124 69 dgisselq
} ZIPI  ZPARSER::op_ldilo(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
125
        ZIPI    in = IMMOP(ZIPO_LDILO, cnd, (imm & 0x0ffff), a);
126 2 dgisselq
        return in;
127
}
128
 
129 26 dgisselq
ZIPI    ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
130 69 dgisselq
        return DBLREGOP(ZIPO_MPYU, cnd, imm, b, a);
131 26 dgisselq
} ZIPI  ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
132 69 dgisselq
        return IMMOP(ZIPO_MPYU, cnd, imm & 0x0ffff, a);
133 8 dgisselq
}
134
 
135 26 dgisselq
ZIPI    ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
136 69 dgisselq
        return DBLREGOP(ZIPO_MPYS, cnd, imm, b, a);
137 26 dgisselq
} ZIPI  ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
138 69 dgisselq
        return IMMOP(ZIPO_MPYS, cnd, imm & 0x0ffff, a);
139 26 dgisselq
}
140
 
141 8 dgisselq
ZIPI    ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
142 69 dgisselq
        return DBLREGOP(ZIPO_ROL, cnd, imm, b, a);
143 8 dgisselq
} ZIPI  ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
144 69 dgisselq
        return IMMOP(ZIPO_ROL, cnd, imm, a);
145 8 dgisselq
}
146
 
147 69 dgisselq
ZIPI    ZPARSER::op_popc(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
148
        return DBLREGOP(ZIPO_POPC, cnd, imm, b, a);
149
} ZIPI  ZPARSER::op_popc(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
150
        return IMMOP(ZIPO_POPC, cnd, imm, a);
151
}
152
 
153
ZIPI    ZPARSER::op_brev(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
154
        return DBLREGOP(ZIPO_BREV, cnd, imm, b, a);
155
} ZIPI  ZPARSER::op_brev(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
156
        return IMMOP(ZIPO_BREV, cnd, imm, a);
157
}
158
 
159 2 dgisselq
ZIPI    ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
160 69 dgisselq
        return DBLREGOP(ZIPO_LOD, cnd, imm, b, a);
161 8 dgisselq
} ZIPI  ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
162 69 dgisselq
        return IMMOP(ZIPO_LOD, cnd, imm, a);
163 2 dgisselq
}
164
 
165
 
166
ZIPI    ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm, ZIPREG b) const {
167 69 dgisselq
        return DBLREGOP(ZIPO_STO, cnd, imm, b, v);
168 2 dgisselq
} ZIPI  ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm) const {
169 69 dgisselq
        return IMMOP(ZIPO_STO, cnd, imm, v);
170 2 dgisselq
}
171
 
172
 
173
ZIPI    ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
174 69 dgisselq
        return DBLREGOP(ZIPO_SUB, cnd, imm, b, a);
175 2 dgisselq
} ZIPI  ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
176 26 dgisselq
        // While it seems like we might do well replacing a subtract immediate
177
        // with an add of the negative same, the conditions aren't the same
178
        // when doing so.  Hence this is an invalid substitution.
179
        // return IMMOP(0xa, cnd, -imm, a); // Do an add of the negative of imm
180 69 dgisselq
        return IMMOP(ZIPO_SUB, cnd, imm, a);
181 2 dgisselq
}
182
 
183
 
184
ZIPI    ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
185 69 dgisselq
        return DBLREGOP(ZIPO_AND, cnd, imm, b, a);
186 2 dgisselq
} ZIPI  ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
187 69 dgisselq
        return IMMOP(ZIPO_AND, cnd, imm, a);
188 2 dgisselq
}
189
 
190
 
191
ZIPI    ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
192 69 dgisselq
        return DBLREGOP(ZIPO_ADD, cnd, imm, b, a);
193 2 dgisselq
} ZIPI  ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
194 69 dgisselq
        return IMMOP(ZIPO_ADD, cnd, imm, a);
195 2 dgisselq
}
196
 
197
 
198
ZIPI    ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
199 69 dgisselq
        return DBLREGOP(ZIPO_OR, cnd, imm, b, a);
200 2 dgisselq
} ZIPI  ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
201 69 dgisselq
        return IMMOP(ZIPO_OR, cnd, imm, a);
202 2 dgisselq
}
203
 
204
ZIPI    ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
205 69 dgisselq
        return DBLREGOP(ZIPO_XOR, cnd, imm, b, a);
206 2 dgisselq
} ZIPI  ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
207 69 dgisselq
        return IMMOP(ZIPO_XOR, cnd, imm, a);
208 2 dgisselq
}
209
 
210
ZIPI    ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
211 69 dgisselq
        return DBLREGOP(ZIPO_LSL, cnd, imm, b, a);
212 2 dgisselq
} ZIPI  ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
213 69 dgisselq
        return IMMOP(ZIPO_LSL, cnd, imm, a);
214 2 dgisselq
}
215
 
216
ZIPI    ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
217 69 dgisselq
        return DBLREGOP(ZIPO_ASR, cnd, imm, b, a);
218 2 dgisselq
} ZIPI  ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
219 69 dgisselq
        return IMMOP(ZIPO_ASR, cnd, imm, a);
220 2 dgisselq
}
221
 
222
ZIPI    ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
223 69 dgisselq
        return DBLREGOP(ZIPO_LSR, cnd, imm, b, a);
224 2 dgisselq
} ZIPI  ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
225 69 dgisselq
        return IMMOP(ZIPO_LSR, cnd, imm, a);
226 2 dgisselq
}
227
 
228 69 dgisselq
ZIPI    ZPARSER::op_divu(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
229
        return DBLREGOP(ZIPO_DIVU, cnd, imm, b, a);
230
} ZIPI  ZPARSER::op_divu(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
231
        return IMMOP(ZIPO_DIVU, cnd, imm, a);
232
}
233
 
234
ZIPI    ZPARSER::op_divs(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
235
        return DBLREGOP(ZIPO_DIVS, cnd, imm, b, a);
236
} ZIPI  ZPARSER::op_divs(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
237
        return IMMOP(ZIPO_DIVS, cnd, imm, a);
238
}
239
 
240
ZPARSER::ZIPIMM ZPARSER::immediate(const ZIPI a) {
241 89 dgisselq
        ZIPOP   op((ZIPOP)((a>>22)&0x01f));
242 69 dgisselq
        ZIPIMM  imm;
243
 
244
        switch(op) {
245
                case ZIPO_MOV:
246
                        imm = (a & 0x0fff); if (a&0x1fff) imm |= -0x1000; break;
247
                case ZIPO_LDI:
248
                        imm = (a & 0x03fffff); break;
249
                case ZIPO_LDIn:
250
                        imm = (a & 0x03fffff); imm |= -0x200000; break;
251
                case ZIPO_LDILO: case ZIPO_LDIHI:
252
                        imm = (a & 0x0ffff);   break;
253
                default:
254
                        if (a & 0x040000) {
255
                                imm = (a&0x3fff);
256
                                if (a&0x2000) imm |= -0x02000;
257
                        } else {
258
                                imm = (a&0x3ffff);
259
                                if (a&0x20000)
260
                                        imm |= -0x20000;
261
                        }
262
        }
263
 
264
        return imm;
265
}
266
 
267
bool    ZPARSER::can_merge(const ZIPI a, const ZIPI b) {
268
        // 1. Can't merge anything that's already merged
269
        if ((a|b) & 0x80000000)
270
                return false;
271
 
272 89 dgisselq
        ZIPOP   opa((ZIPOP)((a>>22)&0x01f)), opb((ZIPOP)((b>>22)&0x01f));
273 69 dgisselq
        // 2. Conditions
274
        {
275
                ZIPCOND ca((ZIPCOND)((a>>19)&0x07)),cb((ZIPCOND)((b>>19)&0x07));
276
 
277
                if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
278
                        ca = ZIPC_ALWAYS;
279
                if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
280
                        cb = ZIPC_ALWAYS;
281
 
282
                if ((ca == ZIPC_ALWAYS)&&(cb != ZIPC_ALWAYS))
283
                        return false;
284
                if ((ca|cb) &0x04)
285
                        return false;
286
                if ((ca != ZIPC_ALWAYS)&&((cb != ca)&&(cb != ZIPC_ALWAYS)))
287
                        return false;
288
                // if ((ca != ZIPC_ALWAYS)||(cb != ZIPC_ALWAYS))
289
                        // return false;
290
        }
291
 
292
        // 3. Moves ... only move if the move doesn't address user registers
293
 
294
        if ((opa == ZIPO_MOV)&&(a & ((1<<18)|(1<<13))))
295
                return false;
296
        if ((opb == ZIPO_MOV)&&(b & ((1<<18)|(1<<13))))
297
                return false;
298
 
299
        // 4. Immediates.  If Register + Immediate, the answer is No.
300
        ZIPIMM imma, immb;
301
        switch(opa) {
302
                case ZIPO_MOV:
303 89 dgisselq
                        imma = (a & 0x03fff); if (a) return false; break;
304 69 dgisselq
                case ZIPO_LDI: case ZIPO_LDIn:
305
                case ZIPO_LDILO: case ZIPO_LDIHI:
306
                        imma = immediate(a);   break;
307
                default:
308
                        if (a & 0x040000) {
309
                                imma = (a&0x3ffff);
310
                                // if (a&0x20000) a |= -0x20000;
311
                                if (imma != 0)
312
                                        return false;
313
                        } else {
314
                                imma = (a&0x3fff);
315
                                if (a&0x2000) // Sign extension?
316
                                        imma |= -0x02000;
317
                        }
318
        } switch(opb) {
319
                case ZIPO_MOV:
320
                        immb = (b & 0x0fff); if (b) return false; break;
321
                case ZIPO_LDI: case ZIPO_LDIn:
322
                case ZIPO_LDILO: case ZIPO_LDIHI:
323
                        immb = immediate(b);   break;
324
                default:
325
                        if (b & 0x040000) {
326
                                immb = (b&0x3fff);
327
                                // if (b&0x2000) b |= -0x02000;
328
                                if (immb != 0)
329
                                        return false;
330
                        } else {
331
                                immb = (b&0x3ffff);
332
                                if (b&0x20000)
333
                                        immb |= -0x20000;
334
                        }
335
        }
336
 
337
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn)||(opa == ZIPO_LDILO)||(opa == ZIPO_LDIHI)) {
338
                if ((imma > 15)||(imma < -16))
339
                        return false;
340
        } else if ((imma > 7)||(imma < -8))
341
                        return false;
342
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn)||(opb == ZIPO_LDILO)||(opb == ZIPO_LDIHI)) {
343
                if ((immb > 15)||(immb < -16))
344
                        return false;
345
        } else if ((immb > 7)||(immb < -8))
346
                        return false;
347
 
348
        return true;
349
}
350
 
351
ZIPI    ZPARSER::merge(const ZIPI a, const ZIPI b) {
352
        assert(can_merge(a, b));
353
        ZIPI    ni;
354
 
355
        ZIPCOND ca( (ZIPCOND)((a>>19)&0x007)), cb( (ZIPCOND)((b>>19)&0x007));
356
        ZIPOP   opa((ZIPOP)((a>>25)&0x012)), opb((ZIPOP)((b>>22)&0x01f));
357
 
358
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
359
                ca = ZIPC_ALWAYS;
360
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
361
                cb = ZIPC_ALWAYS;
362
 
363
        ZIPIMM imma, immb;
364
        imma = immediate(a);
365
        immb = immediate(b);
366
 
367
        ni = (opa << 26)|(opb<<9)|0x80000000;
368
        if (ca != ZIPC_ALWAYS) {
369
                ni |= (ca << 19);
370
                if (cb == ca)
371
                        ni |= (1<<21);
372
        }
373
 
374
        // The result register(s)
375
        ni |= (a & 0x78000000);
376
        ni |= ((b>>27)&0x0f)<<5;
377
 
378
        // Are we using the register form of opB?
379
        switch(opa) {
380
                case ZIPO_MOV: ni |= (a&0x078000); break; // Always a register
381
                case ZIPO_LDI: case ZIPO_LDIn:
382
                case ZIPO_LDILO: case ZIPO_LDIHI:
383
                        ni |= (imma & 0x01f)<<14;
384
                        break;
385
                default:
386
                        if (a & 0x040000) {
387
                                ni |= (a&0x078000);
388
                        } else
389
                                ni |= (imma & 0x0f)<<14;
390
        }
391
 
392
        switch(opb) {
393
                case ZIPO_MOV:
394
                        ni |= ((b>>14)&0x0f)|0x10; break;
395
                case ZIPO_LDI: case ZIPO_LDIn:
396
                case ZIPO_LDILO: case ZIPO_LDIHI:
397
                        ni |= (immb & 0x01f);
398
                        break;
399
                default:
400
                        if (b & 0x040000) {
401
                                ni |= ((b>>14)&0x0f)|0x10;
402
                        } else
403
                                ni |= (immb & 0x0f);
404
        }
405
 
406
        return ni;
407
}

powered by: WebSVN 2.1.0

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