From a541b353cd8609ce97811a8caafa8ea2f7ceab4b Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 15 Oct 2015 16:15:13 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=C3=84nderungen=20an=20IFStmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BCEL/bcelifier/BooleanValue.java | 6 ++ BCEL/bcelifier/BooleanValueCreator.java | 63 +++++++++++++++++++ BCEL/bcelifier/JavaToBCEL.java | 1 + .../syntaxtree/statement/IfStmt.java | 2 + .../syntaxtree/type/RefType.java | 2 +- test/bytecode/IfElseStatement.jav | 4 +- test/bytecode/IfTest.java | 9 +++ 7 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 BCEL/bcelifier/BooleanValue.java create mode 100644 BCEL/bcelifier/BooleanValueCreator.java create mode 100644 test/bytecode/IfTest.java diff --git a/BCEL/bcelifier/BooleanValue.java b/BCEL/bcelifier/BooleanValue.java new file mode 100644 index 00000000..f6859aeb --- /dev/null +++ b/BCEL/bcelifier/BooleanValue.java @@ -0,0 +1,6 @@ +package bcelifier; + +public class BooleanValue { + Boolean c = true; + Boolean b = c.booleanValue(); +} diff --git a/BCEL/bcelifier/BooleanValueCreator.java b/BCEL/bcelifier/BooleanValueCreator.java new file mode 100644 index 00000000..d019f948 --- /dev/null +++ b/BCEL/bcelifier/BooleanValueCreator.java @@ -0,0 +1,63 @@ +package bcelifier; + +import org.apache.commons.bcel6.generic.*; +import org.apache.commons.bcel6.classfile.*; +import org.apache.commons.bcel6.*; +import java.io.*; + +public class BooleanValueCreator implements Constants { + private InstructionFactory _factory; + private ConstantPoolGen _cp; + private ClassGen _cg; + + public BooleanValueCreator() { + _cg = new ClassGen("bcelifier.BooleanValue", "java.lang.Object", "BooleanValue.java", ACC_PUBLIC | ACC_SUPER, new String[] { }); + + _cp = _cg.getConstantPool(); + _factory = new InstructionFactory(_cg, _cp); + } + + public void create(OutputStream out) throws IOException { + createFields(); + createMethod_0(); + _cg.getJavaClass().dump(out); + } + + private void createFields() { + FieldGen field; + + field = new FieldGen(0, new ObjectType("java.lang.Boolean"), "c", _cp); + _cg.addField(field.getField()); + + field = new FieldGen(0, new ObjectType("java.lang.Boolean"), "b", _cp); + _cg.addField(field.getField()); + } + + private void createMethod_0() { + InstructionList il = new InstructionList(); + MethodGen method = new MethodGen(ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] { }, "", "bcelifier.BooleanValue", il, _cp); + + InstructionHandle ih_0 = il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(_factory.createInvoke("java.lang.Object", "", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL)); + InstructionHandle ih_4 = il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(new PUSH(_cp, 1)); + il.append(_factory.createInvoke("java.lang.Boolean", "valueOf", new ObjectType("java.lang.Boolean"), new Type[] { Type.BOOLEAN }, Constants.INVOKESTATIC)); + il.append(_factory.createFieldAccess("bcelifier.BooleanValue", "c", new ObjectType("java.lang.Boolean"), Constants.PUTFIELD)); + InstructionHandle ih_12 = il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(_factory.createLoad(Type.OBJECT, 0)); + il.append(_factory.createFieldAccess("bcelifier.BooleanValue", "c", new ObjectType("java.lang.Boolean"), Constants.GETFIELD)); + il.append(_factory.createInvoke("java.lang.Boolean", "booleanValue", Type.BOOLEAN, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); + il.append(_factory.createInvoke("java.lang.Boolean", "valueOf", new ObjectType("java.lang.Boolean"), new Type[] { Type.BOOLEAN }, Constants.INVOKESTATIC)); + il.append(_factory.createFieldAccess("bcelifier.BooleanValue", "b", new ObjectType("java.lang.Boolean"), Constants.PUTFIELD)); + InstructionHandle ih_26 = il.append(_factory.createReturn(Type.VOID)); + method.setMaxStack(); + method.setMaxLocals(); + _cg.addMethod(method.getMethod()); + il.dispose(); + } + + public static void main(String[] args) throws Exception { + bcelifier.BooleanValueCreator creator = new bcelifier.BooleanValueCreator(); + creator.create(new FileOutputStream("bcelifier.BooleanValue.class")); + } +} diff --git a/BCEL/bcelifier/JavaToBCEL.java b/BCEL/bcelifier/JavaToBCEL.java index f4281a3c..10c74c32 100644 --- a/BCEL/bcelifier/JavaToBCEL.java +++ b/BCEL/bcelifier/JavaToBCEL.java @@ -31,6 +31,7 @@ public class JavaToBCEL { new BCELifier(new ClassParser(rootDirectory+"Null.class").parse(), new FileOutputStream(new File(rootDirectory+"NullCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"LocalVarAccess.class").parse(), new FileOutputStream(new File(rootDirectory+"LocalVarAccessCreator.java"))).start(); new BCELifier(new ClassParser(rootDirectory+"Wildcard.class").parse(), new FileOutputStream(new File(rootDirectory+"WildcardCreator.java"))).start(); + new BCELifier(new ClassParser(rootDirectory+"BooleanValue.class").parse(), new FileOutputStream(new File(rootDirectory+"BooleanValueCreator.java"))).start(); } catch (ClassFormatException | IOException e) { e.printStackTrace(); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index a8eac123..5a7488c7 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -185,6 +185,8 @@ public class IfStmt extends Statement IfInstruction ifInstruction = new IFEQ(null); il.append(expr.genByteCode(_cg)); + //Anmerkung: expr ist vom Typ java.lang.Boolean (per Definition) + il.append(_factory.createInvoke("java.lang.Boolean", "booleanValue", org.apache.commons.bcel6.generic.Type.BOOLEAN, org.apache.commons.bcel6.generic.Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(ifInstruction); il.append(then_block.genByteCode(_cg)); diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 097ed4e4..5b96e961 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -814,7 +814,7 @@ public class RefType extends ObjectType implements IMatchable } public org.apache.commons.bcel6.generic.Type getBytecodeType(ClassGenerator cg) { - return new org.apache.commons.bcel6.generic.ObjectType(this.getTypeName()); + return new org.apache.commons.bcel6.generic.ObjectType(this.getTypeName()); } @Override diff --git a/test/bytecode/IfElseStatement.jav b/test/bytecode/IfElseStatement.jav index 82c9d5a8..ee4c5eee 100644 --- a/test/bytecode/IfElseStatement.jav +++ b/test/bytecode/IfElseStatement.jav @@ -1,9 +1,9 @@ class IfElseStatement{ method(flag){ if(flag){ - + return 0; }else{ - + return 1; } } } \ No newline at end of file diff --git a/test/bytecode/IfTest.java b/test/bytecode/IfTest.java new file mode 100644 index 00000000..1c2c1464 --- /dev/null +++ b/test/bytecode/IfTest.java @@ -0,0 +1,9 @@ +class IfTest{ + +public static void main(String[] args){ + + System.out.println(new IfElseStatement().method(true)); + System.out.println(new IfElseStatement().method(false)); +} + +} From 88b1c3e57c98587842996dbc55bac1fbc9f75b19 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 15 Oct 2015 16:28:10 +0200 Subject: [PATCH 2/2] =?UTF-8?q?WriteLog=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/bytecode/SingleClassTester.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/bytecode/SingleClassTester.java b/test/bytecode/SingleClassTester.java index d769fe98..bbee95d6 100644 --- a/test/bytecode/SingleClassTester.java +++ b/test/bytecode/SingleClassTester.java @@ -3,17 +3,21 @@ package bytecode; import java.io.File; import java.io.IOException; +import com.google.common.io.Files; + import junit.framework.TestCase; import de.dhbwstuttgart.core.MyCompiler; import de.dhbwstuttgart.core.MyCompilerAPI; +import de.dhbwstuttgart.logger.Logger; import de.dhbwstuttgart.logger.LoggerConfiguration; import de.dhbwstuttgart.logger.Section; +import de.dhbwstuttgart.logger.Timewatch; import de.dhbwstuttgart.parser.JavaParser.yyException; import de.dhbwstuttgart.typeinference.ByteCodeResult; import de.dhbwstuttgart.typeinference.Menge; public class SingleClassTester { - + public static void compileToBytecode(String inputFile, String outputFile){ LoggerConfiguration logConfig = new LoggerConfiguration().setOutput(Section.PARSER, System.out); MyCompilerAPI compiler = MyCompiler.getAPI(logConfig); @@ -25,7 +29,19 @@ public class SingleClassTester { } catch (IOException | yyException e) { e.printStackTrace(); TestCase.fail(); + }finally{ + writeLog(outputFile+".log"); } } + private static void writeLog(String toFile){ + String log = Logger.getWholeLog()+"\n"; + log+=Timewatch.getTimewatch().dumpTimeData(); + try { + Files.write(log.getBytes(),new File(toFile)); + } catch (IOException e) { + e.printStackTrace(); + TestCase.fail(); + } + } }