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

Subversion Repositories zipcpu

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

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 126 dgisselq
#define LONG_MPY
64
 
65
ZPARSER::ZIPIMM ZPARSER::brev(ZIPIMM v) const {
66
        unsigned r=0, b;
67
 
68
        for(b=0; b<32; b++, v>>=1)
69
                r = (r<<1)|(v&1);
70
 
71
        return r;
72
}
73
 
74 2 dgisselq
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
75 69 dgisselq
        return DBLREGOP(ZIPO_CMP, cnd, imm, b, a);
76 2 dgisselq
}
77
 
78
ZIPI    ZPARSER::op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
79 69 dgisselq
        return IMMOP(ZIPO_CMP, cnd, imm, a);
80 2 dgisselq
}
81
 
82
 
83
ZIPI    ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
84 69 dgisselq
        return DBLREGOP(ZIPO_TST, cnd, imm, b, a);
85 2 dgisselq
} ZIPI  ZPARSER::op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
86 69 dgisselq
        return IMMOP(ZIPO_TST, cnd, imm, a);
87 2 dgisselq
}
88
 
89
ZIPI    ZPARSER::op_mov(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
90
        ZIPI    in;
91 69 dgisselq
 
92
        in = (ZIPO_MOV)<<22;
93
        in |= ((a  &0x0f)<<27);
94
        in |= ((cnd&0x07)<<19);
95
        in |= ((b  &0x0f)<<14);
96
        in |= ( imm&0x01fff);
97
 
98
 
99
        if (b & 0x10)
100
                in |= (1<<13);
101
        if (a & 0x10)
102
                in |= (1<<18);
103 2 dgisselq
        return in;
104
}
105
 
106
 
107
ZIPI    ZPARSER::op_ldi(ZIPIMM imm, ZIPREG a) const {
108
        ZIPI    in;
109 69 dgisselq
        in = ((a&0x0f)<<27) | (ZIPO_LDI << 22) | (imm & ((1<<23)-1));
110 2 dgisselq
        return in;
111
}
112
 
113
ZIPI    ZPARSER::op_trap(ZIPCOND cnd, ZIPIMM imm) const {
114
        ZIPI    in;
115 13 dgisselq
        if (cnd != ZIPC_ALWAYS)
116
                return op_ldilo(cnd, imm, ZIP_CC);
117
        else
118
                return op_ldi(imm, ZIP_CC);
119
        // in  = ((0x4f)<<24)|((cnd&0x07)<<21)|(1<<20)|((0x0e)<<16);
120
        // in |= (imm & 0x0ffff);
121 2 dgisselq
        return in;
122
}
123
 
124
ZIPI    ZPARSER::op_noop(void) const {
125 69 dgisselq
        return 0x76000000;
126
} ZIPI  ZPARSER::op_break(void) const {
127
        return 0x76400000;
128
} ZIPI  ZPARSER::op_lock(void) const {
129
        return 0x76800000;
130 2 dgisselq
}
131
 
132 126 dgisselq
#ifdef  LONG_MPY
133
ZIPI    ZPARSER::op_mpy(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
134
        return DBLREGOP(ZIPO_MPY, cnd, imm, b, a);
135
} ZIPI  ZPARSER::op_mpy(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
136
        return IMMOP(ZIPO_MPY, cnd, imm, a);
137
} ZIPI  ZPARSER::op_ldihi(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
138
        ZIPI    in = IMMOP(ZIPO_BREV, cnd, brev(imm)&0x0ffff, a);
139
        return in;
140
}
141
#else
142 2 dgisselq
ZIPI    ZPARSER::op_ldihi(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
143 69 dgisselq
        ZIPI    in = IMMOP(ZIPO_LDIHI, cnd, (imm & 0x0ffff), a);
144 2 dgisselq
        return in;
145 126 dgisselq
}
146
#endif
147
ZIPI    ZPARSER::op_ldilo(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
148 69 dgisselq
        ZIPI    in = IMMOP(ZIPO_LDILO, cnd, (imm & 0x0ffff), a);
149 2 dgisselq
        return in;
150
}
151
 
152 26 dgisselq
ZIPI    ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
153 69 dgisselq
        return DBLREGOP(ZIPO_MPYU, cnd, imm, b, a);
