From 71b249c0aba5f94cb26d7f72e4bcbd44554a679f Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sun, 16 Jun 2024 17:19:17 +0200 Subject: [PATCH] PrintCminor: don't quote all local variable names, only those that need quoting This greatly improves legibility of the debug .cm files. --- backend/PrintCminor.ml | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/backend/PrintCminor.ml b/backend/PrintCminor.ml index 59b340f745..173a6e62a1 100644 --- a/backend/PrintCminor.ml +++ b/backend/PrintCminor.ml @@ -40,9 +40,41 @@ let precedence = function | Ebinop((Oor|Oorl), _, _) -> (6, LtoR) | Eload _ -> (15, RtoL) +(* Reserved keywords *) + +module StringSet = Set.Make(String) + +let keywords = StringSet.of_list [ + "absf"; "abss"; "any32"; "any64"; + "builtin"; + "case"; + "default"; + "else"; "exit"; "extern"; + "float"; "float32"; "float64"; "floatofint"; "floatofintu"; + "floatoflong"; "floatoflongu"; + "goto"; + "if"; "int"; "int16"; "int16s"; "int16u"; "int32"; "int64"; + "int8"; "int8s"; "int8u"; "intoffloat"; "intoflong"; + "intofsingle"; "intuoffloat"; "intuofsingle"; + "long"; "longoffloat"; "longofint"; "longofintu"; "longuoffloat"; "loop"; + "match"; + "readonly"; "return"; + "single"; "singleofint"; "singleofintu"; "singleoflong"; "singleoflongu"; + "stack"; "switch"; "switchl"; + "tailcall"; + "var"; "void"; "volatile"; + "while" +] + (* Naming idents. *) -let ident_name id = "'" ^ Camlcoq.extern_atom id ^ "'" +let re_ident = Str.regexp {|[A-Za-z_][A-Za-z_$0-9]*$\|\$[1-9][0-9]*$|} + +let ident_name id = + let s = Camlcoq.extern_atom id in + if Str.string_match re_ident s 0 && not (StringSet.mem s keywords) + then s + else "'" ^ s ^ "'" (* Naming operators *)