Skip to content
Snippets Groups Projects
Verified Commit 488299fc authored by Recolic Keghart's avatar Recolic Keghart
Browse files

finish `list[1]` = "fuckyou", passing more

parent 91985dba
No related branches found
No related tags found
No related merge requests found
Pipeline #498 passed with warnings with stages
in 13 minutes and 17 seconds
...@@ -398,6 +398,7 @@ public class CodeGenImpl extends CodeGenBase { ...@@ -398,6 +398,7 @@ public class CodeGenImpl extends CodeGenBase {
RiscVBackend.Register tmpReg = /*regMgr.borrowOneTmp()*/ T6; RiscVBackend.Register tmpReg = /*regMgr.borrowOneTmp()*/ T6;
RiscVBackend.Register tmpReg2 = /*regMgr.borrowOneTmp()*/ T5; RiscVBackend.Register tmpReg2 = /*regMgr.borrowOneTmp()*/ T5;
betterBackend.emitPop(tmpReg, "ASSIGN: Get the value to assign: Pop the expr result from stack"); betterBackend.emitPop(tmpReg, "ASSIGN: Get the value to assign: Pop the expr result from stack");
SymbolType symbolType = null; SymbolType symbolType = null;
for(Expr expr : node.targets) { for(Expr expr : node.targets) {
if (expr instanceof MemberExpr) { if (expr instanceof MemberExpr) {
...@@ -433,9 +434,20 @@ public class CodeGenImpl extends CodeGenBase { ...@@ -433,9 +434,20 @@ public class CodeGenImpl extends CodeGenBase {
} else if (foundMethod) { } else if (foundMethod) {
//TODO: Implement //TODO: Implement
} }
} else { }
else if(expr instanceof IndexExpr) {
IndexExpr indexExpr = (IndexExpr) expr;
indexExpr.list.dispatch(this);
indexExpr.index.dispatch(this);
RiscVBackend.Register indexReg = A6, listReg = tmpReg2, lenReg = A7, elementReg = tmpReg;
betterBackend.emitPop(indexReg, "Assign list element: get index");
betterBackend.emitPop(listReg, "Assign list element: get list");
emitSetListElement(listReg, indexReg, lenReg, elementReg);
}
else {
SymbolInfo symbolInfo = sym.get(((Identifier) expr).name); SymbolInfo symbolInfo = sym.get(((Identifier) expr).name);
Label label = null; /* Recolic: no need to deal with int/bool anymore. they're only wrapped before calling print.
if(symbolType == null) { if(symbolType == null) {
// first run: // first run:
if (symbolInfo instanceof GlobalVarInfo) { if (symbolInfo instanceof GlobalVarInfo) {
...@@ -449,9 +461,10 @@ public class CodeGenImpl extends CodeGenBase { ...@@ -449,9 +461,10 @@ public class CodeGenImpl extends CodeGenBase {
// first run: // first run:
/* All int and bool variable is not wrapped. /* All int and bool variable is not wrapped.
backend.emitLW(tmpReg, tmpReg, 3 * backend.getWordSize(), "ASSIGN: Load the value of INT or BOOL"); backend.emitLW(tmpReg, tmpReg, 3 * backend.getWordSize(), "ASSIGN: Load the value of INT or BOOL");
*/ *-/
} }
} }
*/
// do assign // do assign
if (symbolInfo instanceof GlobalVarInfo) { if (symbolInfo instanceof GlobalVarInfo) {
...@@ -932,6 +945,7 @@ public class CodeGenImpl extends CodeGenBase { ...@@ -932,6 +945,7 @@ public class CodeGenImpl extends CodeGenBase {
private void emitGetElementByIndex(RiscVBackend.Register listReg, RiscVBackend.Register indexReg, RiscVBackend.Register lenReg, SymbolType listType) { private void emitGetElementByIndex(RiscVBackend.Register listReg, RiscVBackend.Register indexReg, RiscVBackend.Register lenReg, SymbolType listType) {
//! Note: All registers in argument will be modified! listType should be listType or STR_TYPE //! Note: All registers in argument will be modified! listType should be listType or STR_TYPE
//! Note: You need to set listReg, indexReg. lenReg is just a temp reg, no need to set it.
//! @returns: listReg //! @returns: listReg
//! listReg is resultReg. Return list element ptr, or new string ptr. //! listReg is resultReg. Return list element ptr, or new string ptr.
backend.emitLW(lenReg, listReg, 3*backend.getWordSize(), "IndexExpr: Get List/str length"); backend.emitLW(lenReg, listReg, 3*backend.getWordSize(), "IndexExpr: Get List/str length");
...@@ -969,10 +983,39 @@ public class CodeGenImpl extends CodeGenBase { ...@@ -969,10 +983,39 @@ public class CodeGenImpl extends CodeGenBase {
backend.emitMV(listReg, A0, "IndexExpr: move result string to resultReg"); backend.emitMV(listReg, A0, "IndexExpr: move result string to resultReg");
} }
}
private void emitSetListElement(RiscVBackend.Register listReg, RiscVBackend.Register indexReg, RiscVBackend.Register lenReg, RiscVBackend.Register elementReg) {
/* DO NOT edit this function. it's copy-pasted from emitGetElement. keep them consist. */
//! Note: All registers in argument will be modified! listType should be listType or STR_TYPE
//! Note: You need to set listReg, indexReg, elementReg. lenReg is just a temp reg, no need to set it.
backend.emitLW(lenReg, listReg, 3*backend.getWordSize(), "IndexExpr: Get List/str length");
backend.emitADDI(listReg, listReg, 4*backend.getWordSize(), "IndexExpr: listPtr = list.data");
String labelPrefix = getLabelPrefix();
Label skipTranslateNegIndex = new Label(labelPrefix + ".fuckNegIndexEnd");
// Get real index.
// Recolic: they don't like "abc"[-1], they want to crash it . Remove the feature and make them happy.
// backend.emitBGEZ(indexReg, skipTranslateNegIndex, "IndexExpr: Skip neg index op");
// backend.emitADD(indexReg, indexReg, lenReg, "IndexExpr: Add length to negative index.");
backend.emitBLTZ(indexReg, errorOob, "IndexExpr: Exception: negative index still negative after adding length. FUCK IT!!!");
backend.emitLocalLabel(skipTranslateNegIndex, "IndexExpr: DO REAL ACCESS");
// Do real access!
backend.emitBGE(indexReg, lenReg, errorOob, "IndexExpr: Exception: index larger than length. FUCK IT.");
if(true) {
assert backend.getWordSize() == 4;
backend.emitSLLI(indexReg, indexReg, 2, "IndexExpr: for list, element size = 4 char, so: indexReg *= 4");
}
backend.emitADD(listReg, listReg, indexReg, "IndexExpr: listPtr += index");
// do real set!
backend.emitSW(elementReg, listReg, 0, "IndexExpr Assign: Do real assign.");
} }
@Override @Override
public Void analyze(IndexExpr node) { public Void analyze(IndexExpr node) {
node.list.dispatch(this); node.list.dispatch(this);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment