Class: CodeTools::Generator

Inherits:
Object
  • Object
show all
Includes:
GeneratorMethods
Defined in:
lib/rubinius/code/compiler/generator.rb

Defined Under Namespace

Classes: BasicBlock, Label

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GeneratorMethods

#add_scope, #allow_private, #check_interrupts, #check_serial, #check_serial_private, #clear_exception, #dup, #dup_many, #emit_push_literal, #goto, #goto_if_equal, #goto_if_false, #goto_if_nil, #goto_if_not_equal, #goto_if_not_nil, #goto_if_not_undefined, #goto_if_true, #goto_if_undefined, #instance_of, #kind_of, #m_bytes, #m_counter, #m_sum, #m_time_stamp, #m_timer_start, #m_timer_stop, #m_value, #make_array, #move_down, #noop, #p_any, #p_call, #p_char, #p_char_set, #p_choice, #p_commit, #p_commit_back, #p_commit_partial, #p_end, #p_fail, #p_fail_twice, #p_init, #p_jump, #p_return, #p_span, #p_test_any, #p_test_char, #p_test_char_set, #pop, #pop_many, #pop_unwind, #push_block, #push_cpath_top, #push_current_exception, #push_exception_state, #push_false, #push_file, #push_has_block, #push_int, #push_memo, #push_mirror, #push_my_field, #push_my_offset, #push_nil, #push_rubinius, #push_scope, #push_self, #push_stack_local, #push_true, #push_type, #push_undef, #push_variables, #restore_exception_state, #rotate, #set_call_flags, #set_const, #set_const_at, #set_stack_local, #setup_unwind, #shift_array, #store_my_field, #string_append, #swap, #yield_debugger

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



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
# File 'lib/rubinius/code/compiler/generator.rb', line 245

def initialize
  @stream = []
  @literals_map = Hash.new { |h,k| h[k] = add_literal(k) }
  @literals = []
  @ip = 0
  @modstack = []
  @break = nil
  @redo = nil
  @next = nil
  @retry = nil
  @last_line = nil
  @file = nil
  @lines = []
  @primitive = nil
  @instruction = nil
  @for_block = nil
  @for_module_body = nil

  @required_args = 0
  @post_args = 0
  @total_args = 0
  @arity = 0

  @detected_args = 0
  @detected_locals = 0

  @splat_index = nil
  @local_names = nil
  @block_index = nil
  @keywords = nil
  @kwrest_index = nil
  @local_count = 0

  @state = []
  @generators = []

  @stack_locals = 0

  @enter_block = new_basic_block
  @current_block = @enter_block
  @max_stack = 0
end

Instance Attribute Details

#arityObject

Returns the value of attribute arity.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def arity
  @arity
end

#block_indexObject

Returns the value of attribute block_index.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def block_index
  @block_index
end

#breakObject

Returns the value of attribute break.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def break
  @break
end

#current_blockObject

Returns the value of attribute current_block.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def current_block
  @current_block
end

#detected_argsObject

Returns the value of attribute detected_args.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def detected_args
  @detected_args
end

#detected_localsObject

Returns the value of attribute detected_locals.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def detected_locals
  @detected_locals
end

#fileObject

Returns the value of attribute file.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def file
  @file
end

#for_blockObject

Returns the value of attribute for_block.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def for_block
  @for_block
end

#for_module_bodyObject

Returns the value of attribute for_module_body.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def for_module_body
  @for_module_body
end

#ipObject (readonly)

Returns the value of attribute ip.



288
289
290
# File 'lib/rubinius/code/compiler/generator.rb', line 288

def ip
  @ip
end

#iseqObject (readonly)

Returns the value of attribute iseq.



288
289
290
# File 'lib/rubinius/code/compiler/generator.rb', line 288

def iseq
  @iseq
end

#keywordsObject

Returns the value of attribute keywords.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def keywords
  @keywords
end

#kwrest_indexObject

Returns the value of attribute kwrest_index.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def kwrest_index
  @kwrest_index
end

#literalsObject (readonly)

Returns the value of attribute literals.



288
289
290
# File 'lib/rubinius/code/compiler/generator.rb', line 288

def literals
  @literals
end

#local_countObject

