...
1 package asm
2
3
4
5
6
7
8
9
10
11 type Source uint8
12
13 const sourceMask OpCode = 0x08
14
15
16 const (
17
18
19 InvalidSource Source = 0xff
20
21 ImmSource Source = 0x00
22
23 RegSource Source = 0x08
24 )
25
26
27 type Endianness uint8
28
29 const endianMask = sourceMask
30
31
32 const (
33 InvalidEndian Endianness = 0xff
34
35 LE Endianness = 0x00
36
37 BE Endianness = 0x08
38 )
39
40
41
42
43
44
45
46 type ALUOp uint8
47
48 const aluMask OpCode = 0xf0
49
50 const (
51
52
53 InvalidALUOp ALUOp = 0xff
54
55 Add ALUOp = 0x00
56
57 Sub ALUOp = 0x10
58
59 Mul ALUOp = 0x20
60
61 Div ALUOp = 0x30
62
63 Or ALUOp = 0x40
64
65 And ALUOp = 0x50
66
67 LSh ALUOp = 0x60
68
69 RSh ALUOp = 0x70
70
71 Neg ALUOp = 0x80
72
73 Mod ALUOp = 0x90
74
75 Xor ALUOp = 0xa0
76
77 Mov ALUOp = 0xb0
78
79 ArSh ALUOp = 0xc0
80
81 Swap ALUOp = 0xd0
82 )
83
84
85 func HostTo(endian Endianness, dst Register, size Size) Instruction {
86 var imm int64
87 switch size {
88 case Half:
89 imm = 16
90 case Word:
91 imm = 32
92 case DWord:
93 imm = 64
94 default:
95 return Instruction{OpCode: InvalidOpCode}
96 }
97
98 return Instruction{
99 OpCode: OpCode(ALUClass).SetALUOp(Swap).SetSource(Source(endian)),
100 Dst: dst,
101 Constant: imm,
102 }
103 }
104
105
106 func (op ALUOp) Op(source Source) OpCode {
107 return OpCode(ALU64Class).SetALUOp(op).SetSource(source)
108 }
109
110
111 func (op ALUOp) Reg(dst, src Register) Instruction {
112 return Instruction{
113 OpCode: op.Op(RegSource),
114 Dst: dst,
115 Src: src,
116 }
117 }
118
119
120 func (op ALUOp) Imm(dst Register, value int32) Instruction {
121 return Instruction{
122 OpCode: op.Op(ImmSource),
123 Dst: dst,
124 Constant: int64(value),
125 }
126 }
127
128
129 func (op ALUOp) Op32(source Source) OpCode {
130 return OpCode(ALUClass).SetALUOp(op).SetSource(source)
131 }
132
133
134 func (op ALUOp) Reg32(dst, src Register) Instruction {
135 return Instruction{
136 OpCode: op.Op32(RegSource),
137 Dst: dst,
138 Src: src,
139 }
140 }
141
142
143 func (op ALUOp) Imm32(dst Register, value int32) Instruction {
144 return Instruction{
145 OpCode: op.Op32(ImmSource),
146 Dst: dst,
147 Constant: int64(value),
148 }
149 }
150
View as plain text