1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import jdk.internal.HotSpotIntrinsicCandidate;
  29 
  30 import java.lang.annotation.Native;
  31 
  32 /**
  33  * TBD
  34  */
  35 public class Continuation {
  36     private static final int INITIAL_STACK_SIZE = 1000;
  37     static {
  38         // registerNatives();
  39     }
  40 
  41     private Runnable target;
  42     private Continuation parent; // null for native stack
  43     private long parentSP;
  44     private long parentFP;
  45     private int[] stack = new int[INITIAL_STACK_SIZE];
  46     private int lastFP = stack.length - 1;
  47     private int lastSP = stack.length - 1;
  48 
  49     // save registers
  50     private long reg0;
  51     private long reg1;
  52     private long reg2;
  53 
  54     private boolean done;
  55 
  56     private static final int overhead = 10;
  57 
  58     /**
  59      * TBD
  60      * @param target TBD
  61      */
  62     public Continuation(Runnable target) {
  63         this.target = target;
  64         this.stack[0] = 0x42;
  65         this.stack[stack.length - 1] = 0xBABA;
  66     }
  67 
  68     /**
  69      * TBD
  70      */
  71     @HotSpotIntrinsicCandidate
  72     public void run() { throw new Error("Intrinsic not installed"); }
  73 
  74     /**
  75      * TBD
  76      */
  77     private void enter() {
  78         System.out.println("Yay");
  79         System.out.println("target: " + Integer.toHexString(System.identityHashCode(target)));
  80         System.out.println("target: " + target);
  81         System.out.println("Yay2");
  82         target.run();
  83         System.out.println("Yay3 - done");
  84         done = true;
  85     }
  86 
  87     /**
  88      * TBD
  89      * @return TBD
  90      */
  91     public boolean isDone() {
  92         return done;
  93     }
  94 
  95     private void getStack(int offset, int additionalLocals) {
  96         System.out.println("JAVA Cont: " + this + " offset: " + offset + " locals: " + additionalLocals);
  97         dump();
  98 //        Thread.dumpStack();
  99         if (offset - additionalLocals < 0) {
 100             int oldLength = stack.length;
 101             int newLength = stack.length * 2;
 102             int[] newStack = new int[newLength];
 103             System.arraycopy(stack, offset, newStack, newStack.length - offset, offset);
 104 
 105             // we need to preserve the same offset from the arrays _end_
 106             this.lastFP = newLength - (oldLength - lastFP);
 107             this.lastSP = newLength - (oldLength - lastSP);
 108 
 109             stack = newStack;
 110         }
 111     }
 112 
 113     private void dump() {
 114         System.out.println("Continuation@" + Long.toHexString(System.identityHashCode(this)));
 115         System.out.println("\tparent: " + parent);
 116         System.out.println("\tparentSP: " + Long.toHexString(parentSP));
 117         System.out.println("\tstack.length: " + stack.length);
 118         for (int i = 1; i <= 10; i++) {
 119             int j = stack.length - i;
 120             System.out.println("\tarray[ " + j + "] = " +stack[j]);
 121         }
 122     }
 123 
 124     /**
 125      * TBD
 126      */
 127     public native void foo();
 128 
 129     // native methods
 130     private static native void registerNatives();
 131 }