Skip to content

Commit e46aa11

Browse files
committed
Clean up userspace code generation code.
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 769db3a commit e46aa11

File tree

1 file changed

+39
-125
lines changed

1 file changed

+39
-125
lines changed

src/userspace_codegen.ml

Lines changed: 39 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ type python_function_call = {
3030
return_type: ir_type;
3131
}
3232

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+
3348
(** Convert IR types to C types *)
3449
let rec c_type_from_ir_type = function
3550
| IRU8 -> "uint8_t"
@@ -80,16 +95,8 @@ let generate_kernelscript_bridge_code resolved_imports =
8095
let function_decls = List.map (fun symbol ->
8196
match symbol.Import_resolver.symbol_type with
8297
| 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
93100
let params_str = if c_param_types = [] then "void" else String.concat ", " c_param_types in
94101
sprintf "extern %s %s_%s(%s);" c_return_type module_name symbol.symbol_name params_str
95102
| _ ->
@@ -186,16 +193,8 @@ let generate_mixed_bridge_code resolved_imports ir_programs =
186193
let function_decls = List.map (fun symbol ->
187194
match symbol.Import_resolver.symbol_type with
188195
| 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
199198
let params_str = if c_param_types = [] then "void" else String.concat ", " c_param_types in
200199
sprintf "extern %s %s_%s(%s);" c_return_type module_name symbol.symbol_name params_str
201200
| _ ->
@@ -682,38 +681,28 @@ type userspace_context = {
682681
global_variables: ir_global_variable list;
683682
mutable inlinable_registers: (int, string) Hashtbl.t;
684683
mutable current_function: ir_function option;
685-
mutable temp_var_counter: int;
686684
(* Ring buffer event handler registrations *)
687685
ring_buffer_handlers: (string, string) Hashtbl.t; (* map_name -> handler_function_name *)
688686
}
689687

690-
let create_userspace_context ?(global_variables = []) () = {
688+
let create_context_base ?(global_variables = []) ~function_name ~is_main () = {
691689
temp_counter = ref 0;
692-
function_name = "user_function";
693-
is_main = false;
690+
function_name;
691+
is_main;
694692
register_vars = Hashtbl.create 32;
695693
var_declarations = Hashtbl.create 32;
696694
function_usage = create_function_usage ();
697695
global_variables;
698696
inlinable_registers = Hashtbl.create 32;
699697
current_function = None;
700-
temp_var_counter = 0;
701698
ring_buffer_handlers = Hashtbl.create 16;
702699
}
703700

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 ()
717706

718707
let fresh_temp_var ctx prefix =
719708
incr ctx.temp_counter;
@@ -795,26 +784,7 @@ let rec track_usage_in_instructions ctx instrs =
795784
| _ -> ()
796785
) instrs
797786

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 *)
818788

819789
(** Collect string sizes from IR - but only those used in concatenation operations *)
820790
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 =
12091179
let type_alias_defs = List.map (fun (alias_name, underlying_type) ->
12101180
match underlying_type with
12111181
| 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
12191183
(* Array typedef syntax: typedef element_type alias_name[size]; *)
12201184
sprintf "typedef %s %s[%d];" element_c_type alias_name size
12211185
| _ ->
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
12361187
sprintf "typedef %s %s;" c_type alias_name
12371188
) type_aliases in
12381189
"/* Type alias definitions */\n" ^ (String.concat "\n" type_alias_defs) ^ "\n\n"
@@ -2259,19 +2210,7 @@ let generate_c_struct_from_ir ir_struct =
22592210
in
22602211
sprintf "struct %s {\n %s;\n};" ir_struct.struct_name fields_str
22612212

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+
22752214

22762215
(** Generate variable declarations for a function *)
22772216
let generate_variable_declarations ctx =
@@ -2833,26 +2772,15 @@ let generate_config_struct_from_decl (config_decl : Ast.config_declaration) =
28332772
let config_name = config_decl.config_name in
28342773
let struct_name = sprintf "%s_config" config_name in
28352774

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 *)
28372776
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
28562784
) config_decl.Ast.config_fields in
28572785

28582786
sprintf "struct %s {\n%s\n};" struct_name (String.concat "\n" field_declarations)
@@ -4085,23 +4013,9 @@ int main(void) {
40854013
)
40864014
| None -> ())
40874015
4088-
(** Compatibility functions for tests *)
4089-
let generate_c_statement _stmt = "/* IR-based statement generation */"
40904016
40914017
(** Check if a variable name is an impl block instance *)
40924018
let is_impl_block_variable ir_multi_prog var_name =
40934019
List.exists (fun struct_ops_decl ->
40944020
struct_ops_decl.ir_instance_name = var_name
40954021
) ir_multi_prog.struct_ops_instances
4096-
4097-
4098-
4099-
4100-
4101-
4102-
4103-
4104-
4105-
4106-
4107-

0 commit comments

Comments
 (0)