@@ -136,3 +136,57 @@ def get_data_ptr_and_size(data_arg, local_sym_tab, struct_sym_tab):
136136 raise NotImplementedError (
137137 "Only simple object names are supported as data in perf event output."
138138 )
139+
140+
141+ def get_buffer_ptr_and_size (buf_arg , builder , local_sym_tab , struct_sym_tab ):
142+ """Extract buffer pointer and size from either a struct field or variable."""
143+
144+ # Case 1: Struct field (obj.field)
145+ if isinstance (buf_arg , ast .Attribute ):
146+ if not isinstance (buf_arg .value , ast .Name ):
147+ raise ValueError (
148+ "Only simple struct field access supported (e.g., obj.field)"
149+ )
150+
151+ struct_name = buf_arg .value .id
152+ field_name = buf_arg .attr
153+
154+ # Lookup struct
155+ if not local_sym_tab or struct_name not in local_sym_tab :
156+ raise ValueError (f"Struct '{ struct_name } ' not found" )
157+
158+ struct_type = local_sym_tab [struct_name ].metadata
159+ if not struct_sym_tab or struct_type not in struct_sym_tab :
160+ raise ValueError (f"Struct type '{ struct_type } ' not found" )
161+
162+ struct_info = struct_sym_tab [struct_type ]
163+
164+ # Get field pointer and type
165+ struct_ptr = local_sym_tab [struct_name ].var
166+ field_ptr = struct_info .gep (builder , struct_ptr , field_name )
167+ field_type = struct_info .field_type (field_name )
168+
169+ if not isinstance (field_type , ir .ArrayType ):
170+ raise ValueError (f"Field '{ field_name } ' must be an array type" )
171+
172+ return field_ptr , field_type .count
173+
174+ # Case 2: Variable name
175+ elif isinstance (buf_arg , ast .Name ):
176+ var_name = buf_arg .id
177+
178+ if not local_sym_tab or var_name not in local_sym_tab :
179+ raise ValueError (f"Variable '{ var_name } ' not found" )
180+
181+ var_ptr = local_sym_tab [var_name ].var
182+ var_type = local_sym_tab [var_name ].ir_type
183+
184+ if not isinstance (var_type , ir .ArrayType ):
185+ raise ValueError (f"Variable '{ var_name } ' must be an array type" )
186+
187+ return var_ptr , var_type .count
188+
189+ else :
190+ raise ValueError (
191+ "comm expects either a struct field (obj.field) or variable name"
192+ )
0 commit comments