Class: Ragweed::Rasm::Bblock

Inherits:
Object
  • Object
show all
Defined in:
lib/ragweed/rasm/bblock.rb

Overview

Ruby inline assembler.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBblock

Don’t call this directly; use Bblock#make



6
7
8
# File 'lib/ragweed/rasm/bblock.rb', line 6

def initialize
  @insns = Ragweed::Rasm::Subprogram.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ragweed/rasm/bblock.rb', line 60

def method_missing(meth, *args)
  k = Ragweed::Rasm.const_get(meth.to_s.capitalize)

  # If it's a class, it's an assembly opcode; otherwise,
  # it's a register or operand.
  if k.class == Class
    @insns << (k = k.new(*args))
  else
    k
  end
  k
end

Class Method Details

.make(&block) ⇒ Object

Takes a block argument, containing (mostly) assembly instructions, as interpreted by Rasm. For example:

Bblock.make {
    push ebp
    mov  ebp, esp
    push ebx
    xor ebx, ebx
    addl esp, 4
    pop ebp
    ret
}

Each of those instructions is in fact the name of a class in Rasm, lowercased; Bblock has a method_missing that catches and instantiates them.

Your block can contain arbitrary Ruby, but remember that it runs in the scope of an anonymous class and so cannot directly reference instance variables.



49
50
51
52
53
# File 'lib/ragweed/rasm/bblock.rb', line 49

def self.make(&block)
  c = Bblock.new
  c.instance_eval(&block)
  c
end

Instance Method Details

#append(&block) ⇒ Object

Append more instructions to a previously created block; see Bblock#make



25
26
27
# File 'lib/ragweed/rasm/bblock.rb', line 25

def append(&block)
  instance_eval(&block)
end

#assembleObject

Assemble the instructions, which also calculates appropriate jump labels.



14
# File 'lib/ragweed/rasm/bblock.rb', line 14

def assemble; @insns.assemble; end

#disassembleObject

Disassemble the block (after it’s been assembled) into Frasm objects.



18
# File 'lib/ragweed/rasm/bblock.rb', line 18

def disassemble; @insns.disassemble; end

#listingObject

Generate a human-readable assembly listing.



21
# File 'lib/ragweed/rasm/bblock.rb', line 21

def listing; @insns.dump_disassembly; end

#sub(*args) ⇒ Object

method to fix collision with Kernel#sub properly



56
57
58
# File 'lib/ragweed/rasm/bblock.rb', line 56

def sub(*args)
  Ragweed::Rasm::Sub.new(*args)
end