all: re-enable -prealloc
							parent
							
								
									2ea187fb85
								
							
						
					
					
						commit
						a1073105e1
					
				| 
						 | 
				
			
			@ -136,6 +136,7 @@ pub fn malloc(n int) byteptr {
 | 
			
		|||
		panic('malloc(<=0)')
 | 
			
		||||
	}
 | 
			
		||||
	$if prealloc {
 | 
			
		||||
		//println('p')
 | 
			
		||||
		res := g_m2_ptr
 | 
			
		||||
		unsafe {
 | 
			
		||||
			g_m2_ptr += n
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,8 +15,8 @@ import v.depgraph
 | 
			
		|||
const (
 | 
			
		||||
	c_reserved = ['delete', 'exit', 'link', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic',
 | 
			
		||||
		'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
 | 
			
		||||
		'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned',
 | 
			
		||||
		'void', 'volatile', 'while', 'new', 'namespace', 'class', 'typename']
 | 
			
		||||
		'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void',
 | 
			
		||||
		'volatile', 'while', 'new', 'namespace', 'class', 'typename']
 | 
			
		||||
	// same order as in token.Kind
 | 
			
		||||
	cmp_str    = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
 | 
			
		||||
	// when operands are switched
 | 
			
		||||
| 
						 | 
				
			
			@ -286,6 +286,9 @@ pub fn (mut g Gen) init() {
 | 
			
		|||
		g.comptime_defines.writeln('#define _VAUTOFREE (1)')
 | 
			
		||||
		// g.comptime_defines.writeln('unsigned char* g_cur_str;')
 | 
			
		||||
	}
 | 
			
		||||
	if g.pref.prealloc {
 | 
			
		||||
		g.comptime_defines.writeln('#define _VPREALLOC (1)')
 | 
			
		||||
	}
 | 
			
		||||
	if g.pref.is_livemain || g.pref.is_liveshared {
 | 
			
		||||
		g.generate_hotcode_reloading_declarations()
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -968,13 +971,11 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type)
 | 
			
		|||
				g.write('/* sum type cast */ ($exp_styp) {.obj = ')
 | 
			
		||||
				g.expr(expr)
 | 
			
		||||
				g.write(', .typ = $got_idx /* $got_sym.name */}')
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
			} else {
 | 
			
		||||
				g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&($got_styp[]) {')
 | 
			
		||||
				g.expr(expr)
 | 
			
		||||
				g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}')
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -1073,16 +1074,30 @@ fn (mut g Gen) gen_assert_metainfo(a ast.AssertStmt) string {
 | 
			
		|||
fn (mut g Gen) gen_assert_single_expr(e ast.Expr, t table.Type) {
 | 
			
		||||
	unknown_value := '*unknown value*'
 | 
			
		||||
	match e {
 | 
			
		||||
		ast.CallExpr { g.write(ctoslit(unknown_value)) }
 | 
			
		||||
		ast.CastExpr { g.write(ctoslit(unknown_value)) }
 | 
			
		||||
		ast.IndexExpr { g.write(ctoslit(unknown_value)) }
 | 
			
		||||
		ast.PrefixExpr { g.write(ctoslit(unknown_value)) }
 | 
			
		||||
		ast.MatchExpr { g.write(ctoslit(unknown_value)) }
 | 
			
		||||
		ast.CallExpr {
 | 
			
		||||
			g.write(ctoslit(unknown_value))
 | 
			
		||||
		}
 | 
			
		||||
		ast.CastExpr {
 | 
			
		||||
			g.write(ctoslit(unknown_value))
 | 
			
		||||
		}
 | 
			
		||||
		ast.IndexExpr {
 | 
			
		||||
			g.write(ctoslit(unknown_value))
 | 
			
		||||
		}
 | 
			
		||||
		ast.PrefixExpr {
 | 
			
		||||
			g.write(ctoslit(unknown_value))
 | 
			
		||||
		}
 | 
			
		||||
		ast.MatchExpr {
 | 
			
		||||
			g.write(ctoslit(unknown_value))
 | 
			
		||||
		}
 | 
			
		||||
		ast.Type {
 | 
			
		||||
			sym := g.table.get_type_symbol(t)
 | 
			
		||||
			g.write(ctoslit('$sym.name'))
 | 
			
		||||
		}
 | 
			
		||||
		else { g.gen_expr_to_string(e, t) or { g.write(ctoslit('[$err]')) } }
 | 
			
		||||
		else {
 | 
			
		||||
			g.gen_expr_to_string(e, t) or {
 | 
			
		||||
				g.write(ctoslit('[$err]'))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	g.write(' /* typeof: ' + typeof(e) + ' type: ' + t.str() + ' */ ')
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1152,14 +1167,13 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
 | 
			
		|||
						arg_len := func.func.args.len
 | 
			
		||||
						for j, arg in func.func.args {
 | 
			
		||||
							arg_type := g.table.get_type_symbol(arg.typ)
 | 
			
		||||
							g.write('${arg_type.str()} ${arg.name}')
 | 
			
		||||
							g.write('$arg_type.str() $arg.name')
 | 
			
		||||
							if j < arg_len - 1 {
 | 
			
		||||
								g.write(', ')
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
						g.writeln(') = $left.name;')
 | 
			
		||||
					}
 | 
			
		||||
					else {
 | 
			
		||||
					} else {
 | 
			
		||||
						styp := g.typ(var_type)
 | 
			
		||||
						g.writeln('$styp _var_$left.pos.pos = $left.name;')
 | 
			
		||||
					}
 | 
			
		||||
| 
						 | 
				
			
			@ -2112,16 +2126,16 @@ fn (mut g Gen) lock_expr(node ast.LockExpr) {
 | 
			
		|||
		deref := if id.is_mut { '->' } else { '.' }
 | 
			
		||||
		lock_prefix := if node.is_rlock { `r` } else { `w` }
 | 
			
		||||
		lock_prefixes << lock_prefix // keep for unlock
 | 
			
		||||
		g.writeln('sync__RwMutex_${lock_prefix:c}_lock(${name}${deref}mtx);')
 | 
			
		||||
		g.writeln('sync__RwMutex_${lock_prefix:c}_lock($name${deref}mtx);')
 | 
			
		||||
	}
 | 
			
		||||
	g.stmts(node.stmts)
 | 
			
		||||
	// unlock in reverse order
 | 
			
		||||
	for i := node.lockeds.len-1; i >= 0; i-- {
 | 
			
		||||
	for i := node.lockeds.len - 1; i >= 0; i-- {
 | 
			
		||||
		id := node.lockeds[i]
 | 
			
		||||
		lock_prefix := lock_prefixes[i]
 | 
			
		||||
		name := id.name
 | 
			
		||||
		deref := if id.is_mut { '->' } else { '.' }
 | 
			
		||||
		g.writeln('sync__RwMutex_${lock_prefix:c}_unlock(${name}${deref}mtx);')
 | 
			
		||||
		g.writeln('sync__RwMutex_${lock_prefix:c}_unlock($name${deref}mtx);')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2863,7 +2877,9 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
 | 
			
		|||
		fields = struct_init.fields
 | 
			
		||||
	}
 | 
			
		||||
	*/
 | 
			
		||||
	if is_multiline { g.indent++ }
 | 
			
		||||
	if is_multiline {
 | 
			
		||||
		g.indent++
 | 
			
		||||
	}
 | 
			
		||||
	// User set fields
 | 
			
		||||
	mut initialized := false
 | 
			
		||||
	for i, field in struct_init.fields {
 | 
			
		||||
| 
						 | 
				
			
			@ -2929,7 +2945,6 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
 | 
			
		|||
				} else {
 | 
			
		||||
					g.write(',')
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				initialized = true
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -2947,11 +2962,12 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
 | 
			
		|||
			} else {
 | 
			
		||||
				g.write(',')
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			initialized = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if is_multiline { g.indent-- }
 | 
			
		||||
	if is_multiline {
 | 
			
		||||
		g.indent--
 | 
			
		||||
	}
 | 
			
		||||
	// if struct_init.fields.len == 0 && info.fields.len == 0 {
 | 
			
		||||
	if !initialized {
 | 
			
		||||
		g.write('\n#ifndef __cplusplus\n0\n#endif\n')
 | 
			
		||||
| 
						 | 
				
			
			@ -3105,6 +3121,10 @@ fn (mut g Gen) write_init_function() {
 | 
			
		|||
		// mb_size := if s_str_buf_size == '' { 1 } else { s_str_buf_size.int() }
 | 
			
		||||
		// g.writeln('g_str_buf = malloc( ${mb_size} * 1024 * 1000 );')
 | 
			
		||||
	}
 | 
			
		||||
	if g.pref.prealloc {
 | 
			
		||||
		g.writeln('g_m2_buf = malloc(50 * 1000 * 1000);')
 | 
			
		||||
		g.writeln('g_m2_ptr = g_m2_buf;')
 | 
			
		||||
	}
 | 
			
		||||
	g.writeln('\tbuiltin_init();')
 | 
			
		||||
	g.writeln('\tvinit_string_literals();')
 | 
			
		||||
	//
 | 
			
		||||
| 
						 | 
				
			
			@ -3788,7 +3808,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
 | 
			
		|||
			return '__GLIBC__'
 | 
			
		||||
		}
 | 
			
		||||
		'prealloc' {
 | 
			
		||||
			return 'VPREALLOC'
 | 
			
		||||
			return '_VPREALLOC'
 | 
			
		||||
		}
 | 
			
		||||
		'no_bounds_checking' {
 | 
			
		||||
			return 'CUSTOM_DEFINE_no_bounds_checking'
 | 
			
		||||
| 
						 | 
				
			
			@ -4174,7 +4194,7 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) {
 | 
			
		|||
	g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) { /* gen_str_for_enum */')
 | 
			
		||||
	g.auto_str_funcs.writeln('\tswitch(it) {')
 | 
			
		||||
	// Only use the first multi value on the lookup
 | 
			
		||||
	mut seen := []string{len:info.vals.len}
 | 
			
		||||
	mut seen := []string{len: info.vals.len}
 | 
			
		||||
	for val in info.vals {
 | 
			
		||||
		if info.is_multi_allowed && val in seen {
 | 
			
		||||
			continue
 | 
			
		||||
| 
						 | 
				
			
			@ -4246,7 +4266,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) {
 | 
			
		|||
				g.auto_str_funcs.write('indents, ')
 | 
			
		||||
				if has_custom_str {
 | 
			
		||||
					g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)} ) ')
 | 
			
		||||
				}else{
 | 
			
		||||
				} else {
 | 
			
		||||
					g.auto_str_funcs.write('indent_${field_styp_fn_name}( it->${c_name(field.name)}, indent_count + 1 ) ')
 | 
			
		||||
				}
 | 
			
		||||
			} else if sym.kind in [.array, .array_fixed, .map] {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -195,6 +195,9 @@ pub fn parse_args(args []string) (&Preferences, string) {
 | 
			
		|||
			'-usecache' {
 | 
			
		||||
				res.use_cache = true
 | 
			
		||||
			}
 | 
			
		||||
			'-prealloc' {
 | 
			
		||||
				res.prealloc = true
 | 
			
		||||
			}
 | 
			
		||||
			'-keepc' {
 | 
			
		||||
				eprintln('-keepc is deprecated. V always keeps the generated .tmp.c files now.')
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue