diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-23 00:24:42 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-23 00:24:42 -0600 |
commit | a1c15f046183373baf5deb66e77188e656806fb7 (patch) | |
tree | fc1a7f584e67dc583d335f09d5271a45ff7d9df4 /submit/ast/While.java | |
parent | 5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (diff) | |
download | cminus-a1c15f046183373baf5deb66e77188e656806fb7.tar.gz cminus-a1c15f046183373baf5deb66e77188e656806fb7.zip |
Diffstat (limited to 'submit/ast/While.java')
-rw-r--r-- | submit/ast/While.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/submit/ast/While.java b/submit/ast/While.java index a37195e..41c3963 100644 --- a/submit/ast/While.java +++ b/submit/ast/While.java @@ -4,6 +4,10 @@ */ package submit.ast; +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + /** * * @author edwajohn @@ -28,6 +32,29 @@ public class While extends AbstractNode implements Statement { } else { statement.toCminus(builder, prefix + " "); } + } + + @Override + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, + SymbolTable symbolTable, + RegisterAllocator regAllocator) { + String loopLabel = "while_truthy_" + SymbolTable.nextId(); + String finishedLabel = "finished_while_" + SymbolTable.nextId(); + + code.append(String.format("%s:\n", loopLabel)); + + MIPSResult expressionTruthiness = + expression.toMIPS(code, data, symbolTable, regAllocator); + + code.append(String.format("bne %s $zero %s\n", + expressionTruthiness.getRegister(), + finishedLabel)); + + MIPSResult inside = statement.toMIPS(code, data, symbolTable, regAllocator); + regAllocator.clear(inside.getRegister()); + code.append(String.format("j %s\n", loopLabel)) + .append(String.format("%s:\n", finishedLabel)); + return MIPSResult.createVoidResult(); } } |