Skip to content

Commit 80c450f

Browse files
committed
CHB: convert type resolution and type size to result types
1 parent fbc8b93 commit 80c450f

20 files changed

+1018
-606
lines changed

CodeHawk/CHB/bchlib/bCHARMFunctionInterface.ml

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
(* chutil *)
2929
open CHFormatStringParser
30+
open CHTraceResult
3031
open CHUtil
3132

3233
(* bchlib *)
@@ -497,34 +498,42 @@ let rec get_arm_struct_field_locations
497498
(parameter_location_t list * arm_argument_state_t) =
498499
let fieldstate = aa_state in
499500
let bftype = resolve_type (get_struct_field_type bfinfo) in
500-
let (bfsize, bfoffset) =
501-
match (get_struct_field_size bfinfo,
502-
get_struct_field_offset bfinfo) with
503-
| (Some s, Some o) -> (s, o)
504-
| _ ->
501+
match bftype with
502+
| Error e ->
503+
raise
504+
(BCH_failure
505+
(LBLOCK [
506+
STR "Problem with type resolution: ";
507+
STR (String.concat "; " e)]))
508+
| Ok bftype ->
509+
let (bfsize, bfoffset) =
510+
match (get_struct_field_size bfinfo,
511+
get_struct_field_offset bfinfo) with
512+
| (Some s, Some o) -> (s, o)
513+
| _ ->
514+
raise
515+
(BCH_failure
516+
(LBLOCK [
517+
STR "get_arm_struct_field_locations: ";
518+
STR "no layout provided: ";
519+
fieldinfo_to_pretty bfinfo])) in
520+
if (is_int bftype || is_pointer bftype) && bfsize = 4 then
521+
let (loc, naas) =
522+
get_int_paramloc_next_state bfsize bftype fieldstate in
523+
([loc], naas)
524+
else if is_int bftype && bfsize < 4 then
525+
let (loc, naas) =
526+
get_int_paramlocpart_next_state bfsize bftype bfoffset fieldstate in
527+
([loc], naas)
528+
else if is_array_type bftype then
529+
get_arm_array_locations bfsize bftype bfoffset fieldstate
530+
else
505531
raise
506532
(BCH_failure
507533
(LBLOCK [
508534
STR "get_arm_struct_field_locations: ";
509-
STR "no layout provided: ";
510-
fieldinfo_to_pretty bfinfo])) in
511-
if (is_int bftype || is_pointer bftype) && bfsize = 4 then
512-
let (loc, naas) =
513-
get_int_paramloc_next_state bfsize bftype fieldstate in
514-
([loc], naas)
515-
else if is_int bftype && bfsize < 4 then
516-
let (loc, naas) =
517-
get_int_paramlocpart_next_state bfsize bftype bfoffset fieldstate in
518-
([loc], naas)
519-
else if is_array_type bftype then
520-
get_arm_array_locations bfsize bftype bfoffset fieldstate
521-
else
522-
raise
523-
(BCH_failure
524-
(LBLOCK [
525-
STR "get_arm_struct_field_locations: ";
526-
STR "not yet implemented: ";
527-
btype_to_pretty bftype]))
535+
STR "not yet implemented: ";
536+
btype_to_pretty bftype]))
528537

529538

530539
and get_arm_array_locations
@@ -572,27 +581,36 @@ let arm_vfp_params (funargs: bfunarg_t list): fts_parameter_t list =
572581
let (_, _, params) =
573582
List.fold_left
574583
(fun (index, aa_state, params) (name, btype, _) ->
575-
let btype = resolve_type btype in
576-
let tysize = size_of_btype btype in
577-
(* assume no packing at the argument top level *)
578-
let size = if tysize < 4 then 4 else tysize in
579-
let (param, new_state) =
580-
if (is_int btype || is_pointer btype || is_enum btype) && size = 4 then
581-
get_arm_int_param_next_state size name btype aa_state index
582-
else if (is_int btype || is_pointer btype) then
583-
get_long_int_param_next_state size name btype aa_state index
584-
else if is_float btype then
585-
get_float_param_next_state size name btype aa_state index
586-
else if (is_struct_type btype )
587-
&& (get_struct_type_compinfo btype).bcstruct then
588-
get_arm_struct_param_next_state size name btype aa_state index
589-
else
590-
raise
591-
(BCH_failure
592-
(LBLOCK [
593-
STR "vfp_params: Not yet implemented; ";
594-
btype_to_pretty btype])) in
595-
(index + 1, new_state, param :: params))
584+
let btype_r = resolve_type btype in
585+
let tysize_r = tbind size_of_btype btype_r in
586+
match btype_r, tysize_r with
587+
| Error e, _
588+
| _, Error e ->
589+
raise
590+
(BCH_failure
591+
(LBLOCK [
592+
STR "Problem with type resolution: ";
593+
STR (String.concat "; " e)]))
594+
| Ok btype, Ok tysize ->
595+
(* assume no packing at the argument top level *)
596+
let size = if tysize < 4 then 4 else tysize in
597+
let (param, new_state) =
598+
if (is_int btype || is_pointer btype || is_enum btype) && size = 4 then
599+
get_arm_int_param_next_state size name btype aa_state index
600+
else if (is_int btype || is_pointer btype) then
601+
get_long_int_param_next_state size name btype aa_state index
602+
else if is_float btype then
603+
get_float_param_next_state size name btype aa_state index
604+
else if (is_struct_type btype )
605+
&& (get_struct_type_compinfo btype).bcstruct then
606+
get_arm_struct_param_next_state size name btype aa_state index
607+
else
608+
raise
609+
(BCH_failure
610+
(LBLOCK [
611+
STR "vfp_params: Not yet implemented; ";
612+
btype_to_pretty btype])) in
613+
(index + 1, new_state, param :: params))
596614
(1, aas_start_state, []) funargs in
597615
params
598616

