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, #b_if, #b_if_int, #b_if_serial, #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, #n_iadd, #n_iadd_o, #n_idiv, #n_idiv_o, #n_ieq, #n_ige, #n_igt, #n_ile, #n_ilt, #n_imul, #n_imul_o, #n_ine, #n_ipopcnt, #n_isub, #n_isub_o, #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, #r_copy, #r_load_int, #r_load_literal, #r_load_local, #r_load_local_depth, #r_load_stack, #r_store_int, #r_store_local, #r_store_local_depth, #r_store_stack, #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
287
# 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
  @registers = 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.



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

def arity
  @arity
end

#block_indexObject

Returns the value of attribute block_index.



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

def block_index
  @block_index
end

#breakObject

Returns the value of attribute break.



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

def break
  @break
end

#current_blockObject

Returns the value of attribute current_block.



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

def current_block
  @current_block
end

#detected_argsObject

Returns the value of attribute detected_args.



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

def detected_args
  @detected_args
end

#detected_localsObject

Returns the value of attribute detected_locals.



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

def detected_locals
  @detected_locals
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#for_blockObject

Returns the value of attribute for_block.



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

def for_block
  @for_block
end

#for_module_bodyObject

Returns the value of attribute for_module_body.



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

def for_module_body
  @for_module_body
end

#ipObject (readonly)

Returns the value of attribute ip.



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

def ip
  @ip
end

#iseqObject (readonly)

Returns the value of attribute iseq.



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

def iseq
  @iseq
end

#keywordsObject

Returns the value of attribute keywords.



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

def keywords
  @keywords
end

#kwrest_indexObject

Returns the value of attribute kwrest_index.



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

def kwrest_index
  @kwrest_index
end

#literalsObject (readonly)

Returns the value of attribute literals.



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

def literals
  @literals
end

#local_countObject

Returns the value of attribute local_count.



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

def local_count
  @local_count
end

#local_namesObject

Returns the value of attribute local_names.



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

def local_names
  @local_names
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#nextObject

Returns the value of attribute next.



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

def next
  @next
end

#post_argsObject

Returns the value of attribute post_args.



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

def post_args
  @post_args
end

#primitiveObject

Returns the value of attribute primitive.



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

def primitive
  @primitive
end

#redoObject

Returns the value of attribute redo.



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

def redo
  @redo
end

#registersObject

Returns the value of attribute registers.



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

def registers
  @registers
end

#required_argsObject

Returns the value of attribute required_args.



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

def required_args
  @required_args
end

#retryObject

Returns the value of attribute retry.



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

def retry
  @retry
end

#splat_indexObject

Returns the value of attribute splat_index.



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

def splat_index
  @splat_index
end

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

#total_argsObject

Returns the value of attribute total_args.



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

def total_args
  @total_args
end

Instance Method Details

#accumulate_stack(size) ⇒ Object



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

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

#add_generator(generator) ⇒ Object



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

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

#add_literal(literal) ⇒ Object



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

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

#cast_arrayObject

instructions that can cause stack unwinding



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

def cast_array
  super
  unwind
end

#cast_for_multi_block_argObject



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

def cast_for_multi_block_arg
  super
  unwind
end

#cast_for_single_block_argObject



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

def cast_for_single_block_arg
  super
  unwind
end

#cast_for_splat_block_argObject



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

def cast_for_splat_block_arg
  super
  unwind
end

#cast_multi_valueObject



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

def cast_multi_value
  super
  unwind
end

#check_frozenObject



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

def check_frozen
  super
  unwind
end

#closeObject



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

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



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

def create_block(literal)
  super
  unwind
end

#definition_line(line) ⇒ Object



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

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



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

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



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

def ensure_return
  super
  unwind
end

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



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

def execute(node)
  node.bytecode self
end

#find_const(literal) ⇒ Object



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

def find_const(literal)
  super
  unwind
end

#find_literal(literal) ⇒ Object



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

def find_literal(literal)
  @literals_map[literal]
end

#invoke_primitive(literal, count) ⇒ Object



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

def invoke_primitive(literal, count)
  super
  unwind
end

#ip_to_line(ip) ⇒ Object



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

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



542
543
544
545
546
# File 'lib/rubinius/code/compiler/generator.rb', line 542

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

#lineObject



423
424
425
# File 'lib/rubinius/code/compiler/generator.rb', line 423

def line
  @last_line
end

#max_stack_sizeObject



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

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

#new_basic_blockObject

Helpers



463
464
465
# File 'lib/rubinius/code/compiler/generator.rb', line 463

def new_basic_block
  BasicBlock.new self
