Skip to content

Commit

Permalink
all: fix new int type promotion rules and cleanup native gen() (#19535)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Oct 10, 2023
1 parent 37e5616 commit 582d593
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 109 deletions.
104 changes: 52 additions & 52 deletions vlib/v/ast/types.v
Expand Up @@ -567,31 +567,31 @@ pub const (
charptr_type_idx = 4
i8_type_idx = 5
i16_type_idx = 6
int_type_idx = 7
i64_type_idx = 8
isize_type_idx = 9
u8_type_idx = 10
u16_type_idx = 11
u32_type_idx = 12
u64_type_idx = 13
usize_type_idx = 14
f32_type_idx = 15
f64_type_idx = 16
char_type_idx = 17
bool_type_idx = 18
none_type_idx = 19
string_type_idx = 20
rune_type_idx = 21
array_type_idx = 22
map_type_idx = 23
chan_type_idx = 24
any_type_idx = 25
float_literal_type_idx = 26
int_literal_type_idx = 27
thread_type_idx = 28
error_type_idx = 29
nil_type_idx = 30
i32_type_idx = 31
i32_type_idx = 7
int_type_idx = 8
i64_type_idx = 9
isize_type_idx = 10
u8_type_idx = 11
u16_type_idx = 12
u32_type_idx = 13
u64_type_idx = 14
usize_type_idx = 15
f32_type_idx = 16
f64_type_idx = 17
char_type_idx = 18
bool_type_idx = 19
none_type_idx = 20
string_type_idx = 21
rune_type_idx = 22
array_type_idx = 23
map_type_idx = 24
chan_type_idx = 25
any_type_idx = 26
float_literal_type_idx = 27
int_literal_type_idx = 28
thread_type_idx = 29
error_type_idx = 30
nil_type_idx = 31
)

// Note: builtin_type_names must be in the same order as the idx consts above
Expand Down Expand Up @@ -628,8 +628,9 @@ pub const (
byteptr_type = new_type(byteptr_type_idx)
charptr_type = new_type(charptr_type_idx)
i8_type = new_type(i8_type_idx)
int_type = new_type(int_type_idx)
i16_type = new_type(i16_type_idx)
i32_type = new_type(i32_type_idx)
int_type = new_type(int_type_idx)
i64_type = new_type(i64_type_idx)
isize_type = new_type(isize_type_idx)
u8_type = new_type(u8_type_idx)
Expand Down Expand Up @@ -657,7 +658,6 @@ pub const (
voidptr_types = new_voidptr_types()
cptr_types = merge_types(voidptr_types, byteptr_types, charptr_types)
nil_type = new_type(nil_type_idx)
i32_type = new_type(i32_type_idx)
)

fn new_charptr_types() []Type {
Expand Down Expand Up @@ -929,37 +929,38 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_sym(kind: .charptr, name: 'charptr', cname: 'charptr', mod: 'builtin') // 4
t.register_sym(kind: .i8, name: 'i8', cname: 'i8', mod: 'builtin') // 5
t.register_sym(kind: .i16, name: 'i16', cname: 'i16', mod: 'builtin') // 6
t.register_sym(kind: .int, name: 'int', cname: int_type_name, mod: 'builtin') // 7
t.register_sym(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin') // 8
t.register_sym(kind: .isize, name: 'isize', cname: 'isize', mod: 'builtin') // 9
t.register_sym(kind: .u8, name: 'u8', cname: 'u8', mod: 'builtin') // 10
t.register_sym(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin') // 11
t.register_sym(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin') // 12
t.register_sym(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin') // 13
t.register_sym(kind: .usize, name: 'usize', cname: 'usize', mod: 'builtin') // 14
t.register_sym(kind: .f32, name: 'f32', cname: 'f32', mod: 'builtin') // 15
t.register_sym(kind: .f64, name: 'f64', cname: 'f64', mod: 'builtin') // 16
t.register_sym(kind: .char, name: 'char', cname: 'char', mod: 'builtin') // 17
t.register_sym(kind: .bool, name: 'bool', cname: 'bool', mod: 'builtin') // 18
t.register_sym(kind: .none_, name: 'none', cname: 'none', mod: 'builtin') // 19
t.register_sym(kind: .string, name: 'string', cname: 'string', mod: 'builtin') // 20
t.register_sym(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin') // 21
t.register_sym(kind: .array, name: 'array', cname: 'array', mod: 'builtin') // 22
t.register_sym(kind: .map, name: 'map', cname: 'map', mod: 'builtin') // 23
t.register_sym(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin') // 24
t.register_sym(kind: .any, name: 'any', cname: 'any', mod: 'builtin') // 25
t.register_sym(kind: .i32, name: 'i32', cname: 'i32', mod: 'builtin') // 7
t.register_sym(kind: .int, name: 'int', cname: int_type_name, mod: 'builtin') // 8
t.register_sym(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin') // 9
t.register_sym(kind: .isize, name: 'isize', cname: 'isize', mod: 'builtin') // 10
t.register_sym(kind: .u8, name: 'u8', cname: 'u8', mod: 'builtin') // 11
t.register_sym(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin') // 12
t.register_sym(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin') // 13
t.register_sym(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin') // 14
t.register_sym(kind: .usize, name: 'usize', cname: 'usize', mod: 'builtin') // 15
t.register_sym(kind: .f32, name: 'f32', cname: 'f32', mod: 'builtin') // 16
t.register_sym(kind: .f64, name: 'f64', cname: 'f64', mod: 'builtin') // 17
t.register_sym(kind: .char, name: 'char', cname: 'char', mod: 'builtin') // 18
t.register_sym(kind: .bool, name: 'bool', cname: 'bool', mod: 'builtin') // 19
t.register_sym(kind: .none_, name: 'none', cname: 'none', mod: 'builtin') // 20
t.register_sym(kind: .string, name: 'string', cname: 'string', mod: 'builtin') // 21
t.register_sym(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin') // 22
t.register_sym(kind: .array, name: 'array', cname: 'array', mod: 'builtin') // 23
t.register_sym(kind: .map, name: 'map', cname: 'map', mod: 'builtin') // 24
t.register_sym(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin') // 25
t.register_sym(kind: .any, name: 'any', cname: 'any', mod: 'builtin') // 26
t.register_sym(
kind: .float_literal
name: 'float literal'
cname: 'float_literal'
mod: 'builtin'
) // 26
) // 27
t.register_sym(
kind: .int_literal
name: 'int literal'
cname: 'int_literal'
mod: 'builtin'
) // 27
) // 28
t.register_sym(
kind: .thread
name: 'thread'
Expand All @@ -968,10 +969,9 @@ pub fn (mut t Table) register_builtin_type_symbols() {
info: Thread{
return_type: ast.void_type
}
) // 28
t.register_sym(kind: .interface_, name: 'IError', cname: 'IError', mod: 'builtin') // 29
t.register_sym(kind: .voidptr, name: 'nil', cname: 'voidptr', mod: 'builtin') // 30
t.register_sym(kind: .i32, name: 'i32', cname: 'i32', mod: 'builtin') // 31
) // 29
t.register_sym(kind: .interface_, name: 'IError', cname: 'IError', mod: 'builtin') // 30
t.register_sym(kind: .voidptr, name: 'nil', cname: 'voidptr', mod: 'builtin') // 31
}

[inline]
Expand Down
4 changes: 1 addition & 3 deletions vlib/v/builder/nativebuilder/nativebuilder.v
Expand Up @@ -58,8 +58,6 @@ pub fn build_native(mut b builder.Builder, v_files []string, out_file string) {
eprintln('Error: Only arm64 and amd64 are supported by V')
}
}
stats_lines, stats_bytes := native.gen(b.parsed_files, b.table, out_file, b.pref)
b.stats_lines = int(stats_lines)
b.stats_bytes = int(stats_bytes)
b.stats_lines, b.stats_bytes = native.gen(b.parsed_files, b.table, out_file, b.pref)
util.timing_measure('Native GEN')
}
14 changes: 11 additions & 3 deletions vlib/v/checker/infix.v
Expand Up @@ -595,13 +595,21 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
left_type
} else {
// signed types' idx adds with 5 will get correct relative unsigned type
// i8 => byte
// i16 => u16
// i8 => byte
// i16 => u16
// int => u32
// i64 => u64
// isize => usize
// i128 => u128 NOT IMPLEMENTED YET
left_type.idx() + ast.u32_type_idx - ast.int_type_idx
match left_type.idx() {
ast.i8_type_idx { ast.u8_type_idx }
ast.i16_type_idx { ast.u16_type_idx }
ast.i32_type_idx { ast.u32_type_idx }
ast.int_type_idx { ast.u32_type_idx }
ast.i64_type_idx { ast.u64_type_idx }
ast.isize_type_idx { ast.usize_type_idx }
else { 0 }
}
}

if modified_left_type == 0 {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/comptime_if_option_string_test.out
@@ -1 +1 @@
202201
212211
4 changes: 2 additions & 2 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -476,14 +476,14 @@ pub fn gen(files []&ast.File, table &ast.Table, pref_ &pref.Preferences) (string
// to make sure type idx's are the same in cached mods
if g.pref.build_mode == .build_module {
for idx, sym in g.table.type_symbols {
if idx in [0, 30] {
if idx in [0, 31] {
continue
}
g.definitions.writeln('int _v_type_idx_${sym.cname}();')
}
} else if g.pref.use_cache {
for idx, sym in g.table.type_symbols {
if idx in [0, 30] {
if idx in [0, 31] {
continue
}
g.definitions.writeln('int _v_type_idx_${sym.cname}() { return ${idx}; };')
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/native/gen.v
Expand Up @@ -327,7 +327,7 @@ fn get_backend(arch pref.Arch, target_os pref.OS) !CodeGen {
return error('unsupported architecture')
}

pub fn gen(files []&ast.File, table &ast.Table, out_name string, pref_ &pref.Preferences) (i32, i32) {
pub fn gen(files []&ast.File, table &ast.Table, out_name string, pref_ &pref.Preferences) (int, int) {
exe_name := if pref_.os == .windows && !out_name.ends_with('.exe') {
out_name + '.exe'
} else {
Expand Down Expand Up @@ -369,7 +369,7 @@ pub fn gen(files []&ast.File, table &ast.Table, out_name string, pref_ &pref.Pre
g.generate_builtins()
g.generate_footer()

return g.nlines, i32(g.buf.len)
return g.nlines, g.buf.len
}

// used in macho_test.v
Expand Down
88 changes: 45 additions & 43 deletions vlib/v/markused/markused.v
Expand Up @@ -62,53 +62,55 @@ pub fn mark_used(mut table ast.Table, pref_ &pref.Preferences, ast_files []&ast.
'4.vstring_with_len',
'4.vstring_literal',
// byte. methods
'10.str_escaped',
'11.str_escaped',
// string. methods
'20.add',
'20.trim_space',
'20.repeat',
'20.replace',
'20.clone',
'20.clone_static',
'20.trim',
'20.substr',
'20.substr_ni',
'20.at',
'20.at_with_check',
'20.index_kmp',
'21.add',
'21.trim_space',
'21.repeat',
'21.replace',
'21.clone',
'21.clone_static',
'21.trim',
'21.substr',
'21.substr_ni',
'21.at',
'21.at_with_check',
'21.index_kmp',
// string. ==, !=, etc...
'20.eq',
'20.ne',
'20.lt',
'20.gt',
'20.le',
'20.ge',
'21.eq',
'21.ne',
'21.lt',
'21.gt',
'21.le',
'21.ge',
'fast_string_eq',
// other array methods
'22.get',
'22.set',
'22.get_unsafe',
'22.set_unsafe',
'22.get_with_check', // used for `x := a[i] or {}`
'22.clone_static_to_depth',
'22.clone_to_depth',
'22.first',
'22.last',
'22.pointers', // TODO: handle generic methods calling array primitives more precisely in pool_test.v
'22.reverse',
'22.repeat_to_depth',
'22.slice',
'22.slice_ni',
'61.get',
'61.set',
'65558.last',
'65558.pop',
'65558.push',
'65558.insert_many',
'65558.prepend_many',
'65558.reverse',
'65558.set',
'65558.set_unsafe',
'23.get',
'23.set',
'23.get_unsafe',
'23.set_unsafe',
'23.get_with_check', // used for `x := a[i] or {}`
'23.clone_static_to_depth',
'23.clone_to_depth',
'23.first',
'23.last',
'23.pointers', // TODO: handle generic methods calling array primitives more precisely in pool_test.v
'23.reverse',
'23.repeat_to_depth',
'23.slice',
'23.slice_ni',
// map methods
'24.get',
'24.set',
// reference array methods
'65559.last',
'65559.pop',
'65559.push',
'65559.insert_many',
'65559.prepend_many',
'65559.reverse',
'65559.set',
'65559.set_unsafe',
// TODO: process the _vinit const initializations automatically too
'json.decode_string',
'json.decode_int',
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/tests/typeof_type_test.v
Expand Up @@ -5,17 +5,17 @@ fn test_typeof_fn() {
}

fn test_typeof_int() {
assert typeof[int]().idx == 7
assert typeof[int]().idx == 8
assert typeof[int]().name == 'int'
}

fn test_typeof_u32() {
assert typeof[u32]().idx == 12
assert typeof[u32]().idx == 13
assert typeof[u32]().name == 'u32'
}

fn test_typeof_string() {
assert typeof[string]().idx == 20
assert typeof[string]().idx == 21
assert typeof[string]().name == 'string'
}

Expand Down

0 comments on commit 582d593

Please sign in to comment.