private WyilFile.Case readFunctionOrMethodCase(Type.FunctionOrMethod type) throws IOException { Block precondition = null; Block postcondition = null; Block body = null; int numInputs = type.params().size(); int nBlocks = input.read_uv(); input.pad_u8(); for (int i = 0; i != nBlocks; ++i) { int kind = input.read_uv(); int size = input.read_uv(); input.pad_u8(); switch (kind) { case WyilFileWriter.BLOCK_Precondition: precondition = readCodeBlock(numInputs); break; case WyilFileWriter.BLOCK_Postcondition: postcondition = readCodeBlock(numInputs + 1); break; case WyilFileWriter.BLOCK_Body: body = readCodeBlock(numInputs); break; default: throw new RuntimeException("Unknown case block encountered"); } } return new WyilFile.Case(body, precondition, postcondition, Collections.EMPTY_LIST); }
private WyilFile.MethodDeclaration readMethodBlock() throws IOException { int nameIdx = input.read_uv(); // System.out.println("=== METHOD " + stringPool.get(nameIdx)); int modifiers = input.read_uv(); int typeIdx = input.read_uv(); int numCases = input.read_uv(); input.pad_u8(); Type.Method type = (Type.Method) typePool[typeIdx]; ArrayList<WyilFile.Case> cases = new ArrayList<WyilFile.Case>(); for (int i = 0; i != numCases; ++i) { int kind = input.read_uv(); // unsued int size = input.read_uv(); input.pad_u8(); switch (kind) { case WyilFileWriter.BLOCK_Case: cases.add(readFunctionOrMethodCase(type)); break; default: throw new RuntimeException("Unknown method block encountered"); } } return new WyilFile.MethodDeclaration( generateModifiers(modifiers), stringPool[nameIdx], type, cases); }
private WyilFile readModule() throws IOException { int kind = input.read_uv(); // block identifier int size = input.read_uv(); input.pad_u8(); int pathIdx = input.read_uv(); int modifiers = input.read_uv(); // unused int numBlocks = input.read_uv(); input.pad_u8(); List<WyilFile.Declaration> declarations = new ArrayList<WyilFile.Declaration>(); for (int i = 0; i != numBlocks; ++i) { declarations.add(readModuleBlock()); } return new WyilFile(pathPool[pathIdx], "unknown.whiley", declarations); }
private WyilFile.TypeDeclaration readTypeBlock() throws IOException { int nameIdx = input.read_uv(); // System.out.println("=== TYPE " + stringPool.get(nameIdx)); int modifiers = input.read_uv(); int typeIdx = input.read_uv(); int nBlocks = input.read_uv(); input.pad_u8(); Block constraint = null; if (nBlocks != 0) { int kind = input.read_uv(); // unsued int size = input.read_uv(); input.pad_u8(); constraint = readCodeBlock(1); } return new WyilFile.TypeDeclaration( generateModifiers(modifiers), stringPool[nameIdx], typePool[typeIdx], constraint); }
private WyilFile.ConstantDeclaration readConstantBlock() throws IOException { int nameIdx = input.read_uv(); // System.out.println("=== CONSTANT " + stringPool.get(nameIdx)); int modifiers = input.read_uv(); int constantIdx = input.read_uv(); int nBlocks = input.read_uv(); // unused input.pad_u8(); return new WyilFile.ConstantDeclaration( generateModifiers(modifiers), stringPool[nameIdx], constantPool[constantIdx]); }
public WyilFile read() throws IOException { for (int i = 0; i != 8; ++i) { char c = (char) input.read_u8(); if (magic[i] != c) { throw new IllegalArgumentException("invalid magic number"); } } // head blocker int kind = input.read_uv(); int size = input.read_uv(); input.pad_u8(); if (kind != WyilFileWriter.BLOCK_Header) { throw new IllegalArgumentException("header block must come first"); } int majorVersion = input.read_uv(); int minorVersion = input.read_uv(); int stringPoolSize = input.read_uv(); int pathPoolSize = input.read_uv(); int namePoolSize = input.read_uv(); int typePoolSize = input.read_uv(); int constantPoolSize = input.read_uv(); int numBlocks = input.read_uv(); readStringPool(stringPoolSize); readPathPool(pathPoolSize); readNamePool(namePoolSize); readTypePool(typePoolSize); readConstantPool(constantPoolSize); input.pad_u8(); return readModule(); }
private WyilFile.Declaration readModuleBlock() throws IOException { int kind = input.read_uv(); int size = input.read_uv(); input.pad_u8(); switch (kind) { case WyilFileWriter.BLOCK_Constant: return readConstantBlock(); case WyilFileWriter.BLOCK_Type: return readTypeBlock(); case WyilFileWriter.BLOCK_Function: return readFunctionBlock(); case WyilFileWriter.BLOCK_Method: return readMethodBlock(); default: throw new RuntimeException("unknown module block encountered (" + kind + ")"); } }
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; }