cache builtin modules
							parent
							
								
									c815f84722
								
							
						
					
					
						commit
						f76931c01e
					
				| 
						 | 
				
			
			@ -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"')
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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] {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
	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')
 | 
			
		||||
	if g.pref.build_mode != .build_module {
 | 
			
		||||
		g.stringliterals.writeln('void vinit_string_literals(){')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn (g mut Gen) finish() {
 | 
			
		||||
	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,8 +1919,10 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) {
 | 
			
		|||
			}
 | 
			
		||||
			ast.StringLiteral {
 | 
			
		||||
				g.definitions.writeln('string _const_$name; // a string literal, inited later')
 | 
			
		||||
				if g.pref.build_mode != .build_module {
 | 
			
		||||
					g.stringliterals.writeln('\t_const_$name = $val;')
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
				// Initialize more complex consts in `void _vinit(){}`
 | 
			
		||||
				// (C doesn't allow init expressions that can't be resolved at compile time).
 | 
			
		||||
| 
						 | 
				
			
			@ -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, ...) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -222,7 +222,12 @@ void _vcleanup();
 | 
			
		|||
#include <intrin.h>
 | 
			
		||||
#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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -204,6 +204,8 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
 | 
			
		|||
		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']
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue