Class: MicroCisc::Compile::Instruction
- Inherits:
-
Object
- Object
- MicroCisc::Compile::Instruction
- Defined in:
- lib/micro_cisc/compile/instruction.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dest ⇒ Object
readonly
Returns the value of attribute dest.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#imm ⇒ Object
readonly
Returns the value of attribute imm.
-
#instruction ⇒ Object
readonly
Returns the value of attribute instruction.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#minimal ⇒ Object
readonly
Returns the value of attribute minimal.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#reg ⇒ Object
readonly
Returns the value of attribute reg.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
Instance Method Summary collapse
- #comment? ⇒ Boolean
- #data? ⇒ Boolean
- #encoded(label_dictionary = nil, current_address = nil) ⇒ Object
-
#initialize(label_generator, minimal, original, sugar) ⇒ Instruction
constructor
A new instance of Instruction.
- #instruction? ⇒ Boolean
- #label? ⇒ Boolean
- #parse(operation) ⇒ Object
- #parse_component(component) ⇒ Object
- #parse_ucisc(minimal_instruction) ⇒ Object
- #validate_alu(component, current) ⇒ Object
- #validate_args(first_arg, second_arg) ⇒ Object
- #validate_boolean(component, current) ⇒ Object
- #validate_dest(arg) ⇒ Object
- #validate_effect(component, current) ⇒ Object
- #validate_immediate(value, index) ⇒ Object
- #validate_reg(arg) ⇒ Object
Constructor Details
#initialize(label_generator, minimal, original, sugar) ⇒ Instruction
Returns a new instance of Instruction.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/micro_cisc/compile/instruction.rb', line 6 def initialize(label_generator, minimal, original, sugar) @label_generator = label_generator @original = original @label = nil @operation = nil @sign = nil @dir = nil @imm = nil @src = nil @dest = nil @sugar = sugar @data = nil @inc = nil @eff = nil @alu_code = nil parse_ucisc(minimal) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def data @data end |
#dest ⇒ Object (readonly)
Returns the value of attribute dest.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def dest @dest end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def dir @dir end |
#imm ⇒ Object (readonly)
Returns the value of attribute imm.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def imm @imm end |
#instruction ⇒ Object (readonly)
Returns the value of attribute instruction.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def instruction @instruction end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def label @label end |
#minimal ⇒ Object (readonly)
Returns the value of attribute minimal.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def minimal @minimal end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def original @original end |
#reg ⇒ Object (readonly)
Returns the value of attribute reg.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def reg @reg end |
#sign ⇒ Object (readonly)
Returns the value of attribute sign.
4 5 6 |
# File 'lib/micro_cisc/compile/instruction.rb', line 4 def sign @sign end |
Instance Method Details
#comment? ⇒ Boolean
36 37 38 |
# File 'lib/micro_cisc/compile/instruction.rb', line 36 def comment? !label? && !instruction? end |
#data? ⇒ Boolean
24 25 26 |
# File 'lib/micro_cisc/compile/instruction.rb', line 24 def data? !@data.nil? end |
#encoded(label_dictionary = nil, current_address = nil) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/micro_cisc/compile/instruction.rb', line 226 def encoded(label_dictionary = nil, current_address = nil) imms = @immediates.map do |imm| if imm.is_a?(Numeric) imm elsif label_dictionary.nil? 0 elsif imm.is_a?(Array) raise ArgumentError, 'Current address is missing' if current_address.nil? label_address = label_dictionary[imm.first] raise ArgumentError, "Missing label '#{imm.first}'" if label_address.nil? label_address = label_address & 0xFFFF if imm.last == 'disp' label_address - current_address elsif imm.last == 'imm' label_address & 0xFFFF else raise ArgumentError, "Invalid immediate spec: #{imm.first}.#{imm.last}" end else raise ArgumentError, "Invalid immediate spec: #{imm.first}.#{imm.last}" end end op_code = @operation == 'alu' ? 1 : 0 msb = (op_code << 7) | (@eff << 5) | (@dest << 2) | (@src >> 1) lsb = ((@src & 0x01) << 7) if @uses_mem_arg lsb = lsb | (@inc << 6) end imm_mask = ~(-1 << @bit_width) if @operation == 'alu' lsb = lsb | ((imms.first & imm_mask) << 4) | (@alu_code & 0xF) else imm = if @source_is_mem && @dest_is_mem ((imms.first & 0x7) << 3) | (imms.last & 0x7) else imms.first end lsb = lsb | (imm & imm_mask) end ((msb & 0xFF) << 8) | (lsb & 0xFF) end |
#instruction? ⇒ Boolean
32 33 34 |
# File 'lib/micro_cisc/compile/instruction.rb', line 32 def instruction? !@operation.nil? end |
#label? ⇒ Boolean
28 29 30 |
# File 'lib/micro_cisc/compile/instruction.rb', line 28 def label? !@label.nil? end |
#parse(operation) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/micro_cisc/compile/instruction.rb', line 126 def parse(operation) @operation = operation components = @components[1..-1] args = [] @immediates = [] @uses_mem_arg = false @source_is_mem = false @dest_is_mem = false while components.size > 0 to_parse = components.shift if(to_parse.start_with?('$') || to_parse.start_with?('&')) raise ArgumentError, "Missing ref #{to_parse}" unless @sugar[to_parse] to_parse = @sugar[to_parse] end parsed = parse_component(to_parse) if ['val', 'reg', 'mem'].include?(parsed.last) @uses_mem_arg = true if parsed.last == 'mem' @source_is_mem = true if args.empty? && parsed.last == 'mem' @dest_is_mem = true if !args.empty? && parsed.last == 'mem' args << parsed @immediates << 0 elsif parsed.last == 'op' @alu_code = validate_alu(parsed, @alu_code) elsif parsed.last == 'push' @inc = validate_boolean(parsed, @sign) elsif parsed.last == 'pop' @inc = validate_boolean(parsed, @sign) elsif parsed.last == 'eff' @eff = validate_effect(parsed, @eff) elsif (parsed.last == 'disp' || parsed.last == 'imm') if args.empty? # if immediate is first arg, this is a 4.val source args << [4, 'val'] @immediates << 0 end imm = if parsed.first.is_a?(Numeric) parsed.first else if parsed.first == 'break' [@label_generator.end_label, parsed.last] elsif parsed.first == 'loop' [@label_generator.start_label, parsed.last] else parsed end end @immediates[args.size - 1] = imm else raise ArgumentError, "Invalid argument for #{@operation}: #{to_parse}" end end if args.size != 2 raise ArgumentError, "Missing source and/or destination arguments" end @eff ||= 3 if @inc && !@uses_mem_arg raise ArgumentError, "Memory argument required to use push and pop" end if @operation == 'alu' && !@alu_code raise ArgumentError, "Compute instruction must have ALU op code" end @inc ||= 0 @bit_width = 7 @bit_width -= 4 if @operation == 'alu' @bit_width -= 1 if @uses_mem_arg @immediates.each_with_index do |imm, index| if imm.is_a?(Numeric) validate_immediate(imm, index) end end if @immediates.last != 0 && (!@source_is_mem || !@dest_is_mem) raise ArgumentError, "Destination immediate is only allowed when both arguments are mem args" end @src, @dest, @dir = validate_args(args.first, args.last) nil end |
#parse_component(component) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/micro_cisc/compile/instruction.rb', line 86 def parse_component(component) parts = component.split('.') if /^-{0,1}[0-9A-Fa-f]+$/.match(parts.first) [parts.first.to_i, parts.last.downcase] elsif /^-{0,1}(0x){0,1}[0-9A-Fa-f]+$/.match(parts.first) [parts.first.to_i(16), parts.last.downcase] else [parts.first, parts.last.downcase] end end |
#parse_ucisc(minimal_instruction) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/micro_cisc/compile/instruction.rb', line 40 def parse_ucisc(minimal_instruction) @components = minimal_instruction.split(/\s/) return if @components.empty? first = @components.first if first == '{' @label_generator.push_context @label = @label_generator.start_label elsif first == '}' @label = @label_generator.end_label @label_generator.pop_context else label = /(?<name>[^\s]+):/.match(first) @label = label['name'] if label end return if @label if @components.first == '%' @components.shift @data = @components.map do |component| if match = /(?<name>[^\s]+)\.(?<type>imm|disp)/.match(component) [match['name'], match['type']] else if component.length % 4 != 0 raise ArgumentError, "Data segment length must be a multiple of 2-bytes" end words = [] (0...(component.length / 4)).each do |index| words << ((component[index * 4, 4]).to_i(16) & 0xFFFF) end words.pack("S*") end end return end @opcode = parse_component(@components.first).first case @opcode when 'copy' parse('copy') when 'compute' parse('alu') end end |
#validate_alu(component, current) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/micro_cisc/compile/instruction.rb', line 97 def validate_alu(component, current) raise ArgumentError, "Duplicate #{component.last} value" if current code = component.first unless code >= 0 && code < 16 raise ArgumentError, "Value of #{component.last} must be between 0x0 and 0xF instead of #{component.first.to_s(16).upcase}" end component.first end |
#validate_args(first_arg, second_arg) ⇒ Object
273 274 275 276 277 278 |
# File 'lib/micro_cisc/compile/instruction.rb', line 273 def validate_args(first_arg, second_arg) register = validate_reg(first_arg) dest = validate_dest(second_arg) [register, dest, dir] end |
#validate_boolean(component, current) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/micro_cisc/compile/instruction.rb', line 114 def validate_boolean(component, current) raise ArgumentError, "Duplicate #{component.last} value" if current if component.first == component.last # Was used with syntax sugar without the numeric argument component[0] = 1 end unless (0..1).include?(component.first) raise ArgumentError, "Value of #{component.last} must be 0x0 or 0x1 instead of 0x#{component.first.to_s(16).upcase}" end component.first end |
#validate_dest(arg) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/micro_cisc/compile/instruction.rb', line 295 def validate_dest(arg) valid = arg.last == 'mem' && [1, 2, 3].include?(arg.first) valid = valid || (arg.last == 'reg' && [0, 4, 1, 2, 3].include?(arg.first)) if valid reg = arg.first reg += 4 if [1, 2, 3].include?(arg.first) && arg.last == 'reg' reg else raise ArgumentError, "Invalid destination: #{arg.first.to_s}.#{arg.last}" end end |
#validate_effect(component, current) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/micro_cisc/compile/instruction.rb', line 106 def validate_effect(component, current) raise ArgumentError, "Duplicate #{component.last} value" if current unless (0..7).include?(component.first) raise ArgumentError, "Value of #{component.last} must be between 0x0 and 0x7 instead of 0x#{component.first.to_s(16).upcase}" end component.first end |
#validate_immediate(value, index) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/micro_cisc/compile/instruction.rb', line 209 def validate_immediate(value, index) width = @bit_width width = width / 2 if @source_is_mem && @dest_is_mem if index == 0 && @source_is_mem || index == 1 && @dest_is_mem min = 0 max = (2 << @bit_width) - 1 else magnitude = 1 << (@bit_width - 1) min = magnitude * -1 max = magnitude - 1 end if (value < min || value > max) signed = @source_is_mem ? 'unsigned' : 'signed' raise ArgumentError, "Immediate max bits is #{@bit_width} #{signed}; value must be between #{min} and #{max} instead of #{value}" end end |
#validate_reg(arg) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/micro_cisc/compile/instruction.rb', line 280 def validate_reg(arg) valid = arg.first == 0 && arg.last == 'reg' valid = valid || ([1, 2, 3].include?(arg.first) && arg.last == 'mem') valid = valid || (arg.first == 4 && arg.last == 'val') valid = valid || ([1, 2, 3].include?(arg.first) && arg.last == 'reg') if valid reg = arg.first reg += 4 if [1, 2, 3].include?(arg.first) && 'reg' == arg.last reg else raise ArgumentError, "Invalid register: #{arg.first.to_s}.#{arg.last}" end end |