-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.rb
344 lines (298 loc) · 8.56 KB
/
vm.rb
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
require_relative './executor'
require_relative './vm/frames'
require_relative './vm/iseq'
require_relative './vm/errors'
class VM
attr_reader :frame_stack
attr_accessor :debug
attr_accessor :debug_focus_on
attr_accessor :debug_print_stack
def initialize
@frame_stack = FrameStack.new
@executor = Executor.new
@jump = nil
@previous_frame = nil
end
def self.instance
@_instance ||= new
end
def execute(iseq, **payload)
iseq = ISeq.new(iseq)
begin
push_frame(iseq, **payload)
rescue Exception => e
puts e
raise InternalError, "Errors inside push_frame are not allowed"
end
pushed_frame = current_frame
if (before_eval = payload[:before_eval]); before_eval.call; end
__log { "\n\n--------- BEGIN #{current_frame.header} ---------" }
begin
current_frame.prepare
evaluate_last_frame
rescue LongJumpError => e
assert_frame(pushed_frame)
e.do_jump!
rescue Exception => e
assert_frame(pushed_frame)
pop_frame(reason: "propagating #{e}")
raise
end
__log { "\n\n--------- END #{current_frame.header} ---------" }
assert_frame(pushed_frame)
pop_frame(reason: "fully evaluated, returning")
end
def assert_frame(expected_frame)
if !expected_frame.equal?(current_frame)
raise InternalError, <<-HERE
must pop what was pushed
Expected: #{expected_frame.header}
Got: #{current_frame.header}
HERE
end
end
def push_frame(iseq, **payload)
case iseq.kind
when :top
@frame_stack.push_top(
iseq: iseq
)
when :eval
@frame_stack.push_eval(
iseq: iseq,
parent_frame: current_frame,
_self: payload[:_self]
)
when :class
case
when iseq.name.start_with?('<module')
@frame_stack.push_module(
iseq: iseq,
parent_frame: current_frame,
name: payload[:name],
scope: payload[:scope]
)
when iseq.name.start_with?('<class')
@frame_stack.push_class(
iseq: iseq,
parent_frame: current_frame,
name: payload[:name],
superclass: payload[:superclass],
scope: payload[:scope]
)
when iseq.name == 'singleton class'
@frame_stack.push_sclass(
iseq: iseq,
parent_frame: current_frame,
of: payload[:of]
)
else
raise NotImplementedError, "Unknown iseq name #{iseq.name}"
end
when :method
@frame_stack.push_method(
iseq: iseq,
parent_nesting: payload[:parent_nesting],
_self: payload[:_self],
arg_values: payload[:method_args],
block: payload[:block]
)
when :block
@frame_stack.push_block(
iseq: iseq,
parent_frame: payload[:parent_frame],
arg_values: payload[:block_args],
block: payload[:block]
)
when :rescue
@frame_stack.push_rescue(
iseq: iseq,
parent_frame: current_frame,
caught: payload[:caught],
exit_to: payload[:exit_to]
)
when :ensure
@frame_stack.push_ensure(
iseq: iseq,
parent_frame: current_frame
)
else
raise NotImplementedError, "Unknown iseq kind #{iseq.kind}"
end
__log { "Pushing frame #{current_frame.header}" }
end
def pop_frame(reason: 'unknown')
if @frame_stack.size == 0
raise InternalError, 'no frame to pop'
end
frame = @frame_stack.pop
__log { "Destroying frame #{frame.header} [#{reason}]" }
if frame.is_a?(RescueFrame)
__log { "Jumping into post-rescue" }
jump(frame.exit_to)
end
frame.returning
end
def evaluate_last_frame
initial_frame_stack_size = frame_stack.size
loop do
raise InternalError, 'malformed frame stack' if frame_stack.size < initial_frame_stack_size
if current_insns.empty?
if frame_stack.size == initial_frame_stack_size
# done with the root iseq
break
else
# done with intermediate iseq
raise InternalError, "unexpected"
pop_frame(reason: 'dead')
next
end
end
unless @previous_frame.equal?(current_frame)
__log { " ====== switching to #{current_frame.pretty_name} =========" }
end
@previous_frame = current_frame
current_insn = current_iseq.shift_insn
execute_insn(current_insn)
end
end
def focused?
current_frame.pretty_name.include?(debug_focus_on)
end
def __log(&blk)
return unless debug
return if debug_focus_on && !focused?
if debug_print_stack && current_frame
$debug.puts "Stack: #{current_frame.stack.inspect}"
end
$debug.print "-->" * @frame_stack.size
$debug.print " "
$debug.puts blk.call
end
def pretty_insn(insn)
return insn unless insn.is_a?(Array)
name, *payload = insn
case name
when :defineclass
[name, payload[0], '...class body omitted']
when :putiseq
[name, payload[0][5], '...method body omitted']
when :send
[name, *payload[0..-2], '...block omitted']
else
insn
end
end
def clear_current_iseq
current_iseq.insns.each { |insn| skip_insn(insn) }
current_iseq.insns.clear
end
def execute_insn(insn)
case insn
when Integer
__log { insn }
@last_numeric_insn = insn
when :RUBY_EVENT_LINE
__log { insn }
current_frame.line = @last_numeric_insn
@last_numeric_insn = nil
when [:leave]
if current_stack.empty?
__log { "#{insn.inspect}" }
raise InternalError, <<~MSG
Stack is empty, cannot to [:leave].
current_frame is #{current_frame.header}
current_frame.returning is #{current_frame.returning}
MSG
end
current_frame.returning = returning = current_stack.pop
__log { "#{insn.inspect} (returning #{returning.inspect})" }
clear_current_iseq
when Array
execute_array_insn(insn)
when :RUBY_EVENT_END, :RUBY_EVENT_CLASS, :RUBY_EVENT_RETURN, :RUBY_EVENT_B_CALL, :RUBY_EVENT_B_RETURN, :RUBY_EVENT_CALL
__log { insn }
# ignore
when /label_\d+/
__log { insn }
on_label(insn)
# ignore
else
raise NotImplementedError, "Unknown insn kind #{insn.inspect}"
end
end
def execute_array_insn(insn)
name, *payload = insn
__log { pretty_insn(insn).inspect }
with_error_handling do
@executor.send(:"execute_#{name}", payload)
end
end
def with_error_handling
yield
rescue InternalError => e
raise
rescue Exception => e
handle_error(e)
end
def handle_error(error)
if current_frame.enabled_ensure_handlers.length > 1
raise InternalError, "current_frame.enabled_ensure_handlers > 1"
end
if (rescue_handler = current_frame.enabled_rescue_handlers[0])
result = execute(rescue_handler.iseq, caught: error, exit_to: rescue_handler.exit_label)
current_stack.push(result)
else
raise
end
end
def skip_insn(insn)
__log { "... #{pretty_insn(insn).inspect}" }
if insn.is_a?(Symbol) && insn =~ /label_\d+/
on_label(insn)
end
end
def current_frame; frame_stack.top; end
def current_self; current_frame._self; end
def current_iseq; current_frame.iseq; end
def current_insns; current_iseq.insns; end
def current_stack; current_frame.stack; end
def current_nesting; current_frame.nesting; end
def backtrace; @frame_stack.to_backtrace; end
def _raise(klass, msg)
e = klass.new(msg)
e.set_backtrace(backtrace)
raise e
end
def jump(label)
insns = current_iseq.insns
loop do
if insns.empty?
# it can be a jump back via `while` loop
if current_iseq.initially_had_insn?(label)
current_iseq.reset!
current_iseq.insns.drop_while { |insn| insn != label }
insns = current_iseq.insns
else
raise InternalError, 'empty insns list, cannot jump'
end
end
break if insns[0] == label
skip_insn(insns.shift)
end
skip_insn(insns.shift) # shift the label itself
end
def on_label(label)
{
current_iseq.rescue_handlers => current_frame.enabled_rescue_handlers,
current_iseq.ensure_handlers => current_frame.enabled_ensure_handlers,
}.each do |all_handlers, enabled_handlers|
all_handlers
.select { |handler| handler.begin_label == label }
.each { |handler| enabled_handlers << handler }
all_handlers
.select { |handler| handler.end_label == label }
.each { |handler| enabled_handlers.delete(handler) }
end
end
end