Class: Haxor::Compiler::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/haxor/compiler/core.rb

Constant Summary collapse

REG_ALIASES =
{
  '$sp'  => Vm::Cpu::Core::REG_STACK,
  '$ret' => Vm::Cpu::Core::REG_RETURN,
  '$sc'  => Vm::Cpu::Core::REG_SYSCALL
}

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Core

Returns a new instance of Core.



10
11
12
13
14
15
16
17
# File 'lib/haxor/compiler/core.rb', line 10

def initialize
  @units = []
  @cmds = []
  @autolabel = 0
  @prev_sections = []

  init_cmds
end

Instance Method Details

#add(token) ⇒ Object



270
271
272
# File 'lib/haxor/compiler/core.rb', line 270

def add(token)
  @unit.add token
end

#add_cmd(info, args = []) ⇒ Object

def resolve_arg(a, type) case type when :reg return a.to_i when :imm return a.to_i else fail end end



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/haxor/compiler/core.rb', line 285

def add_cmd(info, args = [])
  registers = []
  immediate = 0
  info[:args].each_with_index do |type, index|
    if type == :reg
      if REG_ALIASES.key? args[index]
        registers << REG_ALIASES[args[index]]
      else
        registers << args[index][1..-1].to_i
      end
    elsif type == :imm
      begin
        immediate = parse_number args[index]
      rescue
        immediate = args[index]
      end
    end
  end

  registers.fill 0, registers.size...3

  token      = Token::Cmd.new
  token.cmd  = info[:opcode]
  token.reg1 = registers[0]
  token.reg2 = registers[1]
  token.reg3 = registers[2]
  token.imm  = immediate
  token.opts = info[:opts]

  add token
end

#bind_cmd(cmd, args, opcode, opts = []) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/haxor/compiler/core.rb', line 77

def bind_cmd(cmd, args, opcode, opts = [])
  @cmds << {
    cmd: cmd,
    args: args,
    opcode: opcode,
    opts: opts
  }
end

#cmd_b(*args) ⇒ Object



161
162
163
# File 'lib/haxor/compiler/core.rb', line 161

def cmd_b(*args)
  process_cmd 'beq', ['$0', '$0', args[0]]
end

#cmd_bal(*args) ⇒ Object



165
166
167
# File 'lib/haxor/compiler/core.rb', line 165

def cmd_bal(*args)
  process_cmd 'beql', ['$0', '$0', args[0]]
end

#cmd_beqz(*args) ⇒ Object



202
203
204
# File 'lib/haxor/compiler/core.rb', line 202

def cmd_beqz(*args)
  process_cmd 'beq', [args[0], '$0', args[1]]
end

#cmd_bge(*args) ⇒ Object

branch is greater or equal



182
183
184
185
# File 'lib/haxor/compiler/core.rb', line 182

def cmd_bge(*args)
  process_cmd 'slt', ['$1', args[0], args[1]]
  process_cmd 'beq', ['$1', '$0', args[2]]
end

#cmd_bgt(*args) ⇒ Object

branch is greater than



170
171
172
173
# File 'lib/haxor/compiler/core.rb', line 170

def cmd_bgt(*args)
  process_cmd 'slt', ['$1', args[1], args[0]]
  process_cmd 'bne', ['$1', '$0', args[2]]
end

#cmd_bgtz(*args) ⇒ Object



197
198
199
200
# File 'lib/haxor/compiler/core.rb', line 197

def cmd_bgtz(*args)
  process_cmd 'slt', ['$1', '$0', args[0]]
  process_cmd 'bne', ['$1', '$0', args[1]]
end

#cmd_ble(*args) ⇒ Object



187
188
189
190
# File 'lib/haxor/compiler/core.rb', line 187

def cmd_ble(*args)
  process_cmd 'slt', ['$1', args[1], args[0]]
  process_cmd 'beq', ['$1', '$0', args[2]]
end

#cmd_blez(*args) ⇒ Object



