if statement asm generation now omits else labels if no else was defined
This commit is contained in:
@@ -137,24 +137,30 @@ 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 (e.alt != null) {
|
if (hasElse) {
|
||||||
e.alt.welcome(this);
|
this.ex.write(" jz .L" + lblElse + "\n");
|
||||||
} else if (e.elif != null) {
|
} else {
|
||||||
e.elif.welcome(this);
|
this.ex.write(" jz .L" + lblEnd + "\n");
|
||||||
}
|
}
|
||||||
this.ex.write(" jmp .L" + end + "\n");
|
e.then.welcome(this);
|
||||||
// then Part
|
if (hasElse) {
|
||||||
this.ex.write(".L" + then + ":\n");
|
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||||
e.then.welcome(this);
|
this.ex.write(".L" + lblElse + ":\n");
|
||||||
this.ex.write(".L" + end + ":\n");
|
if (e.alt != null) {
|
||||||
return null;
|
e.alt.welcome(this);
|
||||||
|
} else {
|
||||||
|
e.elif.welcome(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.ex.write(".L" + lblEnd + ":\n");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user