View Javadoc
1   /*
2    * Copyright (c) 2010, 2013, 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  package jdk.nashorn.internal.codegen;
26  
27  import jdk.nashorn.internal.codegen.types.Type;
28  import jdk.nashorn.internal.runtime.Debug;
29  
30  /**
31   * Abstraction for labels, separating a label from the underlying
32   * byte code emitter. Also augmenting label with e.g. a name
33   * for easier debugging and reading code
34   *
35   * see -Dnashorn.codegen.debug, --log=codegen
36   */
37  public final class Label {
38      //byte code generation evaluation type stack for consistency check
39      //and correct opcode selection. one per label as a label may be a
40      //join point
41      static final class Stack {
42          Type[] data = new Type[8];
43          int sp = 0;
44  
45          Stack() {
46          }
47  
48          private Stack(final Type[] type, final int sp) {
49              this();
50              this.data = new Type[type.length];
51              this.sp   = sp;
52              for (int i = 0; i < sp; i++) {
53                  data[i] = type[i];
54              }
55          }
56  
57          boolean isEmpty() {
58              return sp == 0;
59          }
60  
61          int size() {
62              return sp;
63          }
64  
65          boolean isEquivalentTo(final Stack other) {
66              if (sp != other.sp) {
67                  return false;
68              }
69              for (int i = 0; i < sp; i++) {
70                  if (!data[i].isEquivalentTo(other.data[i])) {
71                      return false;
72                  }
73              }
74              return true;
75          }
76  
77          void clear() {
78              sp = 0;
79          }
80  
81          void push(final Type type) {
82              if (data.length == sp) {
83                  final Type[] newData = new Type[sp * 2];
84                  for (int i = 0; i < sp; i++) {
85                      newData[i] = data[i];
86                  }
87                  data = newData;
88              }
89              data[sp++] = type;
90          }
91  
92          Type peek() {
93              return peek(0);
94          }
95  
96          Type peek(final int n) {
97              int pos = sp - 1 - n;
98              return pos < 0 ? null : data[pos];
99          }
100 
101         Type pop() {
102             return data[--sp];
103         }
104 
105         Stack copy() {
106             return new Stack(data, sp);
107         }
108 
109         @Override
110         public String toString() {
111             final StringBuilder builder = new StringBuilder("[");
112             for (int i = 0; i < sp; i++) {
113                 builder.append(data[i]);
114                 if (i < sp - 1) {
115                     builder.append(", ");
116                 }
117             }
118             return builder.append("]").toString();
119         }
120     }
121 
122     /** Name of this label */
123     private final String name;
124 
125     /** Type stack at this label */
126     private Label.Stack stack;
127 
128     /** ASM representation of this label */
129     private jdk.internal.org.objectweb.asm.Label label;
130 
131     /**
132      * Constructor
133      *
134      * @param name name of this label
135      */
136     public Label(final String name) {
137         super();
138         this.name = name;
139     }
140 
141     /**
142      * Copy constructor
143      *
144      * @param label a label to clone
145      */
146     public Label(final Label label) {
147         super();
148         this.name = label.name;
149     }
150 
151 
152     jdk.internal.org.objectweb.asm.Label getLabel() {
153         if (this.label == null) {
154             this.label = new jdk.internal.org.objectweb.asm.Label();
155         }
156         return label;
157     }
158 
159     Label.Stack getStack() {
160         return stack;
161     }
162 
163     void setStack(final Label.Stack stack) {
164         this.stack = stack;
165     }
166 
167     @Override
168     public String toString() {
169         return name + '_' + Debug.id(this);
170     }
171 }