154 26 dgisselq
} ZIPI  ZPARSER::op_mpyu(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
155 69 dgisselq
        return IMMOP(ZIPO_MPYU, cnd, imm & 0x0ffff, a);
156 8 dgisselq
}
157
 
158 26 dgisselq
ZIPI    ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
159 69 dgisselq
        return DBLREGOP(ZIPO_MPYS, cnd, imm, b, a);
160 26 dgisselq
} ZIPI  ZPARSER::op_mpys(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
161 69 dgisselq
        return IMMOP(ZIPO_MPYS, cnd, imm & 0x0ffff, a);
162 26 dgisselq
}
163
 
164 8 dgisselq
ZIPI    ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
165 69 dgisselq
        return DBLREGOP(ZIPO_ROL, cnd, imm, b, a);
166 8 dgisselq
} ZIPI  ZPARSER::op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
167 69 dgisselq
        return IMMOP(ZIPO_ROL, cnd, imm, a);
168 8 dgisselq
}
169
 
170 69 dgisselq
ZIPI    ZPARSER::op_popc(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
171
        return DBLREGOP(ZIPO_POPC, cnd, imm, b, a);
172
} ZIPI  ZPARSER::op_popc(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
173
        return IMMOP(ZIPO_POPC, cnd, imm, a);
174
}
175
 
176
ZIPI    ZPARSER::op_brev(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
177
        return DBLREGOP(ZIPO_BREV, cnd, imm, b, a);
178
} ZIPI  ZPARSER::op_brev(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
179
        return IMMOP(ZIPO_BREV, cnd, imm, a);
180
}
181
 
182 2 dgisselq
ZIPI    ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
183 69 dgisselq
        return DBLREGOP(ZIPO_LOD, cnd, imm, b, a);
184 8 dgisselq
} ZIPI  ZPARSER::op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
185 69 dgisselq
        return IMMOP(ZIPO_LOD, cnd, imm, a);
186 2 dgisselq
}
187
 
188
 
189
ZIPI    ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm, ZIPREG b) const {
190 69 dgisselq
        return DBLREGOP(ZIPO_STO, cnd, imm, b, v);
191 2 dgisselq
} ZIPI  ZPARSER::op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm) const {
192 69 dgisselq
        return IMMOP(ZIPO_STO, cnd, imm, v);
193 2 dgisselq
}
194
 
195
 
196
ZIPI    ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
197 69 dgisselq
        return DBLREGOP(ZIPO_SUB, cnd, imm, b, a);
198 2 dgisselq
} ZIPI  ZPARSER::op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
199 26 dgisselq
        // While it seems like we might do well replacing a subtract immediate
200
        // with an add of the negative same, the conditions aren't the same
201
        // when doing so.  Hence this is an invalid substitution.
202
        // return IMMOP(0xa, cnd, -imm, a); // Do an add of the negative of imm
203 69 dgisselq
        return IMMOP(ZIPO_SUB, cnd, imm, a);
204 2 dgisselq
}
205
 
206
 
207
ZIPI    ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
208 69 dgisselq
        return DBLREGOP(ZIPO_AND, cnd, imm, b, a);
209 2 dgisselq
} ZIPI  ZPARSER::op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
210 69 dgisselq
        return IMMOP(ZIPO_AND, cnd, imm, a);
211 2 dgisselq
}
212
 
213
 
214
ZIPI    ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
215 69 dgisselq
        return DBLREGOP(ZIPO_ADD, cnd, imm, b, a);
216 2 dgisselq
} ZIPI  ZPARSER::op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
217 69 dgisselq
        return IMMOP(ZIPO_ADD, cnd, imm, a);
218 2 dgisselq
}
219
 
220
 
221
ZIPI    ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
222 69 dgisselq
        return DBLREGOP(ZIPO_OR, cnd, imm, b, a);
223 2 dgisselq
} ZIPI  ZPARSER::op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
224 69 dgisselq
        return IMMOP(ZIPO_OR, cnd, imm, a);
225 2 dgisselq
}
226
 
227
ZIPI    ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
228 69 dgisselq
        return DBLREGOP(ZIPO_XOR, cnd, imm, b, a);
229 2 dgisselq
} ZIPI  ZPARSER::op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
230 69 dgisselq
        return IMMOP(ZIPO_XOR, cnd, imm, a);
231 2 dgisselq
}
232
 
233
ZIPI    ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
234 69 dgisselq
        return DBLREGOP(ZIPO_LSL, cnd, imm, b, a);
