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

Subversion Repositories zipcpu

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

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
        ZIPOP   op((ZIPOP)((a>>25)&0x012));
242
        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
                                if (imm != 0)
258
                                        return false;
259
                        } else {
260
                                imm = (a&0x3ffff);
261
                                if (a&0x20000)
262
                                        imm |= -0x20000;
263
                        }
264
        }
265
 
266
        return imm;
267
}
268
 
269
bool    ZPARSER::can_merge(const ZIPI a, const ZIPI b) {
270
        // 1. Can't merge anything that's already merged
271
        if ((a|b) & 0x80000000)
272
                return false;
273
 
274
        ZIPOP   opa((ZIPOP)((a>>25)&0x012)), opb((ZIPOP)((b>>22)&0x01f));
275
        // 2. Conditions
276
        {
277
                ZIPCOND ca((ZIPCOND)((a>>19)&0x07)),cb((ZIPCOND)((b>>19)&0x07));
278
 
279
                if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
280
                        ca = ZIPC_ALWAYS;
281
                if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
282
                        cb = ZIPC_ALWAYS;
283
 
284
                if ((ca == ZIPC_ALWAYS)&&(cb != ZIPC_ALWAYS))
285
                        return false;
286
                if ((ca|cb) &0x04)
287
                        return false;
288
                if ((ca != ZIPC_ALWAYS)&&((cb != ca)&&(cb != ZIPC_ALWAYS)))
289
                        return false;
290
                // if ((ca != ZIPC_ALWAYS)||(cb != ZIPC_ALWAYS))
291
                        // return false;
292
        }
293
 
294
        // 3. Moves ... only move if the move doesn't address user registers
295
 
296
        if ((opa == ZIPO_MOV)&&(a & ((1<<18)|(1<<13))))
297
                return false;
298
        if ((opb == ZIPO_MOV)&&(b & ((1<<18)|(1<<13))))
299
                return false;
300
 
301
        // 4. Immediates.  If Register + Immediate, the answer is No.
302
        ZIPIMM imma, immb;
303
        switch(opa) {
304
                case ZIPO_MOV:
305
                        imma = (a & 0x0fff); if (a) return false; break;
306
                case ZIPO_LDI: case ZIPO_LDIn:
307
                case ZIPO_LDILO: case ZIPO_LDIHI:
308
                        imma = immediate(a);   break;
309
                default:
310
                        if (a & 0x040000) {
311
                                imma = (a&0x3ffff);
312
                                // if (a&0x20000) a |= -0x20000;
313
                                if (imma != 0)
314
                                        return false;
315
                        } else {
316
                                imma = (a&0x3fff);
317
                                if (a&0x2000) // Sign extension?
318
                                        imma |= -0x02000;
319
                        }
320
        } switch(opb) {
321
                case ZIPO_MOV:
322
                        immb = (b & 0x0fff); if (b) return false; break;
323
                case ZIPO_LDI: case ZIPO_LDIn:
324
                case ZIPO_LDILO: case ZIPO_LDIHI:
325
                        immb = immediate(b);   break;
326
                default:
327
                        if (b & 0x040000) {
328
                                immb = (b&0x3fff);
329
                                // if (b&0x2000) b |= -0x02000;
330
                                if (immb != 0)
331
                                        return false;
332
                        } else {
333
                                immb = (b&0x3ffff);
334
                                if (b&0x20000)
335
                                        immb |= -0x20000;
336
                        }
337
        }
338
 
339
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn)||(opa == ZIPO_LDILO)||(opa == ZIPO_LDIHI)) {
340
                if ((imma > 15)||(imma < -16))
341
                        return false;
342
        } else if ((imma > 7)||(imma < -8))
343
                        return false;
344
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn)||(opb == ZIPO_LDILO)||(opb == ZIPO_LDIHI)) {
345
                if ((immb > 15)||(immb < -16))
346
                        return false;
347
        } else if ((immb > 7)||(immb < -8))
348
                        return false;
349
 
350
        return true;
351
}
352
 
353
ZIPI    ZPARSER::merge(const ZIPI a, const ZIPI b) {
354
        assert(can_merge(a, b));
355
        ZIPI    ni;
356
 
357
        ZIPCOND ca( (ZIPCOND)((a>>19)&0x007)), cb( (ZIPCOND)((b>>19)&0x007));
358
        ZIPOP   opa((ZIPOP)((a>>25)&0x012)), opb((ZIPOP)((b>>22)&0x01f));
359
 
360
        if ((opa == ZIPO_LDI)||(opa == ZIPO_LDIn))
361
                ca = ZIPC_ALWAYS;
362
        if ((opb == ZIPO_LDI)||(opb == ZIPO_LDIn))
363
                cb = ZIPC_ALWAYS;
364
 
365
        ZIPIMM imma, immb;
366
        imma = immediate(a);
367
        immb = immediate(b);
368
 
369
        ni = (opa << 26)|(opb<<9)|0x80000000;
370
        if (ca != ZIPC_ALWAYS) {
371
                ni |= (ca << 19);
372
                if (cb == ca)
373
                        ni |= (1<<21);
374
        }
375
 
376
        // The result register(s)
377
        ni |= (a & 0x78000000);
378
        ni |= ((b>>27)&0x0f)<<5;
379
 
380
        // Are we using the register form of opB?
381
        switch(opa) {
382
                case ZIPO_MOV: ni |= (a&0x078000); break; // Always a register
383
                case ZIPO_LDI: case ZIPO_LDIn:
384
                case ZIPO_LDILO: case ZIPO_LDIHI:
385
                        ni |= (imma & 0x01f)<<14;
386
                        break;
387
                default:
388
                        if (a & 0x040000) {
389
                                ni |= (a&0x078000);
390
                        } else
391
                                ni |= (imma & 0x0f)<<14;
392
        }
393
 
394
        switch(opb) {
395
                case ZIPO_MOV:
396
                        ni |= ((b>>14)&0x0f)|0x10; break;
397
                case ZIPO_LDI: case ZIPO_LDIn:
398
                case ZIPO_LDILO: case ZIPO_LDIHI:
399
                        ni |= (immb & 0x01f);
400
                        break;
401
                default:
402
                        if (b & 0x040000) {
403
                                ni |= ((b>>14)&0x0f)|0x10;
404
                        } else
405
                                ni |= (immb & 0x0f);
406
        }
407
 
408
        return ni;
409
}

powered by: WebSVN 2.1.0

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