Returns the value of attribute local_count.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def local_count
  @local_count
end

#local_namesObject

Returns the value of attribute local_names.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def local_names
  @local_names
end

#nameObject

Returns the value of attribute name.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def name
  @name
end

#nextObject

Returns the value of attribute next.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def next
  @next
end

#post_argsObject

Returns the value of attribute post_args.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def post_args
  @post_args
end

#primitiveObject

Returns the value of attribute primitive.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def primitive
  @primitive
end

#redoObject

Returns the value of attribute redo.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def redo
  @redo
end

#required_argsObject

Returns the value of attribute required_args.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def required_args
  @required_args
end

#retryObject

Returns the value of attribute retry.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def retry
  @retry
end

#splat_indexObject

Returns the value of attribute splat_index.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def splat_index
  @splat_index
end

#streamObject (readonly)

Returns the value of attribute stream.



288
289
290
# File 'lib/rubinius/code/compiler/generator.rb', line 288

def stream
  @stream
end

#total_argsObject

Returns the value of attribute total_args.



289
290
291
# File 'lib/rubinius/code/compiler/generator.rb', line 289

def total_args
  @total_args
end

Instance Method Details

#accumulate_stack(size) ⇒ Object



461
462
463
# File 'lib/rubinius/code/compiler/generator.rb', line 461

def accumulate_stack(size)
  @max_stack = size if size > @max_stack
end

#add_generator(generator) ⇒ Object



484
485
486
487
488
# File 'lib/rubinius/code/compiler/generator.rb', line 484

def add_generator(generator)
  index = add_literal generator
  @generators << index
  index
end

#add_literal(literal) ⇒ Object



494
495
496
497
498
# File 'lib/rubinius/code/compiler/generator.rb', line 494

def add_literal(literal)
  index = @literals.size
  @literals << literal
  index
end

#cast_arrayObject

instructions that can cause stack unwinding



590
591
592
593
# File 'lib/rubinius/code/compiler/generator.rb', line 590

def cast_array
  super
  unwind
end

#cast_for_multi_block_argObject



595
596
597
598
# File 'lib/rubinius/code/compiler/generator.rb', line 595

def cast_for_multi_block_arg
  super
  unwind
end

#cast_for_single_block_argObject



600
601
602
603
# File 'lib/rubinius/code/compiler/generator.rb', line 600

def cast_for_single_block_arg
  super
  unwind
end

#cast_for_splat_block_argObject



605
606
607
608
# File 'lib/rubinius/code/compiler/generator.rb', line 605

def cast_for_splat_block_arg
  super
  unwind
end

#cast_multi_valueObject



610
611
612
613
# File 'lib/rubinius/code/compiler/generator.rb', line 610

def cast_multi_value
  super
  unwind
end

#check_frozenObject



615
616
617
618
# File 'lib/rubinius/code/compiler/generator.rb', line 615

def check_frozen
  super
  unwind
end

#closeObject



438
439
440
441
442
443
444
445
# File 'lib/rubinius/code/compiler/generator.rb', line 438

def close
  if @lines.empty?
    msg = "closing a method definition with no line info: #{file}:#{line}"
    raise Exception, msg
  end

  @lines << @ip
end

#create_block(literal) ⇒ Object



620
621
622
623
# File 'lib/rubinius/code/compiler/generator.rb', line 620

def create_block(literal)
  super
  unwind
end

#definition_line(line) ⇒ Object



391
392
393
394
395
396
397
398
399
400
# File 'lib/rubinius/code/compiler/generator.rb', line 391

def definition_line(line)
  unless @stream.empty?
    raise Exception, "only use #definition_line first"
  end

  @lines << -1
  @lines << line

  @last_line = line
end

#encodeObject

Formalizers



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rubinius/code/compiler/generator.rb', line 303

def encode
  @iseq = Rubinius::InstructionSequence.new @stream.to_tuple

  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

  @generators.each { |x| @literals[x].encode }
end

#ensure_returnObject



625
626
627
628
# File 'lib/rubinius/code/compiler/generator.rb', line 625

def ensure_return
  super
  unwind
end

#execute(node) ⇒ Object Also known as: run



295
296
297
# File 'lib/rubinius/code/compiler/generator.rb', line 295