235 2 dgisselq
} ZIPI  ZPARSER::op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
236 69 dgisselq
        return IMMOP(ZIPO_LSL, cnd, imm, a);
237 2 dgisselq
}
238
 
239
ZIPI    ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
240 69 dgisselq
        return DBLREGOP(ZIPO_ASR, cnd, imm, b, a);
241 2 dgisselq
} ZIPI  ZPARSER::op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
242 69 dgisselq
        return IMMOP(ZIPO_ASR, cnd, imm, a);
243 2 dgisselq
}
244
 
245
ZIPI    ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
246 69 dgisselq
        return DBLREGOP(ZIPO_LSR, cnd, imm, b, a);
247 2 dgisselq
} ZIPI  ZPARSER::op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
248 69 dgisselq
        return IMMOP(ZIPO_LSR, cnd, imm, a);
249 2 dgisselq
}
250
 
251 69 dgisselq
ZIPI    ZPARSER::op_divu(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
252
        return DBLREGOP(ZIPO_DIVU, cnd, imm, b, a);
253
} ZIPI  ZPARSER::op_divu(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
254
        return IMMOP(ZIPO_DIVU, cnd, imm, a);
255
}
256
 
257
ZIPI    ZPARSER::op_divs(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const {
258
        return DBLREGOP(ZIPO_DIVS, cnd, imm, b, a);
259
} ZIPI  ZPARSER::op_divs(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const {
260
        return IMMOP(ZIPO_DIVS, cnd, imm, a);
261
}
262
 
263
ZPARSER::ZIPIMM ZPARSER::immediate(const ZIPI a) {
264 89 dgisselq
        ZIPOP   op((ZIPOP)((a>>22)&0x01f));
265 69 dgisselq
        ZIPIMM  imm;
266
 
267
        switch(op) {
268
                case ZIPO_MOV:
269
                        imm = (a & 0x0fff); if (a&0x1fff) imm |= -0x1000; break;
270
                case ZIPO_LDI:
271
                        imm = (a & 0x03fffff); break;
272
                case ZIPO_LDIn:
273
                        imm = (a & 0x03fffff); imm |= -0x200000; break;
274
                case ZIPO_LDILO: case ZIPO_LDIHI:
275
                        imm = (a & 0x0ffff);   break;
276
                default:
277
                        if (a & 0x040000) {
278
                                imm = (a&0x3fff);
279
                                if (a&0x2000) imm |= -0x02000;
280
                        } else {
281
                                imm = (a&0x3ffff);
282
                                if (a&0x20000)
283
                                        imm |= -0x20000;
284
                        }
285
        }
286
 
287
        return imm;
288
}
289
 
290
bool    ZPARSER::can_merge(const ZIPI a, const ZIPI b) {
291
        // 1. Can't merge anything that's already merged
292
        if ((a|b) & 0x80000000)
293
                return false;
294
 
295 89 dgisselq
        ZIPOP   opa((ZIPOP)((a>>22)&0x01f)), opb((ZIPOP)((b>>22)&0x01f));
296 69 dgisselq
        // 2. Conditions
297
        {
298
                ZIPCOND ca((ZIPCOND)((a>>19)&0x07)),cb((ZIPCOND)((b>>19)&0x07));
299
 
300
                if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
301
                        ca = ZIPC_ALWAYS;
302
                if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
303
                        cb = ZIPC_ALWAYS;
304
 
305
                if ((ca == ZIPC_ALWAYS)&&(cb != ZIPC_ALWAYS))
306
                        return false;
307
                if ((ca|cb) &0x04)
308
                        return false;
309
                if ((ca != ZIPC_ALWAYS)&&((cb != ca)&&(cb != ZIPC_ALWAYS)))
310
                        return false;
311
                // if ((ca != ZIPC_ALWAYS)||(cb != ZIPC_ALWAYS))
312
                        // return false;
313
        }
314
 
315
        // 3. Moves ... only move if the move doesn't address user registers
316
 
317
        if ((opa == ZIPO_MOV)&&(a & ((1<<18)|(1<<13))))
318
                return false;
319
        if ((opb == ZIPO_MOV)&&(b & ((1<<18)|(1<<13))))
320
                return false;
321
 
322
        // 4. Immediates.  If Register + Immediate, the answer is No.
323
        ZIPIMM imma, immb;
324
        switch(opa) {
325
                case ZIPO_MOV:
326 89 dgisselq
                        imma = (a & 0x03fff); if (a) return false; break;
327 69 dgisselq
                case ZIPO_LDI: case ZIPO_LDIn:
328
                case ZIPO_LDILO: case ZIPO_LDIHI:
329
                        imma = immediate(a);   break;
330
                default:
331
                        if (a & 0x040000) {
332
                                imma = (a&0x3ffff);
333
                                // if (a&0x20000) a |= -0x20000;
334
                                if (imma != 0)
335
                                        return false;
336
                        } else {
337
                                imma = (a&0x3fff);
338
                                if (a&0x2000) // Sign extension?
339
                                        imma |= -0x02000;
340
                        }
341
        } switch(opb) {
342
                case ZIPO_MOV:
343
                        immb = (b & 0x0fff); if (b) return false; break;
344
                case ZIPO_LDI: case ZIPO_LDIn:
345
                case ZIPO_LDILO: case ZIPO_LDIHI:
346
                        immb = immediate(b);   break;
347
                default:
348
                        if (b & 0x040000) {
349
                                immb = (b&0x3fff);
350
                                // if (b&0x2000) b |= -0x02000;
351
                                if (immb != 0)
352
                                        return false;
353
                        } else {
354
                                immb = (b&0x3ffff);
355
                                if (b&0x20000)
356
                                        immb |= -0x20000;
357
                        }
358
        }
359
 
360
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn)||(opa == ZIPO_LDILO)||(opa == ZIPO_LDIHI)) {
361
                if ((imma > 15)||(imma < -16))
362
                        return false;
363
        } else if ((imma > 7)||(imma < -8))
364
                        return false;
365
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn)||(opb == ZIPO_LDILO)||(opb == ZIPO_LDIHI)) {
366
                if ((immb > 15)||(immb < -16))
367
                        return false;
368
        } else if ((immb > 7)||(immb < -8))
