@@ -85,52 +85,6 @@ def create_int_constant_ptr(value, builder, local_sym_tab, int_width=64):
8585 return ptr
8686
8787
88- def get_struct_char_array_ptr (expr , builder , local_sym_tab , struct_sym_tab ):
89- """Get pointer to first element of char array in struct field, or None."""
90- if not (isinstance (expr , ast .Attribute ) and isinstance (expr .value , ast .Name )):
91- return None
92-
93- var_name = expr .value .id
94- field_name = expr .attr
95-
96- # Check if it's a valid struct field
97- if not (
98- local_sym_tab
99- and var_name in local_sym_tab
100- and struct_sym_tab
101- and local_sym_tab [var_name ].metadata in struct_sym_tab
102- ):
103- return None
104-
105- struct_type = local_sym_tab [var_name ].metadata
106- struct_info = struct_sym_tab [struct_type ]
107-
108- if field_name not in struct_info .fields :
109- return None
110-
111- field_type = struct_info .field_type (field_name )
112-
113- # Check if it's a char array
114- is_char_array = (
115- isinstance (field_type , ir .ArrayType )
116- and isinstance (field_type .element , ir .IntType )
117- and field_type .element .width == 8
118- )
119-
120- if not is_char_array :
121- return None
122-
123- # Get field pointer and GEP to first element: [N x i8]* -> i8*
124- struct_ptr = local_sym_tab [var_name ].var
125- field_ptr = struct_info .gep (builder , struct_ptr , field_name )
126-
127- return builder .gep (
128- field_ptr ,
129- [ir .Constant (ir .IntType (32 ), 0 ), ir .Constant (ir .IntType (32 ), 0 )],
130- inbounds = True ,
131- )
132-
133-
13488def get_or_create_ptr_from_arg (
13589 func ,
13690 module ,
@@ -148,6 +102,7 @@ def get_or_create_ptr_from_arg(
148102 # Stack space is already allocated
149103 ptr = get_var_ptr_from_name (arg .id , local_sym_tab )
150104 elif isinstance (arg , ast .Constant ) and isinstance (arg .value , int ):
105+ int_width = 64 # Deafult to i64
151106 if expected_type and isinstance (expected_type , ir .IntType ):
152107 int_width = expected_type .width
153108 ptr = create_int_constant_ptr (arg .value , builder , local_sym_tab , int_width )
@@ -178,7 +133,9 @@ def get_or_create_ptr_from_arg(
178133 and isinstance (field_type .element , ir .IntType )
179134 and field_type .element .width == 8
180135 ):
181- ptr = get_struct_char_array_ptr (arg , builder , local_sym_tab , struct_sym_tab )
136+ ptr , sz = get_char_array_ptr_and_size (
137+ arg , builder , local_sym_tab , struct_sym_tab
138+ )
182139 if not ptr :
183140 raise ValueError ("Failed to get char array pointer from struct field" )
184141 else :
@@ -203,6 +160,10 @@ def get_or_create_ptr_from_arg(
203160 val = builder .trunc (val , expected_type )
204161 builder .store (val , ptr )
205162
163+ # NOTE: For char arrays, also return size
164+ if sz :
165+ return ptr , sz
166+
206167 return ptr
207168
208169
0 commit comments