v / vlib / v2 / ssa / build_infix_from_flat_test.v
135 lines · 129 sloc · 3.8 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 s193: `build_infix_from_flat` is the ninth per-kind
7// arm inside `build_expr_from_flat`. InfixExpr flat encoding (flat.v:1956)
8// is (.expr_infix, pos, -1, -1, u16(op), 0, [edge0=lhs, edge1=rhs]). Both
9// edges need full `decode_expr` rehydration: `build_infix` calls
10// `is_none_expr` (sum-type match) and recursive `build_expr` on both sides.
11module ssa
12
13import v2.ast
14import v2.token
15import v2.types
16
17// Fixture: `fn add(x int, y int) int { return x + y }`. InfixExpr with
18// op=.plus, lhs=Ident('x'), rhs=Ident('y'). Exercises the common-case
19// integer addition path (no string/array/pointer-arith/float promotion
20// branches fire).
21fn make_infix_fixture() []ast.File {
22 return [
23 ast.File{
24 name: 'main.v'
25 mod: 'main'
26 stmts: [
27 ast.Stmt(ast.ModuleStmt{
28 name: 'main'
29 }),
30 ast.Stmt(ast.FnDecl{
31 name: 'add'
32 typ: ast.FnType{
33 params: [
34 ast.Parameter{
35 name: 'x'
36 typ: ast.Expr(ast.Ident{
37 name: 'int'
38 })
39 },
40 ast.Parameter{
41 name: 'y'
42 typ: ast.Expr(ast.Ident{
43 name: 'int'
44 })
45 },
46 ]
47 return_type: ast.Expr(ast.Ident{
48 name: 'int'
49 })
50 }
51 stmts: [
52 ast.Stmt(ast.ReturnStmt{
53 exprs: [
54 ast.Expr(ast.InfixExpr{
55 op: token.Token.plus
56 lhs: ast.Expr(ast.Ident{
57 name: 'x'
58 })
59 rhs: ast.Expr(ast.Ident{
60 name: 'y'
61 })
62 }),
63 ]
64 }),
65 ]
66 }),
67 ]
68 },
69 ]
70}
71
72fn build_via_legacy_infix(files []ast.File, env &types.Environment, name string) &Module {
73 mut mod := Module.new(name)
74 mut b := Builder.new_with_env(mod, env)
75 b.register_fn_signatures(files[0])
76 b.build_fn_bodies(files[0])
77 return mod
78}
79
80fn build_via_flat_infix(files []ast.File, env &types.Environment, name string) &Module {
81 flat := ast.flatten_files(files)
82 mut mod := Module.new(name)
83 mut b := Builder.new_with_env(mod, env)
84 b.register_fn_signatures_from_flat(flat.file_cursor(0))
85 stmts := flat.file_cursor(0).stmts()
86 for si in 0 .. stmts.len() {
87 c := stmts.at(si)
88 if c.kind() != .stmt_fn_decl {
89 continue
90 }
91 decl := c.fn_decl_signature()
92 fn_name := b.mangle_fn_name(decl)
93 func_idx := b.fn_index[fn_name] or { continue }
94 b.cur_func = func_idx
95 b.label_blocks = map[string]BlockID{}
96 b.vars = map[string]ValueID{}
97 entry := mod.add_block(func_idx, 'entry')
98 b.cur_block = entry
99 for param in decl.typ.params {
100 param_type := b.ast_type_to_ssa(param.typ)
101 param_val := mod.add_value_node(.argument, param_type, param.name, 0)
102 mod.func_add_param(func_idx, param_val)
103 alloca := mod.add_instr(.alloca, entry, mod.type_store.get_ptr(param_type), []ValueID{})
104 mod.add_instr(.store, entry, 0, [param_val, alloca])
105 b.vars[param.name] = alloca
106 }
107 body := c.list_at(3)
108 for bi in 0 .. body.len() {
109 stmt_c := body.at(bi)
110 if stmt_c.kind() == .stmt_return {
111 ret_expr_c := stmt_c.edge(0)
112 val := b.build_expr_from_flat(ret_expr_c)
113 mod.add_instr(.ret, b.cur_block, 0, [val])
114 } else {
115 b.build_stmt_from_flat(stmt_c)
116 }
117 }
118 if !b.block_has_terminator(b.cur_block) {
119 mod.add_instr(.ret, b.cur_block, 0, []ValueID{})
120 }
121 }
122 return mod
123}
124
125fn test_build_infix_from_flat_add_matches_legacy() {
126 files := make_infix_fixture()
127 env := types.Environment.new()
128 mod_legacy := build_via_legacy_infix(files, env, 'infix_legacy')
129 mod_flat := build_via_flat_infix(files, env, 'infix_flat')
130
131 assert mod_legacy.funcs.len == mod_flat.funcs.len
132 assert mod_legacy.blocks.len == mod_flat.blocks.len
133 assert mod_legacy.instrs.len == mod_flat.instrs.len
134 assert mod_legacy.values.len == mod_flat.values.len
135}
136