* @param value the numeric value to push
* @param type the type of the expression that produced the value.
*/
public void pushNumericConstant(InstructionList result_list, Number value, IDefinition type)
{
ICompilerProject project = currentScope.getProject();
if( type == project.getBuiltinType(BuiltinType.INT) )
{
int val = ECMASupport.toInt32(value);
if( (byte)val == val )
{
result_list.addInstruction(OP_pushbyte, val);
}
else if( (short)val == val )
{
result_list.addInstruction(OP_pushshort, val);
}
else
{
result_list.addInstruction(OP_pushint, Integer.valueOf(val));
}
}
else if( type == project.getBuiltinType(BuiltinType.UINT) )
{
long uval = ECMASupport.toUInt32(value.doubleValue());
if ((uval & 0x7F) == uval) { // Pushbyte sign extends
result_list.addInstruction(OP_pushbyte, (int)uval);
}
else if ((uval & 0x7FFF) == uval) { // Pushshort sign extends
result_list.addInstruction(OP_pushshort, (int)uval);
}
else {
result_list.addInstruction(OP_pushuint, Long.valueOf(uval));
}
}
else
{
double dval = value.doubleValue();
if( ECMASupport.isNan(dval) )
{
result_list.addInstruction(OP_pushnan);
}
else if (!Double.isInfinite(dval) && (dval == ECMASupport.toInt32(value)) )
{
// distinguish pos/neg 0
// java treats -0 and 0 as equal, but divide by -0 results in NEG INFINITY, whereas pos
// 0 results in POS INFINITY
// positive 0 can be encoded with a pushbyte, but neg zero requires a pushdouble
if( dval == 0 && 1/dval == Double.NEGATIVE_INFINITY )
result_list.addInstruction(OP_pushdouble, dval);
else
// Integer
pushNumericConstant(result_list, ECMASupport.toInt32(value), project.getBuiltinType(BuiltinType.INT));
}
else if( Double.isNaN(dval) )
{
result_list.addInstruction(OP_pushnan);
}