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() {
39 // Unconditionally branch to a PC-relative or absoulute address.
41 18: b({{ NIA = CIA + disp; }},
45 // Conditionally branch to a PC-relative or absoulute address based
47 format BranchDispCondOp {
48 16: bc({{ NIA = CIA + disp; }},
54 // Conditionally branch to an address in a register based on
55 // either CR only or both CR and CTR.
56 format BranchRegCondOp {
57 16: bclr({{ NIA = LR & -4ULL; }}, true, [ IsReturn ]);
58 528: bcctr({{ NIA = CTR & -4ULL; }});
59 560: bctar({{ NIA = TAR & -4ULL; }}, true);
62 // Condition register manipulation instructions.
65 uint32_t crBa = bits(CR, 31 - ba);
66 uint32_t crBb = bits(CR, 31 - bb);
67 CR = insertBits(CR, 31 - bt, crBa & crBb);
71 uint32_t crBa = bits(CR, 31 - ba);
72 uint32_t crBb = bits(CR, 31 - bb);
73 CR = insertBits(CR, 31 - bt, crBa | crBb);
77 uint32_t crBa = bits(CR, 31 - ba);
78 uint32_t crBb = bits(CR, 31 - bb);
79 CR = insertBits(CR, 31 - bt, !(crBa & crBb));
83 uint32_t crBa = bits(CR, 31 - ba);
84 uint32_t crBb = bits(CR, 31 - bb);
85 CR = insertBits(CR, 31 - bt, crBa ^ crBb);
89 uint32_t crBa = bits(CR, 31 - ba);
90 uint32_t crBb = bits(CR, 31 - bb);
91 CR = insertBits(CR, 31 - bt, !(crBa | crBb));
95 uint32_t crBa = bits(CR, 31 - ba);
96 uint32_t crBb = bits(CR, 31 - bb);
97 CR = insertBits(CR, 31 - bt, crBa == crBb);
101 uint32_t crBa = bits(CR, 31 - ba);
102 uint32_t crBb = bits(CR, 31 - bb);
103 CR = insertBits(CR, 31 - bt, crBa & !crBb);
107 uint32_t crBa = bits(CR, 31 - ba);
108 uint32_t crBb = bits(CR, 31 - bb);
109 CR = insertBits(CR, 31 - bt, crBa | !crBb);
115 uint32_t crBfa = bits(CR, 31 - bfa*4, 28 - bfa*4);
116 CR = insertBits(CR, 31 - bf*4, 28 - bf*4, crBfa);
121 150: isync({{ }}, [ IsSerializeAfter ]);
124 default: decode DX_XO {
125 format IntDispArithOp {
126 2: addpcis({{ Rt = NIA + (disp << 16); }});
131 17: IntOp::sc({{ return std::make_shared<SESyscallFault>(); }});
134 34: lbz({{ Rt = Mem_ub; }});
135 40: lhz({{ Rt = Mem_uh; }});
136 42: lha({{ Rt = Mem_sh; }});
137 32: lwz({{ Rt = Mem_uw; }});
141 format LoadDispShiftOp {
142 2: lwa({{ Rt = Mem_sw; }});
143 0: ld({{ Rt = Mem; }});
146 format LoadDispShiftUpdateOp {
147 1: ldu({{ Rt = Mem; }});
152 format StoreDispShiftOp {
153 0: std({{ Mem = Rs; }});
156 format StoreDispShiftUpdateOp {
157 1: stdu({{ Mem = Rs; }});
161 format LoadDispUpdateOp {
162 35: lbzu({{ Rt = Mem_ub; }});
163 41: lhzu({{ Rt = Mem_uh; }});
164 43: lhau({{ Rt = Mem_sh; }});
165 33: lwzu({{ Rt = Mem_uw; }});
169 38: stb({{ Mem_ub = Rs_ub; }});
170 44: sth({{ Mem_uh = Rs_uh; }});
171 36: stw({{ Mem_uw = Rs_uw; }});
174 format StoreDispUpdateOp {
175 39: stbu({{ Mem_ub = Rs_ub; }});
176 45: sthu({{ Mem_uh = Rs_uh; }});
177 37: stwu({{ Mem_uw = Rs_uw; }});
180 format IntImmArithCheckRaOp {
181 14: addi({{ Rt = Ra + simm; }},
183 15: addis({{ Rt = Ra + (simm << 16); }},
184 {{ Rt = simm << 16; }});
187 format IntImmArithOp {
207 int64_t res = Ra_sd * simm;
215 uint32_t cr = makeCRField(Ra, (uint32_t)uimm, xer.so);
216 CR = insertCRField(CR, BF, cr);
220 uint32_t cr = makeCRField(Ra_sw, (int32_t)imm, xer.so);
221 CR = insertCRField(CR, BF, cr);
225 format IntImmLogicOp {
226 24: ori({{ Ra = Rs | uimm; }});
227 25: oris({{ Ra = Rs | (uimm << 16); }});
228 26: xori({{ Ra = Rs ^ uimm; }});
229 27: xoris({{ Ra = Rs ^ (uimm << 16); }});
230 28: andi_({{ Ra = Rs & uimm; }},
232 29: andis_({{ Ra = Rs & (uimm << 16); }},
237 21: rlwinm({{ Ra = rotateValue(Rs, sh) & fullMask; }});
238 23: rlwnm({{ Ra = rotateValue(Rs, Rb) & fullMask; }});
239 20: rlwimi({{ Ra = (rotateValue(Rs, sh) & fullMask) |
240 (Ra & ~fullMask); }});
243 // There are a large number of instructions that have the same primary
244 // opcode (PO) of 31. In this case, the instructions are of different
245 // forms. For every form, the XO fields may vary in position and width.
246 // The X, XFL, XFX and XL form instructions use bits 21 - 30 and the
247 // XO form instructions use bits 22 - 30 as extended opcode (XO). To
248 // avoid conflicts, instructions of each form have to be defined under
249 // separate decode blocks. However, only a single decode block can be
250 // associated with a particular PO and it will recognize only one type
251 // of XO field. A solution for associating decode blocks for the other
252 // types of XO fields with the same PO is to have the other blocks as
253 // nested default cases.
256 // All loads with an index register. The non-update versions
257 // all use the value 0 if Ra == R0, not the value contained in
258 // R0. Others update Ra with the effective address. In all cases,
259 // Ra and Rb are source registers, Rt is the destintation.
261 87: lbzx({{ Rt = Mem_ub; }});
262 52: lbarx({{ Rt = Mem_ub; Rsv = 1; RsvLen = 1; RsvAddr = EA; }});
263 279: lhzx({{ Rt = Mem_uh; }});
264 343: lhax({{ Rt = Mem_sh; }});
265 116: lharx({{ Rt = Mem_uh; Rsv = 1; RsvLen = 2; RsvAddr = EA; }});
266 790: lhbrx({{ Rt = swap_byte(Mem_uh); }});
267 23: lwzx({{ Rt = Mem_uw; }});
268 341: lwax({{ Rt = Mem_sw; }});
269 20: lwarx({{ Rt = Mem_uw; Rsv = 1; RsvLen = 4; RsvAddr = EA; }});
270 534: lwbrx({{ Rt = swap_byte(Mem_uw); }});
271 21: ldx({{ Rt = Mem; }});
272 84: ldarx({{ Rt = Mem_ud; Rsv = 1; RsvLen = 8; RsvAddr = EA; }});
273 532: ldbrx({{ Rt = swap_byte(Mem); }});
274 535: lfsx({{ Ft_sf = Mem_sf; }});
275 599: lfdx({{ Ft = Mem_df; }});
276 855: lfiwax({{ Ft_uw = Mem; }});
279 format LoadIndexUpdateOp {
280 119: lbzux({{ Rt = Mem_ub; }});
281 311: lhzux({{ Rt = Mem_uh; }});
282 375: lhaux({{ Rt = Mem_sh; }});
283 55: lwzux({{ Rt = Mem_uw; }});
284 373: lwaux({{ Rt = Mem_sw; }});
285 53: ldux({{ Rt = Mem; }});
286 567: lfsux({{ Ft_sf = Mem_sf; }});
287 631: lfdux({{ Ft = Mem_df; }});
290 format StoreIndexOp {
291 215: stbx({{ Mem_ub = Rs_ub; }});
293 bool store_performed = false;
298 store_performed = true;
304 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
308 407: sthx({{ Mem_uh = Rs_uh; }});
310 bool store_performed = false;
315 store_performed = true;
321 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
325 918: sthbrx({{ Mem_uh = swap_byte(Rs_uh); }});
326 151: stwx({{ Mem_uw = Rs_uw; }});
328 bool store_performed = false;
333 store_performed = true;
339 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
343 662: stwbrx({{ Mem_uw = swap_byte(Rs_uw); }});
344 149: stdx({{ Mem = Rs }});
346 bool store_performed = false;
351 store_performed = true;
357 cr.cr0 = ((store_performed ? 0x2 : 0x0) | xer.so);
361 660: stdbrx({{ Mem = swap_byte(Rs); }});
364 format StoreIndexUpdateOp {
365 247: stbux({{ Mem_ub = Rs_ub; }});
366 439: sthux({{ Mem_uh = Rs_uh; }});
367 183: stwux({{ Mem_uw = Rs_uw; }});
368 181: stdux({{ Mem = Rs; }});
374 uint32_t cr = makeCRField(Ra_sw, Rb_sw, xer.so);
375 CR = insertCRField(CR, BF, cr);
380 uint32_t cr = makeCRField(Ra, Rb, xer.so);
381 CR = insertCRField(CR, BF, cr);
385 // Integer logic instructions use source registers Rs and Rb,
386 // with destination register Ra.
388 28: and({{ Ra = Rs & Rb; }});
389 316: xor({{ Ra = Rs ^ Rb; }});
390 476: nand({{ Ra = ~(Rs & Rb); }});
391 444: or({{ Ra = Rs | Rb; }});
392 124: nor({{ Ra = ~(Rs | Rb); }});
393 60: andc({{ Ra = Rs & ~Rb; }});
394 954: extsb({{ Ra = sext<8>(Rs); }});
395 284: eqv({{ Ra = ~(Rs ^ Rb); }});
396 412: orc({{ Ra = Rs | ~Rb; }});
397 922: extsh({{ Ra = sext<16>(Rs); }});
398 26: cntlzw({{ Ra = Rs == 0 ? 32 : 31 - findMsbSet(Rs); }});
401 for (int n = 0; n < 32; n += 8) {
402 if(bits(Rs, n+7, n) == bits(Rb, n+7, n)) {
403 val = insertBits(val, n+7, n, 0xff);
413 Ra = Rs << (Rb & 0x1f);
421 Ra = Rs >> (Rb & 0x1f);
426 bool shiftSetCA = false;
431 } else if (Rb & 0x20) {
434 if (s & 0x7fffffff) {
444 Ra = s >> (Rb & 0x1f);
445 if (s < 0 && (s << (32 - (Rb & 0x1f))) != 0) {
461 // Integer logic instructions with a shift value.
464 bool shiftSetCA = false;
471 if (s < 0 && (s << (32 - sh)) != 0) {
487 format StoreIndexOp {
488 663: stfsx({{ Mem_sf = Fs_sf; }});
489 727: stfdx({{ Mem_df = Fs; }});
490 983: stfiwx({{ Mem = Fs_uw; }});
493 format StoreIndexUpdateOp {
494 695: stfsux({{ Mem_sf = Fs_sf; }});
495 759: stfdux({{ Mem_df = Fs; }});
498 // These instructions all provide data cache hints
502 598: sync({{ }}, [ IsReadBarrier, IsWriteBarrier ]);
503 854: eieio({{ }}, [ IsReadBarrier, IsWriteBarrier ]);
506 // These instructions are of XO form with bit 21 as the OE bit.
507 default: decode XO_XO {
509 // These instructions can all be reduced to the form
510 // Rt = src1 + src2 [+ CA], therefore we just give src1 and src2
511 // (and, if necessary, CA) definitions and let the python script
512 // deal with setting things up correctly. We also give flags to
513 // say which control registers to set.
515 266: add({{ Ra }}, {{ Rb }});
516 40: subf({{ ~Ra }}, {{ Rb }}, {{ 1 }});
517 10: addc({{ Ra }}, {{ Rb }},
519 8: subfc({{ ~Ra }}, {{ Rb }}, {{ 1 }},
521 104: neg({{ ~Ra }}, {{ 1 }});
522 138: adde({{ Ra }}, {{ Rb }}, {{ xer.ca }},
524 234: addme({{ Ra }}, {{ -1ULL }}, {{ xer.ca }},
526 136: subfe({{ ~Ra }}, {{ Rb }}, {{ xer.ca }},
528 232: subfme({{ ~Ra }}, {{ -1ULL }}, {{ xer.ca }},
530 202: addze({{ Ra }}, {{ xer.ca }},
532 200: subfze({{ ~Ra }}, {{ xer.ca }},
536 // Arithmetic instructions all use source registers Ra and Rb,
537 // with destination register Rt.
538 format IntArithCheckRcOp {
540 uint64_t res = (int64_t)Ra_sw * Rb_sw;
546 uint64_t res = (uint64_t)Ra_uw * Rb_uw;
552 int64_t res = (int64_t)Ra_sw * Rb_sw;
553 if (res != (int32_t)res) {
561 int32_t src1 = Ra_sw;
562 int32_t src2 = Rb_sw;
563 if ((src1 != INT32_MIN || src2 != -1) && src2 != 0) {
564 Rt = (uint32_t)(src1 / src2);
573 uint32_t src1 = Ra_uw;
574 uint32_t src2 = Rb_uw;
585 default: decode XFX_XO {
588 0x20: mfxer({{ Rt = XER; }});
589 0x100: mflr({{ Rt = LR; }});
590 0x120: mfctr({{ Rt = CTR; }});
591 0x1f9: mftar({{ Rt = TAR; }});
595 0x20: mtxer({{ XER = Rs; }});
596 0x100: mtlr({{ LR = Rs; }});
597 0x120: mtctr({{ CTR = Rs; }});
598 0x1f9: mttar({{ TAR = Rs; }});
603 for (int i = 0; i < 8; ++i) {
604 if (((FXM >> i) & 0x1) == 0x1) {
605 mask |= 0xf << (4 * i);
608 CR = (Rs & mask) | (CR & ~mask);
611 19: mfcr({{ Rt = CR; }});
614 CR = insertCRField(CR, BF, XER<31:28>);
623 48: lfs({{ Ft_sf = Mem_sf; }});
624 50: lfd({{ Ft = Mem_df; }});
627 format LoadDispUpdateOp {
628 49: lfsu({{ Ft_sf = Mem_sf; }});
629 51: lfdu({{ Ft = Mem_df; }});
633 52: stfs({{ Mem_sf = Fs_sf; }});
634 54: stfd({{ Mem_df = Fs; }});
637 format StoreDispUpdateOp {
638 53: stfsu({{ Mem_sf = Fs_sf; }});
639 55: stfdu({{ Mem_df = Fs; }});
642 format FloatArithOp {
644 21: fadds({{ Ft = Fa + Fb; }});
645 20: fsubs({{ Ft = Fa - Fb; }});
646 25: fmuls({{ Ft = Fa * Fc; }});
647 18: fdivs({{ Ft = Fa / Fb; }});
648 29: fmadds({{ Ft = (Fa * Fc) + Fb; }});
649 28: fmsubs({{ Ft = (Fa * Fc) - Fb; }});
650 31: fnmadds({{ Ft = -((Fa * Fc) + Fb); }});
651 30: fnmsubs({{ Ft = -((Fa * Fc) - Fb); }});
656 format FloatArithOp {
657 21: fadd({{ Ft = Fa + Fb; }});
658 20: fsub({{ Ft = Fa - Fb; }});
659 25: fmul({{ Ft = Fa * Fc; }});
660 18: fdiv({{ Ft = Fa / Fb; }});
661 29: fmadd({{ Ft = (Fa * Fc) + Fb; }});
662 28: fmsub({{ Ft = (Fa * Fc) - Fb; }});
663 31: fnmadd({{ Ft = -((Fa * Fc) + Fb); }});
664 30: fnmsub({{ Ft = -((Fa * Fc) - Fb); }});
667 default: decode X_XO {
668 format FloatRCCheckOp {
669 72: fmr({{ Ft = Fb; }});
672 Ft_ud = insertBits(Ft_ud, 63, 0); }});
675 Ft_ud = insertBits(Ft_ud, 63, 1); }});
676 40: fneg({{ Ft = -Fb; }});
679 Ft_ud = insertBits(Ft_ud, 63, Fa_ud<63:63>);
683 format FloatConvertOp {
684 12: frsp({{ Ft_sf = Fb; }});
685 15: fctiwz({{ Ft_sw = (int32_t)trunc(Fb); }});
690 uint32_t c = makeCRField(Fa, Fb);
694 CR = insertCRField(CR, BF, c);
698 format FloatRCCheckOp {
699 583: mffs({{ Ft_ud = FPSCR; }});
701 FPSCR = insertCRField(FPSCR, BF + (8 * (1 - W_FIELD)),
704 70: mtfsb0({{ FPSCR = insertBits(FPSCR, 31 - BT, 0); }});
705 38: mtfsb1({{ FPSCR = insertBits(FPSCR, 31 - BT, 1); }});
707 default: decode XFL_XO {
709 if (L_FIELD == 1) { FPSCR = Fb_ud; }
711 for (int i = 0; i < 8; ++i) {
712 if (bits(FLM, i) == 1) {
713 int k = 4 * (i + (8 * (1 - W_FIELD)));
714 FPSCR = insertBits(FPSCR, k + 3, k,
715 bits(Fb_ud, k + 3, k));