diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a6024ffca..b8b1a1ad56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/cmd/tools/vtest-cleancode.v b/cmd/tools/vtest-cleancode.v index 664e72d99c..520b1b5d59 100644 --- a/cmd/tools/vtest-cleancode.v +++ b/cmd/tools/vtest-cleancode.v @@ -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', diff --git a/vlib/v/README.md b/vlib/v/README.md index 6738b15e5d..65d46f12e7 100644 --- a/vlib/v/README.md +++ b/vlib/v/README.md @@ -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) ``` diff --git a/vlib/v/builder/c.v b/vlib/v/builder/c.v index e18d1a5fee..8e26bb8d9c 100644 --- a/vlib/v/builder/c.v +++ b/vlib/v/builder/c.v @@ -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) diff --git a/vlib/v/builder/js.v b/vlib/v/builder/js.v index 04f3ebc205..00ba3f922c 100644 --- a/vlib/v/builder/js.v +++ b/vlib/v/builder/js.v @@ -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 { diff --git a/vlib/v/builder/x64.v b/vlib/v/builder/x64.v index 2b276d0610..0efb77e999 100644 --- a/vlib/v/builder/x64.v +++ b/vlib/v/builder/x64.v @@ -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) { diff --git a/vlib/v/gen/array.v b/vlib/v/gen/c/array.v similarity index 99% rename from vlib/v/gen/array.v rename to vlib/v/gen/c/array.v index aabab95571..874b9eed33 100644 --- a/vlib/v/gen/array.v +++ b/vlib/v/gen/c/array.v @@ -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 diff --git a/vlib/v/gen/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v similarity index 99% rename from vlib/v/gen/auto_eq_methods.v rename to vlib/v/gen/c/auto_eq_methods.v index bf28018761..301ff1fcc1 100644 --- a/vlib/v/gen/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -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 diff --git a/vlib/v/gen/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v similarity index 99% rename from vlib/v/gen/auto_str_methods.v rename to vlib/v/gen/c/auto_str_methods.v index 6fb9e42426..db6db299dc 100644 --- a/vlib/v/gen/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -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 diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/c/cgen.v similarity index 99% rename from vlib/v/gen/cgen.v rename to vlib/v/gen/c/cgen.v index addecdb6a7..152335dfc5 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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 diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/c/cgen_test.v similarity index 100% rename from vlib/v/gen/cgen_test.v rename to vlib/v/gen/c/cgen_test.v diff --git a/vlib/v/gen/cheaders.v b/vlib/v/gen/c/cheaders.v similarity index 99% rename from vlib/v/gen/cheaders.v rename to vlib/v/gen/c/cheaders.v index 03282a9207..1bf6b1712f 100644 --- a/vlib/v/gen/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -1,4 +1,4 @@ -module gen +module c // NB: @@@ here serve as placeholders. // They will be replaced with correct strings diff --git a/vlib/v/gen/cmain.v b/vlib/v/gen/c/cmain.v similarity index 99% rename from vlib/v/gen/cmain.v rename to vlib/v/gen/c/cmain.v index f0e27c183a..2b20fb9c4f 100644 --- a/vlib/v/gen/cmain.v +++ b/vlib/v/gen/c/cmain.v @@ -1,4 +1,4 @@ -module gen +module c import v.util diff --git a/vlib/v/gen/comptime.v b/vlib/v/gen/c/comptime.v similarity index 99% rename from vlib/v/gen/comptime.v rename to vlib/v/gen/c/comptime.v index c94183122d..f4d3820f3d 100644 --- a/vlib/v/gen/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -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 diff --git a/vlib/v/gen/ctempvars.v b/vlib/v/gen/c/ctempvars.v similarity index 97% rename from vlib/v/gen/ctempvars.v rename to vlib/v/gen/c/ctempvars.v index b2dc39f655..73b5b147a2 100644 --- a/vlib/v/gen/ctempvars.v +++ b/vlib/v/gen/c/ctempvars.v @@ -1,4 +1,4 @@ -module gen +module c import v.ast import v.table diff --git a/vlib/v/gen/embed.v b/vlib/v/gen/c/embed.v similarity index 99% rename from vlib/v/gen/embed.v rename to vlib/v/gen/c/embed.v index 388d198f0a..5db0bbf9b4 100644 --- a/vlib/v/gen/embed.v +++ b/vlib/v/gen/c/embed.v @@ -1,4 +1,4 @@ -module gen +module c import os import v.ast diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/c/fn.v similarity index 99% rename from vlib/v/gen/fn.v rename to vlib/v/gen/c/fn.v index 67f3d2c91a..e40756aee3 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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 diff --git a/vlib/v/gen/json.v b/vlib/v/gen/c/json.v similarity index 99% rename from vlib/v/gen/json.v rename to vlib/v/gen/c/json.v index 1da7e2f838..118fb266f7 100644 --- a/vlib/v/gen/json.v +++ b/vlib/v/gen/c/json.v @@ -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 diff --git a/vlib/v/gen/live.v b/vlib/v/gen/c/live.v similarity index 96% rename from vlib/v/gen/live.v rename to vlib/v/gen/c/live.v index e96c69592b..5bcf6015ba 100644 --- a/vlib/v/gen/live.v +++ b/vlib/v/gen/c/live.v @@ -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'))) } diff --git a/vlib/v/gen/profile.v b/vlib/v/gen/c/profile.v similarity index 99% rename from vlib/v/gen/profile.v rename to vlib/v/gen/c/profile.v index 880b375158..044a3de391 100644 --- a/vlib/v/gen/profile.v +++ b/vlib/v/gen/c/profile.v @@ -1,4 +1,4 @@ -module gen +module c import v.ast diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/c/sql.v similarity index 96% rename from vlib/v/gen/sql.v rename to vlib/v/gen/c/sql.v index 5f8ef28bb1..c1c4a0ef43 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/c/sql.v @@ -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() diff --git a/vlib/v/gen/str.v b/vlib/v/gen/c/str.v similarity index 99% rename from vlib/v/gen/str.v rename to vlib/v/gen/c/str.v index d6b918b411..5c104be6ce 100644 --- a/vlib/v/gen/str.v +++ b/vlib/v/gen/c/str.v @@ -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 diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index 40d0dde3ea..06d496b0bd 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -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('========')