Skip to content

Commit

Permalink
freestanding: init consts
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman authored and medvednikov committed Dec 3, 2019
1 parent 590566f commit eb20dd3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions vlib/builtin/bare/.checks/checks.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn vcheck(vfile string) {
fn main() {
vcheck("string")
vcheck("linuxsys")
vcheck("consts")
exit(0)
}

15 changes: 15 additions & 0 deletions vlib/builtin/bare/.checks/consts/consts.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module main

const (
integer1 = 111
integer2 = 222
integer3 = 333
integer9 = integer3 * 3
abc = "123"
)

fn main(){
assert abc == "123"
assert integer9 == 999
println("constants are properly initialized")
}
4 changes: 4 additions & 0 deletions vlib/builtin/bare/builtin_bare.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module builtin

// called by the generated main/init
fn init() {
}

pub fn isnil(p voidptr) bool {
return p == 0
}
Expand Down
19 changes: 16 additions & 3 deletions vlib/compiler/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ fn (v mut V) generate_init() {
}
}
consts_init_body := v.cgen.consts_init.join_lines()

if v.pref.is_bare {
// vlib can't have init_consts()
v.cgen.genln('
void init() {
$call_mod_init_consts
$consts_init_body
builtin__init();
$call_mod_init
}
')
}

if !v.pref.is_bare {
// vlib can't have `init_consts()`
v.cgen.genln('void init() {
Expand Down Expand Up @@ -510,9 +523,9 @@ pub fn (v mut V) generate_main() {

pub fn (v mut V) gen_main_start(add_os_args bool){
v.cgen.genln('int main(int argc, char** argv) { ')
if !v.pref.is_bare {
v.cgen.genln(' init();')
}
v.cgen.genln(' init();')
if add_os_args && 'os' in v.table.imports {
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/bare/bare.S
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pop rdi
mov rsi,rsp
and rsp,-16
call main__main
call main

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

0 comments on commit eb20dd3

Please sign in to comment.