all: re-enable -prealloc

pull/5798/head
Alexander Medvednikov 2020-07-11 11:25:53 +02:00
parent 2ea187fb85
commit a1073105e1
3 changed files with 54 additions and 30 deletions

View File

@ -136,6 +136,7 @@ pub fn malloc(n int) byteptr {
panic('malloc(<=0)') panic('malloc(<=0)')
} }
$if prealloc { $if prealloc {
//println('p')
res := g_m2_ptr res := g_m2_ptr
unsafe { unsafe {
g_m2_ptr += n g_m2_ptr += n

View File

@ -15,8 +15,8 @@ import v.depgraph
const ( const (
c_reserved = ['delete', 'exit', 'link', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', c_reserved = ['delete', 'exit', 'link', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic',
'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register', 'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void',
'void', 'volatile', 'while', 'new', 'namespace', 'class', 'typename'] 'volatile', 'while', 'new', 'namespace', 'class', 'typename']
// same order as in token.Kind // same order as in token.Kind
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
// when operands are switched // 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('#define _VAUTOFREE (1)')
// g.comptime_defines.writeln('unsigned char* g_cur_str;') // 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 { if g.pref.is_livemain || g.pref.is_liveshared {
g.generate_hotcode_reloading_declarations() g.generate_hotcode_reloading_declarations()
} }
@ -409,7 +412,7 @@ fn (mut g Gen) register_optional(t table.Type) string {
} }
fn (mut g Gen) find_or_register_shared(t table.Type, base string) string { fn (mut g Gen) find_or_register_shared(t table.Type, base string) string {
sh_typ := '__shared__$base' sh_typ := '__shared__$base'
t_idx := t.idx() t_idx := t.idx()
if t_idx in g.shareds { if t_idx in g.shareds {
return sh_typ return sh_typ
@ -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.write('/* sum type cast */ ($exp_styp) {.obj = ')
g.expr(expr) g.expr(expr)
g.write(', .typ = $got_idx /* $got_sym.name */}') g.write(', .typ = $got_idx /* $got_sym.name */}')
} } else {
else {
g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&($got_styp[]) {') g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&($got_styp[]) {')
g.expr(expr) g.expr(expr)
g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}') g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}')
} }
return 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) { fn (mut g Gen) gen_assert_single_expr(e ast.Expr, t table.Type) {
unknown_value := '*unknown value*' unknown_value := '*unknown value*'
match e { match e {
ast.CallExpr { g.write(ctoslit(unknown_value)) } ast.CallExpr {
ast.CastExpr { g.write(ctoslit(unknown_value)) } g.write(ctoslit(unknown_value))
ast.IndexExpr { g.write(ctoslit(unknown_value)) } }
ast.PrefixExpr { g.write(ctoslit(unknown_value)) } ast.CastExpr {
ast.MatchExpr { g.write(ctoslit(unknown_value)) } 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 { ast.Type {
sym := g.table.get_type_symbol(t) sym := g.table.get_type_symbol(t)
g.write(ctoslit('$sym.name')) 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() + ' */ ') 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 arg_len := func.func.args.len
for j, arg in func.func.args { for j, arg in func.func.args {
arg_type := g.table.get_type_symbol(arg.typ) 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 { if j < arg_len - 1 {
g.write(', ') g.write(', ')
} }
} }
g.writeln(') = $left.name;') g.writeln(') = $left.name;')
} } else {
else {
styp := g.typ(var_type) styp := g.typ(var_type)
g.writeln('$styp _var_$left.pos.pos = $left.name;') g.writeln('$styp _var_$left.pos.pos = $left.name;')
} }
@ -1240,7 +1254,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
is_call = true is_call = true
return_type = val.return_type return_type = val.return_type
} }
// TODO: no buffer fiddling // TODO: no buffer fiddling
ast.AnonFn { ast.AnonFn {
if blank_assign { if blank_assign {
g.write('{') g.write('{')
@ -2112,16 +2126,16 @@ fn (mut g Gen) lock_expr(node ast.LockExpr) {
deref := if id.is_mut { '->' } else { '.' } deref := if id.is_mut { '->' } else { '.' }
lock_prefix := if node.is_rlock { `r` } else { `w` } lock_prefix := if node.is_rlock { `r` } else { `w` }
lock_prefixes << lock_prefix // keep for unlock 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) g.stmts(node.stmts)
// unlock in reverse order // 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] id := node.lockeds[i]
lock_prefix := lock_prefixes[i] lock_prefix := lock_prefixes[i]
name := id.name name := id.name
deref := if id.is_mut { '->' } else { '.' } 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 fields = struct_init.fields
} }
*/ */
if is_multiline { g.indent++ } if is_multiline {
g.indent++
}
// User set fields // User set fields
mut initialized := false mut initialized := false
for i, field in struct_init.fields { for i, field in struct_init.fields {
@ -2929,7 +2945,6 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
} else { } else {
g.write(',') g.write(',')
} }
initialized = true initialized = true
continue continue
} }
@ -2947,11 +2962,12 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
} else { } else {
g.write(',') g.write(',')
} }
initialized = true initialized = true
} }
} }
if is_multiline { g.indent-- } if is_multiline {
g.indent--
}
// if struct_init.fields.len == 0 && info.fields.len == 0 { // if struct_init.fields.len == 0 && info.fields.len == 0 {
if !initialized { if !initialized {
g.write('\n#ifndef __cplusplus\n0\n#endif\n') 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() } // mb_size := if s_str_buf_size == '' { 1 } else { s_str_buf_size.int() }
// g.writeln('g_str_buf = malloc( ${mb_size} * 1024 * 1000 );') // 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('\tbuiltin_init();')
g.writeln('\tvinit_string_literals();') g.writeln('\tvinit_string_literals();')
// //
@ -3297,7 +3317,7 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
field_deps << dep field_deps << dep
} }
} }
// table.Interface {} // table.Interface {}
else {} else {}
} }
// add type and dependant types to graph // add type and dependant types to graph
@ -3757,11 +3777,11 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
'linux_or_macos' { 'linux_or_macos' {
return '' return ''
} }
// //
'js' { 'js' {
return '_VJS' return '_VJS'
} }
// compilers: // compilers:
'tinyc' { 'tinyc' {
return '__TINYC__' return '__TINYC__'
} }
@ -3777,7 +3797,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
'cplusplus' { 'cplusplus' {
return '__cplusplus' return '__cplusplus'
} }
// other: // other:
'debug' { 'debug' {
return '_VDEBUG' return '_VDEBUG'
} }
@ -3788,7 +3808,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string {
return '__GLIBC__' return '__GLIBC__'
} }
'prealloc' { 'prealloc' {
return 'VPREALLOC' return '_VPREALLOC'
} }
'no_bounds_checking' { 'no_bounds_checking' {
return 'CUSTOM_DEFINE_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('string ${str_fn_name}($styp it) { /* gen_str_for_enum */')
g.auto_str_funcs.writeln('\tswitch(it) {') g.auto_str_funcs.writeln('\tswitch(it) {')
// Only use the first multi value on the lookup // 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 { for val in info.vals {
if info.is_multi_allowed && val in seen { if info.is_multi_allowed && val in seen {
continue 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, ') g.auto_str_funcs.write('indents, ')
if has_custom_str { if has_custom_str {
g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)} ) ') 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 ) ') 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] { } else if sym.kind in [.array, .array_fixed, .map] {

View File

@ -195,6 +195,9 @@ pub fn parse_args(args []string) (&Preferences, string) {
'-usecache' { '-usecache' {
res.use_cache = true res.use_cache = true
} }
'-prealloc' {
res.prealloc = true
}
'-keepc' { '-keepc' {
eprintln('-keepc is deprecated. V always keeps the generated .tmp.c files now.') eprintln('-keepc is deprecated. V always keeps the generated .tmp.c files now.')
} }