Skip to content

Commit eb20dd3

Browse files
spythemanmedvednikov
authored andcommitted
freestanding: init consts
1 parent 590566f commit eb20dd3

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

vlib/builtin/bare/.checks/checks.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn vcheck(vfile string) {
2525
fn main() {
2626
vcheck("string")
2727
vcheck("linuxsys")
28+
vcheck("consts")
2829
exit(0)
2930
}
3031

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module main
2+
3+
const (
4+
integer1 = 111
5+
integer2 = 222
6+
integer3 = 333
7+
integer9 = integer3 * 3
8+
abc = "123"
9+
)
10+
11+
fn main(){
12+
assert abc == "123"
13+
assert integer9 == 999
14+
println("constants are properly initialized")
15+
}

vlib/builtin/bare/builtin_bare.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module builtin
22

3+
// called by the generated main/init
4+
fn init() {
5+
}
6+
37
pub fn isnil(p voidptr) bool {
48
return p == 0
59
}

vlib/compiler/main.v

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ fn (v mut V) generate_init() {
395395
}
396396
}
397397
consts_init_body := v.cgen.consts_init.join_lines()
398+
399+
if v.pref.is_bare {
400+
// vlib can't have init_consts()
401+
v.cgen.genln('
402+
void init() {
403+
$call_mod_init_consts
404+
$consts_init_body
405+
builtin__init();
406+
$call_mod_init
407+
}
408+
')
409+
}
410+
398411
if !v.pref.is_bare {
399412
// vlib can't have `init_consts()`
400413
v.cgen.genln('void init() {
@@ -510,9 +523,9 @@ pub fn (v mut V) generate_main() {
510523

511524
pub fn (v mut V) gen_main_start(add_os_args bool){
512525
v.cgen.genln('int main(int argc, char** argv) { ')
513-
if !v.pref.is_bare {
514-
v.cgen.genln(' init();')
515-
}
526+
527+
v.cgen.genln(' init();')
528+
516529
if add_os_args && 'os' in v.table.imports {
517530
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
518531
}

vlib/os/bare/bare.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
pop rdi
88
mov rsi,rsp
99
and rsp,-16
10-
call main__main
10+
call main
1111

1212
mov rdi,rax /* syscall param 1 = rax (ret value of main) */
1313
mov rax,60 /* SYS_exit */

0 commit comments

Comments
 (0)