v / vlib / v2 / ssa / build_index_from_flat_test.v
134 lines · 128 sloc · 3.92 KB · d358576851079d4716834cc86627307d28acd1ae
Raw
1// Copyright (c) 2026 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4// vtest build: macos
5//
6// Bit-equality pin for s194: `build_index_from_flat` is the tenth per-kind
7// arm inside `build_expr_from_flat`. IndexExpr flat encoding (flat.v:1946)
8// is (.expr_index, pos, -1, -1, 0, flags, [edge0=lhs, edge1=expr]). Both
9// edges need full `decode_expr` rehydration: `build_index` recursively calls
10// `build_expr` on both, and `expr_type(ast.Expr(expr))` for the pos.id
11// type-check lookup. `is_gated` flag is intentionally ignored (build_index
12// doesn't read it).
13module ssa
14
15import v2.ast
16import v2.token
17import v2.types
18
19// Fixture: `fn first(p &int) int { return p[0] }`. IndexExpr with
20// lhs=Ident('p'), expr=BasicLiteral('0'). Param type is `&int` (ptr to int)
21// so build_index falls through to the final ptr_t branch — emits GEP+load
22// without needing struct registration for array/string/map.
23fn make_index_fixture() []ast.File {
24 return [
25 ast.File{
26 name: 'main.v'
27 mod: 'main'
28 stmts: [
29 ast.Stmt(ast.ModuleStmt{
30 name: 'main'
31 }),
32 ast.Stmt(ast.FnDecl{
33 name: 'first'
34 typ: ast.FnType{
35 params: [
36 ast.Parameter{
37 name: 'p'
38 typ: ast.Expr(ast.PrefixExpr{
39 op: token.Token.amp
40 expr: ast.Expr(ast.Ident{
41 name: 'int'
42 })
43 })
44 },
45 ]
46 return_type: ast.Expr(ast.Ident{
47 name: 'int'
48 })
49 }
50 stmts: [
51 ast.Stmt(ast.ReturnStmt{
52 exprs: [
53 ast.Expr(ast.IndexExpr{
54 lhs: ast.Expr(ast.Ident{
55 name: 'p'
56 })
57 expr: ast.Expr(ast.BasicLiteral{
58 kind: .number
59 value: '0'
60 })
61 }),
62 ]
63 }),
64 ]
65 }),
66 ]
67 },
68 ]
69}
70
71fn build_via_legacy_index(files []ast.File, env &types.Environment, name string) &Module {
72 mut mod := Module.new(name)
73 mut b := Builder.new_with_env(mod, env)
74 b.register_fn_signatures(files[0])
75 b.build_fn_bodies(files[0])
76 return mod
77}
78
79fn build_via_flat_index(files []ast.File, env &types.Environment, name string) &Module {
80 flat := ast.flatten_files(files)
81 mut mod := Module.new(name)
82 mut b := Builder.new_with_env(mod, env)
83 b.register_fn_signatures_from_flat(flat.file_cursor(0))
84 stmts := flat.file_cursor(0).stmts()
85 for si in 0 .. stmts.len() {
86 c := stmts.at(si)
87 if c.kind() != .stmt_fn_decl {
88 continue
89 }
90 decl := c.fn_decl_signature()
91 fn_name := b.mangle_fn_name(decl)
92 func_idx := b.fn_index[fn_name] or { continue }
93 b.cur_func = func_idx
94 b.label_blocks = map[string]BlockID{}
95 b.vars = map[string]ValueID{}
96 entry := mod.add_block(func_idx, 'entry')
97 b.cur_block = entry
98 for param in decl.typ.params {
99 param_type := b.ast_type_to_ssa(param.typ)
100 param_val := mod.add_value_node(.argument, param_type, param.name, 0)
101 mod.func_add_param(func_idx, param_val)
102 alloca := mod.add_instr(.alloca, entry, mod.type_store.get_ptr(param_type), []ValueID{})
103 mod.add_instr(.store, entry, 0, [param_val, alloca])
104 b.vars[param.name] = alloca
105 }
106 body := c.list_at(3)
107 for bi in 0 .. body.len() {
108 stmt_c := body.at(bi)
109 if stmt_c.kind() == .stmt_return {
110 ret_expr_c := stmt_c.edge(0)
111 val := b.build_expr_from_flat(ret_expr_c)
112 mod.add_instr(.ret, b.cur_block, 0, [val])
113 } else {
114 b.build_stmt_from_flat(stmt_c)
115 }
116 }
117 if !b.block_has_terminator(b.cur_block) {
118 mod.add_instr(.ret, b.cur_block, 0, []ValueID{})
119 }
120 }
121 return mod
122}
123
124fn test_build_index_from_flat_ptr_matches_legacy() {
125 files := make_index_fixture()
126 env := types.Environment.new()
127 mod_legacy := build_via_legacy_index(files, env, 'index_legacy')
128 mod_flat := build_via_flat_index(files, env, 'index_flat')
129
130 assert mod_legacy.funcs.len == mod_flat.funcs.len
131 assert mod_legacy.blocks.len == mod_flat.blocks.len
132 assert mod_legacy.instrs.len == mod_flat.instrs.len
133 assert mod_legacy.values.len == mod_flat.values.len
134}
135