192
193
194
195
# File 'lib/haxor/compiler/core.rb', line 192

def cmd_blez(*args)
  process_cmd 'slt', ['$1', '$0', args[0]]
  process_cmd 'beq', ['$1', '$0', args[1]]
end

#cmd_blt(*args) ⇒ Object

branch if less than



176
177
178
179
# File 'lib/haxor/compiler/core.rb', line 176

def cmd_blt(*args)
  process_cmd 'slt', ['$1', args[0], args[1]]
  process_cmd 'bne', ['$1', '$0', args[2]]
end

#cmd_clear(*args) ⇒ Object



149
150
151
# File 'lib/haxor/compiler/core.rb', line 149

def cmd_clear(*args)
  process_cmd 'add', [args[0], '$0', '$0']
end

#cmd_dw(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/haxor/compiler/core.rb', line 90

def cmd_dw(*args)
  add Token::Label.new(args[0])

  if args.size == 1
    add Token::Int64.new(0)
  else
    (1...args.size).each do |i|
      begin
        add Token::Int64.new(Integer(args[i]))
      rescue
        args[i][1...-1].each_char do |c|
          add Token::Int64.new(c.ord)
        end
      end
    end
  end
end

#cmd_move(*args) ⇒ Object



145
146
147
# File 'lib/haxor/compiler/core.rb', line 145

def cmd_move(*args)
  process_cmd 'add', [args[0], args[1], '$0']
end

#cmd_not(*args) ⇒ Object



153
154
155
# File 'lib/haxor/compiler/core.rb', line 153

def cmd_not(*args)
  process_cmd 'nor', [args[0], args[1], '$0']
end

#cmd_pop(*args) ⇒ Object



134
135
136
137
# File 'lib/haxor/compiler/core.rb', line 134

def cmd_pop(*args)
  process_cmd 'lw', [args[0], '$sp', '0']
  process_cmd 'addi', ['$sp', '$sp', Consts::WORD_SIZE.to_s]
end

#cmd_popm(*args) ⇒ Object



139
140
141
142
143
# File 'lib/haxor/compiler/core.rb', line 139

def cmd_popm(*args)
  process_cmd 'lw', ['$1', '$sp', '0']
  process_cmd 'sw', ['$0', args[0], '$1']
  process_cmd 'addi', ['$sp', '$sp', Consts::WORD_SIZE.to_s]
end

#cmd_push(*args) ⇒ Object



117
118
119
120
# File 'lib/haxor/compiler/core.rb', line 117

def cmd_push(*args)
  process_cmd 'addi', ['$sp', '$sp', '-' + Consts::WORD_SIZE.to_s]
  process_cmd 'sw',   ['$sp', '0', args[0]]
end

#cmd_pushi(*args) ⇒ Object



122
123
124
125
126
# File 'lib/haxor/compiler/core.rb', line 122

def cmd_pushi(*args)
  process_cmd 'addi', ['$1', '$0', args[0]]
  process_cmd 'addi', ['$sp', '$sp', '-' + Consts::WORD_SIZE.to_s]
  process_cmd 'sw',   ['$sp', '0', '$1']
end

#cmd_pushm(*args) ⇒ Object



128
129
130
131
132
# File 'lib/haxor/compiler/core.rb', line 128

def cmd_pushm(*args)
  process_cmd 'lw', ['$1', '$0', args[0]]
  process_cmd 'addi', ['$sp', '$sp', '-' + Consts::WORD_SIZE.to_s]
  process_cmd 'sw',   ['$sp', '0', '$1']
end

#cmd_rem ⇒ Object



114
115
# File 'lib/haxor/compiler/core.rb', line 114

def cmd_rem(*)
end

#cmd_resw(*args) ⇒ Object



108
109
110
111
112
# File 'lib/haxor/compiler/core.rb', line 108

def cmd_resw(*args)
  (1..args[0].to_i).each do
    add Token::Int64.new(0)
  end
end

#cmd_ret(*_args) ⇒ Object



