Debian Codegraph @

asm/jump.go

raw symbols SCIP
1 package asm
2
3 //go:generate go tool stringer -output jump_string.go -type=JumpOp
4
5 // JumpOp affect control flow.
6 //
7 // msb lsb
8 // +----+-+---+
9 // |OP |s|cls|
10 // +----+-+---+
11 type JumpOp uint8
12
13 const jumpMask OpCode = 0xf0
14
15 const (
16 // InvalidJumpOp is returned by getters when invoked
17 // on non branch OpCodes
18 InvalidJumpOp JumpOp = 0xff
19 // Ja jumps by offset unconditionally
20 Ja JumpOp = 0x00
21 // JEq jumps by offset if r == imm
22 JEq JumpOp = 0x10
23 // JGT jumps by offset if r > imm
24 JGT JumpOp = 0x20
25 // JGE jumps by offset if r >= imm
26 JGE JumpOp = 0x30
27 // JSet jumps by offset if r & imm
28 JSet JumpOp = 0x40
29 // JNE jumps by offset if r != imm
30 JNE JumpOp = 0x50
31 // JSGT jumps by offset if signed r > signed imm
32 JSGT JumpOp = 0x60
33 // JSGE jumps by offset if signed r >= signed imm
34 JSGE JumpOp = 0x70
35 // Call builtin or user defined function from imm
36 Call JumpOp = 0x80
37 // Exit ends execution, with value in r0
38 Exit JumpOp = 0x90
39 // JLT jumps by offset if r < imm
40 JLT JumpOp = 0xa0
41 // JLE jumps by offset if r <= imm
42 JLE JumpOp = 0xb0
43 // JSLT jumps by offset if signed r < signed imm
44 JSLT JumpOp = 0xc0
45 // JSLE jumps by offset if signed r <= signed imm
46 JSLE JumpOp = 0xd0
47 // JCOND is a conditional pseudo jump to encode the may_goto instruction
48 JCOND JumpOp = 0xe0
49 )
50
51 // Return emits an exit instruction.
52 //
53 // Requires a return value in R0.
54 func Return() Instruction {
55 return Instruction{
56 OpCode: OpCode(JumpClass).SetJumpOp(Exit),
57 }
58 }
59
60 // Op returns the OpCode for a given jump source.
61 func (op JumpOp) Op(source Source) OpCode {
62 return OpCode(JumpClass).SetJumpOp(op).SetSource(source)
63 }
64
65 // Imm compares 64 bit dst to 64 bit value (sign extended), and adjusts PC by offset if the condition is fulfilled.
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 // Imm32 compares 32 bit dst to 32 bit value, and adjusts PC by offset if the condition is fulfilled.
76 // Requires kernel 5.1.
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 // Reg compares 64 bit dst to 64 bit src, and adjusts PC by offset if the condition is fulfilled.
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 // Reg32 compares 32 bit dst to 32 bit src, and adjusts PC by offset if the condition is fulfilled.
97 // Requires kernel 5.1.
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 // LongJump returns a jump always instruction with a range of [-2^31, 2^31 - 1].
116 func LongJump(label string) Instruction {
117 return Instruction{
118 OpCode: Ja.opCode(Jump32Class, ImmSource),
119 Constant: -1,
120 }.WithReference(label)
121 }
122
123 // Label adjusts PC to the address of the label.
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 }