Skip to content

Commit

Permalink
native: fix small issues noted in vlang#19498
Browse files Browse the repository at this point in the history
  • Loading branch information
Spydr06 committed Oct 4, 2023
1 parent 5f223c3 commit 4183108
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 41 deletions.
7 changes: 7 additions & 0 deletions vlib/v/gen/native/amd64.v
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,13 @@ fn (mut c Amd64) call(addr int) i64 {
return c_addr
}

fn (mut c Amd64) patch_relative_jmp(pos int, addr i64) {
// Update jmp or cjmp address.
// The value is the relative address, difference between current position and the location
// after `jxx 00 00 00 00`
c.g.write32_at(pos, int(addr - pos - 4))
}

fn (mut c Amd64) extern_call(addr int) {
match c.g.pref.os {
.linux {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/gen/native/arm64.v
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,7 @@ pub fn (mut c Arm64) add(r Register, val int) {
fn (mut c Arm64) mov_deref(reg Register, regptr Register, typ ast.Type) {
panic('Arm64.mov_deref() not implemented')
}

fn (mut c Arm64) patch_relative_jmp(pos int, addr i64) {
panic('Arm64.patch_relative_jmp() not implemented')
}
24 changes: 3 additions & 21 deletions vlib/v/gen/native/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,12 @@ fn (mut g Gen) comptime_at(node ast.AtExpr) string {
}

fn (mut g Gen) comptime_conditional(node ast.IfExpr) ?ast.IfBranch {
if node.branches.len == 0 {
return none
}

for i, branch in node.branches {
// handle $else branch, which does not have a condition
if (node.has_else && i + 1 == node.branches.len) || g.comptime_is_truthy(branch.cond) {
return branch
}
}

return none
return node.branches.filter((node.has_else && it == node.branches.last())
|| g.comptime_is_truthy(it.cond))[0] or { return none }
}

fn (mut g Gen) should_emit_hash_stmt(node ast.HashStmt) bool {
if node.ct_conds.len == 0 {
return true
}

mut emit := true
for cond in node.ct_conds {
emit = emit && g.comptime_is_truthy(cond)
}
return emit
return node.ct_conds.all(g.comptime_is_truthy(it))
}

fn (mut g Gen) comptime_is_truthy(cond ast.Expr) bool {
Expand Down
31 changes: 14 additions & 17 deletions vlib/v/gen/native/gen.v
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ mut:
mov(r Register, val int)
mov64(r Register, val i64)
movabs(reg Register, val i64)
patch_relative_jmp(pos int, addr i64)
prefix_expr(node ast.PrefixExpr)
push(r Register)
ret()
Expand Down Expand Up @@ -428,17 +429,17 @@ pub fn (mut g Gen) has_external_deps() bool {
return g.extern_symbols.len != 0
}

pub fn (mut g Gen) ast_fetch_external_deps() bool {
pub fn (mut g Gen) ast_fetch_external_deps() {
for file in g.files {
g.current_file = file
walker.inspect(file, unsafe { &mut g }, node_fetch_external_deps)
}

return g.has_external_deps()
g.requires_linking = g.has_external_deps()
}

pub fn (mut g Gen) generate_header() {
g.requires_linking = g.ast_fetch_external_deps()
g.ast_fetch_external_deps()

match g.pref.os {
.macos {
Expand Down Expand Up @@ -482,13 +483,7 @@ pub fn (mut g Gen) create_executable() {
os.write_file_array(obj_name, g.buf) or { panic(err) }

if g.requires_linking {
match g.pref.os {
// TEMPORARY
.linux { // TEMPORARY
g.link(obj_name)
} // TEMPORARY
else {} // TEMPORARY
} // TEMPORARY
g.link(obj_name)
}

os.chmod(g.out_name, 0o775) or { panic(err) } // make it executable
Expand Down Expand Up @@ -524,6 +519,12 @@ pub fn (mut g Gen) link(obj_name string) {
.linux {
g.link_elf_file(obj_name)
}
.windows {
// windows linking is alredy done before codegen
}
.macos {
// TODO: implement linking for macos!
}
else {
g.n_error('native linking is not implemented for ${g.pref.os}')
}
Expand Down Expand Up @@ -564,9 +565,7 @@ pub fn (g &Gen) pos() i64 {
}

fn (mut g Gen) write(bytes []u8) {
for _, b in bytes {
g.buf << b
}
g.buf << bytes
}

fn (mut g Gen) write8(n int) {
Expand Down Expand Up @@ -1034,10 +1033,8 @@ fn (mut g Gen) patch_labels() {
g.n_error('label addr = 0')
return
}
// Update jmp or cjmp address.
// The value is the relative address, difference between current position and the location
// after `jxx 00 00 00 00`
g.write32_at(label.pos, int(addr - label.pos - 4))

g.code_gen.patch_relative_jmp(label.pos, addr)
}
}

Expand Down
5 changes: 2 additions & 3 deletions vlib/v/gen/native/stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ fn (mut g Gen) stmt(node ast.Stmt) {

match node.kind {
'include', 'preinclude', 'define', 'insert' {
g.warning('#${node.kind} is not supported with the native backend',
g.v_error('#${node.kind} is not supported with the native backend',
node.pos)
// TODO: replace with error once issues with builtin are resolved
}
'flag' {
// flags are already handled when dispatching extern dependencies
// do nothing; flags are already handled when dispatching extern dependencies
}
else {
g.gen_native_hash_stmt(node)
Expand Down

0 comments on commit 4183108

Please sign in to comment.