Beispiel #1
0
    def end(self):
        """Do post-loop iterator code"""
        if self.mode == CTR:
            self.code.add(x86.loop(self.start_label))

        elif self.mode == DEC:
            # branch if r_count is not zero (CR)
            #   Note that this relies on someone (e.g. cleanup()) setting the
            #   condition register properly.
            if self.step_size() == 1:
                self.code.add(x86.jnz(self.start_label))
            else:
                self.code.add(x86.cmp(self.r_count, 0))
                self.code.add(x86.jg(self.start_label))

        elif self.mode == INC:
            if self.external_stop:
                if isinstance(self.r_count, memory.MemRef) and isinstance(
                        self.r_stop, memory.MemRef):
                    if self.r_clobber == None:
                        raise Exception(
                            'Must specify clobber_reg if count and stop values are both stored in memory.'
                        )
                        #self.code.add(x86.push(registers.eax))
                        #if self.r_count.base != registers.esp:
                        #  self.code.add(x86.mov(registers.eax, self.r_count))
                        #else:
                        #  oldm = self.r_count
                        #  m = memory.MemRef(oldm.base, oldm.disp+8, oldm.index, oldm.scale, oldm.data_size)
                        #  self.code.add(x86.mov(registers.eax, m))
                        #if self.r_stop.base != registers.esp:
                        #  self.code.add(x86.cmp(registers.eax, self.r_stop))
                        #else:
                        #  oldm = self.r_stop
                        #  m = memory.MemRef(oldm.base, oldm.disp+8, oldm.index, oldm.scale, oldm.data_size)
                        #  self.code.add(x86.cmp(registers.eax, m))
                        #self.code.add(x86.pop(registers.eax))
                    else:
                        self.code.add(x86.mov(self.r_clobber, self.r_count))
                        self.code.add(x86.cmp(self.r_clobber, self.r_stop))
                else:
                    self.code.add(x86.cmp(self.r_count, self.r_stop))
            else:
                self.code.add(x86.cmp(self.r_count, self.n))
            self.code.add(x86.jnge(self.start_label))

        return
Beispiel #2
0
  def end(self):
    """Do post-loop iterator code"""
    if self.mode == CTR:
      self.code.add(x86.loop(self.start_label))

    elif self.mode == DEC:
      # branch if r_count is not zero (CR)
      #   Note that this relies on someone (e.g. cleanup()) setting the
      #   condition register properly.
      if self.step_size() == 1:
        self.code.add(x86.jnz(self.start_label))
      else:
        self.code.add(x86.cmp(self.r_count, 0))
        self.code.add(x86.jg(self.start_label))

    elif self.mode == INC:
      if self.external_stop:
        if isinstance(self.r_count, memory.MemRef) and isinstance(self.r_stop, memory.MemRef):
          if self.r_clobber == None:
            raise Exception('Must specify clobber_reg if count and stop values are both stored in memory.')
            #self.code.add(x86.push(registers.eax))
            #if self.r_count.base != registers.esp:
            #  self.code.add(x86.mov(registers.eax, self.r_count))
            #else:
            #  oldm = self.r_count
            #  m = memory.MemRef(oldm.base, oldm.disp+8, oldm.index, oldm.scale, oldm.data_size)
            #  self.code.add(x86.mov(registers.eax, m))
            #if self.r_stop.base != registers.esp:
            #  self.code.add(x86.cmp(registers.eax, self.r_stop))
            #else:
            #  oldm = self.r_stop
            #  m = memory.MemRef(oldm.base, oldm.disp+8, oldm.index, oldm.scale, oldm.data_size)
            #  self.code.add(x86.cmp(registers.eax, m))
            #self.code.add(x86.pop(registers.eax))
          else:
            self.code.add(x86.mov(self.r_clobber, self.r_count))
            self.code.add(x86.cmp(self.r_clobber, self.r_stop))
        else:
          self.code.add(x86.cmp(self.r_count, self.r_stop))
      else:
        self.code.add(x86.cmp(self.r_count, self.n))
      self.code.add(x86.jnge(self.start_label))

      

    return
