Skip to content

Commit 52d3d83

Browse files
committed
v2: parse and build builtin/string.v; remove ssa.register_string_type()
1 parent a3bb7da commit 52d3d83

4 files changed

Lines changed: 35 additions & 54 deletions

File tree

‎cmd/v2/builtin/string.v‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
5+
// Builtin string struct for v2 SSA backend testing.
6+
// This must match the definition in vlib/v2/types/universe.v
7+
8+
pub struct string {
9+
str &u8
10+
len int
11+
is_lit int
12+
}

‎cmd/v2/test_ssa_backends.v‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ fn main() {
2626
prefs := &pref.Preferences{}
2727
mut file_set := token.FileSet.new()
2828
mut p := parser.Parser.new(prefs)
29+
mut transformer := transform.Transformer.new()
30+
31+
// Get the directory where this script is located
32+
exe_dir := os.dir(os.executable())
33+
34+
// Initialize SSA Module
35+
mut mod := ssa.Module.new('main')
36+
mut builder := ssa.Builder.new(mod)
37+
38+
// Parse and build builtin string.v first
39+
builtin_file := os.join_path(exe_dir, 'builtin', 'string.v')
40+
if os.exists(builtin_file) {
41+
println('[*] Parsing builtin/string.v...')
42+
parsed_builtin := p.parse_file(builtin_file, mut file_set)
43+
if parsed_builtin.stmts.len > 0 {
44+
println(' Found ${parsed_builtin.stmts.len} statements in builtin')
45+
builtin_transformed := transformer.transform(parsed_builtin)
46+
builder.build(builtin_transformed)
47+
}
48+
}
49+
2950
// Parse File
3051
input_file := 'test.v'
3152
if !os.exists(input_file) {
@@ -39,13 +60,9 @@ fn main() {
3960
}
4061
// Transform AST (lower complex constructs like ArrayInitExpr)
4162
println('[*] Transforming AST...')
42-
mut transformer := transform.Transformer.new()
4363
file := transformer.transform(parsed_file)
44-
// Initialize SSA Module
45-
mut mod := ssa.Module.new('main')
4664
// Build SSA from AST
4765
println('[*] Building SSA...')
48-
mut builder := ssa.Builder.new(mod)
4966
builder.build(file)
5067
// Optimize
5168
println('[*] Optimizing SSA...')

‎vlib/v2/ssa/builder.v‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct LoopInfo {
4545
}
4646

4747
pub fn Builder.new(mod &Module) &Builder {
48-
mut b := &Builder{
48+
return &Builder{
4949
mod: mod
5050
vars: map[string]ValueID{}
5151
var_struct_types: map[string]string{}
@@ -54,30 +54,6 @@ pub fn Builder.new(mod &Module) &Builder {
5454
enum_values: map[string]int{}
5555
var_array_sizes: map[string]int{}
5656
}
57-
// Register builtin string struct type: { str &u8, len int, is_lit int }
58-
b.register_string_type()
59-
return b
60-
}
61-
62-
fn (mut b Builder) register_string_type() {
63-
// Get string struct definition from v2.types (defined in universe.v)
64-
string_struct := types.get_string_struct()
65-
66-
// Convert types.Struct to SSA type
67-
mut ssa_fields := []TypeID{}
68-
mut ssa_field_names := []string{}
69-
70-
for field in string_struct.fields {
71-
ssa_fields << b.type_to_ssa(field.typ)
72-
ssa_field_names << field.name
73-
}
74-
75-
string_type_id := b.mod.type_store.register(Type{
76-
kind: .struct_t
77-
fields: ssa_fields
78-
field_names: ssa_field_names
79-
})
80-
b.struct_types['string'] = string_type_id
8157
}
8258

8359
// Convert v2.types.Type to SSA TypeID

‎vlib/v2/types/universe.v‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,8 @@ const f64_ = Primitive{
6363
size: 64
6464
}
6565
// complex / non primitives
66-
// String struct as defined in builtin/string.v:
66+
// String struct is defined in cmd/v2/builtin/string.v:
6767
// pub struct string { str &u8, len int, is_lit int }
68-
const string_struct_ = Struct{
69-
name: 'string'
70-
fields: [
71-
Field{
72-
name: 'str'
73-
typ: Pointer{
74-
base_type: u8_
75-
}
76-
},
77-
Field{
78-
name: 'len'
79-
typ: int_
80-
},
81-
Field{
82-
name: 'is_lit'
83-
typ: int_
84-
},
85-
]
86-
}
8768
const string_ = String(0)
8869
const chan_ = Channel{}
8970
const char_ = Char(0)
@@ -122,11 +103,6 @@ const float_literal_ = Primitive{
122103
// TODO: is this what thread should be?
123104
const thread_ = Thread{}
124105

125-
// Returns the string struct type definition (for SSA builder)
126-
pub fn get_string_struct() Struct {
127-
return string_struct_
128-
}
129-
130106
pub fn init_universe() &Scope {
131107
// universe scope
132108
mut universe_ := new_scope(unsafe { nil })

0 commit comments

Comments
 (0)