Skip to content

Commit

Permalink
[CN-Exec] Add convert_from_cn_map (#581)
Browse files Browse the repository at this point in the history
And special-case logic in `cn_internal_to_ail.ml`
  • Loading branch information
ZippeyKeys12 committed Sep 24, 2024
1 parent c665b48 commit 2c4e255
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
55 changes: 34 additions & 21 deletions backend/cn/lib/cn_internal_to_ail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -377,34 +377,27 @@ let get_underscored_typedef_string_from_bt ?(is_record = false) bt =
| _ -> None)


let get_conversion_to_fn_str (bt : BT.t) : string option =
let get_type_underscored_str (bt : BT.t) : string option =
let typedef_name = get_typedef_string (bt_to_ail_ctype bt) in
match typedef_name with
| Some str ->
let str = String.concat "_" (String.split_on_char ' ' str) in
Some ("convert_to_" ^ str)
| Some str -> Some (String.concat "_" (String.split_on_char ' ' str))
| None ->
(match bt with
| BT.Struct sym ->
let cn_sym = generate_sym_with_suffix ~suffix:"_cn" sym in
let conversion_fn_str = "convert_to_struct_" ^ Sym.pp_string cn_sym in
Some conversion_fn_str
Some ("struct_" ^ Sym.pp_string (generate_sym_with_suffix ~suffix:"_cn" sym))
| _ -> None)


let get_conversion_to_fn_str (bt : BT.t) : string option =
let open Option in
let@ ty_str = get_type_underscored_str bt in
return ("convert_to_" ^ ty_str)


let get_conversion_from_fn_str (bt : BT.t) : string option =
let typedef_name = get_typedef_string (bt_to_ail_ctype bt) in
match typedef_name with
| Some str ->
let str = String.concat "_" (String.split_on_char ' ' str) in
Some ("convert_from_" ^ str)
| None ->
(match bt with
| BT.Struct sym ->
let cn_sym = generate_sym_with_suffix ~suffix:"_cn" sym in
let conversion_fn_str = "convert_from_struct_" ^ Sym.pp_string cn_sym in
Some conversion_fn_str
| _ -> None)
let open Option in
let@ ty_str = get_type_underscored_str bt in
return ("convert_from_" ^ ty_str)


let wrap_with_convert_to ?sct ail_expr_ bt =
Expand Down Expand Up @@ -2310,9 +2303,29 @@ let generate_struct_conversion_from_function
let sct_opt = Sctypes.of_ctype ctype in
let sct = match sct_opt with Some t -> t | None -> failwith "Bad sctype" in
let bt = BT.of_sct Memory.is_signed_integer_type Memory.size_of_integer_type sct in
let rhs = wrap_with_convert_from ~sct rhs bt in
let lhs = A.(AilEmemberof (mk_expr (AilEident res_sym), id)) in
A.(AilSexpr (mk_expr (AilEassign (mk_expr lhs, mk_expr rhs))))
match (bt, sct) with
| BT.Map (_k_bt, v_bt), Sctypes.Array (v_sct, Some sz) ->
A.(
AilSexpr
(mk_expr
(AilEcall
( mk_expr
(AilEident
(Sym.fresh_named (Option.get (get_conversion_from_fn_str bt)))),
List.map
mk_expr
[ lhs;
rhs;
AilEident
(Sym.fresh_named (Option.get (get_type_underscored_str v_bt)));
AilEconst
(ConstantInteger (IConstant (Z.of_int sz, Decimal, None)))
] ))))
| BT.Map _, _ -> failwith "unsupported map types"
| _ ->
let rhs = wrap_with_convert_from ~sct rhs bt in
A.(AilSexpr (mk_expr (AilEassign (mk_expr lhs, mk_expr rhs))))
in
let member_assignments = List.map generate_member_assignment members in
let return_stmt = A.(AilSreturn (mk_expr (AilEident res_sym))) in
Expand Down
4 changes: 4 additions & 0 deletions runtime/libcn/include/cn-executable/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ cn_bool *cn_map_equality(cn_map *m1, cn_map *m2, cn_bool *(value_equality_fun)(v
}\
m;\
})
#define convert_from_cn_map(arr, m, cntype, num_elements)\
for (int i = 0; i < num_elements; i++) {\
arr[i] = convert_from_##cntype(cn_map_get_##cntype(m, convert_to_cn_integer(i)));\
}


cn_bool *cn_pointer_equality(void *i1, void *i2);
Expand Down

0 comments on commit 2c4e255

Please sign in to comment.