@@ -684,17 +702,22 @@ let get_arm_format_spec_parameters
684702
promote_int ftype
685703
else
686704
ftype in
687-
let size = size_of_btype ftype in
705+
let size_r = size_of_btype ftype in
688706
let name = "vararg_" ^ (string_of_int varargindex) in
689707
let (param, new_state) =
690-
match size with
691-
| 4 -> get_arm_int_param_next_state size name ftype aas nxtindex
692-
| 8 -> get_long_int_param_next_state size name ftype aas varargindex
693-
| _ ->
708+
match size_r with
709+
| Ok 4 -> get_arm_int_param_next_state 4 name ftype aas nxtindex
710+
| Ok 8 -> get_long_int_param_next_state 8 name ftype aas varargindex
711+
| Ok size ->
712+
raise
713+
(BCH_failure
714+
(LBLOCK [
715+
STR "Var-arg size: "; INT size; STR " not supported"]))
716+
| Error e ->
694717
raise
695718
(BCH_failure
696719
(LBLOCK [
697-
STR "Var-arg size: "; INT size; STR " not supported"])) in
720+
STR "Error in var-args: "; STR (String.concat "; " e)])) in
698721
(new_state, param :: accpars, varargindex + 1, nxtindex + 1))
699722
(fmtaas, [], 1, nextindex) argspecs in
700723
pars

CodeHawk/CHB/bchlib/bCHBCFiles.ml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ open CHPretty
3030

3131
(* chutil *)
3232
open CHLogger
33+
open CHTraceResult
3334
open CHXmlDocument
3435

3536
(* bchlib *)
@@ -204,8 +205,8 @@ object (self)
204205
(BCH_failure
205206
(LBLOCK [STR "No typedef found with name "; STR name]))
206207

207-
method resolve_type (ty: btype_t): btype_t =
208-
let rec aux (t: btype_t) =
208+
method resolve_type (ty: btype_t): btype_t traceresult =
209+
let rec aux (t: btype_t): btype_t traceresult =
209210
match t with
210211
| TVoid _
211212
| TInt _
@@ -218,25 +219,40 @@ object (self)
218219
| TClass _
219220
| TBuiltin_va_list _
220221
| TVarArg _
221-
| TUnknown _ -> t
222-
| TPtr (tt, a) -> TPtr (aux tt, a)
223-
| TRef (tt, a) -> TRef (aux tt, a)
224-
| TArray (tt, e, a) -> TArray (aux tt, e, a)
225-
| TFun (tt, fs, b, a) -> TFun (aux tt, auxfs fs, b, a)
222+
| TUnknown _ -> Ok t
223+
| TPtr (tt, a) -> tmap (fun v -> TPtr (v, a)) (aux tt)
224+
| TRef (tt, a) -> tmap (fun v -> TRef (v, a)) (aux tt)
225+
| TArray (tt, e, a) -> tmap (fun v -> TArray (v, e, a)) (aux tt)
226+
| TFun (tt, fs, b, a) ->
227+
let auxtt_r = aux tt in
228+
let auxfs_r = auxfs fs in
229+
(match auxtt_r, auxfs_r with
230+
| Ok v1, Ok v2 -> Ok (TFun (v1, v2, b, a))
231+
| Error e1, Ok _ -> Error e1
232+
| Ok _, Error e2 -> Error e2
233+
| Error e1, Error e2 -> Error (e1 @ e2))
226234
| TNamed (name, a) when self#has_typedef name ->
227235
aux (add_attributes (self#get_typedef name) a)
228236
| TNamed (name, _) ->
229-
begin
230-
ch_diagnostics_log#add
231-
"unknown typedef"
232-
(LBLOCK [STR "Named type "; STR name; STR " not defined"]);
233-
t
234-
end
235-
236-
and auxfs (fs: bfunarg_t list option): bfunarg_t list option =
237+
Error ["resolve_type: type name not found: " ^ name]
238+
239+
and auxfs (fs: bfunarg_t list option): bfunarg_t list option traceresult =
237240
match fs with
238-
| None -> None
239-
| Some l -> Some (List.map (fun (s, t, a) -> (s, aux t, a)) l)
241+
| None -> Ok None
242+
| Some l ->
243+
let fs_r = List.map (fun (s, t, a) -> (s, aux t, a)) l in
244+
let fs_r =
245+
List.fold_left (fun acc (s, t_r, a) ->
246+
match acc with
247+
| Error _ -> acc
248+
| Ok accv ->
249+
(match t_r with
250+
| Error e -> Error e
251+
| Ok v -> Ok ((s, v, a) :: accv))) (Ok []) fs_r in
252+
match fs_r with
253+
| Ok v -> Ok (Some v)
254+
| Error e -> Error e
255+
(* Some (List.map (fun (s, t, a) -> (s, aux t, a)) l) *)
240256

241257
in
242258
aux ty

0 commit comments

Comments
 (0)