Class: Emfrp::AllocRequirement

Inherits:
Object
  • Object
show all
Defined in:
lib/emfrp/compile/c/alloc.rb

Instance Method Summary collapse

Constructor Details

#initialize(top) ⇒ AllocRequirement



3
4
5
6
7
# File 'lib/emfrp/compile/c/alloc.rb', line 3

def initialize(top)
  @top = top
  @alloc_table = AllocTable.new(top)
  @sorted_nodes = @top[:dict][:sorted_nodes].map{|x| x.get}
end

Instance Method Details

#data_requirement(sorted_datas, max_amount) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/emfrp/compile/c/alloc.rb', line 19

def data_requirement(sorted_datas, max_amount)
  retains = []
  sorted_datas.each do |d|
    max_amount |= type_alloc_sum(retains) & exp_alloc(d[:exp])
  end
  return max_amount
end

#exp_alloc(exp) ⇒ Object



101
102
103
# File 'lib/emfrp/compile/c/alloc.rb', line 101

def exp_alloc(exp)
  @alloc_table.exp_alloc(exp)
end

#life_point(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/emfrp/compile/c/alloc.rb', line 72

def life_point(node)
  self_position = @sorted_nodes.index{|x| x == node}
  distance_to_end = @sorted_nodes.size - self_position
  res = []
  @sorted_nodes.each_with_index do |x, i|
    x[:params].each do |param|
      if param[:name] == node[:name]
        if param[:last]
          res << distance_to_end + i
        else
          res << i - self_position
        end
      end
    end
  end
  if res == []
    raise "Assertion error"
  else
    return res.max
  end
end

#node_init_requirement(init_nodes, max_amount, datas) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/emfrp/compile/c/alloc.rb', line 27

def node_init_requirement(init_nodes, max_amount, datas)
  retains = datas.clone
  init_nodes.each do |n|
    max_amount |= type_alloc_sum(retains) & exp_alloc(n[:init_exp])
    retains << n
  end
  return max_amount
end

#node_loop_requirement(max_amount, init_nodes, datas) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/emfrp/compile/c/alloc.rb', line 36

def node_loop_requirement(max_amount, init_nodes, datas)
  lrefs = init_nodes
  crefs = []
  @sorted_nodes.each_with_index do |n, i|
    max_amount |= type_alloc_sum(datas + lrefs + crefs) & exp_alloc(n[:exp])
    crefs << n
    lrefs.reject! do |x|
      i >= ref_pos_last(x)
    end
    crefs.reject! do |x|
      ref_pos_last(x) == -1 and i >= ref_pos_current(x)
    end
  end
  return max_amount
end

#ref_pos_current(node) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/emfrp/compile/c/alloc.rb', line 62

def ref_pos_current(node)
  res = -1
  @sorted_nodes.each_with_index do |n, i|
    if n[:params].any?{|param| !param[:last] && param[:name] == node[:name]}
      res = i
    end
  end
  return res
end

#ref_pos_last(node) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/emfrp/compile/c/alloc.rb', line 52

def ref_pos_last(node)
  res = -1
  @sorted_nodes.each_with_index do |n, i|
    if n[:params].any?{|param| param[:last] && param[:name] == node[:name]}
      res = i
    end
  end
  return res
end

#requirementObject



9
10
11
12
13
14
15
16
17
# File 'lib/emfrp/compile/c/alloc.rb', line 9

def requirement
  sorted_datas = @top[:dict][:sorted_datas].map{|x| x.get}
  init_nodes = @sorted_nodes.select{|n| n[:init_exp]}
  max_amount = Alloc.empty
  max_amount = data_requirement(sorted_datas, max_amount)
  max_amount = node_init_requirement(init_nodes, max_amount, sorted_datas)
  max_amount = node_loop_requirement(max_amount, init_nodes, sorted_datas)
  return max_amount
end

#type_alloc_sum(defs) ⇒ Object



94
95
96
97
98
99
# File 'lib/emfrp/compile/c/alloc.rb', line 94

def type_alloc_sum(defs)
  defs.inject(Alloc.empty) do |acc, d|
    type_def = @alloc_table.utype_to_type_def(d[:typing])
    acc & @alloc_table.type_alloc(type_def)
  end
end