From 7b78cf847103dfe02fd45764c76fc78e99dfa93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Schr=C3=B6dter?= Date: Thu, 7 Apr 2016 12:19:42 +0200 Subject: [PATCH] Bugfix: Store and Load --- .../bytecode/DHBWInstructionFactory.java | 14 +++++++-- .../syntaxtree/FieldDeclaration.java | 2 -- .../syntaxtree/statement/Assign.java | 14 ++------- .../typeinference/TypeinferenceResults.java | 29 ------------------- test/bytecode/types/Overloading.jav | 10 +++++++ test/bytecode/types/WildcardTest.jav | 13 +++++++++ test/bytecode/types/WildcardTest.java | 28 ++++++++++++++++++ 7 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 test/bytecode/types/WildcardTest.jav create mode 100644 test/bytecode/types/WildcardTest.java diff --git a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java index f14f66c2..fb555342 100644 --- a/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java +++ b/src/de/dhbwstuttgart/bytecode/DHBWInstructionFactory.java @@ -2,6 +2,8 @@ package de.dhbwstuttgart.bytecode; import java.awt.List; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.bcel6.Constants; import org.apache.commons.bcel6.classfile.Attribute; @@ -32,6 +34,7 @@ public class DHBWInstructionFactory extends InstructionFactory{ private DHBWConstantPoolGen cp; private ClassGenerator cg; + private static Map storeIndexes = new HashMap<>(); public DHBWInstructionFactory(ClassGenerator cg, DHBWConstantPoolGen cp) { super(cg, cp); @@ -137,8 +140,15 @@ public class DHBWInstructionFactory extends InstructionFactory{ } public LocalVariableInstruction createLoad(org.apache.commons.bcel6.generic.Type bytecodeType, String variableName) { - int index = 1; //TODO: Hier muss ein Logger her, welcher aufzeichnet, an welcher Position welche Variable liegt. - return InstructionFactory.createLoad(bytecodeType, index); + return InstructionFactory.createLoad(bytecodeType, storeIndexes.get(variableName)); + } + + public Integer getStoreIndex(String variableName) { + if(!storeIndexes.containsKey(variableName)){ + storeIndexes.put(variableName, storeIndexes.size()+1); + } + + return storeIndexes.get(variableName); } public static Type createObjectType() { diff --git a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java index 7cc3c54b..f9c2c5cc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java +++ b/src/de/dhbwstuttgart/syntaxtree/FieldDeclaration.java @@ -183,8 +183,6 @@ public class FieldDeclaration extends Field{ if(wert != null){ il.append(this.wert.genByteCode(cg, rs)); - }else{ - //todo: default wert? } FieldInstruction putFieldInstruction = diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 9e1ade7c..9e863d01 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -17,6 +17,7 @@ import org.apache.commons.bcel6.generic.LSTORE; import de.dhbwstuttgart.typeinference.Menge; import de.dhbwstuttgart.bytecode.ClassGenerator; +import de.dhbwstuttgart.bytecode.DHBWInstructionFactory; import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.myexception.CTypeReconstructionException; import de.dhbwstuttgart.myexception.JVMCodeException; @@ -176,18 +177,12 @@ public class Assign extends Expr return ret; } - - public static int counterAssign = 0; //Zaehlvariable f�r ISTORE - @Override public InstructionList genByteCode(ClassGenerator cg, TypeinferenceResultSet rs) { - // TODO Auto-generated method stub - InstructionFactory _factory = new InstructionFactory(cg, cg.getConstantPool()); + DHBWInstructionFactory _factory = new DHBWInstructionFactory(cg, cg.getConstantPool()); InstructionList il = expr2.genByteCode(cg, rs);//expr2 rechte expr - - /* String expr2Type = expr2.getType().get_Name().toString(); switch(expr2Type){ @@ -213,10 +208,7 @@ public class Assign extends Expr } */ //Es wird momentan immer von RefType ausgegangen: - il.append(new ASTORE(counterAssign)); - - //TODO: ^^ - counterAssign++; //macht STORE f�r meherere Variable nutzbar (nicht nur ISTORE_1, ISTORE_2, etc.) + il.append(new ASTORE(_factory.getStoreIndex(expr1.get_Name()))); return il; } diff --git a/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java index b2486c2c..5ce7a445 100644 --- a/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java +++ b/src/de/dhbwstuttgart/typeinference/TypeinferenceResults.java @@ -16,35 +16,6 @@ public class TypeinferenceResults { } public TypeinferenceResults(Menge typeReconstructions) { - //TODO: filter - - int limit = typeReconstructions.size(); - Logger.getLogger("TypeinferenceResults").error(new Integer(limit).toString(), Section.CODEGEN); - for(int i = 0; i < limit; i++){ - Logger.getLogger("TypeinferenceResults").error(new Integer(i).toString(), Section.CODEGEN); - - for(Pair p: typeReconstructions.get(i).getUnifiedConstraints()){ - boolean flag = false; - - Logger.getLogger("TypeinferenceResults").error(p.toString(), Section.CODEGEN); - - if( p.TA1 instanceof SuperWildcardType || - p.TA1 instanceof ExtendsWildcardType || - p.TA2 instanceof SuperWildcardType || - p.TA2 instanceof ExtendsWildcardType || - flag){ - - Logger.getLogger("TypeinferenceResults").error("remove", Section.CODEGEN); - - typeReconstructions.remove(i); - i--; - limit--; - flag = true; - } - } - } - - this.typeReconstructions = typeReconstructions; } diff --git a/test/bytecode/types/Overloading.jav b/test/bytecode/types/Overloading.jav index ce94fb7d..57206bdd 100644 --- a/test/bytecode/types/Overloading.jav +++ b/test/bytecode/types/Overloading.jav @@ -9,4 +9,14 @@ class Overloading{ void method(Vector v) { } + + main(String[] args) { + ol; + ol = new Overloading(); + v; + v = new Vector (); + ol.method(v); + + } + } \ No newline at end of file diff --git a/test/bytecode/types/WildcardTest.jav b/test/bytecode/types/WildcardTest.jav new file mode 100644 index 00000000..f029c2d0 --- /dev/null +++ b/test/bytecode/types/WildcardTest.jav @@ -0,0 +1,13 @@ +import java.util.Vector; + +class SuperType{ + Vector numberVector; + + void method() { + method(numberVector); + } + + void method(Vector v) { + + } +} \ No newline at end of file diff --git a/test/bytecode/types/WildcardTest.java b/test/bytecode/types/WildcardTest.java new file mode 100644 index 00000000..6dd41df8 --- /dev/null +++ b/test/bytecode/types/WildcardTest.java @@ -0,0 +1,28 @@ +package bytecode.types; + +import static org.junit.Assert.*; + + +import org.junit.Test; + + +import bytecode.SourceFileBytecodeTest; + + +public class WildcardTest extends SourceFileBytecodeTest{ + @Override + protected void init() { + testName = "Wildcard"; + rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/"; + } + + @Test + public void testConstruct() throws Exception{ + ClassLoader classLoader = getClassLoader(); + + Class cls = classLoader.loadClass(testName); + + Object obj = cls.newInstance(); + assertTrue(true); + } +}