JavaPatternMatching/src/de/dhbwstuttgart/syntaxtree/operator/LogOp.java

241 lines
9.3 KiB
Java
Raw Normal View History

2013-10-18 11:33:46 +00:00
// ino.module.LogOp.8602.package
2014-09-02 08:33:54 +00:00
package de.dhbwstuttgart.syntaxtree.operator;
2013-10-18 11:33:46 +00:00
// ino.end
// ino.module.LogOp.8602.import
import java.util.HashMap;
2013-10-18 11:33:46 +00:00
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
2014-04-23 15:59:39 +00:00
2014-09-04 14:35:44 +00:00
import de.dhbwstuttgart.bytecode.ClassFile;
import de.dhbwstuttgart.bytecode.CodeAttribute;
import de.dhbwstuttgart.bytecode.JVMCode;
import de.dhbwstuttgart.myexception.CTypeReconstructionException;
import de.dhbwstuttgart.myexception.JVMCodeException;
2014-09-02 08:33:54 +00:00
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.Expr;
import de.dhbwstuttgart.syntaxtree.statement.NotExpr;
import de.dhbwstuttgart.syntaxtree.statement.Null;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.BooleanType;
import de.dhbwstuttgart.syntaxtree.type.Pair;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.Type;
import de.dhbwstuttgart.typeinference.ConstraintsSet;
import de.dhbwstuttgart.typeinference.SingleConstraint;
import de.dhbwstuttgart.typeinference.assumptions.TypeAssumptions;
import de.dhbwstuttgart.typeinference.exceptions.DebugException;
2014-09-02 09:07:16 +00:00
import de.dhbwstuttgart.typeinference.unify.Unify;
2013-10-18 11:33:46 +00:00
// ino.class.LogOp.24186.declaration
public abstract class LogOp extends Operator
// ino.end
// ino.class.LogOp.24186.body
{
// ino.method.LogOp.24190.definition
public LogOp(int offset, int variableLength)
// ino.end
// ino.method.LogOp.24190.body
{
super(offset, variableLength);
}
// ino.end
// ino.method.codegen.24193.definition
public void codegen(ClassFile classfile, CodeAttribute code, Expr expr, boolean not, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.codegen.24193.body
{
Vector<Integer> indices = new Vector<Integer>();
Operator op = ((Binary)expr).get_Operator();
Expr expr1 = ((Binary)expr).get_Expr1();
Expr expr2 = ((Binary)expr).get_Expr2();
boolean and;
if(op instanceof OrOp) and = false;
else if(op instanceof AndOp) and = true;
else throw new JVMCodeException("JVMCodeException: LogOp: void codegen(ClassFile classfile, Code_attribute code, Expr expr, boolean not)");
build(classfile, code, expr1, indices, not, false, and, paralist);
build(classfile, code, expr2, indices, not, true, and, paralist);
code.add_code(JVMCode.iconst_1);
code.add_code(JVMCode.goto_);
code.add_code_short(4);
code.add_code(JVMCode.iconst_0);
int code_length = code.get_code_length();
replace_index(code, indices, code_length - 1, code_length - 5);
}
// ino.end
// ino.method.if_codegen.24196.definition
public void if_codegen(ClassFile classfile, CodeAttribute code, boolean not, Expr expr, Statement then_block, Statement else_block, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.if_codegen.24196.body
{
Vector<Integer> indices = new Vector<Integer>();
Operator op = ((Binary)expr).get_Operator();
Expr expr1 = ((Binary)expr).get_Expr1();
Expr expr2 = ((Binary)expr).get_Expr2();
boolean and;
if(op instanceof OrOp) and = false;
else if(op instanceof AndOp) and = true;
else throw new JVMCodeException("JVMCodeException: LogOp: if_codegen(ClassFile classfile, Code_attribute code, boolean not, Expr expr, Statement then_block, Statement else_block)");
build(classfile, code, expr1, indices, not, false, and, paralist);
build(classfile, code, expr2, indices, not, true, and, paralist);
int breakpoint1 = code.get_code_length();
if(then_block != null) then_block.codegen(classfile, code, paralist);
int breakpoint2 = code.get_code_length();
if(else_block != null) {
code.add_code(JVMCode.goto_);
breakpoint2 = code.get_code_length();
code.add_code_short(0);
else_block.codegen(classfile, code, paralist);
code.set_code_short(code.get_code_length() + 1 - breakpoint2, breakpoint2);
}
replace_index(code, indices, breakpoint2 + 2 , breakpoint1);
}
// ino.end
// ino.method.loop_codegen.24199.definition
public void loop_codegen(ClassFile classfile, CodeAttribute code, Expr expr, int breakpoint, boolean not, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.loop_codegen.24199.body
{
Vector<Integer> indices = new Vector<Integer>();
Operator op = ((Binary)expr).get_Operator();
Expr expr1 = ((Binary)expr).get_Expr1();
Expr expr2 = ((Binary)expr).get_Expr2();
boolean and;
if(op instanceof OrOp) and = false;
else if(op instanceof AndOp) and = true;
else throw new JVMCodeException("JVMCodeException: LogOp: void loop_codegen(ClassFile classfile, Code_attribute code, Expr expr, int breakpoint, boolean not)");
build(classfile, code, expr1, indices, not, false, and, paralist);
build(classfile, code, expr2, indices, not, true, and, paralist);
replace_index(code, indices, breakpoint + 2, code.get_code_length());
}
// ino.end
// ino.method.build.24202.definition
public static void build(ClassFile classfile, CodeAttribute code, Expr expr, Vector<Integer> indices, boolean not, boolean ex2, boolean and, Vector paralist)
throws JVMCodeException
// ino.end
// ino.method.build.24202.body
{
if(expr instanceof NotExpr)
build(classfile, code, ((NotExpr)expr).get_Expr(), indices, !not, ex2, and, paralist);
else if(expr instanceof Binary) {
Operator op = ((Binary)expr).get_Operator();
Expr expr1 = ((Binary)expr).get_Expr1();
Expr expr2 = ((Binary)expr).get_Expr2();
if(op instanceof LogOp) {
boolean and2;
if(op instanceof OrOp) and2 = false;
else if(op instanceof AndOp) and2 = true;
else throw new JVMCodeException("JVMCodeException: LogOp: void build(ClassFile classfile, Code_attribute code, Expr expr, Vector indices, boolean not, boolean ex2, boolean and)");
if((and != and2) && !ex2) {
Vector<Integer> indices2 = new Vector<Integer>();
build(classfile, code, expr1, indices2, not, false, and2, paralist);
build(classfile, code, expr2, indices, not, ex2, and, paralist);
int breakpoint = code.get_code_length();
replace_index(code, indices2, breakpoint, breakpoint);
}
else {
build(classfile, code, expr1, indices, not, false, and2, paralist);
build(classfile, code, expr2, indices, not, ex2, and, paralist);
}
}
else if(op instanceof RelOp) {
expr1.codegen(classfile, code, paralist);
expr2.codegen(classfile, code, paralist);
if(expr1 instanceof Null || expr2 instanceof Null) ((RelOp)op).if_codegen(classfile, code, "null", not);
else ((RelOp)op).if_codegen(classfile, code, expr.getTypeName(), not);
indices.addElement(new Integer(code.get_code_length()));
if(and) {
if(not && !ex2) code.add_code_short(1);
else code.add_code_short(0);
}
else {
if(not || ex2) code.add_code_short(0);
else code.add_code_short(1);
}
}
else throw new JVMCodeException("JVMCodeException: LogOp: void build(ClassFile classfile, Code_attribute code, Expr expr, Vector indices, boolean not, boolean ex2, boolean and)");
}
else {
expr.codegen(classfile, code, paralist);
indices.addElement(new Integer(code.get_code_length() + 1));
if(and) {
if(not && ex2) code.add_code(JVMCode.ifne);
else code.add_code(JVMCode.ifeq);
if(not && !ex2) code.add_code_short(1);
else code.add_code_short(0);
}
else {
if(!not && ex2) code.add_code(JVMCode.ifeq);
else code.add_code(JVMCode.ifne);
if(not || ex2) code.add_code_short(0);
else code.add_code_short(1);
}
}
}
// ino.end
// ino.method.replace_index.24205.definition
public static void replace_index(CodeAttribute code, Vector<Integer> indices, int else_index, int then_index)
throws JVMCodeException
// ino.end
// ino.method.replace_index.24205.body
{
Vector b = code.get_code_Vector();
for(int i = 0; i < indices.size(); i++) {
int y = indices.elementAt(i).intValue();
int high = JVMCode.bytes_to_int((Byte)b.elementAt(y));
int low = JVMCode.bytes_to_int((Byte)b.elementAt(y+1));
int x = (high << 8) + low;
if(x == 0) code.set_code_short(else_index + 1 - y, y);
else if(x == 1) code.set_code_short(then_index + 1 - y, y);
else throw new JVMCodeException("JVMCodeException: LogOp: replace_index(Code_attribute code, Vector indices, int else_index, int then_index)");
}
}
// ino.end
@Override
protected Hashtable<RefType, RefType> getOperatorTypes() {
Hashtable<RefType, RefType> types = new Hashtable<RefType, RefType>();
types.put(new RefType("java.lang.Boolean",-1), new RefType("java.lang.Boolean",-1));
return types;
}
@Override
public HashMap<Type,Type> getReturnTypes(TypeAssumptions ass) {
HashMap<Type,Type> ret = new HashMap<Type,Type>();
2014-08-11 12:35:33 +00:00
ret.put(ass.getTypeFor(new RefType("java.lang.Boolean",-1), this), ass.getTypeFor(new RefType("java.lang.Boolean",-1), this));
2014-04-23 15:59:39 +00:00
return ret;
}
2013-10-18 11:33:46 +00:00
}
// ino.end