| 1 |
package asm |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type JumpOp uint8 |
| 12 |
|
| 13 |
const jumpMask OpCode = 0xf0 |
| 14 |
|
| 15 |
const ( |
| 16 |
|
| 17 |
|
| 18 |
InvalidJumpOp JumpOp = 0xff |
| 19 |
|
| 20 |
Ja JumpOp = 0x00 |
| 21 |
|
| 22 |
JEq JumpOp = 0x10 |
| 23 |
|
| 24 |
JGT JumpOp = 0x20 |
| 25 |
|
| 26 |
JGE JumpOp = 0x30 |
| 27 |
|
| 28 |
JSet JumpOp = 0x40 |
| 29 |
|
| 30 |
JNE JumpOp = 0x50 |
| 31 |
|
| 32 |
JSGT JumpOp = 0x60 |
| 33 |
|
| 34 |
JSGE JumpOp = 0x70 |
| 35 |
|
| 36 |
Call JumpOp = 0x80 |
| 37 |
|
| 38 |
Exit JumpOp = 0x90 |
| 39 |
|
| 40 |
JLT JumpOp = 0xa0 |
| 41 |
|
| 42 |
JLE JumpOp = 0xb0 |
| 43 |
|
| 44 |
JSLT JumpOp = 0xc0 |
| 45 |
|
| 46 |
JSLE JumpOp = 0xd0 |
| 47 |
|
| 48 |
JCOND JumpOp = 0xe0 |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func Return() Instruction { |
| 55 |
return Instruction{ |
| 56 |
OpCode: OpCode(JumpClass).SetJumpOp(Exit), |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
func (op JumpOp) Op(source Source) OpCode { |
| 62 |
return OpCode(JumpClass).SetJumpOp(op).SetSource(source) |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func (op JumpOp) Imm(dst Register, value int32, label string) Instruction { |
| 67 |
return Instruction{ |
| 68 |
OpCode: op.opCode(JumpClass, ImmSource), |
| 69 |
Dst: dst, |
| 70 |
Offset: -1, |
| 71 |
Constant: int64(value), |
| 72 |
}.WithReference(label) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (op JumpOp) Imm32(dst Register, value int32, label string) Instruction { |
| 78 |
return Instruction{ |
| 79 |
OpCode: op.opCode(Jump32Class, ImmSource), |
| 80 |
Dst: dst, |
| 81 |
Offset: -1, |
| 82 |
Constant: int64(value), |
| 83 |
}.WithReference(label) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (op JumpOp) Reg(dst, src Register, label string) Instruction { |
| 88 |
return Instruction{ |
| 89 |
OpCode: op.opCode(JumpClass, RegSource), |
| 90 |
Dst: dst, |
| 91 |
Src: src, |
| 92 |
Offset: -1, |
| 93 |
}.WithReference(label) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
func (op JumpOp) Reg32(dst, src Register, label string) Instruction { |
| 99 |
return Instruction{ |
| 100 |
OpCode: op.opCode(Jump32Class, RegSource), |
| 101 |
Dst: dst, |
| 102 |
Src: src, |
| 103 |
Offset: -1, |
| 104 |
}.WithReference(label) |
| 105 |
} |
| 106 |
|
| 107 |
func (op JumpOp) opCode(class Class, source Source) OpCode { |
| 108 |
if op == Exit || op == Call { |
| 109 |
return InvalidOpCode |
| 110 |
} |
| 111 |
|
| 112 |
return OpCode(class).SetJumpOp(op).SetSource(source) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
func LongJump(label string) Instruction { |
| 117 |
return Instruction{ |
| 118 |
OpCode: Ja.opCode(Jump32Class, ImmSource), |
| 119 |
Constant: -1, |
| 120 |
}.WithReference(label) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (op JumpOp) Label(label string) Instruction { |
| 125 |
if op == Call { |
| 126 |
return Instruction{ |
| 127 |
OpCode: OpCode(JumpClass).SetJumpOp(Call), |
| 128 |
Src: PseudoCall, |
| 129 |
Constant: -1, |
| 130 |
}.WithReference(label) |
| 131 |
} |
| 132 |
|
| 133 |
return Instruction{ |
| 134 |
OpCode: OpCode(JumpClass).SetJumpOp(op), |
| 135 |
Offset: -1, |
| 136 |
}.WithReference(label) |
| 137 |
} |