01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2003 */
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: /**
14: Inlining of native unary numeric operators.
15:
16: @author Daniel Bonniot (bonniot@users.sourceforge.net)
17: */
18:
19: import gnu.mapping.Procedure1;
20: import gnu.expr.*;
21: import gnu.bytecode.*;
22:
23: public class UnaryNumOp extends Procedure1 implements Inlineable {
24: private final static int error = 0, Neg = 1, // negation (unary -)
25: Comp = 2; // bitwise complement (~)
26:
27: public static Procedure1 create(String param) {
28: PrimType type = Tools.numericType(param.charAt(0));
29: if (type == null)
30: bossa.util.User
31: .error("Unknown type in inlined numeric operator: "
32: + param);
33:
34: param = param.substring(1);
35:
36: int kind = error;
37: if ("Neg".equals(param))
38: kind = Neg;
39: else if ("Comp".equals(param))
40: kind = Comp;
41: else
42: bossa.util.User
43: .error("Unknown inlined unary numeric operator "
44: + param);
45: return new UnaryNumOp(kind, type);
46: }
47:
48: private UnaryNumOp(int kind, PrimType type) {
49: this .kind = kind;
50: this .type = type;
51: }
52:
53: private final PrimType type;
54: private final int kind;
55:
56: public void compile(ApplyExp exp, Compilation comp, Target target) {
57: Expression[] args = exp.getArgs();
58: CodeAttr code = comp.getCode();
59: Target stack = new StackTarget(type);
60: args[0].compile(comp, stack);
61:
62: if (kind == Neg) {
63: code.emitNeg();
64: } else if (kind == Comp) // Bitwise complement
65: {
66: // ~x == (x xor -1)
67: if (type == Type.long_type)
68: code.emitPushLong(-1);
69: else
70: code.emitPushInt(-1);
71: code.emitXOr();
72: } else
73: throw new Error();
74:
75: target.compileFromStack(comp, type);
76: }
77:
78: public Type getReturnType(Expression[] args) {
79: return type;
80: }
81:
82: // Interpretation
83:
84: public Object apply1(Object arg) {
85: throw new Error("Not implemented");
86: }
87: }
|