mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
x64 fixes
This commit is contained in:
@@ -8,10 +8,10 @@ import (
|
||||
v.types
|
||||
)
|
||||
|
||||
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral |
|
||||
pub type Expr = BinaryExpr | UnaryExpr | IfExpr | StringLiteral | IntegerLiteral |
|
||||
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit
|
||||
|
||||
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt |
|
||||
pub type Stmt = VarDecl | FnDecl | Return | Module | Import | ExprStmt | AssignStmt |
|
||||
ForStmt | StructDecl
|
||||
// Stand-alone expression in a statement list.
|
||||
pub struct ExprStmt {
|
||||
@@ -158,9 +158,9 @@ pub:
|
||||
|
||||
pub struct IfExpr {
|
||||
pub:
|
||||
tok_kind token.Kind
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
tok_kind token.Kind
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
else_stmts []Stmt
|
||||
}
|
||||
|
||||
@@ -168,6 +168,7 @@ pub struct ForStmt {
|
||||
pub:
|
||||
cond Expr
|
||||
stmts []Stmt
|
||||
is_in bool
|
||||
}
|
||||
|
||||
pub struct ReturnStmt {
|
||||
|
||||
@@ -72,7 +72,11 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
}
|
||||
ast.ForStmt {
|
||||
g.write('while (')
|
||||
g.expr(it.cond)
|
||||
if !it.is_in {
|
||||
// `for in` loops don't have a condition
|
||||
println('it cond')
|
||||
g.expr(it.cond)
|
||||
}
|
||||
g.writeln(') {')
|
||||
for stmt in it.stmts {
|
||||
g.stmt(stmt)
|
||||
@@ -116,7 +120,8 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
if it.op in [.inc, .dec] {
|
||||
g.expr(it.left)
|
||||
g.write(it.op.str())
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
g.write(it.op.str())
|
||||
g.expr(it.left)
|
||||
}
|
||||
|
||||
@@ -89,12 +89,11 @@ pub fn (g mut Gen) generate_elf_footer() {
|
||||
// -5 is for "e8 00 00 00 00"
|
||||
g.write64_at(int(g.main_fn_addr - g.code_start_pos) - 5, g.code_start_pos + 1)
|
||||
// Create the binary
|
||||
mut f := os.create(g.out_name)or{
|
||||
mut f := os.create(g.out_name) or {
|
||||
panic(err)
|
||||
}
|
||||
os.chmod(g.out_name, 0775)
|
||||
os.chmod(g.out_name, 0775) // make it an executable
|
||||
f.write_bytes(g.buf.data, g.buf.len)
|
||||
f.close()
|
||||
println('x64 elf binary has been successfully generated')
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,6 @@ enum Size {
|
||||
|
||||
pub fn gen(files []ast.File, out_name string) {
|
||||
mut g := Gen{
|
||||
// out: strings.new_builder(100)
|
||||
sect_header_name_pos: 0
|
||||
buf: []
|
||||
out_name: out_name
|
||||
@@ -58,6 +57,7 @@ pub fn gen(files []ast.File, out_name string) {
|
||||
g.generate_elf_footer()
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn new_gen(out_name string) &Gen {
|
||||
return &Gen{
|
||||
sect_header_name_pos: 0
|
||||
@@ -65,6 +65,8 @@ pub fn new_gen(out_name string) &Gen {
|
||||
out_name: out_name
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
pub fn (g &Gen) pos() i64 {
|
||||
return g.buf.len
|
||||
@@ -233,6 +235,15 @@ pub fn (g mut Gen) save_main_fn_addr() {
|
||||
g.main_fn_addr = g.buf.len
|
||||
}
|
||||
|
||||
pub fn (g mut Gen) gen_print_from_expr(expr ast.Expr) {
|
||||
match expr {
|
||||
ast.StringLiteral {
|
||||
g.gen_print(it.val)
|
||||
}
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (g mut Gen) gen_print(s string) {
|
||||
g.strings << s + '\n'
|
||||
// g.string_addr[s] = str_pos
|
||||
@@ -306,23 +317,19 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
g.writeln(';')
|
||||
}
|
||||
ast.FnDecl {
|
||||
if it.name == 'main' {
|
||||
g.write('int ${it.name}(')
|
||||
is_main := it.name == 'main'
|
||||
if is_main {
|
||||
g.save_main_fn_addr()
|
||||
}
|
||||
else {
|
||||
g.write('$it.typ.name ${it.name}(')
|
||||
}
|
||||
for arg in it.args {
|
||||
g.write(arg.typ.name + ' ' + arg.name)
|
||||
}
|
||||
g.writeln(') { ')
|
||||
for arg in it.args {}
|
||||
for stmt in it.stmts {
|
||||
g.stmt(stmt)
|
||||
}
|
||||
if it.name == 'main' {
|
||||
g.writeln('return 0;')
|
||||
if is_main {
|
||||
println('end of main: gen exit')
|
||||
g.gen_exit()
|
||||
}
|
||||
g.writeln('}')
|
||||
g.ret()
|
||||
}
|
||||
ast.Return {
|
||||
g.write('return ')
|
||||
@@ -335,6 +342,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
g.writeln(';')
|
||||
}
|
||||
ast.ForStmt {
|
||||
if it.is_in {}
|
||||
g.write('while (')
|
||||
g.expr(it.cond)
|
||||
g.writeln(') {')
|
||||
@@ -401,6 +409,11 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
g.write('}')
|
||||
}
|
||||
ast.CallExpr {
|
||||
if it.name == 'println' || it.name == 'print' {
|
||||
expr := it.args[0]
|
||||
g.gen_print_from_expr(expr)
|
||||
}
|
||||
/*
|
||||
g.write('${it.name}(')
|
||||
for i, expr in it.args {
|
||||
g.expr(expr)
|
||||
@@ -409,6 +422,8 @@ fn (g mut Gen) expr(node ast.Expr) {
|
||||
}
|
||||
}
|
||||
g.write(')')
|
||||
*/
|
||||
|
||||
}
|
||||
ast.ArrayInit {
|
||||
g.writeln('new_array_from_c_array($it.exprs.len, $it.exprs.len, sizeof($it.typ.name), {\t')
|
||||
|
||||
@@ -93,6 +93,7 @@ pub fn parse_file(path string, table &table.Table) ast.File {
|
||||
pub fn parse_files(paths []string, table &table.Table) []ast.File {
|
||||
mut files := []ast.File
|
||||
for path in paths {
|
||||
println('parse file "$path"')
|
||||
mut stmts := []ast.Stmt
|
||||
text := os.read_file(path) or {
|
||||
panic(err)
|
||||
@@ -437,6 +438,24 @@ pub fn (p mut Parser) expr(rbp int) (ast.Expr,types.Type) {
|
||||
|
||||
fn (p mut Parser) for_statement() ast.ForStmt {
|
||||
p.check(.key_for)
|
||||
// `for i in start .. end`
|
||||
if p.peek_tok.kind == .key_in {
|
||||
var := p.check_name()
|
||||
p.check(.key_in)
|
||||
start := p.tok.lit.int()
|
||||
p.check(.number)
|
||||
p.check(.dotdot)
|
||||
end := p.tok.lit.int()
|
||||
// println('for start=$start $end')
|
||||
p.check(.number)
|
||||
stmts := p.parse_block()
|
||||
// println('nr stmts=$stmts.len')
|
||||
return ast.ForStmt{
|
||||
stmts: stmts
|
||||
is_in: true
|
||||
}
|
||||
}
|
||||
// `for cond {`
|
||||
cond,typ := p.expr(0)
|
||||
if !types.check(types.bool_type, typ) {
|
||||
p.error('non-bool used as for condition')
|
||||
|
||||
Reference in New Issue
Block a user