summaryrefslogtreecommitdiff
path: root/pp2cc.py
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2011-12-09 15:46:41 +0000
committerPeter Wu <lekensteyn@gmail.com>2011-12-09 15:46:41 +0000
commit538cf03964d804c5da66385acd725b9449dfae09 (patch)
treece2247cca8e00735ee83e6fd191fd0a99895b959 /pp2cc.py
parenta2e9eaa837bde1d0a35f8c18b8e31f7e5398f3bc (diff)
downloadpp2cc-538cf03964d804c5da66385acd725b9449dfae09.tar.gz
Support nested case/default labels
Diffstat (limited to 'pp2cc.py')
-rwxr-xr-xpp2cc.py50
1 files changed, 29 insertions, 21 deletions
diff --git a/pp2cc.py b/pp2cc.py
index ac00ffd..6205e94 100755
--- a/pp2cc.py
+++ b/pp2cc.py
@@ -1139,29 +1139,37 @@ class Parse(object):
switch_end = self.uniqLbl("switchEnd")
linked_node.setBreak(switch_end)
lbl_default = None
+
for cn in stmt.block_items:
linked_cn = LinkedNode(cn, linked_node)
- if linked_cn.type == "Case":
- lbl_case = self.uniqLbl("case")
- try:
- case_val = self.determineConstValue(LinkedNode(cn.expr,
- linked_cn))
- except RuntimeError:
- e = sys.exc_info()[1]
- print "case label does not reduce to an integer constant"
- print e
- raise
-
- lines_cases.append(self.asm.binary_op("CMP", switch_reg, case_val))
- lines_cases.append(self.asm.branch_op("BEQ", lbl_case))
- lines_stmts.append(self.asm.noop(lbl_case))
- lines_stmts += self.parseStatement(cn.stmt, linked_cn)
- elif linked_cn.type == "Default":
- lbl_default = self.uniqLbl("default")
- lines_stmts.append(self.asm.noop(lbl_default))
- lines_stmts += self.parseStatement(cn.stmt, linked_cn)
- else:
- lines_stmts += self.parseStatement(cn, linked_cn)
+ while linked_cn is not None:
+ cn = linked_cn.node
+ if linked_cn.type == "Case":
+ lbl_case = self.uniqLbl("case")
+ try:
+ case_val = self.determineConstValue(LinkedNode(cn.expr,
+ linked_cn))
+ except RuntimeError:
+ e = sys.exc_info()[1]
+ print "case label does not reduce to an integer constant"
+ print e
+ raise
+
+ lines_cases.append(self.asm.binary_op("CMP", switch_reg, case_val))
+ lines_cases.append(self.asm.branch_op("BEQ", lbl_case))
+ lines_stmts.append(self.asm.noop(lbl_case))
+ linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
+ elif linked_cn.type == "Default":
+ lbl_default = self.uniqLbl("default")
+ lines_stmts.append(self.asm.noop(lbl_default))
+ linked_cn = LinkedNode(cn.stmt, linked_cn.parent)
+ if linked_cn.type in ("Case", "Default"):
+ # nested case/default labels like case 1: case 2: stmt;
+ pass
+ else:
+ # regular body
+ lines_stmts += self.parseStatement(linked_cn.node, linked_cn.parent)
+ linked_cn = None
lines += lines_cases
if lbl_default:
lines.append(self.asm.branch_op("BRA", lbl_default))