369
                        return false;
370
 
371
        return true;
372
}
373
 
374
ZIPI    ZPARSER::merge(const ZIPI a, const ZIPI b) {
375
        assert(can_merge(a, b));
376
        ZIPI    ni;
377
 
378
        ZIPCOND ca( (ZIPCOND)((a>>19)&0x007)), cb( (ZIPCOND)((b>>19)&0x007));
379
        ZIPOP   opa((ZIPOP)((a>>25)&0x012)), opb((ZIPOP)((b>>22)&0x01f));
380
 
381
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
382
                ca = ZIPC_ALWAYS;
383
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
384
                cb = ZIPC_ALWAYS;
385
 
386
        ZIPIMM imma, immb;
387
        imma = immediate(a);
388
        immb = immediate(b);
389
 
390
        ni = (opa << 26)|(opb<<9)|0x80000000;
391
        if (ca != ZIPC_ALWAYS) {
392
                ni |= (ca << 19);
393
                if (cb == ca)
394
                        ni |= (1<<21);
395
        }
396
 
397
        // The result register(s)
398
        ni |= (a & 0x78000000);
399
        ni |= ((b>>27)&0x0f)<<5;
400
 
401
        // Are we using the register form of opB?
402
        switch(opa) {
403
                case ZIPO_MOV: ni |= (a&0x078000); break; // Always a register
404
                case ZIPO_LDI: case ZIPO_LDIn:
405
                case ZIPO_LDILO: case ZIPO_LDIHI:
406
                        ni |= (imma & 0x01f)<<14;
407
                        break;
408
                default:
409
                        if (a & 0x040000) {
410
                                ni |= (a&0x078000);
411
                        } else
412
                                ni |= (imma & 0x0f)<<14;
413
        }
414
 
415
        switch(opb) {
416
                case ZIPO_MOV:
417
                        ni |= ((b>>14)&0x0f)|0x10; break;
418
                case ZIPO_LDI: case ZIPO_LDIn:
419
                case ZIPO_LDILO: case ZIPO_LDIHI:
420
                        ni |= (immb & 0x01f);
421
                        break;
422
                default:
423
                        if (b & 0x040000) {
424
                                ni |= ((b>>14)&0x0f)|0x10;
425
                        } else
426
                                ni |= (immb & 0x0f);
427
        }
428
 
429
        return ni;
430
}

powered by: WebSVN 2.1.0

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