From 1cd385f3b354a78ae8d02333f40cd065073c9b19 Mon Sep 17 00:00:00 2001 From: xleroy Date: Sat, 21 Dec 2013 17:00:43 +0000 Subject: Support "default" cases in the middle of a "switch", not just at the end. git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@2383 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- cfrontend/Clight.v | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'cfrontend/Clight.v') diff --git a/cfrontend/Clight.v b/cfrontend/Clight.v index 6a57999..137decc 100644 --- a/cfrontend/Clight.v +++ b/cfrontend/Clight.v @@ -108,8 +108,9 @@ Inductive statement : Type := | Sgoto : label -> statement with labeled_statements : Type := (**r cases of a [switch] *) - | LSdefault: statement -> labeled_statements - | LScase: int -> statement -> labeled_statements -> labeled_statements. + | LSnil: labeled_statements + | LScons: option int -> statement -> labeled_statements -> labeled_statements. + (**r [None] is [default], [Some x] is [case x] *) (** The C loops are derived forms. *) @@ -301,19 +302,32 @@ Definition set_opttemp (optid: option ident) (v: val) (le: temp_env) := (** Selection of the appropriate case of a [switch], given the value [n] of the selector expression. *) -Fixpoint select_switch (n: int) (sl: labeled_statements) - {struct sl}: labeled_statements := +Fixpoint select_switch_default (sl: labeled_statements): labeled_statements := match sl with - | LSdefault _ => sl - | LScase c s sl' => if Int.eq c n then sl else select_switch n sl' + | LSnil => sl + | LScons None s sl' => sl + | LScons (Some i) s sl' => select_switch_default sl' + end. + +Fixpoint select_switch_case (n: int) (sl: labeled_statements): option labeled_statements := + match sl with + | LSnil => None + | LScons None s sl' => select_switch_case n sl' + | LScons (Some c) s sl' => if Int.eq c n then Some sl else select_switch_case n sl' + end. + +Definition select_switch (n: int) (sl: labeled_statements): labeled_statements := + match select_switch_case n sl with + | Some sl' => sl' + | None => select_switch_default sl end. (** Turn a labeled statement into a sequence *) Fixpoint seq_of_labeled_statement (sl: labeled_statements) : statement := match sl with - | LSdefault s => s - | LScase c s sl' => Ssequence s (seq_of_labeled_statement sl') + | LSnil => Sskip + | LScons _ s sl' => Ssequence s (seq_of_labeled_statement sl') end. Section SEMANTICS. @@ -507,8 +521,8 @@ Fixpoint find_label (lbl: label) (s: statement) (k: cont) with find_label_ls (lbl: label) (sl: labeled_statements) (k: cont) {struct sl}: option (statement * cont) := match sl with - | LSdefault s => find_label lbl s k - | LScase _ s sl' => + | LSnil => None + | LScons _ s sl' => match find_label lbl s (Kseq (seq_of_labeled_statement sl') k) with | Some sk => Some sk | None => find_label_ls lbl sl' k -- cgit v1.2.3