| 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 s191: `build_selector_from_flat` is the seventh |
| 7 | // per-kind arm inside `build_expr_from_flat`. SelectorExpr flat encoding |
| 8 | // (flat.v:2074) is (.expr_selector, pos, -1, -1, 0, 0, [edge0=lhs, |
| 9 | // edge1=rhs=Ident]). The cursor port decodes the lhs from edge 0 via |
| 10 | // `decode_expr` (build_selector pattern-matches on lhs heavily — `is |
| 11 | // ast.EmptyExpr`, `is ast.Ident`, recursive `build_expr(expr.lhs)` / |
| 12 | // `build_selector_addr(expr.lhs)` for chained selectors) and rebuilds the |
| 13 | // rhs Ident from edge 1's name/pos. |
| 14 | module ssa |
| 15 | |
| 16 | import v2.ast |
| 17 | import v2.types |
| 18 | |
| 19 | // Fixture: `fn seek_end() int { return C.SEEK_END }`. SelectorExpr with |
| 20 | // lhs=Ident('C'), rhs=Ident('SEEK_END'). build_selector immediately matches |
| 21 | // the `C.X` branch and emits a constant int(32) value '2' — exercises the |
| 22 | // SelectorExpr arm without needing struct registration. |
| 23 | fn make_selector_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: 'seek_end' |
| 34 | typ: ast.FnType{ |
| 35 | return_type: ast.Expr(ast.Ident{ |
| 36 | name: 'int' |
| 37 | }) |
| 38 | } |
| 39 | stmts: [ |
| 40 | ast.Stmt(ast.ReturnStmt{ |
| 41 | exprs: [ |
| 42 | ast.Expr(ast.SelectorExpr{ |
| 43 | lhs: ast.Expr(ast.Ident{ |
| 44 | name: 'C' |
| 45 | }) |
| 46 | rhs: ast.Ident{ |
| 47 | name: 'SEEK_END' |
| 48 | } |
| 49 | }), |
| 50 | ] |
| 51 | }), |
| 52 | ] |
| 53 | }), |
| 54 | ] |
| 55 | }, |
| 56 | ] |
| 57 | } |
| 58 | |
| 59 | fn build_via_legacy_selector(files []ast.File, env &types.Environment, name string) &Module { |
| 60 | mut mod := Module.new(name) |
| 61 | mut b := Builder.new_with_env(mod, env) |
| 62 | b.register_fn_signatures(files[0]) |
| 63 | b.build_fn_bodies(files[0]) |
| 64 | return mod |
| 65 | } |
| 66 | |
| 67 | fn build_via_flat_selector(files []ast.File, env &types.Environment, name string) &Module { |
| 68 | flat := ast.flatten_files(files) |
| 69 | mut mod := Module.new(name) |
| 70 | mut b := Builder.new_with_env(mod, env) |
| 71 | b.register_fn_signatures_from_flat(flat.file_cursor(0)) |
| 72 | stmts := flat.file_cursor(0).stmts() |
| 73 | for si in 0 .. stmts.len() { |
| 74 | c := stmts.at(si) |
| 75 | if c.kind() != .stmt_fn_decl { |
| 76 | continue |
| 77 | } |
| 78 | decl := c.fn_decl_signature() |
| 79 | fn_name := b.mangle_fn_name(decl) |
| 80 | func_idx := b.fn_index[fn_name] or { continue } |
| 81 | b.cur_func = func_idx |
| 82 | b.label_blocks = map[string]BlockID{} |
| 83 | b.vars = map[string]ValueID{} |
| 84 | entry := mod.add_block(func_idx, 'entry') |
| 85 | b.cur_block = entry |
| 86 | for param in decl.typ.params { |
| 87 | param_type := b.ast_type_to_ssa(param.typ) |
| 88 | param_val := mod.add_value_node(.argument, param_type, param.name, 0) |
| 89 | mod.func_add_param(func_idx, param_val) |
| 90 | alloca := mod.add_instr(.alloca, entry, mod.type_store.get_ptr(param_type), []ValueID{}) |
| 91 | mod.add_instr(.store, entry, 0, [param_val, alloca]) |
| 92 | b.vars[param.name] = alloca |
| 93 | } |
| 94 | body := c.list_at(3) |
| 95 | for bi in 0 .. body.len() { |
| 96 | stmt_c := body.at(bi) |
| 97 | if stmt_c.kind() == .stmt_return { |
| 98 | ret_expr_c := stmt_c.edge(0) |
| 99 | val := b.build_expr_from_flat(ret_expr_c) |
| 100 | mod.add_instr(.ret, b.cur_block, 0, [val]) |
| 101 | } else { |
| 102 | b.build_stmt_from_flat(stmt_c) |
| 103 | } |
| 104 | } |
| 105 | if !b.block_has_terminator(b.cur_block) { |
| 106 | mod.add_instr(.ret, b.cur_block, 0, []ValueID{}) |
| 107 | } |
| 108 | } |
| 109 | return mod |
| 110 | } |
| 111 | |
| 112 | fn test_build_selector_from_flat_c_const_matches_legacy() { |
| 113 | files := make_selector_fixture() |
| 114 | env := types.Environment.new() |
| 115 | mod_legacy := build_via_legacy_selector(files, env, 'sel_legacy') |
| 116 | mod_flat := build_via_flat_selector(files, env, 'sel_flat') |
| 117 | |
| 118 | assert mod_legacy.funcs.len == mod_flat.funcs.len |
| 119 | assert mod_legacy.blocks.len == mod_flat.blocks.len |
| 120 | assert mod_legacy.instrs.len == mod_flat.instrs.len |
| 121 | assert mod_legacy.values.len == mod_flat.values.len |
| 122 | } |
| 123 | |