コード例 #1
0
ファイル: WyilFileReader.java プロジェクト: ejrh/Whiley
  private Code readNaryOp(
      int opcode,
      boolean wideBase,
      boolean wideRest,
      int offset,
      HashMap<Integer, Code.Label> labels)
      throws IOException {
    int nOperands = readBase(wideBase);
    int[] operands = new int[nOperands];
    for (int i = 0; i != nOperands; ++i) {
      operands[i] = readBase(wideBase);
    }

    if (opcode == Code.OPCODE_loop) {
      // special case which doesn't have a type.
      int target = readTarget(wideRest, offset);
      Code.LoopEnd l = findLoopLabel(target, labels);
      return Code.Loop(l.label, operands);
    }

    int typeIdx = readRest(wideRest);
    Type type = typePool[typeIdx];

    switch (opcode) {
      case Code.OPCODE_forall:
        {
          if (!(type instanceof Type.EffectiveCollection)) {
            throw new RuntimeException("expected collection type");
          }
          int target = readTarget(wideRest, offset);
          Code.LoopEnd l = findLoopLabel(target, labels);
          int indexOperand = operands[0];
          int sourceOperand = operands[1];
          operands = Arrays.copyOfRange(operands, 2, operands.length);
          return Code.ForAll(
              (Type.EffectiveCollection) type, sourceOperand, indexOperand, operands, l.label);
        }
      case Code.OPCODE_indirectinvokefnv:
      case Code.OPCODE_indirectinvokemdv:
        {
          if (!(type instanceof Type.FunctionOrMethod)) {
            throw new RuntimeException("expected function or method type");
          }
          int operand = operands[0];
          operands = Arrays.copyOfRange(operands, 1, operands.length);
          return Code.IndirectInvoke(
              (Type.FunctionOrMethod) type, Code.NULL_REG, operand, operands);
        }
      case Code.OPCODE_invokefnv:
      case Code.OPCODE_invokemdv:
        {
          if (!(type instanceof Type.FunctionOrMethod)) {
            throw new RuntimeException("expected function or method type");
          }
          int nameIdx = readRest(wideRest);
          ;
          NameID nid = namePool[nameIdx];
          return Code.Invoke((Type.FunctionOrMethod) type, Code.NULL_REG, operands, nid);
        }
    }
    throw new RuntimeException("unknown opcode encountered (" + opcode + ")");
  }