2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / Label.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Roger Lawrence\r
23  *\r
24  * Alternatively, the contents of this file may be used under the\r
25  * terms of the GNU Public License (the "GPL"), in which case the\r
26  * provisions of the GPL are applicable instead of those above.\r
27  * If you wish to allow use of your version of this file only\r
28  * under the terms of the GPL and not to allow others to use your\r
29  * version of this file under the NPL, indicate your decision by\r
30  * deleting the provisions above and replace them with the notice\r
31  * and other provisions required by the GPL.  If you do not delete\r
32  * the provisions above, a recipient may use your version of this\r
33  * file under either the NPL or the GPL.\r
34  */\r
35 \r
36 package org.mozilla.javascript;\r
37 \r
38 public class Label {\r
39     \r
40     private static final int FIXUPTABLE_SIZE = 8;\r
41 \r
42     private static final boolean DEBUG = true;\r
43 \r
44     public Label()\r
45     {\r
46         itsPC = -1;\r
47     }\r
48 \r
49     public short getPC()\r
50     {\r
51         return itsPC;\r
52     }\r
53     \r
54     public void fixGotos(byte theCodeBuffer[])\r
55     {\r
56         if (DEBUG) {\r
57             if ((itsPC == -1) && (itsFixupTable != null))\r
58                 throw new RuntimeException("Unlocated label");\r
59         }\r
60         if (itsFixupTable != null) {\r
61             for (int i = 0; i < itsFixupTableTop; i++) {\r
62                 int fixupSite = itsFixupTable[i];\r
63                 // -1 to get delta from instruction start\r
64                 short offset = (short)(itsPC - (fixupSite - 1));\r
65                 theCodeBuffer[fixupSite++] = (byte)(offset >> 8);\r
66                 theCodeBuffer[fixupSite] = (byte)offset;\r
67             }\r
68         }\r
69         itsFixupTable = null;\r
70     }\r
71 \r
72     public void setPC(short thePC)\r
73     {\r
74         if (DEBUG) {\r
75             if ((itsPC != -1) && (itsPC != thePC)) {\r
76                 throw new RuntimeException("Duplicate label");\r
77             }\r
78         }\r
79 \r
80         itsPC = thePC;\r
81     }\r
82 \r
83     public void addFixup(int fixupSite)\r
84     {\r
85         if (itsFixupTable == null) {\r
86             itsFixupTableTop = 1;\r
87             itsFixupTable = new int[FIXUPTABLE_SIZE];\r
88             itsFixupTable[0] = fixupSite;            \r
89         }\r
90         else {\r
91             if (itsFixupTableTop == itsFixupTable.length) {\r
92                 int oldLength = itsFixupTable.length;\r
93                 int newTable[] = new int[oldLength + FIXUPTABLE_SIZE];\r
94                 System.arraycopy(itsFixupTable, 0, newTable, 0, oldLength);\r
95                 itsFixupTable = newTable;\r
96             }\r
97             itsFixupTable[itsFixupTableTop++] = fixupSite;            \r
98         }\r
99     }\r
100 \r
101     private short itsPC = -1;\r
102     private int itsFixupTable[];\r
103     private int itsFixupTableTop;\r
104 \r
105 }\r
106 \r