forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.rb
868 lines (702 loc) · 20 KB
/
generator.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# -*- encoding: us-ascii -*-
module Rubinius
class Generator
include GeneratorMethods
##
# Jump label for the branch instructions. The use scenarios for labels:
# 1. Used and then set
# g.gif label
# ...
# label.set!
# 2. Set and then used
# label.set!
# ...
# g.git label
# 3. 1, 2
#
# Many labels are only used once. This class employs two small
# optimizations. First, for the case where a label is used once then set,
# the label merely records the point it was used and updates that location
# to the concrete IP when the label is set. In the case where the label is
# used multiple times, it records each location and updates them to an IP
# when the label is set. In both cases, once the label is set, each use
# after that updates the instruction stream with a concrete IP at the
# point the label is used. This avoids the need to ever record all the
# labels or search through the stream later to change symbolic labels into
# concrete IP's.
class Label
attr_reader :used, :basic_block
alias_method :used?, :used
def initialize(generator)
@generator = generator
@basic_block = generator.new_basic_block
@position = nil
@used = false
@location = nil
@locations = nil
end
def set!
@position = @generator.ip
@position.basic_blocks << @basic_block
if @locations
@locations.each { |x| set_jump_slot(x) }
elsif @location
set_jump_slot(@location)
end
end
def used_at(ip)
if @position
set_jump_slot(ip)
elsif !@location
@location = ip
elsif @locations
@locations << ip
else
@locations = [@location, ip]
end
@used = true
end
private
def set_jump_slot(ip)
@position.jump_from(ip)
ip[:slot] = @position
end
end
class BasicBlock
attr_accessor :left
attr_accessor :right
def initialize(generator)
@generator = generator
@enter_size = nil
@max_size = 0
@min_size = 0
@exit_ip = 0
@exit_size = nil
@stack = 0
@left = nil
@right = nil
@visited = false
@closed = false
end
def add_stack(read, write)
read_change = @stack - read
@min_size = read_change if read_change < @min_size
@stack += (write - read)
@max_size = @stack if @stack > @max_size
end
def open
end
def close
@closed = true
end
def close_and_exit(exit_instruction)
close
@exit_size = @stack
@exit_ip = exit_instruction[:ip]
end
def location(ip=nil)
line = nil #@generator.ip_to_line(ip)
"#name: line: #{line}, IP: #{ip}"
end
SEPARATOR_SIZE = 40
def invalid(message)
if $DEBUG
puts message
name = @generator.name.inspect
size = (SEPARATOR_SIZE - name.size - 2) / 2
size = 1 if size <= 0
puts "\n#{"=" * size} #{name} #{"=" * (size + name.size % 2)}"
literals = @generator.literals
names = @generator.local_names
stream = @generator.stream
i = 0
n = stream.size
stack = 0
while i < n
insn = InstructionSet[stream[i]]
printf "[%3d] %04d %-28s" % [stack, i, insn.opcode.inspect]
args = stream[i+1, insn.size-1]
if insn.size > 1
insn.args.each_with_index do |kind, index|
arg = args[index]
case kind
when :literal
printf "%s " % literals[arg].inspect
when :local
printf "%s " % (names ? names[arg] : arg)
else
printf "%d " % arg
end
end
end
puts
if insn.variable_stack?
use = insn.stack_consumed
if use.kind_of? Array
use = args[use[1] - 1] + use[0]
end
pro = insn.stack_produced
if pro.kind_of? Array
pro = (args[pro[1] - 1] * pro[2]) + pro[0]
end
stack += pro - use
else
stack += insn.stack_difference
end
i += insn.size
end
puts "-" * SEPARATOR_SIZE
end
raise CompileError, message
end
def visited?
@visited
end
def validate_stack
@enter_size = 0
stack = [self]
until stack.empty?
bb = stack.shift
bb.flow_stack_size stack
end
end
def flow_stack_size(stack)
unless @visited
@visited = true
@generator.accumulate_stack(@enter_size + @max_size)
net_size = @enter_size + @stack
if net_size < 0
invalid "net stack underflow in block starting at #{location}"
end
if @enter_size + @min_size < 0
invalid "minimum stack underflow in block starting at #{location}"
end
if @exit_size and @enter_size + @exit_size < 1
invalid "exit stack underflow in block starting at #{location(@exit_ip)}"
end
if @left
@left.check_stack net_size
stack.push @left unless @left.visited?
end
if @right
@right.check_stack net_size
stack.push @right unless @right.visited?
end
end
end
def check_stack(stack_size)
if @enter_size
unless stack_size == @enter_size
invalid "unbalanced stack at #{location}: #{stack_size} != #{@enter_size}"
end
else
if not @closed
invalid "control fails to exit properly at #{location}"
end
@enter_size = stack_size
end
end
end
def initialize
@instruction_list = InstructionList.new
@literals_map = Hash.new { |h,k| h[k] = add_literal(k) }
@literals = []
@primitive = nil
@for_block = nil
@required_args = 0
@post_args = 0
@total_args = 0
@detected_args = 0
@detected_locals = 0
@state = []
@generators = []
@current_line = nil
@definition_line = nil
end
attr_accessor :file, :name,
:required_args, :post_args, :total_args, :splat_index,
:local_count, :local_names, :primitive, :for_block,
:detected_args, :detected_locals
def ip
@instruction_list.ip
end
def execute(node)
node.bytecode self
end
alias_method :run, :execute
# Formalizers
def encode
@instruction_list.source
@instruction_list.materialize
@instruction_list.validate_stack
@iseq = @instruction_list.iseq
if @definition_line
@lines = [-1, @definition_line] + @instruction_list.lines
else
@lines = @instruction_list.lines
end
@generators.each { |x| @literals[x].encode }
end
def package(klass)
@generators.each { |x| @literals[x] = @literals[x].package klass }
cm = klass.new
cm.iseq = @iseq
cm.literals = @literals.to_tuple
cm.lines = @lines.to_tuple
cm.required_args = required_args
cm.post_args = post_args
cm.total_args = total_args
cm.splat = splat_index
cm.local_count = local_count
cm.local_names = local_names.to_tuple if local_names
cm.stack_size = max_stack_size
cm.file = file
cm.name = name
cm.primitive = @primitive
if @for_block
cm.add_metadata :for_block, true
end
cm.verify_bytecode if cm.respond_to?(:verify_bytecode)
cm
end
def use_detected
if @required_args < @detected_args
@required_args = @detected_args
end
if @total_args < @detected_args
@total_args = @detected_args
end
if @local_count < @detected_locals
@local_count = @detected_locals
end
end
# Commands (these don't generate data in the stream)
def state
@state.last
end
def push_state(scope)
@state << AST::State.new(scope)
end
def pop_state
@state.pop
end
def push_modifiers
@instruction_list.push_modifiers
end
def pop_modifiers
@instruction_list.pop_modifiers
end
def definition_line(line)
unless @instruction_list.empty?
raise Exception, "only use #definition_line first"
end
@definition_line = line
end
def set_line(line)
raise Exception, "source code line cannot be nil" unless line
@current_line = line
end
def line
@last_line
end
def ip_to_line(ip)
total = @lines.size - 2
i = 0
while i < total
if ip >= @lines[i] and ip <= @lines[i+2]
return @lines[i+1]
end
i += 2
end
end
def close
if @current_line.nil?
msg = "closing a method definition with no line info: #{file}:#{line}"
raise Exception, msg
end
end
def send_primitive(name)
@primitive = name
end
def new_label
@instruction_list.new_label
end
# Aliases
alias_method :dup, :dup_top
alias_method :git, :goto_if_true
alias_method :gif, :goto_if_false
alias_method :swap, :swap_stack
# Helpers
def create_instruction(name)
@instruction = @instruction_list.create_instruction(name, @current_line)
end
def max_stack_size
size = @instruction_list.max_stack_size + local_count
size += 1 if @for_block
size
end
def new_stack_local
@instruction_list.new_stack_local
end
def push(what)
case what
when :true
push_true
when :false
push_false
when :self
push_self
when :nil
push_nil
when Integer
push_int what
else
raise CompileError, "Unknown push argument '#{what.inspect}'"
end
end
def push_generator(generator)
index = push_literal generator
@generators << index
index
end
# Find the index for the specified literal, or create a new slot if the
# literal has not been encountered previously.
def find_literal(literal)
@literals_map[literal]
end
# Add literal exists to allow RegexLiteral's to create a new regex literal
# object at run-time. All other literals should be added via find_literal,
# which re-use an existing matching literal if one exists.
def add_literal(literal)
index = @literals.size
@literals << literal
return index
end
# Pushes the specified literal value into the literal's tuple
def push_literal(literal)
index = find_literal literal
emit_push_literal index
return index
end
# Puts +what+ is the literals tuple without trying to see if
# something that is like +what+ is already there.
def push_unique_literal(literal)
index = add_literal literal
emit_push_literal index
return index
end
# Pushes the literal value on the stack into the specified position in the
# literals tuple. Most timees, push_literal should be used instead; this
# method exists to support RegexLiteral, where the compiled literal value
# (a Regex object) does not exist until runtime.
def push_literal_at(index)
emit_push_literal index
return index
end
# The push_const instruction itself is unused right now. The instruction
# parser does not emit a GeneratorMethods#push_const. This method/opcode
# was used in the compiler before the push_const_fast instruction. Rather
# than changing the compiler code, this helper was used.
def push_const(name)
push_const_fast find_literal(name), add_literal(nil)
end
def push_local(idx)
if @detected_locals <= idx
@detected_locals = idx + 1
end
super
end
def set_local(idx)
if @detected_locals <= idx
@detected_locals = idx + 1
end
super
end
# Minor meta operations that can be used to detect
# the number of method arguments needed
def push_arg(idx)
push_local(idx)
@detected_args = @detected_locals
end
def set_arg(idx)
set_local(idx)
@detected_args = @detected_locals
end
def last_match(mode, which)
push_int Integer(mode)
push_int Integer(which)
invoke_primitive :regexp_last_match_result, 2
end
def send(meth, count, priv=false)
allow_private if priv
unless count.kind_of? Fixnum
raise CompileError, "count must be a number"
end
idx = find_literal(meth)
# Don't use send_method, it's only for when the syntax
# specified no arguments and no parens.
send_stack idx, count
end
# Do a private send to self with no arguments specified, ie, a vcall
# style send.
def send_vcall(meth)
idx = find_literal(meth)
send_method idx
end
def send_with_block(meth, count, priv=false)
allow_private if priv
unless count.kind_of? Fixnum
raise CompileError, "count must be a number"
end
idx = find_literal(meth)
send_stack_with_block idx, count
end
def send_with_splat(meth, args, priv=false, concat=false)
val = 0
val |= InstructionSet::CALL_FLAG_CONCAT if concat
set_call_flags val unless val == 0
allow_private if priv
idx = find_literal(meth)
send_stack_with_splat idx, args
end
def send_super(meth, args, splat=false)
idx = find_literal(meth)
if splat
send_super_stack_with_splat idx, args
else
send_super_stack_with_block idx, args
end
end
def meta_to_s(name=:to_s, priv=true)
allow_private if priv
super find_literal(name)
end
def break
@instruction_list.break
end
def break=(label)
@instruction_list.break = label
end
def redo
@instruction_list.redo
end
def redo=(label)
@instruction_list.redo = label
end
def next
@instruction_list.next
end
def next=(label)
@instruction_list.next = label
end
def retry
@instruction_list.retry
end
def retry=(label)
@instruction_list.retry = label
end
end
class Slot
attr_accessor :instruction, :basic_blocks, :jumps, :line
def initialize
@instruction = nil
@basic_blocks = []
@jumps = []
end
def jump_from(instruction)
@jumps << instruction
end
def materialize
@instruction[:ip]
end
end
class InstructionList
def initialize
@instruction_slots = [Slot.new]
initialize_modifiers
initialize_stack
end
attr_accessor :break, :redo, :next, :retry
def initialize_modifiers
@modstack = []
@break = nil
@redo = nil
@next = nil
@retry = nil
end
def push_modifiers
@modstack << [@break, @redo, @next, @retry]
end
def pop_modifiers
@break, @redo, @next, @retry = @modstack.pop
end
def used_slots
@instruction_slots[0..-2]
end
def instructions
used_slots.collect(&:instruction)
end
def empty?
used_slots.empty?
end
def initialize_stack
@max_stack = 0
@stack_locals = 0
end
def new_stack_local
idx = @stack_locals
@stack_locals += 1
idx
end
def accumulate_stack(size)
@max_stack = size if size > @max_stack
end
def max_stack_size
@max_stack + @stack_locals
end
def last_slot
@instruction_slots.last
end
def create_instruction(name, current_line)
instruction = {:name => name}
last_slot.instruction = instruction
last_slot.line = current_line
@instruction_slots << Slot.new
instruction
end
def ip
last_slot
end
def source
last_line = nil
@instruction_slots.each do |slot|
if slot.line
if last_line != slot.line
slot.instruction[:new_line] = true
end
last_line = slot.line
end
end
slots = []
@instruction_slots.each do |slot|
if slot.instruction and slot.instruction[:new_line] and slot.line > 0
new_slot = Slot.new
new_slot.instruction = slot.instruction
new_slot.line = slot.line
instruction = {:stream => [1, slot.line - 1], :name => :source, :stack => [0, 0]}
slot.instruction = instruction
slots << slot
slots << new_slot
else
slots << slot
end
end
@instruction_slots = slots
end
def materialize
@instruction_slots.freeze
calculate_ip
@instruction_slots.each(&:freeze)
materialize_position
end
def calculate_ip
ip = 0
instructions.each do |instruction|
instruction[:ip] = ip
ip += instruction[:stream].size
end
last_slot.instruction = {:ip => ip}
end
def replace_with_ip(instruction)
basic_block = instruction[:stream][1]
instruction[:stream][1] = instruction[:slot].materialize
basic_block
end
def materialize_position
@enter_block = current = new_basic_block
used_slots.each do |slot|
instruction = slot.instruction
slot.basic_blocks.each do |basic_block|
current.left = basic_block
current.close
current = basic_block
end
if instruction[:name] != :cast_array
current.add_stack(*instruction[:stack])
end
case instruction[:name]
when :goto
basic_block = replace_with_ip(instruction)
current.left = basic_block
current.close
current = new_basic_block
when :goto_if_false, :goto_if_true, :setup_unwind
basic_block = replace_with_ip(instruction)
current.left = basic_block
current.close
current = current.right = new_basic_block
when :ret, :raise_return, :ensure_return
current.close_and_exit instruction
current = new_basic_block
when :raise_exc, :reraise, :break
current.close
current = new_basic_block
end
end
end
def validate_stack
#pp(@instruction_slots.collect(&:instruction).compact.collect do |instruction| {:ip => instruction[:ip], :name => instruction[:name], :stream => instruction[:stream]} end) if ENV["DEBUG"]
begin
# Validate the stack and calculate the max depth
@enter_block.validate_stack
rescue Exception => e
if $DEBUG
puts "Error computing stack for #{@name}: #{e.message} (#{e.class})"
end
raise e
end
end
def iseq
InstructionSequence.new(instruction_stream.to_tuple)
end
def lines
lines = []
last_line = nil
used_slots.each.with_index do |slot, index|
if lines.empty?
if slot.line
lines << 0
lines << slot.line
last_line = slot.line
end
else
if slot.line != last_line
if lines[-2] == @instruction_slots[index].instruction[:ip]
lines[-1] = slot.line
else
lines << @instruction_slots[index].instruction[:ip]
lines << slot.line
end
last_line = slot.line
end
end
end
lines << last_slot.instruction[:ip]
lines
end
def new_basic_block
Generator::BasicBlock.new(self)
end
def new_label
Generator::Label.new(self)
end
private
def instruction_stream
stream = []
instructions.each do |instruction|
stream += instruction[:stream]
end
stream
end
end
end