157
158
159
# File 'lib/haxor/compiler/core.rb', line 157

def cmd_ret(*_args)
  process_cmd 'jr', ['$' + Vm::Cpu::Core::REG_RETURN.to_s]
end

#cmd_section(name) ⇒ Object



86
87
88
# File 'lib/haxor/compiler/core.rb', line 86

def cmd_section(name)
  @unit.section = name[1..-1].to_sym
end

#compile(filename) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/haxor/compiler/core.rb', line 248

def compile(filename)
  input = File.read(filename, encoding: 'ASCII-8BIT')

  @unit = Unit.new filename

  input.lines.map(&:chomp).each_with_index do |line, index|
    next if line.empty?

    tmp = line.split ' ', 2
    cmd = tmp[0]
    args = split_arguments(tmp[1] || '')

    if cmd[-1] == ':'
      add Token::Label.new(cmd[0...-1])
    else
      process_cmd(cmd, args)
    end
  end

  @unit.save(filename + '.u')
end

#init_cmds ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/haxor/compiler/core.rb', line 19

def init_cmds
  bind_cmd 'section', [:any],       :cmd_section
  bind_cmd 'dw',      [:wildcard],  :cmd_dw
  bind_cmd 'resw',    [:any],       :cmd_resw
  bind_cmd '#',       [:wildcard],  :cmd_rem
  bind_cmd 'push',    [:reg],       :cmd_push
  bind_cmd 'pushi',   [:imm],       :cmd_pushi
  bind_cmd 'pushm',   [:imm],       :cmd_pushm
  bind_cmd 'pop',     [:reg],       :cmd_pop
  bind_cmd 'popm',    [:imm],       :cmd_popm
  bind_cmd 'move',    [:reg, :reg], :cmd_move
  bind_cmd 'clear',   [:reg],       :cmd_clear
  bind_cmd 'not',     [:reg, :reg], :cmd_not
  bind_cmd 'ret',     [],           :cmd_ret

  bind_cmd 'b',       [:imm],             :cmd_b
  bind_cmd 'bal',     [:imm],             :cmd_bal
  bind_cmd 'bgt',     [:reg, :reg, :imm], :cmd_bgt
  bind_cmd 'blt',     [:reg, :reg, :imm], :cmd_blt
  bind_cmd 'bge',     [:reg, :reg, :imm], :cmd_bge
  bind_cmd 'ble',     [:reg, :reg, :imm], :cmd_ble
  bind_cmd 'blez',    [:reg, :imm],       :cmd_blez
  bind_cmd 'bgtz',    [:reg, :imm],       :cmd_bgtz
  bind_cmd 'beqz',    [:reg, :imm],       :cmd_beqz

  bind_cmd 'nop',     [],                 Vm::Cpu::Core::OP_NOP
  bind_cmd 'add',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_ADD
  bind_cmd 'addi',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_ADDI
  bind_cmd 'sub',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_SUB
  bind_cmd 'mult',    [:reg, :reg, :reg], Vm::Cpu::Core::OP_MULT
  bind_cmd 'div',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_DIV
  bind_cmd 'mod',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_MOD
  bind_cmd 'lw',      [:reg, :reg, :imm], Vm::Cpu::Core::OP_LW
  bind_cmd 'sw',      [:reg, :imm, :reg], Vm::Cpu::Core::OP_SW
  bind_cmd 'lui',     [:reg, :imm],       Vm::Cpu::Core::OP_LUI
  bind_cmd 'and',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_AND
  bind_cmd 'andi',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_ANDI
  bind_cmd 'or',      [:reg, :reg, :reg], Vm::Cpu::Core::OP_OR
  bind_cmd 'ori',     [:reg, :reg, :imm], Vm::Cpu::Core::OP_ORI
  bind_cmd 'xor',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_XOR
  bind_cmd 'nor',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_NOR
  bind_cmd 'slt',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_SLT
  bind_cmd 'slti',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_SLTI
  bind_cmd 'slli',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_SLLI
  bind_cmd 'srli',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_SRLI
  bind_cmd 'sll',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_SLL
  bind_cmd 'srl',     [:reg, :reg, :reg], Vm::Cpu::Core::OP_SRL
  bind_cmd 'beq',     [:reg, :reg, :imm], Vm::Cpu::Core::OP_BEQ,  [:rel_imm, :x8]
  bind_cmd 'beql',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_BEQL, [:rel_imm, :x8]
  bind_cmd 'bne',     [:reg, :reg, :imm], Vm::Cpu::Core::OP_BNE,  [:rel_imm, :x8]
  bind_cmd 'bnel',    [:reg, :reg, :imm], Vm::Cpu::Core::OP_BNEL, [:rel_imm, :x8]
  bind_cmd 'j',       [:imm],             Vm::Cpu::Core::OP_J,    [:x8]
  bind_cmd 'jr',      [:reg],             Vm::Cpu::Core::OP_JR
  bind_cmd 'jal',     [:imm],             Vm::Cpu::Core::OP_JAL,  [:x8]
  bind_cmd 'exiti',   [:imm],             Vm::Cpu::Core::OP_EXITI
  bind_cmd 'syscall', [],                 Vm::Cpu::Core::OP_SYSCALL