def execute(node)
  node.bytecode self
end

#find_const(literal) ⇒ Object



630
631
632
633
# File 'lib/rubinius/code/compiler/generator.rb', line 630

def find_const(literal)
  super
  unwind
end

#find_literal(literal) ⇒ Object



490
491
492
# File 'lib/rubinius/code/compiler/generator.rb', line 490

def find_literal(literal)
  @literals_map[literal]
end

#invoke_primitive(literal, count) ⇒ Object



635
636
637
638
# File 'lib/rubinius/code/compiler/generator.rb', line 635

def invoke_primitive(literal, count)
  super
  unwind
end

#ip_to_line(ip) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/rubinius/code/compiler/generator.rb', line 425

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

#last_match(mode, which) ⇒ Object



536
537
538
539
540
# File 'lib/rubinius/code/compiler/generator.rb', line 536

def last_match(mode, which)
  push_int Integer(mode)
  push_int Integer(which)
  invoke_primitive :regexp_last_match_result, 2
end

#lineObject



421
422
423
# File 'lib/rubinius/code/compiler/generator.rb', line 421

def line
  @last_line
end

#max_stack_sizeObject



465
466
467
468
469
# File 'lib/rubinius/code/compiler/generator.rb', line 465

def max_stack_size
  size = @max_stack + @local_count + @stack_locals
  size += 1 if @for_block
  size
end

#new_basic_blockObject

Helpers



457
458
459
# File 'lib/rubinius/code/compiler/generator.rb', line 457

def new_basic_block
  BasicBlock.new self
end

#new_labelObject



451
452
453
# File 'lib/rubinius/code/compiler/generator.rb', line 451

def new_label
  Label.new(self)
end

#new_stack_localObject



471
472
473
474
475
# File 'lib/rubinius/code/compiler/generator.rb', line 471

def new_stack_local
  idx = @stack_locals
  @stack_locals += 1
  return idx
end

#object_to_s(literal) ⇒ Object



640
641
642
643
# File 'lib/rubinius/code/compiler/generator.rb', line 640

def object_to_s(literal)
  super
  unwind
end

#package(klass) ⇒ Object



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
# File 'lib/rubinius/code/compiler/generator.rb', line 319

def package(klass)
  @generators.each { |x| @literals[x] = @literals[x].package klass }

  code = klass.new
  code.iseq           = @iseq
  code.literals       = @literals.to_tuple
  code.lines          = @lines.to_tuple

  code.required_args  = @required_args
  code.post_args      = @post_args
  code.total_args     = @total_args
  code.splat          = @splat_index
  code.arity          = @arity
  code.local_count    = @local_count
  code.local_names    = @local_names.to_tuple if @local_names
  code.keywords       = @keywords.to_tuple if @keywords

  code. :block_index, @block_index if @block_index
  code. :kwrest_index, @kwrest_index if @kwrest_index

  code.stack_size     = max_stack_size
  code.file           = @file
  code.name           = @name
  code.primitive      = @primitive

  if @for_block
    code. :for_block, true
  end

  if @for_module_body
    code. :for_module_body, true
  end

  code
end

#passed_arg(index) ⇒ Object



645
646
647
648
# File 'lib/rubinius/code/compiler/generator.rb', line 645

def passed_arg(index)
  super
  unwind
end

#passed_blockarg(count) ⇒ Object



650
651
652
653
# File 'lib/rubinius/code/compiler/generator.rb', line 650

def passed_blockarg(count)
  super
  unwind
end

#pop_modifiersObject



387
388
389
# File 'lib/rubinius/code/compiler/generator.rb', line 387

def pop_modifiers
  @break, @redo, @next, @retry = @modstack.pop
end

#pop_stateObject



379
380
381
# File 'lib/rubinius/code/compiler/generator.rb', line 379

def pop_state
  @state.pop
end

#push_arg(idx) ⇒ Object

Minor meta operations that can be used to detect the number of method arguments needed



510
511
512
513
# File 'lib/rubinius/code/compiler/generator.rb', line 510

def push_arg(idx)
  push_local(idx)
  @detected_args = @detected_locals
end

#push_block_argObject



