Skip to content

Commit 09cc0c5

Browse files
committed
v2: lots of arm64 fixes: v2 compiled with arm64 can now compile hello world!; v2.eval; v2.cleanc fixes to compile viper engine
1 parent 53db6f1 commit 09cc0c5

86 files changed

Lines changed: 18894 additions & 3998 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/v2/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
./v2 -nomarkused -backend cleanc file.v
3434
```
3535

36+
# Eval backend
37+
```
38+
# Execute a small V program directly from the v2 AST:
39+
./v2 -backend eval file.v
40+
41+
# Current scope: literals, locals/consts, plain fn calls, if/for/range,
42+
# arrays, indexing, len, println/print, and basic string interpolation.
43+
```
44+
3645
# SSA C backend (restored)
3746
```
3847
# Run the SSA -> C backend directly:

‎cmd/v2/test_all.sh‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@ set -euo pipefail
33

44
cd "$(dirname "$0")"
55

6-
echo "=== 1/11: Self-host test ==="
6+
echo "=== 1/12: Self-host test ==="
77
bash test_v2_self.sh
88

99
echo ""
10-
echo "=== 2/11: Rebuild v2 and run builtin test files ==="
10+
echo "=== 2/12: Rebuild v2 and run builtin test files ==="
1111
rm -rf /tmp/v2_cleanc_obj_cache
1212
v self && v -o v2 v2.v
1313
./v2 ../../vlib/builtin/array_test.v
1414
./v2 ../../vlib/builtin/string_test.v
1515
./v2 ../../vlib/builtin/map_test.v
1616

1717
echo ""
18-
echo "=== 3/11: Builtin test files (arm64) ==="
18+
echo "=== 3/12: Builtin test files (arm64) ==="
1919
./v2 -backend arm64 ../../vlib/builtin/array_test.v
2020
./v2 -backend arm64 ../../vlib/builtin/string_test.v
2121
./v2 -backend arm64 ../../vlib/builtin/map_test.v
2222

2323
echo ""
24-
echo "=== 4/11: Math test ==="
24+
echo "=== 4/12: Math test ==="
2525
./v2 ../../vlib/math/math_test.v
2626

2727
echo ""
28-
echo "=== 5/11: Math test (arm64) ==="
28+
echo "=== 5/12: Math test (arm64) ==="
2929
./v2 -backend arm64 ../../vlib/math/math_test.v
3030

3131
echo ""
32-
echo "=== 6/11: Sumtype tests ==="
32+
echo "=== 6/12: Sumtype tests ==="
3333
./v2 test_sumtype.v
3434
./v2 test_sumtype2.v
3535
./v2 test_sumtype3.v
@@ -42,23 +42,36 @@ echo "=== 6/11: Sumtype tests ==="
4242
./v2 test_sumtype_global.v
4343

4444
echo ""
45-
echo "=== 7/11: SSA backends test (arm64) ==="
45+
echo "=== 7/12: Sumtype tests (arm64) ==="
46+
./v2 -backend arm64 test_sumtype.v
47+
./v2 -backend arm64 test_sumtype2.v
48+
./v2 -backend arm64 test_sumtype3.v
49+
./v2 -backend arm64 test_sumtype4.v
50+
./v2 -backend arm64 test_sumtype_pos.v
51+
./v2 -backend arm64 test_sumtype_data.v
52+
./v2 -backend arm64 test_sumtype_ifexpr.v
53+
./v2 -backend arm64 test_sumtype_nested.v
54+
./v2 -backend arm64 test_sumtype_many.v
55+
./v2 -backend arm64 test_sumtype_global.v
56+
57+
echo ""
58+
echo "=== 8/12: SSA backends test (arm64) ==="
4659
v -gc none run test_ssa_backends.v arm64
4760

4861
echo ""
49-
echo "=== 8/11: SSA backends test (cleanc) ==="
62+
echo "=== 9/12: SSA backends test (cleanc) ==="
5063
v -gc none run test_ssa_backends.v cleanc
5164

5265
echo ""
53-
echo "=== 9/11: Transformer unit tests ==="
66+
echo "=== 10/12: Transformer unit tests ==="
5467
v ../../vlib/v2/transformer/transformer_test.v
5568

5669
echo ""
57-
echo "=== 10/11: Transformer integration test ==="
70+
echo "=== 11/12: Transformer integration test ==="
5871
v ../../vlib/v2/transformer/transformer_v2_darwin_test.v
5972

