@@ -1895,7 +1895,11 @@ let rec generate_c_instruction_from_ir ctx instruction =
18951895 (match init_expr_opt with
18961896 | Some init_expr ->
18971897 let init_str = generate_c_expression_from_ir ctx init_expr in
1898- sprintf " %s = %s;" array_decl init_str
1898+ (match init_expr.expr_desc with
1899+ | IRValue { value_desc = IRLiteral (ArrayLit _ ); _ } ->
1900+ sprintf " %s = %s;" array_decl init_str
1901+ | _ ->
1902+ sprintf " %s;\n memcpy(%s, %s, sizeof(%s));" array_decl c_var_name init_str c_var_name)
18991903 | None ->
19001904 sprintf " %s;" array_decl)
19011905 | _ ->
@@ -2394,8 +2398,33 @@ let collect_undeclared_variables_in_function ir_func =
23942398 | _ -> ()
23952399 in
23962400
2401+ let rec collect_declared_from_instr ir_instr =
2402+ collect_declared_vars ir_instr;
2403+ match ir_instr.instr_desc with
2404+ | IRIf (_ , then_instrs , else_instrs_opt ) ->
2405+ List. iter collect_declared_from_instr then_instrs;
2406+ (match else_instrs_opt with
2407+ | Some else_instrs -> List. iter collect_declared_from_instr else_instrs
2408+ | None -> () )
2409+ | IRIfElseChain (conditions_and_bodies , final_else ) ->
2410+ List. iter (fun (_ , instrs ) ->
2411+ List. iter collect_declared_from_instr instrs
2412+ ) conditions_and_bodies;
2413+ (match final_else with
2414+ | Some instrs -> List. iter collect_declared_from_instr instrs
2415+ | None -> () )
2416+ | IRBpfLoop (_ , _ , _ , _ , body_instructions ) ->
2417+ List. iter collect_declared_from_instr body_instructions
2418+ | IRTry (try_instrs , catch_clauses ) ->
2419+ List. iter collect_declared_from_instr try_instrs;
2420+ List. iter (fun clause ->
2421+ List. iter collect_declared_from_instr clause.catch_body
2422+ ) catch_clauses
2423+ | _ -> ()
2424+ in
2425+
23972426 let collect_declared_from_instrs instrs =
2398- List. iter collect_declared_vars instrs
2427+ List. iter collect_declared_from_instr instrs
23992428 in
24002429
24012430 List. iter (fun block ->
@@ -2614,6 +2643,10 @@ let generate_c_function_from_ir ?(global_variables = []) ?(base_name = "") ?(con
26142643 let rec collect_declared_vars ir_instr =
26152644 match ir_instr.instr_desc with
26162645 | IRVariableDecl (dest_val , _ , _ ) ->
2646+ (match dest_val.value_desc with
2647+ | IRVariable var_name | IRTempVariable var_name ->
2648+ Hashtbl. replace ctx.declared_via_ir var_name ()
2649+ | _ -> () );
26172650 (* Only user variables (IRVariable) need var_ prefix, not compiler temps (IRTempVariable) *)
26182651 (match dest_val.value_desc with
26192652 | IRVariable var_name ->
0 commit comments