Method: MicroCisc::Compile::Instruction#parse

Defined in:
lib/micro_cisc/compile/instruction.rb

#parse(operation) ⇒ Object



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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/micro_cisc/compile/instruction.rb', line 149

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 @statement.get_var(to_parse)
      to_parse = @statement.get_var(to_parse)
      components = to_parse.split(/\s/) + components
      to_parse = components.shift
    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 && @operation == 'alu'
    raise ArgumentError, "Destination immediate is not allowed for compute instructions"
  elsif @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