Skip to content

fix the mistake of parameter passing #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ project/plugins/project/
.scala_dependencies
.worksheet
.idea/
.metals/

# Tmp files
*.tac
Expand Down
87 changes: 54 additions & 33 deletions src/main/java/decaf/lowlevel/Mips.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,13 @@ public class Mips {
public static final Reg S8 = new Reg(30, "$s8"); // also called $fp, but used as an additional saved register
public static final Reg RA = new Reg(31, "$ra"); // return address

public static final Reg[] callerSaved = new Reg[]{
V1, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9
};
public static final Reg[] callerSaved = new Reg[] { V1, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9 };

public static final Reg[] calleeSaved = new Reg[]{
S0, S1, S2, S3, S4, S5, S6, S7, S8
};
public static final Reg[] calleeSaved = new Reg[] { S0, S1, S2, S3, S4, S5, S6, S7, S8 };

public static final Reg[] allocatableRegs = ArrayUtils.addAll(callerSaved, calleeSaved);

public static final Reg[] argRegs = new Reg[]{
A0, A1, A2, A3
};
public static final Reg[] argRegs = new Reg[] { A0, A1, A2, A3 };

// Instructions

Expand All @@ -81,7 +75,7 @@ private static String format(String op, String fmt, Object... args) {
public static class Move extends PseudoInstr {

public Move(Temp dst, Temp src) {
super(new Temp[]{dst}, new Temp[]{src});
super(new Temp[] { dst }, new Temp[] { src });
}

@Override
Expand All @@ -97,7 +91,7 @@ public enum UnaryOp {
public static class Unary extends PseudoInstr {

public Unary(UnaryOp op, Temp dst, Temp src) {
super(new Temp[]{dst}, new Temp[]{src});
super(new Temp[] { dst }, new Temp[] { src });
this.op = op.toString().toLowerCase();
}

Expand All @@ -110,15 +104,13 @@ public String toString() {
}

public enum BinaryOp {
ADD, SUB, MUL, DIV, REM,
SGT, SGE, SEQ, SNE, SLE, SLT,
AND, OR
ADD, SUB, MUL, DIV, REM, SGT, SGE, SEQ, SNE, SLE, SLT, AND, OR
}

public static class Binary extends PseudoInstr {

public Binary(BinaryOp op, Temp dst, Temp src0, Temp src1) {
super(new Temp[]{dst}, new Temp[]{src0, src1});
super(new Temp[] { dst }, new Temp[] { src0, src1 });
this.op = op.toString().toLowerCase();
}

Expand All @@ -137,7 +129,7 @@ public enum BranchOp {
public static class Branch extends PseudoInstr {

public Branch(BranchOp op, Temp src, Label to) {
super(Kind.COND_JMP, new Temp[]{}, new Temp[]{src}, to);
super(Kind.COND_JMP, new Temp[] {}, new Temp[] { src }, to);
this.op = op.toString().toLowerCase();
}

Expand All @@ -152,7 +144,7 @@ public String toString() {
public static class Jump extends PseudoInstr {

public Jump(Label to) {
super(Kind.JMP, new Temp[]{}, new Temp[]{}, to);
super(Kind.JMP, new Temp[] {}, new Temp[] {}, to);
}

@Override
Expand All @@ -162,12 +154,13 @@ public String toString() {
}

/**
* The special jump-to-epilogue instruction {@code j epilogue} is regarded as a return statement.
* The special jump-to-epilogue instruction {@code j epilogue} is regarded as a
* return statement.
*/
public static class JumpToEpilogue extends PseudoInstr {

public JumpToEpilogue(Label label) {
super(Kind.RET, new Temp[]{}, new Temp[]{}, new Label(label + EPILOGUE_SUFFIX));
super(Kind.RET, new Temp[] {}, new Temp[] {}, new Label(label + EPILOGUE_SUFFIX));
}

@Override
Expand All @@ -179,7 +172,7 @@ public String toString() {
public static class JumpAndLink extends PseudoInstr {

public JumpAndLink(Label to) {
super(Kind.SEQ, new Temp[]{}, new Temp[]{}, to);
super(Kind.SEQ, new Temp[] {}, new Temp[] {}, to);
}

@Override
Expand All @@ -191,7 +184,7 @@ public String toString() {
public static class JumpAndLinkReg extends PseudoInstr {

public JumpAndLinkReg(Temp src) {
super(new Temp[]{}, new Temp[]{src});
super(new Temp[] {}, new Temp[] { src });
}

@Override
Expand All @@ -203,7 +196,7 @@ public String toString() {
public static class LoadWord extends PseudoInstr {

public LoadWord(Temp dst, Temp base, int offset) {
super(new Temp[]{dst}, new Temp[]{base});
super(new Temp[] { dst }, new Temp[] { base });
this.offset = offset;
}

Expand All @@ -218,7 +211,7 @@ public String toString() {
public static class StoreWord extends PseudoInstr {

public StoreWord(Temp src, Temp base, int offset) {
super(new Temp[]{}, new Temp[]{src, base});
super(new Temp[] {}, new Temp[] { src, base });
this.offset = offset;
}

Expand All @@ -233,7 +226,7 @@ public String toString() {
public static class LoadImm extends PseudoInstr {

public LoadImm(Temp dst, int value) {
super(new Temp[]{dst}, new Temp[]{});
super(new Temp[] { dst }, new Temp[] {});
this.value = value;
}

Expand All @@ -248,7 +241,7 @@ public String toString() {
public static class LoadAddr extends PseudoInstr {

public LoadAddr(Temp dst, Label label) {
super(Kind.SEQ, new Temp[]{dst}, new Temp[]{}, label);
super(Kind.SEQ, new Temp[] { dst }, new Temp[] {}, label);
}

@Override
Expand All @@ -272,7 +265,7 @@ public String toString() {
public static class Syscall extends NativeInstr {

public Syscall() {
super(new Reg[]{}, new Reg[]{});
super(new Reg[] {}, new Reg[] {});
}

@Override
Expand All @@ -284,7 +277,7 @@ public String toString() {
public static class NativeMove extends NativeInstr {

public NativeMove(Reg dst, Reg src) {
super(new Reg[]{dst}, new Reg[]{src});
super(new Reg[] { dst }, new Reg[] { src });
}

@Override
Expand All @@ -296,10 +289,22 @@ public String toString() {
public static class NativeLoadWord extends NativeInstr {

public NativeLoadWord(Reg dst, Reg base, int offset) {
super(new Reg[]{dst}, new Reg[]{base});
super(new Reg[] { dst }, new Reg[] { base });
this.offset = offset;
}

public NativeLoadWord(Reg dst, Reg base, int offset, boolean isArgOrLocal) {
super(new Reg[] { dst }, new Reg[] { base });
this.offset = offset;
this.isArgOrLocal = isArgOrLocal;
}

private boolean isArgOrLocal; // [true] represents argument, and [false] represents local data.

public void backfill(int maxArgsSize, int frameLength) {
offset += isArgOrLocal ? frameLength : maxArgsSize;
}

private int offset;

@Override
Expand All @@ -311,10 +316,26 @@ public String toString() {
public static class NativeStoreWord extends NativeInstr {

public NativeStoreWord(Reg src, Reg base, int offset) {
super(new Reg[]{}, new Reg[]{src, base});
super(new Reg[] {}, new Reg[] { src, base });
this.offset = offset;
}

public NativeStoreWord(Reg src, Reg base, int offset, boolean isArgOrLocal) {
super(new Reg[] {}, new Reg[] { src, base });
this.offset = offset;
this.isArgOrLocal = isArgOrLocal;
}

private boolean isArgOrLocal; // [true] represents argument, and [false] represents local data.

public void backfill(int maxArgsSize, int frameLength) {
offset += isArgOrLocal ? frameLength : maxArgsSize;

// System.out.println("After backfill(maxArgsSize = " + maxArgsSize + ",
// frameLength = " + frameLength
// + "), offset = " + offset + "\n");
}

private int offset;

@Override
Expand All @@ -324,13 +345,13 @@ public String toString() {
}

/**
* Since the only possible usage of the {@code jr} is to return a subroutine with {@code jr $ra}.
* Why not simply call this "Return"?
* Since the only possible usage of the {@code jr} is to return a subroutine
* with {@code jr $ra}. Why not simply call this "Return"?
*/
public static class NativeReturn extends NativeInstr {

public NativeReturn() {
super(Kind.RET, new Reg[]{RA}, new Reg[]{}, null);
super(Kind.RET, new Reg[] { RA }, new Reg[] {}, null);
}

@Override
Expand All @@ -342,7 +363,7 @@ public String toString() {
public static class SPAdd extends NativeInstr {

public SPAdd(int offset) {
super(new Reg[]{SP}, new Reg[]{SP});
super(new Reg[] { SP }, new Reg[] { SP });
this.offset = offset;
}

Expand Down
28 changes: 16 additions & 12 deletions src/main/java/decaf/lowlevel/tac/FuncVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void visitReturn(Temp value) {
public Temp visitNewClass(String clazz) {
var temp = freshTemp();
var entry = ctx.getConstructorLabel(clazz);
func.add(new TacInstr.DirectCall(temp, entry));
func.add(new TacInstr.DirectCall(temp, entry, 1));
return temp;
}

Expand Down Expand Up @@ -195,7 +195,8 @@ public void visitMemberWrite(Temp object, String clazz, String variable, Temp va
* @param clazz class name
* @param method member method name
* @param args argument temps
* @param needReturn do we need a fresh temp to store the return value? (default false)
* @param needReturn do we need a fresh temp to store the return value? (default
* false)
* @return the fresh temp if we need return (or else null)
*/
public Temp visitMemberCall(Temp object, String clazz, String method, List<Temp> args, boolean needReturn) {
Expand All @@ -209,9 +210,9 @@ public Temp visitMemberCall(Temp object, String clazz, String method, List<Temp>
}
if (needReturn) {
temp = freshTemp();
func.add(new TacInstr.IndirectCall(temp, entry));
func.add(new TacInstr.IndirectCall(temp, entry, args.size()));
} else {
func.add(new TacInstr.IndirectCall(entry));
func.add(new TacInstr.IndirectCall(entry, args.size()));
}
return temp;
}
Expand All @@ -229,7 +230,8 @@ public void visitMemberCall(Temp object, String clazz, String method, List<Temp>
* @param clazz class name
* @param method method name
* @param args argument temps
* @param needReturn do we need a fresh temp to store the return value? (default false)
* @param needReturn do we need a fresh temp to store the return value? (default
* false)
* @return the fresh temp if we need return (or else null)
*/
public Temp visitStaticCall(String clazz, String method, List<Temp> args, boolean needReturn) {
Expand All @@ -241,9 +243,9 @@ public Temp visitStaticCall(String clazz, String method, List<Temp> args, boolea
}
if (needReturn) {
temp = freshTemp();
func.add(new TacInstr.DirectCall(temp, entry));
func.add(new TacInstr.DirectCall(temp, entry, args.size()));
} else {
func.add(new TacInstr.DirectCall(entry));
func.add(new TacInstr.DirectCall(entry, args.size()));
}
return temp;
}
Expand All @@ -259,7 +261,8 @@ public void visitStaticCall(String clazz, String method, List<Temp> args) {
* Append instructions to invoke an intrinsic method.
*
* @param func intrinsic function
* @param needReturn do we need a fresh temp to store the return value? (default false)
* @param needReturn do we need a fresh temp to store the return value? (default
* false)
* @param args argument temps
* @return the fresh temp if we need return (or else null)
*/
Expand All @@ -271,9 +274,9 @@ public Temp visitIntrinsicCall(Intrinsic func, boolean needReturn, Temp... args)
}
if (needReturn) {
temp = freshTemp();
this.func.add(new TacInstr.DirectCall(temp, func));
this.func.add(new TacInstr.DirectCall(temp, func, args.length));
} else {
this.func.add(new TacInstr.DirectCall(func));
this.func.add(new TacInstr.DirectCall(func, args.length));
}
return temp;
}
Expand Down Expand Up @@ -394,8 +397,9 @@ public Temp freshTemp() {
/**
* Get the temp for the {@code index}-th argument.
* <p>
* According to TAC virtual machine calling convention, for a function with {@code n} arguments, the temps with id
* from 0 to {@code n - 1} are reserved for passing these {@code n} arguments.
* According to TAC virtual machine calling convention, for a function with
* {@code n} arguments, the temps with id from 0 to {@code n - 1} are reserved
* for passing these {@code n} arguments.
*
* @param index argument index, start from 0
* @return temp
Expand Down
Loading