655
656
657
658
# File 'lib/rubinius/code/compiler/generator.rb', line 655

def push_block_arg
  super
  unwind
end

#push_const(literal) ⇒ Object



660
661
662
663
# File 'lib/rubinius/code/compiler/generator.rb', line 660

def push_const(literal)
  super
  unwind
end

#push_float(float) ⇒ Object



500
501
502
# File 'lib/rubinius/code/compiler/generator.rb', line 500

def push_float(float)
  emit_push_literal add_literal(float)
end

#push_generator(generator) ⇒ Object



477
478
479
480
481
482
# File 'lib/rubinius/code/compiler/generator.rb', line 477

def push_generator(generator)
  index = @literals.size
  push_literal generator
  @generators << index
  index
end

#push_ivar(literal) ⇒ Object



665
666
667
668
# File 'lib/rubinius/code/compiler/generator.rb', line 665

def push_ivar(literal)
  super
  unwind
end

#push_literal(literal) ⇒ Object



504
505
506
# File 'lib/rubinius/code/compiler/generator.rb', line 504

def push_literal(literal)
  emit_push_literal find_literal(literal)
end

#push_local(index) ⇒ Object



515
516
517
518
519
520
521
# File 'lib/rubinius/code/compiler/generator.rb', line 515

def push_local(index)
  if @detected_locals <= index
    @detected_locals = index + 1
  end

  super
end

#push_local_depth(depth, index) ⇒ Object



670
671
672
673
# File 'lib/rubinius/code/compiler/generator.rb', line 670

def push_local_depth(depth, index)
  super
  unwind
end

#push_modifiersObject



383
384
385
# File 'lib/rubinius/code/compiler/generator.rb', line 383

def push_modifiers
  @modstack << [@break, @redo, @next, @retry]
end

#push_procObject



675
676
677
678
# File 'lib/rubinius/code/compiler/generator.rb', line 675

def push_proc
  super
  unwind
end

#push_state(scope) ⇒ Object



375
376
377
# File 'lib/rubinius/code/compiler/generator.rb', line 375

def push_state(scope)
  @state << AST::State.new(scope)
end

#raise_breakObject



680
681
682
683
# File 'lib/rubinius/code/compiler/generator.rb', line 680

def raise_break
  super
  unwind
end

#raise_excObject



685
686
687
688
# File 'lib/rubinius/code/compiler/generator.rb', line 685

def raise_exc
  super
  unwind
end

#raise_returnObject



690
691
692
693
# File 'lib/rubinius/code/compiler/generator.rb', line 690

def raise_return
  super
  unwind
end

#reraiseObject



695
696
697
698
# File 'lib/rubinius/code/compiler/generator.rb', line 695

def reraise
  super
  unwind
end

#retObject



584
585
586
# File 'lib/rubinius/code/compiler/generator.rb', line 584

def ret
  super 0
end

#send(meth, count, priv = false) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/rubinius/code/compiler/generator.rb', line 542

def send(meth, count, priv=false)
  allow_private if priv

  unless count.kind_of? Fixnum
    raise CompileError, "count must be a number"
  end

  if count == 0
    send_method meth
  else
    send_stack meth, count
  end
end

#send_method(literal) ⇒ Object



700
701
702
703
# File 'lib/rubinius/code/compiler/generator.rb', line 700

def send_method(literal)
  super
  unwind
end

#send_primitive(name) ⇒ Object



447
448
449
# File 'lib/rubinius/code/compiler/generator.rb', line 447

def send_primitive(name)
  @primitive = name
end

#send_stack(literal, count) ⇒ Object



705
706
707
708
# File 'lib/rubinius/code/compiler/generator.rb', line 705

def send_stack(literal, count)
  super
  unwind
end

#send_stack_with_block(literal, count) ⇒ Object



710
711
712
713
# File 'lib/rubinius/code/compiler/generator.rb', line 710

def send_stack_with_block(literal, count)
  super
  unwind
end

#send_stack_with_splat(literal, count) ⇒ Object



715
716
717
718
# File 'lib/rubinius/code/compiler/generator.rb', line 715

def send_stack_with_splat(literal, count)
  super
  unwind
end

#send_super(meth, args, splat = false) ⇒ Object



