|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | + |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from src.arch.z80.backend import Quad |
| 7 | +from src.arch.z80.backend._8bit import _8bit_oper, int8 |
| 8 | +from src.arch.z80.backend.common import _int_ops |
| 9 | + |
| 10 | + |
| 11 | +def _mul8(ins: Quad) -> List[str]: |
| 12 | + """Multiplies 2 las values from the stack. |
| 13 | +
|
| 14 | + Optimizations: |
| 15 | + * If any of the ops is ZERO, |
| 16 | + then do A = 0 ==> XOR A, cause A * 0 = 0 * A = 0 |
| 17 | +
|
| 18 | + * If any ot the ops is ONE, do NOTHING |
| 19 | + A * 1 = 1 * A = A |
| 20 | + """ |
| 21 | + |
| 22 | + op1, op2 = tuple(ins.quad[2:]) |
| 23 | + if _int_ops(op1, op2) is not None: |
| 24 | + op1, op2 = _int_ops(op1, op2) |
| 25 | + |
| 26 | + output = _8bit_oper(op1) |
| 27 | + |
| 28 | + if op2 == 0: |
| 29 | + output.append("xor a") |
| 30 | + output.append("push af") |
| 31 | + return output |
| 32 | + |
| 33 | + if op2 == 1: # A * 1 = 1 * A = A |
| 34 | + output.append("push af") |
| 35 | + return output |
| 36 | + |
| 37 | + if op2 == 2: # A * 2 == A SLA 1 |
| 38 | + output.append("add a, a") |
| 39 | + output.append("push af") |
| 40 | + return output |
| 41 | + |
| 42 | + if op2 == 4: # A * 4 == A SLA 2 |
| 43 | + output.append("add a, a") |
| 44 | + output.append("add a, a") |
| 45 | + output.append("push af") |
| 46 | + return output |
| 47 | + |
| 48 | + output.append("ld h, %i" % int8(op2)) |
| 49 | + else: |
| 50 | + if op2[0] == "_": # stack optimization |
| 51 | + op1, op2 = op2, op1 |
| 52 | + |
| 53 | + output = _8bit_oper(op1, op2) |
| 54 | + |
| 55 | + output.append("ld d, h") # Immediate |
| 56 | + output.append("ld e, a") |
| 57 | + output.append("mul d, e") |
| 58 | + output.append("ld a, e") |
| 59 | + output.append("push af") |
| 60 | + return output |
0 commit comments