Skip to content

Commit b67ea33

Browse files
committed
assign arrays param and local var
1 parent eebc8d9 commit b67ea33

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/main/pt/up/fe/comp2023/ollir/Optimization.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import pt.up.fe.comp.jmm.analysis.JmmSemanticsResult;
44
import pt.up.fe.comp.jmm.analysis.table.Symbol;
55
import pt.up.fe.comp.jmm.analysis.table.SymbolTable;
6+
import pt.up.fe.comp.jmm.ast.AJmmVisitor;
67
import pt.up.fe.comp.jmm.ast.JmmNode;
78
import pt.up.fe.comp.jmm.ollir.JmmOptimization;
89
import pt.up.fe.comp.jmm.ollir.OllirResult;
910
import pt.up.fe.comp.jmm.report.Report;
10-
import pt.up.fe.comp.jmm.ast.AJmmVisitor;
11-
import pt.up.fe.comp2023.ollir.OllirUtils;
1211

1312
import java.util.ArrayList;
1413
import java.util.List;
@@ -330,8 +329,62 @@ private Void dealWithNegationExpr(JmmNode jmmNode, Void unused) {
330329
}
331330

332331
private Void dealWithArrayAssignment(JmmNode jmmNode, Void unused) {
333-
for (var child : jmmNode.getChildren())
334-
visit(child);
332+
var method = jmmNode.getAncestor("MethodDecl");
333+
var voidMethod = jmmNode.getAncestor("VoidMethodDecl");
334+
var mainMethod = jmmNode.getAncestor("MainMethodDecl");
335+
Symbol var = null;
336+
boolean isLocal = false, isParam = false, isField = false;
337+
int idParam = 0;
338+
String left = jmmNode.get("arrayname");
339+
340+
if (method.isPresent() || voidMethod.isPresent() || mainMethod.isPresent()) {
341+
var methodName = mainMethod.isPresent() ? "main" : method.orElseGet(voidMethod::get).get("methodname");
342+
List<Symbol> localVarClass = table.getLocalVariables(methodName);
343+
List<Symbol> paramsOnClass = table.getParameters(methodName);
344+
345+
// Check if local var
346+
for (Symbol lv : localVarClass)
347+
if (lv.getName().equals(left)) {
348+
var = lv;
349+
isLocal = true;
350+
}
351+
352+
// If not local var, then check if param
353+
if (var == null)
354+
for (int p = 0; p < paramsOnClass.size(); p++)
355+
if (paramsOnClass.get(p).getName().equals(left)) {
356+
var = paramsOnClass.get(p);
357+
isParam = true;
358+
idParam = p + 1;
359+
}
360+
}
361+
362+
List<Symbol> fields = table.getFields();
363+
364+
// If not local nor param, check if field
365+
if (var == null)
366+
for (Symbol f : fields)
367+
if (f.getName().equals(left)) {
368+
var = f;
369+
isField = true;
370+
}
371+
372+
JmmNode right = jmmNode.getChildren().get(0);
373+
JmmNode last = jmmNode.getChildren().get(1);
374+
visit(right);
375+
376+
if (isLocal)
377+
code += "\t\t" + left + "[" ;
378+
else if (isParam)
379+
code += "\t\t$" + idParam + '.' + left + "[";
380+
else if (isField)
381+
code += "\t\tputfield(this, " + left + "[";
382+
383+
code += right.get("valueOl") + "].i32 :=.i32 ";
384+
385+
visit(last);
386+
387+
code += last.get("valueOl") +";\n";
335388

336389
return null;
337390
}

test/pt/up/fe/comp/cpf/3_ollir/arrays/ArrayAccess.jmm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ArrayAccess {
33

44
public int foo(int[] a) {
55
int result;
6-
6+
77
result = a[0];
88
result = a[1] + a[2];
99
result = result + a[3];

0 commit comments

Comments
 (0)