if(value instanceof Expression)
from=((Expression)value).writeOut(bc, mode);
else {
Integer var=db.locals.get(value);
if(var==null)
throw new BytecodeException("there is no variable with name ["+value+"] in the enviroment", line);
from=bc.getAdapter().getLocalType(var.intValue());
bc.getAdapter().loadLocal(var.intValue(),from);
}
if(to!=null && !from.equals(to)){
boolean isRefFrom = ASMUtil.isRefType(from);
boolean isRefTo = ASMUtil.isRefType(to);
if(castExplicit) {
Class fc=null,tc=null;
if(!isRefFrom && !isRefTo){
fc = ASMUtil.getValueTypeClass(from,null);
tc = ASMUtil.getValueTypeClass(to,null);
}
else {
try {
fc = ClassUtil.loadClass(from.getClassName());
tc = ClassUtil.loadClass(to.getClassName());
}
catch (ClassException e) {
throw new BytecodeException(e, line);
}
}
if(((tc==boolean.class && fc!=boolean.class))||(fc==boolean.class && tc!=boolean.class))
throw new BytecodeException("cannot cast from ["+fc.getName()+"] to ["+tc.getName()+"]", line);
bc.getAdapter().cast(from, to);
}
else {
// unbox
if(isRefFrom && !isRefTo){
bc.getAdapter().unbox(to);
from=ASMUtil.toValueType(from);
isRefFrom=false;
}
// box
else if(!isRefFrom && isRefTo){
bc.getAdapter().box(from);
from=ASMUtil.toRefType(from);
isRefFrom=true;
}
// value types
if(!isRefFrom && !isRefTo){
Class fc = ASMUtil.getValueTypeClass(from,null);
Class tc = ASMUtil.getValueTypeClass(to,null);
if(Reflector.canConvert(fc, tc))
bc.getAdapter().cast(from, to);
else {
boolean doThrow=true;
if(value instanceof LitDouble){
double d=((LitDouble)value).getDoubleValue();
if(canConvert(d, tc)){
bc.getAdapter().cast(from, to);
doThrow=false;
}
}
if(value instanceof LitFloat){
float f=((LitFloat)value).getFloatValue();
if(canConvert(f, tc)){
bc.getAdapter().cast(from, to);
doThrow=false;
}
}
if(value instanceof LitInteger){
int i=((LitInteger)value).geIntValue();
if(canConvert(i, tc)){
bc.getAdapter().cast(from, to);
doThrow=false;
}
}
if(doThrow)throw new BytecodeException("cannot convert from ["+fc.getName()+"] to ["+tc.getName()+"]", line);
}
}
// ref types
else {
try {
Class fc = ClassUtil.loadClass(from.getClassName());
Class tc = ClassUtil.loadClass(to.getClassName());
if(value instanceof NullExpression || Reflector.isInstaneOf(fc, tc))
bc.getAdapter().cast(from, to);
else
throw new BytecodeException("cannot convert from ["+fc.getName()+"] to ["+tc.getName()+"]", line);
}
catch (ClassException e) {
throw new BytecodeException(e, line);
}
}
}
}
return from;