From c221b3226b50b40472864ab261e781a44bf6a5b9 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Sat, 13 Sep 2025 15:03:52 +0800 Subject: [PATCH] cgen,builtin: support for 64bit int 1 (#25236) --- vlib/builtin/array.v | 6 +- vlib/builtin/int.v | 14 +- vlib/builtin/int_d_new_int.v | 7 - vlib/builtin/js/int.js.v | 6 +- vlib/builtin/string.v | 9 +- vlib/builtin/string_interpolation.v | 2 +- vlib/builtin/wasm/wasi/int_notd_no_imports.v | 6 +- vlib/json/tests/json_sumtype_test.v | 20 +++ vlib/math/limit.v | 10 +- vlib/math/min_max_abs_test.v | 5 + vlib/v/ast/ast.v | 15 +- vlib/v/ast/table.v | 12 +- vlib/v/ast/types.v | 83 +++++++---- vlib/v/checker/assign.v | 16 +- vlib/v/checker/check_types.v | 48 ++++-- vlib/v/checker/checker.v | 24 ++- vlib/v/checker/fn.v | 4 +- vlib/v/checker/infix.v | 22 +-- vlib/v/checker/match.v | 14 +- vlib/v/checker/tests/enum_field_overflow.out | 6 +- vlib/v/checker/tests/enum_field_overflow.vv | 2 +- .../tests/enum_field_value_overflow.out | 8 +- .../tests/enum_field_value_overflow.vv | 4 +- vlib/v/gen/c/array.v | 65 +++----- vlib/v/gen/c/assign.v | 2 +- vlib/v/gen/c/auto_eq_methods.v | 8 +- vlib/v/gen/c/auto_free_methods.v | 2 +- vlib/v/gen/c/auto_str_methods.v | 66 ++++----- vlib/v/gen/c/cgen.v | 139 ++++++++++-------- vlib/v/gen/c/cheaders.v | 12 +- vlib/v/gen/c/consts_and_globals.v | 2 +- vlib/v/gen/c/coverage.v | 8 +- vlib/v/gen/c/dumpexpr.v | 7 +- vlib/v/gen/c/fn.v | 2 +- vlib/v/gen/c/for.v | 14 +- vlib/v/gen/c/index.v | 2 +- vlib/v/gen/c/json.v | 34 +++-- vlib/v/gen/c/orm.v | 31 ++-- vlib/v/gen/c/reflection.v | 2 +- vlib/v/gen/c/str_intp.v | 58 ++++++-- vlib/v/gen/js/builtin_types.v | 14 +- vlib/v/gen/js/deep_copy.v | 2 +- vlib/v/gen/js/js.v | 15 +- vlib/v/gen/native/amd64.v | 53 ++++++- vlib/v/gen/native/gen.v | 76 +++++++--- vlib/v/gen/wasm/ops.v | 9 +- vlib/v/parser/parse_type.v | 9 +- vlib/v/pref/pref.v | 5 - 48 files changed, 610 insertions(+), 370 deletions(-) delete mode 100644 vlib/builtin/int_d_new_int.v diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index 2f2fa6084e..a05420f741 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -607,7 +607,8 @@ pub fn (mut a array) delete_last() { // Alternative: Slices can also be made with [start..end] notation // Alternative: `.slice_ni()` will always return an array. fn (a array) slice(start int, _end int) array { - end := if _end == max_int { a.len } else { _end } // max_int + // WARNNING: The is a temp solution for bootstrap! + end := if _end == max_i64 || _end == max_i32 { a.len } else { _end } // max_int $if !no_bounds_checking { if start > end { panic('array.slice: invalid slice index (start>end):' + impl_i64_to_string(i64(start)) + @@ -644,7 +645,8 @@ fn (a array) slice(start int, _end int) array { // This function always return a valid array. fn (a array) slice_ni(_start int, _end int) array { // a.flags.clear(.noslices) - mut end := if _end == max_int { a.len } else { _end } // max_int + // WARNNING: The is a temp solution for bootstrap! + mut end := if _end == max_i64 || _end == max_i32 { a.len } else { _end } // max_int mut start := _start if start < 0 { diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index c69679d93d..93c8687928 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -42,15 +42,23 @@ pub const max_i16 = i16(32767) pub const min_i32 = i32(-2147483648) pub const max_i32 = i32(2147483647) -pub const min_int = int(-2147483648) -pub const max_int = int(2147483647) - // -9223372036854775808 is wrong, because C compilers parse literal values // without sign first, and 9223372036854775808 overflows i64, hence the // consecutive subtraction by 1 pub const min_i64 = i64(-9223372036854775807 - 1) pub const max_i64 = i64(9223372036854775807) +pub const min_int = $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + int(min_i64) +} $else { + int(min_i32) +} +pub const max_int = $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + int(max_i64) +} $else { + int(max_i32) +} + pub const min_u8 = u8(0) pub const max_u8 = u8(255) diff --git a/vlib/builtin/int_d_new_int.v b/vlib/builtin/int_d_new_int.v deleted file mode 100644 index 7fc3997c4c..0000000000 --- a/vlib/builtin/int_d_new_int.v +++ /dev/null @@ -1,7 +0,0 @@ -module builtin - -// str returns the value of the `int` as a `string`. -// Example: assert int(-2020).str() == '-2020' -pub fn int_str(n int) string { - return i64(n).str() -} diff --git a/vlib/builtin/js/int.js.v b/vlib/builtin/js/int.js.v index 467ecc27ae..1e31ee5e3a 100644 --- a/vlib/builtin/js/int.js.v +++ b/vlib/builtin/js/int.js.v @@ -11,15 +11,15 @@ pub const max_i16 = i16(32767) pub const min_i32 = i32(-2147483648) pub const max_i32 = i32(2147483647) -pub const min_int = min_i32 -pub const max_int = max_i32 - // -9223372036854775808 is wrong, because C compilers parse literal values // without sign first, and 9223372036854775808 overflows i64, hence the // consecutive subtraction by 1 pub const min_i64 = i64(-9223372036854775807 - 1) pub const max_i64 = i64(9223372036854775807) +pub const min_int = int(min_i32) +pub const max_int = int(max_i32) + pub const min_u8 = u8(0) pub const max_u8 = u8(255) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 53f9c87f08..f2ad288545 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1165,7 +1165,8 @@ pub fn (s string) split_by_space() []string { // Example: assert 'ABCD'.substr(1,3) == 'BC' @[direct_array_access] pub fn (s string) substr(start int, _end int) string { - end := if _end == max_int { s.len } else { _end } // max_int + // WARNNING: The is a temp solution for bootstrap! + end := if _end == max_i64 || _end == max_i32 { s.len } else { _end } // max_int $if !no_bounds_checking { if start > end || start > s.len || end > s.len || start < 0 || end < 0 { panic('substr(' + impl_i64_to_string(start) + ', ' + impl_i64_to_string(end) + @@ -1205,7 +1206,8 @@ pub fn (s string) substr_unsafe(start int, _end int) string { // return an error when the index is out of range @[direct_array_access] pub fn (s string) substr_with_check(start int, _end int) !string { - end := if _end == max_int { s.len } else { _end } // max_int + // WARNNING: The is a temp solution for bootstrap! + end := if _end == max_i64 || _end == max_i32 { s.len } else { _end } // max_int if start > end || start > s.len || end > s.len || start < 0 || end < 0 { return error('substr(' + impl_i64_to_string(start) + ', ' + impl_i64_to_string(end) + ') out of bounds (len=' + impl_i64_to_string(s.len) + ')') @@ -1230,7 +1232,8 @@ pub fn (s string) substr_with_check(start int, _end int) !string { @[direct_array_access] pub fn (s string) substr_ni(_start int, _end int) string { mut start := _start - mut end := if _end == max_int { s.len } else { _end } // max_int + // WARNNING: The is a temp solution for bootstrap! + mut end := if _end == max_i64 || _end == max_i32 { s.len } else { _end } // borders math if start < 0 { diff --git a/vlib/builtin/string_interpolation.v b/vlib/builtin/string_interpolation.v index 9c1457d455..d89a9db69b 100644 --- a/vlib/builtin/string_interpolation.v +++ b/vlib/builtin/string_interpolation.v @@ -71,7 +71,7 @@ pub mut: d_u16 u16 d_i16 i16 d_u32 u32 - d_i32 int + d_i32 i32 d_u64 u64 d_i64 i64 d_f32 f32 diff --git a/vlib/builtin/wasm/wasi/int_notd_no_imports.v b/vlib/builtin/wasm/wasi/int_notd_no_imports.v index 0c31ce96f4..386a63a430 100644 --- a/vlib/builtin/wasm/wasi/int_notd_no_imports.v +++ b/vlib/builtin/wasm/wasi/int_notd_no_imports.v @@ -16,15 +16,15 @@ pub const max_i16 = i16(32767) pub const min_i32 = i32(-2147483648) pub const max_i32 = i32(2147483647) -pub const min_int = min_i32 -pub const max_int = max_i32 - // -9223372036854775808 is wrong, because C compilers parse literal values // without sign first, and 9223372036854775808 overflows i64, hence the // consecutive subtraction by 1 pub const min_i64 = i64(-9223372036854775807 - 1) pub const max_i64 = i64(9223372036854775807) +pub const min_int = int(min_i32) +pub const max_int = int(max_i32) + pub const min_u8 = u8(0) pub const max_u8 = u8(255) diff --git a/vlib/json/tests/json_sumtype_test.v b/vlib/json/tests/json_sumtype_test.v index 5ead3605d0..7dca3f9289 100644 --- a/vlib/json/tests/json_sumtype_test.v +++ b/vlib/json/tests/json_sumtype_test.v @@ -105,3 +105,23 @@ fn test_sum_types() { j := json.encode(animals[0]) assert j == '{"cat_name":"Whiskers","_type":"Cat"}' } + +type Value = string | i32 + +struct Node { + value Value +} + +fn test_sum_types_with_i32() { + data1 := json.encode([Node{i32(128)}, Node{'mystring'}]) + assert data1 == '[{"value":128},{"value":"mystring"}]' + + node := json.decode([]Node, data1) or { + println(err) + assert false + return + } + assert node.len == 2 + assert node[0].value == Value(i32(128)) + assert node[1].value == Value('mystring') +} diff --git a/vlib/math/limit.v b/vlib/math/limit.v index c9b80b6f43..ef185a89c7 100644 --- a/vlib/math/limit.v +++ b/vlib/math/limit.v @@ -31,10 +31,7 @@ pub fn maxof[T]() T { } $else $if T is f64 { return max_f64 } $else $if T is int { - $if new_int ? { - return int(max_i64) - } - return int(max_i32) + return max_int } $else { panic('A maximum value of the type `' + typeof[T]().name + '` is not defined.') } @@ -68,10 +65,7 @@ pub fn minof[T]() T { } $else $if T is f64 { return -max_f64 } $else $if T is int { - $if new_int ? { - return int(min_i64) - } - return int(min_i32) + return min_int } $else { panic('A minimum value of the type `' + typeof[T]().name + '` is not defined.') } diff --git a/vlib/math/min_max_abs_test.v b/vlib/math/min_max_abs_test.v index 2b1d133e1d..9dfcc3648a 100644 --- a/vlib/math/min_max_abs_test.v +++ b/vlib/math/min_max_abs_test.v @@ -20,3 +20,8 @@ fn test_abs() { assert math.abs(1.2345) == 1.2345 assert math.abs(-5.5) == 5.5 } + +fn test_max_min_int_has_type_of_int() { + assert math.max(int(100), min_int) == 100 + assert math.min(int(100), max_int) == 100 +} diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5c948082aa..c1ba46c0bb 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -11,9 +11,9 @@ import v.pref import sync.stdatomic // V type names that cannot be used as global var name -pub const global_reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', - 'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread', - 'array'] +pub const global_reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'i32', 'int', 'i64', + 'u8', 'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', + 'thread', 'array'] pub const result_name = '_result' pub const option_name = '_option' @@ -24,10 +24,9 @@ pub const builtins = ['string', 'array', 'DenseArray', 'map', 'Error', 'IError', pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl -// pub const int_type_name = $if amd64 || arm64 { -pub const int_type_name = $if new_int ? { - //'int' - 'i64' +pub const int_type_name = $if new_int ? + && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + 'vint_t' } $else { 'int' } @@ -2816,7 +2815,7 @@ pub fn (expr Expr) is_literal() bool { } CastExpr { !expr.has_arg && expr.expr.is_literal() && (expr.typ.is_any_kind_of_pointer() - || expr.typ in [i8_type, i16_type, int_type, i64_type, u8_type, u16_type, u32_type, u64_type, f32_type, f64_type, char_type, bool_type, rune_type]) + || expr.typ in [i8_type, i16_type, i32_type, int_type, i64_type, u8_type, u16_type, u32_type, u64_type, f32_type, f64_type, char_type, bool_type, rune_type]) } SizeOf, IsRefType { expr.is_type || expr.expr.is_literal() diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 5443519302..a92694c813 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1340,7 +1340,7 @@ pub fn (mut t Table) find_or_register_fn_type(f Fn, is_anon bool, has_decl bool) ) } -pub fn (mut t Table) add_placeholder_type(name string, language Language) int { +pub fn (mut t Table) add_placeholder_type(name string, cname string, language Language) int { mut modname := '' if name.contains('.') { modname = name.all_before_last('.') @@ -1348,7 +1348,7 @@ pub fn (mut t Table) add_placeholder_type(name string, language Language) int { ph_type := TypeSymbol{ kind: .placeholder name: name - cname: util.no_dots(name).replace_each(['&', '']) + cname: util.no_dots(cname).replace_each(['&', '']) language: language mod: modname is_pub: true @@ -1591,7 +1591,7 @@ pub fn (mut t Table) bitsize_to_type(bit_size int) Type { return i16_type } 32 { - return int_type + return i32_type } 64 { return i64_type @@ -1854,6 +1854,7 @@ pub fn (mut t Table) convert_generic_type(generic_type Type, generic_names []str if sym.info.is_generic { mut nrt := '${sym.name}[' mut rnrt := '${sym.rname}[' + mut cnrt := '${sym.cname}[' mut t_generic_names := generic_names.clone() mut t_to_types := to_types.clone() if sym.generic_types.len > 0 && sym.generic_types.len == sym.info.generic_types.len @@ -1886,9 +1887,11 @@ pub fn (mut t Table) convert_generic_type(generic_type Type, generic_names []str } nrt += gts.name rnrt += gts.name + cnrt += gts.cname if i != sym.info.generic_types.len - 1 { nrt += ', ' rnrt += ', ' + cnrt += ', ' } } else { return none @@ -1896,11 +1899,12 @@ pub fn (mut t Table) convert_generic_type(generic_type Type, generic_names []str } nrt += ']' rnrt += ']' + cnrt += ']' mut idx := t.type_idxs[nrt] if idx == 0 { idx = t.type_idxs[rnrt] if idx == 0 { - idx = t.add_placeholder_type(nrt, .v) + idx = t.add_placeholder_type(nrt, cnrt, .v) } } return new_type(idx).derive_add_muls(generic_type).clear_flag(.generic) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 0e30e7d597..b5905955b2 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -625,18 +625,46 @@ pub fn (typ Type) is_unsigned() bool { pub fn (typ Type) flip_signedness() Type { return match typ { - i8_type { u8_type } - i16_type { u16_type } - int_type { u32_type } - i32_type { u32_type } - isize_type { usize_type } - i64_type { u64_type } - u8_type { i8_type } - u16_type { i16_type } - u32_type { int_type } - usize_type { isize_type } - u64_type { i64_type } - else { typ } + i8_type { + u8_type + } + i16_type { + u16_type + } + i32_type { + u32_type + } + i64_type { + u64_type + } + int_type { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + u64_type + } $else { + u32_type + } + } + isize_type { + usize_type + } + u8_type { + i8_type + } + u16_type { + i16_type + } + u32_type { + i32_type + } + u64_type { + i64_type + } + usize_type { + isize_type + } + else { + void_type + } } } @@ -699,18 +727,18 @@ pub const error_type_idx = 30 pub const nil_type_idx = 31 // Note: builtin_type_names must be in the same order as the idx consts above -pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'int', 'i64', - 'isize', 'u8', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', 'string', - 'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', 'Error', 'nil', - 'i32'] +pub const builtin_type_names = ['void', 'voidptr', 'byteptr', 'charptr', 'i8', 'i16', 'i32', 'int', + 'i64', 'isize', 'u8', 'u16', 'u32', 'u64', 'usize', 'f32', 'f64', 'char', 'bool', 'none', + 'string', 'rune', 'array', 'map', 'chan', 'any', 'float_literal', 'int_literal', 'thread', + 'Error', 'nil'] pub const builtin_type_names_matcher = token.new_keywords_matcher_from_array_trie(builtin_type_names) -pub const integer_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, u8_type_idx, - u16_type_idx, u32_type_idx, u64_type_idx, isize_type_idx, usize_type_idx, int_literal_type_idx, - rune_type_idx, i32_type_idx] -pub const signed_integer_type_idxs = [char_type_idx, i8_type_idx, i16_type_idx, int_type_idx, - i64_type_idx, i32_type_idx, isize_type_idx] +pub const integer_type_idxs = [i8_type_idx, i16_type_idx, i32_type_idx, int_type_idx, i64_type_idx, + u8_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, isize_type_idx, usize_type_idx, + int_literal_type_idx, rune_type_idx] +pub const signed_integer_type_idxs = [char_type_idx, i8_type_idx, i16_type_idx, i32_type_idx, + int_type_idx, i64_type_idx, isize_type_idx] pub const unsigned_integer_type_idxs = [u8_type_idx, u16_type_idx, u32_type_idx, u64_type_idx, usize_type_idx] // C will promote any type smaller than int to int in an expression @@ -1161,7 +1189,7 @@ pub fn (t &TypeSymbol) is_pointer() bool { @[inline] pub fn (t &TypeSymbol) is_int() bool { - res := t.kind in [.i8, .i16, .int, .i64, .i32, .isize, .u8, .u16, .u32, .u64, .usize, + res := t.kind in [.i8, .i16, .i32, .int, .i64, .isize, .u8, .u16, .u32, .u64, .usize, .int_literal, .rune] if !res && t.kind == .alias { return (t.info as Alias).parent_type.is_int() @@ -1232,14 +1260,9 @@ pub fn (t &Table) type_size(typ Type) (int, int) { align = 4 } .int { - $if new_int ? { - $if arm64 || amd64 { - size = 8 - align = 8 - } $else { - size = 4 - align = 4 - } + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + size = 8 + align = 8 } $else { size = 4 align = 4 diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index af0b4b2633..b53703e6b1 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -746,14 +746,14 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', } else if left_type.is_unsigned() { left_type } else { - // signed types' idx adds with 5 will get correct relative unsigned type - // i8 => byte - // i16 => u16 - // int => u32 - // i64 => u64 - // isize => usize - // i128 => u128 NOT IMPLEMENTED YET - ast.idx_to_type(left_type.idx() + ast.u32_type_idx - ast.int_type_idx) + // signed types => unsigned types + unsigned_type := left_type.flip_signedness() + if unsigned_type == ast.void_type { + c.error('invalid operation: shift on type `${c.table.sym(left_type).name}`', + node.pos) + ast.void_type + } + unsigned_type } node = ast.AssignStmt{ diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index f05b5b749d..dfde41f54d 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -625,18 +625,46 @@ fn (mut c Checker) check_shift(mut node ast.InfixExpr, left_type_ ast.Type, righ return left_type } moffset := match left_type_final { - ast.char_type { 7 } - ast.i8_type { 7 } - ast.i16_type { 15 } - ast.int_type { 31 } - ast.i64_type { 63 } + ast.char_type { + 7 + } + ast.i8_type { + 7 + } + ast.i16_type { + 15 + } + ast.i32_type { + 31 + } + ast.int_type { + $if new_int ? + && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + 63 + } $else { + 31 + } + } + ast.i64_type { + 63 + } // - ast.u8_type { 7 } + ast.u8_type { + 7 + } // ast.u8_type { 7 } - ast.u16_type { 15 } - ast.u32_type { 31 } - ast.u64_type { 63 } - else { 64 } + ast.u16_type { + 15 + } + ast.u32_type { + 31 + } + ast.u64_type { + 63 + } + else { + 64 + } } if ival > moffset && !c.pref.translated && !c.file.is_translated { c.error('shift count for type `${left_sym_final.name}` too large (maximum: ${moffset} bits)', diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 79b6a2cdd9..dff88767f2 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -36,8 +36,8 @@ pub const fixed_array_builtin_methods = ['contains', 'index', 'any', 'all', 'wai 'sorted', 'sort_with_compare', 'sorted_with_compare', 'reverse', 'reverse_in_place', 'count'] pub const fixed_array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(fixed_array_builtin_methods) // TODO: remove `byte` from this list when it is no longer supported -pub const reserved_type_names = ['bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16', 'u32', - 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread'] +pub const reserved_type_names = ['bool', 'char', 'i8', 'i16', 'i32', 'int', 'i64', 'u8', 'u16', + 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread'] pub const reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names) pub const vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead' @@ -686,7 +686,7 @@ fn (mut c Checker) alias_type_decl(mut node ast.AliasTypeDecl) { .voidptr, .byteptr, .charptr {} .char, .rune, .bool {} .string, .enum, .none, .any {} - .i8, .i16, .int, .i64, .isize {} + .i8, .i16, .i32, .int, .i64, .isize {} .u8, .u16, .u32, .u64, .usize {} .f32, .f64 {} .interface {} @@ -2048,9 +2048,16 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) { ast.i16_type { signed, enum_imin, enum_imax = true, min_i16, max_i16 } - ast.int_type { + ast.i32_type { signed, enum_imin, enum_imax = true, min_i32, max_i32 } + ast.int_type { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + signed, enum_imin, enum_imax = true, min_i32, max_i32 + } $else { + signed, enum_imin, enum_imax = true, min_i64, max_i64 + } + } ast.i64_type { signed, enum_imin, enum_imax = true, min_i64, max_i64 } @@ -3795,9 +3802,16 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type { ast.i16_type_idx { u64(0xffff) } - ast.int_type_idx, ast.i32_type_idx { + ast.i32_type_idx { u64(0xffffffff) } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + u64(0xffffffffffffffff) + } $else { + u64(0xffffffff) + } + } ast.i64_type_idx { u64(0xffffffffffffffff) } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 1c6ebf8f7d..a07e73b0e3 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3427,7 +3427,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as '\ne.g. `users.${method_name}(a.id < b.id)`', node.pos) } } else if !(c.table.sym(elem_typ).has_method('<') - || c.table.unalias_num_type(elem_typ) in [ast.int_type, ast.int_type.ref(), ast.string_type, ast.string_type.ref(), ast.i8_type, ast.i16_type, ast.i64_type, ast.u8_type, ast.rune_type, ast.u16_type, ast.u32_type, ast.u64_type, ast.f32_type, ast.f64_type, ast.char_type, ast.bool_type, ast.float_literal_type, ast.int_literal_type]) { + || c.table.unalias_num_type(elem_typ) in [ast.int_type, ast.int_type.ref(), ast.string_type, ast.string_type.ref(), ast.i8_type, ast.i16_type, ast.i32_type, ast.i64_type, ast.u8_type, ast.rune_type, ast.u16_type, ast.u32_type, ast.u64_type, ast.f32_type, ast.f64_type, ast.char_type, ast.bool_type, ast.float_literal_type, ast.int_literal_type]) { c.error('custom sorting condition must be supplied for type `${c.table.type_to_str(elem_typ)}`', node.pos) } @@ -3778,7 +3778,7 @@ fn (mut c Checker) fixed_array_builtin_method_call(mut node ast.CallExpr, left_t '\ne.g. `users.${method_name}(a.id < b.id)`', node.pos) } } else if !(c.table.sym(elem_typ).has_method('<') - || c.table.unalias_num_type(elem_typ) in [ast.int_type, ast.int_type.ref(), ast.string_type, ast.string_type.ref(), ast.i8_type, ast.i16_type, ast.i64_type, ast.u8_type, ast.rune_type, ast.u16_type, ast.u32_type, ast.u64_type, ast.f32_type, ast.f64_type, ast.char_type, ast.bool_type, ast.float_literal_type, ast.int_literal_type]) { + || c.table.unalias_num_type(elem_typ) in [ast.int_type, ast.int_type.ref(), ast.string_type, ast.string_type.ref(), ast.i8_type, ast.i16_type, ast.i32_type, ast.i64_type, ast.u8_type, ast.rune_type, ast.u16_type, ast.u32_type, ast.u64_type, ast.f32_type, ast.f64_type, ast.char_type, ast.bool_type, ast.float_literal_type, ast.int_literal_type]) { c.error('custom sorting condition must be supplied for type `${c.table.type_to_str(elem_typ)}`', node.pos) } diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 1493278d74..6f354c2f51 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -651,22 +651,12 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { } else if left_type.is_unsigned() { left_type } else { - // signed types' idx adds with 5 will get correct relative unsigned type - // i8 => byte - // i16 => u16 - // int => u32 - // i64 => u64 - // isize => usize - // i128 => u128 NOT IMPLEMENTED YET - ast.idx_to_type(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 } - }) + unsigned_type := left_type.flip_signedness() + if unsigned_type == ast.void_type { + // signed type can't convert to an unsigned type + 0 + } + unsigned_type } if modified_left_type == 0 { diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 56f51e0cb9..4554467718 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -390,11 +390,23 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type { needs_explicit_cast = true } } - .i32, .int { + .i32 { if !(num >= min_i32 && num <= max_i32) { needs_explicit_cast = true } } + .int { + $if new_int ? && (arm64 || amd64 || rv64 + || s390x || ppc64le || loongarch64) { + if !(num >= min_i64 && num <= max_i64) { + needs_explicit_cast = true + } + } $else { + if !(num >= min_i32 && num <= max_i32) { + needs_explicit_cast = true + } + } + } .i64 { if !(num >= min_i64 && num <= max_i64) { needs_explicit_cast = true diff --git a/vlib/v/checker/tests/enum_field_overflow.out b/vlib/v/checker/tests/enum_field_overflow.out index c51d6580c4..7bca995ae8 100644 --- a/vlib/v/checker/tests/enum_field_overflow.out +++ b/vlib/v/checker/tests/enum_field_overflow.out @@ -1,11 +1,11 @@ -vlib/v/checker/tests/enum_field_overflow.vv:3:10: error: enum value `2147483647` overflows the enum type `int`, values of which have to be in [-2147483648, 2147483647] - 1 | enum Color { +vlib/v/checker/tests/enum_field_overflow.vv:3:10: error: enum value `2147483647` overflows the enum type `i32`, values of which have to be in [-2147483648, 2147483647] + 1 | enum Color as i32 { 2 | red 3 | green = 2147483647 | ~~~~~~~~~~ 4 | blue 5 | } -vlib/v/checker/tests/enum_field_overflow.vv:4:2: error: enum value overflows type `int`, which has a maximum value of 2147483647 +vlib/v/checker/tests/enum_field_overflow.vv:4:2: error: enum value overflows type `i32`, which has a maximum value of 2147483647 2 | red 3 | green = 2147483647 4 | blue diff --git a/vlib/v/checker/tests/enum_field_overflow.vv b/vlib/v/checker/tests/enum_field_overflow.vv index 8c79d29003..b44810185e 100644 --- a/vlib/v/checker/tests/enum_field_overflow.vv +++ b/vlib/v/checker/tests/enum_field_overflow.vv @@ -1,4 +1,4 @@ -enum Color { +enum Color as i32 { red green = 2147483647 blue diff --git a/vlib/v/checker/tests/enum_field_value_overflow.out b/vlib/v/checker/tests/enum_field_value_overflow.out index 55c8cc2bb8..30c8e9664b 100644 --- a/vlib/v/checker/tests/enum_field_value_overflow.out +++ b/vlib/v/checker/tests/enum_field_value_overflow.out @@ -1,5 +1,5 @@ -vlib/v/checker/tests/enum_field_value_overflow.vv:3:10: error: enum value `2147483648` overflows the enum type `int`, values of which have to be in [-2147483648, 2147483647] - 1 | enum Color { +vlib/v/checker/tests/enum_field_value_overflow.vv:3:10: error: enum value `2147483648` overflows the enum type `i32`, values of which have to be in [-2147483648, 2147483647] + 1 | enum Color as i32 { 2 | red 3 | green = 2147483648 | ~~~~~~~~~~ @@ -40,8 +40,8 @@ vlib/v/checker/tests/enum_field_value_overflow.vv:21:10: error: enum value `3276 | ~~~~~ 22 | blue 23 | } -vlib/v/checker/tests/enum_field_value_overflow.vv:27:10: error: enum value `2147483648` overflows the enum type `int`, values of which have to be in [-2147483648, 2147483647] - 25 | enum ColorI32 { +vlib/v/checker/tests/enum_field_value_overflow.vv:27:10: error: enum value `2147483648` overflows the enum type `i32`, values of which have to be in [-2147483648, 2147483647] + 25 | enum ColorI32 as i32 { 26 | red 27 | green = 2147483648 | ~~~~~~~~~~ diff --git a/vlib/v/checker/tests/enum_field_value_overflow.vv b/vlib/v/checker/tests/enum_field_value_overflow.vv index 13802116c3..2da3bda9a8 100644 --- a/vlib/v/checker/tests/enum_field_value_overflow.vv +++ b/vlib/v/checker/tests/enum_field_value_overflow.vv @@ -1,4 +1,4 @@ -enum Color { +enum Color as i32 { red green = 2147483648 blue @@ -22,7 +22,7 @@ enum ColorI16 as i16 { blue } -enum ColorI32 { +enum ColorI32 as i32 { red green = 2147483648 blue diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 1a4f1e9bd6..0a3ee8ec02 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -117,11 +117,11 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st g.writeln2(';', '{') g.indent++ g.writeln('${elem_typ_str}* pelem = (${elem_typ_str}*)${past.tmp_var};') - g.writeln('int _len = (int)sizeof(${past.tmp_var}) / sizeof(${elem_typ_str});') - g.writeln('for (int index=0; index<_len; index++, pelem++) {') + g.writeln('${ast.int_type_name} _len = (${ast.int_type_name})sizeof(${past.tmp_var}) / sizeof(${elem_typ_str});') + g.writeln('for (${ast.int_type_name} index=0; index<_len; index++, pelem++) {') g.set_current_pos_as_last_stmt_pos() g.indent++ - g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden + g.writeln('${ast.int_type_name} it = index;') // FIXME: Remove this line when it is fully forbidden g.write('*pelem = ') g.expr_with_init(node) g.writeln(';') @@ -354,10 +354,10 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.writeln2(';', '{') g.indent++ g.writeln('${elem_typ}* pelem = (${elem_typ}*)${past.tmp_var}.data;') - g.writeln('for (int index=0; index<${past.tmp_var}.len; index++, pelem++) {') + g.writeln('for (${ast.int_type_name} index=0; index<${past.tmp_var}.len; index++, pelem++) {') g.set_current_pos_as_last_stmt_pos() g.indent++ - g.writeln('int it = index;') // FIXME: Remove this line when it is fully forbidden + g.writeln('${ast.int_type_name} it = index;') // FIXME: Remove this line when it is fully forbidden if elem_type.unaliased_sym.kind != .array_fixed { g.write('*pelem = ') g.expr_with_init(node) @@ -423,7 +423,7 @@ fn (mut g Gen) array_init_with_fields(node ast.ArrayInit, elem_type Type, is_amp g.expr(node.len_expr) g.writeln(') * sizeof(${elem_styp}));') ind := g.new_tmp_var() - g.write('for (int ${ind}=0; ${ind}<') + g.write('for (${ast.int_type_name} ${ind}=0; ${ind}<') g.expr(node.len_expr) g.writeln('; ${ind}++) {') g.write('\t${tmp}[${ind}] = ') @@ -565,7 +565,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) { } i := g.new_tmp_var() - g.writeln('for (int ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') g.indent++ var_name := g.get_array_expr_param_name(mut expr) is_auto_heap := expr is ast.CastExpr && (expr.expr is ast.Ident && expr.expr.is_auto_heap()) @@ -958,7 +958,7 @@ fn (mut g Gen) gen_fixed_array_reverse_in_place(node ast.CallExpr) { i := g.new_tmp_var() left_var := g.expr_string(node.left) g.empty_line = true - g.writeln('for (int ${i} = 0; ${i} < ${info.size}/2; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${info.size}/2; ++${i}) {') g.writeln('\tmemcpy(&${tmp_var}, &${left_var}[${i}], sizeof(${elem_styp}));') g.writeln('\tmemcpy(&${left_var}[${i}], &${left_var}[${info.size}-${i}-1], sizeof(${elem_styp}));') g.writeln('\tmemcpy(&${left_var}[${info.size}-${i}-1], &${tmp_var}, sizeof(${elem_styp}));') @@ -995,7 +995,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) { } i := g.new_tmp_var() - g.writeln('for (int ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') g.indent++ g.write_prepared_var(var_name, info.elem_type, elem_type_str, past.tmp_var, i, true, false) @@ -1133,10 +1133,6 @@ fn (mut g Gen) get_array_contains_method(typ ast.Type) string { fn (mut g Gen) gen_array_contains_methods() { mut done := []ast.Type{} - mut got_int_str := false - $if new_int ? { - println(g.array_contains_types) - } for t in g.array_contains_types { left_final_sym := g.table.final_sym(t) if left_final_sym.idx in done || g.table.sym(t).has_method('contains') { @@ -1147,20 +1143,6 @@ fn (mut g Gen) gen_array_contains_methods() { mut left_type_str := g.styp(t) fn_name := '${left_type_str}_contains' - $if new_int ? { - if fn_name == 'Array_i64_contains' { - if got_int_str { - continue - } else { - got_int_str = true - } - } - - // if t == ast.int_type_idx || t == ast.i64_type_idx { - // continue - //} - } - if left_final_sym.kind == .array { elem_type := (left_final_sym.info as ast.Array).elem_type mut elem_type_str := g.styp(elem_type) @@ -1172,7 +1154,7 @@ fn (mut g Gen) gen_array_contains_methods() { } g.type_definitions.writeln('${g.static_non_parallel}bool ${fn_name}(${left_type_str} a, ${elem_type_str} v);') fn_builder.writeln('${g.static_non_parallel}bool ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') - fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < a.len; ++i) {') if elem_kind == .string { fn_builder.writeln('\t\tif (builtin__fast_string_eq(((string*)a.data)[i], v)) {') } else if elem_kind in [.array, .array_fixed] && elem_is_not_ptr { @@ -1214,7 +1196,7 @@ fn (mut g Gen) gen_array_contains_methods() { } g.type_definitions.writeln('${g.static_non_parallel}bool ${fn_name}(${left_type_str} a, ${elem_type_str} v);') fn_builder.writeln('${g.static_non_parallel}bool ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') - fn_builder.writeln('\tfor (int i = 0; i < ${size}; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${size}; ++i) {') if elem_kind == .string { fn_builder.writeln('\t\tif (builtin__fast_string_eq(a[i], v)) {') } else if elem_kind in [.array, .array_fixed] && elem_is_not_ptr { @@ -1318,10 +1300,10 @@ fn (mut g Gen) gen_array_index_methods() { left_type_str = 'Array_voidptr' elem_type_str = 'voidptr' } - g.type_definitions.writeln('${g.static_non_parallel}int ${fn_name}(${left_type_str} a, ${elem_type_str} v);') - fn_builder.writeln('${g.static_non_parallel}int ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') + g.type_definitions.writeln('${g.static_non_parallel}${ast.int_type_name} ${fn_name}(${left_type_str} a, ${elem_type_str} v);') + fn_builder.writeln('${g.static_non_parallel}${ast.int_type_name} ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') fn_builder.writeln('\t${elem_type_str}* pelem = a.data;') - fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i, ++pelem) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < a.len; ++i, ++pelem) {') if elem_sym.kind == .string { fn_builder.writeln('\t\tif (builtin__fast_string_eq(*pelem, v)) {') } else if elem_sym.kind in [.array, .array_fixed] && !info.elem_type.is_ptr() { @@ -1362,9 +1344,9 @@ fn (mut g Gen) gen_array_index_methods() { if elem_sym.kind == .function { elem_type_str = 'voidptr' } - g.type_definitions.writeln('${g.static_non_parallel}int ${fn_name}(${left_type_str} a, ${elem_type_str} v);') - fn_builder.writeln('${g.static_non_parallel}int ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') - fn_builder.writeln('\tfor (int i = 0; i < ${info.size}; ++i) {') + g.type_definitions.writeln('${g.static_non_parallel}${ast.int_type_name} ${fn_name}(${left_type_str} a, ${elem_type_str} v);') + fn_builder.writeln('${g.static_non_parallel}${ast.int_type_name} ${fn_name}(${left_type_str} a, ${elem_type_str} v) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${info.size}; ++i) {') if elem_sym.kind == .string { fn_builder.writeln('\t\tif (builtin__fast_string_eq(a[i], v)) {') } else if elem_sym.kind in [.array, .array_fixed] && !info.elem_type.is_ptr() { @@ -1499,7 +1481,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) { } } i := g.new_tmp_var() - g.writeln('for (int ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') g.indent++ g.write_prepared_var(var_name, elem_type, elem_type_str, past.tmp_var, i, left_is_array, @@ -1577,7 +1559,8 @@ fn (mut g Gen) gen_array_count(node ast.CallExpr) { (sym.info as ast.ArrayFixed).elem_type } elem_type_str := g.styp(elem_type) - has_infix_left_var_name := g.write_prepared_tmp_value(past.tmp_var, node, 'int', '0') + has_infix_left_var_name := g.write_prepared_tmp_value(past.tmp_var, node, ast.int_type_name, + '0') mut expr := node.args[0].expr var_name := g.get_array_expr_param_name(mut expr) @@ -1590,7 +1573,7 @@ fn (mut g Gen) gen_array_count(node ast.CallExpr) { } } i := g.new_tmp_var() - g.writeln('for (int ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') g.indent++ g.write_prepared_var(var_name, elem_type, elem_type_str, past.tmp_var, i, left_is_array, @@ -1684,7 +1667,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { } } - g.writeln('for (int ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${past.tmp_var}_len; ++${i}) {') g.indent++ g.write_prepared_var(var_name, elem_type, elem_type_str, past.tmp_var, i, left_is_array, false) @@ -1774,7 +1757,7 @@ fn (mut g Gen) write_prepared_tmp_value(tmp string, node &ast.CallExpr, tmp_styp g.write('->val') } g.writeln(';') - g.writeln('int ${tmp}_len = ${tmp}_orig.len;') + g.writeln('${ast.int_type_name} ${tmp}_len = ${tmp}_orig.len;') } else if left_sym.kind == .array_fixed { left_info := left_sym.info as ast.ArrayFixed left_styp := g.styp(left_type) @@ -1792,7 +1775,7 @@ fn (mut g Gen) write_prepared_tmp_value(tmp string, node &ast.CallExpr, tmp_styp } } g.writeln(', sizeof(${left_styp}));') - g.writeln('int ${tmp}_len = ${left_info.size};') + g.writeln('${ast.int_type_name} ${tmp}_len = ${left_info.size};') } return has_infix_left_var_name } diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 79d6e95a65..e9cc3d8cd6 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -1386,7 +1386,7 @@ fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) { if resolved_sym.is_builtin() && !fn_name.starts_with('builtin__') { fn_name = 'builtin__${fn_name}' } - } else if rec_typ_name in ['int_literal', 'float_literal'] { + } else if rec_typ_name in ['int_literal', 'float_literal', 'vint_t'] { fn_name = 'builtin__${fn_name}' } g.write('${fn_name}(&') diff --git a/vlib/v/gen/c/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v index e4f5afa9e3..bcd87cac85 100644 --- a/vlib/v/gen/c/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -384,7 +384,7 @@ fn (mut g Gen) gen_array_equality_fn(left_type ast.Type) string { fn_builder.writeln('\tif (${left_len} != ${right_len}) {') fn_builder.writeln('\t\treturn false;') fn_builder.writeln('\t}') - fn_builder.writeln('\tfor (int i = 0; i < ${left_len}; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${left_len}; ++i) {') // compare every pair of elements of the two arrays if elem.sym.kind == .string { fn_builder.writeln('\t\tif (!builtin__string__eq(*((${ptr_elem_styp}*)((byte*)${left_data}+(i*${left_elem}))), *((${ptr_elem_styp}*)((byte*)${right_data}+(i*${right_elem}))))) {') @@ -473,7 +473,7 @@ fn (mut g Gen) gen_fixed_array_equality_fn(left_type ast.Type) string { fn_builder.writeln('\t\treturn true;') fn_builder.writeln('\t}') } - fn_builder.writeln('\tfor (int i = 0; i < ${size}; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${size}; ++i) {') // compare every pair of elements of the two fixed arrays if elem.sym.kind == .string { fn_builder.writeln('\t\tif (!builtin__string__eq(((string*)${left})[i], ((string*)${right})[i])) {') @@ -538,7 +538,7 @@ fn (mut g Gen) gen_map_equality_fn(left_type ast.Type) string { fn_builder.writeln('\tif (${left_len} != ${right_len}) {') fn_builder.writeln('\t\treturn false;') fn_builder.writeln('\t}') - fn_builder.writeln('\tfor (int i = 0; i < ${key_values}.len; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${key_values}.len; ++i) {') fn_builder.writeln('\t\tif (!builtin__DenseArray_has_index(&${key_values}, i)) continue;') fn_builder.writeln('\t\tvoidptr k = builtin__DenseArray_key(&${key_values}, i);') fn_builder.writeln('\t\tif (!builtin__map_exists(${b}, k)) return false;') @@ -628,7 +628,7 @@ fn (mut g Gen) gen_interface_equality_fn(left_type ast.Type) string { fn_builder.writeln('${g.static_non_parallel}inline bool ${fn_name}_interface_eq(${ptr_styp} a, ${ptr_styp} b) {') fn_builder.writeln('\tif (${left_arg} == ${right_arg}) {') - fn_builder.writeln('\t\tint idx = v_typeof_interface_idx_${idx_fn}(${left_arg});') + fn_builder.writeln('\t\tu32 idx = v_typeof_interface_idx_${idx_fn}(${left_arg});') if info is ast.Interface { for typ in info.types { sym := g.table.sym(typ.set_nr_muls(0)) diff --git a/vlib/v/gen/c/auto_free_methods.v b/vlib/v/gen/c/auto_free_methods.v index 0ca765a951..5497b29efc 100644 --- a/vlib/v/gen/c/auto_free_methods.v +++ b/vlib/v/gen/c/auto_free_methods.v @@ -188,7 +188,7 @@ fn (mut g Gen) gen_free_for_array(info ast.Array, styp string, fn_name string) { sym := g.table.sym(g.unwrap_generic(info.elem_type)) if sym.kind in [.string, .array, .map, .struct] { - fn_builder.writeln('\tfor (int i = 0; i < it->len; i++) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < it->len; i++) {') mut elem_styp := g.styp(info.elem_type).replace('*', '') mut elem_styp_fn_name := if sym.has_method('free') { diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 2ff47d1b4e..df14b92cc4 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -16,23 +16,9 @@ fn (mut g Gen) gen_str_default(sym ast.TypeSymbol, styp string, str_fn_name stri } mut convertor := '' mut typename_ := '' - mut got_int_str := false if sym.parent_idx in ast.integer_type_idxs { convertor = 'int' typename_ = 'int' - $if new_int ? { - if str_fn_name == 'i64_str' { - if got_int_str { - return - } else { - got_int_str = true - } - } - - // if sym.parent_idx == ast.int_type_idx { - // return - //} - } } else if sym.parent_idx == ast.f32_type_idx { convertor = 'float' typename_ = 'f32' @@ -202,8 +188,8 @@ fn (mut g Gen) gen_str_for_option(typ ast.Type, styp string, str_fn_name string) g.definitions.writeln('string ${str_fn_name}(${styp} it);') g.auto_str_funcs.writeln('string ${str_fn_name}(${styp} it) { return indent_${str_fn_name}(it, 0); }') - g.definitions.writeln('string indent_${str_fn_name}(${styp} it, int indent_count);') - g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, int indent_count) {') + g.definitions.writeln('string indent_${str_fn_name}(${styp} it, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, ${ast.int_type_name} indent_count) {') g.auto_str_funcs.writeln('\tstring res;') g.auto_str_funcs.writeln('\tif (it.state == 0) {') deref := if typ.is_ptr() && !typ.has_flag(.option_mut_param_t) { @@ -255,8 +241,8 @@ fn (mut g Gen) gen_str_for_result(typ ast.Type, styp string, str_fn_name string) g.definitions.writeln('string ${str_fn_name}(${styp} it);') g.auto_str_funcs.writeln('string ${str_fn_name}(${styp} it) { return indent_${str_fn_name}(it, 0); }') - g.definitions.writeln('string indent_${str_fn_name}(${styp} it, int indent_count);') - g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, int indent_count) {') + g.definitions.writeln('string indent_${str_fn_name}(${styp} it, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('string indent_${str_fn_name}(${styp} it, ${ast.int_type_name} indent_count) {') g.auto_str_funcs.writeln('\tstring res;') g.auto_str_funcs.writeln('\tif (!it.is_error) {') if sym.kind == .string { @@ -292,8 +278,8 @@ fn (mut g Gen) gen_str_for_alias(info ast.Alias, styp string, str_fn_name string g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${arg_def});') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${arg_def}) { return indent_${str_fn_name}(it, 0); }') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, int indent_count);') - g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, int indent_count) {') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, ${ast.int_type_name} indent_count) {') old := g.reset_tmp_count() defer { g.tmp_count = old } g.auto_str_funcs.writeln('\tstring indents = builtin__string_repeat(_S(" "), indent_count);') @@ -373,7 +359,7 @@ fn (mut g Gen) gen_str_for_enum(info ast.Enum, styp string, str_fn_name string) if info.is_flag { clean_name := util.strip_main_name(styp.replace('__', '.')) g.auto_str_funcs.writeln('\tstring ret = _S("${clean_name}{");') - g.auto_str_funcs.writeln('\tint first = 1;') + g.auto_str_funcs.writeln('\t${ast.int_type_name} first = 1;') g.auto_str_funcs.writeln('\tu64 zit = (u64)it;') for i, val in info.vals { mask := u64(1) << i @@ -413,10 +399,10 @@ fn (mut g Gen) gen_str_for_interface(info ast.Interface, styp string, typ_str st // _str() functions should have a single argument, the indenting ones take 2: g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} x);') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} x) { return indent_${str_fn_name}(x, 0); }') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, int indent_count);') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, ${ast.int_type_name} indent_count);') mut fn_builder := strings.new_builder(512) clean_interface_v_type_name := util.strip_main_name(typ_str) - fn_builder.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, int indent_count) { /* gen_str_for_interface */') + fn_builder.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, ${ast.int_type_name} indent_count) { /* gen_str_for_interface */') for typ in info.types { sub_sym := g.table.sym(ast.mktyp(typ)) if g.pref.skip_unused && sub_sym.idx !in g.table.used_features.used_syms { @@ -473,9 +459,9 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, typ_str // _str() functions should have a single argument, the indenting ones take 2: g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} x);') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} x) { return indent_${str_fn_name}(x, 0); }') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, int indent_count);') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, ${ast.int_type_name} indent_count);') mut fn_builder := strings.new_builder(512) - fn_builder.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, int indent_count) {') + fn_builder.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} x, ${ast.int_type_name} indent_count) {') mut clean_sum_type_v_type_name := '' if info.is_anon { variant_names := info.variants.map(util.strip_main_name(g.table.sym(it).name)) @@ -632,11 +618,11 @@ fn (mut g Gen) gen_str_for_array(info ast.Array, styp string, str_fn_name string g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} a);') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} a) { return indent_${str_fn_name}(a, 0);}') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} a, int indent_count);') - g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} a, int indent_count) {') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} a, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} a, ${ast.int_type_name} indent_count) {') g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(2 + a.len * 10);') g.auto_str_funcs.writeln('\tstrings__Builder_write_string(&sb, _S("["));') - g.auto_str_funcs.writeln('\tfor (int i = 0; i < a.len; ++i) {') + g.auto_str_funcs.writeln('\tfor (${ast.int_type_name} i = 0; i < a.len; ++i) {') if sym.kind == .function { g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}();') } else { @@ -739,11 +725,11 @@ fn (mut g Gen) gen_str_for_array_fixed(info ast.ArrayFixed, styp string, str_fn_ g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${def_arg});') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${def_arg}) { return indent_${str_fn_name}(a, 0);}') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${def_arg}, int indent_count);') - g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${def_arg}, int indent_count) {') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${def_arg}, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${def_arg}, ${ast.int_type_name} indent_count) {') g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(2 + ${info.size} * 10);') g.auto_str_funcs.writeln('\tstrings__Builder_write_string(&sb, _S("["));') - g.auto_str_funcs.writeln('\tfor (int i = 0; i < ${info.size}; ++i) {') + g.auto_str_funcs.writeln('\tfor (${ast.int_type_name} i = 0; i < ${info.size}; ++i) {') if sym.kind == .function { g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}();') g.auto_str_funcs.writeln('\t\tstrings__Builder_write_string(&sb, x);') @@ -826,12 +812,12 @@ fn (mut g Gen) gen_str_for_map(info ast.Map, styp string, str_fn_name string) { g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} m);') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${styp} m) { return indent_${str_fn_name}(m, 0);}') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} m, int indent_count);') - g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} m, int indent_count) { /* gen_str_for_map */') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} m, ${ast.int_type_name} indent_count);') + g.auto_str_funcs.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${styp} m, ${ast.int_type_name} indent_count) { /* gen_str_for_map */') g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(2 + m.key_values.len * 10);') g.auto_str_funcs.writeln('\tstrings__Builder_write_string(&sb, _S("{"));') g.auto_str_funcs.writeln('\tbool is_first = true;') - g.auto_str_funcs.writeln('\tfor (int i = 0; i < m.key_values.len; ++i) {') + g.auto_str_funcs.writeln('\tfor (${ast.int_type_name} i = 0; i < m.key_values.len; ++i) {') g.auto_str_funcs.writeln('\t\tif (!builtin__DenseArray_has_index(&m.key_values, i)) { continue; }') g.auto_str_funcs.writeln('\t\telse if (!is_first) { strings__Builder_write_string(&sb, _S(", ")); }') @@ -937,7 +923,11 @@ fn (g &Gen) type_to_fmt(typ ast.Type) StrIntpType { } return .si_g64 } else if sym.kind == .int { - return .si_i32 + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + return .si_i64 + } $else { + return .si_i32 + } } else if sym.kind == .u32 { return .si_u32 } else if sym.kind == .u64 { @@ -961,12 +951,12 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, lang ast.Language, styp strin arg_def := if is_c_struct { '${styp}* it' } else { '${styp} it' } g.definitions.writeln('${g.static_non_parallel}string ${str_fn_name}(${arg_def});') g.auto_str_funcs.writeln('${g.static_non_parallel}string ${str_fn_name}(${arg_def}) { return indent_${str_fn_name}(it, 0);}') - g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, int indent_count);') + g.definitions.writeln('${g.static_non_parallel}string indent_${str_fn_name}(${arg_def}, ${ast.int_type_name} indent_count);') mut fn_builder := strings.new_builder(512) defer { g.auto_fn_definitions << fn_builder.str() } - fn_builder.writeln('string indent_${str_fn_name}(${arg_def}, int indent_count) {') + fn_builder.writeln('string indent_${str_fn_name}(${arg_def}, ${ast.int_type_name} indent_count) {') old := g.reset_tmp_count() defer { g.tmp_count = old } clean_struct_v_type_name := if info.is_anon { 'struct ' } else { util.strip_main_name(typ_str) } @@ -1321,7 +1311,7 @@ fn (mut g Gen) gen_enum_static_from_string(fn_name string, mod_enum_name string, for field_val in enum_field_vals { fn_builder.writeln('\tbuiltin__array_push((array*)&field_vals, _MOV((i64[]){ ${field_val} }));') } - fn_builder.writeln('\tfor (int i = 0; i < ${enum_field_names.len}; ++i) {') + fn_builder.writeln('\tfor (${ast.int_type_name} i = 0; i < ${enum_field_names.len}; ++i) {') fn_builder.writeln('\t\tif (builtin__fast_string_eq(name, (*(string*)builtin__array_get(field_names, i)))) {') fn_builder.writeln('\t\t\texists = true;') fn_builder.writeln('\t\t\tinx = i;') diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index db2fa815b0..d1ed45d7d5 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -197,7 +197,7 @@ mut: sumtype_casting_fns []SumtypeCastingFn anon_fn_definitions []string // anon generated functions definition list anon_fns shared []string // remove duplicate anon generated functions - sumtype_definitions map[int]bool // `_TypeA_to_sumtype_TypeB()` fns that have been generated + sumtype_definitions map[u32]bool // `_TypeA_to_sumtype_TypeB()` fns that have been generated trace_fn_definitions []string json_types []ast.Type // to avoid json gen duplicates pcs []ProfileCounterMeta // -prof profile counter fn_names => fn counter name @@ -532,7 +532,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO // Temporary hack to make toml work with -usecache TODO remove continue } - g.definitions.writeln('int _v_type_idx_${sym.cname}(); // 1build module ${g.pref.path}') + g.definitions.writeln('u32 _v_type_idx_${sym.cname}(); // 1build module ${g.pref.path}') } } else if g.pref.use_cache { is_toml := g.pref.path.contains('/toml') @@ -543,7 +543,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO if is_toml && sym.cname.contains('map[string]') { continue } - g.definitions.writeln('int _v_type_idx_${sym.cname}() { return ${idx}; }; //lol ${g.pref.path}') + g.definitions.writeln('u32 _v_type_idx_${sym.cname}() { return ${idx}; }; //lol ${g.pref.path}') } } @@ -1138,8 +1138,8 @@ pub fn (mut g Gen) write_typeof_functions() { if sum_info.is_generic { continue } - g.writeln('${static_prefix}char * v_typeof_sumtype_${sym.cname}(int sidx) {') - g.definitions.writeln('${static_prefix}char * v_typeof_sumtype_${sym.cname}(int);') + g.writeln('${static_prefix}char * v_typeof_sumtype_${sym.cname}(u32 sidx) {') + g.definitions.writeln('${static_prefix}char * v_typeof_sumtype_${sym.cname}(u32);') if g.pref.build_mode == .build_module { g.writeln('\t\tif( sidx == _v_type_idx_${sym.cname}() ) return "${util.strip_main_name(sym.name)}";') for v in sum_info.variants { @@ -1152,39 +1152,39 @@ pub fn (mut g Gen) write_typeof_functions() { tidx := g.table.find_type_idx(sym.name) g.writeln('\tswitch(sidx) {') g.writeln('\t\tcase ${tidx}: return "${util.strip_main_name(sym.name)}";') - mut idxs := []int{} + mut idxs := []ast.Type{} for v in sum_info.variants { if v in idxs { continue } subtype := g.table.sym(v) - g.writeln('\t\tcase ${int(v)}: return "${util.strip_main_name(subtype.name)}";') + g.writeln('\t\tcase ${u32(v)}: return "${util.strip_main_name(subtype.name)}";') idxs << v } g.writeln('\t\tdefault: return "unknown ${util.strip_main_name(sym.name)}";') g.writeln('\t}') } g.writeln2('}', '') - g.writeln('${static_prefix}int v_typeof_sumtype_idx_${sym.cname}(int sidx) {') + g.writeln('${static_prefix}u32 v_typeof_sumtype_idx_${sym.cname}(u32 sidx) {') if g.pref.build_mode == .build_module { - g.writeln('\t\tif( sidx == _v_type_idx_${sym.cname}() ) return ${int(ityp)};') + g.writeln('\t\tif( sidx == _v_type_idx_${sym.cname}() ) return ${u32(ityp)};') for v in sum_info.variants { subtype := g.table.sym(v) - g.writeln('\tif( sidx == _v_type_idx_${subtype.cname}() ) return ${int(v)};') + g.writeln('\tif( sidx == _v_type_idx_${subtype.cname}() ) return ${u32(v)};') } - g.writeln('\treturn ${int(ityp)};') + g.writeln('\treturn ${u32(ityp)};') } else { tidx := g.table.find_type_idx(sym.name) - g.writeln2('\tswitch(sidx) {', '\t\tcase ${tidx}: return ${int(ityp)};') - mut idxs := []int{} + g.writeln2('\tswitch(sidx) {', '\t\tcase ${tidx}: return ${u32(ityp)};') + mut idxs := []ast.Type{} for v in sum_info.variants { if v in idxs { continue } - g.writeln('\t\tcase ${int(v)}: return ${int(v)};') + g.writeln('\t\tcase ${u32(v)}: return ${u32(v)};') idxs << v } - g.writeln2('\t\tdefault: return ${int(ityp)};', '\t}') + g.writeln2('\t\tdefault: return ${u32(ityp)};', '\t}') } g.writeln('}') } else if sym.kind == .interface { @@ -1202,11 +1202,11 @@ pub fn (mut g Gen) write_typeof_functions() { continue } already_generated_ifaces[sym.cname] = true - g.definitions.writeln('${g.static_non_parallel}char * v_typeof_interface_${sym.cname}(int sidx);') + g.definitions.writeln('${g.static_non_parallel}char * v_typeof_interface_${sym.cname}(u32 sidx);') if g.pref.parallel_cc { - g.extern_out.writeln('extern char * v_typeof_interface_${sym.cname}(int sidx);') + g.extern_out.writeln('extern char * v_typeof_interface_${sym.cname}(u32 sidx);') } - g.writeln('${g.static_non_parallel}char * v_typeof_interface_${sym.cname}(int sidx) {') + g.writeln('${g.static_non_parallel}char * v_typeof_interface_${sym.cname}(u32 sidx) {') for t in inter_info.types { sub_sym := g.table.sym(ast.mktyp(t)) if sub_sym.info is ast.Struct && sub_sym.info.is_unresolved_generic() { @@ -1219,10 +1219,10 @@ pub fn (mut g Gen) write_typeof_functions() { g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return "${util.strip_main_name(sub_sym.name)}";') } g.writeln2('\treturn "unknown ${util.strip_main_name(sym.name)}";', '}') - g.definitions.writeln('int v_typeof_interface_idx_${sym.cname}(int sidx);') - g.writeln2('', 'int v_typeof_interface_idx_${sym.cname}(int sidx) {') + g.definitions.writeln('u32 v_typeof_interface_idx_${sym.cname}(u32 sidx);') + g.writeln2('', 'u32 v_typeof_interface_idx_${sym.cname}(u32 sidx) {') if g.pref.parallel_cc { - g.extern_out.writeln('extern int v_typeof_interface_idx_${sym.cname}(int sidx);') + g.extern_out.writeln('extern u32 v_typeof_interface_idx_${sym.cname}(u32 sidx);') } for t in inter_info.types { sub_sym := g.table.sym(ast.mktyp(t)) @@ -1233,9 +1233,9 @@ pub fn (mut g Gen) write_typeof_functions() { && sub_sym.idx !in g.table.used_features.used_syms { continue } - g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return ${int(t.set_nr_muls(0))};') + g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return ${u32(t.set_nr_muls(0))};') } - g.writeln2('\treturn ${int(ityp)};', '}') + g.writeln2('\treturn ${u32(ityp)};', '}') } } g.writeln2('// << typeof() support for sum types', '') @@ -1267,14 +1267,6 @@ fn (mut g Gen) base_type(_t ast.Type) string { return 'u64' } } - /* - // On 64 bit systems int is an i64 - $if amd64 || arm64 { - if g.pref.use_64_int && t == ast.int_type { - return 'i64' - } - } - */ share := t.share() mut styp := if share == .atomic_t { t.atomic_typename() } else { g.cc_type(t, true) } if t.has_flag(.shared_f) { @@ -1504,7 +1496,7 @@ fn (mut g Gen) write_shareds() { g.shared_types.writeln('\t${mtx_typ} mtx;') g.shared_types.writeln('\t${base} val;') g.shared_types.writeln('};') - g.shared_functions.writeln('static inline voidptr __dup${sh_typ}(voidptr src, int sz) {') + g.shared_functions.writeln('static inline voidptr __dup${sh_typ}(voidptr src, ${ast.int_type_name} sz) {') g.shared_functions.writeln('\t${sh_typ}* dest = builtin__memdup(src, sz);') g.shared_functions.writeln('\tsync__RwMutex_init(&dest->mtx);') g.shared_functions.writeln('\treturn dest;') @@ -1553,7 +1545,7 @@ fn (mut g Gen) register_thread_array_wait_call(eltyp string) string { g.waiter_fn_definitions.writeln('void ${fn_name}(${thread_arr_typ} a);') g.gowrappers.writeln(' void ${fn_name}(${thread_arr_typ} a) { - for (int i = 0; i < a.len; ++i) { + for (${ast.int_type_name} i = 0; i < a.len; ++i) { ${thread_typ} t = ((${thread_typ}*)a.data)[i]; if (t == 0) continue; __v_thread_wait(t); @@ -1564,7 +1556,7 @@ void ${fn_name}(${thread_arr_typ} a) { g.gowrappers.writeln(' ${ret_typ} ${fn_name}(${thread_arr_typ} a) { ${ret_typ} res = builtin____new_array_with_default(a.len, a.len, sizeof(${eltyp}), 0); - for (int i = 0; i < a.len; ++i) { + for (${ast.int_type_name} i = 0; i < a.len; ++i) { ${thread_typ} t = ((${thread_typ}*)a.data)[i];') if g.pref.os == .windows { g.gowrappers.writeln('\t\tif (t.handle == 0) continue;') @@ -1601,7 +1593,7 @@ fn (mut g Gen) register_thread_fixed_array_wait_call(node ast.CallExpr, eltyp st g.waiter_fn_definitions.writeln('void ${fn_name}(${thread_arr_typ} a);') g.gowrappers.writeln(' void ${fn_name}(${thread_arr_typ} a) { - for (int i = 0; i < ${len}; ++i) { + for (${ast.int_type_name} i = 0; i < ${len}; ++i) { ${thread_typ} t = ((${thread_typ}*)a)[i]; if (t == 0) continue; __v_thread_wait(t); @@ -1612,7 +1604,7 @@ void ${fn_name}(${thread_arr_typ} a) { g.gowrappers.writeln(' ${ret_typ} ${fn_name}(${thread_arr_typ} a) { ${ret_typ} res = builtin____new_array_with_default(${len}, ${len}, sizeof(${eltyp}), 0); - for (int i = 0; i < ${len}; ++i) { + for (${ast.int_type_name} i = 0; i < ${len}; ++i) { ${thread_typ} t = ((${thread_typ}*)a)[i];') if g.pref.os == .windows { g.gowrappers.writeln('\t\tif (t.handle == 0) continue;') @@ -1722,7 +1714,7 @@ fn (g &Gen) type_sidx(t ast.Type) string { sym := g.table.sym(t) return '_v_type_idx_${sym.cname}()' } - return int(t).str() + return u32(t).str() } pub fn (mut g Gen) write_typedef_types() { @@ -1926,7 +1918,7 @@ pub fn (mut g Gen) write_interface_typesymbol_declaration(sym ast.TypeSymbol) { g.type_definitions.writeln('\t\t${vcname}* _${vcname};') } g.type_definitions.writeln('\t};') - g.type_definitions.writeln('\tint _typ;') + g.type_definitions.writeln('\tu32 _typ;') for field in info.fields { styp := g.styp(field.typ) cname := c_name(field.name) @@ -2152,7 +2144,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { mut styp := g.base_type(stmt.typ) $if tinyc && x32 && windows { if stmt.typ == ast.int_literal_type { - styp = 'int' + styp = ast.int_type_name } else if stmt.typ == ast.float_literal_type { styp = 'f64' } @@ -2210,7 +2202,7 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) bool { mut styp := g.base_type(stmt.typ) $if tinyc && x32 && windows { if stmt.typ == ast.int_literal_type { - styp = 'int' + styp = ast.int_type_name } else if stmt.typ == ast.float_literal_type { styp = 'f64' } @@ -2767,7 +2759,7 @@ struct SumtypeCastingFn { fn (mut g Gen) get_sumtype_casting_fn(got_ ast.Type, exp_ ast.Type) string { mut got, exp := got_.idx_type(), exp_.idx_type() - i := int(got) | int(u32(exp) << 18) | int(u32(exp_.has_flag(.option)) << 17) | int(u32(got_.has_flag(.option)) << 16) + i := u32(got) | u32(u32(exp) << 18) | u32(u32(exp_.has_flag(.option)) << 17) | u32(u32(got_.has_flag(.option)) << 16) exp_sym := g.table.sym(exp) mut got_sym := g.table.sym(got) cname := if exp == ast.int_type_idx { @@ -2826,7 +2818,7 @@ fn (mut g Gen) write_sumtype_casting_fn(fun SumtypeCastingFn) { if g.table.fn_type_source_signature(variant_sym.info.func) == g.table.fn_type_source_signature(got_sym.info.func) { variant_name = g.get_sumtype_variant_name(variant, variant_sym) got_cname = g.get_sumtype_variant_type_name(variant, variant_sym) - type_idx = int(variant).str() + type_idx = u32(variant).str() break } } @@ -3571,11 +3563,31 @@ fn (mut g Gen) map_fn_ptrs(key_sym ast.TypeSymbol) (string, string, string, stri if g.pref.ccompiler_type == .tinyc && einfo.typ in [ast.u8_type, ast.u16_type, ast.i8_type, ast.i16_type] { // workaround for tcc, since we can not generate a packed Enum with size < 4 bytes - return g.map_fn_ptrs(g.table.sym(ast.int_type)) + return g.map_fn_ptrs(g.table.sym(ast.i32_type)) + } + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + // enum type alway use 32bit `int` + if einfo.typ == ast.int_type { + return g.map_fn_ptrs(g.table.sym(ast.i32_type)) + } else { + return g.map_fn_ptrs(g.table.sym(einfo.typ)) + } + } $else { + return g.map_fn_ptrs(g.table.sym(einfo.typ)) } - return g.map_fn_ptrs(g.table.sym(einfo.typ)) } - .int, .i32, .u32, .rune, .f32 { + .int { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + hash_fn = '&builtin__map_hash_int_8' + key_eq_fn = '&builtin__map_eq_int_8' + clone_fn = '&builtin__map_clone_int_8' + } $else { + hash_fn = '&builtin__map_hash_int_4' + key_eq_fn = '&builtin__map_eq_int_4' + clone_fn = '&builtin__map_clone_int_4' + } + } + .i32, .u32, .rune, .f32 { hash_fn = '&builtin__map_hash_int_4' key_eq_fn = '&builtin__map_eq_int_4' clone_fn = '&builtin__map_clone_int_4' @@ -4149,11 +4161,11 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { return } .typ { - g.write(int(g.unwrap_generic(node.name_type)).str()) + g.write(u32(g.unwrap_generic(node.name_type)).str()) return } .unaliased_typ { - g.write(int(g.table.unaliased_type(g.unwrap_generic(node.name_type))).str()) + g.write(u32(g.table.unaliased_type(g.unwrap_generic(node.name_type))).str()) return } .indirections { @@ -4592,7 +4604,7 @@ fn (mut g Gen) gen_closure_fn(expr_styp string, m ast.Fn, name string) { if resolved_sym.is_builtin() { full_method_name = 'builtin__${full_method_name}' } - } else if expr_styp in ['int_literal', 'float_literal'] { + } else if expr_styp in ['int_literal', 'float_literal', 'vint_t'] { full_method_name = 'builtin__${full_method_name}' } } @@ -4963,7 +4975,8 @@ fn (mut g Gen) lock_expr(node ast.LockExpr) { } else { g.writeln('__sort_ptr(_arr_${mtxs}, _isrlck_${mtxs}, ${node.lockeds.len});') } - g.writeln2('for (int ${mtxs}=0; ${mtxs}<${node.lockeds.len}; ${mtxs}++) {', '\tif (${mtxs} && _arr_${mtxs}[${mtxs}] == _arr_${mtxs}[${mtxs}-1]) continue;') + g.writeln2('for (${ast.int_type_name} ${mtxs}=0; ${mtxs}<${node.lockeds.len}; ${mtxs}++) {', + '\tif (${mtxs} && _arr_${mtxs}[${mtxs}] == _arr_${mtxs}[${mtxs}-1]) continue;') g.writeln2('\tif (_isrlck_${mtxs}[${mtxs}])', '\t\tsync__RwMutex_rlock((sync__RwMutex*)_arr_${mtxs}[${mtxs}]);') g.writeln2('\telse', '\t\tsync__RwMutex_lock((sync__RwMutex*)_arr_${mtxs}[${mtxs}]);') g.writeln('}') @@ -4993,7 +5006,7 @@ fn (mut g Gen) unlock_locks() { g.expr(g.cur_lock.lockeds[0]) g.write('->mtx);') } else { - g.writeln('for (int ${g.mtxs}=${g.cur_lock.lockeds.len - 1}; ${g.mtxs}>=0; ${g.mtxs}--) {') + g.writeln('for (${ast.int_type_name} ${g.mtxs}=${g.cur_lock.lockeds.len - 1}; ${g.mtxs}>=0; ${g.mtxs}--) {') g.writeln('\tif (${g.mtxs} && _arr_${g.mtxs}[${g.mtxs}] == _arr_${g.mtxs}[${g.mtxs}-1]) continue;') g.writeln('\tif (_isrlck_${g.mtxs}[${g.mtxs}])') g.writeln('\t\tsync__RwMutex_runlock((sync__RwMutex*)_arr_${g.mtxs}[${g.mtxs}]);') @@ -5673,7 +5686,7 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { expr_styp := g.styp(expr_typ) g.write('{._${g.table.sym(expr_typ).cname}=builtin__memdup(ADDR(${expr_styp}, ') g.expr(node.expr) - g.write('), sizeof(${expr_styp})),._typ=${int(expr_typ)}})') + g.write('), sizeof(${expr_styp})),._typ=${u32(expr_typ)}})') } else { g.write('(') if node.expr is ast.Ident { @@ -6925,7 +6938,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { } struct_names[name] = true g.typedefs.writeln('typedef struct ${name} ${name};') - mut idxs := []int{} + mut idxs := []ast.Type{} if !g.pref.is_prod { // Do not print union sum type coment in prod mode g.type_definitions.writeln('') @@ -6962,7 +6975,7 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) { g.type_definitions.writeln('\t\t${var_type} _${variant_name};') } g.type_definitions.writeln('\t};') - g.type_definitions.writeln('\tint _typ;') + g.type_definitions.writeln('\tu32 _typ;') if sym.info.fields.len > 0 { g.writeln('\t// pointers to common sumtype fields') for field in sym.info.fields { @@ -7465,9 +7478,9 @@ fn (mut g Gen) type_default_sumtype(typ_ ast.Type, sym ast.TypeSymbol) string { g.type_default_no_sumtype(first_typ) } if default_str[0] == `{` { - return '(${g.styp(typ_)}){._${first_field}=HEAP(${first_styp}, ((${first_styp})${default_str})),._typ=${int(first_typ)}}' + return '(${g.styp(typ_)}){._${first_field}=HEAP(${first_styp}, ((${first_styp})${default_str})),._typ=${u32(first_typ)}}' } else { - return '(${g.styp(typ_)}){._${first_field}=HEAP(${first_styp}, (${default_str})),._typ=${int(first_typ)}}' + return '(${g.styp(typ_)}){._${first_field}=HEAP(${first_styp}, (${default_str})),._typ=${u32(first_typ)}}' } } @@ -8304,9 +8317,9 @@ return ${cast_shared_struct_str}; } iin_idx := already_generated_mwrappers[interface_index_name] - iinidx_minimum_base if g.pref.build_mode != .build_module { - sb.writeln('${g.static_modifier}const int ${interface_index_name} = ${iin_idx};') + sb.writeln('${g.static_modifier}const u32 ${interface_index_name} = ${iin_idx};') } else { - sb.writeln('extern const int ${interface_index_name};') + sb.writeln('extern const u32 ${interface_index_name};') } } for vtyp, variants in inter_info.conversions { @@ -8486,7 +8499,7 @@ pub fn (mut g Gen) contains_ptr(el_typ ast.Type) bool { return true } match sym.kind { - .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .rune, .bool, .enum { + .i8, .i16, .i32, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .rune, .bool, .enum { g.contains_ptr_cache[typ] = false return false } @@ -8542,6 +8555,16 @@ fn (mut g Gen) check_noscan(elem_typ ast.Type) string { return '' } +// vint2int rename `_vint_t` to `int` +fn vint2int(name string) string { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + if name == ast.int_type_name { + return 'int' + } + } + return name +} + const hi_32_mask = u64(0xFFFFFFFF00000000) const lo_32_mask = u64(0x00000000FFFFFFFF) const sign_bit_32 = u32(0x80000000) diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index 2ca85e0781..fe7a2940b6 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -1,5 +1,7 @@ module c +import v.ast + // Note: @@@ here serve as placeholders. // They will be replaced with correct strings // for each constant, during C code generation. @@ -17,7 +19,7 @@ struct __shared_map { sync__RwMutex mtx; map val; }; -static inline voidptr __dup_shared_map(voidptr src, int sz) { +static inline voidptr __dup_shared_map(voidptr src, ${ast.int_type_name} sz) { __shared_map* dest = builtin__memdup(src, sz); sync__RwMutex_init(&dest->mtx); return dest; @@ -27,16 +29,16 @@ struct __shared_array { sync__RwMutex mtx; array val; }; -static inline voidptr __dup_shared_array(voidptr src, int sz) { +static inline voidptr __dup_shared_array(voidptr src, ${ast.int_type_name} sz) { __shared_array* dest = builtin__memdup(src, sz); sync__RwMutex_init(&dest->mtx); return dest; } -static inline void __sort_ptr(uintptr_t a[], bool b[], int l) { - for (int i=1; i0 && a[j-1] > ins) { a[j] = a[j-1]; b[j] = b[j-1]; diff --git a/vlib/v/gen/c/consts_and_globals.v b/vlib/v/gen/c/consts_and_globals.v index ae66427023..c030d323fe 100644 --- a/vlib/v/gen/c/consts_and_globals.v +++ b/vlib/v/gen/c/consts_and_globals.v @@ -292,7 +292,7 @@ fn (mut g Gen) const_decl_simple_define(mod string, name string, val string) { if g.pref.translated { g.global_const_defs[util.no_dots(name)] = GlobalConstDef{ mod: mod - def: 'const int ${x} = ${val};' + def: 'const ${ast.int_type_name} ${x} = ${val};' order: -1 } } else { diff --git a/vlib/v/gen/c/coverage.v b/vlib/v/gen/c/coverage.v index 5c9a7a1001..f4a9d51a68 100644 --- a/vlib/v/gen/c/coverage.v +++ b/vlib/v/gen/c/coverage.v @@ -86,10 +86,10 @@ fn (mut g Gen) write_coverage_stats() { fmeta.writeln('}') or { continue } fmeta.close() } - g.cov_declarations.writeln('\tlong int secs = 0;') - g.cov_declarations.writeln('\tlong int nsecs = 0;') + g.cov_declarations.writeln('\tint secs = 0;') + g.cov_declarations.writeln('\tint nsecs = 0;') g.cov_declarations.writeln('\t#if defined(_WIN32)') - g.cov_declarations.writeln('\tlong int ticks_passed = GetTickCount();') + g.cov_declarations.writeln('\tint ticks_passed = GetTickCount();') g.cov_declarations.writeln('\nsecs = ticks_passed / 1000;') g.cov_declarations.writeln('\nnsecs = (ticks_passed % 1000) * 1000000;') g.cov_declarations.writeln('\t#endif') @@ -109,7 +109,7 @@ fn (mut g Gen) write_coverage_stats() { for k, cov in g.coverage_files { nr_points := cov.points.len g.cov_declarations.writeln('\t{') - g.cov_declarations.writeln('\t\tfor (int i = 0; i < ${nr_points}; ++i) {') + g.cov_declarations.writeln('\t\tfor (${ast.int_type_name} i = 0; i < ${nr_points}; ++i) {') g.cov_declarations.writeln('\t\t\tif (_v_cov[_v_cov_file_offset_${k}+i]) {') g.cov_declarations.writeln("\t\t\t\tfprintf(fp, \"%s,%d,%ld\\n\", \"${cov.fhash}\", i, _v_cov[_v_cov_file_offset_${k}+i]);") g.cov_declarations.writeln('\t\t\t}') diff --git a/vlib/v/gen/c/dumpexpr.v b/vlib/v/gen/c/dumpexpr.v index 9df0a12acb..6b05ca51e0 100644 --- a/vlib/v/gen/c/dumpexpr.v +++ b/vlib/v/gen/c/dumpexpr.v @@ -177,14 +177,15 @@ fn (mut g Gen) dump_expr_definitions() { } dump_already_generated_fns[dump_fn_name] = true - dump_fn_defs.writeln('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg);') - if g.writeln_fn_header('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg)', mut + dump_fn_defs.writeln('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, ${ast.int_type_name} line, string sexpr, ${str_dumparg_type} dump_arg);') + if g.writeln_fn_header('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, ${ast.int_type_name} line, string sexpr, ${str_dumparg_type} dump_arg)', mut dump_fns) { continue } mut surrounder := util.new_surrounder(3) - surrounder.add('\tstring sline = builtin__int_str(line);', '\tbuiltin__string_free(&sline);') + int_str := g.get_str_fn(ast.int_type) + surrounder.add('\tstring sline = ${int_str}(line);', '\tbuiltin__string_free(&sline);') if dump_sym.kind == .function && !is_option { surrounder.add('\tstring value = ${to_string_fn_name}();', '\tbuiltin__string_free(&value);') } else if dump_sym.kind == .none { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 77ddecb611..02dc049ed6 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1686,7 +1686,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) { if resolved_sym.is_builtin() && !receiver_type_name.starts_with('_') { name = 'builtin__${name}' } - } else if receiver_type_name in ['int_literal', 'float_literal'] { + } else if receiver_type_name in ['int_literal', 'float_literal', 'vint_t'] { name = 'builtin__${name}' } if left_sym.kind == .chan && node.name in ['close', 'try_pop', 'try_push'] { diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index 28812e7eb0..26cae27efc 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -246,7 +246,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { } else { '${cond_var}${op_field}len' } - g.writeln('for (int ${i} = 0; ${i} < ${cond_expr}; ++${i}) {') + g.writeln('for (${ast.int_type_name} ${i} = 0; ${i} < ${cond_expr}; ++${i}) {') if node.val_var != '_' { if mut val_sym.info is ast.FnType { g.write('\t') @@ -308,7 +308,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { idx := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } cond_sym := g.table.final_sym(node.cond_type) info := cond_sym.info as ast.ArrayFixed - g.writeln('for (int ${idx} = 0; ${idx} != ${info.size}; ++${idx}) {') + g.writeln('for (${ast.int_type_name} ${idx} = 0; ${idx} != ${info.size}; ++${idx}) {') if node.val_var != '_' { val_sym := g.table.sym(node.val_type) is_fixed_array := val_sym.kind == .array_fixed && !node.val_is_mut @@ -357,12 +357,12 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { idx := g.new_tmp_var() map_len := g.new_tmp_var() g.empty_line = true - g.writeln('int ${map_len} = ${cond_var}${dot_or_ptr}key_values.len;') - g.writeln('for (int ${idx} = 0; ${idx} < ${map_len}; ++${idx} ) {') + g.writeln('${ast.int_type_name} ${map_len} = ${cond_var}${dot_or_ptr}key_values.len;') + g.writeln('for (${ast.int_type_name} ${idx} = 0; ${idx} < ${map_len}; ++${idx} ) {') // TODO: don't have this check when the map has no deleted elements g.indent++ diff := g.new_tmp_var() - g.writeln('int ${diff} = ${cond_var}${dot_or_ptr}key_values.len - ${map_len};') + g.writeln('${ast.int_type_name} ${diff} = ${cond_var}${dot_or_ptr}key_values.len - ${map_len};') g.writeln('${map_len} = ${cond_var}${dot_or_ptr}key_values.len;') // TODO: optimize this g.writeln('if (${diff} < 0) {') @@ -413,7 +413,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { } field_accessor := if node.cond_type.is_ptr() { '->' } else { '.' } i := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } - g.write('for (int ${i} = 0; ${i} < ') + g.write('for (${ast.int_type_name} ${i} = 0; ${i} < ') g.expr(cond) g.writeln('${field_accessor}len; ++${i}) {') if node.val_var != '_' { @@ -522,7 +522,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { if node.kind == .map { // diff := g.new_tmp_var() - // g.writeln('int $diff = $cond_var${arw_or_pt}key_values.len - $map_len;') + // g.writeln('${ast.int_type_name} $diff = $cond_var${arw_or_pt}key_values.len - $map_len;') // g.writeln('if ($diff < 0) {') // g.writeln('\t$idx = -1;') // g.writeln('\t$map_len = $cond_var${arw_or_pt}key_values.len;') diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index ebef84bac0..8d9f7b4cfa 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -152,7 +152,7 @@ fn (mut g Gen) index_range_expr(node ast.IndexExpr, range ast.RangeExpr) { } else if sym.info is ast.ArrayFixed { g.write('${sym.info.size}') } else { - g.write('2147483647') // max_int + g.write('${max_int}') } g.write(')') diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 265b48d372..5b06a70d92 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -87,12 +87,12 @@ ${dec_fn_dec} { if (!root) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { - const int error_pos = (int)cJSON_GetErrorPos(); - int maxcontext_chars = 30; + const ${ast.int_type_name} error_pos = (${ast.int_type_name})cJSON_GetErrorPos(); + ${ast.int_type_name} maxcontext_chars = 30; byte *buf = builtin__vcalloc_noscan(maxcontext_chars + 10); if (error_pos > 0) { - int backlines = 1; - int backchars = error_pos < maxcontext_chars-7 ? (int)error_pos : maxcontext_chars-7 ; + ${ast.int_type_name} backlines = 1; + ${ast.int_type_name} backchars = error_pos < maxcontext_chars-7 ? (${ast.int_type_name})error_pos : maxcontext_chars-7 ; char *prevline_ptr = (char*)error_ptr; while(backchars--){ char prevc = *(prevline_ptr - 1); @@ -107,7 +107,7 @@ ${dec_fn_dec} { break; // stop at `{` too } } - int maxchars = builtin__vstrlen_char(prevline_ptr); + ${ast.int_type_name} maxchars = builtin__vstrlen_char(prevline_ptr); builtin__vmemcpy(buf, prevline_ptr, (maxchars < maxcontext_chars ? maxchars : maxcontext_chars)); } string msg; @@ -612,8 +612,8 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st dec.writeln('\t\t}') } - if var_t in ['i64', 'int', 'i8', 'u64', 'u32', 'u16', 'byte', 'u8', 'rune', 'f64', - 'f32'] { + if var_t in ['i8', 'i16', 'i32', 'i64', ast.int_type_name, 'int', 'u8', 'u16', + 'u32', 'u64', 'byte', 'rune', 'f64', 'f32'] { if number_is_met { var_num := var_t.replace('__', '.') last_num := last_number_type.replace('__', '.') @@ -703,7 +703,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st if is_skip { continue } - field_type := g.styp(field.typ) + field_type := vint2int(g.styp(field.typ)) field_sym := g.table.sym(field.typ) op := if utyp.is_ptr() { '->' } else { '.' } embed_member := if embed_prefix.len > 0 { '.${embed_prefix}' } else { '' } @@ -1031,6 +1031,7 @@ fn js_enc_name(typ string) string { if typ == 'i32' { suffix = typ.replace('i32', 'int') } + suffix = vint2int(suffix) name := 'json__encode_${suffix}' return util.no_dots(name) } @@ -1040,13 +1041,14 @@ fn js_dec_name(typ string) string { if typ == 'i32' { suffix = typ.replace('i32', 'int') } + suffix = vint2int(suffix) name := 'json__decode_${suffix}' return util.no_dots(name) } fn is_js_prim(typ string) bool { - return typ in ['int', 'rune', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i32', 'i64', 'u8', - 'u16', 'u32', 'u64', 'byte'] + return typ in [ast.int_type_name, 'int', 'rune', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', + 'i32', 'i64', 'u8', 'u16', 'u32', 'u64', 'byte'] } fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size int, ret_styp string) string { @@ -1062,7 +1064,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size is_array_fixed_val := g.table.final_sym(value_type).kind == .array_fixed if utyp.has_flag(.option) { if fixed_array_size > -1 { - fixed_array_idx += 'int fixed_array_idx = 0;' + fixed_array_idx += '${ast.int_type_name} fixed_array_idx = 0;' array_element_assign += '((${styp}*)res.data)[fixed_array_idx] = val;' fixed_array_idx_increment += 'fixed_array_idx++; res.state = 0;' } else { @@ -1072,11 +1074,11 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size } } else { if is_array_fixed_val { - fixed_array_idx += 'int fixed_array_idx = 0;' + fixed_array_idx += '${ast.int_type_name} fixed_array_idx = 0;' array_element_assign += 'memcpy(res[fixed_array_idx], val, sizeof(${styp}));' fixed_array_idx_increment += 'fixed_array_idx++;' } else if fixed_array_size > -1 { - fixed_array_idx += 'int fixed_array_idx = 0;' + fixed_array_idx += '${ast.int_type_name} fixed_array_idx = 0;' array_element_assign += 'res[fixed_array_idx] = val;' fixed_array_idx_increment += 'fixed_array_idx++;' } else { @@ -1150,7 +1152,7 @@ fn (mut g Gen) encode_array(utyp ast.Type, value_type ast.Type, fixed_array_size return ' o = cJSON_CreateArray(); - for (int i = 0; i < ${size_str}; i++){ + for (${ast.int_type_name} i = 0; i < ${size_str}; i++){ cJSON_AddItemToArray(o, ${fn_name}( (${data_str})[i] )); } ' @@ -1225,7 +1227,7 @@ fn (mut g Gen) encode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type) return ' o = cJSON_CreateObject(); Array_${styp} ${keys_tmp} = builtin__map_keys((map*)val.data); - for (int i = 0; i < ${keys_tmp}.len; ++i) { + for (${ast.int_type_name} i = 0; i < ${keys_tmp}.len; ++i) { ${key} cJSON_AddItemToObject(o, (char*) key.str, ${fn_name_v} ( *(${styp_v}*) builtin__map_get((map*)val.data, &key, &(${styp_v}[]) { ${zero} } ) ) ); } @@ -1235,7 +1237,7 @@ fn (mut g Gen) encode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type) return ' o = cJSON_CreateObject(); Array_${styp} ${keys_tmp} = builtin__map_keys(&val); - for (int i = 0; i < ${keys_tmp}.len; ++i) { + for (${ast.int_type_name} i = 0; i < ${keys_tmp}.len; ++i) { ${key} cJSON_AddItemToObject(o, (char*) key.str, ${fn_name_v} ( *(${styp_v}*) builtin__map_get(&val, &key, &(${styp_v}[]) { ${zero} } ) ) ); } diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index f29533ec7e..da4455f2ed 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -292,7 +292,7 @@ fn (mut g Gen) write_orm_update(node &ast.SqlStmtLine, table_name string, connec g.writeln('.kinds = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__OperationKind), 0),') g.writeln('.is_and = builtin____new_array_with_default_noscan(0, 0, sizeof(bool), 0),') g.writeln('.types = builtin____new_array_with_default_noscan(0, 0, sizeof(${ast.int_type_name}), 0),') - g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_int), 0),') + g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_${ast.int_type_name}), 0),') if node.updated_columns.len > 0 { g.writeln('.fields = builtin__new_array_from_c_array(${node.updated_columns.len}, ${node.updated_columns.len}, sizeof(string),') @@ -481,6 +481,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v } else if sym.kind == .enum { typ = g.table.sym(final_field_typ).cname } + typ = vint2int(typ) var := '${node.object_var}${member_access_type}${c_name(field.name)}' if final_field_typ.has_flag(.option) { g.writeln('${var}.state == 2? _const_orm__null_primitive : orm__${typ}_to_primitive(*(${ctyp}*)(${var}.data)),') @@ -503,7 +504,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v if auto_fields.len > 0 { g.writeln('.auto_fields = builtin__new_array_from_c_array(${auto_fields.len}, ${auto_fields.len}, sizeof(${ast.int_type_name}),') g.indent++ - g.write('_MOV((int[${auto_fields.len}]){') + g.write('_MOV((${ast.int_type_name}[${auto_fields.len}]){') for i in auto_fields { g.write(' ${i},') } @@ -533,6 +534,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v if typ == 'time__Time' { typ = 'time' } + typ = vint2int(typ) g.writeln('orm__Primitive ${id_name} = orm__${typ}_to_primitive(${node.object_var}${member_access_type}${c_name(primary_field.name)});') } for i, mut arr in arrs { @@ -540,9 +542,9 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v ctyp := g.styp(arr.table_expr.typ) is_option := opt_fields.contains(i) if is_option { - g.writeln('for (int ${idx} = 0; ${node.object_var}${member_access_type}${arr.object_var}.state != 2 && ${idx} < (*(Array_${ctyp}*)${node.object_var}${member_access_type}${arr.object_var}.data).len; ${idx}++) {') + g.writeln('for (${ast.int_type_name} ${idx} = 0; ${node.object_var}${member_access_type}${arr.object_var}.state != 2 && ${idx} < (*(Array_${ctyp}*)${node.object_var}${member_access_type}${arr.object_var}.data).len; ${idx}++) {') } else { - g.writeln('for (int ${idx} = 0; ${idx} < ${node.object_var}${member_access_type}${arr.object_var}.len; ${idx}++) {') + g.writeln('for (${ast.int_type_name} ${idx} = 0; ${idx} < ${node.object_var}${member_access_type}${arr.object_var}.len; ${idx}++) {') } g.indent++ last_ids := g.new_tmp_var() @@ -680,6 +682,7 @@ fn (mut g Gen) write_orm_primitive(t ast.Type, expr ast.Expr) { } else if g.table.final_sym(t).kind == .array { typ = g.table.sym(g.table.final_type(t)).cname.to_lower() } + typ = vint2int(typ) g.write('orm__${typ}_to_primitive(') if expr is ast.CallExpr { g.call_expr(expr) @@ -741,10 +744,10 @@ fn (mut g Gen) write_orm_where(where_expr ast.Expr) { g.write('.parentheses = ') if parentheses.len > 0 { - g.write('builtin__new_array_from_c_array(${parentheses.len}, ${parentheses.len}, sizeof(Array_int), _MOV((Array_int[${parentheses.len}]){') + g.write('builtin__new_array_from_c_array(${parentheses.len}, ${parentheses.len}, sizeof(Array_${ast.int_type_name}), _MOV((Array_${ast.int_type_name}[${parentheses.len}]){') for par in parentheses { if par.len > 0 { - g.write('builtin__new_array_from_c_array(${par.len}, ${par.len}, sizeof(${ast.int_type_name}), _MOV((int[${par.len}]){') + g.write('builtin__new_array_from_c_array(${par.len}, ${par.len}, sizeof(${ast.int_type_name}), _MOV((${ast.int_type_name}[${par.len}]){') for val in par { g.write('${val},') } @@ -755,7 +758,7 @@ fn (mut g Gen) write_orm_where(where_expr ast.Expr) { } g.write('}))') } else { - g.write('builtin____new_array_with_default_noscan(0, 0, sizeof(Array_int), 0)') + g.write('builtin____new_array_with_default_noscan(0, 0, sizeof(Array_${ast.int_type_name}), 0)') } g.writeln(',') @@ -1006,7 +1009,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re g.indent++ if types.len > 0 { - g.write('_MOV((int[${types.len}]){') + g.write('_MOV((${ast.int_type_name}[${types.len}]){') for typ in types { g.write(' ${typ},') } @@ -1032,7 +1035,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re g.writeln('.types = builtin____new_array_with_default_noscan(0, 0, sizeof(${ast.int_type_name}), 0),') g.writeln('.kinds = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__OperationKind), 0),') g.writeln('.is_and = builtin____new_array_with_default_noscan(0, 0, sizeof(bool), 0),') - g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_int), 0),') + g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_${ast.int_type_name}), 0),') if exprs.len > 0 { g.write('.data = builtin__new_array_from_c_array(${exprs.len}, ${exprs.len}, sizeof(orm__Primitive),') g.write(' _MOV((orm__Primitive[${exprs.len}]){') @@ -1054,7 +1057,7 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re g.writeln('.types = builtin____new_array_with_default_noscan(0, 0, sizeof(${ast.int_type_name}), 0),') g.writeln('.kinds = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__OperationKind), 0),') g.writeln('.is_and = builtin____new_array_with_default_noscan(0, 0, sizeof(bool), 0),') - g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_int), 0),') + g.writeln('.parentheses = builtin____new_array_with_default_noscan(0, 0, sizeof(Array_${ast.int_type_name}), 0),') g.writeln('.data = builtin____new_array_with_default_noscan(0, 0, sizeof(orm__Primitive), 0)') g.indent-- g.writeln('}') @@ -1077,11 +1080,11 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re g.writeln('Array_Array_orm__Primitive ${select_unwrapped_result_var_name} = (*(Array_Array_orm__Primitive*)${select_result_var_name}.data);') if node.is_count { - g.writeln('*(${unwrapped_c_typ}*) ${result_var}.data = *((*(orm__Primitive*) builtin__array_get((*(Array_orm__Primitive*) builtin__array_get(${select_unwrapped_result_var_name}, 0)), 0))._int);') + g.writeln('*(${unwrapped_c_typ}*) ${result_var}.data = *((*(orm__Primitive*) builtin__array_get((*(Array_orm__Primitive*) builtin__array_get(${select_unwrapped_result_var_name}, 0)), 0))._${ast.int_type_name});') } else { tmp := g.new_tmp_var() idx := g.new_tmp_var() - g.writeln('int ${idx} = 0;') + g.writeln('${ast.int_type_name} ${idx} = 0;') mut typ_str := '' if node.is_array { info := g.table.sym(node.typ).array_info() @@ -1337,8 +1340,8 @@ fn (g &Gen) get_orm_column_name_from_struct_field(field ast.StructField) string mut name := field.name if attr := field.attrs.find_first('sql') { - if attr.arg !in ['serial', 'i8', 'i16', 'int', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', - 'f64', 'bool', 'string'] { + if attr.arg !in ['serial', 'i8', 'i16', 'i32', 'int', 'i64', 'u8', 'u16', 'u32', 'u64', + 'f32', 'f64', 'bool', 'string'] { name = attr.arg } } diff --git a/vlib/v/gen/c/reflection.v b/vlib/v/gen/c/reflection.v index 4e289878eb..bafeaea450 100644 --- a/vlib/v/gen/c/reflection.v +++ b/vlib/v/gen/c/reflection.v @@ -123,7 +123,7 @@ fn (g &Gen) gen_type_array(types []ast.Type) string { if types.len == 0 { return g.gen_empty_array(ast.int_type_name) } - return 'builtin__new_array_from_c_array(${types.len},${types.len},sizeof(${ast.int_type_name}),_MOV((int[${types.len}]){${types.map(int(it).str()).join(',')}}))' + return 'builtin__new_array_from_c_array(${types.len},${types.len},sizeof(int),_MOV((int[${types.len}]){${types.map(int(it).str()).join(',')}}))' } // gen_string_array generates C code for []string diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index b97f8b4f4d..0978fbc57f 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -119,16 +119,46 @@ fn (mut g Gen) str_format(node ast.StringInterLiteral, i int, fmts []u8) (u64, s fmt_type = .si_c } else { match typ { - ast.i8_type { fmt_type = .si_i8 } - ast.u8_type { fmt_type = .si_u8 } - ast.i16_type { fmt_type = .si_i16 } - ast.u16_type { fmt_type = .si_u16 } - ast.i64_type { fmt_type = .si_i64 } - ast.u64_type { fmt_type = .si_u64 } - ast.u32_type { fmt_type = .si_u32 } - ast.usize_type { fmt_type = .si_u64 } - ast.isize_type { fmt_type = .si_i64 } - else { fmt_type = .si_i32 } + ast.i8_type { + fmt_type = .si_i8 + } + ast.u8_type { + fmt_type = .si_u8 + } + ast.i16_type { + fmt_type = .si_i16 + } + ast.u16_type { + fmt_type = .si_u16 + } + ast.i64_type { + fmt_type = .si_i64 + } + ast.u64_type { + fmt_type = .si_u64 + } + ast.i32_type { + fmt_type = .si_i32 + } + ast.u32_type { + fmt_type = .si_u32 + } + ast.int_type { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + fmt_type = .si_i64 + } $else { + fmt_type = .si_i32 + } + } + ast.usize_type { + fmt_type = .si_u64 + } + ast.isize_type { + fmt_type = .si_i64 + } + else { + fmt_type = .si_i32 + } } } } else { @@ -206,8 +236,14 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) { g.write('(byte)(') } else if typ == ast.i16_type { g.write('(u16)(') - } else if typ == ast.int_type { + } else if typ == ast.i32_type { g.write('(u32)(') + } else if typ == ast.int_type { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + g.write('(u64)(') + } $else { + g.write('(u32)(') + } } else { g.write('(u64)(') } diff --git a/vlib/v/gen/js/builtin_types.v b/vlib/v/gen/js/builtin_types.v index 2dc38dd9ef..1ee504cca5 100644 --- a/vlib/v/gen/js/builtin_types.v +++ b/vlib/v/gen/js/builtin_types.v @@ -29,7 +29,8 @@ fn (mut g JsGen) to_js_typ_val(t ast.Type) string { return g.to_js_typ_val(sym.info.variants[0]) } match sym.kind { - .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .int_literal, .float_literal { + .i8, .i16, .i32, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .int_literal, + .float_literal { styp = '${prefix}${g.sym_to_js_typ(sym)}(0)' } .bool { @@ -67,9 +68,16 @@ fn (mut g JsGen) sym_to_js_typ(sym ast.TypeSymbol) string { .i16 { styp = 'i16' } - .int { + .i32 { styp = 'int' } + .int { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + styp = 'i64' + } $else { + styp = 'int' + } + } .i64 { styp = 'i64' } @@ -352,7 +360,7 @@ fn (mut g JsGen) gen_builtin_type_defs() { for typ_name in v_types { // TODO: JsDoc match typ_name { - 'i8', 'i16', 'int', 'int_literal' { + 'i8', 'i16', 'i32', 'int', 'int_literal' { // TODO: Bounds checking g.gen_builtin_prototype( typ_name: typ_name diff --git a/vlib/v/gen/js/deep_copy.v b/vlib/v/gen/js/deep_copy.v index ebc0949e64..f407699c83 100644 --- a/vlib/v/gen/js/deep_copy.v +++ b/vlib/v/gen/js/deep_copy.v @@ -199,7 +199,7 @@ fn (mut g JsGen) final_gen_copy(typ StrType) { return } match styp { - 'byte', 'u8', 'u16', 'u32', 'u64', 'i16', 'int', 'i64', 'isize', 'usize', 'bool', + 'byte', 'u8', 'u16', 'u32', 'u64', 'i16', 'i32', 'int', 'i64', 'isize', 'usize', 'bool', 'int_literal', 'float_literal', 'f32', 'f64', 'voidptr' { g.definitions.writeln('function ${sym.cname}_\$copy(it) { return new ${sym.cname}(it.val); }') return diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index e9d358ffd3..6ec499619c 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -17,9 +17,9 @@ const js_reserved = ['await', 'break', 'case', 'catch', 'class', 'const', 'conti 'var', 'void', 'while', 'with', 'yield', 'Number', 'String', 'Boolean', 'Array', 'Map', 'document', 'Promise'] // used to generate type structs -const v_types = ['i8', 'i16', 'int', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64', 'int_literal', - 'float_literal', 'bool', 'string', 'map', 'array', 'rune', 'any', 'voidptr'] -const shallow_equatables = [ast.Kind.i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, +const v_types = ['i8', 'i16', 'i32', 'int', 'i64', 'u8', 'u16', 'u32', 'u64', 'f32', 'f64', + 'int_literal', 'float_literal', 'bool', 'string', 'map', 'array', 'rune', 'any', 'voidptr'] +const shallow_equatables = [ast.Kind.i8, .i16, .i32, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .int_literal, .float_literal, .bool, .string] const option_name = '_option' @@ -3186,8 +3186,15 @@ fn (mut g JsGen) greater_typ(left ast.Type, right ast.Type) ast.Type { if ast.u32_type_idx in lr { return ast.Type(ast.u32_type_idx) } + if ast.i32_type_idx in lr { + return ast.Type(ast.i32_type_idx) + } if ast.int_type_idx in lr { - return ast.Type(ast.int_type_idx) + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + return ast.Type(ast.i64_type_idx) + } $else { + return ast.Type(ast.i32_type_idx) + } } if ast.u16_type_idx in lr { return ast.Type(ast.u16_type_idx) diff --git a/vlib/v/gen/native/amd64.v b/vlib/v/gen/native/amd64.v index 0370e80ea8..346965632e 100644 --- a/vlib/v/gen/native/amd64.v +++ b/vlib/v/gen/native/amd64.v @@ -501,10 +501,19 @@ fn (mut c Amd64) inc_var(var Var, config VarConfig) { c.g.write16(0xFF48) size_str = 'QWORD' } - ast.i32_type_idx, ast.int_type_idx, ast.u32_type_idx, ast.rune_type_idx { + ast.i32_type_idx, ast.u32_type_idx, ast.rune_type_idx { c.g.write8(0xFF) size_str = 'DWORD' } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + c.g.write16(0xFF48) + size_str = 'QWORD' + } $else { + c.g.write8(0xFF) + size_str = 'DWORD' + } + } ast.i16_type_idx, ast.u16_type_idx { c.g.write8(0xFF) size_str = 'WORD' @@ -764,13 +773,25 @@ fn (mut c Amd64) mov_reg_to_var(var Var, r Register, config VarConfig) { c.g.write16(0x8948 + if is_extended_register { i32(4) } else { i32(0) }) size_str = 'QWORD' } - ast.i32_type_idx, ast.int_type_idx, ast.u32_type_idx, ast.rune_type_idx { + ast.i32_type_idx, ast.u32_type_idx, ast.rune_type_idx { if is_extended_register { c.g.write8(0x44) } c.g.write8(0x89) size_str = 'DWORD' } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + c.g.write16(0x8948 + if is_extended_register { i32(4) } else { i32(0) }) + size_str = 'QWORD' + } $else { + if is_extended_register { + c.g.write8(0x44) + } + c.g.write8(0x89) + size_str = 'DWORD' + } + } ast.i16_type_idx, ast.u16_type_idx { c.g.write8(0x66) if is_extended_register { @@ -904,7 +925,7 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) { c.g.write16(u16(integer)) c.g.println('mov WORD PTR[rbp-${int(offset).hex2()}], ${integer}') } - ast.i32_type_idx, ast.int_type_idx, ast.u32_type_idx, ast.rune_type_idx { + ast.i32_type_idx, ast.u32_type_idx, ast.rune_type_idx { c.g.write8(0xc7) c.g.write8(if is_far_var { i32(0x85) } else { i32(0x45) }) if is_far_var { @@ -915,6 +936,30 @@ fn (mut c Amd64) mov_int_to_var(var Var, integer i32, config VarConfig) { c.g.write32(integer) c.g.println('mov DWORD PTR[rbp-${int(offset).hex2()}], ${integer}') } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + c.g.write8(0x48) + c.g.write8(0xc7) + c.g.write8(if is_far_var { i32(0x85) } else { i32(0x45) }) + if is_far_var { + c.g.write32(i32((0xffffffff - i64(offset) + 1) % 0x100000000)) + } else { + c.g.write8((0xff - offset + 1) % 0x100) + } + c.g.write32(integer) + c.g.println('mov QWORD PTR[rbp-${int(offset).hex2()}], ${integer}') + } $else { + c.g.write8(0xc7) + c.g.write8(if is_far_var { i32(0x85) } else { i32(0x45) }) + if is_far_var { + c.g.write32(i32((0xffffffff - i64(offset) + 1) % 0x100000000)) + } else { + c.g.write8((0xff - offset + 1) % 0x100) + } + c.g.write32(integer) + c.g.println('mov DWORD PTR[rbp-${int(offset).hex2()}], ${integer}') + } + } ast.i64_type_idx, ast.u64_type_idx, ast.isize_type_idx, ast.usize_type_idx, ast.int_literal_type_idx { c.g.write8(0x48) @@ -3950,7 +3995,7 @@ fn (mut c Amd64) zero_fill(size i32, var LocalVar) { left -= 8 } if left >= 4 { - c.mov_int_to_var(var, 0, offset: size - left, typ: ast.int_type_idx) + c.mov_int_to_var(var, 0, offset: size - left, typ: ast.i32_type_idx) left -= 4 } if left >= 2 { diff --git a/vlib/v/gen/native/gen.v b/vlib/v/gen/native/gen.v index 3a02880b5e..2c8c3e593f 100644 --- a/vlib/v/gen/native/gen.v +++ b/vlib/v/gen/native/gen.v @@ -827,24 +827,64 @@ fn (mut g Gen) get_type_size(raw_type ast.Type) i32 { } if typ in ast.number_type_idxs { return match typ { - ast.i8_type_idx { 1 } - ast.u8_type_idx { 1 } - ast.i16_type_idx { 2 } - ast.u16_type_idx { 2 } - ast.int_type_idx { 4 } // TODO: change when V will have changed - ast.i32_type_idx { 4 } - ast.u32_type_idx { 4 } - ast.i64_type_idx { 8 } - ast.u64_type_idx { 8 } - ast.isize_type_idx { 8 } - ast.usize_type_idx { 8 } - ast.int_literal_type_idx { 8 } - ast.f32_type_idx { 4 } - ast.f64_type_idx { 8 } - ast.float_literal_type_idx { 8 } - ast.char_type_idx { 1 } - ast.rune_type_idx { 4 } - else { g.n_error('${@LOCATION} unknown type size ${typ}') } + ast.i8_type_idx { + 1 + } + ast.u8_type_idx { + 1 + } + ast.i16_type_idx { + 2 + } + ast.u16_type_idx { + 2 + } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + 8 + } $else { + 4 + } + } + ast.i32_type_idx { + 4 + } + ast.u32_type_idx { + 4 + } + ast.i64_type_idx { + 8 + } + ast.u64_type_idx { + 8 + } + ast.isize_type_idx { + 8 + } + ast.usize_type_idx { + 8 + } + ast.int_literal_type_idx { + 8 + } + ast.f32_type_idx { + 4 + } + ast.f64_type_idx { + 8 + } + ast.float_literal_type_idx { + 8 + } + ast.char_type_idx { + 1 + } + ast.rune_type_idx { + 4 + } + else { + g.n_error('${@LOCATION} unknown type size ${typ}') + } } } if typ.is_bool() { diff --git a/vlib/v/gen/wasm/ops.v b/vlib/v/gen/wasm/ops.v index 895cc593ba..87e1cc1a40 100644 --- a/vlib/v/gen/wasm/ops.v +++ b/vlib/v/gen/wasm/ops.v @@ -37,9 +37,16 @@ pub fn (mut g Gen) get_wasm_type(typ_ ast.Type) wasm.ValType { return match typ { ast.isize_type_idx, ast.usize_type_idx, ast.i8_type_idx, ast.u8_type_idx, ast.char_type_idx, ast.rune_type_idx, ast.i16_type_idx, ast.u16_type_idx, - ast.int_type_idx, ast.u32_type_idx { + ast.i32_type_idx, ast.u32_type_idx { wasm.ValType.i32_t } + ast.int_type_idx { + $if new_int ? && (arm64 || amd64 || rv64 || s390x || ppc64le || loongarch64) { + wasm.ValType.i64_t + } $else { + wasm.ValType.i32_t + } + } ast.i64_type_idx, ast.u64_type_idx { wasm.ValType.i64_t } diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 3e025b609e..6817e5d627 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -702,6 +702,9 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b 'i16' { ret = ast.i16_type } + 'i32' { + ret = ast.i32_type + } 'int' { ret = ast.int_type } @@ -856,7 +859,7 @@ fn (mut p Parser) find_type_or_add_placeholder(name string, language ast.Languag return typ } // not found - add placeholder - idx = p.table.add_placeholder_type(name, language) + idx = p.table.add_placeholder_type(name, name, language) return ast.new_type(idx) } @@ -936,10 +939,10 @@ fn (mut p Parser) parse_generic_inst_type(name string) ast.Type { if gt_idx > 0 { return ast.new_type(gt_idx) } - gt_idx = p.table.add_placeholder_type(bs_name, .v) + gt_idx = p.table.add_placeholder_type(bs_name, bs_cname, .v) mut parent_idx := p.table.type_idxs[name] if parent_idx == 0 { - parent_idx = p.table.add_placeholder_type(name, .v) + parent_idx = p.table.add_placeholder_type(name, name, .v) } parent_sym := p.table.sym(ast.new_type(parent_idx)) match parent_sym.info { diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index fcceb679ea..4fda3816d3 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -252,8 +252,6 @@ pub mut: warn_about_allocs bool // -warn-about-allocs warngs about every single allocation, e.g. 'hi $name'. Mostly for low level development where manual memory management is used. // game prototyping flags: div_by_zero_is_zero bool // -div-by-zero-is-zero makes so `x / 0 == 0`, i.e. eliminates the division by zero panics/segfaults - // temp - // use_64_int bool // forwards compatibility settings: relaxed_gcc14 bool = true // turn on the generated pragmas, that make gcc versions > 14 a lot less pedantic. The default is to have those pragmas in the generated C output, so that gcc-14 can be used on Arch etc. // @@ -424,9 +422,6 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin '-progress' { // processed by testing tools in cmd/tools/modules/testing/common.v } - //'-i64' { - // res.use_64_int = true - //} '-Wimpure-v' { res.warn_impure_v = true }