Module: Kumi::Core::LIR::Validate

Defined in:
lib/kumi/core/lir/validate.rb

Class Method Summary collapse

Class Method Details

.declaration!(instructions) ⇒ Object

Validate one declaration’s instruction list

Raises:



10
11
12
13
14
15
16
17
18
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
# File 'lib/kumi/core/lir/validate.rb', line 10

def declaration!(instructions)
  ensure_opcodes!(instructions)

  defs = {}
  depth = 0
  yield_seen = false

  instructions.each_with_index do |ins, i|
    # producer stamps
    if ins.result_register
      raise Error, "missing stamp at #{i}" unless ins.stamp.is_a?(Stamp)

      r = ins.result_register
      raise Error, "redefinition of #{r} at #{i}" if defs.key?(r)

      defs[r] = i
    end

    case ins.opcode
    when :LoopStart
      attrs = ins.attributes || {}
      raise Error, "LoopStart missing :axis at #{i}" unless attrs[:axis]

      depth += 1
    when :LoopEnd
      raise Error, "LoopEnd without LoopStart at #{i}" if depth.zero?

      depth -= 1
    when :LoadDeclaration
      raise Error, "LoadDeclaration missing :axes at #{i}" unless Array(ins.attributes[:axes]).is_a?(Array)
      raise Error, "LoadDeclaration missing stamp at #{i}" unless ins.stamp.is_a?(Stamp)
    when :Yield
      raise Error, "multiple Yield (at #{i})" if yield_seen

      yield_seen = true
      raise Error, "instructions after Yield (at #{i})" unless i == instructions.length - 1
    end
  end

  raise Error, "unclosed loops" unless depth.zero?
  raise Error, "missing Yield" unless yield_seen

  true
end

.program!(ops_by_decl) ⇒ Object

Validate whole program: { name => { operations: […] } }



56
57
58
59
60
61
# File 'lib/kumi/core/lir/validate.rb', line 56

def program!(ops_by_decl)
  ops_by_decl.each do |_name, h|
    declaration!(Array(h[:operations]))
  end
  true
end