class MIPSUnit::MSpec::DataLabel

Represents a label in an assembly file’s .data section. This class

Author

Zachary Kurmas

Copyright

Copyright © 2012

Constants

DATA_DIRECTIVES

A Hash whose keys are the allowed data directives (:word, :byte, :asciiz, etc.)

Attributes

data_string[R]

the line of assembly code to initialize the label.

label[R]

the underlying Label object

Public Instance Methods

local_name() click to toggle source

return the local name of this label

# File lib/mipsunit/mspec/data_label.rb, line 97
def local_name
  @label.local_name
end
to_s() click to toggle source

return the line of assembly text for this label

# File lib/mipsunit/mspec/data_label.rb, line 107
def to_s
  "#{@label.unique_name}: #{@data_string}"
end
unique_name() click to toggle source

return the unique name of this label

# File lib/mipsunit/mspec/data_label.rb, line 102
def unique_name
  @label.unique_name
end

Public Class Methods

all_labels() click to toggle source

returns a list of all DataLabel objects that have been created

# File lib/mipsunit/mspec/data_label.rb, line 85
def self.all_labels
  @@labels
end
data_item_to_string(datum, name=nil) click to toggle source

converts the given datum for a data label to the corresponding String.

  • Integer and Float are unchanged

  • String are placed in quotation marks

  • true becomes 1 and false becomes 0

  • Symbols are converted into the corresponding directive (:word is converted to .word, :ascii to .ascii, etc.)

name is used for error reporting only

raises an InvalidSpecException if the datum is not recognized.

# File lib/mipsunit/mspec/data_label.rb, line 53
def self.data_item_to_string(datum, name=nil)
  if (name.nil?)
    name = ""
  else
    name = "#{name}: "
  end
  if datum.is_a?(Symbol) && DATA_DIRECTIVES.has_key?(datum)
    ".#{datum.to_s}"
  elsif datum.is_a?(Integer) || datum.is_a?(Float)
    datum.to_s
  elsif datum.is_a?(String)
    %Q"#{datum}"!
  elsif datum.is_a?(TrueClass)
    "1"
  elsif datum.is_a?(FalseClass)
    "0"
  elsif datum.nil?
    raise InvalidSpecException.new("#{name}Data items may not be nil.")
  else
    raise InvalidSpecException.new(%Q#{name}"#{datum.inspect}" is not a valid data item.!)
  end
end
is_size_directive?(directive) click to toggle source

returns true if directive specifies a data size (:word, :byte, etc.)

# File lib/mipsunit/mspec/data_label.rb, line 28
def self.is_size_directive?(directive)
  return false if directive == :align
  return false if directive == :space
  DATA_DIRECTIVES.has_key?(directive)
end
make_data_string(name, *data) click to toggle source

Convert the array of values into a String to be placed in an assembly file’s .data section. The name parameter is used only to generate error messages.

# File lib/mipsunit/mspec/data_label.rb, line 80
def self.make_data_string(name, *data)
  data.map { |item| data_item_to_string(item, name) }.join(' ')
end
new(local_name, *data) click to toggle source

generate a new DataLabel with the given local name and array of values/directives

# File lib/mipsunit/mspec/data_label.rb, line 90
def initialize(local_name, *data)
  @label = Label.new(local_name)
  @data_string = DataLabel.make_data_string(local_name, *data)
  @@labels << self
end
size_of(directive) click to toggle source

returns the size (in bytes) corresponding to the given directive

raises InvalidSpecException if the directive does not correspond to a size (e.g., :space, :align)

# File lib/mipsunit/mspec/data_label.rb, line 37
def self.size_of (directive)
  raise InvalidSpecException.new(".align does not have a size and cannot be used in this context") if directive ==:align
  raise InvalidSpecException.new(".space does not have a size and cannot be used in this context") if directive == :space
  raise InvalidSpecException.new("#{directive.to_s} is not a valid data type") unless is_size_directive?(directive)
  DATA_DIRECTIVES[directive]
end