From 7b4f34a5216003c50266c9a448ff0257a9ff3e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9onard=20G=C3=A9rard?= Date: Thu, 4 Nov 2010 18:06:11 +0100 Subject: [PATCH] Cgen only stdbool.h booleans no more TRUE and FALSE. --- compiler/obc/c/c.ml | 5 ++--- compiler/obc/c/cgen.ml | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/compiler/obc/c/c.ml b/compiler/obc/c/c.ml index bb5eea8..7805387 100644 --- a/compiler/obc/c/c.ml +++ b/compiler/obc/c/c.ml @@ -240,8 +240,8 @@ and pp_cexpr fmt ce = match ce with fprintf fmt "%a(@[%a@])" pp_string s (pp_list1 pp_cexpr ",") el | Cconst (Ccint i) -> fprintf fmt "%d" i | Cconst (Ccfloat f) -> fprintf fmt "%f" f - | Cconst (Ctag "true") -> fprintf fmt "TRUE" - | Cconst (Ctag "false") -> fprintf fmt "FALSE" + | Cconst (Ctag "true") -> fprintf fmt "true" + | Cconst (Ctag "false") -> fprintf fmt "false" | Cconst (Ctag t) -> pp_string fmt t | Cconst (Cstrlit t) -> fprintf fmt "\"%s\"" t | Clhs lhs -> fprintf fmt "%a" pp_clhs lhs @@ -305,7 +305,6 @@ let pp_cfile_desc fmt filen cfile = fprintf fmt "#include @\n"; fprintf fmt "#include @\n"; fprintf fmt "#include \"%s\"@\n@\n" headern; - fprintf fmt "#define FALSE 0@\n#define TRUE 1@\n@\n"; iter (pp_cdef fmt) cdefs (******************************) diff --git a/compiler/obc/c/cgen.ml b/compiler/obc/c/cgen.ml index 3f98fa5..cf51ea1 100644 --- a/compiler/obc/c/cgen.ml +++ b/compiler/obc/c/cgen.ml @@ -239,7 +239,7 @@ let rec cexpr_of_static_exp se = match se.se_desc with | Sint i -> Cconst (Ccint i) | Sfloat f -> Cconst (Ccfloat f) - | Sbool b -> Cconst (Ctag (if b then "TRUE" else "FALSE")) + | Sbool b -> Cconst (Ctag (if b then "true" else "false")) | Sfield _ -> assert false | Sconstructor c -> Cconst (Ctag (cname_of_qn c)) | Sarray sl -> Carraylit (List.map cexpr_of_static_exp sl) @@ -425,13 +425,24 @@ let rec create_affect_const var_env dest c = class names. *) let rec cstm_of_act var_env obj_env act = match act with - (** Case on boolean values are converted to if instead of switch! *) + (** Cosmetic : cases on boolean values are converted to if statements. *) | Acase (c, [({name = "true"}, te); ({ name = "false" }, fe)]) | Acase (c, [({name = "false"}, fe); ({ name = "true"}, te)]) -> let cc = cexpr_of_exp var_env c in let cte = cstm_of_act_list var_env obj_env te in let cfe = cstm_of_act_list var_env obj_env fe in [Cif (cc, cte, cfe)] + | Acase (c, [({name = "true"}, te)]) -> + let cc = cexpr_of_exp var_env c in + let cte = cstm_of_act_list var_env obj_env te in + let cfe = [] in + [Cif (cc, cte, cfe)] + | Acase (c, [({name = "false"}, fe)]) -> + let cc = Cuop ("!", (cexpr_of_exp var_env c)) in + let cte = cstm_of_act_list var_env obj_env fe in + let cfe = [] in + [Cif (cc, cte, cfe)] + (** Translation of case into a C switch statement is simple enough: we just recursively translate obj expressions and statements to