package edu.mit.streamjit.impl.compiler.insts;

import static com.google.common.base.Preconditions.*;
import edu.mit.streamjit.impl.compiler.Field;
import edu.mit.streamjit.impl.compiler.Value;
import edu.mit.streamjit.impl.compiler.types.InstanceFieldType;
import edu.mit.streamjit.impl.compiler.types.StaticFieldType;

/**
 * Loads a static or instance field.  (Does not load array elements.)
 * @author Jeffrey Bosboom <jeffreybosboom@gmail.com>
 * @since 4/14/2013
 */
public class LoadInst extends Instruction {
	public LoadInst(Field f) {
		super(checkNotNull(f).getType().getFieldType(), f.isStatic() ? 1 : 2);
		setOperand(0, f);
	}
	public LoadInst(Field f, Value v) {
		this(f);
		setOperand(1, v);
	}

	public Field getField() {
		return (Field)getOperand(0);
	}
	public void setField(Field f) {
		setOperand(0, f);
	}
	public Value getInstance() {
		return getOperand(1);
	}
	public void setInstance(Value v) {
		setOperand(1, v);
	}

	@Override
	protected void checkOperand(int i, Value v) {
		checkElementIndex(i, 2);
		if (i == 0) {
			checkArgument(v instanceof Field);
			checkArgument(((Field)v).isStatic() == getType() instanceof StaticFieldType);
		} else if (i == 1) {
			checkState(getType() instanceof InstanceFieldType);
			checkArgument(v.getType().isSubtypeOf(((InstanceFieldType)getField().getType()).getInstanceType()));
		}
		super.checkOperand(i, v);
	}
}

@@ -1179,24 +1179,70 @@
  (i == 1) %7B%0A
+%09%09%09Field f = getField();%0A%09%09%09if (f != null) %7B%0A%09
 %09%09%09checkStat
@@ -1243,16 +1243,18 @@
 ckState(
+f.
 getType(
@@ -1286,16 +1286,17 @@
 dType);%0A
+%09
 %09%09%09check
@@ -1348,26 +1348,17 @@
 eldType)
-getField()
+f
 .getType
@@ -1382,16 +1382,21 @@
 pe()));%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09su

fix check again