Module: YABFI::Unroll

Defined in:
lib/yabfi/unroll.rb

Overview

This module is used to transforms unrolls loops into multiple branch_if_zero and branch_not_zero instructions.

Class Method Summary collapse

Class Method Details

.unroll(forest) ⇒ Array<Object>

Unroll an entire syntax forest.

Parameters:

  • forest (Array<Object>)

    the forest to unroll.

Returns:

  • (Array<Object>)

    the unrolled commands.



11
12
13
14
15
16
17
18
19
# File 'lib/yabfi/unroll.rb', line 11

def unroll(forest)
  forest.each_with_object([]) do |(command, arg), ary|
    if command == :loop
      ary.push(*unroll_loop(arg))
    else
      ary.push([command, arg])
    end
  end
end

.unroll_loop(commands) ⇒ Array<Object>

Unroll a single loop of commands.

Parameters:

  • commands (Array<Object>)

    the loop to unroll.

Returns:

  • (Array<Object>)

    the unrolled commands.



25
26
27
28
29
30
31
# File 'lib/yabfi/unroll.rb', line 25

def unroll_loop(commands)
  unroll(commands).tap do |unrolled|
    offset = unrolled.length
    unrolled.unshift([:branch_if_zero, offset + 2])
    unrolled.push([:branch_not_zero, -1 * offset])
  end
end