gpt4 book ai didi

assembly - LLVM 程序集 : call a function using varargs

转载 作者:行者123 更新时间:2023-12-02 19:05:39 25 4
gpt4 key购买 nike

我想在 LLVM 程序集中定义一个函数作为参数:

  • 子功能的标识符
  • 一个可变参数

这个函数应该做一些预处理,找到标识符的正确函数并使用可变参数调用它,并返回其结果。

类似于:

define ??? @1 (i32 %identifier, ...vararg...)
{
switch i32 %identifier, label %def, i32 1, label %a
a:
%1 = tail call @function_for_a, ...vararg...
ret ??? %1
def:
ret void
}

这似乎不可能。还有办法做到这一点吗?我认为使用普通汇编程序应该是可能的。

这是面向对象语言的调度函数。我希望它能快点。

我想要的是一种方法:

  • 从堆栈中删除@1使用的第一个参数
  • 分支到第二个函数。

然后,第二个函数将代替第一个函数(它是尾部调用)执行,但其参数列表对于第一个函数来说并不完全已知(第一个函数的可变参数)。

最佳答案

首先:如果你想传递可变参数,就不能使用尾部调用:

http://llvm.org/docs/LangRef.html

  1. The optional "tail" marker indicates that the callee function does not access any allocas or varargs in the caller.

第二:您的调用约定是什么?

第三:要处理可变参数(就像在 C 中一样),您需要使用 va_* 函数创建一个新的 va_list 并将所有参数复制到其中:

http://llvm.org/docs/LangRef.html#int-varargs

最后:此调度程序调用的每个函数都必须使用 va_* 函数来获取其参数。

更新:

在谈论堆栈作为函数参数的存储之前,您应该知道将使用哪种调用约定(默认值是什么)。然后。如果没有 va_* 函数,则无法访问不传递“...”参数,因为这是在 LLVM 程序集中访问它们的唯一方法。

有一种像 C 语言一样的方法,这里 printf 将使用所有“...”参数调用 vfprintf,并且不知道要传递多少个参数

// 3-clause BSD licensed to The Regents of the University of California.

int
printf(const char *fmt, ...)
{
int ret;
va_list ap;

va_start(ap, fmt);
ret = vfprintf(stdout, fmt, ap);
va_end(ap);
return (ret);
}

Vfprintf 以特殊方式声明以获取“...”并从中提取参数:

int
vfprintf(FILE *fp, const char *fmt0, __va_list ap)
{
...
va_arg(ap, type) //to get next arg of type `type`

关于assembly - LLVM 程序集 : call a function using varargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7015477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com