native: native prints (#11426)

pull/11438/head
pancake 2021-09-07 14:44:15 +02:00 committed by GitHub
parent 0b6d585b07
commit ddc62ab0d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 2 deletions

View File

@ -763,6 +763,7 @@ pub fn (mut g Gen) call_fn(node ast.CallExpr) {
eprintln('call fn ($n)') eprintln('call fn ($n)')
addr := g.fn_addr[n] addr := g.fn_addr[n]
if addr == 0 { if addr == 0 {
// g.warning('fn addr of `$name` = 0')
g.n_error('fn addr of `$name` = 0') g.n_error('fn addr of `$name` = 0')
} }
// Copy values to registers (calling convention) // Copy values to registers (calling convention)

View File

@ -95,6 +95,10 @@ pub fn gen(files []&ast.File, table &ast.Table, out_name string, pref &pref.Pref
return g.nlines, g.buf.len return g.nlines, g.buf.len
} }
pub fn (mut g Gen) typ(a int) &ast.TypeSymbol {
return &g.table.type_symbols[a]
}
pub fn (mut g Gen) generate_header() { pub fn (mut g Gen) generate_header() {
match g.pref.os { match g.pref.os {
.macos { .macos {
@ -246,10 +250,103 @@ pub fn (mut g Gen) gen_print_from_expr(expr ast.Expr, name string) {
g.expr(expr) g.expr(expr)
g.gen_print_reg(.rax, 3, fd) g.gen_print_reg(.rax, 3, fd)
} }
ast.IntegerLiteral {
g.mov64(.rax, g.allocate_string('$expr.val\n', 2))
g.gen_print_reg(.rax, 3, fd)
}
ast.BoolLiteral {
// register 'true' and 'false' strings // g.expr(expr)
if expr.val {
g.mov64(.rax, g.allocate_string('true', 2))
} else {
g.mov64(.rax, g.allocate_string('false', 2))
}
g.gen_print_reg(.rax, 3, fd)
}
ast.SizeOf {}
ast.OffsetOf {
styp := g.typ(expr.struct_type)
field_name := expr.field
if styp.kind == .struct_ {
s := styp.info as ast.Struct
ptrsz := 4 // should be 8, but for locals is used 8 and C backend shows that too
mut off := 0
for f in s.fields {
if f.name == field_name {
g.mov64(.rax, g.allocate_string('$off\n', 2))
g.gen_print_reg(.rax, 3, fd)
break
}
off += ptrsz
}
} else {
g.v_error('_offsetof expects a struct Type as first argument', expr.pos)
}
}
ast.None {}
ast.PostfixExpr {}
ast.PrefixExpr {}
ast.SelectorExpr {
// struct.field
g.expr(expr)
g.gen_print_reg(.rax, 3, fd)
/*
field_name := expr.field_name
g.expr
if expr.is_mut {
// mutable field access (rw)
}
*/
dump(expr)
g.v_error('struct.field selector not yet implemented for this backend', expr.pos)
}
/*
ast.AnonFn {}
ast.ArrayDecompose {}
ast.ArrayInit {}
ast.AsCast {}
ast.Assoc {}
ast.AtExpr {}
ast.CTempVar {}
ast.CastExpr {}
ast.ChanInit {}
ast.CharLiteral {}
ast.Comment {}
ast.ComptimeCall {}
ast.ComptimeSelector {}
ast.ConcatExpr {}
ast.DumpExpr {}
ast.EmptyExpr {}
ast.EnumVal {}
ast.FloatLiteral {}
ast.GoExpr {}
ast.IfExpr {}
ast.IfGuardExpr {}
ast.IndexExpr {}
ast.InfixExpr {}
ast.IsRefType {}
ast.Likely {}
ast.LockExpr {}
ast.MapInit {}
ast.MatchExpr {}
ast.NodeError {}
ast.OrExpr {}
ast.ParExpr {}
ast.RangeExpr {}
ast.SelectExpr {}
ast.SqlExpr {}
ast.StringInterLiteral {}
ast.StructInit {}
ast.TypeNode {}
ast.TypeOf {}
ast.UnsafeExpr {}
*/
else { else {
dump(typeof(expr).name) dump(typeof(expr).name)
dump(expr) dump(expr)
g.n_error('expected string as argument for print') // g.v_error('expected string as argument for print', expr.pos)
g.n_error('expected string as argument for print') // , expr.pos)
// g.warning('expected string as argument for print')
} }
} }
} }
@ -386,9 +483,10 @@ fn (mut g Gen) stmt(node ast.Stmt) {
ast.AssertStmt { ast.AssertStmt {
g.gen_assert(node) g.gen_assert(node)
} }
ast.Import {} // do nothing here
ast.StructDecl {} ast.StructDecl {}
else { else {
println('native.stmt(): bad node: ' + node.type_name()) eprintln('native.stmt(): bad node: ' + node.type_name())
} }
} }
} }

View File

@ -1,8 +1,29 @@
struct Foo {
a int
b int
c int
}
fn test_stdout() { fn test_stdout() {
print('Hello ') print('Hello ')
println('World') println('World')
} }
fn test_booleans() {
println(true)
println(false)
}
fn test_numbers() {
println(123)
}
fn test_oof() {
println(__offsetof(Foo, a))
println(__offsetof(Foo, b))
println(__offsetof(Foo, c))
}
fn test_stderr() { fn test_stderr() {
eprint('2(Hello)') eprint('2(Hello)')
eprintln('2(World)') eprintln('2(World)')
@ -11,4 +32,6 @@ fn test_stderr() {
fn main() { fn main() {
test_stdout() test_stdout()
test_stderr() test_stderr()
test_numbers()
test_oof()
} }

View File

@ -1 +1,5 @@
Hello World Hello World
123
0
4
8