summaryrefslogtreecommitdiff
path: root/tool/compile_prelude.rb
blob: ea77729311b4597cc7875aff5f8c59940f81ac72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# This file is interpreted by $(BASERUBY) and miniruby.
# $(BASERUBY) is used for miniprelude.c.
# miniruby is used for prelude.c.
# Since $(BASERUBY) may be older than Ruby 1.9,
# Ruby 1.9 feature should not be used.

preludes = ARGV.dup
outfile = preludes.pop

C_ESC = {
  "\\" => "\\\\",
  '"' => '\"',
  "\n" => '\n',
}

0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\x%02x" % ch }
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\x%02x" % ch }
C_ESC_PAT = Regexp.union(*C_ESC.keys)

def c_esc(str)
  '"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
end

mkconf = nil
setup_ruby_prefix = nil
teardown_ruby_prefix = nil
lines_list = preludes.map {|filename|
  lines = []
  need_ruby_prefix = false
  File.readlines(filename).each {|line|
    line.gsub!(/RbConfig::CONFIG\["(\w+)"\]/) {
      unless mkconf
        require 'rbconfig'
        mkconf = RbConfig::MAKEFILE_CONFIG.merge('prefix'=>'#{TMP_RUBY_PREFIX}')
        exlen = $:.grep(%r{\A/}).last.length - RbConfig::CONFIG["prefix"].length
        setup_ruby_prefix = "TMP_RUBY_PREFIX = $:.grep(%r{\\A/}).last[0..#{-exlen-1}]\n"
        teardown_ruby_prefix = 'Object.class_eval { remove_const "TMP_RUBY_PREFIX" }'
      end
      if RbConfig::MAKEFILE_CONFIG.has_key? $1
        val = RbConfig.expand("$(#$1)", mkconf)
        need_ruby_prefix = true if /\A\#{TMP_RUBY_PREFIX\}/ =~ val
        c_esc(val)
      else
        $&
      end
    }
    lines << c_esc(line)
  }
  setup_lines = []
  if need_ruby_prefix
    setup_lines << c_esc(setup_ruby_prefix)
    lines << c_esc(teardown_ruby_prefix)
  end
  [setup_lines, lines]
}

def serb(str, var)
  result = ''
  str.each_line {|line|
    if /\A!/ =~ line
      result << $'
    else
      line.split(/(<%.*?%>)/).each {|x|
        if /\A<%(.*)%>\z/ =~ x
          result << "#{var} << (#{$1}).to_s\n"
        else
          result << "#{var} << #{x.dump}\n"
        end
      }
    end
  }
  result
end

tmp = ''
eval(serb(<<'EOS', 'tmp'))
#include "ruby/ruby.h"
#include "vm_core.h"

! preludes.zip(lines_list).each_with_index {|(prelude, (setup_lines, lines)), i|
static const char prelude_name<%i%>[] = <%c_esc(File.basename(prelude))%>;
static const char prelude_code<%i%>[] =
!   (setup_lines+lines).each {|line|
<%line%>
!   }
;
! }

void
Init_prelude(void)
{
! lines_list.each_with_index {|(setup_lines, lines), i|
  rb_iseq_eval(rb_iseq_compile(
    rb_str_new(prelude_code<%i%>, sizeof(prelude_code<%i%>) - 1),
    rb_str_new(prelude_name<%i%>, sizeof(prelude_name<%i%>) - 1),
    INT2FIX(<%1-setup_lines.length%>)));

! }
#if 0
! preludes.length.times {|i|
    puts(prelude_code<%i%>);
! }
#endif
}
EOS

open(outfile, 'w'){|f|
  f << tmp
}