private static Code.Label findLabel(int target, HashMap<Integer, Code.Label> labels) { Code.Label label = labels.get(target); if (label == null) { label = Code.Label("label" + labelCount++); labels.put(target, label); } return label; }
private static Code.LoopEnd findLoopLabel(int target, HashMap<Integer, Code.Label> labels) { Code.Label label = labels.get(target); if (label == null) { Code.LoopEnd end = Code.LoopEnd("label" + labelCount++); labels.put(target, end); return end; } else { Code.LoopEnd end = Code.LoopEnd(label.label); labels.put(target, end); return end; } }
private Block readCodeBlock(int numInputs) throws IOException { Block block = new Block(numInputs); int nCodes = input.read_uv(); HashMap<Integer, Code.Label> labels = new HashMap<Integer, Code.Label>(); for (int i = 0; i != nCodes; ++i) { Code code = readCode(i, labels); block.append(code); } // NOTE: we must go up to nCodes+1 because of the possibility of a label // occurring after the very last bytecode instruction. for (int i = 0, j = 0; i != nCodes + 1; ++i, ++j) { Code.Label label = labels.get(i); if (label != null) { block.insert(j++, label); } } input.pad_u8(); // necessary return block; }
private void readConstantPool(int size) throws IOException { final Constant[] myConstantPool = new Constant[size]; for (int i = 0; i != size; ++i) { int code = input.read_uv(); Constant constant; switch (code) { case WyilFileWriter.CONSTANT_Null: constant = Constant.V_NULL; break; case WyilFileWriter.CONSTANT_False: constant = Constant.V_BOOL(false); break; case WyilFileWriter.CONSTANT_True: constant = Constant.V_BOOL(true); break; case WyilFileWriter.CONSTANT_Byte: { byte val = (byte) input.read_u8(); constant = Constant.V_BYTE(val); break; } case WyilFileWriter.CONSTANT_Char: { char val = (char) input.read_uv(); constant = Constant.V_CHAR(val); break; } case WyilFileWriter.CONSTANT_Int: { int len = input.read_uv(); byte[] bytes = new byte[len]; input.read(bytes); BigInteger bi = new BigInteger(bytes); constant = Constant.V_INTEGER(bi); break; } case WyilFileWriter.CONSTANT_Real: { int len = input.read_uv(); byte[] bytes = new byte[len]; input.read(bytes); BigInteger num = new BigInteger(bytes); len = input.read_uv(); bytes = new byte[len]; input.read(bytes); BigInteger den = new BigInteger(bytes); BigRational br = new BigRational(num, den); constant = Constant.V_RATIONAL(br); break; } case WyilFileWriter.CONSTANT_String: { int index = input.read_uv(); constant = Constant.V_STRING(stringPool[index]); break; } case WyilFileWriter.CONSTANT_List: { int len = input.read_uv(); ArrayList<Constant> values = new ArrayList<Constant>(); for (int j = 0; j != len; ++j) { int index = input.read_uv(); values.add(myConstantPool[index]); } constant = Constant.V_LIST(values); break; } case WyilFileWriter.CONSTANT_Set: { int len = input.read_uv(); ArrayList<Constant> values = new ArrayList<Constant>(); for (int j = 0; j != len; ++j) { int index = input.read_uv(); values.add(myConstantPool[index]); } constant = Constant.V_SET(values); break; } case WyilFileWriter.CONSTANT_Tuple: { int len = input.read_uv(); ArrayList<Constant> values = new ArrayList<Constant>(); for (int j = 0; j != len; ++j) { int index = input.read_uv(); values.add(myConstantPool[index]); } constant = Constant.V_TUPLE(values); break; } case WyilFileWriter.CONSTANT_Map: { int len = input.read_uv(); HashSet<Pair<Constant, Constant>> values = new HashSet<Pair<Constant, Constant>>(); for (int j = 0; j != len; ++j) { int keyIndex = input.read_uv(); int valIndex = input.read_uv(); Constant key = myConstantPool[keyIndex]; Constant val = myConstantPool[valIndex]; values.add(new Pair<Constant, Constant>(key, val)); } constant = Constant.V_MAP(values); break; } case WyilFileWriter.CONSTANT_Record: { int len = input.read_uv(); HashMap<String, Constant> tvs = new HashMap<String, Constant>(); for (int j = 0; j != len; ++j) { int fieldIndex = input.read_uv(); int constantIndex = input.read_uv(); String str = stringPool[fieldIndex]; tvs.put(str, myConstantPool[constantIndex]); } constant = Constant.V_RECORD(tvs); break; } case WyilFileWriter.CONSTANT_Function: case WyilFileWriter.CONSTANT_Method: { int typeIndex = input.read_uv(); int nameIndex = input.read_uv(); Type.FunctionOrMethod t = (Type.FunctionOrMethod) typePool[typeIndex]; NameID name = namePool[nameIndex]; constant = Constant.V_LAMBDA(name, t); break; } default: throw new RuntimeException("Unknown constant encountered in WhileyDefine: " + code); } myConstantPool[i] = constant; } constantPool = myConstantPool; }