@@ -77,36 +77,50 @@ def get_var_ptr_from_name(var_name, local_sym_tab):
7777def create_int_constant_ptr (value , builder , local_sym_tab , int_width = 64 ):
7878 """Create a pointer to an integer constant."""
7979
80- # Default to 64-bit integer
81- ptr , temp_name = _temp_pool_manager .get_next_temp (local_sym_tab )
80+ int_type = ir . IntType ( int_width )
81+ ptr , temp_name = _temp_pool_manager .get_next_temp (local_sym_tab , int_type )
8282 logger .info (f"Using temp variable '{ temp_name } ' for int constant { value } " )
83- const_val = ir .Constant (ir . IntType ( int_width ) , value )
83+ const_val = ir .Constant (int_type , value )
8484 builder .store (const_val , ptr )
8585 return ptr
8686
8787
8888def get_or_create_ptr_from_arg (
89- func , module , arg , builder , local_sym_tab , map_sym_tab , struct_sym_tab = None
89+ func ,
90+ module ,
91+ arg ,
92+ builder ,
93+ local_sym_tab ,
94+ map_sym_tab ,
95+ expected_type = None ,
96+ struct_sym_tab = None ,
9097):
9198 """Extract or create pointer from the call arguments."""
9299
93100 if isinstance (arg , ast .Name ):
101+ # Stack space is already allocated
94102 ptr = get_var_ptr_from_name (arg .id , local_sym_tab )
95103 elif isinstance (arg , ast .Constant ) and isinstance (arg .value , int ):
96- ptr = create_int_constant_ptr (arg .value , builder , local_sym_tab )
104+ if expected_type and isinstance (expected_type , ir .IntType ):
105+ int_width = expected_type .width
106+ ptr = create_int_constant_ptr (arg .value , builder , local_sym_tab , int_width )
97107 else :
108+ # NOTE: For any integer expression reaching this branch, it is probably a struct field or a binop
98109 # Evaluate the expression and store the result in a temp variable
99110 val = get_operand_value (
100111 func , module , arg , builder , local_sym_tab , map_sym_tab , struct_sym_tab
101112 )
102113 if val is None :
103114 raise ValueError ("Failed to evaluate expression for helper arg." )
104115
105- # NOTE: We assume the result is an int64 for now
106- # if isinstance(arg, ast.Attribute):
107- # return val
108- ptr , temp_name = _temp_pool_manager .get_next_temp (local_sym_tab )
116+ ptr , temp_name = _temp_pool_manager .get_next_temp (local_sym_tab , expected_type )
109117 logger .info (f"Using temp variable '{ temp_name } ' for expression result" )
118+ if (
119+ isinstance (val .type , ir .IntType )
120+ and expected_type
121+ and val .type .width > expected_type .width
122+ ):
123+ val = builder .trunc (val , expected_type )
110124 builder .store (val , ptr )
111125
112126 return ptr
0 commit comments