diff --git a/cmd/v/v.v b/cmd/v/v.v index a3e6d8ac2b..9ff25fa871 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -104,6 +104,8 @@ fn main() { else {} } if command in ['run', 'build'] || command.ends_with('.v') || os.exists(command) { + println('command') + println(prefs.path) builder.compile(command, prefs) return } @@ -177,6 +179,10 @@ fn parse_args(args []string) (&pref.Preferences, string) { res.path = args[command_pos+1] res.run_args = args[command_pos+1..] } + if command == 'build' { + res.build_mode = .build_module + res.path = args[command_pos+1] + } if res.is_verbose { println('setting pref.path to "$res.path"') } diff --git a/vlib/crypto/rand/rand.v b/vlib/crypto/rand/rand.v index 0b822c37a1..f8f1c66cb2 100644 --- a/vlib/crypto/rand/rand.v +++ b/vlib/crypto/rand/rand.v @@ -10,12 +10,18 @@ const ( // NOTE: temp until we have []bytes(buff) fn c_array_to_bytes_tmp(len, buffer voidptr) []byte { + mut arr := []byte + arr = make(len, 1, 1) + arr.data = buffer + /* + arr = array { len: len cap: 1 element_size: 1 data: buffer } + */ return arr } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 908c7f86ac..e03080ea8b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -185,6 +185,7 @@ pub: rec_mut bool // is receiver mutable is_c bool no_body bool // just a definition `fn C.malloc()` + is_builtin bool // this function is defined in builtin/strconv pos token.Position } diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 4fa1962192..e01d094867 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -301,6 +301,11 @@ fn (v mut Builder) cc() { // add all flags (-I -l -L etc) not .o files a << cflags.c_options_without_object_files() a << libs + + if v.pref.show_cc { + a << pref.default_module_path + '/cache/vlib/builtin.o' + } + // Without these libs compilation will fail on Linux // || os.user_os() == 'linux' if !v.pref.is_bare && v.pref.build_mode != .build_module && v.pref.os in [.linux, .freebsd, .openbsd, .netbsd, .dragonfly, .solaris, .haiku] { diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 4310424daf..e5521a0586 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -31,7 +31,7 @@ pub fn compile(command string, pref &pref.Preferences) { mut b := new_builder(pref) if pref.is_verbose { println('builder.compile() pref:') - println(pref) + // println(pref) } mut tmark := benchmark.new_benchmark() mut files := []string @@ -65,7 +65,7 @@ pub fn compile(command string, pref &pref.Preferences) { mut out_name_c := get_vtmp_filename(pref.out_name, '.tmp.c') if pref.is_so { out_name_c = get_vtmp_filename(pref.out_name, '.tmp.so.c') - } + } b.build_c(files, out_name_c) b.cc() } @@ -145,6 +145,13 @@ fn (v mut Builder) set_module_lookup_paths() { } pub fn (v Builder) get_builtin_files() []string { + if v.pref.build_mode == .build_module && v.pref.path == 'vlib/builtin' { // .contains('builtin/' + location { + // We are already building builtin.o, no need to import them again + if v.pref.is_verbose { + println('skipping builtin modules for builtin.o') + } + return [] + } // println('get_builtin_files() lookuppath:') // println(v.pref.lookup_path) // Lookup for built-in folder in lookup path. diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5b173fe047..a4f6ea3e18 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -519,14 +519,19 @@ pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T // TODO: non deferred pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) { c.expected_type = c.fn_return_type - if return_stmt.exprs.len == 0 { + if return_stmt.exprs.len > 0 && c.fn_return_type == table.void_type { + c.error('1too many arguments to return, current function does not return anything', + return_stmt.pos) return } - if return_stmt.exprs.len > 0 && c.fn_return_type == table.void_type { - c.error('too many arguments to return, current function does not return anything', + else if return_stmt.exprs.len == 0 && c.fn_return_type != table.void_type { + c.error('too few arguments to return', return_stmt.pos) return } + if return_stmt.exprs.len == 0 { + return + } expected_type := c.fn_return_type expected_type_sym := c.table.get_type_symbol(expected_type) exp_is_optional := table.type_is(expected_type, .optional) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index a036c6ad10..df4c948b23 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -112,7 +112,10 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string } g.write_variadic_types() // g.write_str_definitions() - g.write_init_function() + if g.pref.build_mode != .build_module { + // no init in builtin.o + g.write_init_function() + } if g.is_test { g.write_tests_main() } @@ -138,18 +141,25 @@ pub fn (g mut Gen) init() { g.write_builtin_types() g.write_typedef_types() g.write_typeof_functions() - g.write_str_definitions() + if g.pref.build_mode != .build_module { + // _STR functions should not be defined in builtin.o + g.write_str_fn_definitions() + } g.write_sorted_types() g.write_multi_return_types() g.definitions.writeln('// end of definitions #endif') // g.stringliterals.writeln('') g.stringliterals.writeln('// >> string literal consts') - g.stringliterals.writeln('void vinit_string_literals(){') + if g.pref.build_mode != .build_module { + g.stringliterals.writeln('void vinit_string_literals(){') + } } pub fn (g mut Gen) finish() { - g.stringliterals.writeln('}') + if g.pref.build_mode != .build_module { + g.stringliterals.writeln('}') + } g.stringliterals.writeln('// << string literal consts') g.stringliterals.writeln('') } @@ -352,7 +362,9 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.writeln(';') } ast.ConstDecl { + // if g.pref.build_mode != .build_module { g.const_decl(it) + // } } ast.CompIf { g.comp_if(it) @@ -808,6 +820,9 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { } else { name = c_name(name) } + // if g.pref.show_cc && it.is_builtin { + // println(name) + // } // type_name := g.table.Type_to_str(it.return_type) type_name := g.typ(it.return_type) g.write('$type_name ${name}(') @@ -832,8 +847,9 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) { */ // g.fn_args(it.args, it.is_variadic) - if it.no_body { + if it.no_body || (g.pref.show_cc && it.is_builtin) { // Just a function header. + // Builtin function bodies are defined in builtin.o g.definitions.writeln(');') g.writeln(');') return @@ -1437,8 +1453,8 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) { g.write(',') g.expr(node.right) g.write(')') - } else if node.op in [.plus, .minus, .mul, .div, .mod] && (left_sym.name[0].is_capital() || left_sym.name.contains('.')) && - left_sym.kind != .alias { + } else if node.op in [.plus, .minus, .mul, .div, .mod] && (left_sym.name[0].is_capital() || + left_sym.name.contains('.')) && left_sym.kind != .alias { // !left_sym.is_number() { g.write(g.typ(node.left_type)) g.write('_') @@ -1903,7 +1919,9 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) { } ast.StringLiteral { g.definitions.writeln('string _const_$name; // a string literal, inited later') - g.stringliterals.writeln('\t_const_$name = $val;') + if g.pref.build_mode != .build_module { + g.stringliterals.writeln('\t_const_$name = $val;') + } } else { // Initialize more complex consts in `void _vinit(){}` @@ -2127,7 +2145,7 @@ fn (g mut Gen) write_init_function() { } } -fn (g mut Gen) write_str_definitions() { +fn (g mut Gen) write_str_fn_definitions() { // _STR function can't be defined in vlib g.writeln(' string _STR(const char *fmt, ...) { diff --git a/vlib/v/gen/cheaders.v b/vlib/v/gen/cheaders.v index 6ebe05a0c4..cd081770ec 100644 --- a/vlib/v/gen/cheaders.v +++ b/vlib/v/gen/cheaders.v @@ -222,7 +222,12 @@ void _vcleanup(); #include #pragma intrinsic(_umul128) #endif -const uint64_t _wyp0=0xa0761d6478bd642full, _wyp1=0xe7037ed1a0b428dbull; + +//const uint64_t _wyp0=0xa0761d6478bd642full, _wyp1=0xe7037ed1a0b428dbull; +#define _wyp0 ((uint64_t)0xa0761d6478bd642full) +#define _wyp1 ((uint64_t)0xe7037ed1a0b428dbull) + + #if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) #define _likely_(x) __builtin_expect(x, 1) #else diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 130556c3f1..c268f5b4e5 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -196,14 +196,16 @@ fn (p mut Parser) fn_decl() ast.FnDecl { is_pub: is_pub is_variadic: is_variadic receiver: ast.Field{ - name: rec_name - typ: rec_type - } + name: rec_name + typ: rec_type + } is_method: is_method rec_mut: rec_mut is_c: is_c no_body: no_body pos: p.tok.position() + is_builtin: p.builtin_mod || p.mod in ['math', 'strconv', 'strconv.ftoa', 'hash.wyhash', + 'math.bits', 'strings'] } }