1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: move cgen from v.gen to v.gen.c (#8515)

This commit is contained in:
joe-conigliaro 2021-02-03 01:41:51 +11:00 committed by GitHub
parent d477e525bb
commit ffedbe4b81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 61 additions and 63 deletions

View File

@ -306,7 +306,7 @@ jobs:
./v build-module vlib/v/token
./v build-module vlib/v/ast
./v build-module vlib/v/parser
./v build-module vlib/v/gen
./v build-module vlib/v/gen/c
./v build-module vlib/v/depgraph
./v build-module vlib/os/cmdline
- name: x64 machine code generation
@ -391,7 +391,7 @@ jobs:
./v build-module vlib/v/token
./v build-module vlib/v/ast
./v build-module vlib/v/parser
./v build-module vlib/v/gen
./v build-module vlib/v/gen/c
./v build-module vlib/v/depgraph
./v build-module vlib/os/cmdline
- name: x64 machine code generation

View File

@ -37,17 +37,17 @@ const (
'vlib/v/errors/',
'vlib/v/eval/',
'vlib/v/fmt/',
'vlib/v/gen/auto_str_methods.v',
'vlib/v/gen/cgen.v',
'vlib/v/gen/cgen_test.v',
'vlib/v/gen/cmain.v',
'vlib/v/gen/comptime.v',
'vlib/v/gen/fn.v',
'vlib/v/gen/json.v',
'vlib/v/gen/live.v',
'vlib/v/gen/profile.v',
'vlib/v/gen/sql.v',
'vlib/v/gen/str.v',
'vlib/v/gen/c/auto_str_methods.v',
'vlib/v/gen/c/cgen.v',
'vlib/v/gen/c/cgen_test.v',
'vlib/v/gen/c/cmain.v',
'vlib/v/gen/c/comptime.v',
'vlib/v/gen/c/fn.v',
'vlib/v/gen/c/json.v',
'vlib/v/gen/c/live.v',
'vlib/v/gen/c/profile.v',
'vlib/v/gen/c/sql.v',
'vlib/v/gen/c/str.v',
'vlib/v/gen/x64/elf.v',
'vlib/v/gen/x64/elf_obj.v',
'vlib/v/gen/x64/gen.v',

View File

@ -113,7 +113,7 @@ checker.check_files(parsed_files)
## Generate target from AST
Generating C code works just as this:
```v oksyntax
import v.gen
import v.gen.c
res := gen.cgen(parsed_files, table, &pref)
res := c.gen(parsed_files, table, &pref)
```

View File

@ -3,7 +3,7 @@ module builder
import os
import v.parser
import v.pref
import v.gen
import v.gen.c
pub fn (mut b Builder) gen_c(v_files []string) string {
b.timing_start('PARSE')
@ -22,7 +22,7 @@ pub fn (mut b Builder) gen_c(v_files []string) string {
b.print_warnings_and_errors()
// TODO: move gen.cgen() to c.gen()
b.timing_start('C GEN')
res := gen.cgen(b.parsed_files, b.table, b.pref)
res := c.gen(b.parsed_files, b.table, b.pref)
b.timing_measure('C GEN')
// println('cgen done')
// println(res)

View File

@ -3,7 +3,6 @@ module builder
import os
import v.parser
import v.pref
import v.gen
import v.gen.js
pub fn (mut b Builder) gen_js(v_files []string) string {

View File

@ -2,7 +2,6 @@ module builder
import v.parser
import v.pref
import v.gen
import v.gen.x64
pub fn (mut b Builder) build_x64(v_files []string, out_file string) {

View File

@ -1,6 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gen
module c
import strings
import v.ast

View File

@ -1,6 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gen
module c
import strings
import v.table

View File

@ -1,6 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gen
module c
import v.table
import v.util

View File

@ -1,7 +1,7 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module gen
module c
import os
import strings
@ -151,7 +151,7 @@ mut:
obf_table map[string]string
}
pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string {
pub fn gen(files []ast.File, table &table.Table, pref &pref.Preferences) string {
// println('start cgen2')
mut module_built := ''
if pref.build_mode == .build_module {
@ -803,8 +803,8 @@ pub fn (mut g Gen) write(s string) {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: $s')
}
if g.indent > 0 && g.empty_line {
if g.indent < gen.tabs.len {
g.out.write(gen.tabs[g.indent])
if g.indent < c.tabs.len {
g.out.write(c.tabs[g.indent])
} else {
for _ in 0 .. g.indent {
g.out.write('\t')
@ -820,8 +820,8 @@ pub fn (mut g Gen) writeln(s string) {
eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: $s')
}
if g.indent > 0 && g.empty_line {
if g.indent < gen.tabs.len {
g.out.write(gen.tabs[g.indent])
if g.indent < c.tabs.len {
g.out.write(c.tabs[g.indent])
} else {
for _ in 0 .. g.indent {
g.out.write('\t')
@ -936,7 +936,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
}
defer {
}
// println('cgen.stmt()')
// println('g.stmt()')
// g.writeln('//// stmt start')
match node {
ast.AssertStmt {
@ -2067,7 +2067,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
if right_sym.kind == .function && is_decl {
if is_inside_ternary && is_decl {
g.out.write(gen.tabs[g.indent - g.inside_ternary])
g.out.write(c.tabs[g.indent - g.inside_ternary])
}
func := right_sym.info as table.FnType
ret_styp := g.typ(func.func.return_type)
@ -2079,7 +2079,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} else {
if is_decl {
if is_inside_ternary {
g.out.write(gen.tabs[g.indent - g.inside_ternary])
g.out.write(c.tabs[g.indent - g.inside_ternary])
}
g.write('$styp ')
}
@ -2096,7 +2096,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
if is_inside_ternary && is_decl {
g.write(';\n$cur_line')
g.out.write(gen.tabs[g.indent])
g.out.write(c.tabs[g.indent])
g.expr(left)
}
g.is_assign_lhs = false
@ -2760,7 +2760,7 @@ fn (mut g Gen) expr(node ast.Expr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write(gen.tabs[g.indent])
g.out.write(c.tabs[g.indent])
line
} else {
''
@ -3327,7 +3327,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else {
64
}
g.write('_us${bitsize}_${gen.cmp_str[int(node.op) - int(token.Kind.eq)]}(')
g.write('_us${bitsize}_${c.cmp_str[int(node.op) - int(token.Kind.eq)]}(')
g.expr(node.left)
g.write(',')
g.expr(node.right)
@ -3340,7 +3340,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else {
64
}
g.write('_us${bitsize}_${gen.cmp_rev[int(node.op) - int(token.Kind.eq)]}(')
g.write('_us${bitsize}_${c.cmp_rev[int(node.op) - int(token.Kind.eq)]}(')
g.expr(node.right)
g.write(',')
g.expr(node.left)
@ -4175,7 +4175,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write(gen.tabs[g.indent])
g.out.write(c.tabs[g.indent])
line
} else {
''
@ -4333,7 +4333,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
is_gen_or_and_assign_rhs := gen_or && g.is_assign_rhs
cur_line := if is_gen_or_and_assign_rhs {
line := g.go_before_stmt(0)
g.out.write(gen.tabs[g.indent])
g.out.write(c.tabs[g.indent])
line
} else {
''
@ -4750,7 +4750,7 @@ const (
fn (mut g Gen) struct_init(struct_init ast.StructInit) {
styp := g.typ(struct_init.typ)
mut shared_styp := '' // only needed for shared &St{...
if styp in gen.skip_struct_init {
if styp in c.skip_struct_init {
// needed for c++ compilers
g.go_back_out(3)
return
@ -5086,7 +5086,7 @@ fn (mut g Gen) write_builtin_types() {
mut builtin_types := []table.TypeSymbol{} // builtin types
// builtin types need to be on top
// everything except builtin will get sorted
for builtin_name in gen.builtins {
for builtin_name in c.builtins {
builtin_types << g.table.types[g.table.type_idxs[builtin_name]]
}
g.write_types(builtin_types)
@ -5098,7 +5098,7 @@ fn (mut g Gen) write_builtin_types() {
fn (mut g Gen) write_sorted_types() {
mut types := []table.TypeSymbol{} // structs that need to be sorted
for typ in g.table.types {
if typ.name !in gen.builtins {
if typ.name !in c.builtins {
types << typ
}
}
@ -5574,7 +5574,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string
[inline]
fn c_name(name_ string) string {
name := util.no_dots(name_)
if name in gen.c_reserved {
if name in c.c_reserved {
return 'v_$name'
}
return name

View File

@ -1,4 +1,4 @@
module gen
module c
// NB: @@@ here serve as placeholders.
// They will be replaced with correct strings

View File

@ -1,4 +1,4 @@
module gen
module c
import v.util

View File

@ -1,7 +1,7 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module gen
module c
import os
import v.ast

View File

@ -1,4 +1,4 @@
module gen
module c
import v.ast
import v.table

View File

@ -1,4 +1,4 @@
module gen
module c
import os
import v.ast

View File

@ -1,7 +1,7 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module gen
module c
import v.ast
import v.table

View File

@ -1,7 +1,7 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module gen
module c
import v.table
import v.util

View File

@ -1,4 +1,4 @@
module gen
module c
import v.pref
import v.util
@ -42,12 +42,12 @@ fn (mut g Gen) generate_hotcode_reloader_code() {
for so_fn in g.hotcode_fn_names {
load_code << 'impl_live_$so_fn = dlsym(live_lib, "impl_live_$so_fn");'
}
phd = gen.posix_hotcode_definitions_1
phd = c.posix_hotcode_definitions_1
} else {
for so_fn in g.hotcode_fn_names {
load_code << 'impl_live_$so_fn = (void *)GetProcAddress(live_lib, "impl_live_$so_fn"); '
}
phd = gen.windows_hotcode_definitions_1
phd = c.windows_hotcode_definitions_1
}
g.hotcode_definitions.writeln(phd.replace('@LOAD_FNS@', load_code.join('\n')))
}

View File

@ -1,4 +1,4 @@
module gen
module c
import v.ast

View File

@ -1,6 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gen
module c
import v.ast
import strings
@ -22,10 +22,10 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
g.writeln('\n\t// sql insert')
db_name := g.new_tmp_var()
g.sql_stmt_name = g.new_tmp_var()
g.write('${gen.dbtype}__DB $db_name = ')
g.write('${c.dbtype}__DB $db_name = ')
g.expr(node.db_expr)
g.writeln(';')
g.write('sqlite3_stmt* $g.sql_stmt_name = ${gen.dbtype}__DB_init_stmt($db_name, _SLIT("')
g.write('sqlite3_stmt* $g.sql_stmt_name = ${c.dbtype}__DB_init_stmt($db_name, _SLIT("')
table_name := util.strip_mod_name(g.table.get_type_symbol(node.table_expr.typ).name)
if node.kind == .insert {
g.write('INSERT INTO `$table_name` (')
@ -131,11 +131,11 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
db_name := g.new_tmp_var()
g.writeln('\n\t// sql select')
// g.write('${dbtype}__DB $db_name = *(${dbtype}__DB*)${node.db_var_name}.data;')
g.write('${gen.dbtype}__DB $db_name = ') // $node.db_var_name;')
g.write('${c.dbtype}__DB $db_name = ') // $node.db_var_name;')
g.expr(node.db_expr)
g.writeln(';')
// g.write('sqlite3_stmt* $g.sql_stmt_name = ${dbtype}__DB_init_stmt(*(${dbtype}__DB*)${node.db_var_name}.data, _SLIT("$sql_query')
g.write('sqlite3_stmt* $g.sql_stmt_name = ${gen.dbtype}__DB_init_stmt($db_name, _SLIT("')
g.write('sqlite3_stmt* $g.sql_stmt_name = ${c.dbtype}__DB_init_stmt($db_name, _SLIT("')
g.write(sql_query)
if node.has_where && node.where_expr is ast.InfixExpr {
g.expr_to_sql(node.where_expr)
@ -170,7 +170,7 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
g.writeln('if ($binding_res != SQLITE_OK) { puts(sqlite3_errmsg(${db_name}.conn)); }')
//
if node.is_count {
g.writeln('$cur_line ${gen.dbtype}__get_int_from_stmt($g.sql_stmt_name);')
g.writeln('$cur_line ${c.dbtype}__get_int_from_stmt($g.sql_stmt_name);')
} else {
// `user := sql db { select from User where id = 1 }`
tmp := g.new_tmp_var()

View File

@ -1,6 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gen
module c
import v.ast
import v.util

View File

@ -2,7 +2,7 @@ module parser
// import v.eval
import v.ast
import v.gen
import v.gen.c
import v.table
import v.checker
import v.pref
@ -83,7 +83,7 @@ x := 10
prog := parse_file(s, table, .skip_comments, vpref, gscope)
mut checker := checker.new_checker(table, vpref)
checker.check(prog)
res := gen.cgen([prog], table, vpref)
res := c.gen([prog], table, vpref)
println(res)
}
@ -111,7 +111,7 @@ fn test_one() {
}
mut checker := checker.new_checker(table, vpref)
checker.check(program)
res := gen.cgen([program], table, vpref).replace('\n', '').trim_space().after('#endif')
res := c.gen([program], table, vpref).replace('\n', '').trim_space().after('#endif')
println(res)
ok := expected == res
println(res)
@ -151,7 +151,7 @@ fn test_parse_expr() {
global_scope: scope
}
checker.check(program)
res := gen.cgen([program], table, vpref).after('#endif')
res := c.gen([program], table, vpref).after('#endif')
println('========')
println(res)
println('========')