Ejemplo n.º 1
0
 private int readTarget(boolean wide, int offset) throws IOException {
   if (wide) {
     return input.read_uv();
   } else {
     return (input.read_u8() + offset) - 128;
   }
 }
Ejemplo n.º 2
0
 private int readRest(boolean wide) throws IOException {
   if (wide) {
     return input.read_uv();
   } else {
     return input.read_u8();
   }
 }
Ejemplo n.º 3
0
  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);
  }
Ejemplo n.º 4
0
 private int readBase(boolean wide) throws IOException {
   if (wide) {
     return input.read_uv();
   } else {
     return input.read_un(4);
   }
 }
Ejemplo n.º 5
0
  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]);
  }
Ejemplo n.º 6
0
  private void readPathPool(int size) throws IOException {
    final Path.ID[] myPathPool = new Path.ID[size];
    myPathPool[0] = Trie.ROOT;

    for (int i = 1; i != size; ++i) {
      int parent = input.read_uv();
      int stringIndex = input.read_uv();
      Path.ID id;
      id = myPathPool[parent];
      id = id.append(stringPool[stringIndex]);
      myPathPool[i] = id;
    }
    pathPool = myPathPool;
  }
Ejemplo n.º 7
0
  private void readStringPool(int size) throws IOException {
    final String[] myStringPool = new String[size];

    for (int i = 0; i != size; ++i) {
      int length = input.read_uv();
      try {
        byte[] data = new byte[length];
        input.read(data);
        String str = new String(data, 0, length, "UTF-8");
        myStringPool[i] = str;
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 Charset not supported?");
      }
    }
    stringPool = myStringPool;
  }
Ejemplo n.º 8
0
  private Code readCode(int offset, HashMap<Integer, Code.Label> labels) throws IOException {
    int opcode = input.read_u8();
    boolean wideBase = false;
    boolean wideRest = false;

    // deal with wide bytecodes first
    switch (opcode) {
      case Code.OPCODE_wide:
        opcode = input.read_u8();
        wideBase = true;
        break;
      case Code.OPCODE_widerest:
        opcode = input.read_u8();
        wideRest = true;
        break;
      case Code.OPCODE_widewide:
        opcode = input.read_u8();
        wideBase = true;
        wideRest = true;
        break;
    }

    int fmt = (opcode & Code.FMT_MASK);

    switch (fmt) {
      case Code.FMT_EMPTY:
        return readEmpty(opcode, wideBase, wideRest, offset, labels);
      case Code.FMT_UNARYOP:
        return readUnaryOp(opcode, wideBase, wideRest, offset, labels);
      case Code.FMT_UNARYASSIGN:
        return readUnaryAssign(opcode, wideBase, wideRest);
      case Code.FMT_BINARYOP:
        return readBinaryOp(opcode, wideBase, wideRest, offset, labels);
      case Code.FMT_BINARYASSIGN:
        return readBinaryAssign(opcode, wideBase, wideRest);
      case Code.FMT_NARYOP:
        return readNaryOp(opcode, wideBase, wideRest, offset, labels);
      case Code.FMT_NARYASSIGN:
        return readNaryAssign(opcode, wideBase, wideRest);
      case Code.FMT_OTHER:
        return readOther(opcode, wideBase, wideRest, offset, labels);
      default:
        throw new RuntimeException("unknown opcode encountered (" + opcode + ")");
    }
  }
Ejemplo n.º 9
0
  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);
  }
Ejemplo n.º 10
0
  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);
  }
Ejemplo n.º 11
0
  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 + ")");
    }
  }
Ejemplo n.º 12
0
  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();
  }
Ejemplo n.º 13
0
  private void readNamePool(int size) throws IOException {
    final NameID[] myNamePool = new NameID[size];

    for (int i = 0; i != size; ++i) {
      // int kind = input.read_uv();
      int pathIndex = input.read_uv();
      int nameIndex = input.read_uv();
      Path.ID id = pathPool[pathIndex];
      String name = stringPool[nameIndex];
      myNamePool[i] = new NameID(id, name);
    }

    namePool = myNamePool;
  }
Ejemplo n.º 14
0
  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;
  }
Ejemplo n.º 15
0
  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);
  }
Ejemplo n.º 16
0
 public void close() throws IOException {
   input.close();
 }
Ejemplo n.º 17
0
  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;
  }