Skip to content

Commit ffc61f8

Browse files
author
João Rebelo
committed
invokestaticinstruction
1 parent 7756346 commit ffc61f8

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

input.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import io;
22

3-
class Test {
4-
3+
class Arithmetic_and {
54
public static void main(String[] args) {
6-
Test test;
7-
test = new Test();
8-
}
5+
boolean a;
6+
7+
a = true && false;
98

10-
}
9+
if(a) {
10+
io.print(1);
11+
} else {
12+
io.print(0);
13+
}
14+
15+
}
16+
}

src/main/pt/up/fe/comp2025/backend/JasminGenerator.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public JasminGenerator(OllirResult ollirResult) {
6666
generators.put(SingleOpCondInstruction.class, this::generateSingleOpCond);
6767
generators.put(NewInstruction.class, this::generateNewInstruction);
6868
generators.put(InvokeSpecialInstruction.class, this::generateInvokeSpecialInstruction);
69+
generators.put(InvokeStaticInstruction.class, this::generateInvokeStaticInstruction);
70+
6971
}
7072

7173
private String apply(TreeNode node) {
@@ -89,6 +91,52 @@ public String build() {
8991
return code;
9092
}
9193

94+
private String generateInvokeStaticInstruction(InvokeStaticInstruction staticInst) {
95+
StringBuilder result = new StringBuilder();
96+
97+
staticInst.getArguments().forEach(argument -> result.append(apply(argument)));
98+
99+
String callerInfo = staticInst.getCaller().toString();
100+
String targetClass = callerInfo.contains(".")
101+
? callerInfo.substring(0, callerInfo.indexOf("."))
102+
: callerInfo;
103+
104+
String methodId = staticInst.getMethodName().toString();
105+
106+
String parameterTypes = staticInst.getArguments().stream()
107+
.map(arg -> convertToJvmType(arg.getType()))
108+
.reduce("", String::concat);
109+
110+
String returnTypeDesc = convertToJvmType(staticInst.getReturnType());
111+
112+
result.append("invokestatic ")
113+
.append(targetClass)
114+
.append("/")
115+
.append(methodId)
116+
.append("(")
117+
.append(parameterTypes)
118+
.append(")")
119+
.append(returnTypeDesc)
120+
.append(NL);
121+
122+
return result.toString();
123+
}
124+
125+
private String convertToJvmType(Type ollirType) {
126+
String typeStr = ollirType.toString();
127+
128+
if ("INT32".equals(typeStr)) return "I";
129+
if ("BOOLEAN".equals(typeStr)) return "Z";
130+
if ("SHORT".equals(typeStr)) return "S";
131+
if ("BYTE".equals(typeStr)) return "B";
132+
if ("CHAR".equals(typeStr)) return "C";
133+
if ("FLOAT".equals(typeStr)) return "F";
134+
if ("VOID".equals(typeStr)) return "V";
135+
if ("INT32[]".equals(typeStr)) return "[I";
136+
if ("STRING[]".equals(typeStr)) return "[Ljava/lang/String;";
137+
138+
return "L" + typeStr + ";";
139+
}
92140

93141
private String generateClassUnit(ClassUnit classUnit) {
94142

0 commit comments

Comments
 (0)