Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/bcverify/bcverify.java
5986 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2017 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
/* JAVACODE */
23
24
/* This Java code will generate the tables/bitmaps for start/part of java identifier unicode
25
characters. */
26
27
package MapUnicode;
28
29
import java.io.*;
30
import java.util.*;
31
32
//
33
// @author mbottomley
34
//
35
public class mapUnicode {
36
37
final static int VALID_START = 2;
38
final static int VALID_PART = 1;
39
final static int INVALID = 0;
40
41
final static int BIT_MAP_END = 128;
42
final static int RANGE_END = 65536;
43
44
static long[] bitMapStartChars = new long[BIT_MAP_END >> 5];
45
static long[] bitMapPartChars = new long[BIT_MAP_END >> 5];
46
static int[] rangesStartChars = new int[1024];
47
static int[] rangesPartChars = new int[512];
48
static int startCharRanges, partCharRanges;
49
50
static File cFile;
51
static FileWriter cOutput;
52
53
public static void createBitMaps() {
54
int i;
55
long j, k, bitPosition;
56
char c;
57
// Create the valid character bit maps for the first BIT_MAP_END characters
58
j = 0;
59
k = 0;
60
bitPosition = 1;
61
for (i = 0; i < BIT_MAP_END; i++) {
62
c = (char) i;
63
if (Character.isJavaIdentifierStart(c)) {
64
j |= bitPosition;
65
}
66
if (Character.isJavaIdentifierPart(c)) {
67
k |= bitPosition;
68
}
69
bitPosition <<= 1;
70
if (bitPosition == 4294967296L) {
71
bitMapStartChars[i >> 5] = j;
72
bitMapPartChars[i >> 5] = k;
73
j = 0;
74
k = 0;
75
bitPosition = 1;
76
}
77
}
78
}
79
80
public static void createStartRangeMap() {
81
int i, j;
82
boolean insideStartRange;
83
char c;
84
85
// Create the valid character range maps for characters BIT_MAP_END..RANGE_END - 1
86
j = 0;
87
insideStartRange = false;
88
// Currently 0 is not a valid start character so don't expect this to execute
89
if (Character.isJavaIdentifierStart((char) 0)) {
90
rangesStartChars[j++] = 0;
91
}
92
// Add a dummy entry at the start of the table
93
rangesStartChars[j++] = BIT_MAP_END - 1;
94
for (i = BIT_MAP_END; i < RANGE_END; i++) {
95
c = (char) i;
96
// Generate the ranges for valid start characters
97
// There are ~500 ranges for valid start characters
98
if (Character.isJavaIdentifierStart(c)) {
99
if (!insideStartRange) {
100
rangesStartChars[j++] = i - 1;
101
insideStartRange = true;
102
}
103
} else {
104
if (insideStartRange) {
105
rangesStartChars[j++] = i - 1;
106
insideStartRange = false;
107
}
108
}
109
}
110
rangesStartChars[j++] = RANGE_END - 1;
111
112
startCharRanges = j;
113
}
114
115
public static void createPartRangeMap() {
116
int i, j;
117
boolean insidePartRange;
118
char c;
119
120
// Create the valid character range maps for characters 128..65535
121
j = 0;
122
insidePartRange = true;
123
// Since the rangesPartChars starts with a valid range, add an extra dummy entry
124
// to make even numbered indexed ranges valid like the valid start character range table.
125
// Expect this condition to be true
126
if (Character.isJavaIdentifierPart((char) 0)) {
127
rangesPartChars[j++] = 0;
128
}
129
rangesPartChars[j++] = BIT_MAP_END - 1;
130
for (i = BIT_MAP_END; i < RANGE_END; i++) {
131
c = (char) i;
132
// Generate the ranges for valid part characters that are not valid start characters
133
// There are fewer of these ranges than part/start ranges (~250 versus ~750)
134
// This will also "glue" together part only ranges separated by start ranges
135
// (2nd condition) saving ~15 ranges
136
if ((!Character.isJavaIdentifierStart(c)
137
&& Character.isJavaIdentifierPart(c))
138
|| (Character.isJavaIdentifierStart(c) && insidePartRange)) {
139
if (!insidePartRange) {
140
rangesPartChars[j++] = i - 1;
141
insidePartRange = true;
142
}
143
} else {
144
if (insidePartRange) {
145
rangesPartChars[j++] = i - 1;
146
insidePartRange = false;
147
}
148
}
149
}
150
rangesPartChars[j++] = RANGE_END - 1;
151
152
partCharRanges = j;
153
}
154
155
public static void writeConstantTables() throws IOException {
156
157
int i;
158
159
cOutput.write("#define VALID_START " + VALID_START + "\n");
160
cOutput.write("#define VALID_PART " + VALID_PART + "\n");
161
cOutput.write("#define INVALID " + INVALID + "\n");
162
cOutput.write("#define BIT_MAP_END " + BIT_MAP_END + "\n");
163
cOutput.write("#define START_CHAR_RANGES " + startCharRanges + "\n");
164
cOutput.write("#define PART_CHAR_RANGES " + partCharRanges + "\n\n");
165
166
cOutput.write("const U_32 bitMapStartChars[] = {\n");
167
for (i = 0; i < ((BIT_MAP_END >> 5) - 1); i++) {
168
cOutput.write(bitMapStartChars[i] + ",\n");
169
}
170
// Write the last entry without a comma
171
cOutput.write(bitMapStartChars[i] + "\n");
172
cOutput.write("};\n\n");
173
174
cOutput.write("const U_32 bitMapPartChars[] = {\n");
175
for (i = 0; i < ((BIT_MAP_END >> 5) - 1); i++) {
176
cOutput.write(bitMapPartChars[i] + ",\n");
177
}
178
// Write the last entry without a comma
179
cOutput.write(bitMapPartChars[i] + "\n");
180
cOutput.write("};\n\n");
181
182
cOutput.write("const U_16 rangesStartChars[] = {\n");
183
for (i = 0; i < (startCharRanges - 1); i++) {
184
cOutput.write(rangesStartChars[i] + ",\n");
185
}
186
// Write the last entry without a comma
187
cOutput.write(rangesStartChars[i] + "\n");
188
cOutput.write("};\n\n");
189
190
cOutput.write("const U_16 rangesPartChars[] = {\n");
191
for (i = 0; i < (partCharRanges - 1); i++) {
192
cOutput.write(rangesPartChars[i] + ",\n");
193
}
194
// Write the last entry without a comma
195
cOutput.write(rangesPartChars[i] + "\n");
196
cOutput.write("};\n\n");
197
}
198
199
public static int checkCharacter(int i) {
200
201
char c;
202
int searchStep, rangeIndex;
203
int result = INVALID;
204
205
c = (char) i;
206
// Check the first BIT_MAP_END - optimization
207
if (i < BIT_MAP_END) {
208
if (((bitMapStartChars[i >> 5]) & (1 << (i & 0x1f))) != 0) {
209
// GoodStartOrPart
210
result = VALID_START | VALID_PART;
211
} else {
212
if (((bitMapPartChars[i >> 5]) & (1 << (i & 0x1f))) != 0) {
213
// GoodPart
214
result = VALID_PART;
215
}
216
}
217
} else {
218
// Binary search the ranges of valid start characters
219
rangeIndex = startCharRanges >> 1;
220
searchStep = rangeIndex;
221
while (true) {
222
searchStep = (searchStep + 1) >> 1;
223
if (i > rangesStartChars[rangeIndex]) {
224
rangeIndex += searchStep;
225
} else {
226
if (i <= rangesStartChars[rangeIndex - 1]) {
227
rangeIndex -= searchStep;
228
} else {
229
if ((rangeIndex & 1) == 0) {
230
// GoodStartOrPart
231
result = VALID_START | VALID_PART;
232
} else {
233
// Not a valid start character so try the valid part characters
234
rangeIndex = partCharRanges >> 1;
235
searchStep = rangeIndex;
236
while (true) {
237
searchStep = (searchStep + 1) >> 1;
238
if (i > rangesPartChars[rangeIndex]) {
239
rangeIndex += searchStep;
240
} else {
241
if (i <= rangesPartChars[rangeIndex - 1]) {
242
rangeIndex -= searchStep;
243
} else {
244
if ((rangeIndex & 1) == 0) {
245
// GoodPart
246
result = VALID_PART;
247
}
248
break;
249
}
250
}
251
}
252
}
253
break;
254
}
255
}
256
}
257
}
258
return result;
259
}
260
261
public static void main(String[] args) {
262
char c;
263
int i;
264
int result;
265
boolean fail;
266
267
fail = false;
268
269
createBitMaps();
270
271
createStartRangeMap();
272
createPartRangeMap();
273
274
//Test starts
275
result = 0;
276
for (i = 0; i < RANGE_END; i++) {
277
c = (char) i;
278
result = checkCharacter(i);
279
if ((result & VALID_START) == VALID_START) {
280
if (!Character.isJavaIdentifierStart(c)) {
281
fail = true;
282
System.out.println(
283
"Lookup table error? " + i + " not a valid start");
284
}
285
} else if ((result & VALID_PART) == VALID_PART) {
286
if (!Character.isJavaIdentifierPart(c)) {
287
fail = true;
288
System.out.println(
289
"Lookup table error? " + i + " not a valid part");
290
}
291
} else {
292
if (Character.isJavaIdentifierPart(c)) {
293
fail = true;
294
System.out.println(
295
"Lookup table error? "
296
+ i
297
+ " is a valid part and possible start");
298
}
299
}
300
}
301
302
if (fail == false) {
303
cFile = new File("d:\\temp\\out.c");
304
try {
305
cOutput = new FileWriter(cFile);
306
Calendar now = Calendar.getInstance();
307
cOutput.write(
308
"/* Autogenerated "
309
+ now.get(Calendar.YEAR)
310
+ "/"
311
+ now.get(Calendar.MONTH)
312
+ "/"
313
+ now.get(Calendar.DATE)
314
+ " "
315
+ now.get(Calendar.HOUR)
316
+ ":"
317
+ now.get(Calendar.MINUTE)
318
+ ":"
319
+ now.get(Calendar.SECOND)
320
+ " *"
321
+ "/\n");
322
cOutput.write(
323
"/* This file is autogenerated - do not edit *");
324
cOutput.write(
325
"/\n\n");
326
writeConstantTables();
327
cOutput.close();
328
System.out.println("Passed table generation");
329
} catch (Exception e) {
330
System.out.println("Failed table generation");
331
}
332
333
} else {
334
System.out.println("Failed table generation");
335
}
336
}
337
}
338
339