576
577
578
579
580
581
582
# File 'lib/rubinius/code/compiler/generator.rb', line 576

def send_super(meth, args, splat=false)
  if splat
    send_super_stack_with_splat meth, args
  else
    send_super_stack_with_block meth, args
  end
end

#send_super_stack_with_block(literal, count) ⇒ Object



720
721
722
723
# File 'lib/rubinius/code/compiler/generator.rb', line 720

def send_super_stack_with_block(literal, count)
  super
  unwind
end

#send_super_stack_with_splat(literal, count) ⇒ Object



725
726
727
728
# File 'lib/rubinius/code/compiler/generator.rb', line 725

def send_super_stack_with_splat(literal, count)
  super
  unwind
end

#send_vcall(literal) ⇒ Object



730
731
732
733
# File 'lib/rubinius/code/compiler/generator.rb', line 730

def send_vcall(literal)
  super
  unwind
end

#send_with_block(meth, count, priv = false) ⇒ Object



556
557
558
559
560
561
562
563
564
# File 'lib/rubinius/code/compiler/generator.rb', line 556

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

  send_stack_with_block meth, count
end

#send_with_splat(meth, args, priv = false, concat = false) ⇒ Object



566
567
568
569
570
571
572
573
574
# File 'lib/rubinius/code/compiler/generator.rb', line 566

def send_with_splat(meth, args, priv=false, concat=false)
  val = 0
  val |= Rubinius::InstructionSet::CALL_FLAG_CONCAT if concat
  set_call_flags val unless val == 0

  allow_private if priv

  send_stack_with_splat meth, args
end

#set_arg(idx) ⇒ Object



523
524
525
526
# File 'lib/rubinius/code/compiler/generator.rb', line 523

def set_arg(idx)
  set_local(idx)
  @detected_args = @detected_locals
end

#set_ivar(literal) ⇒ Object



735
736
737
738
# File 'lib/rubinius/code/compiler/generator.rb', line 735

def set_ivar(literal)
  super
  unwind
end

#set_line(line) ⇒ Object

Raises:

  • (Exception)


402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/rubinius/code/compiler/generator.rb', line 402

def set_line(line)
  raise Exception, "source code line cannot be nil" unless line

  if !@last_line
    @lines << @ip
    @lines << line
    @last_line = line
  elsif line != @last_line
    if @lines[-2] == @ip
      @lines[-1] = line
    else
      @lines << @ip
      @lines << line
    end

    @last_line = line
  end
end

#set_local(index) ⇒ Object



528
529
530
531
532
533
534
# File 'lib/rubinius/code/compiler/generator.rb', line 528

def set_local(index)
  if @detected_locals <= index
    @detected_locals = index + 1
  end

  super
end

#set_local_depth(depth, index) ⇒ Object



740
741
742
743
# File 'lib/rubinius/code/compiler/generator.rb', line 740

def set_local_depth(depth, index)
  super
  unwind
end

#stateObject

Commands (these don’t generate data in the stream)



371
372
373
# File 'lib/rubinius/code/compiler/generator.rb', line 371

def state
  @state.last
end

#string_build(count) ⇒ Object



745
746
747
748
# File 'lib/rubinius/code/compiler/generator.rb', line 745

def string_build(count)
  super
  unwind
end

#string_dupObject



750
751
752
753
# File 'lib/rubinius/code/compiler/generator.rb', line 750

def string_dup
  super
  unwind
end

#unwindObject



755
756
757
# File 'lib/rubinius/code/compiler/generator.rb', line 755

def unwind
  super 0
end

#use_detectedObject



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/rubinius/code/compiler/generator.rb', line 355

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

#yield_splat(count) ⇒ Object



759
760
761
762
# File 'lib/rubinius/code/compiler/generator.rb', line 759

def yield_splat(count)
  super
  unwind
end

#yield_stack(count) ⇒ Object



764
765
766
767
# File 'lib/rubinius/code/compiler/generator.rb', line 764

def yield_stack(count)
  super
  unwind
end

#zsuper(literal) ⇒ Object



769
770
771
772
# File 'lib/rubinius/code/compiler/generator.rb', line 769

def zsuper(literal)
  super
  unwind
end