if statement asm generation now omits else labels if no else was defined

This commit is contained in:
Marvin Kaiser
2019-12-17 15:12:36 +01:00
parent bf50587cdb
commit aaf1c3195d

View File

@@ -137,23 +137,29 @@ public class GenASM implements Visitor<Void> {
@Override @Override
public Void visit(IfStatement e) { public Void visit(IfStatement e) {
int then = ++lCount; int lblElse = ++lCount;
int end = ++lCount; int lblEnd = ++lCount;
boolean hasElse = e.alt != null || e.elif != null;
e.cond.welcome(this); e.cond.welcome(this);
this.ex.write(" cmp $0, %rax\n"); this.ex.write(" cmp $0, %rax\n");
this.ex.write(" jz .L" + then + "\n"); // in case of cond evaluating to false, jump to else/elif
// else Part // Jump to end if there is no else part, this saves a label declaration
if (hasElse) {
this.ex.write(" jz .L" + lblElse + "\n");
} else {
this.ex.write(" jz .L" + lblEnd + "\n");
}
e.then.welcome(this);
if (hasElse) {
this.ex.write(" jmp .L" + lblEnd + "\n");
this.ex.write(".L" + lblElse + ":\n");
if (e.alt != null) { if (e.alt != null) {
e.alt.welcome(this); e.alt.welcome(this);
} else if (e.elif != null) { } else {
e.elif.welcome(this); e.elif.welcome(this);
} }
this.ex.write(" jmp .L" + end + "\n"); }
// then Part this.ex.write(".L" + lblEnd + ":\n");
this.ex.write(".L" + then + ":\n");
e.then.welcome(this);
this.ex.write(".L" + end + ":\n");
return null; return null;
} }