Path: blob/master/runtime/bcverify/bcverify.java
5986 views
/*******************************************************************************1* Copyright (c) 2001, 2017 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21/* JAVACODE */2223/* This Java code will generate the tables/bitmaps for start/part of java identifier unicode24characters. */2526package MapUnicode;2728import java.io.*;29import java.util.*;3031//32// @author mbottomley33//34public class mapUnicode {3536final static int VALID_START = 2;37final static int VALID_PART = 1;38final static int INVALID = 0;3940final static int BIT_MAP_END = 128;41final static int RANGE_END = 65536;4243static long[] bitMapStartChars = new long[BIT_MAP_END >> 5];44static long[] bitMapPartChars = new long[BIT_MAP_END >> 5];45static int[] rangesStartChars = new int[1024];46static int[] rangesPartChars = new int[512];47static int startCharRanges, partCharRanges;4849static File cFile;50static FileWriter cOutput;5152public static void createBitMaps() {53int i;54long j, k, bitPosition;55char c;56// Create the valid character bit maps for the first BIT_MAP_END characters57j = 0;58k = 0;59bitPosition = 1;60for (i = 0; i < BIT_MAP_END; i++) {61c = (char) i;62if (Character.isJavaIdentifierStart(c)) {63j |= bitPosition;64}65if (Character.isJavaIdentifierPart(c)) {66k |= bitPosition;67}68bitPosition <<= 1;69if (bitPosition == 4294967296L) {70bitMapStartChars[i >> 5] = j;71bitMapPartChars[i >> 5] = k;72j = 0;73k = 0;74bitPosition = 1;75}76}77}7879public static void createStartRangeMap() {80int i, j;81boolean insideStartRange;82char c;8384// Create the valid character range maps for characters BIT_MAP_END..RANGE_END - 185j = 0;86insideStartRange = false;87// Currently 0 is not a valid start character so don't expect this to execute88if (Character.isJavaIdentifierStart((char) 0)) {89rangesStartChars[j++] = 0;90}91// Add a dummy entry at the start of the table92rangesStartChars[j++] = BIT_MAP_END - 1;93for (i = BIT_MAP_END; i < RANGE_END; i++) {94c = (char) i;95// Generate the ranges for valid start characters96// There are ~500 ranges for valid start characters97if (Character.isJavaIdentifierStart(c)) {98if (!insideStartRange) {99rangesStartChars[j++] = i - 1;100insideStartRange = true;101}102} else {103if (insideStartRange) {104rangesStartChars[j++] = i - 1;105insideStartRange = false;106}107}108}109rangesStartChars[j++] = RANGE_END - 1;110111startCharRanges = j;112}113114public static void createPartRangeMap() {115int i, j;116boolean insidePartRange;117char c;118119// Create the valid character range maps for characters 128..65535120j = 0;121insidePartRange = true;122// Since the rangesPartChars starts with a valid range, add an extra dummy entry123// to make even numbered indexed ranges valid like the valid start character range table.124// Expect this condition to be true125if (Character.isJavaIdentifierPart((char) 0)) {126rangesPartChars[j++] = 0;127}128rangesPartChars[j++] = BIT_MAP_END - 1;129for (i = BIT_MAP_END; i < RANGE_END; i++) {130c = (char) i;131// Generate the ranges for valid part characters that are not valid start characters132// There are fewer of these ranges than part/start ranges (~250 versus ~750)133// This will also "glue" together part only ranges separated by start ranges134// (2nd condition) saving ~15 ranges135if ((!Character.isJavaIdentifierStart(c)136&& Character.isJavaIdentifierPart(c))137|| (Character.isJavaIdentifierStart(c) && insidePartRange)) {138if (!insidePartRange) {139rangesPartChars[j++] = i - 1;140insidePartRange = true;141}142} else {143if (insidePartRange) {144rangesPartChars[j++] = i - 1;145insidePartRange = false;146}147}148}149rangesPartChars[j++] = RANGE_END - 1;150151partCharRanges = j;152}153154public static void writeConstantTables() throws IOException {155156int i;157158cOutput.write("#define VALID_START " + VALID_START + "\n");159cOutput.write("#define VALID_PART " + VALID_PART + "\n");160cOutput.write("#define INVALID " + INVALID + "\n");161cOutput.write("#define BIT_MAP_END " + BIT_MAP_END + "\n");162cOutput.write("#define START_CHAR_RANGES " + startCharRanges + "\n");163cOutput.write("#define PART_CHAR_RANGES " + partCharRanges + "\n\n");164165cOutput.write("const U_32 bitMapStartChars[] = {\n");166for (i = 0; i < ((BIT_MAP_END >> 5) - 1); i++) {167cOutput.write(bitMapStartChars[i] + ",\n");168}169// Write the last entry without a comma170cOutput.write(bitMapStartChars[i] + "\n");171cOutput.write("};\n\n");172173cOutput.write("const U_32 bitMapPartChars[] = {\n");174for (i = 0; i < ((BIT_MAP_END >> 5) - 1); i++) {175cOutput.write(bitMapPartChars[i] + ",\n");176}177// Write the last entry without a comma178cOutput.write(bitMapPartChars[i] + "\n");179cOutput.write("};\n\n");180181cOutput.write("const U_16 rangesStartChars[] = {\n");182for (i = 0; i < (startCharRanges - 1); i++) {183cOutput.write(rangesStartChars[i] + ",\n");184}185// Write the last entry without a comma186cOutput.write(rangesStartChars[i] + "\n");187cOutput.write("};\n\n");188189cOutput.write("const U_16 rangesPartChars[] = {\n");190for (i = 0; i < (partCharRanges - 1); i++) {191cOutput.write(rangesPartChars[i] + ",\n");192}193// Write the last entry without a comma194cOutput.write(rangesPartChars[i] + "\n");195cOutput.write("};\n\n");196}197198public static int checkCharacter(int i) {199200char c;201int searchStep, rangeIndex;202int result = INVALID;203204c = (char) i;205// Check the first BIT_MAP_END - optimization206if (i < BIT_MAP_END) {207if (((bitMapStartChars[i >> 5]) & (1 << (i & 0x1f))) != 0) {208// GoodStartOrPart209result = VALID_START | VALID_PART;210} else {211if (((bitMapPartChars[i >> 5]) & (1 << (i & 0x1f))) != 0) {212// GoodPart213result = VALID_PART;214}215}216} else {217// Binary search the ranges of valid start characters218rangeIndex = startCharRanges >> 1;219searchStep = rangeIndex;220while (true) {221searchStep = (searchStep + 1) >> 1;222if (i > rangesStartChars[rangeIndex]) {223rangeIndex += searchStep;224} else {225if (i <= rangesStartChars[rangeIndex - 1]) {226rangeIndex -= searchStep;227} else {228if ((rangeIndex & 1) == 0) {229// GoodStartOrPart230result = VALID_START | VALID_PART;231} else {232// Not a valid start character so try the valid part characters233rangeIndex = partCharRanges >> 1;234searchStep = rangeIndex;235while (true) {236searchStep = (searchStep + 1) >> 1;237if (i > rangesPartChars[rangeIndex]) {238rangeIndex += searchStep;239} else {240if (i <= rangesPartChars[rangeIndex - 1]) {241rangeIndex -= searchStep;242} else {243if ((rangeIndex & 1) == 0) {244// GoodPart245result = VALID_PART;246}247break;248}249}250}251}252break;253}254}255}256}257return result;258}259260public static void main(String[] args) {261char c;262int i;263int result;264boolean fail;265266fail = false;267268createBitMaps();269270createStartRangeMap();271createPartRangeMap();272273//Test starts274result = 0;275for (i = 0; i < RANGE_END; i++) {276c = (char) i;277result = checkCharacter(i);278if ((result & VALID_START) == VALID_START) {279if (!Character.isJavaIdentifierStart(c)) {280fail = true;281System.out.println(282"Lookup table error? " + i + " not a valid start");283}284} else if ((result & VALID_PART) == VALID_PART) {285if (!Character.isJavaIdentifierPart(c)) {286fail = true;287System.out.println(288"Lookup table error? " + i + " not a valid part");289}290} else {291if (Character.isJavaIdentifierPart(c)) {292fail = true;293System.out.println(294"Lookup table error? "295+ i296+ " is a valid part and possible start");297}298}299}300301if (fail == false) {302cFile = new File("d:\\temp\\out.c");303try {304cOutput = new FileWriter(cFile);305Calendar now = Calendar.getInstance();306cOutput.write(307"/* Autogenerated "308+ now.get(Calendar.YEAR)309+ "/"310+ now.get(Calendar.MONTH)311+ "/"312+ now.get(Calendar.DATE)313+ " "314+ now.get(Calendar.HOUR)315+ ":"316+ now.get(Calendar.MINUTE)317+ ":"318+ now.get(Calendar.SECOND)319+ " *"320+ "/\n");321cOutput.write(322"/* This file is autogenerated - do not edit *");323cOutput.write(324"/\n\n");325writeConstantTables();326cOutput.close();327System.out.println("Passed table generation");328} catch (Exception e) {329System.out.println("Failed table generation");330}331332} else {333System.out.println("Failed table generation");334}335}336}337338339