Method: MicroCisc::Compile::Instruction#parse_ucisc

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

#parse_ucisc(minimal_instruction) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/micro_cisc/compile/instruction.rb', line 41

def parse_ucisc(minimal_instruction)
  @components = minimal_instruction.scan(COMPONENT_WITH_QUOTES_REGEX).flatten

  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
    @components = @components.map do |component|
      if component =~ /"(\\"|[^"])*"/
        # Remove surrounding quotes
        component = component[1...(component.length - 1)]
        component = component.gsub("\\n","\n")
        component = component.gsub("\\\"","\"")
        hex = []
        offset = 0
        while(offset < component.length)
          pair = component[offset...(offset + 2)]
          pair = pair.bytes
          pair << 0 if pair.length < 2
          word = pair.pack("C*").unpack("S>").last
          hex_word = "%04X" % word
          hex << hex_word
          offset += 2
        end
        component = ["%04X" % component.length] + hex
      else
        component
      end
    end.flatten
    @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