Implemented boolean expressions
This commit is contained in:
@@ -74,6 +74,111 @@ public class GenASM implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(EqualityExpression e) {
|
||||
int lblTrue = ++lCount;
|
||||
int lblEnd = ++lCount;
|
||||
|
||||
e.lhs.welcome(this);
|
||||
this.ex.write(" pushq %rax\n");
|
||||
e.rhs.welcome(this);
|
||||
this.ex.write(" popq %rbx\n");
|
||||
this.ex.write(" cmp %rax, %rbx\n");
|
||||
this.ex.write(" je .L" + lblTrue + "\n");
|
||||
// false
|
||||
this.ex.write(" movq $0, %rax\n");
|
||||
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||
this.ex.write(".L" + lblTrue + ":\n");
|
||||
// true
|
||||
this.ex.write(" movq $1, %rax\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(GTExpression e) {
|
||||
int lblTrue = ++lCount;
|
||||
int lblEnd = ++lCount;
|
||||
|
||||
e.lhs.welcome(this);
|
||||
this.ex.write(" pushq %rax\n");
|
||||
e.rhs.welcome(this);
|
||||
this.ex.write(" popq %rbx\n");
|
||||
this.ex.write(" cmp %rax, %rbx\n");
|
||||
this.ex.write(" jg .L" + lblTrue + "\n");
|
||||
// false
|
||||
this.ex.write(" movq $0, %rax\n");
|
||||
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||
this.ex.write(".L" + lblTrue + ":\n");
|
||||
// true
|
||||
this.ex.write(" movq $1, %rax\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(GTEExpression e) {
|
||||
int lblTrue = ++lCount;
|
||||
int lblEnd = ++lCount;
|
||||
|
||||
e.lhs.welcome(this);
|
||||
this.ex.write(" pushq %rax\n");
|
||||
e.rhs.welcome(this);
|
||||
this.ex.write(" popq %rbx\n");
|
||||
this.ex.write(" cmp %rax, %rbx\n");
|
||||
this.ex.write(" jge .L" + lblTrue + "\n");
|
||||
// false
|
||||
this.ex.write(" movq $0, %rax\n");
|
||||
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||
this.ex.write(".L" + lblTrue + ":\n");
|
||||
// true
|
||||
this.ex.write(" movq $1, %rax\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(LTExpression e) {
|
||||
int lblTrue = ++lCount;
|
||||
int lblEnd = ++lCount;
|
||||
|
||||
e.lhs.welcome(this);
|
||||
this.ex.write(" pushq %rax\n");
|
||||
e.rhs.welcome(this);
|
||||
this.ex.write(" popq %rbx\n");
|
||||
this.ex.write(" cmp %rax, %rbx\n");
|
||||
this.ex.write(" jl .L" + lblTrue + "\n");
|
||||
// false
|
||||
this.ex.write(" movq $0, %rax\n");
|
||||
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||
this.ex.write(".L" + lblTrue + ":\n");
|
||||
// true
|
||||
this.ex.write(" movq $1, %rax\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(LTEExpression e) {
|
||||
int lblTrue = ++lCount;
|
||||
int lblEnd = ++lCount;
|
||||
|
||||
e.lhs.welcome(this);
|
||||
this.ex.write(" pushq %rax\n");
|
||||
e.rhs.welcome(this);
|
||||
this.ex.write(" popq %rbx\n");
|
||||
this.ex.write(" cmp %rax, %rbx\n");
|
||||
this.ex.write(" jle .L" + lblTrue + "\n");
|
||||
// false
|
||||
this.ex.write(" movq $0, %rax\n");
|
||||
this.ex.write(" jmp .L" + lblEnd + "\n");
|
||||
this.ex.write(".L" + lblTrue + ":\n");
|
||||
// true
|
||||
this.ex.write(" movq $1, %rax\n");
|
||||
this.ex.write(".L" + lblEnd + ":\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(AdditionExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
@@ -286,4 +391,5 @@ public class GenASM implements Visitor<Void> {
|
||||
this.ex.write(" ret\n");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,6 +24,41 @@ class GetVars implements Visitor<Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(EqualityExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
e.rhs.welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(GTExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
e.rhs.welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(GTEExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
e.rhs.welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(LTExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
e.rhs.welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(LTEExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
e.rhs.welcome(this);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visit(AdditionExpression e) {
|
||||
e.lhs.welcome(this);
|
||||
|
||||
Reference in New Issue
Block a user