/
builtins.v
87 lines (74 loc) · 2.38 KB
/
builtins.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
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module native
import term
enum Builtin {
int_to_string
bool_to_string
reverse_string
compare_strings
}
struct BuiltinFn {
body fn (builtin BuiltinFn, mut g Gen) = unsafe { nil }
arg_regs []Register
mut:
calls []i64 // call addresses
}
pub const inline_builtins = ['assert', 'print', 'eprint', 'println', 'eprintln', 'exit', 'C.syscall'] // classic V builtin functios accessible to the user get inlined
pub fn (mut g Gen) init_builtins() {
g.builtins = {
// longer algorithms and internal functions inaccessible to the user
// used to keep executable size small and the bytecode distraction-free
.int_to_string: BuiltinFn{
// 32-bit signed integer to string conversion
body: fn (builtin BuiltinFn, mut g Gen) {
g.code_gen.convert_int_to_string(builtin.arg_regs[0], builtin.arg_regs[1])
}
arg_regs: [Amd64Register.rcx, Amd64Register.rdi]
}
.bool_to_string: BuiltinFn{
body: fn (builtin BuiltinFn, mut g Gen) {
g.code_gen.convert_bool_to_string(builtin.arg_regs[0])
}
arg_regs: [Amd64Register.rax]
}
.reverse_string: BuiltinFn{
body: fn (builtin BuiltinFn, mut g Gen) {
g.code_gen.reverse_string(builtin.arg_regs[0])
}
arg_regs: [Amd64Register.rdi]
}
}
}
pub fn (mut g Gen) generate_builtins() {
for name, builtin in g.builtins {
if builtin.calls.len == 0 { // if a builtin does not get called, do not emit it
continue
}
if g.pref.is_verbose {
println(term.green('\n(builtin) ${name}:'))
}
g.stack_var_pos = 0
call_addr := g.pos()
g.defer_stmts.clear()
g.labels = &LabelTable{}
g.code_gen.builtin_decl(builtin)
g.patch_labels()
// patch all call addresses where this builtin gets called
for call in builtin.calls {
rel := g.code_gen.call_addr_at(int(call_addr), call)
g.write32_at(call + 1, int(rel))
}
}
}
pub fn (mut g Gen) get_builtin_arg_reg(name Builtin, index int) Register {
builtin := g.builtins[name] or { panic('undefined builtin function ${name}') }
if index >= builtin.arg_regs.len {
g.n_error('builtin ${name} does only have ${builtin.arg_regs.len} arguments, requested ${index}')
}
return builtin.arg_regs[index]
}
pub fn (mut g Gen) call_builtin(name Builtin) {
g.builtins[name].calls << g.code_gen.call_builtin(name)
}