summaryrefslogtreecommitdiff
path: root/submit/ast/UnaryOperator.java
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-04-23 00:24:42 -0600
commita1c15f046183373baf5deb66e77188e656806fb7 (patch)
treefc1a7f584e67dc583d335f09d5271a45ff7d9df4 /submit/ast/UnaryOperator.java
parent5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (diff)
downloadcminus-a1c15f046183373baf5deb66e77188e656806fb7.tar.gz
cminus-a1c15f046183373baf5deb66e77188e656806fb7.zip
squash all the thingsHEADmain
Diffstat (limited to 'submit/ast/UnaryOperator.java')
-rw-r--r--submit/ast/UnaryOperator.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/submit/ast/UnaryOperator.java b/submit/ast/UnaryOperator.java
index 1b5082a..5195ef0 100644
--- a/submit/ast/UnaryOperator.java
+++ b/submit/ast/UnaryOperator.java
@@ -4,6 +4,10 @@
*/
package submit.ast;
+import submit.MIPSResult;
+import submit.RegisterAllocator;
+import submit.SymbolTable;
+
/**
*
* @author edwajohn
@@ -23,5 +27,27 @@ public class UnaryOperator extends AbstractNode implements Expression {
builder.append(type);
expression.toCminus(builder, prefix);
}
+ @Override
+ public MIPSResult toMIPS(StringBuilder code, StringBuilder data,
+ SymbolTable symbolTable,
+ RegisterAllocator regAllocator) {
+ MIPSResult result =
+ expression.toMIPS(code, data, symbolTable, regAllocator);
+
+ String reg = regAllocator.getRegisterOrLoadIntoRegister(result, code);
+ switch (type) {
+ case NEG:
+ code.append(String.format("sub %s $zero %s\n", reg, reg));
+ break;
+ case NOT:
+ code.append(String.format("sltu %s $zero %s\n", reg, reg))
+ .append(String.format("xori %s %s 1\n", reg, reg));
+ break;
+ default:
+ break;
+ }
+
+ return MIPSResult.createRegisterResult(reg, VarType.INT);
+ }
}