2018-02-12 01:54:25 -05:00
|
|
|
% # -*- 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.
|
|
|
|
%
|
|
|
|
% to_cstr = lambda do |line|
|
|
|
|
% normalized = line.gsub(/\t/, ' ' * 8)
|
|
|
|
% indented = normalized.sub(/\A(?!#)/, ' ') # avoid indenting preprocessor
|
|
|
|
% rstring2cstr(indented.rstrip).sub(/"\z/, '\\n"')
|
|
|
|
% end
|
|
|
|
%
|
2018-02-12 02:27:48 -05:00
|
|
|
% #
|
|
|
|
% # Expand simple macro, which doesn't require dynamic C code.
|
|
|
|
% #
|
|
|
|
% expand_simple_macros = lambda do |arg_expr|
|
|
|
|
% arg_expr.dup.tap do |expr|
|
|
|
|
% # For `opt_xxx`'s fallbacks.
|
|
|
|
% expr.gsub!(/\bDISPATCH_ORIGINAL_INSN\([^)]+\);/, 'return Qundef; /* cancel JIT */')
|
|
|
|
%
|
|
|
|
% # For `leave`. We can't proceed next ISeq in the same JIT function.
|
|
|
|
% expr.gsub!(/^(?<indent>\s*)RESTORE_REGS\(\);\n/) do
|
|
|
|
% indent = Regexp.last_match[:indent]
|
2018-02-12 04:02:30 -05:00
|
|
|
% <<-RESTORE_REGS.gsub(/^ +/, '')
|
2018-02-12 02:27:48 -05:00
|
|
|
% #if OPT_CALL_THREADED_CODE
|
|
|
|
% #{indent}rb_ec_thread_ptr(ec)->retval = val;
|
|
|
|
% #{indent}return 0;
|
|
|
|
% #else
|
|
|
|
% #{indent}return val;
|
|
|
|
% #endif
|
|
|
|
% RESTORE_REGS
|
|
|
|
% end
|
2018-02-12 01:54:25 -05:00
|
|
|
% end
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
|
|
|
%
|
|
|
|
% #
|
|
|
|
% # Print a body of insn, but with macro expansion.
|
|
|
|
% #
|
|
|
|
% expand_simple_macros.call(insn.expr.expr).each_line do |line|
|
|
|
|
% #
|
|
|
|
% # Expand dynamic macro here (only JUMP for now)
|
|
|
|
% #
|
|
|
|
% if line =~ /\A\s+JUMP\((?<dest>[^)]+)\);\s+\z/
|
|
|
|
% dest = Regexp.last_match[:dest]
|
2018-02-12 01:54:25 -05:00
|
|
|
%
|
2018-02-12 02:27:48 -05:00
|
|
|
% if insn.name == 'opt_case_dispatch' # special case... TODO: use another macro to avoid checking name
|
2018-02-12 01:54:25 -05:00
|
|
|
{
|
|
|
|
struct case_dispatch_var arg;
|
|
|
|
arg.f = f;
|
|
|
|
arg.base_pos = pos + insn_len(insn);
|
|
|
|
arg.last_value = Qundef;
|
|
|
|
|
|
|
|
fprintf(f, " switch (<%= dest %>) {\n");
|
|
|
|
st_foreach(RHASH_TBL_RAW(hash), compile_case_dispatch_each, (VALUE)&arg);
|
|
|
|
fprintf(f, " case %lu:\n", else_offset);
|
|
|
|
fprintf(f, " goto label_%lu;\n", arg.base_pos + else_offset);
|
|
|
|
fprintf(f, " }\n");
|
|
|
|
}
|
2018-02-12 02:27:48 -05:00
|
|
|
% else
|
|
|
|
% # Before we `goto` next insn, we need to set return values, especially for getinlinecache
|
|
|
|
% insn.rets.reverse_each.with_index do |ret, i|
|
|
|
|
% # TOPN(n) = ...
|
|
|
|
fprintf(f, " stack[%d] = <%= ret.fetch(:name) %>;\n", b->stack_size + (int)<%= insn.call_attribute('sp_inc') %> - <%= i + 1 %>);
|
|
|
|
% end
|
|
|
|
%
|
2018-02-12 01:54:25 -05:00
|
|
|
next_pos = pos + insn_len(insn) + (unsigned int)<%= dest %>;
|
|
|
|
fprintf(f, " goto label_%d;\n", next_pos);
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
|
|
|
% else
|
2018-02-12 01:54:25 -05:00
|
|
|
fprintf(f, <%= to_cstr.call(line) %>);
|
2018-02-12 02:27:48 -05:00
|
|
|
% end
|
2018-02-12 01:54:25 -05:00
|
|
|
% end
|