/* -*- mode:c; style:ruby; coding: utf-8; indent-tabs-mode: nil -*- */

% # Copyright (c) 2018 Takashi Kokubun.  All rights reserved.
% #
% # This file is a part of  the programming language Ruby.  Permission is hereby
% # granted, to either  redistribute and/or modify this file,  provided that the
% # conditions mentioned  in the  file COPYING  are met.   Consult the  file for
% # details.
<%= render 'copyright' %>
%
% # This is an ERB template that generates Ruby code that generates C code that
% # generates JIT-ed C code.
<%= render 'notice', locals: {
    this_file: 'is the main part of compile_insn() in mjit_compile.c',
    edit: __FILE__,
} -%>
%
% unsupported_insns = [
%   'getblockparamproxy',  # TODO: support this
%   'defineclass',         # low priority
%   'opt_call_c_function', # low priority
% ]
%
% opt_send_without_block = RubyVM::Instructions.find { |i| i.name == 'opt_send_without_block' }
% if opt_send_without_block.nil?
%   raise 'opt_send_without_block not found'
% end
%
% # Available variables and macros in JIT-ed function:
% #   ec: the first argument of _mjitXXX
% #   reg_cfp: the second argument of _mjitXXX
% #   GET_CFP(): refers to `reg_cfp`
% #   GET_EP(): refers to `reg_cfp->ep`
% #   GET_SP(): refers to `reg_cfp->sp`
% #   INC_SP(): refers to `reg_cfp->sp`
% #   SET_SV(): refers to `reg_cfp->sp`
% #   PUSH(): refers to `SET_SV()`, `INC_SP()`
% #   GET_SELF(): refers to `reg_cfp->self`
% #   GET_LEP(): refers to `VM_EP_LEP(reg_cfp->ep)`
% #   EXEC_EC_CFP(): refers to `val = vm_exec(ec)` with frame setup
% #   CALL_METHOD(): using `GET_CFP()` and `EXEC_EC_CFP()`
% #   TOPN(): refers to `reg_cfp->sp`, which needs to have correct sp (of course)
% #   STACK_ADDR_FROM_TOP(): refers to `reg_cfp->sp`, same problem here
% #   DISPATCH_ORIGINAL_INSN(): expanded in _mjit_compile_insn.erb
% #   THROW_EXCEPTION(): specially defined for JIT
% #   RESTORE_REGS(): specially defined for `leave`

switch (insn) {
% (RubyVM::BareInstructions.to_a + RubyVM::OperandsUnifications.to_a).each do |insn|
%   next if unsupported_insns.include?(insn.name)
  case BIN(<%= insn.name %>):
%   if %w[opt_send_without_block send].include?(insn.name)
<%= render 'mjit_compile_send', locals: { insn: insn } -%>
%   elsif %w[opt_aref].include?(insn.name) # experimental. TODO: increase insns and make the list automatically by finding DISPATCH_ORIGINAL_INSN
<%= render 'mjit_compile_send', locals: { insn: opt_send_without_block } -%>
%   end
<%= render 'mjit_compile_insn', locals: { insn: insn, dispatched: false } -%>
    break;
% end
% # We don't support InstructionsUnifications yet because it's not used for now.
% # We don't support TraceInstructions yet. There is no blocker for it but it's just not implemented.
  default:
    if (mjit_opts.warnings || mjit_opts.verbose)
        fprintf(stderr, "MJIT warning: Skipped to compile unsupported instruction: %s\n", insn_name(insn));
    status->success = FALSE;
    break;
}

/* if next_pos is already compiled, next instruction won't be compiled in C code and needs `goto`. */
if ((next_pos < body->iseq_size && status->compiled_for_pos[next_pos]))
    fprintf(f, "goto label_%d;\n", next_pos);
