-
Notifications
You must be signed in to change notification settings - Fork 1
/
jit.c.v
145 lines (126 loc) · 2.83 KB
/
jit.c.v
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
138
139
140
141
142
143
144
145
import term
import strings
$if !freestanding {
#include <sys/mman.h>
#include <unistd.h>
}
fn C.sysconf(name int) i64
fn C.mmap(base voidptr, len usize, prot int, flags int, fd int, offset i64) voidptr
fn C.munmap(ptr voidptr, size usize) int
fn C.mprotect(addr voidptr, len usize, prot int) int
struct Comment {
pos int
mut:
comment string
}
struct JitProgram {
mut:
code []u8
comments []Comment
}
fn create_program() JitProgram {
len := C.sysconf(C._SC_PAGESIZE)
if len == -1 {
panic("create_program: sysconf() failed")
}
ptr := C.mmap(0, len, C.PROT_READ | C.PROT_WRITE, C.MAP_ANONYMOUS | C.MAP_PRIVATE, -1, 0)
if ptr == -1 {
panic("create_program: mmap() failed")
}
code := unsafe {
array {
element_size: 1
data: ptr
cap: int(len)
flags: .noshrink | .nogrow | .nofree // i added `.nogrow | .nofree` !! :)
}
}
return JitProgram {
code: code
}
}
fn (mut p JitProgram) comment(comment string) {
p.comments << Comment {
comment: comment
pos: p.code.len
}
}
fn (mut p JitProgram) reset() {
if C.mprotect(p.code.data, p.code.cap, C.PROT_READ | C.PROT_WRITE) != 0 {
panic("JitProgram.reset: mprotect() failed")
}
p.code.clear()
p.comments.clear()
}
type JitProgramTyp = fn () i64
fn (mut p JitProgram) finalise() JitProgramTyp {
if C.mprotect(p.code.data, p.code.cap, C.PROT_READ | C.PROT_EXEC) != 0 {
panic("JitProgram.finalise: mprotect() failed")
}
return unsafe { JitProgramTyp(p.code.data) }
}
fn (mut p JitProgram) free() {
if C.munmap(p.code.data, p.code.cap) != 0 {
panic("JitProgram.free: munmap() failed")
}
}
fn (mut p JitProgram) hexdump() {
maxsize := 40
mut sb := strings.new_builder(80)
mut c := 1
for idx, v in p.code {
sb.write_string(v.hex())
sb.write_u8(` `)
if c < p.comments.len && p.comments[c].pos - 1 == idx {
if sb.len < maxsize {
sb.write_string(` `.repeat(maxsize - sb.len))
}
print(term.dim(sb.str()))
println(term.bold(p.comments[c - 1].comment))
c++
}
}
if c - 1 < p.comments.len {
if sb.len < maxsize {
sb.write_string(` `.repeat(maxsize - sb.len))
}
print(term.dim(sb.str()))
println(term.bold(p.comments[c - 1].comment))
}
}
fn (mut p JitProgram) ret() {
p.comment('ret')
p.code << 0xc3
}
fn (mut p JitProgram) write64(n i64) {
p.code << u8(n)
p.code << u8(n >> 8)
p.code << u8(n >> 16)
p.code << u8(n >> 24)
p.code << u8(n >> 32)
p.code << u8(n >> 40)
p.code << u8(n >> 48)
p.code << u8(n >> 56)
}
fn (mut p JitProgram) mov64_rax(val i64) {
if val == 0 {
p.comment('xor rax, rax')
p.code << [u8(0x48), 0x31, 0xC0]
} else {
p.comment('mov rax, ${val}')
p.code << 0x48
p.code << 0xb8
p.write64(val)
}
}
fn (mut p JitProgram) mov64_rcx(val i64) {
if val == 0 {
p.comment('xor rcx, rcx')
p.code << [u8(0x48), 0x31, 0xC9]
} else {
p.comment('mov rcx, ${val}')
p.code << 0x48
p.code << 0xb9
p.write64(val)
}
}