@@ -30,6 +30,21 @@ type python_function_call = {
30
30
return_type : ir_type ;
31
31
}
32
32
33
+ (* * Convert AST types to C types *)
34
+ let ast_type_to_c_type = function
35
+ | Ast. U8 -> " uint8_t"
36
+ | Ast. U16 -> " uint16_t"
37
+ | Ast. U32 -> " uint32_t"
38
+ | Ast. U64 -> " uint64_t"
39
+ | Ast. I8 -> " int8_t"
40
+ | Ast. I16 -> " int16_t"
41
+ | Ast. I32 -> " int32_t"
42
+ | Ast. I64 -> " int64_t"
43
+ | Ast. Bool -> " bool"
44
+ | Ast. Char -> " char"
45
+ | Ast. Void -> " void"
46
+ | _ -> " int" (* fallback for complex types *)
47
+
33
48
(* * Convert IR types to C types *)
34
49
let rec c_type_from_ir_type = function
35
50
| IRU8 -> " uint8_t"
@@ -80,16 +95,8 @@ let generate_kernelscript_bridge_code resolved_imports =
80
95
let function_decls = List. map (fun symbol ->
81
96
match symbol.Import_resolver. symbol_type with
82
97
| Ast. Function (param_types , return_type ) ->
83
- let c_return_type = match return_type with
84
- | Ast. U8 -> " uint8_t" | Ast. U16 -> " uint16_t" | Ast. U32 -> " uint32_t" | Ast. U64 -> " uint64_t"
85
- | Ast. I8 -> " int8_t" | Ast. I16 -> " int16_t" | Ast. I32 -> " int32_t" | Ast. I64 -> " int64_t"
86
- | Ast. Bool -> " bool" | Ast. Char -> " char" | _ -> " int"
87
- in
88
- let c_param_types = List. map (function
89
- | Ast. U8 -> " uint8_t" | Ast. U16 -> " uint16_t" | Ast. U32 -> " uint32_t" | Ast. U64 -> " uint64_t"
90
- | Ast. I8 -> " int8_t" | Ast. I16 -> " int16_t" | Ast. I32 -> " int32_t" | Ast. I64 -> " int64_t"
91
- | Ast. Bool -> " bool" | Ast. Char -> " char" | _ -> " int"
92
- ) param_types in
98
+ let c_return_type = ast_type_to_c_type return_type in
99
+ let c_param_types = List. map ast_type_to_c_type param_types in
93
100
let params_str = if c_param_types = [] then " void" else String. concat " , " c_param_types in
94
101
sprintf " extern %s %s_%s(%s);" c_return_type module_name symbol.symbol_name params_str
95
102
| _ ->
@@ -186,16 +193,8 @@ let generate_mixed_bridge_code resolved_imports ir_programs =
186
193
let function_decls = List. map (fun symbol ->
187
194
match symbol.Import_resolver. symbol_type with
188
195
| Ast. Function (param_types , return_type ) ->
189
- let c_return_type = match return_type with
190
- | Ast. U8 -> " uint8_t" | Ast. U16 -> " uint16_t" | Ast. U32 -> " uint32_t" | Ast. U64 -> " uint64_t"
191
- | Ast. I8 -> " int8_t" | Ast. I16 -> " int16_t" | Ast. I32 -> " int32_t" | Ast. I64 -> " int64_t"
192
- | Ast. Bool -> " bool" | Ast. Char -> " char" | _ -> " int"
193
- in
194
- let c_param_types = List. map (function
195
- | Ast. U8 -> " uint8_t" | Ast. U16 -> " uint16_t" | Ast. U32 -> " uint32_t" | Ast. U64 -> " uint64_t"
196
- | Ast. I8 -> " int8_t" | Ast. I16 -> " int16_t" | Ast. I32 -> " int32_t" | Ast. I64 -> " int64_t"
197
- | Ast. Bool -> " bool" | Ast. Char -> " char" | _ -> " int"
198
- ) param_types in
196
+ let c_return_type = ast_type_to_c_type return_type in
197
+ let c_param_types = List. map ast_type_to_c_type param_types in
199
198
let params_str = if c_param_types = [] then " void" else String. concat " , " c_param_types in
200
199
sprintf " extern %s %s_%s(%s);" c_return_type module_name symbol.symbol_name params_str
201
200
| _ ->
@@ -682,38 +681,28 @@ type userspace_context = {
682
681
global_variables : ir_global_variable list ;
683
682
mutable inlinable_registers : (int , string ) Hashtbl .t ;
684
683
mutable current_function : ir_function option ;
685
- mutable temp_var_counter : int ;
686
684
(* Ring buffer event handler registrations *)
687
685
ring_buffer_handlers : (string , string ) Hashtbl .t ; (* map_name -> handler_function_name *)
688
686
}
689
687
690
- let create_userspace_context ?(global_variables = [] ) () = {
688
+ let create_context_base ?(global_variables = [] ) ~ function_name ~ is_main () = {
691
689
temp_counter = ref 0 ;
692
- function_name = " user_function " ;
693
- is_main = false ;
690
+ function_name;
691
+ is_main;
694
692
register_vars = Hashtbl. create 32 ;
695
693
var_declarations = Hashtbl. create 32 ;
696
694
function_usage = create_function_usage () ;
697
695
global_variables;
698
696
inlinable_registers = Hashtbl. create 32 ;
699
697
current_function = None ;
700
- temp_var_counter = 0 ;
701
698
ring_buffer_handlers = Hashtbl. create 16 ;
702
699
}
703
700
704
- let create_main_context ?(global_variables = [] ) () = {
705
- temp_counter = ref 0 ;
706
- function_name = " main" ;
707
- is_main = true ;
708
- register_vars = Hashtbl. create 32 ;
709
- var_declarations = Hashtbl. create 32 ;
710
- function_usage = create_function_usage () ;
711
- global_variables;
712
- inlinable_registers = Hashtbl. create 32 ;
713
- current_function = None ;
714
- temp_var_counter = 0 ;
715
- ring_buffer_handlers = Hashtbl. create 16 ;
716
- }
701
+ let create_userspace_context ?(global_variables = [] ) () =
702
+ create_context_base ~global_variables ~function_name: " user_function" ~is_main: false ()
703
+
704
+ let create_main_context ?(global_variables = [] ) () =
705
+ create_context_base ~global_variables ~function_name: " main" ~is_main: true ()
717
706
718
707
let fresh_temp_var ctx prefix =
719
708
incr ctx.temp_counter;
@@ -795,26 +784,7 @@ let rec track_usage_in_instructions ctx instrs =
795
784
| _ -> ()
796
785
) instrs
797
786
798
- (* * Collect string sizes from IR *)
799
- let rec collect_string_sizes_from_ir_type = function
800
- | IRStr size -> [size]
801
- | IRPointer (inner_type , _ ) -> collect_string_sizes_from_ir_type inner_type
802
- | IRArray (inner_type , _ , _ ) -> collect_string_sizes_from_ir_type inner_type
803
-
804
- | IRResult (ok_type , err_type ) ->
805
- (collect_string_sizes_from_ir_type ok_type) @ (collect_string_sizes_from_ir_type err_type)
806
- | _ -> []
807
-
808
- let collect_string_sizes_from_ir_value ir_value =
809
- let type_sizes = collect_string_sizes_from_ir_type ir_value.val_type in
810
- let literal_sizes = match ir_value.value_desc with
811
- | IRLiteral (StringLit _ ) ->
812
- (match ir_value.val_type with
813
- | IRStr size -> [size]
814
- | _ -> [] )
815
- | _ -> []
816
- in
817
- type_sizes @ literal_sizes
787
+ (* Removed unused string size collection functions *)
818
788
819
789
(* * Collect string sizes from IR - but only those used in concatenation operations *)
820
790
let rec collect_string_concat_sizes_from_ir_expr ir_expr =
@@ -1209,30 +1179,11 @@ let generate_type_alias_definitions_userspace_from_ast type_aliases =
1209
1179
let type_alias_defs = List. map (fun (alias_name , underlying_type ) ->
1210
1180
match underlying_type with
1211
1181
| Ast. Array (element_type , size ) ->
1212
- let element_c_type = match element_type with
1213
- | Ast. U8 -> " uint8_t"
1214
- | Ast. U16 -> " uint16_t"
1215
- | Ast. U32 -> " uint32_t"
1216
- | Ast. U64 -> " uint64_t"
1217
- | _ -> " uint8_t"
1218
- in
1182
+ let element_c_type = ast_type_to_c_type element_type in
1219
1183
(* Array typedef syntax: typedef element_type alias_name[size]; *)
1220
1184
sprintf " typedef %s %s[%d];" element_c_type alias_name size
1221
1185
| _ ->
1222
- let c_type = match underlying_type with
1223
- | Ast. U8 -> " uint8_t"
1224
- | Ast. U16 -> " uint16_t"
1225
- | Ast. U32 -> " uint32_t"
1226
- | Ast. U64 -> " uint64_t"
1227
- | Ast. I8 -> " int8_t"
1228
- | Ast. I16 -> " int16_t"
1229
- | Ast. I32 -> " int32_t"
1230
- | Ast. I64 -> " int64_t"
1231
- | Ast. Bool -> " bool"
1232
- | Ast. Char -> " char"
1233
- | Ast. Void -> " void"
1234
- | _ -> " uint32_t" (* fallback *)
1235
- in
1186
+ let c_type = ast_type_to_c_type underlying_type in
1236
1187
sprintf " typedef %s %s;" c_type alias_name
1237
1188
) type_aliases in
1238
1189
" /* Type alias definitions */\n " ^ (String. concat " \n " type_alias_defs) ^ " \n\n "
@@ -2259,19 +2210,7 @@ let generate_c_struct_from_ir ir_struct =
2259
2210
in
2260
2211
sprintf " struct %s {\n %s;\n };" ir_struct.struct_name fields_str
2261
2212
2262
- (* * Generate proper C declaration for any IR type with variable name *)
2263
- let generate_c_declaration ir_type var_name =
2264
- match ir_type with
2265
- | IRFunctionPointer (param_types , return_type ) ->
2266
- let return_type_str = c_type_from_ir_type return_type in
2267
- let param_types_str = List. map c_type_from_ir_type param_types in
2268
- let params_str = if param_types_str = [] then " void" else String. concat " , " param_types_str in
2269
- sprintf " %s (*%s)(%s)" return_type_str var_name params_str
2270
- | IRStr size -> sprintf " char %s[%d]" var_name size
2271
- | IRArray (element_type , size , _ ) ->
2272
- let element_type_str = c_type_from_ir_type element_type in
2273
- sprintf " %s %s[%d]" element_type_str var_name size
2274
- | _ -> sprintf " %s %s" (c_type_from_ir_type ir_type) var_name
2213
+
2275
2214
2276
2215
(* * Generate variable declarations for a function *)
2277
2216
let generate_variable_declarations ctx =
@@ -2833,26 +2772,15 @@ let generate_config_struct_from_decl (config_decl : Ast.config_declaration) =
2833
2772
let config_name = config_decl.config_name in
2834
2773
let struct_name = sprintf " %s_config" config_name in
2835
2774
2836
- (* Generate C struct for config - using same logic as eBPF but with standard C types *)
2775
+ (* Generate C struct for config - using reusable type conversion *)
2837
2776
let field_declarations = List. map (fun field ->
2838
- let field_declaration = match field.Ast. field_type with
2839
- | Ast. U8 -> sprintf " uint8_t %s;" field.Ast. field_name
2840
- | Ast. U16 -> sprintf " uint16_t %s;" field.Ast. field_name
2841
- | Ast. U32 -> sprintf " uint32_t %s;" field.Ast. field_name
2842
- | Ast. U64 -> sprintf " uint64_t %s;" field.Ast. field_name
2843
- | Ast. I8 -> sprintf " int8_t %s;" field.Ast. field_name
2844
- | Ast. I16 -> sprintf " int16_t %s;" field.Ast. field_name
2845
- | Ast. I32 -> sprintf " int32_t %s;" field.Ast. field_name
2846
- | Ast. I64 -> sprintf " int64_t %s;" field.Ast. field_name
2847
- | Ast. Bool -> sprintf " bool %s;" field.Ast. field_name
2848
- | Ast. Char -> sprintf " char %s;" field.Ast. field_name
2849
- | Ast. Array (Ast. U16, size ) -> sprintf " uint16_t %s[%d];" field.Ast. field_name size
2850
- | Ast. Array (Ast. U32, size ) -> sprintf " uint32_t %s[%d];" field.Ast. field_name size
2851
- | Ast. Array (Ast. U64, size ) -> sprintf " uint64_t %s[%d];" field.Ast. field_name size
2852
- | Ast. Array (Ast. U8, size ) -> sprintf " uint8_t %s[%d];" field.Ast. field_name size
2853
- | _ -> sprintf " uint32_t %s;" field.Ast. field_name (* fallback *)
2854
- in
2855
- field_declaration
2777
+ match field.Ast. field_type with
2778
+ | Ast. Array (element_type , size ) ->
2779
+ (* For arrays, the syntax is: element_type field_name[size]; *)
2780
+ sprintf " %s %s[%d];" (ast_type_to_c_type element_type) field.Ast. field_name size
2781
+ | other_type ->
2782
+ (* For non-arrays, the syntax is: type field_name; *)
2783
+ sprintf " %s %s;" (ast_type_to_c_type other_type) field.Ast. field_name
2856
2784
) config_decl.Ast. config_fields in
2857
2785
2858
2786
sprintf " struct %s {\n %s\n };" struct_name (String. concat " \n " field_declarations)
@@ -4085,23 +4013,9 @@ int main(void) {
4085
4013
)
4086
4014
| None -> ())
4087
4015
4088
- (* * Compatibility functions for tests *)
4089
- let generate_c_statement _stmt = "/* IR-based statement generation */"
4090
4016
4091
4017
(* * Check if a variable name is an impl block instance *)
4092
4018
let is_impl_block_variable ir_multi_prog var_name =
4093
4019
List.exists (fun struct_ops_decl ->
4094
4020
struct_ops_decl.ir_instance_name = var_name
4095
4021
) ir_multi_prog.struct_ops_instances
4096
-
4097
-
4098
-
4099
-
4100
-
4101
-
4102
-
4103
-
4104
-
4105
-
4106
-
4107
-
0 commit comments