3 // Copyright (c) 2009 The University of Edinburgh
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met: redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer;
10 // redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution;
13 // neither the name of the copyright holders nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ////////////////////////////////////////////////////////////////////
31 // The actual Power ISA decoder
32 // ------------------------------
34 // I've used the Power ISA Book I v2.06 for instruction formats,
35 // opcode numbers, register names, etc.
37 decode PO default Unknown::unknown() {
41 // Unconditionally branch relative to PC.
43 0: b({{ NIA = (uint32_t)(CIA + disp); }});
46 // Unconditionally branch to fixed address.
47 format BranchNonPCRel {
48 1: ba({{ NIA = targetAddr; }});
54 // Conditionally branch relative to PC based on CR and CTR.
55 format BranchPCRelCondCtr {
56 0: bc({{ NIA = (uint32_t)(CIA + disp); }});
59 // Conditionally branch to fixed address based on CR and CTR.
60 format BranchNonPCRelCondCtr {
61 1: bca({{ NIA = targetAddr; }});
67 // Conditionally branch to address in LR based on CR and CTR.
68 format BranchLrCondCtr {
69 16: bclr({{ NIA = LR & 0xfffffffc; }});
72 // Conditionally branch to address in CTR based on CR.
73 format BranchCtrCond {
74 528: bcctr({{ NIA = CTR & 0xfffffffc; }});
77 // Condition register manipulation instructions.
80 uint32_t crBa = bits(CR, 31 - ba);
81 uint32_t crBb = bits(CR, 31 - bb);
82 CR = insertBits(CR, 31 - bt, crBa & crBb);
86 uint32_t crBa = bits(CR, 31 - ba);
87 uint32_t crBb = bits(CR, 31 - bb);
88 CR = insertBits(CR, 31 - bt, crBa | crBb);
92 uint32_t crBa = bits(CR, 31 - ba);
93 uint32_t crBb = bits(CR, 31 - bb);
94 CR = insertBits(CR, 31 - bt, !(crBa & crBb));
98 uint32_t crBa = bits(CR, 31 - ba);
99 uint32_t crBb = bits(CR, 31 - bb);
100 CR = insertBits(CR, 31 - bt, crBa ^ crBb);
104 uint32_t crBa = bits(CR, 31 - ba);
105 uint32_t crBb = bits(CR, 31 - bb);
106 CR = insertBits(CR, 31 - bt, !(crBa | crBb));
110 uint32_t crBa = bits(CR, 31 - ba);
111 uint32_t crBb = bits(CR, 31 - bb);
112 CR = insertBits(CR, 31 - bt, crBa == crBb);
116 uint32_t crBa = bits(CR, 31 - ba);
117 uint32_t crBb = bits(CR, 31 - bb);
118 CR = insertBits(CR, 31 - bt, crBa & !crBb);
122 uint32_t crBa = bits(CR, 31 - ba);
123 uint32_t crBb = bits(CR, 31 - bb);
124 CR = insertBits(CR, 31 - bt, crBa | !crBb);
130 uint32_t crBfa = bits(CR, 31 - bfa*4, 28 - bfa*4);
131 CR = insertBits(CR, 31 - bf*4, 28 - bf*4, crBfa);
136 150: isync({{ }}, [ IsSerializeAfter ]);
140 17: IntOp::sc({{ xc->syscall(R0, &fault); }},
141 [ IsSyscall, IsNonSpeculative, IsSerializeAfter ]);
144 34: lbz({{ Rt = Mem_ub; }});
145 40: lhz({{ Rt = Mem_uh; }});
146 42: lha({{ Rt = Mem_sh; }});
147 32: lwz({{ Rt = Mem_uw; }});
151 format LoadDispShiftOp {
152 2: lwa({{ Rt = Mem_sw; }});
153 0: ld({{ Rt = Mem; }});
156 format LoadDispShiftUpdateOp {
157 1: ldu({{ Rt = Mem; }});
162 format StoreDispShiftOp {
163 0: std({{ Mem = Rs; }});
166 format StoreDispShiftUpdateOp {
167 1: stdu({{ Mem = Rs; }});
171 format LoadDispUpdateOp {
172 35: lbzu({{ Rt = Mem_ub; }});
173 41: lhzu({{ Rt = Mem_uh; }});
174 43: lhau({{ Rt = Mem_sh; }});
175 33: lwzu({{ Rt = Mem_uw; }});
179 38: stb({{ Mem_ub = Rs_ub; }});
180 44: sth({{ Mem_uh = Rs_uh; }});
181 36: stw({{ Mem = Rs_uw; }});
184 format StoreDispUpdateOp {
185 39: stbu({{ Mem_ub = Rs_ub; }});
186 45: sthu({{ Mem_uh = Rs_uh; }});
187 37: stwu({{ Mem = Rs_uw; }});
190 format IntImmArithCheckRaOp {
191 14: addi({{ Rt = Ra + simm; }},
193 15: addis({{ Rt = Ra + (simm << 16); }},
194 {{ Rt = simm << 16; }});
197 format IntImmArithOp {
218 int64_t prod = src * simm;
226 uint32_t cr = makeCRField(Ra, (uint32_t)uimm, xer.so);
227 CR = insertCRField(CR, BF, cr);
231 uint32_t cr = makeCRField(Ra_sw, (int32_t)imm, xer.so);
232 CR = insertCRField(CR, BF, cr);
236 format IntImmLogicOp {
237 24: ori({{ Ra = Rs | uimm; }});
238 25: oris({{ Ra = Rs | (uimm << 16); }});
239 26: xori({{ Ra = Rs ^ uimm; }});
240 27: xoris({{ Ra = Rs ^ (uimm << 16); }});
241 28: andi_({{ Ra = Rs & uimm; }},
243 29: andis_({{ Ra = Rs & (uimm << 16); }},
248 21: rlwinm({{ Ra = rotateValue(Rs, sh) & fullMask; }});
249 23: rlwnm({{ Ra = rotateValue(Rs, Rb) & fullMask; }});
250 20: rlwimi({{ Ra = (rotateValue(Rs, sh) & fullMask) |
251 (Ra & ~fullMask); }});
254 // There are a large number of instructions that have the same primary
255 // opcode (PO) of 31. In this case, the instructions are of different
256 // forms. For every form, the XO fields may vary in position and width.
257 // The X, XFL, XFX and XL form instructions use bits 21 - 30 and the
258 // XO form instructions use bits 22 - 30 as extended opcode (XO). To
259 // avoid conflicts, instructions of each form have to be defined under
260 // separate decode blocks. However, only a single decode block can be
261 // associated with a particular PO and it will recognize only one type
262 // of XO field. A solution for associating decode blocks for the other
263 // types of XO fields with the same PO is to have the other blocks as
264 // nested default cases.
267 // All loads with an index register. The non-update versions
268 // all use the value 0 if Ra == R0, not the value contained in
269 // R0. Others update Ra with the effective address. In all cases,
270 // Ra and Rb are source registers, Rt is the destintation.
272 87: lbzx({{ Rt = Mem_ub; }});
273 52: lbarx({{ Rt = Mem_ub; Rsv = 1; RsvLen = 1; RsvAddr = EA; }});
274 279: lhzx({{ Rt = Mem_uh; }});
275 343: lhax({{ Rt = Mem_sh; }});
276 116: lharx({{ Rt = Mem_uh; Rsv = 1; RsvLen = 2; RsvAddr = EA; }});
277 790: lhbrx({{ Rt = swap_byte(Mem_uh); }});
278 23: lwzx({{ Rt = Mem_uw; }});
279 341: lwax({{ Rt = Mem_sw; }});
280 20: lwarx({{ Rt = Mem_uw; Rsv = 1; RsvLen = 4; RsvAddr = EA; }});
281 534: lwbrx({{ Rt = swap_byte(Mem_uw); }});
282 21: ldx({{ Rt = Mem; }});
283 84: ldarx({{ Rt = Mem_ud; Rsv = 1; RsvLen = 8; RsvAddr = EA; }});
284 532: ldbrx({{ Rt = swap_byte(Mem); }});
285 535: lfsx({{ Ft_sf = Mem_sf; }});
286 599: lfdx({{ Ft = Mem_df; }});
287 855: lfiwax({{ Ft_uw = Mem; }});
290 format LoadIndexUpdateOp {
291 119: lbzux({{ Rt = Mem_ub; }});
292 311: lhzux({{ Rt = Mem_uh; }});
293 375: lhaux({{ Rt = Mem_sh; }});
294 55: lwzux({{ Rt = Mem_uw; }});
295 373: lwaux({{ Rt = Mem_sw; }});
296 53: ldux({{ Rt = Mem; }});
297 567: lfsux({{ Ft_sf = Mem_sf; }});
298 631: lfdux({{ Ft = Mem_df; }});
301 format StoreIndexOp {
302 215: stbx({{ Mem_ub = Rs_ub; }});
304 bool store_performed = false;
309 store_performed = true;
315 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
319 407: sthx({{ Mem_uh = Rs_uh; }});
321 bool store_performed = false;
326 store_performed = true;
332 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
336 918: sthbrx({{ Mem_uh = swap_byte(Rs_uh); }});
337 151: stwx({{ Mem_uw = Rs_uw; }});
339 bool store_performed = false;
344 store_performed = true;
350 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
354 662: stwbrx({{ Mem_uw = swap_byte(Rs_uw); }});
355 149: stdx({{ Mem = Rs }});
357 bool store_performed = false;
362 store_performed = true;
368 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
372 660: stdbrx({{ Mem = swap_byte(Rs); }});
375 format StoreIndexUpdateOp {
376 247: stbux({{ Mem_ub = Rs_ub; }});
377 439: sthux({{ Mem_uh = Rs_uh; }});
378 183: stwux({{ Mem_uw = Rs_uw; }});
379 181: stdux({{ Mem = Rs; }});
385 uint32_t cr = makeCRField(Ra_sw, Rb_sw, xer.so);
386 CR = insertCRField(CR, BF, cr);
391 uint32_t cr = makeCRField(Ra, Rb, xer.so);
392 CR = insertCRField(CR, BF, cr);
396 // Integer logic instructions use source registers Rs and Rb,
397 // with destination register Ra.
399 28: and({{ Ra = Rs & Rb; }});
400 316: xor({{ Ra = Rs ^ Rb; }});
401 476: nand({{ Ra = ~(Rs & Rb); }});
402 444: or({{ Ra = Rs | Rb; }});
403 124: nor({{ Ra = ~(Rs | Rb); }});
404 60: andc({{ Ra = Rs & ~Rb; }});
405 954: extsb({{ Ra = sext<8>(Rs); }});
406 284: eqv({{ Ra = ~(Rs ^ Rb); }});
407 412: orc({{ Ra = Rs | ~Rb; }});
408 922: extsh({{ Ra = sext<16>(Rs); }});
409 26: cntlzw({{ Ra = Rs == 0 ? 32 : 31 - findMsbSet(Rs); }});
412 for (int n = 0; n < 32; n += 8) {
413 if(bits(Rs, n+7, n) == bits(Rb, n+7, n)) {
414 val = insertBits(val, n+7, n, 0xff);
424 Ra = Rs << (Rb & 0x1f);
432 Ra = Rs >> (Rb & 0x1f);
437 bool shiftSetCA = false;
442 } else if (Rb & 0x20) {
445 if (s & 0x7fffffff) {
455 Ra = s >> (Rb & 0x1f);
456 if (s < 0 && (s << (32 - (Rb & 0x1f))) != 0) {
472 // Integer logic instructions with a shift value.
475 bool shiftSetCA = false;
482 if (s < 0 && (s << (32 - sh)) != 0) {
498 // Generic integer format instructions.
501 0x20: mfxer({{ Rt = XER; }});
502 0x100: mflr({{ Rt = LR; }});
503 0x120: mfctr({{ Rt = CTR; }});
506 0x20: mtxer({{ XER = Rs; }});
507 0x100: mtlr({{ LR = Rs; }});
508 0x120: mtctr({{ CTR = Rs; }});
512 format StoreIndexOp {
513 663: stfsx({{ Mem_sf = Fs_sf; }});
514 727: stfdx({{ Mem_df = Fs; }});
515 983: stfiwx({{ Mem = Fs_uw; }});
518 format StoreIndexUpdateOp {
519 695: stfsux({{ Mem_sf = Fs_sf; }});
520 759: stfdux({{ Mem_df = Fs; }});
523 // These instructions all provide data cache hints
527 598: sync({{ }}, [ IsMemBarrier ]);
528 854: eieio({{ }}, [ IsMemBarrier ]);
531 // These instructions are of XO form with bit 21 as the OE bit.
532 default: decode XO_XO {
534 // These instructions can all be reduced to the form
535 // Rt = src1 + src2 [+ CA], therefore we just give src1 and src2
536 // (and, if necessary, CA) definitions and let the python script
537 // deal with setting things up correctly. We also give flags to
538 // say which control registers to set.
540 266: add({{ Ra }}, {{ Rb }});
541 40: subf({{ ~Ra }}, {{ Rb }}, {{ 1 }});
542 10: addc({{ Ra }}, {{ Rb }},
544 8: subfc({{ ~Ra }}, {{ Rb }}, {{ 1 }},
546 104: neg({{ ~Ra }}, {{ 1 }});
547 138: adde({{ Ra }}, {{ Rb }}, {{ xer.ca }},
549 234: addme({{ Ra }}, {{ -1ULL }}, {{ xer.ca }},
551 136: subfe({{ ~Ra }}, {{ Rb }}, {{ xer.ca }},
553 232: subfme({{ ~Ra }}, {{ -1ULL }}, {{ xer.ca }},
555 202: addze({{ Ra }}, {{ xer.ca }},
557 200: subfze({{ ~Ra }}, {{ xer.ca }},
561 // Arithmetic instructions all use source registers Ra and Rb,
562 // with destination register Rt.
565 int64_t prod = Ra_sd * Rb_sd;
569 uint64_t prod = Ra_ud * Rb_ud;
572 235: mullw({{ int64_t prod = Ra_sd * Rb_sd; Rt = prod; }});
574 int64_t src1 = Ra_sd;
576 int64_t prod = src1 * src2;
582 int32_t src1 = Ra_sw;
583 int32_t src2 = Rb_sw;
584 if ((src1 != 0x80000000 || src2 != 0xffffffff)
593 int32_t src1 = Ra_sw;
594 int32_t src2 = Rb_sw;
595 if ((src1 != 0x80000000 || src2 != 0xffffffff)
606 uint32_t src1 = Ra_sw;
607 uint32_t src2 = Rb_sw;
616 uint32_t src1 = Ra_sw;
617 uint32_t src2 = Rb_sw;
628 default: decode XFX_XO {
632 for (int i = 0; i < 8; ++i) {
633 if (((FXM >> i) & 0x1) == 0x1) {
634 mask |= 0xf << (4 * i);
637 CR = (Rs & mask) | (CR & ~mask);
640 19: mfcr({{ Rt = CR; }});
647 48: lfs({{ Ft_sf = Mem_sf; }});
648 50: lfd({{ Ft = Mem_df; }});
651 format LoadDispUpdateOp {
652 49: lfsu({{ Ft_sf = Mem_sf; }});
653 51: lfdu({{ Ft = Mem_df; }});
657 52: stfs({{ Mem_sf = Fs_sf; }});
658 54: stfd({{ Mem_df = Fs; }});
661 format StoreDispUpdateOp {
662 53: stfsu({{ Mem_sf = Fs_sf; }});
663 55: stfdu({{ Mem_df = Fs; }});
666 format FloatArithOp {
668 21: fadds({{ Ft = Fa + Fb; }});
669 20: fsubs({{ Ft = Fa - Fb; }});
670 25: fmuls({{ Ft = Fa * Fc; }});
671 18: fdivs({{ Ft = Fa / Fb; }});
672 29: fmadds({{ Ft = (Fa * Fc) + Fb; }});
673 28: fmsubs({{ Ft = (Fa * Fc) - Fb; }});
674 31: fnmadds({{ Ft = -((Fa * Fc) + Fb); }});
675 30: fnmsubs({{ Ft = -((Fa * Fc) - Fb); }});
680 format FloatArithOp {
681 21: fadd({{ Ft = Fa + Fb; }});
682 20: fsub({{ Ft = Fa - Fb; }});
683 25: fmul({{ Ft = Fa * Fc; }});
684 18: fdiv({{ Ft = Fa / Fb; }});
685 29: fmadd({{ Ft = (Fa * Fc) + Fb; }});
686 28: fmsub({{ Ft = (Fa * Fc) - Fb; }});
687 31: fnmadd({{ Ft = -((Fa * Fc) + Fb); }});
688 30: fnmsub({{ Ft = -((Fa * Fc) - Fb); }});
691 default: decode X_XO {
692 format FloatRCCheckOp {
693 72: fmr({{ Ft = Fb; }});
696 Ft_ud = insertBits(Ft_ud, 63, 0); }});
699 Ft_ud = insertBits(Ft_ud, 63, 1); }});
700 40: fneg({{ Ft = -Fb; }});
703 Ft_ud = insertBits(Ft_ud, 63, Fa_ud<63:63>);
707 format FloatConvertOp {
708 12: frsp({{ Ft_sf = Fb; }});
709 15: fctiwz({{ Ft_sw = (int32_t)trunc(Fb); }});
714 uint32_t c = makeCRField(Fa, Fb);
718 CR = insertCRField(CR, BF, c);
722 format FloatRCCheckOp {
723 583: mffs({{ Ft_ud = FPSCR; }});
725 FPSCR = insertCRField(FPSCR, BF + (8 * (1 - W_FIELD)),
728 70: mtfsb0({{ FPSCR = insertBits(FPSCR, 31 - BT, 0); }});
729 38: mtfsb1({{ FPSCR = insertBits(FPSCR, 31 - BT, 1); }});
731 default: decode XFL_XO {
733 if (L_FIELD == 1) { FPSCR = Fb_ud; }
735 for (int i = 0; i < 8; ++i) {
736 if (bits(FLM, i) == 1) {
737 int k = 4 * (i + (8 * (1 - W_FIELD)));
738 FPSCR = insertBits(FPSCR, k + 3, k,
739 bits(Fb_ud, k + 3, k));