arch-power: Fix disassembly for logical instructions
[gem5.git] / src / arch / power / insts / integer.hh
1 /*
2 * Copyright (c) 2009 The University of Edinburgh
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __ARCH_POWER_INSTS_INTEGER_HH__
30 #define __ARCH_POWER_INSTS_INTEGER_HH__
31
32 #include "arch/power/insts/static_inst.hh"
33 #include "base/bitfield.hh"
34 #include "base/cprintf.hh"
35
36 namespace PowerISA
37 {
38
39 /**
40 * We provide a base class for integer operations and then inherit for
41 * several other classes. These specialise for instructions using immediate
42 * values and also rotate instructions. We also need to have versions that
43 * consider the Rc and OE bits.
44 */
45
46 /**
47 * Base class for integer operations.
48 */
49 class IntOp : public PowerStaticInst
50 {
51 protected:
52
53 bool rcSet;
54 bool oeSet;
55
56 // Needed for srawi only
57 uint32_t sh;
58
59 /// Constructor
60 IntOp(const char *mnem, MachInst _machInst, OpClass __opClass)
61 : PowerStaticInst(mnem, _machInst, __opClass),
62 rcSet(false), oeSet(false)
63 {
64 }
65
66 /* Compute the CR (condition register) field using signed comparison */
67 inline uint32_t
68 makeCRField(int64_t a, int64_t b, uint32_t xerSO) const
69 {
70 uint32_t c = xerSO;
71
72 /* We've pre-shifted the immediate values here */
73 if (a < b) { c += 0x8; }
74 else if (a > b) { c += 0x4; }
75 else { c += 0x2; }
76 return c;
77 }
78
79 inline uint32_t
80 makeCRField(int64_t a, int32_t b, uint32_t xerSO) const
81 {
82 return makeCRField(a, (int64_t)b, xerSO);
83 }
84
85 inline uint32_t
86 makeCRField(int32_t a, int32_t b, uint32_t xerSO) const
87 {
88 return makeCRField((int64_t)a, (int64_t)b, xerSO);
89 }
90
91 /* Compute the CR (condition register) field using unsigned comparison */
92 inline uint32_t
93 makeCRField(uint64_t a, uint64_t b, uint32_t xerSO) const
94 {
95 uint32_t c = xerSO;
96
97 /* We've pre-shifted the immediate values here */
98 if (a < b) { c += 0x8; }
99 else if (a > b) { c += 0x4; }
100 else { c += 0x2; }
101 return c;
102 }
103
104 inline uint32_t
105 makeCRField(uint64_t a, uint32_t b, uint32_t xerSO) const
106 {
107 return makeCRField(a, (uint64_t)b, xerSO);
108 }
109
110 inline uint32_t
111 makeCRField(uint32_t a, uint32_t b, uint32_t xerSO) const
112 {
113 return makeCRField((uint64_t)a, (uint64_t)b, xerSO);
114 }
115
116 std::string generateDisassembly(
117 Addr pc, const Loader::SymbolTable *symtab) const override;
118 };
119
120
121 /**
122 * Class for integer immediate (signed and unsigned) operations.
123 */
124 class IntImmOp : public IntOp
125 {
126 protected:
127
128 int32_t imm;
129 uint32_t uimm;
130
131 /// Constructor
132 IntImmOp(const char *mnem, MachInst _machInst, OpClass __opClass)
133 : IntOp(mnem, _machInst, __opClass),
134 imm(sext<16>(machInst.si)),
135 uimm(machInst.si)
136 {
137 }
138
139 std::string generateDisassembly(
140 Addr pc, const Loader::SymbolTable *symtab) const override;
141 };
142
143
144 /**
145 * Class for integer arithmetic operations.
146 */
147 class IntArithOp : public IntOp
148 {
149 protected:
150
151 /// Constructor
152 IntArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
153 : IntOp(mnem, _machInst, __opClass)
154 {
155 }
156
157 /* Compute 128-bit sum of 128-bit to 64-bit unsigned integer addition */
158 inline std::tuple<uint64_t, uint64_t>
159 add(uint64_t ralo, uint64_t rahi, uint64_t rb) const
160 {
161 uint64_t slo, shi;
162 #if defined(__SIZEOF_INT128__)
163 __uint128_t ra = ((__uint128_t)rahi << 64) | ralo;
164 __uint128_t sum = ra + rb;
165 slo = sum;
166 shi = sum >> 64;
167 #else
168 shi = rahi + ((ralo + rb) < ralo);
169 slo = ralo + rb;
170 #endif
171 return std::make_tuple(slo, shi);
172 }
173
174 /* Compute 128-bit sum of 128-bit to 64-bit signed integer addition */
175 inline std::tuple<uint64_t, int64_t>
176 add(uint64_t ralo, int64_t rahi, int64_t rb) const
177 {
178 uint64_t slo;
179 int64_t shi;
180 #if defined(__SIZEOF_INT128__)
181 __int128_t ra = ((__int128_t)rahi << 64) | ralo;
182 __int128_t sum = (__int128_t)ra + rb;
183 slo = sum;
184 shi = sum >> 64;
185 #else
186 if (rb < 0) {
187 shi = rahi - 1;
188 slo = ralo + rb;
189 if (slo < rb) {
190 shi++;
191 }
192 } else {
193 shi = rahi;
194 slo = ralo + rb;
195 if (slo < rb) {
196 shi++;
197 }
198 }
199 #endif
200 return std::make_tuple(slo, shi);
201 }
202
203 /**
204 * Compute 128-bit product of 64-bit unsigned integer multiplication
205 * based on https://stackoverflow.com/a/28904636
206 */
207 inline std::tuple<uint64_t, uint64_t>
208 multiply(uint64_t ra, uint64_t rb) const
209 {
210 uint64_t plo, phi;
211 #if defined(__SIZEOF_INT128__)
212 __uint128_t prod = (__uint128_t)ra * rb;
213 plo = prod;
214 phi = prod >> 64;
215 #else
216 uint64_t ralo = (uint32_t)ra, rahi = ra >> 32;
217 uint64_t rblo = (uint32_t)rb, rbhi = rb >> 32;
218 uint64_t pp0 = ralo * rblo;
219 uint64_t pp1 = rahi * rblo;
220 uint64_t pp2 = ralo * rbhi;
221 uint64_t pp3 = rahi * rbhi;
222 uint64_t c = ((uint32_t)pp1) + ((uint32_t)pp2) + (pp0 >> 32);
223 phi = pp3 + (pp2 >> 32) + (pp1 >> 32) + (c >> 32);
224 plo = (c << 32) | ((uint32_t)pp0);
225 #endif
226 return std::make_tuple(plo, phi);
227 }
228
229 /* Compute 128-bit product of 64-bit signed integer multiplication */
230 inline std::tuple<uint64_t, int64_t>
231 multiply(int64_t ra, int64_t rb) const
232 {
233 uint64_t plo, phi;
234 #if defined(__SIZEOF_INT128__)
235 __int128_t prod = (__int128_t)ra * rb;
236 plo = prod;
237 phi = prod >> 64;
238 #else
239 std::tie(plo, phi) = multiply((uint64_t)ra, (uint64_t)rb);
240 if (rb < 0) phi -= (uint64_t)ra;
241 if (ra < 0) phi -= (uint64_t)rb;
242 #endif
243 return std::make_tuple(plo, (int64_t)phi);
244 }
245
246 /**
247 * Compute 128-bit result of 64-bit unsigned integer multiplication
248 * followed by addition
249 */
250 inline std::tuple<uint64_t, uint64_t>
251 multiplyAdd(uint64_t ra, uint64_t rb, uint64_t rc) const
252 {
253 uint64_t rlo, rhi;
254 #if defined(__SIZEOF_INT128__)
255 __uint128_t res = ((__uint128_t)ra * rb) + rc;
256 rlo = res;
257 rhi = res >> 64;
258 #else
259 uint64_t plo, phi;
260 std::tie(plo, phi) = multiply(ra, rb);
261 std::tie(rlo, rhi) = add(plo, phi, rc);
262 #endif
263 return std::make_tuple(rlo, rhi);
264 }
265
266 /**
267 * Compute 128-bit result of 64-bit signed integer multiplication
268 * followed by addition
269 */
270 inline std::tuple<uint64_t, int64_t>
271 multiplyAdd(int64_t ra, int64_t rb, int64_t rc) const
272 {
273 uint64_t rlo;
274 int64_t rhi;
275 #if defined(__SIZEOF_INT128__)
276 __int128_t res = (__int128_t)ra * rb + rc;
277 rlo = res;
278 rhi = res >> 64;
279 #else
280 uint64_t plo;
281 int64_t phi;
282 std::tie(plo, phi) = multiply(ra, rb);
283 std::tie(rlo, rhi) = add(plo, phi, rc);
284 #endif
285 return std::make_tuple(rlo, rhi);
286 }
287
288 /**
289 * Compute overflow, 64-bit quotient and 64-bit remainder of
290 * 128-bit by 64-bit unsigned integer division based on
291 * https://codereview.stackexchange.com/a/71013
292 */
293 inline std::tuple<bool, uint64_t, uint64_t>
294 divide(uint64_t ralo, uint64_t rahi, uint64_t rb) const
295 {
296 bool ov;
297 uint64_t q, r;
298 #if defined(__SIZEOF_INT128__)
299 if (rb == 0) {
300 ov = true;
301 } else {
302 __uint128_t ra = ((__uint128_t)rahi << 64) | ralo;
303 __uint128_t res = ra / rb;
304 q = res;
305 r = ra % rb;
306 ov = res > UINT64_MAX;
307 }
308 #else
309 uint64_t c = 0;
310
311 if (rb == 0) {
312 ov = true;
313 } else if (rahi == 0) {
314 q = ralo / rb;
315 r = ralo % rb;
316 ov = false;
317 } else if (rahi >= rb) {
318 ov = true;
319 } else {
320 for (int i = 0; i < 64; ++i) {
321 c = rahi >> 63;
322 rahi = (rahi << 1) | (ralo >> 63);
323 if (c || (rahi >= rb)) {
324 rahi -= rb;
325 c = 1;
326 } else {
327 c = 0;
328 }
329 ralo = (ralo << 1) | c;
330 }
331 q = ralo;
332 r = rahi;
333 ov = false;
334 }
335 #endif
336 return std::make_tuple(ov, q, r);
337 }
338
339 /**
340 * Compute overflow, 64-bit quotient and 64-bit remainder of
341 * 128-bit by 64-bit signed integer division
342 */
343 inline std::tuple<bool, int64_t, int64_t>
344 divide(uint64_t ralo, int64_t rahi, int64_t rb) const
345 {
346 bool ov;
347 int64_t q, r;
348 #if defined(__SIZEOF_INT128__)
349 if (rb == 0) {
350 ov = true;
351 } else {
352 __int128_t ra = ((__int128_t)rahi << 64) | ralo;
353 __int128_t res = ra / rb;
354 q = res;
355 r = ra % rb;
356 ov = res != q;
357 }
358 #else
359 bool raneg = rahi < 0;
360 bool rbneg = rb < 0;
361
362 if (raneg) {
363 ralo = ~(ralo);
364 rahi = ~(rahi);
365 if (ralo == -1ULL) {
366 ralo = 0;
367 rahi++;
368 } else {
369 ralo++;
370 }
371 }
372
373 if (rbneg) rb = -rb;
374 std::tie(ov, q, r) = divide(ralo, (uint64_t)rahi, (uint64_t)rb);
375 if (raneg ^ rbneg) q = -q;
376 if (raneg) r = -r;
377 if (!ov) ov = ((q < 0) ^ (raneg ^ rbneg));
378 #endif
379 return std::make_tuple(ov, q, r);
380 }
381
382 std::string generateDisassembly(
383 Addr pc, const Loader::SymbolTable *symtab) const override;
384 };
385
386
387 /**
388 * Class for integer immediate arithmetic operations.
389 */
390 class IntImmArithOp : public IntArithOp
391 {
392 protected:
393
394 int32_t simm;
395
396 /// Constructor
397 IntImmArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
398 : IntArithOp(mnem, _machInst, __opClass),
399 simm((int16_t)machInst.si)
400 {
401 }
402
403 std::string generateDisassembly(
404 Addr pc, const Loader::SymbolTable *symtab) const override;
405 };
406
407
408 /**
409 * Class for integer arithmetic operations with displacement.
410 */
411 class IntDispArithOp : public IntArithOp
412 {
413 protected:
414
415 int32_t disp;
416
417 /// Constructor
418 IntDispArithOp(const char *mnem, MachInst _machInst, OpClass __opClass)
419 : IntArithOp(mnem, _machInst, __opClass),
420 disp((int16_t)((machInst.d0 << 6) | (machInst.d1 << 1) | machInst.d2))
421 {
422 }
423
424 std::string generateDisassembly(
425 Addr pc, const Loader::SymbolTable *symtab) const override;
426 };
427
428
429 /**
430 * Class for integer compare operations.
431 */
432 class IntCompOp : public IntOp
433 {
434 protected:
435
436 uint32_t length;
437 uint32_t field;
438
439 /// Constructor
440 IntCompOp(const char *mnem, MachInst _machInst, OpClass __opClass)
441 : IntOp(mnem, _machInst, __opClass),
442 length(machInst.l),
443 field(machInst.bf)
444 {
445 }
446
447 std::string generateDisassembly(
448 Addr pc, const Loader::SymbolTable *symtab) const override;
449 };
450
451
452 /**
453 * Class for integer immediate compare operations.
454 */
455 class IntImmCompOp : public IntCompOp
456 {
457 protected:
458
459 int32_t simm;
460
461 /// Constructor
462 IntImmCompOp(const char *mnem, MachInst _machInst, OpClass __opClass)
463 : IntCompOp(mnem, _machInst, __opClass),
464 simm((int16_t)machInst.si)
465 {
466 }
467
468 std::string generateDisassembly(
469 Addr pc, const Loader::SymbolTable *symtab) const override;
470 };
471
472
473 /**
474 * Class for integer immediate compare logical operations.
475 */
476 class IntImmCompLogicOp : public IntCompOp
477 {
478 protected:
479
480 uint32_t uimm;
481
482 /// Constructor
483 IntImmCompLogicOp(const char *mnem, MachInst _machInst, OpClass __opClass)
484 : IntCompOp(mnem, _machInst, __opClass),
485 uimm(machInst.ui)
486 {
487 }
488
489 std::string generateDisassembly(
490 Addr pc, const Loader::SymbolTable *symtab) const override;
491 };
492
493
494 /**
495 * Class for integer logical operations.
496 */
497 class IntLogicOp : public IntOp
498 {
499 protected:
500
501 /// Constructor
502 IntLogicOp(const char *mnem, MachInst _machInst, OpClass __opClass)
503 : IntOp(mnem, _machInst, __opClass)
504 {
505 }
506
507 inline int
508 findLeadingZeros(uint32_t rs) const
509 {
510 if (rs) {
511 #if defined(__GNUC__) || (defined(__clang__) && \
512 __has_builtin(__builtin_clz))
513 return __builtin_clz(rs);
514 #else
515 return 31 - findMsbSet(rs);
516 #endif
517 } else {
518 return 32;
519 }
520 }
521
522 std::string generateDisassembly(
523 Addr pc, const Loader::SymbolTable *symtab) const override;
524 };
525
526
527 /**
528 * Class for integer immediate logical operations.
529 */
530 class IntImmLogicOp : public IntLogicOp
531 {
532 protected:
533
534 uint32_t uimm;
535
536 /// Constructor
537 IntImmLogicOp(const char *mnem, MachInst _machInst, OpClass __opClass)
538 : IntLogicOp(mnem, _machInst, __opClass),
539 uimm(machInst.si)
540 {
541 }
542
543 std::string generateDisassembly(
544 Addr pc, const Loader::SymbolTable *symtab) const override;
545 };
546
547
548 /**
549 * Class for integer operations with a shift.
550 */
551 class IntShiftOp : public IntOp
552 {
553 protected:
554
555 uint32_t sh;
556
557 /// Constructor
558 IntShiftOp(const char *mnem, MachInst _machInst, OpClass __opClass)
559 : IntOp(mnem, _machInst, __opClass),
560 sh(machInst.sh)
561 {
562 }
563
564 std::string generateDisassembly(
565 Addr pc, const Loader::SymbolTable *symtab) const override;
566 };
567
568
569 /**
570 * Class for integer rotate operations.
571 */
572 class IntRotateOp : public IntShiftOp
573 {
574 protected:
575
576 uint32_t mb;
577 uint32_t me;
578 uint32_t fullMask;
579
580 /// Constructor
581 IntRotateOp(const char *mnem, MachInst _machInst, OpClass __opClass)
582 : IntShiftOp(mnem, _machInst, __opClass),
583 mb(machInst.mb),
584 me(machInst.me)
585 {
586 if (me >= mb) {
587 fullMask = mask(31 - mb, 31 - me);
588 } else {
589 fullMask = ~mask(31 - (me + 1), 31 - (mb - 1));
590 }
591 }
592
593 uint32_t
594 rotateValue(uint32_t rs, uint32_t shift) const
595 {
596 uint32_t n = shift & 31;
597 return (rs << n) | (rs >> (32 - n));
598 }
599
600 std::string generateDisassembly(
601 Addr pc, const Loader::SymbolTable *symtab) const override;
602 };
603
604 } // namespace PowerISA
605
606 #endif //__ARCH_POWER_INSTS_INTEGER_HH__