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, #cast_array, #cast_for_multi_block_arg, #cast_for_single_block_arg, #cast_for_splat_block_arg, #cast_multi_value, #check_frozen, #check_interrupts, #check_serial, #check_serial_private, #clear_exception, #create_block, #dup, #dup_many, #emit_push_literal, #ensure_return, #find_const, #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, #invoke_primitive, #kind_of, #make_array, #move_down, #noop, #object_to_s, #passed_arg, #passed_blockarg, #pop, #pop_many, #pop_unwind, #push_block, #push_block_arg, #push_const, #push_cpath_top, #push_current_exception, #push_exception_state, #push_false, #push_has_block, #push_int, #push_ivar, #push_local_depth, #push_memo, #push_mirror, #push_my_field, #push_my_offset, #push_nil, #push_proc, #push_rubinius, #push_scope, #push_self, #push_stack_local, #push_true, #push_type, #push_undef, #push_variables, #raise_break, #raise_exc, #raise_return, #reraise, #restore_exception_state, #ret, #rotate, #send_method, #send_stack, #send_stack_with_block, #send_stack_with_splat, #send_super_stack_with_block, #send_super_stack_with_splat, #send_vcall, #set_call_flags, #set_const, #set_const_at, #set_ivar, #set_local_depth, #set_stack_local, #setup_unwind, #shift_array, #store_my_field, #string_append, #string_build, #string_dup, #swap, #yield_debugger, #yield_splat, #yield_stack, #zsuper

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



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

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.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def arity
  @arity
end

#block_indexObject

Returns the value of attribute block_index.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def block_index
  @block_index
end

#breakObject

Returns the value of attribute break.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def break
  @break
end

#current_blockObject

Returns the value of attribute current_block.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def current_block
  @current_block
end

#detected_argsObject

Returns the value of attribute detected_args.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def detected_args
  @detected_args
end

#detected_localsObject

Returns the value of attribute detected_locals.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def detected_locals
  @detected_locals
end

#fileObject

Returns the value of attribute file.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def file
  @file
end

#for_blockObject

Returns the value of attribute for_block.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def for_block
  @for_block
end

#for_module_bodyObject

Returns the value of attribute for_module_body.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def for_module_body
  @for_module_body
end

#ipObject (readonly)

Returns the value of attribute ip.



284
285
286
# File 'lib/rubinius/code/compiler/generator.rb', line 284

def ip
  @ip
end

#iseqObject (readonly)

Returns the value of attribute iseq.



284
285
286
# File 'lib/rubinius/code/compiler/generator.rb', line 284

def iseq
  @iseq
end

#keywordsObject

Returns the value of attribute keywords.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def keywords
  @keywords
end

#kwrest_indexObject

Returns the value of attribute kwrest_index.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def kwrest_index
  @kwrest_index
end

#literalsObject (readonly)

Returns the value of attribute literals.



284
285
286
# File 'lib/rubinius/code/compiler/generator.rb', line 284

def literals
  @literals
end

#local_countObject

Returns the value of attribute local_count.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def local_count
  @local_count
end

#local_namesObject

Returns the value of attribute local_names.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def local_names
  @local_names
end

#nameObject

Returns the value of attribute name.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def name
  @name
end

#nextObject

Returns the value of attribute next.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def next
  @next
end

#post_argsObject

Returns the value of attribute post_args.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def post_args
  @post_args
end

#primitiveObject

Returns the value of attribute primitive.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def primitive
  @primitive
end

#redoObject

Returns the value of attribute redo.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def redo
  @redo
end

#required_argsObject

Returns the value of attribute required_args.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def required_args
  @required_args
end

#retryObject

Returns the value of attribute retry.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def retry
  @retry
end

#splat_indexObject

Returns the value of attribute splat_index.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def splat_index
  @splat_index
end

#streamObject (readonly)

Returns the value of attribute stream.



284
285
286
# File 'lib/rubinius/code/compiler/generator.rb', line 284

def stream
  @stream
end

#total_argsObject

Returns the value of attribute total_args.



285
286
287
# File 'lib/rubinius/code/compiler/generator.rb', line 285

def total_args
  @total_args
end

Instance Method Details

#accumulate_stack(size) ⇒ Object



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

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

#add_generator(generator) ⇒ Object



480
481
482
483
484
# File 'lib/rubinius/code/compiler/generator.rb', line 480

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

#add_literal(literal) ⇒ Object



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

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

#closeObject



434
435
436
437
438
439
440
441
# File 'lib/rubinius/code/compiler/generator.rb', line 434

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

  @lines << @ip
end

#definition_line(line) ⇒ Object



387
388
389
390
391
392
393
394
395
396
# File 'lib/rubinius/code/compiler/generator.rb', line 387

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



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rubinius/code/compiler/generator.rb', line 299

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

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



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

def execute(node)
  node.bytecode self
end

#find_literal(literal) ⇒ Object



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

def find_literal(literal)
  @literals_map[literal]
end

#ip_to_line(ip) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/rubinius/code/compiler/generator.rb', line 421

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



532
533
534
535
536
# File 'lib/rubinius/code/compiler/generator.rb', line 532

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

#lineObject



417
418
419
# File 'lib/rubinius/code/compiler/generator.rb', line 417

def line
  @last_line
end

#max_stack_sizeObject



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

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

#new_basic_blockObject

Helpers



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

def new_basic_block
  BasicBlock.new self
end

#new_labelObject



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

def new_label
  Label.new(self)
end

#new_stack_localObject



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

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

#package(klass) ⇒ Object



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

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

#pop_modifiersObject



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

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

#pop_stateObject



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

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



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

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

#push_float(float) ⇒ Object



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

def push_float(float)
  emit_push_literal add_literal(float)
end

#push_generator(generator) ⇒ Object



473
474
475
476
477
478
# File 'lib/rubinius/code/compiler/generator.rb', line 473

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

#push_literal(literal) ⇒ Object



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

def push_literal(literal)
  emit_push_literal find_literal(literal)
end

#push_local(idx) ⇒ Object



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

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

  super
end

#push_modifiersObject



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

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

#push_state(scope) ⇒ Object



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

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

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



538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/rubinius/code/compiler/generator.rb', line 538

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_primitive(name) ⇒ Object



443
444
445
# File 'lib/rubinius/code/compiler/generator.rb', line 443

def send_primitive(name)
  @primitive = name
end

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



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

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_with_block(meth, count, priv = false) ⇒ Object



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

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



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

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



527
528
529
530
# File 'lib/rubinius/code/compiler/generator.rb', line 527

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

#set_line(line) ⇒ Object

Raises:

  • (Exception)


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/rubinius/code/compiler/generator.rb', line 398

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(idx) ⇒ Object



512
513
514
515
516
517
518
# File 'lib/rubinius/code/compiler/generator.rb', line 512

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

  super
end

#stateObject

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



367
368
369
# File 'lib/rubinius/code/compiler/generator.rb', line 367

def state
  @state.last
end

#use_detectedObject



351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/rubinius/code/compiler/generator.rb', line 351

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