summaryrefslogtreecommitdiff
path: root/template/prelude.c.tmpl
blob: a359e45cf8c417c2d027b755c95da6d2233a0cbf (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<%
# 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.

class Prelude
  LINE_LIMIT = 509 # by C89

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

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

  def c_esc(str)
    str.gsub(C_ESC_PAT) { C_ESC[$&] }
  end
  def prelude_base(filename)
    filename.chomp(".rb")
  end
  def prelude_name(filename)
    "<internal:" + prelude_base(filename) + ">"
  end

  def initialize(init_name, preludes, vpath)
    @init_name = init_name
    @have_sublib = false
    @vpath = vpath
    @preludes = {}
    @mains = preludes.map {|filename| translate(filename)[0]}
    @preludes.delete_if {|_, (_, _, lines, sub)| sub && lines.empty?}
  end

  def translate(filename, sub = false)
    idx = @preludes[filename]
    return idx if idx
    lines = []
    result = [@preludes.size, @vpath.strip(filename), lines, sub]
    @vpath.foreach(filename) do |line|
      @preludes[filename] ||= result
      comment = ($1 || '' if line.sub!(/(?:^|\s+)\#(?:$|[#\s](.*))/, ''))
      if line.size > LINE_LIMIT
        raise "#{filename}:#{lines.size+1}: too long line"
      end
      line.sub!(/require(_relative)?\s*\(?\s*(["'])(.*?)(?:\.rb)?\2\)?/) do
        orig, rel, path = $&, $2, $3
        if rel
          path = File.join(File.dirname(filename), path)
          nil while path.gsub!(%r'(\A|/)(?!\.\.?/)[^/]+/\.\.(?:/|\z)', '')
        end
        path = translate("#{path}.rb", true) rescue nil
        if path
          @have_sublib = true
          "TMP_RUBY_PREFIX.require(#{path[0]})"
        else
          orig
        end
      end
      lines << [line, comment]
    end
    result
  end
end
Prelude.new(output && output[/\w+(?=_prelude.c\b)/] || 'prelude', ARGV, vpath).instance_eval do
-%>
/* -*-c-*-
 THIS FILE WAS AUTOGENERATED BY template/prelude.c.tmpl. DO NOT EDIT.

 sources: <%= @preludes.map {|n,*| prelude_base(n)}.join(', ') %><%=%>
*/
%unless @preludes.empty?
#include "ruby/ruby.h"
#include "internal.h"
#include "vm_core.h"
#include "iseq.h"

% preludes = @preludes.values.sort
% preludes.each {|i, prelude, lines, sub|

%   name = prelude_name(*prelude)
static const char prelude_name<%=i%><%=%>[] = "<%=c_esc(name)%>";
static const struct {
%   size = beg = 0
%   lines.each_with_index {|(line, comment), n|
%     if size + line.size < Prelude::LINE_LIMIT
%       size += line.size
%       next
%     end
    char L<%=beg%><%=%>[<%=size%><%=%>]; /* <%=beg+1%>..<%=n%> */
%     size = line.size
%     beg = n
%   }
%   if size > 0
    char L<%=beg%><%=%>[<%=size%><%=%>]; /* <%=beg+1%>..<%=lines.size+1%> */
%   end
} prelude_code<%=i%><%=%> = {
%   size = 0
#line 1 "<%=c_esc(prelude)%>"
%   lines.each_with_index {|(line, comment), n|
%     if size + line.size >= Prelude::LINE_LIMIT
%       size = 0
,
#line <%=n+1%> "<%=c_esc(prelude)%>"
%     end
%     size += line.size
"<%=c_esc(line)%>"<%if comment%>/* <%=c_esc(comment)%> */<%end%>
%   }
#line <%=_erbout.count("\n")+2%> "<%=@init_name%>.c"
};
% }

% if @have_sublib
#define PRELUDE_COUNT <%=preludes.size%>

struct prelude_env {
    volatile VALUE prefix_path;
#if PRELUDE_COUNT > 0
    char loaded[PRELUDE_COUNT];
#endif
};

static VALUE
prelude_prefix_path(VALUE self)
{
    struct prelude_env *ptr = DATA_PTR(self);
    return ptr->prefix_path;
}
% end

% unless preludes.empty?
#define PRELUDE_NAME(n) rb_usascii_str_new_static(prelude_name##n, sizeof(prelude_name##n)-1)
#define PRELUDE_CODE(n) rb_usascii_str_new_static(prelude_code##n.L0, sizeof(prelude_code##n))
#if defined __GNUC__ && __GNUC__ >= 5
# pragma GCC diagnostic push
# pragma GCC diagnostic error "-Wmissing-field-initializers"
#endif
static void
prelude_eval(VALUE code, VALUE name, int line)
{
    static const rb_compile_option_t optimization = {
	TRUE, /* int inline_const_cache; */
	TRUE, /* int peephole_optimization; */
	TRUE, /* int tailcall_optimization; */
	TRUE, /* int specialized_instruction; */
	TRUE, /* int operands_unification; */
	TRUE, /* int instructions_unification; */
	TRUE, /* int stack_caching; */
	TRUE, /* int frozen_string_literal; */
	FALSE, /* int debug_frozen_string_literal; */
	FALSE, /* unsigned int coverage_enabled; */
	0, /* int debug_level; */
    };

    rb_ast_t *ast = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
    if (!ast->body.root) {
	rb_ast_dispose(ast);
	rb_exc_raise(rb_errinfo());
    }
    rb_iseq_eval(rb_iseq_new_with_opt(&ast->body, name, name, Qnil, INT2FIX(line),
				      NULL, ISEQ_TYPE_TOP, &optimization));
    rb_ast_dispose(ast);
}
#if defined __GNUC__ && __GNUC__ >= 5
# pragma GCC diagnostic pop
#endif
% end

% if @have_sublib
static VALUE
prelude_require(VALUE self, VALUE nth)
{
    struct prelude_env *ptr = DATA_PTR(self);
    VALUE code, name;
    int n = FIX2INT(nth);

    if (n > PRELUDE_COUNT) return Qfalse;
    if (ptr->loaded[n]) return Qfalse;
    ptr->loaded[n] = 1;
    switch (n) {
%   @preludes.each_value do |i, prelude, lines, sub|
%     if sub
      case <%=i%><%=%>:
	code = PRELUDE_CODE(<%=i%><%=%>);
	name = PRELUDE_NAME(<%=i%><%=%>);
	break;
%     end
%   end
      default:
	return Qfalse;
    }
    prelude_eval(code, name, 1);
    return Qtrue;
}

% end
%end
void
Init_<%=@init_name%><%=%>(void)
{
%unless @preludes.empty?
% if @have_sublib
    struct prelude_env memo;
    ID name = rb_intern("TMP_RUBY_PREFIX");
    VALUE prelude = Data_Wrap_Struct(rb_cData, 0, 0, &memo);

    memo.prefix_path = rb_const_remove(rb_cObject, name);
    rb_const_set(rb_cObject, name, prelude);
    rb_define_singleton_method(prelude, "to_s", prelude_prefix_path, 0);
% end
% if @have_sublib
    memset(memo.loaded, 0, sizeof(memo.loaded));
    rb_define_singleton_method(prelude, "require", prelude_require, 1);
% end
% preludes.each do |i, prelude, lines, sub|
%   next if sub
    prelude_eval(PRELUDE_CODE(<%=i%><%=%>), PRELUDE_NAME(<%=i%><%=%>), 1);
% end
% if @have_sublib
    rb_gc_force_recycle(prelude);
% end

#if 0
% preludes.length.times {|i|
    printf("%.*s", (int)sizeof(prelude_code<%=i%><%=%>), prelude_code<%=i%><%=%>.L0);
% }
#endif
%end
}
<%end -%>