+class Array
+ def find_max_index
+ return nil if self.length==0
+ maxidx=0
+ max=self[0]
+ self.each do |i|
+ if(self[i]!=nil && max<self[i])
+ maxidx = i
+ max = self[i]
+ end
+ end
+ return maxidx
+ end
+
+end
+
+def calculate_load_pressure(use_map, use_locations, regs, steps)
+ loads=0
+ reg_map = Array.new(steps)
+ (0..(reg_map.length-1)).each{|i| reg_map[i]=Array.new(regs)}
+ (0..(steps-1)).each do |step|
+ use_locations.each do |e|
+ e.pop if e[-1] && e[-1]<step
+ end
+ local_use_map = Array.new(regs)
+ reg_map[step] = reg_map[step-1].clone if step>0
+ #(0..(regs-1)).each {|i| reg_map[step][i] = reg_map[step-1][i]}
+ use_map[step].each do |entry|
+# print 'DBG: step='+step.to_s+' entry='+entry.to_s
+ found = reg_map[step].find_index(entry)
+ if found!=nil
+# print ' (direct)'
+ reg_map[step][found] = entry
+ local_use_map[found] = 1
+ else
+ loads += 1
+ if t0=reg_map[step].find_index(nil)
+# print ' (found unsused slot)'
+ reg_map[step][t0] = entry
+ local_use_map[t0] = 1
+ else
+ # find a register to clear
+ a = reg_map[step].collect {|e| use_locations[e][-1]}
+ if t1 = a.find_index(nil)
+# print ' (found not further used slot)'
+ reg_map[step][t1] = entry
+ else
+# print ' (reassigned slot)'
+ reg_map[step][a.find_max_index] = entry
+ end
+ end
+ end
+# print "\n"
+ end
+# puts 'DBG: map part ('+step.to_s+'): '+reg_map[step].inspect
+ end
+ return loads, reg_map
+end
+
+################################################################################
+