class Bones::Variable
This class represents individual variables. Variables have a name, a type, a direction and an id. They might also have an algorithmic species coupled if they represent an array. The variable type is in AST form and holds information which can be extracted through methods implemented for the Type class.
The provided methods are able to obtain information from the variables, such as the number of dimensions and the definition. Other methods query variables for properties, such as whether a variable is an array or not. Several methods should only be executed if the variable is an array - this is not checked within the methods itself.
Attributes
Public Class Methods
Method to initilize the variable class with a name, type and a direction.
# File lib/bones/variable.rb 21 def initialize(name,type,size,direction,id,shared) 22 @name = name 23 @type = type 24 @size = size 25 @direction = direction 26 @id = id 27 @shared = shared 28 @guess = false 29 @species = nil 30 end
Public Instance Methods
Method to obtain the full defintion of a variable. This includes the variable type, the variable name, and the dimensions and/or pointers. Example return values are:
int example[10] char **variable unsigned int* example[N] float array[][]
# File lib/bones/variable.rb 90 def definition 91 definition_prefix + ' ' + @name + definition_suffix 92 end
Method to return the device version of a variable’s definition. This includes the variable’s type, zero or more stars and the variable’s name, all returned as a single string.
# File lib/bones/variable.rb 79 def device_definition 80 type_name + device_pointer + ' ' + @name 81 end
This method returns the device name of the variable.
# File lib/bones/variable.rb 33 def device_name 34 DEVICE+@name 35 end
This method returns a star (‘*’) if the variable is an array or an empty string if it is not. It will not be able to return more than a single star, as device varia- bles are assumed to be flattened.
# File lib/bones/variable.rb 98 def device_pointer 99 (@type.array_or_pointer?) ? '*' : '' 100 end
Method to obtain the number of dimensions of a variable. This method returns a positive integer. The functionality is implemented as a recursive search in the Type class.
# File lib/bones/variable.rb 71 def dimensions 72 @type.dimensions 73 end
This method detects whether the variable is dynamically allocated or not. It returns either true or false.
# File lib/bones/variable.rb 119 def dynamic? 120 (definition_prefix.count('*') > 0) 121 end
Method to return the full flattened address.
# File lib/bones/variable.rb 136 def flatindex 137 indices = [] 138 @species.dimensions.each_with_index do |dimension,num_dimension| 139 index_reverse = !(@species.reverse?) ? num_dimension : @species.dimensions.length-num_dimension-1 # FIXME: REVERSED 140 if (from(dimension) != to(dimension)) 141 data = "#{GLOBAL_ID}_#{index_reverse}" 142 else 143 data = from(dimension) 144 end 145 indices.push(data + @factors[index_reverse]) 146 end 147 return indices.join(' + ') 148 end
Method to flatten an array into a one dimensional array. If an array has multiple dimensions, the method will return a ‘[0]’ for each additional dimension. This method assumes that the variable is an array.
# File lib/bones/variable.rb 106 def flatten 107 ''+'[0]'*(dimensions-1) 108 end
This method returns the ‘golden’ name of a variable. This is used to generate verification code.
# File lib/bones/variable.rb 39 def golden_name 40 GOLDEN + '_' + @name + '_' + @id 41 end
This method returns the initialization code for a varia- ble, formatted as a string. This is used to generate the verification code.
# File lib/bones/variable.rb 113 def initialization 114 (dynamic?) ? " = (#{definition_prefix})malloc(#{@size.join('*')}*sizeof(#{type_name}));"+NL : ';'+NL 115 end
Method to find out whether a variable is used as an in- put. If the current algorithm uses the ‘shared’ pattern, then the variable must be input only, otherwise it can be both input or inout to be considered input. The return value is boolean.
# File lib/bones/variable.rb 48 def input? 49 return (@shared) ? (@direction == INPUT) : (@direction == INPUT || @direction == INOUT) 50 end
Method to find out whether a variable is used as an out- put. The variable can be both output or inout to be con- sidered input. The return value is boolean.
# File lib/bones/variable.rb 55 def output? 56 return (@direction == OUTPUT || @direction == INOUT) 57 end
Method to return an array of multiplication factors for multi-dimensional arrays that need to be flattened. For every dimension, one factor is generated.
# File lib/bones/variable.rb 126 def set_factors 127 raise_error("Species dimensions (#{@species.dimensions.length}) and array dimensions (#{@size.length}) mismatch for array '#{@name}'") if @species.dimensions.length != @size.length 128 sizes, @factors = [], [] 129 @species.dimensions.each_with_index do |dimension,num_dimension| 130 (sizes.empty?) ? @factors.push('') : @factors.push('*'+sizes.join('*')) 131 sizes.push(simplify(@size[dimensions-num_dimension-1])) 132 end 133 end
Method to obtain the type of a variable, omitting any information about arrays or pointers. Examples of out- puts are: int
, float
or char
. This functionality is implemented as a recursive search in the Type class, since the type is in AST form.
# File lib/bones/variable.rb 64 def type_name 65 @type.type_name.to_s 66 end
Private Instance Methods
Method to obtain the prefix of a variable’s definition, formatted as a string. The string contains the variable’s type and zero or more stars (‘*’).
# File lib/bones/variable.rb 156 def definition_prefix 157 @type.to_s.partition('[')[0].strip 158 end
Method to obtain the suffix of a variable’s definition, formatted as a string. The string is either empty or contains one or more brackets (e.g. [N] or [10]).
# File lib/bones/variable.rb 163 def definition_suffix 164 @type.to_s.partition('[')[1]+@type.to_s.partition('[')[2] 165 end