Example #1
0
 private int readTarget(boolean wide, int offset) throws IOException {
   if (wide) {
     return input.read_uv();
   } else {
     return (input.read_u8() + offset) - 128;
   }
 }
Example #2
0
 private int readRest(boolean wide) throws IOException {
   if (wide) {
     return input.read_uv();
   } else {
     return input.read_u8();
   }
 }
Example #3
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 + ")");
    }
  }
Example #4
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();
  }
Example #5
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;
  }