ytl 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ytl.rb +117 -20
- data/runtime/prelude.rb +26 -13
- data/runtime/test.rb +1 -0
- data/test/basictest.rb +39 -0
- data/test/breaktest.rb +84 -0
- data/test/classtest.rb +26 -0
- data/test/condtest.rb +22 -0
- data/test/exceptiontest.rb +5 -2
- data/test/execytl.rb +5 -0
- data/test/macrotest.rb +13 -4
- data/test/tmp.rb +6 -6
- metadata +12 -5
data/lib/ytl.rb
CHANGED
@@ -8,6 +8,8 @@ require 'optparse'
|
|
8
8
|
include YTLJit
|
9
9
|
module YTL
|
10
10
|
include YTLJit
|
11
|
+
|
12
|
+
VERSION = "0.0.3"
|
11
13
|
|
12
14
|
ISEQ_OPTS = {
|
13
15
|
:peephole_optimization => true,
|
@@ -37,7 +39,8 @@ module YTL
|
|
37
39
|
ytlopt[:dump_context] = f
|
38
40
|
end
|
39
41
|
|
40
|
-
opt.on('--write-code =FILE',
|
42
|
+
opt.on('-o FILE', '--write-code =FILE',
|
43
|
+
'Write generating naitive code and node objects') do |f|
|
41
44
|
ytlopt[:write_code] = f
|
42
45
|
end
|
43
46
|
|
@@ -56,10 +59,79 @@ module YTL
|
|
56
59
|
ytlopt[:execute_before_compile].push f
|
57
60
|
end
|
58
61
|
|
62
|
+
opt.on('-c', '--compile-only',
|
63
|
+
'Stop when compile finished (not execute compiled code)') do |f|
|
64
|
+
ytlopt[:compile_only] = f
|
65
|
+
end
|
66
|
+
|
67
|
+
opt.on('--compile-array-as-unboxed',
|
68
|
+
'Compile Array as unboxed if nesseary(not excape, not use special methods)') do |f|
|
69
|
+
ytlopt[:compile_array_as_uboxed] = f
|
70
|
+
end
|
71
|
+
|
59
72
|
opt.parse!(argv)
|
60
73
|
ytlopt
|
61
74
|
end
|
62
|
-
|
75
|
+
|
76
|
+
def self.dump_node(tnode, fn)
|
77
|
+
if defined? yield then
|
78
|
+
yield
|
79
|
+
end
|
80
|
+
File.open(fn, "w") do |fp|
|
81
|
+
ClassClassWrapper.instance_tab.keys.each do |klass|
|
82
|
+
if !klass.is_a?(ClassClassWrapper) then
|
83
|
+
fp.print "class #{klass.name}; end\n"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
fp.print "Marshal.load(<<'EOS')\n"
|
88
|
+
fp.print Marshal.dump(tnode)
|
89
|
+
fp.print "\n"
|
90
|
+
fp.print "EOS\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.reduced_main(prog, options)
|
95
|
+
tr_context = VM::YARVContext.new
|
96
|
+
|
97
|
+
import_ruby_object(tr_context)
|
98
|
+
|
99
|
+
tnode = nil
|
100
|
+
is = RubyVM::InstructionSequence.compile(prog, ARGV[0],
|
101
|
+
"", 0, ISEQ_OPTS).to_a
|
102
|
+
iseq = VMLib::InstSeqTree.new(nil, is)
|
103
|
+
|
104
|
+
tr = VM::YARVTranslatorCRubyObject.new([iseq])
|
105
|
+
tnode = tr.translate(tr_context)
|
106
|
+
|
107
|
+
ci_context = VM::CollectInfoContext.new(tnode)
|
108
|
+
ci_context.options = options
|
109
|
+
tnode.collect_info(ci_context)
|
110
|
+
|
111
|
+
dmylit = VM::Node::LiteralNode.new(tnode, nil)
|
112
|
+
arg = [dmylit, dmylit, dmylit]
|
113
|
+
sig = []
|
114
|
+
arg.each do |ele|
|
115
|
+
sig.push RubyType::BaseType.from_ruby_class(NilClass)
|
116
|
+
end
|
117
|
+
|
118
|
+
ti_context = VM::TypeInferenceContext.new(tnode)
|
119
|
+
ti_context.options = options
|
120
|
+
begin
|
121
|
+
tnode.collect_candidate_type(ti_context, arg, sig)
|
122
|
+
end until ti_context.convergent
|
123
|
+
ti_context = tnode.collect_candidate_type(ti_context, arg, sig)
|
124
|
+
|
125
|
+
c_context = VM::CompileContext.new(tnode)
|
126
|
+
c_context.current_method_signature.push sig
|
127
|
+
c_context.options = options
|
128
|
+
c_context = tnode.compile(c_context)
|
129
|
+
tnode.make_frame_struct_tab
|
130
|
+
|
131
|
+
tcs = tnode.code_space
|
132
|
+
tcs.call(tcs.base_address)
|
133
|
+
end
|
134
|
+
|
63
135
|
def self.main(options)
|
64
136
|
tr_context = VM::YARVContext.new
|
65
137
|
progs = []
|
@@ -73,26 +145,42 @@ module YTL
|
|
73
145
|
"", 0, ISEQ_OPTS).to_a
|
74
146
|
iseq = VMLib::InstSeqTree.new(nil, is)
|
75
147
|
tr = VM::YARVTranslatorCRubyObject.new([iseq])
|
148
|
+
tr_context.current_file_name = fn
|
76
149
|
tr.translate(tr_context)
|
77
150
|
end
|
78
151
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
152
|
+
tnode = nil
|
153
|
+
case File.extname(ARGV[0])
|
154
|
+
when ".ytl"
|
155
|
+
File.open(ARGV[0]) do |fp|
|
156
|
+
tnode = eval(fp.read, TOPLEVEL_BINDING)
|
157
|
+
end
|
158
|
+
tnode.update_after_restore
|
159
|
+
if tnode.code_space then
|
160
|
+
tnode.code_space.call(tnode.code_space.base_address)
|
161
|
+
return
|
162
|
+
end
|
163
|
+
|
164
|
+
when ".rb"
|
165
|
+
prog = File.read(ARGV[0])
|
166
|
+
is = RubyVM::InstructionSequence.compile(prog, ARGV[0],
|
167
|
+
"", 0, ISEQ_OPTS).to_a
|
168
|
+
iseq = VMLib::InstSeqTree.new(nil, is)
|
169
|
+
if options[:dump_yarv] then
|
170
|
+
pp iseq
|
171
|
+
end
|
86
172
|
|
87
|
-
|
88
|
-
|
173
|
+
tr = VM::YARVTranslatorCRubyObject.new([iseq])
|
174
|
+
tr_context.current_file_name = ARGV[0]
|
175
|
+
tnode = tr.translate(tr_context)
|
176
|
+
end
|
177
|
+
|
89
178
|
ci_context = VM::CollectInfoContext.new(tnode)
|
179
|
+
ci_context.options = options
|
90
180
|
tnode.collect_info(ci_context)
|
91
|
-
|
181
|
+
|
92
182
|
if fn = options[:write_node_before_ti] then
|
93
|
-
|
94
|
-
fp.print Marshal.dump(tnode)
|
95
|
-
end
|
183
|
+
dump_node(tnode, fn)
|
96
184
|
end
|
97
185
|
|
98
186
|
dmylit = VM::Node::LiteralNode.new(tnode, nil)
|
@@ -103,10 +191,15 @@ module YTL
|
|
103
191
|
end
|
104
192
|
|
105
193
|
ti_context = VM::TypeInferenceContext.new(tnode)
|
194
|
+
ti_context.options = options
|
106
195
|
begin
|
107
196
|
tnode.collect_candidate_type(ti_context, arg, sig)
|
108
197
|
end until ti_context.convergent
|
109
198
|
ti_context = tnode.collect_candidate_type(ti_context, arg, sig)
|
199
|
+
|
200
|
+
if fn = options[:write_node_after_ti] then
|
201
|
+
dump_node(tnode, fn)
|
202
|
+
end
|
110
203
|
|
111
204
|
c_context = VM::CompileContext.new(tnode)
|
112
205
|
c_context.current_method_signature.push sig
|
@@ -114,10 +207,10 @@ module YTL
|
|
114
207
|
c_context = tnode.compile(c_context)
|
115
208
|
tnode.make_frame_struct_tab
|
116
209
|
|
117
|
-
if fn = options[:
|
118
|
-
|
119
|
-
|
120
|
-
|
210
|
+
if fn = options[:write_code] then
|
211
|
+
dump_node(tnode, fn) {
|
212
|
+
tnode.code_store_hook
|
213
|
+
}
|
121
214
|
end
|
122
215
|
|
123
216
|
if options[:disasm] then
|
@@ -129,10 +222,14 @@ module YTL
|
|
129
222
|
|
130
223
|
tcs = tnode.code_space
|
131
224
|
STDOUT.flush
|
132
|
-
|
225
|
+
if !options[:compile_only] then
|
226
|
+
tcs.call(tcs.base_address)
|
227
|
+
end
|
133
228
|
end
|
134
229
|
end
|
135
230
|
|
231
|
+
Version = "#{YTL::VERSION} (ytljit #{YTLJit::VERSION})"
|
232
|
+
|
136
233
|
if __FILE__ == $0 then
|
137
234
|
YTL::main(YTL::parse_opt(ARGV))
|
138
235
|
end
|
data/runtime/prelude.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
# runtime library written in ytl
|
2
2
|
<<-'EOS'
|
3
3
|
#
|
4
|
-
|
5
|
-
x
|
6
|
-
|
4
|
+
class Module
|
5
|
+
def attr(*x)
|
6
|
+
x.each do |ele|
|
7
|
+
eval "def #{ele}; @#{ele} ; end"
|
8
|
+
end
|
7
9
|
end
|
8
|
-
end
|
9
10
|
|
10
|
-
def attr_accessor(*x)
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def attr_accessor(*x)
|
12
|
+
x.each do |ele|
|
13
|
+
eval "def #{ele}; @#{ele} ; end\n"
|
14
|
+
eval "def #{ele}=(val); @#{ele} = val ; end"
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
@@ -25,6 +27,10 @@ class Array
|
|
25
27
|
|
26
28
|
self
|
27
29
|
end
|
30
|
+
|
31
|
+
def at(idx)
|
32
|
+
self[idx]
|
33
|
+
end
|
28
34
|
end
|
29
35
|
|
30
36
|
class Range
|
@@ -41,9 +47,9 @@ class Range
|
|
41
47
|
yield i
|
42
48
|
i = i + 1
|
43
49
|
end
|
44
|
-
|
50
|
+
end
|
45
51
|
|
46
|
-
|
52
|
+
e
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
@@ -60,9 +66,16 @@ class Fixnum
|
|
60
66
|
|
61
67
|
def step(max, st)
|
62
68
|
i = self
|
63
|
-
|
64
|
-
|
65
|
-
|
69
|
+
if st > 0 then
|
70
|
+
while i <= max
|
71
|
+
yield i
|
72
|
+
i = i + st
|
73
|
+
end
|
74
|
+
else
|
75
|
+
while i >= max
|
76
|
+
yield i
|
77
|
+
i = i + st
|
78
|
+
end
|
66
79
|
end
|
67
80
|
|
68
81
|
self
|
data/runtime/test.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
|
data/test/basictest.rb
CHANGED
@@ -61,11 +61,13 @@ end
|
|
61
61
|
|
62
62
|
p id(blk3 { "abc"})
|
63
63
|
p id(blk3 { 1 })
|
64
|
+
p blk3 { 1 }
|
64
65
|
|
65
66
|
def mul(x, y)
|
66
67
|
x * y
|
67
68
|
end
|
68
69
|
|
70
|
+
p "mul"
|
69
71
|
p mul(30, 40)
|
70
72
|
p mul(30, -40)
|
71
73
|
p mul(-30, 40)
|
@@ -79,6 +81,7 @@ def div(x, y)
|
|
79
81
|
x / y
|
80
82
|
end
|
81
83
|
|
84
|
+
p "div"
|
82
85
|
p div(30, 4)
|
83
86
|
p div(30, -4)
|
84
87
|
p div(-30, 4)
|
@@ -92,6 +95,25 @@ p div(35, -7)
|
|
92
95
|
p div(-35, 7)
|
93
96
|
p div(-35, -7)
|
94
97
|
|
98
|
+
def rem(x, y)
|
99
|
+
x % y
|
100
|
+
end
|
101
|
+
p "rem"
|
102
|
+
p rem(30, 4)
|
103
|
+
p rem(30, -4)
|
104
|
+
p rem(-30, 4)
|
105
|
+
p rem(-30, -4)
|
106
|
+
p rem(35, 7)
|
107
|
+
p rem(35, -7)
|
108
|
+
p rem(-35, 7)
|
109
|
+
p rem(-35, -7)
|
110
|
+
|
111
|
+
p "shift"
|
112
|
+
p 1 << 2
|
113
|
+
p 1 << 0
|
114
|
+
p 3 >> 1
|
115
|
+
p 1024 >> 3
|
116
|
+
|
95
117
|
p 1 < 1
|
96
118
|
p 1 > 1
|
97
119
|
p 1 <= 1
|
@@ -133,6 +155,7 @@ def test_while
|
|
133
155
|
i = 10
|
134
156
|
j = 0
|
135
157
|
k = 10
|
158
|
+
|
136
159
|
while i > 0
|
137
160
|
while k > 0
|
138
161
|
j = j + i
|
@@ -161,6 +184,21 @@ p a.last
|
|
161
184
|
p a
|
162
185
|
p Range.new(i, 2, false)
|
163
186
|
p 1...3
|
187
|
+
|
188
|
+
def test_poly(a, b, c)
|
189
|
+
if c == 1 then
|
190
|
+
a + b
|
191
|
+
else
|
192
|
+
a * b
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
p test_poly(2, 2, 1)
|
197
|
+
p test_poly(2.0, 2.0, 1)
|
198
|
+
p test_poly(2, 2, 0)
|
199
|
+
p test_poly(2.0, 2.0, 0)
|
200
|
+
|
201
|
+
|
164
202
|
=begin
|
165
203
|
def id(a, *b)
|
166
204
|
p a
|
@@ -177,3 +215,4 @@ for i in 1...2
|
|
177
215
|
end
|
178
216
|
|
179
217
|
=end
|
218
|
+
|
data/test/breaktest.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
def test0
|
2
|
+
begin
|
3
|
+
return
|
4
|
+
ensure
|
5
|
+
p "BAR0"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def test1
|
10
|
+
[1, 2, 3].each do |n|
|
11
|
+
p n
|
12
|
+
next
|
13
|
+
end
|
14
|
+
p "foo1"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test2
|
18
|
+
[1, 2, 3].each do |n|
|
19
|
+
p n
|
20
|
+
return
|
21
|
+
end
|
22
|
+
p "bar2"
|
23
|
+
end
|
24
|
+
|
25
|
+
def test3
|
26
|
+
begin
|
27
|
+
[1, 2, 3].each do |n|
|
28
|
+
p n
|
29
|
+
return
|
30
|
+
end
|
31
|
+
p "foo3"
|
32
|
+
ensure
|
33
|
+
p "bar3"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test4
|
38
|
+
begin
|
39
|
+
[1, 2, 3].each do |n|
|
40
|
+
p n
|
41
|
+
break
|
42
|
+
end
|
43
|
+
p "foo4"
|
44
|
+
ensure
|
45
|
+
p "bar4"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
test0
|
51
|
+
|
52
|
+
test1
|
53
|
+
test2
|
54
|
+
|
55
|
+
test3
|
56
|
+
test4
|
57
|
+
|
58
|
+
=begin
|
59
|
+
# Not support yet
|
60
|
+
|
61
|
+
def test5
|
62
|
+
begin
|
63
|
+
[1, 2, 3].each do |n|
|
64
|
+
p n
|
65
|
+
next
|
66
|
+
end
|
67
|
+
p "foo5"
|
68
|
+
ensure
|
69
|
+
p "bar5"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test6
|
74
|
+
begin
|
75
|
+
[1, 2, 3].each do |n|
|
76
|
+
p n
|
77
|
+
redo
|
78
|
+
end
|
79
|
+
p "foo"
|
80
|
+
ensure
|
81
|
+
p "bar"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
=end
|
data/test/classtest.rb
CHANGED
@@ -42,6 +42,32 @@ end
|
|
42
42
|
p Baz.new
|
43
43
|
p Baz.alloc
|
44
44
|
|
45
|
+
module Foo0
|
46
|
+
def mess
|
47
|
+
p "from included module Foo0"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module Foo1
|
52
|
+
def mess
|
53
|
+
p "from included module Foo1"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Foo
|
58
|
+
include Foo0
|
59
|
+
include Foo1
|
60
|
+
end
|
61
|
+
|
62
|
+
Foo.new.mess
|
63
|
+
|
64
|
+
class Foo
|
65
|
+
extend Foo0
|
66
|
+
extend Foo1
|
67
|
+
end
|
68
|
+
|
69
|
+
Foo.mess
|
70
|
+
|
45
71
|
#=end
|
46
72
|
=begin
|
47
73
|
class Asd
|
data/test/condtest.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
def test(bool1, bool2)
|
2
|
+
if bool1 then
|
3
|
+
p "bool1"
|
4
|
+
elsif bool2
|
5
|
+
p "bool2"
|
6
|
+
else
|
7
|
+
p "nothing"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
a = true
|
12
|
+
b = false
|
13
|
+
p a ? 1 : 0
|
14
|
+
p b ? 1 : 0
|
15
|
+
|
16
|
+
p 1 + (a ? 1 : 0)
|
17
|
+
p 1 + (b ? 0b1 : 0b0)
|
18
|
+
|
19
|
+
test(true, true)
|
20
|
+
test(false, true)
|
21
|
+
test(false, false)
|
22
|
+
|
data/test/exceptiontest.rb
CHANGED
data/test/execytl.rb
ADDED
data/test/macrotest.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
class Module
|
2
|
+
def myattr(*x)
|
3
|
+
str = ""
|
4
|
+
x.each do |e|
|
5
|
+
eval "def #{e}; @#{e}; end\n"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Foo
|
11
|
+
def myattr(x)
|
12
|
+
p x
|
5
13
|
end
|
6
14
|
end
|
7
15
|
|
8
16
|
myattr :foo, :bar
|
17
|
+
Foo.new.myattr("abc")
|
9
18
|
|
10
19
|
# Can't psss this test yet
|
11
20
|
=begin
|
data/test/tmp.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Hideki Miura
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-05-06 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -28,8 +28,8 @@ dependencies:
|
|
28
28
|
segments:
|
29
29
|
- 0
|
30
30
|
- 0
|
31
|
-
-
|
32
|
-
version: 0.0.
|
31
|
+
- 7
|
32
|
+
version: 0.0.7
|
33
33
|
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
description:
|
@@ -46,10 +46,14 @@ files:
|
|
46
46
|
- lib/ytl/importobj.rb
|
47
47
|
- lib/ytl/macro.rb
|
48
48
|
- runtime/prelude.rb
|
49
|
+
- runtime/test.rb
|
49
50
|
- runtime/type.rb
|
50
51
|
- test/basictest.rb
|
52
|
+
- test/breaktest.rb
|
51
53
|
- test/classtest.rb
|
54
|
+
- test/condtest.rb
|
52
55
|
- test/exceptiontest.rb
|
56
|
+
- test/execytl.rb
|
53
57
|
- test/exttest.rb
|
54
58
|
- test/floattest.rb
|
55
59
|
- test/ivtest.rb
|
@@ -92,8 +96,11 @@ specification_version: 3
|
|
92
96
|
summary: Very tiny subset of YARV to native code translator
|
93
97
|
test_files:
|
94
98
|
- test/basictest.rb
|
99
|
+
- test/breaktest.rb
|
95
100
|
- test/classtest.rb
|
101
|
+
- test/condtest.rb
|
96
102
|
- test/exceptiontest.rb
|
103
|
+
- test/execytl.rb
|
97
104
|
- test/exttest.rb
|
98
105
|
- test/floattest.rb
|
99
106
|
- test/ivtest.rb
|