6073
echo ""
61-
echo "=== 11/11: Cleanc runtime tests ==="
74+
echo "=== 12/12: Cleanc runtime tests ==="
6275
v -gc none run ../../vlib/v2/gen/cleanc/tests/run_tests.v
6376

6477
echo ""

‎cmd/v2/test_sumtype_nested.v‎

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,107 @@
1-
struct Pos {
2-
x int
3-
y int
1+
// Test sum type with struct variants (not just primitives)
2+
// This mirrors the AST's Expr sum type pattern
3+
4+
struct BasicLit {
5+
kind int
6+
value string
47
}
58

69
struct Ident {
7-
pos Pos
810
name string
911
}
1012

11-
struct EmptyExpr {
12-
dummy u8
13+
struct CallOrCast {
14+
lhs Expr
15+
expr Expr
1316
}
1417

15-
type Expr = EmptyExpr | Ident
18+
type Expr = BasicLit | CallOrCast | Ident
1619

17-
fn (e Expr) get_pos() Pos {
18-
return match e {
19-
Ident { e.pos }
20-
EmptyExpr { Pos{} }
20+
fn make_basic_lit(val string) Expr {
21+
return BasicLit{
22+
kind: 1
23+
value: val
2124
}
2225
}
2326

24-
fn parse_a() Expr {
25-
return Expr(Ident{
26-
pos: Pos{
27-
x: 42
28-
y: 99
29-
}
30-
name: 'aaa'
31-
})
27+
fn make_ident(name string) Expr {
28+
return Ident{
29+
name: name
30+
}
3231
}
3332

34-
fn parse_b() Expr {
35-
return Expr(Ident{
36-
pos: Pos{
37-
x: 10
38-
y: 20
39-
}
40-
name: 'bbb'
41-
})
33+
fn make_call_or_cast(lhs Expr, expr Expr) Expr {
34+
return CallOrCast{
35+
lhs: lhs
36+
expr: expr
37+
}
38+
}
39+
40+
fn test_basic_lit() {
41+
e := make_basic_lit('42')
42+
if e is BasicLit {
43+
assert e.value == '42'
44+
assert e.kind == 1
45+
} else {
46+
assert false
47+
}
4248
}
4349

44-
fn test_nested(is_lcbr bool, is_comptime bool) {
45-
// This mirrors the parser pattern exactly
46-
mut cond := if is_lcbr {
47-
Expr(EmptyExpr{})
50+
fn test_ident() {
51+
e := make_ident('hello')
52+
if e is Ident {
53+
assert e.name == 'hello'
4854
} else {
49-
if is_comptime { parse_a() } else { parse_b() }
55+
assert false
5056
}
51-
p := cond.get_pos()
52-
println(p.x.str())
53-
println(p.y.str())
5457
}
5558

56-
fn main() {
57-
println('case 1: lcbr=false comptime=true')
58-
test_nested(false, true) // should print 42, 99
59-
println('case 2: lcbr=false comptime=false')
60-
test_nested(false, false) // should print 10, 20
61-
println('case 3: lcbr=true comptime=true')
62-
test_nested(true, true) // should print 0, 0
59+
fn test_call_or_cast() {
60+
lhs := make_ident('int')
61+
arg := make_basic_lit('0')
62+
e := make_call_or_cast(lhs, arg)
63+
if e is CallOrCast {
64+
if e.lhs is Ident {
65+
assert e.lhs.name == 'int'
66+
} else {
67+
assert false
68+
}
69+
if e.expr is BasicLit {
70+
assert e.expr.value == '0'
71+
} else {
72+
assert false
73+
}
74+
} else {
75+
assert false
76+
}
77+
}
78+
79+
fn test_nested_sum_field_access() {
80+
// Create CallOrCast{lhs: Ident("int"), expr: BasicLit("0")}
81+
// This mirrors: __global g_main_argc = int(0)
82+
lhs := Expr(Ident{
83+
name: 'int'
84+
})
85+
arg := Expr(BasicLit{
86+
kind: 1
87+
value: '0'
88+
})
89+
coce := CallOrCast{
90+
lhs: lhs
91+
expr: arg
92+
}
93+
e := Expr(coce)
94+
if e is CallOrCast {
95+
// Access the nested Expr fields
96+
inner_expr := e.expr
97+
if inner_expr is BasicLit {
98+
assert inner_expr.value == '0'
99+
} else {
100+
eprintln('ERROR: inner_expr is not BasicLit')
101+
assert false
102+
}
103+
} else {
104+
eprintln('ERROR: e is not CallOrCast')
105+
assert false
106+
}
63107
}

‎cmd/v2/v2.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ fn is_test_file(files []string) bool {
8585

8686
// get_files extracts source files from args, excluding options and their values
8787
fn get_files(args []string) []string {
88-
options_with_values := ['-backend', '-b', '-o', '-output', '-arch', '-printfn', '-gc', '-d']
88+
options_with_values := ['-backend', '-b', '-o', '-output', '-arch', '-printfn', '-gc', '-d',
89+
'-hot-fn']
8990
mut files := []string{}
9091
mut skip_next := false
9192
for arg in args {

‎vlib/builtin/array.v‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
263263
for _ in 0 .. count {
264264
if depth > 0 {
265265
ary_clone := a.clone_to_depth(depth)
266-
vmemcpy(eptr, &u8(ary_clone.data), a_total_size)
266+
vmemcpy(eptr, ary_clone.data, a_total_size)
267267
} else {
268-
vmemcpy(eptr, &u8(a.data), a_total_size)
268+
vmemcpy(eptr, a.data, a_total_size)
269269
}
270270
eptr += arr_step_size
271271
}
@@ -733,7 +733,7 @@ pub fn (a &array) clone_to_depth(depth int) array {
733733
return arr
734734
} else {
735735
if a.data != 0 && source_capacity_in_bytes > 0 {
736-
unsafe { vmemcpy(&u8(arr.data), a.data, source_capacity_in_bytes) }
736+
unsafe { vmemcpy(arr.data, a.data, source_capacity_in_bytes) }
737737
}
738738
return arr
739739
}
@@ -976,14 +976,9 @@ pub fn (mut a array) sort_with_compare(callback fn (voidptr, voidptr) int) {
976976
// It uses the results of the given function to determine sort order.
977977
// See also .sort_with_compare()
978978
pub fn (a &array) sorted_with_compare(callback fn (voidptr, voidptr) int) array {
979-
$if freestanding {
980-
panic('sorted_with_compare does not work with -freestanding')
981-
} $else {
982-
mut r := a.clone()
983-
unsafe { vqsort(r.data, usize(r.len), usize(r.element_size), callback) }
984-
return r
985-
}
986-
return array{}
979+
mut r := a.clone()
980+
unsafe { C.qsort(r.data, usize(r.len), usize(r.element_size), voidptr(callback)) }
981+
return r
987982
}
988983

989984
// contains determines whether an array includes a certain value among its elements.
@@ -1046,7 +1041,7 @@ pub fn (b []u8) hex() string {
10461041
if b.len == 0 {
10471042
return ''
10481043
}
1049-
return unsafe { data_to_hex_string(&u8(b.data), b.len) }
1044+
return unsafe { data_to_hex_string(b.data, b.len) }
10501045
}
10511046

10521047
// copy copies the `src` byte array elements to the `dst` byte array.
@@ -1057,7 +1052,7 @@ pub fn (b []u8) hex() string {
10571052
pub fn copy(mut dst []u8, src []u8) int {
10581053
min := if dst.len < src.len { dst.len } else { src.len }
10591054
if min > 0 {
1060-
unsafe { vmemmove(&u8(dst.data), src.data, min) }
1055+
unsafe { vmemmove(dst.data, src.data, min) }
10611056
}
10621057
return min
10631058
}

‎vlib/builtin/builtin.v‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ fn __as_cast(obj voidptr, obj_type int, expected_type int) voidptr {
3535
}
3636
return obj
3737
}
38+
39+
@[inline]
40+
fn __v2_flag_has_int(receiver int, flag int) bool {
41+
return (receiver & flag) != 0
42+
}
43+
44+
@[inline]
45+
fn __v2_flag_all_int(receiver int, flags int) bool {
46+
return (receiver & flags) == flags
47+
}

‎vlib/builtin/chan_option_result.v‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,10 @@ fn _result_ok(data voidptr, mut res _result, size int) {
5555

5656
// str returns the message of IError.
5757
pub fn (err IError) str() string {
58-
return match err {
59-
None__ {
60-
'none'
61-
}
62-
Error {
63-
err.msg()
64-
}
65-
MessageError {
66-
(*err).str()
67-
}
68-
else {
69-
'${err.type_name()}: ${err.msg()}'
70-
}
58+
if err is None__ {
59+
return 'none'
7160
}
61+
return err.msg()
7262
}
7363

7464
// Error is the empty default implementation of `IError`.

0 commit comments

Comments
 (0)