KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > SeriesTarget


1 // Copyright (c) 2000, 2001, 2006 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.expr;
5 import gnu.bytecode.*;
6 import gnu.kawa.reflect.OccurrenceType;
7 import gnu.kawa.reflect.SingletonType;
8
9 /** The value in the result (as a sequence of values) is passed to a function.
10  */

11
12 public class SeriesTarget extends Target
13 {
14   /** Where to place each value. */
15   public Declaration param;
16
17   /** A function to call (using jsr/jsr_w). */
18   public Label function;
19
20   /** Where to go when done executing the Expression whose target this is.
21    * If null, execution should continue just after the Expression. */

22   public Label done;
23
24   /** A surrounding Scope for local Variables.
25    * This Scope should include both any calls to compileFromStackSimple
26    * and the entirety of the 'function' subroutine. This is protect against
27    * where a variable logically goes out of scope, but we cannot re-use
28    * the local variable slot until we're past the 'function'. */

29   public Scope scope;
30
31   public void compileFromStackSimple(Compilation comp, Type stackType)
32   {
33     CodeAttr code = comp.getCode();
34     StackTarget.convert(comp, stackType, param.getType());
35     param.compileStore(comp);
36     code.emitJsr(function);
37     if (done != null && code.reachableHere())
38        code.emitGoto(done);
39     // Make sure we don't free the local variable slots for any variable
40
// slots prematurely. I.e. any local variables in use at this point
41
// must be protected from being re-used in the Jsr subroutine.
42
code.locals.preserveVariablesUpto(scope);
43   }
44
45   public void compileFromStack(Compilation comp, Type stackType)
46   {
47     CodeAttr code = comp.getCode();
48
49     if (OccurrenceType.itemCountIsOne(stackType))
50       {
51     compileFromStackSimple(comp, stackType);
52     return;
53       }
54
55     /* emit the following:
56        int index = 0;
57        for (;;)
58        {
59          int next = Values.nextIndex(values, index);
60      if (next < 0)
61        goto done;
62      Values.nextValue(values, index);
63      compileFromStackSimple(comp, Type.pointerType);
64      index = value;
65        }
66     */

67     Variable indexVar = code.addLocal(Type.int_type);
68     Variable valuesVar = code.addLocal(Type.pointer_type);
69     Variable nextVar = code.addLocal(Type.int_type);
70     Label doneLabel = done;
71     boolean doneGiven;
72     if (doneLabel == null)
73       {
74         doneGiven = false;
75         doneLabel = new Label(code);
76       }
77     else
78       {
79         doneGiven = true;
80         done = null; // To suppress goto in compileFromStackSimple.
81
}
82     StackTarget.convert(comp, stackType, Type.pointer_type);
83     code.emitStore(valuesVar);
84     code.emitPushInt(0);
85     code.emitStore(indexVar);
86
87     Label top = new Label(code);
88     top.define(code);
89     code.emitLoad(valuesVar);
90     code.emitLoad(indexVar);
91     code.emitInvokeStatic(Compilation.typeValues.getDeclaredMethod("nextIndex", 2));
92     code.emitDup(Type.int_type);
93     code.emitStore(nextVar);
94     code.emitGotoIfIntLtZero(doneLabel);
95     code.emitLoad(valuesVar);
96     code.emitLoad(indexVar);
97     code.emitInvokeStatic(Compilation.typeValues.getDeclaredMethod("nextValue", 2));
98     compileFromStackSimple(comp, SingletonType.getInstance());
99     code.emitLoad(nextVar);
100     code.emitStore(indexVar);
101     code.emitGoto(top);
102     if (doneGiven)
103       done = doneLabel;
104     else
105       doneLabel.define(code);
106
107     /*
108     if (stackType is singleton type)
109       compileFromStackSimple(comp, stackType);
110     else
111       {
112     code.emitDup(stackType);
113     emit[if TOP instanceof Values];
114     emit loop [Values, get, ...];
115     code.emitElse();
116     compileFromStackSimple(comp, stackType);
117     code.emitFi();
118       }
119     */

120   }
121
122   public String JavaDoc toString()
123   {
124     return "SeriesTarget[param: "+param+"; func:"+function+" done:"+done+"]";
125   }
126
127   public Type getType() { return Type.pointer_type; }
128 }
129
Popular Tags