compiler/asm/asm.rb
Sami Samhuri a4506bab10 [NEW] First hints of cross-platform support. Compiles to Mach-O on Darwin with nasm and gcc.
There is no binary assembler support for Darwin yet! I'm not sure when I will dive into the details
of generating a Mach-O binary from Ruby or C.

[MERGED] Binary assembler support.  It *should* work on ELF but it needs testing on Linux.
2009-05-25 16:26:21 -07:00

51 lines
1.2 KiB
Ruby

# Assembler container module. Sub modules are Text and Binary, which
# both export the same interface for generating either assembly or
# machine code for x86.
#
# sjs
# may 2009
module Assembler
# Abstract class for common functionality between different code
# generators. Also defines somewhat of an interface that must be
# implemented to be useful.
class AssemblerBase
def initialize(*args)
@vars = {} # Symbol table, maps names to locations in BSS.
@num_labels = 0 # Used to generate unique labels.
@num_labels_with_suffix = Hash.new(0)
# Maps names to locations.
@labels = Hash.new {|h, key| raise "undefined label: #{key}"}
end
def block(*args, &blk)
instance_eval(&blk)
end
def output
raise "#{self.class} is supposed to implement this method!"
end
def var(name)
@vars[name]
end
alias_method :var?, :var
# Generate a unique label.
def label(suffix=nil)
@num_labels += 1
if suffix
@num_labels_with_suffix[suffix] += 1
suffix = "_#{suffix}_#{@num_labels_with_suffix[suffix]}"
end
name = "L#{sprintf "%06d", @num_labels}#{suffix}"
return name
end
end
end