@@ -21,10 +21,24 @@ def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder
2121 return None
2222
2323
24- def _handle_constant_expr (expr : ast .Constant ):
24+ def _handle_constant_expr (module , builder , expr : ast .Constant ):
2525 """Handle ast.Constant expressions."""
2626 if isinstance (expr .value , int ) or isinstance (expr .value , bool ):
2727 return ir .Constant (ir .IntType (64 ), int (expr .value )), ir .IntType (64 )
28+ elif isinstance (expr .value , str ):
29+ str_name = f".str.{ id (expr )} "
30+ str_bytes = expr .value .encode ("utf-8" ) + b"\x00 "
31+ str_type = ir .ArrayType (ir .IntType (8 ), len (str_bytes ))
32+ str_constant = ir .Constant (str_type , bytearray (str_bytes ))
33+
34+ # Create global variable
35+ global_str = ir .GlobalVariable (module , str_type , name = str_name )
36+ global_str .linkage = "internal"
37+ global_str .global_constant = True
38+ global_str .initializer = str_constant
39+
40+ str_ptr = builder .bitcast (global_str , ir .PointerType (ir .IntType (8 )))
41+ return str_ptr , ir .PointerType (ir .IntType (8 ))
2842 else :
2943 logger .error (f"Unsupported constant type { ast .dump (expr )} " )
3044 return None
@@ -343,7 +357,7 @@ def eval_expr(
343357 if isinstance (expr , ast .Name ):
344358 return _handle_name_expr (expr , local_sym_tab , builder )
345359 elif isinstance (expr , ast .Constant ):
346- return _handle_constant_expr (expr )
360+ return _handle_constant_expr (module , builder , expr )
347361 elif isinstance (expr , ast .Call ):
348362 if isinstance (expr .func , ast .Name ) and expr .func .id == "deref" :
349363 return _handle_deref_call (expr , local_sym_tab , builder )
0 commit comments