예제 #1
0
파일: bp.c 프로젝트: pcpa/lightning
int main()
{
  pifi      nfibs;
  int	    in;				/* offset of the argument */
  jit_insn  *ref;			/* to patch the forward reference */
  jit_insn  *mref;                     /* ref of move to backpatch */
  jit_insn  *tp;                       /* location to patch */

  codeBuffer = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC,
                    MAP_PRIVATE | MAP_ANON, -1, 0);
  if (codeBuffer == MAP_FAILED) {
    perror("mmap");
    exit(0);
  }

  nfibs = (pifi) (jit_set_ip(codeBuffer).iptr);

        jit_prolog   (1);
  in =  jit_arg_ui   ();
        jit_getarg_ui(JIT_V0, in);              /* V0 = n */
  mref= jit_movi_p(JIT_V2,jit_forward ());      /* Generate a dumb movi */
        jit_jmpr(JIT_V2);
        /* generate some dump filler that will never be executed!*/
        jit_addi_ui(JIT_V0,JIT_V0,1);
        jit_addi_ui(JIT_V0,JIT_V0,1);        
        jit_addi_ui(JIT_V0,JIT_V0,1);        
        jit_addi_ui(JIT_V0,JIT_V0,1);        
  tp  = jit_get_label ();
  ref = jit_blti_ui  (jit_forward(), JIT_V0, 2);
        jit_subi_ui  (JIT_V1, JIT_V0, 1);       /* V1 = n-1 */
        jit_subi_ui  (JIT_V2, JIT_V0, 2);       /* V2 = n-2 */
        jit_prepare  (1);
          jit_pusharg_ui(JIT_V1);
        jit_finish(nfibs);
        jit_retval(JIT_V1);                     /* V1 = nfibs(n-1) */
        jit_prepare(1);
          jit_pusharg_ui(JIT_V2);
        jit_finish(nfibs);
        jit_retval(JIT_V2);                     /* V2 = nfibs(n-2) */
        jit_addi_ui(JIT_V1,  JIT_V1,  1);
        jit_addr_ui(JIT_RET, JIT_V1, JIT_V2);   /* RET = V1 + V2 + 1 */
        jit_ret();

  jit_patch(ref);                               /* patch jump */
        jit_movi_i(JIT_RET, 1);                 /* RET = 1 */
        jit_ret();

  jit_patch_movi(mref,tp);                      /* Ok. Do the back-patching */

  /* call the generated code, passing 32 as an argument */
  jit_flush_code(codeBuffer, jit_get_ip().ptr);

#ifdef LIGHTNING_DISASSEMBLE
  disassemble(stderr, (char *)codeBuffer, jit_get_ip().ptr);
#endif
#ifndef LIGHTNING_CROSS
  printf("nfibs(%d) = %d\n", 32, nfibs(32));
#endif
  return 0;
}
예제 #2
0
int main()
{
  pifi      nfibs = (pifi) (jit_set_ip(codeBuffer).iptr);
  int	    in;				/* offset of the argument */
  jit_insn  *ref;			/* to patch the forward reference */
  jit_insn  *loop;			/* start of the loop */

        jit_prolog   (1);
  in =  jit_arg_ui   ();
        jit_getarg_ui(JIT_R2, in);              /* V0 = n */
        jit_movi_ui  (JIT_R1, 1);
  ref = jit_blti_ui  (jit_forward(), JIT_R2, 2);
        jit_subi_ui  (JIT_R2, JIT_R2, 1);
        jit_movi_ui  (JIT_R0, 1);

  loop= jit_get_label();
        jit_subi_ui  (JIT_R2, JIT_R2, 1);	/* we'll calculate one more */
        jit_addr_ui  (JIT_V0, JIT_R0, JIT_R1);  /* V0 = R0 + R1 */
        jit_movr_ui  (JIT_R0, JIT_R1);          /* R0 = R1 */
        jit_addi_ui  (JIT_R1, JIT_V0, 1);       /* R1 = V0 + 1 */
        jit_bnei_ui  (loop, JIT_R2, 0);         /* if (R2) goto loop; */

  jit_patch(ref);                               /* patch forward jump */
        jit_movr_ui  (JIT_RET, JIT_R1);         /* RET = R1 */
        jit_ret();

  jit_flush_code(codeBuffer, jit_get_ip().ptr);

#ifdef LIGHTNING_DISASSEMBLE
  disassemble(stderr, (char *) codeBuffer, jit_get_ip().ptr);
#endif
#ifndef LIGHTNING_CROSS
  /* call the generated code, passing 36 as an argument */
  printf("nfibs(%d) = %d\n", 36, nfibs(36));
#endif
  return 0;
}