Skip to content

Commit 4a31425

Browse files
committed
Simplify
1 parent fa16977 commit 4a31425

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

ir.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -484,29 +484,26 @@ def op(idx: int) -> str:
484484
return _decl("bool", f"is_list({op(0)})")
485485
if isinstance(instr, IsEmptyList):
486486
return _decl("bool", f"{op(0)} == empty_list()")
487+
if isinstance(instr, Return):
488+
return f"return {op(0)};\n"
489+
if isinstance(instr, Jump):
490+
return f"goto {instr.target.name()};\n"
491+
if isinstance(instr, CondBranch):
492+
return f"if ({op(0)}) {{ goto {instr.conseq.name()}; }} else {{ goto {instr.alt.name()}; }}\n"
493+
if isinstance(instr, MatchFail):
494+
return "\n".join(
495+
[
496+
"""fprintf(stderr, "no matching cases\\n");""",
497+
"abort();",
498+
"return NULL;\n", # Pacify the C compiler
499+
]
500+
)
487501
raise NotImplementedError(type(instr))
488502

489503
def _to_c(self, f: io.StringIO, block: Block, gvn: InstrId) -> None:
490504
f.write(f"{block.name()}:;\n")
491505
for instr in block.instrs:
492-
if isinstance(instr, Control):
493-
break
494506
f.write(self._instr_to_c(instr.find(), gvn))
495-
assert isinstance(instr, Control)
496-
if isinstance(instr, Return):
497-
f.write(f"return {gvn.name(instr.operands[0])};\n")
498-
elif isinstance(instr, Jump):
499-
f.write(f"goto {instr.target.name()};\n")
500-
elif isinstance(instr, CondBranch):
501-
f.write(
502-
f"if ({gvn.name(instr.operands[0])}) {{ goto {instr.conseq.name()}; }} else {{ goto {instr.alt.name()}; }}\n"
503-
)
504-
elif isinstance(instr, MatchFail):
505-
f.write("""fprintf(stderr, "no matching cases\\n");\n""")
506-
f.write("abort();\n")
507-
f.write("return NULL;\n") # Pacify the C compiler
508-
else:
509-
raise NotImplementedError(instr)
510507

511508

512509
class Compiler:

0 commit comments

Comments
 (0)