cache builtin modules
							parent
							
								
									c815f84722
								
							
						
					
					
						commit
						f76931c01e
					
				| 
						 | 
					@ -104,6 +104,8 @@ fn main() {
 | 
				
			||||||
		else {}
 | 
							else {}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if command in ['run', 'build'] || command.ends_with('.v') || os.exists(command) {
 | 
						if command in ['run', 'build'] || command.ends_with('.v') || os.exists(command) {
 | 
				
			||||||
 | 
							println('command')
 | 
				
			||||||
 | 
							println(prefs.path)
 | 
				
			||||||
		builder.compile(command, prefs)
 | 
							builder.compile(command, prefs)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -177,6 +179,10 @@ fn parse_args(args []string) (&pref.Preferences, string) {
 | 
				
			||||||
		res.path = args[command_pos+1]
 | 
							res.path = args[command_pos+1]
 | 
				
			||||||
		res.run_args = 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 {
 | 
						if res.is_verbose {
 | 
				
			||||||
		println('setting pref.path to "$res.path"')
 | 
							println('setting pref.path to "$res.path"')
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,12 +10,18 @@ const (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NOTE: temp until we have []bytes(buff)
 | 
					// NOTE: temp until we have []bytes(buff)
 | 
				
			||||||
fn c_array_to_bytes_tmp(len, buffer voidptr) []byte {
 | 
					fn c_array_to_bytes_tmp(len, buffer voidptr) []byte {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mut arr := []byte
 | 
						mut arr := []byte
 | 
				
			||||||
 | 
						arr = make(len, 1, 1)
 | 
				
			||||||
 | 
						arr.data = buffer
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	arr = array {
 | 
						arr = array {
 | 
				
			||||||
		len: len
 | 
							len: len
 | 
				
			||||||
		cap: 1
 | 
							cap: 1
 | 
				
			||||||
		element_size: 1
 | 
							element_size: 1
 | 
				
			||||||
		data: buffer
 | 
							data: buffer
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						*/
 | 
				
			||||||
	return arr
 | 
						return arr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -185,6 +185,7 @@ pub:
 | 
				
			||||||
	rec_mut       bool // is receiver mutable
 | 
						rec_mut       bool // is receiver mutable
 | 
				
			||||||
	is_c          bool
 | 
						is_c          bool
 | 
				
			||||||
	no_body       bool // just a definition `fn C.malloc()`
 | 
						no_body       bool // just a definition `fn C.malloc()`
 | 
				
			||||||
 | 
						is_builtin    bool // this function is defined in builtin/strconv
 | 
				
			||||||
	pos           token.Position
 | 
						pos           token.Position
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -301,6 +301,11 @@ fn (v mut Builder) cc() {
 | 
				
			||||||
	// add all flags (-I -l -L etc) not .o files
 | 
						// add all flags (-I -l -L etc) not .o files
 | 
				
			||||||
	a << cflags.c_options_without_object_files()
 | 
						a << cflags.c_options_without_object_files()
 | 
				
			||||||
	a << libs
 | 
						a << libs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if v.pref.show_cc {
 | 
				
			||||||
 | 
							a << pref.default_module_path + '/cache/vlib/builtin.o'
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Without these libs compilation will fail on Linux
 | 
						// Without these libs compilation will fail on Linux
 | 
				
			||||||
	// || os.user_os() == '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] {
 | 
						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)
 | 
						mut b := new_builder(pref)
 | 
				
			||||||
	if pref.is_verbose {
 | 
						if pref.is_verbose {
 | 
				
			||||||
		println('builder.compile() pref:')
 | 
							println('builder.compile() pref:')
 | 
				
			||||||
		println(pref)
 | 
							// println(pref)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	mut tmark := benchmark.new_benchmark()
 | 
						mut tmark := benchmark.new_benchmark()
 | 
				
			||||||
	mut files := []string
 | 
						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')
 | 
							mut out_name_c := get_vtmp_filename(pref.out_name, '.tmp.c')
 | 
				
			||||||
		if pref.is_so {
 | 
							if pref.is_so {
 | 
				
			||||||
			out_name_c = get_vtmp_filename(pref.out_name, '.tmp.so.c')
 | 
								out_name_c = get_vtmp_filename(pref.out_name, '.tmp.so.c')
 | 
				
			||||||
		}	   	   
 | 
							}
 | 
				
			||||||
		b.build_c(files, out_name_c)
 | 
							b.build_c(files, out_name_c)
 | 
				
			||||||
		b.cc()
 | 
							b.cc()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -145,6 +145,13 @@ fn (v mut Builder) set_module_lookup_paths() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn (v Builder) get_builtin_files() []string {
 | 
					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('get_builtin_files() lookuppath:')
 | 
				
			||||||
	// println(v.pref.lookup_path)
 | 
						// println(v.pref.lookup_path)
 | 
				
			||||||
	// Lookup for built-in folder in 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
 | 
					// TODO: non deferred
 | 
				
			||||||
pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) {
 | 
					pub fn (c mut Checker) return_stmt(return_stmt mut ast.Return) {
 | 
				
			||||||
	c.expected_type = c.fn_return_type
 | 
						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
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if return_stmt.exprs.len > 0 && c.fn_return_type == table.void_type {
 | 
						else 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',
 | 
							c.error('too few arguments to return',
 | 
				
			||||||
			return_stmt.pos)
 | 
								return_stmt.pos)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if return_stmt.exprs.len == 0 {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	expected_type := c.fn_return_type
 | 
						expected_type := c.fn_return_type
 | 
				
			||||||
	expected_type_sym := c.table.get_type_symbol(expected_type)
 | 
						expected_type_sym := c.table.get_type_symbol(expected_type)
 | 
				
			||||||
	exp_is_optional := table.type_is(expected_type, .optional)
 | 
						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_variadic_types()
 | 
				
			||||||
	// g.write_str_definitions()
 | 
						// 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 {
 | 
						if g.is_test {
 | 
				
			||||||
		g.write_tests_main()
 | 
							g.write_tests_main()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -138,18 +141,25 @@ pub fn (g mut Gen) init() {
 | 
				
			||||||
	g.write_builtin_types()
 | 
						g.write_builtin_types()
 | 
				
			||||||
	g.write_typedef_types()
 | 
						g.write_typedef_types()
 | 
				
			||||||
	g.write_typeof_functions()
 | 
						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_sorted_types()
 | 
				
			||||||
	g.write_multi_return_types()
 | 
						g.write_multi_return_types()
 | 
				
			||||||
	g.definitions.writeln('// end of definitions #endif')
 | 
						g.definitions.writeln('// end of definitions #endif')
 | 
				
			||||||
	//
 | 
						//
 | 
				
			||||||
	g.stringliterals.writeln('')
 | 
						g.stringliterals.writeln('')
 | 
				
			||||||
	g.stringliterals.writeln('// >> string literal consts')
 | 
						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() {
 | 
					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('// << string literal consts')
 | 
				
			||||||
	g.stringliterals.writeln('')
 | 
						g.stringliterals.writeln('')
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -352,7 +362,9 @@ fn (g mut Gen) stmt(node ast.Stmt) {
 | 
				
			||||||
			g.writeln(';')
 | 
								g.writeln(';')
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		ast.ConstDecl {
 | 
							ast.ConstDecl {
 | 
				
			||||||
 | 
								// if g.pref.build_mode != .build_module {
 | 
				
			||||||
			g.const_decl(it)
 | 
								g.const_decl(it)
 | 
				
			||||||
 | 
								// }
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		ast.CompIf {
 | 
							ast.CompIf {
 | 
				
			||||||
			g.comp_if(it)
 | 
								g.comp_if(it)
 | 
				
			||||||
| 
						 | 
					@ -808,6 +820,9 @@ fn (g mut Gen) gen_fn_decl(it ast.FnDecl) {
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			name = c_name(name)
 | 
								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.table.Type_to_str(it.return_type)
 | 
				
			||||||
		type_name := g.typ(it.return_type)
 | 
							type_name := g.typ(it.return_type)
 | 
				
			||||||
		g.write('$type_name ${name}(')
 | 
							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)
 | 
						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.
 | 
							// Just a function header.
 | 
				
			||||||
 | 
							// Builtin function bodies are defined in builtin.o
 | 
				
			||||||
		g.definitions.writeln(');')
 | 
							g.definitions.writeln(');')
 | 
				
			||||||
		g.writeln(');')
 | 
							g.writeln(');')
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
| 
						 | 
					@ -1437,8 +1453,8 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
 | 
				
			||||||
		g.write(',')
 | 
							g.write(',')
 | 
				
			||||||
		g.expr(node.right)
 | 
							g.expr(node.right)
 | 
				
			||||||
		g.write(')')
 | 
							g.write(')')
 | 
				
			||||||
	} else if node.op in [.plus, .minus, .mul, .div, .mod] && (left_sym.name[0].is_capital() || left_sym.name.contains('.')) &&
 | 
						} else if node.op in [.plus, .minus, .mul, .div, .mod] && (left_sym.name[0].is_capital() ||
 | 
				
			||||||
		left_sym.kind != .alias {
 | 
							left_sym.name.contains('.')) && left_sym.kind != .alias {
 | 
				
			||||||
		// !left_sym.is_number() {
 | 
							// !left_sym.is_number() {
 | 
				
			||||||
		g.write(g.typ(node.left_type))
 | 
							g.write(g.typ(node.left_type))
 | 
				
			||||||
		g.write('_')
 | 
							g.write('_')
 | 
				
			||||||
| 
						 | 
					@ -1903,7 +1919,9 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			ast.StringLiteral {
 | 
								ast.StringLiteral {
 | 
				
			||||||
				g.definitions.writeln('string _const_$name; // a string literal, inited later')
 | 
									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 {
 | 
								else {
 | 
				
			||||||
				// Initialize more complex consts in `void _vinit(){}`
 | 
									// 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
 | 
						// _STR function can't be defined in vlib
 | 
				
			||||||
	g.writeln('
 | 
						g.writeln('
 | 
				
			||||||
string _STR(const char *fmt, ...) {
 | 
					string _STR(const char *fmt, ...) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -222,7 +222,12 @@ void _vcleanup();
 | 
				
			||||||
#include <intrin.h>
 | 
					#include <intrin.h>
 | 
				
			||||||
#pragma intrinsic(_umul128)
 | 
					#pragma intrinsic(_umul128)
 | 
				
			||||||
#endif
 | 
					#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__)
 | 
					#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
 | 
				
			||||||
#define _likely_(x) __builtin_expect(x, 1)
 | 
					#define _likely_(x) __builtin_expect(x, 1)
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -196,14 +196,16 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
 | 
				
			||||||
		is_pub: is_pub
 | 
							is_pub: is_pub
 | 
				
			||||||
		is_variadic: is_variadic
 | 
							is_variadic: is_variadic
 | 
				
			||||||
		receiver: ast.Field{
 | 
							receiver: ast.Field{
 | 
				
			||||||
		name: rec_name
 | 
								name: rec_name
 | 
				
			||||||
		typ: rec_type
 | 
								typ: rec_type
 | 
				
			||||||
	}
 | 
							}
 | 
				
			||||||
		is_method: is_method
 | 
							is_method: is_method
 | 
				
			||||||
		rec_mut: rec_mut
 | 
							rec_mut: rec_mut
 | 
				
			||||||
		is_c: is_c
 | 
							is_c: is_c
 | 
				
			||||||
		no_body: no_body
 | 
							no_body: no_body
 | 
				
			||||||
		pos: p.tok.position()
 | 
							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