Fixed fields with no imminent value assignment

This commit is contained in:
Jochen Seyfried 2024-07-01 20:55:02 +02:00
parent 192dfae94b
commit cabbbdcaf3
2 changed files with 58 additions and 29 deletions

View File

@ -77,18 +77,31 @@ public class MethodDecl implements Node {
HashMap<String, String> classFields = typeContext.get(classThatContainsMethod);
//Set the fields of the class
boolean fieldFound = false;
for (Map.Entry<String, String> entry : classFields.entrySet()) {
String fieldName = entry.getKey();
for (FieldDecl field : fieldDecls) {
if (field.identifier.equals(fieldName)) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
if (field.expression != null) {
field.expression.codeGen(mv, localVars, typeContext, methodContext);
} else {
// If the field is not initialized, we need to load a default value onto the stack
switch (field.type) {
case "int", "boolean", "char" -> mv.visitInsn(Opcodes.ICONST_0);
default -> mv.visitInsn(Opcodes.ACONST_NULL);
}
}
descriptor = getFieldDescriptor(field.type);
mv.visitFieldInsn(Opcodes.PUTFIELD, classThatContainsMethod, fieldName, descriptor);
} else {
throw new Exception("Field " + fieldName + " not found");
fieldFound = true;
break;
}
}
if (!fieldFound){
throw new Exception("Field " + fieldName + " not found");
}
}
//Load the parameters onto the stack

View File

@ -67,11 +67,6 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception {
//TODO: Do we need the value on the stack after assigning it?
//TODO: WE do not differentiate between InstanceVar and FieldVar
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
right.codeGen(mv, localVars, typeContext, methodContext);
if (left instanceof LocalVarIdentifier) {
LocalVarIdentifier localVar = (LocalVarIdentifier) left;
@ -89,10 +84,25 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
}
if (index == -1) {
String fieldType = typeContext.get(thisClass).get(varName);
if (fieldType != null) {
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
right.codeGen(mv, localVars, typeContext, methodContext);
// Get the field descriptor
String descriptor = getFieldDescriptor(fieldType, typeContext);
// Store the value in the field
mv.visitFieldInsn(Opcodes.PUTFIELD, thisClass, varName, descriptor);
return;
} else {
throw new Exception("Variable " + varName + " not found");
}
}
String type = localVars.get(localVar.getIdentifier());
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
right.codeGen(mv, localVars, typeContext, methodContext);
switch (type) {
case "int", "char", "boolean":
mv.visitVarInsn(Opcodes.ISTORE, index);
@ -108,34 +118,40 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
// Duplicate the top of the stack as we'll need it for both the PUTFIELD and subsequent expressions
mv.visitInsn(Opcodes.DUP_X1);
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
right.codeGen(mv, localVars, typeContext, methodContext);
// Get the field information
String fieldType = typeContext.get(instVar.thisClass).get(instVar.fieldName);
StringBuilder descriptor = new StringBuilder();
// Get the field descriptor
String descriptor = getFieldDescriptor(fieldType, typeContext);
switch (fieldType) {
// Store the value in the field
mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.thisClass, instVar.fieldName, descriptor);
}
}
private String getFieldDescriptor(String type, HashMap<String, HashMap<String, String>> typeContext) {
StringBuilder descriptor = new StringBuilder();
switch (type) {
case "int":
descriptor.append("I");
break;
return "I";
case "boolean":
descriptor.append("Z");
break;
default:
return "Z";
case "char":
return "C";
default: {
String fullReturnType = typeContext.get(instVar.thisClass).get(instVar.fieldName);
// If it is a class reference replace the "." with "/" and return it
if (fieldType.contains(".")) fullReturnType = fullReturnType.replaceAll("\\.", "/");
if (type.contains(".")) fullReturnType = fullReturnType.replaceAll("\\.", "/");
if (fullReturnType != null) descriptor.append("L").append(fullReturnType).append(";");
break;
return descriptor.toString();
}
}
}
// Store the value in the field
mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.thisClass, instVar.fieldName, descriptor.toString());
}
}
@Override
public boolean equals(Object o) {