如何确保命名参数列表的评估顺序和形式参数顺序?

时间:2016-06-15 18:17:54

标签: algorithm compiler-construction associations postfix-notation jvm-bytecode

在编译器上工作时,我遇到了与命名参数列表和评估顺序相关的问题。基本上,该语言确保对于以下函数和函数调用:

int call(int first, int second) = first - second

int sideEffect(int i) { println(i); return i }

println(call(second: sideEffect(0), first: sideEffect(1)))

输出

0
1
1

尽管参数的顺序相反。我正在使用JVM,因此必须在字节码级别对它们进行排序以匹配call的签名。在基于堆栈的伪字节码中:

push 0
call sideEffect(int)
push 1
call sideEffect(int)
swap
call call(int, int)

如您所见,此处需要swap字节码指令,以确保它们都以正确的顺序进行评估和传递。这可以画成图表:

Actual:   second   first
                \ /
                swap
                / \
Formal:    first   second

只有一条swap指令可以交换堆栈中的前两个元素,因此对于更多元素,编译器需要生成局部变量。

Actual: third   second   first
          |       |  /¯¯¯¯
        local0   swap    local0
            ____/ |        |
Formal: first   second   third

字节码:

third...
store local0
second...
first...
swap
load local0
call ...

当然,这可以扩展到任意数量的实际和形式参数。

我现在正在寻找的是某种算法,用于确定是否应该插入这些swap或本地变量指令。对编译器实现的引用也会有所帮助。

请注意,不是我的问题的一部分,哪个实际参数属于哪个形式参数。这已经由我的编译器解决了。为简化起见,问题可以这样看:

给定两个相同大小的数组,它们包含不同顺序的相同元素,哪些元素可以从第一个数组交换或移位,以获得第二个数组的顺序。

1 个答案:

答案 0 :(得分:3)

仅使用寄存器,基本上只有一种解决方案。迭代所需堆栈顺序中的参数。如果已经计算了当前参数,则从本地加载它。否则,在执行顺序之前计算未计算的参数,将它们存储在寄存器中,然后计算当前参数。

swap操作的存在意味着我们可以使用堆栈的顶部作为事实上的局部变量。下面的Python代码。

import heapq
import itertools


class Allocator:

  def __init__(self):
    self.free_heap = []
    self.next_free = 0

  def allocate(self):
    if self.free_heap:
      return heapq.heappop(self.free_heap)
    i = self.next_free
    self.next_free += 1
    return i

  def free(self, i):
    heapq.heappush(self.free_heap, i)

  def is_allocated(self, i):
    return i < self.next_free and i not in self.free_heap


def loads_and_stores(perm):
  perm = list(perm)
  n = len(perm)
  assert set(perm) == set(range(n))
  location = {}
  allocator = Allocator()
  i = iter(perm)
  # "location 0" is the top of the stack
  for j in range(n):
    if j in location:
      if location[j] != 0:
        # not top of stack; need an actual load
        print 'load', location[j]
        if allocator.is_allocated(0):
          # need to maintain the top of the stack
          print 'swap'
      allocator.free(location[j])
    else:
      while True:
        k = next(i)
        print 'compute', k
        if k == j:
          if allocator.is_allocated(0):
            # need to maintain the top of the stack
            print 'swap'
          break
        location[k] = allocator.allocate()
        if location[k] != 0:
          # not top of stack; need an actual store
          print 'store', location[k]
  return perm


def test(n):
  for perm in itertools.permutations(range(n)):
    print perm
    loads_and_stores(perm)


test(4)

这里有一些有趣的优化,因为allocate可以返回任何免费的本地。最优算法将考虑进行每次分配的执行成本。