forked from rasky/gojit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassembler.go
More file actions
137 lines (119 loc) · 2.47 KB
/
assembler.go
File metadata and controls
137 lines (119 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Package amd64 implements a simple amd64 assembler.
package gojit
import (
"errors"
)
var ErrBufferTooSmall = errors.New("buffer is too small")
// Assembler implements a simple amd64 assembler. All methods on
// Assembler will emit code to Buf[Off:] and advances Off. Buf will
// never be reallocated, and attempts to assemble off the end of Buf
// will panic.
type Assembler struct {
Buf []byte
Off int
err error
}
func New(size int) (*Assembler, error) {
buf, e := Alloc(size)
if e != nil || len(buf) == 0{
return nil, e
}
return &Assembler{Buf: buf}, nil
}
func (a *Assembler) Release() {
Release(a.Buf)
}
func (a *Assembler) Error() error {
err := a.err
a.err = nil
return err
}
func (a *Assembler) byte(b byte) {
if a.Off+1 > len(a.Buf) {
a.err = ErrBufferTooSmall
return
}
a.Buf[a.Off] = b
a.Off++
}
func (a *Assembler) bytes(bs []byte) {
for _, b := range bs {
a.byte(b)
}
}
func (a *Assembler) int16(i uint16) {
if a.Off+2 > len(a.Buf) {
a.err = ErrBufferTooSmall
return
}
a.Buf[a.Off] = byte(i & 0xFF)
a.Buf[a.Off+1] = byte(i >> 8)
a.Off += 2
}
func (a *Assembler) int32(i uint32) {
if a.Off+4 > len(a.Buf) {
a.err = ErrBufferTooSmall
return
}
a.Buf[a.Off] = byte(i & 0xFF)
a.Buf[a.Off+1] = byte(i >> 8)
a.Buf[a.Off+2] = byte(i >> 16)
a.Buf[a.Off+3] = byte(i >> 24)
a.Off += 4
}
func (a *Assembler) int64(i uint64) {
if a.Off+8 > len(a.Buf) {
a.err = ErrBufferTooSmall
return
}
a.Buf[a.Off] = byte(i & 0xFF)
a.Buf[a.Off+1] = byte(i >> 8)
a.Buf[a.Off+2] = byte(i >> 16)
a.Buf[a.Off+3] = byte(i >> 24)
a.Buf[a.Off+4] = byte(i >> 32)
a.Buf[a.Off+5] = byte(i >> 40)
a.Buf[a.Off+6] = byte(i >> 48)
a.Buf[a.Off+7] = byte(i >> 56)
a.Off += 8
}
func (a *Assembler) rel32(addr uintptr) {
off := uintptr(addr) - Addr(a.Buf[a.Off:]) - 4
if uintptr(int32(off)) != off {
panic("call rel: target out of range")
}
a.int32(uint32(off))
}
func (a *Assembler) rex(w, r, x, b bool) {
var bits byte
if w {
bits |= REXW
}
if r {
bits |= REXR
}
if x {
bits |= REXX
}
if b {
bits |= REXB
}
if bits != 0 {
a.byte(PFX_REX | bits)
}
}
func (a *Assembler) rexBits(lsize, rsize byte, r, x, b bool) {
if lsize != 0 && rsize != 0 && lsize != rsize {
panic("mismatched instruction sizes")
}
lsize = lsize | rsize
if lsize == 0 {
lsize = 64
}
a.rex(lsize == 64, r, x, b)
}
func (a *Assembler) modrm(mod, reg, rm byte) {
a.byte((mod << 6) | (reg << 3) | rm)
}
func (a *Assembler) sib(s, i, b byte) {
a.byte((s << 6) | (i << 3) | b)
}