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
|
op_table = {
"!" => "not",
"!=" => "neq",
"!~" => "nmatch",
"%" => "mod",
"&" => "and",
"&&" => "andand",
"*" => "mul",
"**" => "pow",
"+" => "add",
"+@" => "plus",
"-" => "sub",
"-@" => "minus",
"/" => "div",
"<" => "lt",
"<=" => "le",
"<<" => "lshift",
"<=>" => "cmp",
"==" => "eq",
"===" => "eqq",
"=~" => "match",
">" => "gt",
">=" => "ge",
">>" => "rshift",
"[]" => "aref",
"[]=" => "aset",
"^" => "xor",
"`" => "tick",
"|" => "or",
"||" => "oror",
"~" => "neg",
}
macro_to_symbol = {
# Macro Symbol
# [prefix, suffix] => [prefix, suffix]
["CV" , "" ] => ["@@" , "" ],
["IV" , "" ] => ["@" , "" ],
["" , "_B" ] => ["" , "!" ],
["" , "_Q" ] => ["" , "?" ],
["" , "_E" ] => ["" , "=" ],
["" , "" ] => ["" , "" ],
}
core_cfiles = Dir.glob("#{MRUBY_ROOT}/src/*.c")
core_rbfiles = Dir.glob("#{MRUBY_ROOT}/mrblib/*.rb")
MRuby.each_target do |build|
cfiles = core_cfiles.dup
rbfiles = core_rbfiles.dup
psfiles = []
presym_file = build.presym_file
presym_inc = build.presym_inc
build.compilers.each {|c| c.include_paths << "#{build.build_dir}/include"}
build.gems.each do |gem|
cfiles.concat(Dir.glob(gem.dir+"/{src,core,tools/*}/*.c"))
if gem.cdump?
rbfiles.concat(gem.rbfiles)
psfiles.concat(Dir.glob(gem.dir+"/**/presym"))
end
gem.compilers.each {|c| c.include_paths << "#{build.build_dir}/include"}
end
file presym_file => [*cfiles, *rbfiles, *psfiles, __FILE__] do
prefix_re = Regexp.union(*macro_to_symbol.keys.uniq.map(&:first))
suffix_re = Regexp.union(*macro_to_symbol.keys.uniq.map(&:last))
macro_re = /MRB_(#{prefix_re})SYM(#{suffix_re})\((\w+)\)/o
csymbols = cfiles.map do |f|
src = File.read(f)
src.gsub!(/\/\/.+(\n|$)/, "\n")
[src.scan(/intern_lit\([^\n"]*"([^\n "]*)"/),
src.scan(/mrb_define_method\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_class_method\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_class\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_module\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_module_function\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_const\([^\n"]*"([^\n"]*)"/),
src.scan(/mrb_define_global_const\([^\n"]*"([^\n"]*)"/),
src.scan(macro_re).map{|prefix, suffix, name|
macro_to_symbol[[prefix, suffix]] * name
}]
end
csymbols += File.readlines("#{MRUBY_ROOT}/include/mruby.h").grep(/define E_/).join.scan(/MRB_SYM\((\w+)\)/)
rbsymbols = rbfiles.map do |f|
src = File.read(f)
src.force_encoding(Encoding::BINARY)
[src.scan(/\bclass +([A-Z]\w*)/),
src.scan(/\bmodule +([A-Z]\w*)/),
src.scan(/\bdef +(\w+[!?=]?)/),
src.scan(/\balias +(\w+[!?]?)/),
src.scan(/\b([A-Z]\w*) *=[^=]/),
src.scan(/(\$[a-zA-Z_]\w*)/),
src.scan(/(\$[$!?0-9]\w*)/),
src.scan(/(@@?[a-zA-Z_]\w*)/),
src.scan(/[^.]\.([a-zA-Z_]\w*[!?]?)/),
src.scan(/\.([a-zA-Z_]\w* *=)/).map{|x|x.map{|s|s.gsub(' ', '')}},
src.scan(/\b([a-zA-Z_]\w*):/),
src.scan(/:([a-zA-Z_]\w*[!?=]?)/),
src.scan(/[\(\[\{ ]:"([^"]+)"/).map{|x|x.map{|s|s.gsub('\#', '#')}},
src.scan(/[ \(\[\{]:'([^']+)'/)
]
end
symbols = [csymbols, rbsymbols, op_table.keys, psfiles.map{|file| symbols.concat(File.readlines(file).grep_v(/^# /))}]
symbols = symbols.flatten.compact.uniq.grep_v(/#/).map{|x| x.gsub("\n", '\n')}.sort_by!{|x| [x.bytesize, x]}
presyms = File.readlines(presym_file) rescue []
presyms.each{|x| x.chomp!}
if presyms != symbols
_pp "GEN", presym_file.relative_path
mkdir_p(File.dirname(presym_file))
File.write(presym_file, symbols.join("\n"))
Rake::Task["gensym:#{presym_inc}"].invoke
end
end
task "gensym:#{presym_inc}" do
presyms = File.readlines(presym_file)
presyms.each{|x| x.chomp!}
symbol_to_macro = macro_to_symbol.invert
prefix_re = Regexp.union(*symbol_to_macro.keys.uniq.map(&:first))
suffix_re = Regexp.union(*symbol_to_macro.keys.uniq.map(&:last))
macro_re = /\A(#{prefix_re})?([\w&&\D]\w*)(#{suffix_re})?\z/o
mkdir_p(File.dirname(presym_inc))
_pp "GEN", presym_inc.relative_path
File.open(presym_inc, "w") do |f|
f.puts "/* MRB_PRESYM_NAMED(lit, num, type, name) */"
f.puts "/* MRB_PRESYM_UNNAMED(lit, num) */"
presyms.each.with_index(1) do |sym, num|
if macro_re =~ sym && (affixes = symbol_to_macro[[$1, $3]])
f.puts %|MRB_PRESYM_NAMED("#{sym}", #{num}, #{affixes * 'SYM'}, #{$2})|
elsif name = op_table[sym]
f.puts %|MRB_PRESYM_NAMED("#{sym}", #{num}, OPSYM, #{name})|
elsif
f.puts %|MRB_PRESYM_UNNAMED("#{sym}", #{num})|
end
end
f.print "#define MRB_PRESYM_MAX #{presyms.size}"
end
end
end
|