Class: CodeTools::Generator
- Inherits:
-
Object
- Object
- CodeTools::Generator
- Includes:
- GeneratorMethods
- Defined in:
- lib/rubinius/code/compiler/generator.rb
Defined Under Namespace
Classes: BasicBlock, Label
Instance Attribute Summary collapse
-
#arity ⇒ Object
Returns the value of attribute arity.
-
#block_index ⇒ Object
Returns the value of attribute block_index.
-
#break ⇒ Object
Returns the value of attribute break.
-
#current_block ⇒ Object
Returns the value of attribute current_block.
-
#detected_args ⇒ Object
Returns the value of attribute detected_args.
-
#detected_locals ⇒ Object
Returns the value of attribute detected_locals.
-
#file ⇒ Object
Returns the value of attribute file.
-
#for_block ⇒ Object
Returns the value of attribute for_block.
-
#for_module_body ⇒ Object
Returns the value of attribute for_module_body.
-
#ip ⇒ Object
readonly
Returns the value of attribute ip.
-
#iseq ⇒ Object
readonly
Returns the value of attribute iseq.
-
#keywords ⇒ Object
Returns the value of attribute keywords.
-
#literals ⇒ Object
readonly
Returns the value of attribute literals.
-
#local_count ⇒ Object
Returns the value of attribute local_count.
-
#local_names ⇒ Object
Returns the value of attribute local_names.
-
#name ⇒ Object
Returns the value of attribute name.
-
#next ⇒ Object
Returns the value of attribute next.
-
#post_args ⇒ Object
Returns the value of attribute post_args.
-
#primitive ⇒ Object
Returns the value of attribute primitive.
-
#redo ⇒ Object
Returns the value of attribute redo.
-
#required_args ⇒ Object
Returns the value of attribute required_args.
-
#retry ⇒ Object
Returns the value of attribute retry.
-
#splat_index ⇒ Object
Returns the value of attribute splat_index.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#total_args ⇒ Object
Returns the value of attribute total_args.
Instance Method Summary collapse
- #accumulate_stack(size) ⇒ Object
- #add_generator(generator) ⇒ Object
- #add_literal(literal) ⇒ Object
- #close ⇒ Object
- #definition_line(line) ⇒ Object
-
#encode ⇒ Object
Formalizers.
- #execute(node) ⇒ Object (also: #run)
- #find_literal(literal) ⇒ Object
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
- #ip_to_line(ip) ⇒ Object
- #last_match(mode, which) ⇒ Object
- #line ⇒ Object
- #max_stack_size ⇒ Object
-
#new_basic_block ⇒ Object
Helpers.
- #new_label ⇒ Object
- #new_stack_local ⇒ Object
- #package(klass) ⇒ Object
- #pop_modifiers ⇒ Object
- #pop_state ⇒ Object
-
#push_arg(idx) ⇒ Object
Minor meta operations that can be used to detect the number of method arguments needed.
- #push_generator(generator) ⇒ Object
- #push_local(idx) ⇒ Object
- #push_modifiers ⇒ Object
- #push_state(scope) ⇒ Object
- #send(meth, count, priv = false) ⇒ Object
- #send_primitive(name) ⇒ Object
- #send_super(meth, args, splat = false) ⇒ Object
- #send_with_block(meth, count, priv = false) ⇒ Object
- #send_with_splat(meth, args, priv = false, concat = false) ⇒ Object
- #set_arg(idx) ⇒ Object
- #set_line(line) ⇒ Object
- #set_local(idx) ⇒ Object
-
#state ⇒ Object
Commands (these don’t generate data in the stream).
- #use_detected ⇒ Object
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, #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_literal, #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
#initialize ⇒ Generator
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 |
# 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 @local_count = 0 @state = [] @generators = [] @stack_locals = 0 @enter_block = new_basic_block @current_block = @enter_block @max_stack = 0 end |
Instance Attribute Details
#arity ⇒ Object
Returns the value of attribute arity.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def arity @arity end |
#block_index ⇒ Object
Returns the value of attribute block_index.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def block_index @block_index end |
#break ⇒ Object
Returns the value of attribute break.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def break @break end |
#current_block ⇒ Object
Returns the value of attribute current_block.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def current_block @current_block end |
#detected_args ⇒ Object
Returns the value of attribute detected_args.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def detected_args @detected_args end |
#detected_locals ⇒ Object
Returns the value of attribute detected_locals.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def detected_locals @detected_locals end |
#file ⇒ Object
Returns the value of attribute file.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def file @file end |
#for_block ⇒ Object
Returns the value of attribute for_block.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def for_block @for_block end |
#for_module_body ⇒ Object
Returns the value of attribute for_module_body.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def for_module_body @for_module_body end |
#ip ⇒ Object (readonly)
Returns the value of attribute ip.
283 284 285 |
# File 'lib/rubinius/code/compiler/generator.rb', line 283 def ip @ip end |
#iseq ⇒ Object (readonly)
Returns the value of attribute iseq.
283 284 285 |
# File 'lib/rubinius/code/compiler/generator.rb', line 283 def iseq @iseq end |
#keywords ⇒ Object
Returns the value of attribute keywords.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def keywords @keywords end |
#literals ⇒ Object (readonly)
Returns the value of attribute literals.
283 284 285 |
# File 'lib/rubinius/code/compiler/generator.rb', line 283 def literals @literals end |
#local_count ⇒ Object
Returns the value of attribute local_count.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def local_count @local_count end |
#local_names ⇒ Object
Returns the value of attribute local_names.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def local_names @local_names end |
#name ⇒ Object
Returns the value of attribute name.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def name @name end |
#next ⇒ Object
Returns the value of attribute next.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def next @next end |
#post_args ⇒ Object
Returns the value of attribute post_args.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def post_args @post_args end |
#primitive ⇒ Object
Returns the value of attribute primitive.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def primitive @primitive end |
#redo ⇒ Object
Returns the value of attribute redo.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def redo @redo end |
#required_args ⇒ Object
Returns the value of attribute required_args.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def required_args @required_args end |
#retry ⇒ Object
Returns the value of attribute retry.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def retry @retry end |
#splat_index ⇒ Object
Returns the value of attribute splat_index.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def splat_index @splat_index end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
283 284 285 |
# File 'lib/rubinius/code/compiler/generator.rb', line 283 def stream @stream end |
#total_args ⇒ Object
Returns the value of attribute total_args.
284 285 286 |
# File 'lib/rubinius/code/compiler/generator.rb', line 284 def total_args @total_args end |
Instance Method Details
#accumulate_stack(size) ⇒ Object
454 455 456 |
# File 'lib/rubinius/code/compiler/generator.rb', line 454 def accumulate_stack(size) @max_stack = size if size > @max_stack end |
#add_generator(generator) ⇒ Object
477 478 479 480 481 |
# File 'lib/rubinius/code/compiler/generator.rb', line 477 def add_generator(generator) index = add_literal generator @generators << index index end |
#add_literal(literal) ⇒ Object
487 488 489 490 491 |
# File 'lib/rubinius/code/compiler/generator.rb', line 487 def add_literal(literal) index = @literals.size @literals << literal index end |
#close ⇒ Object
431 432 433 434 435 436 437 438 |
# File 'lib/rubinius/code/compiler/generator.rb', line 431 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
384 385 386 387 388 389 390 391 392 393 |
# File 'lib/rubinius/code/compiler/generator.rb', line 384 def definition_line(line) unless @stream.empty? raise Exception, "only use #definition_line first" end @lines << -1 @lines << line @last_line = line end |
#encode ⇒ Object
Formalizers
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/rubinius/code/compiler/generator.rb', line 298 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.} (#{e.class})" end raise e end @generators.each { |x| @literals[x].encode } end |
#execute(node) ⇒ Object Also known as: run
290 291 292 |
# File 'lib/rubinius/code/compiler/generator.rb', line 290 def execute(node) node.bytecode self end |
#find_literal(literal) ⇒ Object
483 484 485 |
# File 'lib/rubinius/code/compiler/generator.rb', line 483 def find_literal(literal) @literals_map[literal] end |
#ip_to_line(ip) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/rubinius/code/compiler/generator.rb', line 418 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
521 522 523 524 525 |
# File 'lib/rubinius/code/compiler/generator.rb', line 521 def last_match(mode, which) push_int Integer(mode) push_int Integer(which) invoke_primitive :regexp_last_match_result, 2 end |
#line ⇒ Object
414 415 416 |
# File 'lib/rubinius/code/compiler/generator.rb', line 414 def line @last_line end |
#max_stack_size ⇒ Object
458 459 460 461 462 |
# File 'lib/rubinius/code/compiler/generator.rb', line 458 def max_stack_size size = @max_stack + @local_count + @stack_locals size += 1 if @for_block size end |
#new_basic_block ⇒ Object
Helpers
450 451 452 |
# File 'lib/rubinius/code/compiler/generator.rb', line 450 def new_basic_block BasicBlock.new self end |
#new_label ⇒ Object
444 445 446 |
# File 'lib/rubinius/code/compiler/generator.rb', line 444 def new_label Label.new(self) end |
#new_stack_local ⇒ Object
464 465 466 467 468 |
# File 'lib/rubinius/code/compiler/generator.rb', line 464 def new_stack_local idx = @stack_locals @stack_locals += 1 return idx end |
#package(klass) ⇒ Object
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 |
# File 'lib/rubinius/code/compiler/generator.rb', line 314 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.block_index = @block_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.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_modifiers ⇒ Object
380 381 382 |
# File 'lib/rubinius/code/compiler/generator.rb', line 380 def pop_modifiers @break, @redo, @next, @retry = @modstack.pop end |
#pop_state ⇒ Object
372 373 374 |
# File 'lib/rubinius/code/compiler/generator.rb', line 372 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
511 512 513 514 |
# File 'lib/rubinius/code/compiler/generator.rb', line 511 def push_arg(idx) push_local(idx) @detected_args = @detected_locals end |
#push_generator(generator) ⇒ Object
470 471 472 473 474 475 |
# File 'lib/rubinius/code/compiler/generator.rb', line 470 def push_generator(generator) index = @literals.size push_literal generator @generators << index index end |
#push_local(idx) ⇒ Object
493 494 495 496 497 498 499 |
# File 'lib/rubinius/code/compiler/generator.rb', line 493 def push_local(idx) if @detected_locals <= idx @detected_locals = idx + 1 end super end |
#push_modifiers ⇒ Object
376 377 378 |
# File 'lib/rubinius/code/compiler/generator.rb', line 376 def push_modifiers @modstack << [@break, @redo, @next, @retry] end |
#push_state(scope) ⇒ Object
368 369 370 |
# File 'lib/rubinius/code/compiler/generator.rb', line 368 def push_state(scope) @state << AST::State.new(scope) end |
#send(meth, count, priv = false) ⇒ Object
527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/rubinius/code/compiler/generator.rb', line 527 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
440 441 442 |
# File 'lib/rubinius/code/compiler/generator.rb', line 440 def send_primitive(name) @primitive = name end |
#send_super(meth, args, splat = false) ⇒ Object
561 562 563 564 565 566 567 |
# File 'lib/rubinius/code/compiler/generator.rb', line 561 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
541 542 543 544 545 546 547 548 549 |
# File 'lib/rubinius/code/compiler/generator.rb', line 541 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
551 552 553 554 555 556 557 558 559 |
# File 'lib/rubinius/code/compiler/generator.rb', line 551 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
516 517 518 519 |
# File 'lib/rubinius/code/compiler/generator.rb', line 516 def set_arg(idx) set_local(idx) @detected_args = @detected_locals end |
#set_line(line) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/rubinius/code/compiler/generator.rb', line 395 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
501 502 503 504 505 506 507 |
# File 'lib/rubinius/code/compiler/generator.rb', line 501 def set_local(idx) if @detected_locals <= idx @detected_locals = idx + 1 end super end |
#state ⇒ Object
Commands (these don’t generate data in the stream)
364 365 366 |
# File 'lib/rubinius/code/compiler/generator.rb', line 364 def state @state.last end |
#use_detected ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/rubinius/code/compiler/generator.rb', line 348 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 |