01: /**************************************************************************/
02: /* N I C E */
03: /* A simple imperative object-oriented research language */
04: /* (c) Daniel Bonniot 2000 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package nice.lang.inline;
12:
13: import gnu.mapping.Procedure2;
14: import gnu.expr.*;
15: import gnu.bytecode.*;
16:
17: /**
18: Inlining of native boolean operators.
19:
20: @author Daniel Bonniot
21: */
22: public class BoolOp extends Procedure2 implements Inlineable {
23: private final static int And = 1, Or = 2, Xor = 3, Eq = 4;
24:
25: public static BoolOp create(String param) {
26: int kind = 0;
27: if ("&".equals(param))
28: kind = And;
29: else if ("|".equals(param))
30: kind = Or;
31: else if ("^".equals(param))
32: kind = Xor;
33: else if ("==".equals(param))
34: kind = Eq;
35: else
36: bossa.util.User.error("Unknown inlined boolean operator "
37: + param);
38: return new BoolOp(kind);
39: }
40:
41: private BoolOp(int kind) {
42: this .kind = kind;
43: }
44:
45: private final int kind;
46:
47: public void compile(ApplyExp exp, Compilation comp, Target target) {
48: Expression[] args = exp.getArgs();
49: CodeAttr code = comp.getCode();
50: Target stack = new StackTarget(Type.boolean_type);
51:
52: args[0].compile(comp, stack);
53: args[1].compile(comp, stack);
54:
55: switch (kind) {
56: case And:
57: code.emitAnd();
58: break;
59: case Or:
60: code.emitIOr();
61: break;
62: case Xor:
63: code.emitXOr();
64: break;
65: case Eq:
66: code.emitXOr();
67: code.emitPushConstant(1, Type.int_type);
68: code.emitXOr();
69: break;
70: }
71:
72: target.compileFromStack(comp, retType);
73: }
74:
75: private static final Type retType = Type.boolean_type;
76:
77: public Type getReturnType(Expression[] args) {
78: return retType;
79: }
80:
81: // Interpretation
82:
83: public Object apply2(Object arg1, Object arg2) {
84: throw new Error("Not implemented");
85: }
86: }
|