end

#offset?(value) ⇒ Boolean

Returns:

  • (Boolean)


366
367
368
369
# File 'lib/haxor/compiler/core.rb', line 366

def offset?(value)
  return false unless value.is_a? String
  value.start_with?('[') && value.end_with?(']')
end

#offset_flags(*operands) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
# File 'lib/haxor/compiler/core.rb', line 354

def offset_flags(*operands)
  result = 0
  flag = Consts::OPERAND_DEREFERENCE << Consts::OPCODE_FLG_OFFSET

  operands.each do |operand|
    result |= flag if offset?(operand)
    flag << Consts::OPERAND_FLAGS
  end

  result
end

#parse_number(value) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/haxor/compiler/core.rb', line 317

def parse_number(value)
  md = value.match(/\A([-+]?[0-9]+[0-9A-F]*)([bhd]?)\z/)
  fail if md.nil?

  case md[2]
  when 'b'
    base = 2
  when 'h'
    base = 16
  when 'd'
  else
    base = 10
  end

  md[1].to_i(base)
end

#parse_value(value) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/haxor/compiler/core.rb', line 334

def parse_value(value)
  value = strip_offset value

  begin
    number = parse_number(value)
    @autolabel += 1
    label = "__autolabel_#{@unit.hash}_#{@autolabel}"

    old_section = @unit.section
    @unit.section = :data
    add Token::Label.new(label)
    add Token::Int64.new(number)
    @unit.section = old_section

    add Token::Pointer.new(label)
  rescue
    add Token::Pointer.new(value)
  end
end

#process_cmd(cmd, args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/haxor/compiler/core.rb', line 231

def process_cmd(cmd, args)
  @cmds.each do |item|
    next if item[:cmd] != cmd

    if item[:opcode].is_a? Symbol
      send(item[:opcode], *args)
      return
    end

    add_cmd item, args
    return
  end

  puts cmd
  fail
end

#split_arguments(string) ⇒ Object

this is ugly and must be reworked



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/haxor/compiler/core.rb', line 207

def split_arguments(string)
  inside_quotes = false
  splits = []
  string.each_char.with_index(0) do |x, i|
    if x == '"' && string[i - 1] != '\\'
      inside_quotes ^= true
    end

    if x == ',' && !inside_quotes
      splits << i
    end
  end

  splits << string.size

  last_x = 0
  args = []
  splits.each do |x|
    args << string[last_x...x]
    last_x = x + 1
  end
  args.map!(&:strip).delete_if { |x| x.length == 0 }
end

#strip_offset(value) ⇒ Object



371
372
373
374
# File 'lib/haxor/compiler/core.rb', line 371

def strip_offset(value)
  return value[1...-1] if offset?(value)
  value
end