Package erjang.beam.repr

Examples of erjang.beam.repr.Insn$AAI


public class Rewriter {
    public void rewriteFunctionBody(List<Insn> body, FunctionInfo sig) {
  ListIterator<Insn> it = body.listIterator();
  while (it.hasNext()) {
      final Insn insn = it.next();
      switch (insn.opcode()) {
      case call_fun: {
    int save_pos = savePos(it);
    boolean trigger = (it.hasNext() &&
           it.next().opcode() == BeamOpcode.deallocate &&
           it.hasNext() &&
View Full Code Here


    private void restorePos(ListIterator<Insn> it, int save_pos) {
  // Only works for restoring backwards.
  int i=0;
  while (it.previousIndex() >= save_pos) {
      int pi = it.previousIndex();
      Insn insn = it.previous(); i++;
  }
  if (it.nextIndex() <= save_pos) it.next(); // Turn iterator.
    }
View Full Code Here

        Arg tuple_reg = null;

        vis.visitBegin(exh);

        for (int insn_idx = 0; insn_idx < insns.size(); insn_idx++) {
          Insn insn_ = insns.get(insn_idx);
          BeamOpcode opcode = insn_.opcode();
          TypeMap type_map = this.map[insn_idx];

          switch (opcode) {
          case func_info: {
            Insn.AAI insn = (Insn.AAI)insn_;
            // log.finer("go: " + insn);
            vis.visitInsn(opcode, insn.getExtFun());
            break;
          }

          case fnegate:
            Insn.LSD insn = (Insn.LSD)insn_;
            EAtom name = opcode.symbol;
            int failLabel = decode_labelref(insn.label, type_map.exh);
            Arg[] in = new Arg[] {src_arg(insn_idx, insn.src)};
            Type[] inTypes = new Type[] {in[0].type};
            Arg out = dest_arg(insn_idx, insn.dest);

            BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(), inTypes,
                failLabel != 0, true);

            vis.visitInsn(opcode, failLabel, in, out, bif);
            break;
          }
         
          case fconv:
          case fmove:
          case move: {
            Insn.SD insn = (Insn.SD)insn_;
            Arg src  = src_arg(insn_idx, insn.src);
            Arg dest = dest_arg(insn_idx, insn.dest);

            if (insns.size() > insn_idx+1) {
            Insn next_insn = insns.get(insn_idx+1);
            if (next_insn.opcode() == BeamOpcode.K_return) {
              vis.visitInsn(BeamOpcode.K_return, src);
              insn_idx += 1;
              break;
            }
            }
           
            if (dest.kind != Kind.F) {
              if (src.kind == Kind.F) {
                dest = new Arg(dest, EDOUBLE_TYPE);
              } else {
                dest = new Arg(dest, src.type);
              }
            } else {
              // arg2.kind == F
            }

            vis.visitInsn(opcode, src, dest);
            break;
          }

          case put_string: {
            Insn.ByD insn = (Insn.ByD)insn_;
            Arg src  = src_arg(insn_idx, insn.bin);
            Arg dest = dest_arg(insn_idx, insn.dest);
//             Arg arg1 = decode_arg(insn_idx, insn.elm(3));
//             Arg arg2 = decode_out_arg(insn_idx, insn.elm(4));
            dest = new Arg(dest, ESEQ_TYPE);
            vis.visitInsn(BeamOpcode.move, src, dest);
            break;
          }

          case fadd:
          case fsub:
          case fmul:
          case fdiv:
          {
            Insn.LSSD insn = (Insn.LSSD)insn_;
            EAtom name = opcode.symbol;
            int failLabel = decode_labelref(insn.label, type_map.exh);
            Arg[] in = new Arg[] {src_arg(insn_idx, insn.src1),
                        src_arg(insn_idx, insn.src2)};
            Type[] inTypes = new Type[] {in[0].type,
                           in[1].type};
            Arg out = dest_arg(insn_idx, insn.dest);

            BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(), inTypes,
                failLabel != 0, true);

            vis.visitInsn(opcode, failLabel, in, out, bif);
            break;
          }

          case bif0:
          case bif1:
          case bif2:
          {
            Insn.Bif insn = (Insn.Bif)insn_;
            EAtom name = insn.ext_fun.fun;
            int failLabel = decode_labelref(insn.label, type_map.exh);
            SourceOperand[] srcs = insn.args;
            Arg[] in = src_args(insn_idx, srcs);
            Arg out  = dest_arg(insn_idx, insn.dest);

            BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(),
                parmTypes(type_map, srcs),
                failLabel != 0, true);

            vis.visitInsn(opcode, failLabel, in, out, bif);
            break;
          }

           case gc_bif1:
           case gc_bif2:
           case gc_bif3:
          {
            Insn.GcBif insn = (Insn.GcBif)insn_;
            EAtom name = insn.ext_fun.fun;
            int failLabel = decode_labelref(insn.label, type_map.exh);
            SourceOperand[] srcs = insn.args;
            Arg[] in = src_args(insn_idx, srcs);
            Arg out  = dest_arg(insn_idx, insn.dest);

            // special case for X+1, 1+X, X-1.
            Int lop = null, rop = null;
            if (srcs.length==2
                && (((name==am_plus || name == am_minus) && (rop=srcs[1].testInt()) != null && rop.equals(1))
                 || (name==am_plus && (lop=srcs[0].testInt()) != null && lop.equals(1))))
            {
              if (name == am_plus) {
                Arg src = (lop == null) ? in[0] : in[1];
               
                vis.visitIncrement(src, out);
                break;
              } else if (name == am_minus) {
                Arg src = in[0];
                vis.visitDecrement(src, out);
                break;               
              }
            }
           
            BuiltInFunction bif = BIFUtil.getMethod("erlang", name.getName(),
                parmTypes(type_map, srcs),
                failLabel != 0, true);

            vis.visitInsn(opcode, failLabel, in, out, bif);
            break;
          }

          case is_tuple: {
           
            if (insn_idx+1 < insns.size()) {
            Insn next_insn = insns.get(insn_idx+1);
            if (next_insn.opcode() == BeamOpcode.test_arity) {
             
              int this_fail = decode_labelref(((Insn.L)insn_).label, this.map[insn_idx].exh);
              int next_fail = decode_labelref(((Insn.L)next_insn).label, this.map[insn_idx+1].exh);

              if (this_fail == next_fail) {
View Full Code Here

      }

      public void analyze0() {
        TypeMap current = initial;
        BeamOpcode last_opcode = BeamOpcode.NONE;
        Insn last_insn = null;

        map = new TypeMap[insns.size()];

        next_insn: for (int insn_idx = 0; insn_idx < insns.size(); insn_idx++) {

          update_max_regs(current);

          if (is_term(last_opcode)) {
            throw new Error("how did we get here then...? "
                + this.block_label + ":" + insn_idx);
          }

          map[insn_idx] = current;
          Insn insn_ = insns.get(insn_idx);
          BeamOpcode code = insn_.opcode();
          last_opcode = code; last_insn = insn_;
          /*
           * System.out.println(name + "(" + bb_label + "):" + i +
           * " :: " + current + "" + insn);
           */

          if (current.exh != null && may_terminate_exceptionally(code))
            addExceptionEdge(current);

          switch (code) {
          case fmove:
          case move: {
            Insn.SD insn = (Insn.SD) insn_;
            SourceOperand src = insn.src;
            DestinationOperand dst = insn.dest;

            Type srcType = getType(current, src);

            // Determine type after possible conversion:
            Type dstType = srcType;
            if (dst.testFReg() != null) {
              dstType = Type.DOUBLE_TYPE; // FRegs are always unboxed
            } else if (sizeof(current, src) > sizeof(current, dst)) { // Conversion needed
              if (srcType.equals(Type.DOUBLE_TYPE)) {
                dstType = EDOUBLE_TYPE; // Box
              } else {
                throw new Error("why?" + insn.toSymbolic()
                    + "; srcType="+getType(current,src));
              }
            }

            current = setType(current, dst, dstType);
            continue next_insn;
          }
          case put_string: {
            Insn.ByD insn = (Insn.ByD) insn_;
            DestinationOperand dst = insn.dest;
            current = setType(current, dst, ESEQ_TYPE);
            continue next_insn;
          }

          case jump: {
            Insn.L insn = (Insn.L) insn_;
            current = branch(current, insn.label, insn_idx);
            continue next_insn;

          }

          case send: {
            current.touchx(0, 2);
            current = current.setx(0, current.getx(1), FV.this);
            continue next_insn;
          }

          case fnegate: {
            Insn.LSD insn = (Insn.LSD)insn_;
            EAtom name = insn.opcode().symbol;
            SourceOperand[] parms = new SourceOperand[] {
              insn.src
            };
            Type type = getBifResult("erlang", name.getName(),
                         parmTypes(current, parms), false);
            current = setType(current, insn.dest, type);

            continue next_insn;
          }
          case fadd:
          case fsub:
          case fmul:
          case fdiv:
          {
            Insn.LSSD insn = (Insn.LSSD) insn_;
            EAtom name = insn.opcode().symbol;
            SourceOperand[] parms = new SourceOperand[] {
              insn.src1, insn.src2
            };
            Type type = getBifResult("erlang", name.getName(),
                         parmTypes(current, parms), false);
            current = setType(current, insn.dest, type);

            continue next_insn;
          }

           case gc_bif1:
           case gc_bif2:
           case gc_bif3:
          {
            // {gc_bif,BifName,F,Live,[A1,A2?],Reg};

            Insn.GcBif insn = (Insn.GcBif) insn_;
            boolean is_guard = (insn.label.nr != 0);

            current = branch(current, insn.label, insn_idx);

            EAtom name = insn.ext_fun.fun;
            SourceOperand[] parms = insn.argList();

            Type type = getBifResult("erlang", name.getName(),
                         parmTypes(current, parms), is_guard);

            current = setType(current, insn.dest, type);

            continue next_insn;
          }

          case bif0:
          case bif1:
          case bif2:
          {
            Insn.Bif insn = (Insn.Bif) insn_;
            current = branch(current, insn.label, insn_idx);

            EAtom name = insn.ext_fun.fun;
            SourceOperand[] parms = insn.argList();

            Type type = getBifResult("erlang", name.getName(),
                         parmTypes(current, parms), false);

            current = setType(current, insn.dest, type);

            continue next_insn;
          }


          case is_tuple: {
           
            if (insn_idx+1 < insns.size()) {
            Insn next_insn = insns.get(insn_idx+1);
            if (next_insn.opcode() == BeamOpcode.test_arity) {
             
              if (this.map[insn_idx+1] == null) {
                this.map[insn_idx+1] = this.map[insn_idx];
              }
             
View Full Code Here

                    ", hop:"+highestOpcode+
                    ", L:"+labelCnt+
                    ", f:"+funCnt);

    code = new ArrayList<Insn>();
    Insn insn;
    do {
      insn = readInstruction();
      code.add(insn);
    } while (insn.opcode() != BeamOpcode.int_code_end);
    }
View Full Code Here

      case if_end:
      case int_code_end:
      case fclearerror:
      case bs_init_writable:
      case on_load:
        return new Insn(opcode); // TODO: use static set of objects

        //---------- 1-ary ----------
            case line: {
        int i1 = readCodeInteger();
                int lineNo = -1;
View Full Code Here

        bp.patch(label_map.get(bp.label));
      }

      if (log.isLoggable(Level.FINE)) {
        for (int i=0; i<code.size(); i++) {
          Insn insn = insn_start.get(i);
          log.fine((insn!=null? "*" : " ") + i +
                     ": " + (int)code.get(i) +
                     (insn!=null ? ("\t"+insn.toSymbolic().toString()) : ""));
        }
      }
    }
View Full Code Here

TOP

Related Classes of erjang.beam.repr.Insn$AAI

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.