end

#new_labelObject



453
454
455
# File 'lib/rubinius/code/compiler/generator.rb', line 453

def new_label
  Label.new(self)
end

#new_registerObject



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

def new_register
  (@registers += 1) - 1
end

#new_stack_localObject



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

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

#object_to_s(literal) ⇒ Object



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

def object_to_s(literal)
  super
  unwind
end

#package(klass) ⇒ Object



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

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.registers      = @registers

  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



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

def passed_arg(index)
  super
  unwind
end

#passed_blockarg(count) ⇒ Object



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

def passed_blockarg(count)
  super
  unwind
end

#pop_modifiersObject



389
390
391
# File 'lib/rubinius/code/compiler/generator.rb', line 389

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

#pop_stateObject



381
382
383
# File 'lib/rubinius/code/compiler/generator.rb', line 381

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



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

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

#push_block_argObject



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

def push_block_arg
  super
  unwind
end

#push_const(literal) ⇒ Object



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

def push_const(literal)
  super
  unwind
end

#push_float(float) ⇒ Object



506
507
508
# File 'lib/rubinius/code/compiler/generator.rb', line 506

def push_float(float)
  emit_push_literal add_literal(float)
end

#push_generator(generator) ⇒ Object



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

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

#push_ivar(literal) ⇒ Object



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

def push_ivar(literal)
  super
  unwind
end

#push_literal(literal) ⇒ Object



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

def push_literal(literal)
  emit_push_literal find_literal(literal)
end

#push_local(index) ⇒ Object



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

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

  super
end

#push_local_depth(depth, index) ⇒ Object



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

def push_local_depth(depth, index)
  super
  unwind
end

#push_modifiersObject



385
386
387
# File 'lib/rubinius/code/compiler/generator.rb', line 385

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

#push_procObject



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

def push_proc
  super
  unwind
end

#push_state(scope) ⇒ Object



377
378
379
# File 'lib/rubinius/code/compiler/generator.rb', line 377

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

#raise_breakObject



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

def raise_break
  super
  unwind
end

#raise_excObject



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

def raise_exc
  super
  unwind
end

#raise_returnObject



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

def raise_return
  super
  unwind
end

#reraiseObject



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

def reraise
  super
  unwind
end

#retObject



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

def ret
  super 0
end

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



548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/rubinius/code/compiler/generator.rb', line 548

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



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

def send_method(literal)
  super
  unwind
end

#send_primitive(name) ⇒ Object



449
450
451
# File 'lib/rubinius/code/compiler/generator.rb', line 449

def send_primitive(name)
  @primitive = name
end

#send_stack(literal, count) ⇒ Object



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

def send_stack(literal, count)
  super
  unwind
end

#send_stack_with_block(literal, count) ⇒ Object



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

def send_stack_with_block(literal, count)
  super
  unwind
end

#send_stack_with_splat(literal, count) ⇒ Object



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

def send_stack_with_splat(literal, count)
  super
  unwind
end

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



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

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



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

def send_super_stack_with_block(literal, count)
  super
  unwind
end

#send_super_stack_with_splat(literal, count) ⇒ Object



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

def send_super_stack_with_splat(literal, count)
  super
  unwind
end

#send_vcall(literal) ⇒ Object



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

def send_vcall(literal)
  super
  unwind
end

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



562
563
564
565
566
567
568
569
570
# File 'lib/rubinius/code/compiler/generator.rb', line 562

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



572
573
574
575
576
577
578
579
580
# File 'lib/rubinius/code/compiler/generator.rb', line 572

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



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

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

#set_ivar(literal) ⇒ Object



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

def set_ivar(literal)
  super
  unwind
end

#set_line(line) ⇒ Object

Raises:

  • (Exception)


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

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



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

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

  super
end

#set_local_depth(depth, index) ⇒ Object



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

def set_local_depth(depth, index)
  super
  unwind
end

#stateObject

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



373
374
375
# File 'lib/rubinius/code/compiler/generator.rb', line 373

def state
  @state.last
end

#string_build(count) ⇒ Object



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

def string_build(count)
  super
  unwind
end

#string_dupObject



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

def string_dup
  super
  unwind
end

#unwindObject



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

def unwind
  super 0
end

#use_detectedObject



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

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



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

def yield_splat(count)
  super
  unwind
end

#yield_stack(count) ⇒ Object



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

def yield_stack(count)
  super
  unwind
end

#zsuper(literal) ⇒ Object



775
776
777
778
# File 'lib/rubinius/code/compiler/generator.rb', line 775

def zsuper(literal)
  super
  unwind
end