View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package net.sf.zel.instr;
7   
8   import java.util.ArrayList;
9   import java.util.concurrent.Future;
10  import java.util.logging.Level;
11  import java.util.logging.Logger;
12  import net.sf.zel.vm.EndParamMarker;
13  import net.sf.zel.vm.ExecutionContext;
14  import net.sf.zel.vm.ZExecutionException;
15  import net.sf.zel.vm.Program;
16  import net.sf.zel.vm.SimpleStack;
17  
18  /**
19   *
20   * @author zoly
21   */
22  public final class CALL extends Instruction
23  {
24      private static final long serialVersionUID = 759722625722456554L;
25  
26      private CALL(){};
27  
28      @Override
29      public void execute(ExecutionContext context) throws ZExecutionException
30      {
31          Program p = (Program) context.stack.pop();
32          ExecutionContext nctx= context.getSubProgramContext(p);
33          Object param; // copy function parameter to stack TODO: optimize this and reuse stacks
34          ArrayList<Object> parameters = new ArrayList<Object>();
35          while (((param = context.stack.pop()) != EndParamMarker.instance))
36              parameters.add(param);
37          for (int i=parameters.size()-1; i>=0 ; i--)
38                      nctx.stack.push(parameters.get(i));
39          switch (p.getType())
40          {
41              case DETERMINISTIC:
42                  SimpleStack params = new SimpleStack();
43                  params.push(p);
44                  for (int i=parameters.size()-1; i>=0 ; i--)
45                      params.push(parameters.get(i));
46  
47                  Object obj = context.resultCache.getResult(params);
48                  if (obj == null)
49                  {                    
50                      context.resultCache.putTransientResult(params, executeProgram(nctx, context));
51                  }
52                  else
53                      context.stack.push(obj);
54                  break;
55              case NONDETERMINISTIC:
56                  executeProgram(nctx, context);
57                  break;                
58          }
59          context.ip++;
60      }
61  
62  
63  
64      private Object executeProgram(ExecutionContext nctx, ExecutionContext calingCtx) throws ZExecutionException
65      {
66          Object result = Program.execute(nctx);
67          calingCtx.stack.push(result);
68          return result;
69      }
70  
71  
72      /**
73       * instance
74       */
75      public static final Instruction instance = new CALL();
76  }
77