From 84c3580d0514c24a7c29eeec635e16183c3c5c65 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Fri, 21 Aug 2015 15:35:13 +0200 Subject: Simplify the handling of extended inline asm, taking advantage of the new, structured builtin arguments and results. --- backend/PrintAsmaux.ml | 29 ++++++++++++++++++++++++----- cparser/ExtendedAsm.ml | 11 +++++------ ia32/TargetPrinter.ml | 4 +--- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/backend/PrintAsmaux.ml b/backend/PrintAsmaux.ml index 63fb6bb2..b842f86d 100644 --- a/backend/PrintAsmaux.ml +++ b/backend/PrintAsmaux.ml @@ -271,23 +271,42 @@ let print_annot_val print_preg oc txt args = (** Inline assembly *) -let re_asm_param = Str.regexp "%%\\|%[0-9]+" +let print_asm_argument print_preg oc modifier = function + | BA r -> print_preg oc r + | BA_longofwords(BA hi, BA lo) -> + begin match modifier with + | "R" -> print_preg oc hi + | "Q" -> print_preg oc lo + | _ -> fprintf oc "%a:%a" print_preg hi print_preg lo + (* Probably not what was intended *) + end + | _ -> failwith "bad asm argument" + +let builtin_arg_of_res = function + | BR r -> BA r + | BR_longofwords(BR hi, BR lo) -> BA_longofwords(BA hi, BA lo) + | _ -> assert false + +let re_asm_param_1 = Str.regexp "%%\\|%[QR]?[0-9]+" +let re_asm_param_2 = Str.regexp "%\\([QR]?\\)\\([0-9]+\\)" let print_inline_asm print_preg oc txt sg args res = let operands = - if sg.sig_res = None then args else res @ args in + if sg.sig_res = None then args else builtin_arg_of_res res :: args in let print_fragment = function | Str.Text s -> output_string oc s | Str.Delim "%%" -> output_char oc '%' | Str.Delim s -> - let n = int_of_string (String.sub s 1 (String.length s - 1)) in + assert (Str.string_match re_asm_param_2 s 0); + let modifier = Str.matched_group 1 s + and number = int_of_string (Str.matched_group 2 s) in try - print_preg oc (List.nth operands n) + print_asm_argument print_preg oc modifier (List.nth operands number) with Failure _ -> fprintf oc "" s in - List.iter print_fragment (Str.full_split re_asm_param txt); + List.iter print_fragment (Str.full_split re_asm_param_1 txt); fprintf oc "\n" diff --git a/cparser/ExtendedAsm.ml b/cparser/ExtendedAsm.ml index fbf8d569..05084561 100644 --- a/cparser/ExtendedAsm.ml +++ b/cparser/ExtendedAsm.ml @@ -57,10 +57,9 @@ let set_label_reg lbl pos pos' subst = have this feature and with which syntax. *) let set_label_regpair lbl pos pos' subst = - StringMap.add (name_of_label ~modifier:"R" lbl pos) (sprintf "%%%d" pos') - (StringMap.add (name_of_label ~modifier:"Q" lbl pos) - (sprintf "%%%d" (pos' + 1)) - subst) + StringMap.add (name_of_label ~modifier:"R" lbl pos) (sprintf "%%R%d" pos') + (StringMap.add (name_of_label ~modifier:"Q" lbl pos) (sprintf "%%Q%d" pos') + subst) let set_label_mem lbl pos pos' subst = StringMap.add (name_of_label lbl pos) @@ -91,7 +90,7 @@ let rec transf_inputs loc env accu pos pos' subst = function let valid = Str.string_match re_valid_input cstr 0 in if valid && String.contains cstr 'r' then if is_reg_pair env e.etyp then - transf_inputs loc env (e :: accu) (pos + 1) (pos' + 2) + transf_inputs loc env (e :: accu) (pos + 1) (pos' + 1) (set_label_regpair lbl pos pos' subst) inputs else transf_inputs loc env (e :: accu) (pos + 1) (pos' + 1) @@ -133,7 +132,7 @@ let transf_outputs loc env = function let valid = Str.string_match re_valid_output cstr 0 in if valid && String.contains cstr 'r' then if is_reg_pair env e.etyp then - (Some e, [], set_label_regpair lbl 0 0 StringMap.empty, 1, 2) + (Some e, [], set_label_regpair lbl 0 0 StringMap.empty, 1, 1) else (Some e, [], set_label_reg lbl 0 0 StringMap.empty, 1, 1) else diff --git a/ia32/TargetPrinter.ml b/ia32/TargetPrinter.ml index 6e931e13..beddd1e8 100644 --- a/ia32/TargetPrinter.ml +++ b/ia32/TargetPrinter.ml @@ -661,9 +661,7 @@ module Target(System: SYSTEM):TARGET = print_annot_stmt oc (extern_atom txt) targs args | EF_inline_asm(txt, sg, clob) -> fprintf oc "%s begin inline assembly\n\t" comment; - print_inline_asm preg oc (extern_atom txt) sg - (params_of_builtin_args args) - (params_of_builtin_res res); + print_inline_asm preg oc (extern_atom txt) sg args res; fprintf oc "%s end inline assembly\n" comment | _ -> assert false -- cgit