* @throws CodeGenerationException
*/
static JavaExpression getPrimOpDefinition(int code, JavaExpression[] args) throws CodeGenerationException {
switch (code) {
case PrimOps.PRIMOP_NOP:
throw new CodeGenerationException ("Attempt to generate code for PRIMOP_NOP.");
case PrimOps.PRIMOP_OR:
return new OperatorExpression.Binary(JavaOperator.CONDITIONAL_OR, args);
case PrimOps.PRIMOP_AND:
return new OperatorExpression.Binary(JavaOperator.CONDITIONAL_AND, args);
case PrimOps.PRIMOP_EQUALS_INT:
return new OperatorExpression.Binary(JavaOperator.EQUALS_INT, args);
case PrimOps.PRIMOP_NOT_EQUALS_INT:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_INT, args);
case PrimOps.PRIMOP_GREATER_THAN_INT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_INT, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_INT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_INT, args);
case PrimOps.PRIMOP_LESS_THAN_INT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_INT, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_INT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_INT, args);
case PrimOps.PRIMOP_ADD_INT:
return new OperatorExpression.Binary(JavaOperator.ADD_INT, args);
case PrimOps.PRIMOP_SUBTRACT_INT:
return new OperatorExpression.Binary(JavaOperator.SUBTRACT_INT, args);
case PrimOps.PRIMOP_MULTIPLY_INT:
return new OperatorExpression.Binary(JavaOperator.MULTIPLY_INT, args);
case PrimOps.PRIMOP_DIVIDE_INT:
return new OperatorExpression.Binary(JavaOperator.DIVIDE_INT, args);
case PrimOps.PRIMOP_ADD_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.ADD_DOUBLE, args);
case PrimOps.PRIMOP_SUBTRACT_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.SUBTRACT_DOUBLE, args);
case PrimOps.PRIMOP_MULTIPLY_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.MULTIPLY_DOUBLE, args);
case PrimOps.PRIMOP_DIVIDE_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.DIVIDE_DOUBLE, args);
case PrimOps.PRIMOP_EQUALS_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.EQUALS_DOUBLE, args);
case PrimOps.PRIMOP_NOT_EQUALS_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_DOUBLE, args);
case PrimOps.PRIMOP_GREATER_THAN_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_DOUBLE, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_DOUBLE, args);
case PrimOps.PRIMOP_LESS_THAN_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_DOUBLE, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_DOUBLE, args);
case PrimOps.PRIMOP_ADD_FLOAT:
return new OperatorExpression.Binary(JavaOperator.ADD_FLOAT, args);
case PrimOps.PRIMOP_SUBTRACT_FLOAT:
return new OperatorExpression.Binary(JavaOperator.SUBTRACT_FLOAT, args);
case PrimOps.PRIMOP_MULTIPLY_FLOAT:
return new OperatorExpression.Binary(JavaOperator.MULTIPLY_FLOAT, args);
case PrimOps.PRIMOP_DIVIDE_FLOAT:
return new OperatorExpression.Binary(JavaOperator.DIVIDE_FLOAT, args);
case PrimOps.PRIMOP_EQUALS_FLOAT:
return new OperatorExpression.Binary(JavaOperator.EQUALS_FLOAT, args);
case PrimOps.PRIMOP_NOT_EQUALS_FLOAT:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_FLOAT, args);
case PrimOps.PRIMOP_GREATER_THAN_FLOAT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_FLOAT, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_FLOAT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_FLOAT, args);
case PrimOps.PRIMOP_LESS_THAN_FLOAT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_FLOAT, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_FLOAT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_FLOAT, args);
case PrimOps.PRIMOP_ADD_LONG:
return new OperatorExpression.Binary(JavaOperator.ADD_LONG, args);
case PrimOps.PRIMOP_SUBTRACT_LONG:
return new OperatorExpression.Binary(JavaOperator.SUBTRACT_LONG, args);
case PrimOps.PRIMOP_MULTIPLY_LONG:
return new OperatorExpression.Binary(JavaOperator.MULTIPLY_LONG, args);
case PrimOps.PRIMOP_DIVIDE_LONG:
return new OperatorExpression.Binary(JavaOperator.DIVIDE_LONG, args);
case PrimOps.PRIMOP_EQUALS_LONG:
return new OperatorExpression.Binary(JavaOperator.EQUALS_LONG, args);
case PrimOps.PRIMOP_NOT_EQUALS_LONG:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_LONG, args);
case PrimOps.PRIMOP_GREATER_THAN_LONG:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_LONG, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_LONG:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_LONG, args);
case PrimOps.PRIMOP_LESS_THAN_LONG:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_LONG, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_LONG:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_LONG, args);
case PrimOps.PRIMOP_EQUALS_SHORT:
return new OperatorExpression.Binary(JavaOperator.EQUALS_SHORT, args);
case PrimOps.PRIMOP_NOT_EQUALS_SHORT:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_SHORT, args);
case PrimOps.PRIMOP_GREATER_THAN_SHORT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_SHORT, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_SHORT:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_SHORT, args);
case PrimOps.PRIMOP_LESS_THAN_SHORT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_SHORT, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_SHORT:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_SHORT, args);
case PrimOps.PRIMOP_EQUALS_BYTE:
return new OperatorExpression.Binary(JavaOperator.EQUALS_BYTE, args);
case PrimOps.PRIMOP_NOT_EQUALS_BYTE:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_BYTE, args);
case PrimOps.PRIMOP_GREATER_THAN_BYTE:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_BYTE, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_BYTE:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_BYTE, args);
case PrimOps.PRIMOP_LESS_THAN_BYTE:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_BYTE, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_BYTE:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_BYTE, args);
case PrimOps.PRIMOP_EQUALS_CHAR:
return new OperatorExpression.Binary(JavaOperator.EQUALS_CHAR, args);
case PrimOps.PRIMOP_NOT_EQUALS_CHAR:
return new OperatorExpression.Binary(JavaOperator.NOT_EQUALS_CHAR, args);
case PrimOps.PRIMOP_GREATER_THAN_CHAR:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_CHAR, args);
case PrimOps.PRIMOP_GREATER_THAN_EQUALS_CHAR:
return new OperatorExpression.Binary(JavaOperator.GREATER_THAN_EQUALS_CHAR, args);
case PrimOps.PRIMOP_LESS_THAN_CHAR:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_CHAR, args);
case PrimOps.PRIMOP_LESS_THAN_EQUALS_CHAR:
return new OperatorExpression.Binary(JavaOperator.LESS_THAN_EQUALS_CHAR, args);
case PrimOps.PRIMOP_NEGATE_INT:
return new OperatorExpression.Unary(JavaOperator.NEGATE_INT, args[0]);
case PrimOps.PRIMOP_REMAINDER_INT:
return new OperatorExpression.Binary(JavaOperator.REM_INT, args);
case PrimOps.PRIMOP_NEGATE_DOUBLE:
return new OperatorExpression.Unary(JavaOperator.NEGATE_DOUBLE, args[0]);
case PrimOps.PRIMOP_REMAINDER_DOUBLE:
return new OperatorExpression.Binary(JavaOperator.REM_DOUBLE, args);
case PrimOps.PRIMOP_NEGATE_FLOAT:
return new OperatorExpression.Unary(JavaOperator.NEGATE_FLOAT, args[0]);
case PrimOps.PRIMOP_REMAINDER_FLOAT:
return new OperatorExpression.Binary(JavaOperator.REM_FLOAT, args);
case PrimOps.PRIMOP_NEGATE_LONG:
return new OperatorExpression.Unary(JavaOperator.NEGATE_LONG, args[0]);
case PrimOps.PRIMOP_REMAINDER_LONG:
return new OperatorExpression.Binary(JavaOperator.REM_LONG, args);
case PrimOps.PRIMOP_FIELD_NAMES:
{
//((RTRecordValue)args[0]).fieldNames()
return new MethodInvocation.Instance(new CastExpression(JavaTypeNames.RTRECORD_VALUE, args[0]),
"fieldNames", JavaTypeName.LIST, InvocationType.VIRTUAL);
}
case PrimOps.PRIMOP_FIELD_VALUES:
{
//((RTRecordValue)args[0]).fieldValues()
return new MethodInvocation.Instance(new CastExpression(JavaTypeNames.RTRECORD_VALUE, args[0]),
"fieldValues", JavaTypeName.LIST, InvocationType.VIRTUAL);
}
case PrimOps.PRIMOP_HAS_FIELD:
{
//((RTRecordValue)args[0]).hasField(args[1])
return new MethodInvocation.Instance(new CastExpression(JavaTypeNames.RTRECORD_VALUE, args[0]), "hasField",
args[1], JavaTypeName.STRING, JavaTypeName.BOOLEAN, InvocationType.VIRTUAL);
}
case PrimOps.PRIMOP_RECORD_FIELD_INDEX:
{
//((RTRecordValue)args[0]).indexOfField(args[1])
return new MethodInvocation.Instance(
new CastExpression(JavaTypeNames.RTRECORD_VALUE, args[0]),
"indexOfField", args[1], JavaTypeName.STRING, JavaTypeName.INT, InvocationType.VIRTUAL);
}
case PrimOps.PRIMOP_CAL_VALUE_TO_OBJECT:
{
return new JavaExpression.MethodInvocation.Static (JavaTypeNames.RTDATA_OPAQUE,
"make",
args[0],
JavaTypeName.OBJECT,
JavaTypeNames.RTDATA_OPAQUE);
}
case PrimOps.PRIMOP_OBJECT_TO_CAL_VALUE:
{
//note that the call to evaluate is necessary because the code generator makes some assumptions that all PrimOps produce
//a value in weak-head normal form.
//((RTValue) args[0]).evaluate($ec);
return SCJavaDefn.createInvocation(new JavaExpression.CastExpression(JavaTypeNames.RTVALUE, args[0]), SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
}
case PrimOps.PRIMOP_MAKE_ITERATOR:
{
//new RTCalListIterator(args[0], args[1], $ec)
JavaExpression[] arguments = new JavaExpression[] {args[0], args[1], new MethodVariable(SCJavaDefn.EXECUTION_CONTEXT_NAME)};
JavaTypeName[] argTypes = new JavaTypeName[] {JavaTypeNames.RTVALUE, JavaTypeNames.RTVALUE, JavaTypeNames.RTEXECUTION_CONTEXT};
return new JavaExpression.ClassInstanceCreationExpression(JavaTypeNames.RTCAL_LIST_ITERATOR, arguments, argTypes);
}
case PrimOps.PRIMOP_MAKE_COMPARATOR:
{
//new RTComparator(args[0], $ec)
JavaExpression[] arguments = new JavaExpression[] {args[0], new MethodVariable(SCJavaDefn.EXECUTION_CONTEXT_NAME)};
JavaTypeName[] argTypes = new JavaTypeName[] {JavaTypeNames.RTVALUE, JavaTypeNames.RTEXECUTION_CONTEXT};
return new JavaExpression.ClassInstanceCreationExpression(JavaTypeNames.RTCOMPARATOR, arguments, argTypes);
}
case PrimOps.PRIMOP_MAKE_EQUIVALENCE_RELATION:
{
//new RTEquivalenceRelation(args[0], $ec)
JavaExpression[] arguments = new JavaExpression[] {args[0], new MethodVariable(SCJavaDefn.EXECUTION_CONTEXT_NAME)};
JavaTypeName[] argTypes = new JavaTypeName[] {JavaTypeNames.RTVALUE, JavaTypeNames.RTEXECUTION_CONTEXT};
return new JavaExpression.ClassInstanceCreationExpression(JavaTypeNames.RTEQUIVALENCE_RELATION, arguments, argTypes);
}
case PrimOps.PRIMOP_MAKE_CAL_FUNCTION:
{
//new RTCalFunction(args[0], $ec)
JavaExpression[] arguments = new JavaExpression[] {args[0], new MethodVariable(SCJavaDefn.EXECUTION_CONTEXT_NAME)};
JavaTypeName[] argTypes = new JavaTypeName[] {JavaTypeNames.RTVALUE, JavaTypeNames.RTEXECUTION_CONTEXT};
return new JavaExpression.ClassInstanceCreationExpression(JavaTypeNames.RTCAL_FUNCTION, arguments, argTypes);
}
case PrimOps.PRIMOP_BITWISE_AND_INT: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_AND_INT, args);
}
case PrimOps.PRIMOP_BITWISE_OR_INT: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_OR_INT, args);
}
case PrimOps.PRIMOP_BITWISE_XOR_INT: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_XOR_INT, args);
}
case PrimOps.PRIMOP_COMPLEMENT_INT: {
return new OperatorExpression.Unary (JavaOperator.COMPLEMENT_INT, args[0]);
}
case PrimOps.PRIMOP_SHIFTL_INT: {
return new OperatorExpression.Binary (JavaOperator.SHIFTL_INT, args);
}
case PrimOps.PRIMOP_SHIFTR_INT: {
return new OperatorExpression.Binary (JavaOperator.SHIFTR_INT, args);
}
case PrimOps.PRIMOP_SHIFTR_UNSIGNED_INT: {
return new OperatorExpression.Binary (JavaOperator.SHIFTR_UNSIGNED_INT, args);
}
case PrimOps.PRIMOP_BITWISE_AND_LONG: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_AND_LONG, args);
}
case PrimOps.PRIMOP_BITWISE_OR_LONG: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_OR_LONG, args);
}
case PrimOps.PRIMOP_BITWISE_XOR_LONG: {
return new OperatorExpression.Binary (JavaOperator.BITWISE_XOR_LONG, args);
}
case PrimOps.PRIMOP_COMPLEMENT_LONG: {
return new OperatorExpression.Unary (JavaOperator.COMPLEMENT_LONG, args[0]);
}
case PrimOps.PRIMOP_SHIFTL_LONG: {
return new OperatorExpression.Binary (JavaOperator.SHIFTL_LONG, args);
}
case PrimOps.PRIMOP_SHIFTR_LONG: {
return new OperatorExpression.Binary (JavaOperator.SHIFTR_LONG, args);
}
case PrimOps.PRIMOP_SHIFTR_UNSIGNED_LONG: {
return new OperatorExpression.Binary (JavaOperator.SHIFTR_UNSIGNED_LONG, args);
}
case PrimOps.PRIMOP_EXECUTION_CONTEXT:
{
//reference to the "$ec" execution context variable
return SCJavaDefn.EXECUTION_CONTEXT_VAR;
}
// This is now handled higher up since more context information is needed
// in order to add the source position to the error message.
//
default:
{
throw new CodeGenerationException("Unrecognized PrimOp: " + code + ".");
}
}
}