01: package gnu.kawa.functions;
02:
03: import gnu.bytecode.*;
04: import gnu.mapping.*;
05: import gnu.expr.*;
06:
07: /** Implement the standard Scheme function <tt>eq?</tt>
08: * and the Lisp <tt>eq</tt>. */
09:
10: public class IsEq extends Procedure2 implements Inlineable {
11: Language language;
12:
13: public IsEq(Language language, String name) {
14: this .language = language;
15: setName(name);
16: }
17:
18: public boolean apply(Object arg1, Object arg2) {
19: return arg1 == arg2;
20: }
21:
22: public Object apply2(Object arg1, Object arg2) {
23: return language.booleanObject(arg1 == arg2);
24: }
25:
26: public void compile(ApplyExp exp, Compilation comp, Target target) {
27: compile(exp.getArgs(), comp, target, language);
28: }
29:
30: public static void compile(Expression[] args, Compilation comp,
31: Target target, Language language) {
32: CodeAttr code = comp.getCode();
33: args[0].compile(comp, Target.pushObject);
34: args[1].compile(comp, Target.pushObject);
35: if (target instanceof ConditionalTarget) {
36: ConditionalTarget ctarget = (ConditionalTarget) target;
37: if (ctarget.trueBranchComesFirst)
38: code.emitGotoIfNE(ctarget.ifFalse);
39: else
40: code.emitGotoIfEq(ctarget.ifTrue);
41: ctarget.emitGotoFirstBranch(code);
42: } else {
43: Type type;
44: code.emitIfEq();
45: if (target.getType() instanceof ClassType) {
46: Object trueValue = language.booleanObject(true);
47: Object falseValue = language.booleanObject(false);
48: comp.compileConstant(trueValue, Target.pushObject);
49: code.emitElse();
50: comp.compileConstant(falseValue, Target.pushObject);
51: if (trueValue instanceof Boolean
52: && falseValue instanceof Boolean)
53: type = Compilation.scmBooleanType;
54: else
55: type = Type.pointer_type;
56: } else {
57: code.emitPushInt(1);
58: code.emitElse();
59: code.emitPushInt(0);
60: type = language.getTypeFor(Boolean.TYPE);
61: }
62: code.emitFi();
63: target.compileFromStack(comp, type);
64: }
65: }
66:
67: public Type getReturnType(Expression[] args) {
68: return language.getTypeFor(Boolean.TYPE);
69: }
70: }
|