#if TEST_AOT_COMPILE

#define YARV_AOT_COMPILED 1

#include <ruby.h>
#include <node.h>
#include <env.h>

#include "yarvcore.h"
#include "vm.h"
#include "insnhelper.h"
#include "call_cfunc.inc"

#undef  INSN_ENTRY
#define INSN_ENTRY(x)

#undef  ADD_PC
#define ADD_PC(x)

#undef  END_INSN
#define END_INSN(insn)

#undef  SCREG
#define SCREG(r) reg_##r

#define FIXNUM_2_P(a, b) ((a) & (b) & 1)
#define BASIC_OP_UNREDEFINED(op) (yarv_unredefined_flag)

#undef  GET_OPERAND
#define GET_OPERAND(x) constant_pool[x + OPERAND_IDX]

#undef  JUMP
#define JUMP(lab) goto lab

#undef  CLEAR_ENV
#define CLEAR_ENV(x)

#undef NEXT_INSN
#define NEXT_INSN()

#ifndef HAVE_TYPE_ULONG
typedef unsigned long ulong;
#endif

extern int yarv_unredefined_flag;

#include "compiled_constantpool.inc"

VALUE compiled_func(){
  VALUE *reg_lfp;
  VALUE *reg_dfp;
  VALUE *reg_cfp;
  VALUE *reg_sp;
  VALUE *reg_pc;
  VALUE  throwed;
  
  VALUE self = yarv_get_current_running_thread();
  struct thread_object *th;
  
#ifdef OPT_STACK_CACHING
  register VALUE reg_a, reg_b;
  reg_a = reg_b = 0;
#endif

  if(IS_YARV_WORKING()){
    rb_bug("Using compiled function without YARV");
  }
  
  GetThreadVal(self , th);
  reg_sp  = th->sp;
  reg_lfp = th->lfp;
  reg_dfp = th->dfp;
  reg_cfp = th->cfp;

//#define INSN_ENTRY(x) printf("insn: %s\n", #x);

#include "compiled.inc"
finish:
  return throwed;
}

extern VALUE constant_pool_pool;

void Init_compiled(){
  int i;

  INIT_CONSTANT_POOL();
  
  for(i=0; i<sizeof(constant_pool)/sizeof(constant_pool[0]); i++){
    if(!SPECIAL_CONST_P(constant_pool[i])){
      rb_ary_push(constant_pool_pool, constant_pool[i]);
    }
  }
  
  rb_define_global_function("compiled", compiled_func, 1);
}

#endif
