native: fix tests, address comments and a little cleaner macho

pull/9900/head
pancake 2021-04-27 00:09:37 +02:00 committed by Delyan Angelov
parent 5be41ebc80
commit d8bb939072
9 changed files with 72 additions and 39 deletions

View File

@ -2072,9 +2072,8 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type {
}
if !found && c.pref.backend == .native {
if fn_name in native.builtins {
found = true
c.table.fns[fn_name].usages++
return ast.string_type
return ast.void_type
}
}
if !found && c.pref.is_script && !found {

View File

@ -529,7 +529,9 @@ fn (mut g Gen) assign_stmt(node ast.AssignStmt) {
g.allocate_var(name, 4, 0)
// `mov DWORD PTR [rbp-0x8],eax`
offset := g.get_var_offset(name)
println('infix assignment $name offset=$offset.hex2()')
if g.pref.is_verbose {
println('infix assignment $name offset=$offset.hex2()')
}
g.mov_reg_to_rbp(offset, .eax)
}
ast.StructInit {

View File

@ -69,7 +69,6 @@ pub fn (mut g Gen) fn_decl_arm64(node ast.FnDecl) {
}
fn (mut g Gen) gen_arm64_helloworld() {
// ADD THE CODE HERE THIS GOES INTO THE STMTS THING
// g.write32(0x77777777)
// assembly
g.mov_arm(.x0, 1)

View File

@ -106,7 +106,7 @@ pub fn (mut g Gen) create_executable() {
unsafe { f.write_ptr(g.buf.data, g.buf.len) }
f.close()
if g.pref.is_verbose {
println('\narm64 mach-o binary has been successfully generated')
println('\n$g.out_name: native binary has been successfully generated')
}
}

View File

@ -13,6 +13,7 @@ const (
macho_d_size = 0x50
lc_symtab = 0x2
lc_dymsymtab = 0xB
mh_object = 1
)
struct Symbol {
@ -44,9 +45,10 @@ pub fn (mut g Gen) generate_macho_header() {
g.write32(0x01000007) // CPU_TYPE_X64
g.write32(0x00000003) // CPU_SUBTYPE_X64
}
g.write32(0x00000001) // MH_OBJECT
g.write32(0x00000004) // # of load commands
g.write32(0x118) // size of load commands
g.write32(native.mh_object) // MH_OBJECT
text_offset := 0x138
g.write32(4) // # of load commands
g.write32(text_offset - 0x20) // size of load commands // 0x138-0x20
// g.write32(0x00002000) // MH_SUBSECTIONS_VIA_SYMBOLS
g.write32(0) // MH_SUBSECTIONS_VIA_SYMBOLS
g.write32(0) // reserved
@ -56,7 +58,7 @@ pub fn (mut g Gen) generate_macho_header() {
g.zeroes(16) // segment name
g.write64(0) // VM address
g.write64(0x25) // VM size
g.write64(0x138) // file offset
g.write64(text_offset) // file offset
g.write64(0x25) // file size
g.write32(0x7) // max vm protection
g.write32(0x7) // initial vm protection
@ -67,7 +69,7 @@ pub fn (mut g Gen) generate_macho_header() {
g.write_string_with_padding('__TEXT', 16) // segment name
g.write64(0) // address
g.write64(0x25) // size
g.write32(0x138) // offset
g.write32(text_offset) // offset
g.write32(0x4) // alignment
g.write32(0x160) // relocation offset
g.write32(0x1) // # of relocations
@ -97,9 +99,17 @@ pub fn (mut g Gen) generate_macho_header() {
for _ in 0 .. 12 {
g.write32(0)
}
if g.pref.is_verbose {
println('commands size = $g.buf.len')
if g.buf.len != 0x138 {
println('macho: invalid header size')
}
}
if g.pref.arch == .arm64 {
g.gen_arm64_helloworld()
} else {
// do nothing
}
}
@ -168,6 +178,9 @@ pub fn (mut g Gen) zeroes(n int) {
}
fn (mut g Gen) write_relocs() {
if g.pref.is_verbose {
println('relocs at $g.buf.len should be 0x160')
}
g.write32(0x8)
g.write32(0x2d000003)
}

View File

@ -1,14 +1,34 @@
fn print_number(n int) {
if n == 0 { println('0') }
if n == 1 { println('1') }
if n == 2 { println('2') }
if n == 3 { println('3') }
if n == 4 { println('4') }
if n == 5 { println('5') }
if n == 6 { println('6') }
if n == 7 { println('7') }
if n == 8 { println('8') }
if n == 9 { println('9') }
if n == 0 {
println('0')
}
if n == 1 {
println('1')
}
if n == 2 {
println('2')
}
if n == 3 {
println('3')
}
if n == 4 {
println('4')
}
if n == 5 {
println('5')
}
if n == 6 {
println('6')
}
if n == 7 {
println('7')
}
if n == 8 {
println('8')
}
if n == 9 {
println('9')
}
}
struct User {
@ -16,7 +36,6 @@ struct User {
}
fn print_user(u User) {
}
fn test_add() {

View File

@ -6,8 +6,8 @@ fn if_test() {
if b == 2 {
println('b == 2')
}
//if b != 2 {
//println('b != 2')
// if b != 2 {
// println('b != 2')
//}
}
if a == 2 {
@ -35,22 +35,27 @@ fn foo(a int) {
if a == 7 {
println('a == 7')
}
a++
if a == 2 {
println('a == 2')
/*
// native gen doesnt support more than one variable
mut b := a + 1
if b == 2 {
println('b == 2')
}
if a == 3 {
println('a == 3')
if b == 3 {
println('b == 3')
}
if b == 8 {
println('b == 8')
}
*/
}
fn args() {
x := 7
println('===args===')
foo(1)
foo(x)
foo(2)
foo(1) // prints 1 2
foo(x) // prints 7 8
foo(2) // prints 3
}
/*
@ -69,9 +74,8 @@ fn expr() {
}
*/
struct User {
age int
age int
nr_orders int
}
@ -79,5 +83,5 @@ fn main() {
if_test()
loop()
args()
//expr()
// expr()
}

View File

@ -9,9 +9,6 @@ hello
===args===
foo:
a == 1
a == 2
foo:
a == 7
foo:
a == 3

View File

@ -1,4 +1,4 @@
fn print_greeting(){
fn print_greeting() {
println('hello from native V')
}