class Template

Attributes

template[R]

Public Class Methods

inherited(subclass) click to toggle source

This method adds every subclass of Template to the list of templates to parse

# File template.rb, line 45
def self.inherited(subclass)
  subclass_file = parse_caller(caller[0])[0]
  puts "Loaded template #{subclass} defined in #{subclass_file}"
  @@template_classes.store subclass, subclass_file
end
new() click to toggle source
Calls superclass method MmuPlugin.new
# File template.rb, line 30
def initialize
  super

  java_import Java::Ru.ispras.microtesk.test.TestEngine
  engine = TestEngine.getInstance()

  TemplateBuilder.define_runtime_methods engine.getMetaModel
  @template = engine.newTemplate
end
parse_caller(at) click to toggle source

Parses the text of stack entries returned by the “caller” method, which have the following format: <file:line> or <file:line: in `method’>.

# File template.rb, line 53
def self.parse_caller(at)
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    file   = Regexp.last_match[1]
    line   = Regexp.last_match[2].to_i
    method = Regexp.last_match[3]
    return [file, line, method]
  end
  raise MTRubyError, "Failed to parse #{at}."
end
template_classes() click to toggle source
# File template.rb, line 40
def self.template_classes
  @@template_classes
end

Public Instance Methods

_(allocator = nil, attrs = {}) click to toggle source

Creates an object that specifies an unknown immediate value to be used as an argument of a mode or op. A corresponding concrete value must be produced as a result of test data generation for some test situation.

# File template.rb, line 376
def _(allocator = nil, attrs = {})
  if allocator.is_a? Hash and attrs.empty? then
    attrs = allocator
    allocator = nil
  end

  if !attrs.is_a?(Hash)
    raise MTRubyError, "#{attrs} is not a Hash."
  end

  retain = attrs[:retain]
  exclude = attrs[:exclude]

  @template.newUnknownImmediate get_caller_location, allocator, retain, exclude
end
address(*args) click to toggle source
# File template.rb, line 680
def address(*args)
  if args.count != 0 and args.count != 2
    raise MTRubyError, "Wrong argument count: #{args.count}. Must be 0 or 2."
  end

  if args.count == 2
    @template.newAddressReference args.at(0), args.at(1)
  else
    @template.newAddressReference
  end
end
align(value) click to toggle source
# File template.rb, line 791
def align(value)
  value_in_bytes = alignment_in_bytes(value)
  @template.setAlignment value, value_in_bytes
end
alignment_in_bytes(n) click to toggle source

By default, align n is interpreted as alignment on 2**n byte border. This behavior can be overridden.

# File template.rb, line 800
def alignment_in_bytes(n)
  2 ** n
end
atomic(attributes = {}, &contents) click to toggle source
# File template.rb, line 158
def atomic(attributes = {}, &contents)
  blockBuilder = @template.beginBlock
  blockBuilder.setWhere get_caller_location

  blockBuilder.setAtomic true
  blockBuilder.setSequence false
  blockBuilder.setIterate false

  if attributes.has_key? :obfuscator
    blockBuilder.setObfuscator(attributes[:obfuscator])
  end

  attributes.each_pair do |key, value|
    blockBuilder.setAttribute(key.to_s, value)
  end

  self.instance_eval &contents
  @template.endBlock
end
block(attributes = {}, &contents) click to toggle source

————————————————————————- # Methods for template description facilities # ————————————————————————- #

# File template.rb, line 101
def block(attributes = {}, &contents)
  blockBuilder = @template.beginBlock
  blockBuilder.setWhere get_caller_location

  blockBuilder.setAtomic false
  blockBuilder.setSequence false
  blockBuilder.setIterate false

  if attributes.has_key? :combinator
    blockBuilder.setCombinator(attributes[:combinator])
  end

  if attributes.has_key? :permutator
    blockBuilder.setPermutator(attributes[:permutator])
  end

  if attributes.has_key? :compositor
    blockBuilder.setCompositor(attributes[:compositor])
  end

  if attributes.has_key? :rearranger
    blockBuilder.setRearranger(attributes[:rearranger])
  end

  if attributes.has_key? :obfuscator
    blockBuilder.setObfuscator(attributes[:obfuscator])
  end

  attributes.each_pair do |key, value|
    blockBuilder.setAttribute(key.to_s, value)
  end

  self.instance_eval &contents

  @template.endBlock
end
buffer_preparator(attrs, &contents) click to toggle source

————————————————————————– # Creating Buffer Preparators # ————————————————————————– #

# File template.rb, line 668
def buffer_preparator(attrs, &contents)
  buffer_id = get_attribute attrs, :target
  builder = @template.beginBufferPreparator buffer_id

  if attrs.has_key?(:level)
    builder.setLevel attrs[:level]
  end

  self.instance_eval &contents
  @template.endBufferPreparator
end
comment(format, *args) click to toggle source

Adds a comment into the test program (uses sl_comment_starts_with).

# File template.rb, line 481
def comment(format, *args)
  print_format :COMMENT, format, *args
end
comparator(attrs, &contents) click to toggle source
# File template.rb, line 540
def comparator(attrs, &contents)
  create_preparator(true, attrs, &contents)
end
create_preparator(is_comparator, attrs, &contents) click to toggle source
# File template.rb, line 544
def create_preparator(is_comparator, attrs, &contents)
  target = get_attribute attrs, :target

  builder = @template.beginPreparator target.to_s, is_comparator
  builder.setWhere get_caller_location 2

  name = attrs[:name]
  if !name.nil?
    builder.setName name.to_s
  end

  mask = attrs[:mask]
  if !mask.nil?
    if mask.is_a?(String)
      builder.setMaskValue mask
    elsif mask.is_a?(Array)
      builder.setMaskCollection mask
    else
      raise MTRubyError, "Illegal mask type: #{mask}"
    end
  end

  arguments = attrs[:arguments]
  if !arguments.nil?
    if !arguments.is_a?(Hash)
      raise MTRubyError, "#{arguments} is not a Hash."
    end

    arguments.each_pair do |name, value|
      if value.is_a?(Integer)
        builder.addArgumentValue name, value
      elsif value.is_a?(Range)
        builder.addArgumentRange name, value.min, value.max
      elsif value.is_a?(Array)
        builder.addArgumentCollection name, value
      else
        raise MTRubyError, "Illegal value of #{name} argument: #{value}"
      end
    end
  end

  self.instance_eval &contents
  @template.endPreparator
end
data(attrs = {}, &contents) click to toggle source
# File template.rb, line 751
def data(attrs = {}, &contents)
  if nil == @data_manager
    raise MTRubyError, "Data configuration is not defined"
  end

  if attrs.has_key?(:global)
    global = attrs[:global]
  else
    global = false
  end

  if attrs.has_key?(:separate_file)
    separate_file = attrs[:separate_file]
  else
    separate_file = false
  end

  @data_manager.beginData global, separate_file
  @data_manager.instance_eval &contents
  @data_manager.endData
end
data_config(attrs, &contents) click to toggle source

————————————————————————– # Data Definition Facilities # ————————————————————————– #

# File template.rb, line 732
def data_config(attrs, &contents)
  if nil != @data_manager
    raise MTRubyError, "Data configuration is already defined"
  end

  text   = get_attribute attrs, :text
  target = get_attribute attrs, :target

  # Default value is 8 bits if other value is not explicitly specified
  addressableSize = attrs.has_key?(:item_size) ? attrs[:item_size] : 8
  baseVirtAddr = attrs.has_key?(:base_virtual_address) ? attrs[:base_virtual_address] : nil

  @data_manager = DataManager.new(self, @template.getDataManager)
  @data_manager.beginConfig text, target, addressableSize, baseVirtAddr

  @data_manager.instance_eval &contents
  @data_manager.endConfig
end
data_source() click to toggle source
# File template.rb, line 644
def data_source
  @template.getDataSource
end
define_mode_group(name, distribution) click to toggle source

————————————————————————– # Defining Groups # ————————————————————————– #

# File template.rb, line 424
def define_mode_group(name, distribution)
  if !distribution.is_a?(Dist)
    raise MTRubyError, "#{distribution} is not a distribution."
  end

  @template.defineGroup name, distribution.java_object
  TemplateBuilder.define_addressing_mode_group name
end
define_op_group(name, distribution) click to toggle source
# File template.rb, line 433
def define_op_group(name, distribution)
  if !distribution.is_a?(Dist)
    raise MTRubyError, "#{distribution} is not a distribution."
  end

  @template.defineGroup name, distribution.java_object
  TemplateBuilder.define_operation_group name
end
dist(*ranges) click to toggle source

Creates an object describing the probability distribution for random generation (biased generation). Methods arguments specify ranges of values with corresponding biases.

# File template.rb, line 290
def dist(*ranges)
  if !ranges.is_a?(Array)
    raise MTRubyError, "#{ranges} is not an Array."
  end

  builder = @template.newVariateBuilder
  ranges.each do |range_item|
    if !range_item.is_a?(ValueRange)
      raise MTRubyError, "#{range_item} is not a ValueRange."
    end

    value = range_item.value
    bias = range_item.bias

    if value.is_a?(Range)
      if bias.nil? then
        builder.addInterval value.min, value.max
      else
        builder.addInterval value.min, value.max, bias
      end
    elsif value.is_a?(Array)
      if bias.nil? then
        builder.addCollection value
      else
        builder.addCollection value, bias
      end
    elsif value.is_a?(Dist)
      if bias.nil? then
        builder.addVariate value.java_object
      else
        builder.addVariate value.java_object, bias
      end
    else
      if bias.nil? then
        builder.addValue value
      else
        builder.addValue value, bias
      end
    end
  end

  Dist.new builder.build
end
end_comment() click to toggle source

Ends a multi-line comment (uses ml_comment_ends_with)

# File template.rb, line 496
def end_comment
  print_format :COMMENT_ML_END, ''
  @is_multiline_comment = false
end
entry(*args) click to toggle source
# File template.rb, line 692
def entry(*args)
  if args.count == 2
    @template.newEntryReference args.at(0), args.at(1)
  else
    unless defined? @entry_reference
      @entry_reference = BufferEntryReference.new @template
    end

    @entry_reference
  end
end
epilogue(&contents) click to toggle source
# File template.rb, line 814
def epilogue(&contents)
  @template.beginEpilogue
  self.instance_eval &contents
  @template.endEpilogue
end
exception_handler(attrs = {}, &contents) click to toggle source

————————————————————————– # Exception Handling # ————————————————————————– #

# File template.rb, line 719
def exception_handler(attrs = {}, &contents)
  builder = @template.beginExceptionHandler

  exception_handler_object = ExceptionHandler.new self, builder
  exception_handler_object.instance_eval &contents

  @template.endExceptionHandler
end
free_all_allocated_modes(mode) click to toggle source
# File template.rb, line 416
def free_all_allocated_modes(mode)
  @template.freeAllocatedMode mode, true
end
free_allocated_mode(mode) click to toggle source
# File template.rb, line 412
def free_allocated_mode(mode)
  @template.freeAllocatedMode mode, false
end
generate() click to toggle source

————————————————————————– # Generation (Execution and Printing) # ————————————————————————– #

# File template.rb, line 899
def generate
  java_import Java::Ru.ispras.microtesk.test.TestEngine
  engine = TestEngine.getInstance()

  @template.beginPreSection
  pre
  @template.endPreSection

  @template.beginPostSection
  post
  @template.endPostSection

  @template.beginMainSection
  run
  @template.endMainSection

  engine.process @template
end
generate_data(address, label, type, length, method, *flags) click to toggle source

————————————————————————– # Generating Data Files # ————————————————————————– #

# File template.rb, line 708
def generate_data(address, label, type, length, method, *flags)
  # puts "Generating data file"
  separate_file = if flags.empty? then true else flags[0] end
  @template.getDataManager.generateData(
    address, label, type, length, method, separate_file)
end
get_address_of(label) click to toggle source
# File template.rb, line 206
def get_address_of(label)
  @template.getAddressForLabel label.to_s
end
get_caller_location(caller_index = 1) click to toggle source
# File template.rb, line 72
def get_caller_location(caller_index = 1)
  # Parses the caller of this method's caller, so the default index is 1
  caller_info = Template.parse_caller(caller[caller_index])
  @template.where File.basename(caller_info[0]), caller_info[1]
end
get_option_value(name) click to toggle source
# File template.rb, line 924
def get_option_value(name)
  java_import Java::Ru.ispras.microtesk.test.TestEngine
  engine = TestEngine.getInstance
  engine.getOptionValue name
end
index_source() click to toggle source
# File template.rb, line 648
def index_source
  @template.getIndexSource
end
iterate(attributes = {}, &contents) click to toggle source
# File template.rb, line 178
def iterate(attributes = {}, &contents)
  blockBuilder = @template.beginBlock
  blockBuilder.setWhere get_caller_location

  blockBuilder.setAtomic false
  blockBuilder.setSequence false
  blockBuilder.setIterate true

  if attributes.has_key? :obfuscator
    blockBuilder.setObfuscator(attributes[:obfuscator])
  end

  if attributes.has_key? :rearranger
    blockBuilder.setRearranger(attributes[:rearranger])
  end

  attributes.each_pair do |key, value|
    blockBuilder.setAttribute(key.to_s, value)
  end

  self.instance_eval &contents
  @template.endBlock
end
label(name) click to toggle source
# File template.rb, line 202
def label(name)
  @template.addLabel name
end
location(name, index) click to toggle source

Creates a location-based format argument for format-like output methods.

# File template.rb, line 449
def location(name, index)
  Location.new name, index
end
memory_object(attrs) click to toggle source

————————————————————————– # Memory Objects # ————————————————————————– #

# File template.rb, line 824
def memory_object(attrs)
  size = get_attribute attrs, :size
  builder = @template.newMemoryObjectBuilder size

  va = get_attribute attrs, :va
  is_va_label = false

  if va.is_a?(Integer)
    builder.setVa va
  elsif va.is_a?(Range)
    builder.setVa va.min, va.max
  elsif va.is_a?(String) or va.is_a?(Symbol) 
    builder.setVa va.to_s
    is_va_label = true
  else
    raise MTRubyError, "The 'va' attribute has unsupported type #{va.class}."
  end

  if !is_va_label
    pa = get_attribute attrs, :pa
    if pa.is_a?(Integer)
      builder.setPa pa
    elsif pa.is_a?(Range)
      builder.setPa pa.min, pa.max
    elsif pa.is_a?(String) or pa.is_a?(Symbol) 
      builder.setPa pa.to_s
    else
      raise MTRubyError, "The 'pa' attribute has unsupported type #{pa.class}."
    end
  end

  if attrs.has_key?(:name) 
    builder.setName attrs[:name].to_s
  end

  if attrs.has_key?(:mode) 
    builder.setMode attrs[:mode].to_s
  end

  if attrs.has_key?(:data) 
    builder.setData attrs[:data]
  end

  builder.build
end
method_missing(meth, *args, &block) click to toggle source

Hack to allow limited use of capslocked characters

Calls superclass method
# File template.rb, line 64
def method_missing(meth, *args, &block)
  if self.respond_to?(meth.to_s.downcase)
    self.send meth.to_s.downcase.to_sym, *args, &block
  else
    super
  end
end
mode_allocator(name, attrs = {}) click to toggle source

— Special “no value” method — Similar to the above method, but the described object is more complex than an immediate value (most likely, it will be some MODE or OP). TODO: Not implemented. Left as a requirement. Should be implemented in the future.

def __(aug_value = nil)

NoValue.new(aug_value)

end

# File template.rb, line 402
def mode_allocator(name, attrs = {})
  builder = @template.newAllocatorBuilder name

  attrs.each_pair do |key, value|
    builder.setAttribute key.to_s, value.to_s
  end

  builder.build
end
newline() click to toggle source

Adds the new line character into the test program

# File template.rb, line 463
def newline
  text ''
end
org(origin) click to toggle source

————————————————————————– # Code Allocation Facilities # ————————————————————————– #

# File template.rb, line 777
def org(origin)
  if origin.is_a?(Integer)
    @template.setOrigin origin
  elsif origin.is_a?(Hash)
    delta = get_attribute origin, :delta
    if !delta.is_a?(Integer)
      raise MTRubyError, "delta (#{delta}) must be an Integer."
    end
    @template.setRelativeOrigin delta
  else
    raise MTRubyError, "origin (#{origin}) must be an Integer or a Hash."
  end
end
page_table(attrs = {}, &contents) click to toggle source
# File template.rb, line 870
def page_table(attrs = {}, &contents)
  if nil == @data_manager
    raise MTRubyError, "Data configuration is not defined"
  end

  if attrs.has_key?(:global)
    global = attrs[:global]
  else
    global = false
  end

  if attrs.has_key?(:separate_file)
    separate_file = attrs[:separate_file]
  else
    separate_file = false
  end

  @data_manager.beginData global, separate_file

  page_table = PageTable.new self, @data_manager
  page_table.instance_eval &contents

  @data_manager.endData
end
post() click to toggle source

Post-condition instructions template

# File template.rb, line 93
def post

end
pre() click to toggle source

Pre-condition instructions template

# File template.rb, line 83
def pre

end
preparator(attrs, &contents) click to toggle source

————————————————————————– # Creating Preparators and Comparators # ————————————————————————– #

# File template.rb, line 536
def preparator(attrs, &contents)
  create_preparator(false, attrs, &contents)
end
prepare(target_mode, value_object, attrs = {}) click to toggle source
# File template.rb, line 614
def prepare(target_mode, value_object, attrs = {})
  preparator_name = attrs[:name]
  if !preparator_name.nil?
    preparator_name = preparator_name.to_s
  end

  variant_name = attrs[:variant]
  if !variant_name.nil?
    variant_name = variant_name.to_s
  end

  @template.addPreparatorCall target_mode, value_object, preparator_name, variant_name
end
print_format(kind, format, *args) click to toggle source

Prints a format-based output to the simulator log or to the test program depending of the is_runtime flag.

prologue(&contents) click to toggle source

————————————————————————– # Test Case Level Prologue and Epilogue # ————————————————————————– #

# File template.rb, line 808
def prologue(&contents)
  @template.beginPrologue
  self.instance_eval &contents
  @template.endPrologue
end
pseudo(text) click to toggle source

Creates a pseudo instruction call that prints user-specified text.

# File template.rb, line 527
def pseudo(text)
  @template.setCallText text
  @template.endBuildingCall
end
rand(*args) click to toggle source

Creates an object for generating a random integer (to be used as an argument of a mode or op) selected from the specified range or according to the specified distribution.

# File template.rb, line 247
def rand(*args)
  if args.count == 1
    distribution = args.at(0)

    if !distribution.is_a?(Dist)
      raise MTRubyError, "the argument must be a distribution."
    end

    @template.newRandom distribution.java_object
  elsif args.count == 2
    from = args.at(0)
    to = args.at(1)

    if !from.is_a?(Integer) or !to.is_a?(Integer)
      raise MTRubyError, "the arguments must be integers."
    end

    @template.newRandom from, to
  else
    raise MTRubyError, "Wrong argument count: #{args.count}. Must be 1 or 2."
  end
end
random_situation(dist) click to toggle source
# File template.rb, line 224
def random_situation(dist)
  dist.java_object
end
range(attrs = {}) click to toggle source

Creates an object describing a value range (with corresponding bias) used in random generation. If the bias attribute is not specified, it will be set to nil, which means the default bias.

# File template.rb, line 350
def range(attrs = {})
  if !attrs.is_a?(Hash)
    raise MTRubyError, "#{attrs} is not a Hash."
  end

  if !attrs.has_key?(:value)
    raise MTRubyError, "The :value attribute is not specified in #{attrs}."
  end
  value = attrs[:value]

  bias = nil
  if attrs.has_key?(:bias)
    bias = attrs[:bias]
    if !bias.is_a?(Integer)
      raise MTRubyError, "#{bias} is not an Integer."
    end
  end

  ValueRange.new value, bias
end
run() click to toggle source

Main instructions template

# File template.rb, line 88
def run
  puts "MTRuby: warning: Trying to execute the original Template#run."
end
sequence(attributes = {}, &contents) click to toggle source
# File template.rb, line 138
def sequence(attributes = {}, &contents)
  blockBuilder = @template.beginBlock
  blockBuilder.setWhere get_caller_location

  blockBuilder.setAtomic false
  blockBuilder.setSequence true
  blockBuilder.setIterate false

  if attributes.has_key? :obfuscator
    blockBuilder.setObfuscator(attributes[:obfuscator])
  end

  attributes.each_pair do |key, value|
    blockBuilder.setAttribute(key.to_s, value)
  end

  self.instance_eval &contents
  @template.endBlock
end
set_default_situation(names, &situations) click to toggle source
# File template.rb, line 228
def set_default_situation(names, &situations)
  if !names.is_a?(String) and !names.is_a?(Array)
    raise MTRubyError, "#{names} must be String or Array."
  end

  default_situation = self.instance_eval &situations
  if names.is_a?(Array)
    names.each do |name|
      @template.setDefaultSituation name, default_situation
    end
  else
    @template.setDefaultSituation names, default_situation
  end
end
set_option_value(name, value) click to toggle source
# File template.rb, line 918
def set_option_value(name, value)
  java_import Java::Ru.ispras.microtesk.test.TestEngine
  engine = TestEngine.getInstance
  engine.setOptionValue name, value
end
situation(name, attrs = {}) click to toggle source
# File template.rb, line 210
def situation(name, attrs = {})
  if !attrs.is_a?(Hash)
    raise MTRubyError, "attrs (#{attrs}) must be a Hash."
  end

  builder = @template.newSituation name
  attrs.each_pair do |name, value|
    attr_value = if value.is_a?(Dist) then value.java_object else value end
    builder.setAttribute name.to_s, attr_value
  end

  builder.build
end
start_comment() click to toggle source

Starts a multi-line comment (uses sl_comment_starts_with)

# File template.rb, line 488
def start_comment
  @is_multiline_comment = true
  print_format :COMMENT_ML_START, ''
end
start_label() click to toggle source
# File template.rb, line 652
def start_label
  @template.getStartLabel
end
stream(label, data, index, length) click to toggle source

————————————————————————– # Creating Streams # ————————————————————————– #

# File template.rb, line 660
def stream(label, data, index, length)
  @template.addStream label.to_s, data, index, length
end
stream_preparator(attrs, &contents) click to toggle source

————————————————————————– # Creating Stream Preparators # ————————————————————————– #

# File template.rb, line 632
def stream_preparator(attrs, &contents)
  data  = get_attribute attrs, :data_source
  index = get_attribute attrs, :index_source

  @template.beginStreamPreparator data.to_s, index.to_s

  data_stream_object = StreamPreparator.new self, @template
  data_stream_object.instance_eval &contents

  @template.endStreamPreparator
end
target() click to toggle source
# File template.rb, line 598
def target
  @template.getPreparatorTarget
end
text(format, *args) click to toggle source

Adds text into the test program.

# File template.rb, line 470
def text(format, *args)
  if @is_multiline_comment
    print_format :COMMENT_ML_BODY, format, *args
  else
    print_format :TEXT, format, *args
  end
end
trace(format, *args) click to toggle source

Prints text into the simulator execution log.

# File template.rb, line 456
def trace(format, *args)
  print_format :TRACE, format, *args
end
value(*args) click to toggle source
# File template.rb, line 602
def value(*args)
  if args.count != 0 and args.count != 2
    raise MTRubyError, "Wrong argument count: #{args.count}. Must be 0 or 2."
  end

  if args.count == 2
    @template.newLazy args.at(0), args.at(1)
  else
    @template.newLazy
  end
end
variant(attrs = {}, &contents) click to toggle source
# File template.rb, line 589
def variant(attrs = {}, &contents)
  name = attrs[:name]
  bias = attrs[:bias]

  @template.beginPreparatorVariant name, bias
  self.instance_eval &contents
  @template.endPreparatorVariant
end