def Test():
    prgm = env.Program()
    code = prgm.get_stream()
    proc = env.Processor()
    params = env.ExecParams()
    params.p1 = 3
    mr32 = MemRef(ebp, 8)
    mr8 = MemRef(ebp, 8, data_size = 8)


    lbl1 = prgm.get_label("lbl1")
    lbl2 = prgm.get_label("lbl2")

    code.add(x86_isa.xor(eax, eax))

    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.jne(lbl1))

    code.add(x86_isa.ret())

    code.add(lbl1)
    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.je(lbl2))
    code.add(x86_isa.add(eax, 12))
    code.add(lbl2)

    prgm += code
    ret = proc.execute(prgm)
    print "ret", ret
    assert(ret == 12)

    prgm = env.Program()
    code = prgm.get_stream()

    fwd_lbl = prgm.get_label("FORWARD")
    bck_lbl = prgm.get_label("BACKWARD")

    code.add(x86_isa.xor(eax, eax))
    code.add(bck_lbl)
    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.jne(fwd_lbl))
    for i in xrange(0, 65):
      code.add(x86_isa.pop(edi))
    code.add(fwd_lbl)

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    assert(ret == 0)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")
    out_lbl = prgm.get_label("OUT")
    skip_lbl = prgm.get_label("SKIP")

    code.add(x86_isa.xor(eax, eax))
    code.add(loop_lbl)
    r_tmp = prgm.acquire_register()
    for i in range(0, 1):
      for i in xrange(0, 24):
        code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))

      code.add(x86_isa.add(eax, 4))
      code.add(x86_isa.cmp(eax, 20))
      code.add(x86_isa.je(out_lbl))

      for i in xrange(0, 24):
        code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))

      code.add(x86_isa.cmp(eax, 32))
      code.add(x86_isa.jne(loop_lbl))

    code.add(out_lbl)

    code.add(x86_isa.jmp(skip_lbl))
    for i in xrange(0, 2):
      code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))
    code.add(skip_lbl)

    prgm.release_register(r_tmp)

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 20)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")
    else_lbl = prgm.get_label("ELSE")
    finish_lbl = prgm.get_label("finish")

    code.add(x86_isa.mov(eax, -1))
    code.add(x86_isa.mov(edx, 0))

    code.add(loop_lbl)

    code.add(x86_isa.add(eax, 1))
    code.add(x86_isa.cmp(eax, 16))
    code.add(x86_isa.jge(finish_lbl))

    code.add(x86_isa.add(edx, eax))
    code.add(x86_isa.mov(edx, edi))
    code.add(x86_isa.and_(edi, 0x1))
    code.add(x86_isa.jnz(else_lbl))

    code.add(x86_isa.add(edx, 1))
    code.add(x86_isa.jmp(loop_lbl))

    code.add(else_lbl)
    code.add(x86_isa.add(edx, edi))
    code.add(x86_isa.jmp(loop_lbl))

    code.add(finish_lbl)
    code.add(x86_isa.mov(eax, edx))

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 1)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")

    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.xor(ecx, ecx))
    code.add(x86_isa.mov(edx, 1))

    code.add(loop_lbl)
    code.add(x86_isa.inc(eax))
    code.add(x86_isa.cmp(eax, 7))
    code.add(x86_isa.cmove(ecx, edx))
    code.add(x86_isa.jecxz(loop_lbl))

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 7)


    prgm = env.Program()
    code = prgm.get_stream()

    code.add(x86_isa.add(eax, 200))
    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.add(al, 32))
    code.add(x86_isa.add(bl, 32))
    code.add(x86_isa.xor(bl, bl))
    code.add(x86_isa.mov(mr8, al))
    code.add(x86_isa.add(mr32, 0))
    code.add(x86_isa.mov(eax, mr32))
    code.add(x86_isa.mov(al, mr8))

    code.add(x86_isa.imul(ax, ax, 4))
    code.add(x86_isa.imul(ebx, eax, 10))
    code.add(x86_isa.mov(cx, 1232))
    code.add(x86_isa.sub(ax, cx))
    code.add(x86_isa.xor(eax,eax))
    code.add(x86_isa.mov(eax,ebx))
    code.add(x86_isa.clc())
    code.add(x86_isa.rcl(eax, 1))
    code.add(x86_isa.rcr(eax, 1))


    prgm += code
    #ret = proc.execute(code, debug = True, params = params)
    id1 = proc.execute(prgm, params = params, mode = 'int', async = True)
    id2 = proc.execute(prgm, params = params, mode = 'int', async = True)
    ret = proc.execute(prgm, params = params, mode = 'int')
    print "Return main thread: %d" % (ret)
    ret = proc.join(id1)
    print "Return thread 1: %d" % (ret)
    ret = proc.join(id2)
    print "Return thread 2: %d" % (ret)

    prgm = env.Program()
    code = prgm.get_stream()
    code.add(x86_isa.fldpi())
    code.add(x86_isa.fld1())
    code.add(x86_isa.fadd(st0, st0))
    code.add(x86_isa.fmulp())
    code.add(x86_isa.fsin())
    code.add(x86_isa.fcos())
    code.add(x86_isa.fld1())
    code.add(x86_isa.fyl2xp1())

    prgm += code
    ret = proc.execute(prgm, params = params, mode = 'fp')
    print "Return main thread: %f" % (ret)

    prgm = env.Program()
    code = prgm.get_stream()
    code.add(x86_isa.movd(xmm0, mr32))
    code.add(x86_isa.mov(ebx, mr32))
    code.add(x86_isa.movd(xmm1, ebx))
    code.add(x86_isa.paddq(xmm0, xmm1))
    code.add(x86_isa.pextrw(ecx, xmm0, 0))
    code.add(x86_isa.pxor(mm1, mm1))
    code.add(x86_isa.pinsrw(mm1, ecx, 0))
    code.add(x86_isa.movq2dq(xmm0, mm1))
    code.add(x86_isa.movdq2q(mm2, xmm0))
    code.add(x86_isa.movd(edx,mm2))
    code.add(x86_isa.movd(xmm5,edx))
    code.add(x86_isa.movd(ecx, xmm5))
    code.add(x86_isa.pxor(xmm6, xmm6))
    code.add(x86_isa.pinsrw(xmm6, ecx, 0))
    code.add(x86_isa.movd(eax, xmm6))
    code.add(x86_isa.emms())

    prgm += code
    prgm.print_code(hex = True)
    ret = proc.execute(prgm, params = params, mode = 'int')
    print "Return main thread: %d" % (ret)


    # Try the LOCK prefix
    prgm = env.Program()
    code = prgm.get_stream()

    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.add(mr32, eax))
    code.add(x86_isa.add(mr32, eax, lock = True))

    prgm += code
    prgm.print_code(hex = True)

    proc.execute(prgm, params = params)
    return