我在 Transforms/Instrumentation 中准备好了代码,它会拦截我的原始代码。我想使用指向原始函数的函数指针(递归)。我不想在运行时直接返回原始函数,而是使用函数指针变量返回(函数指针部分应该在 LLVM IR 代码中)。
我找不到任何示例代码。我试过用 getOrInsertFunction 函数来创建一个新的函数指针,看起来这个 API 是用来插入一个新函数,而不是变量。
//-----------------------------------------------------
//Original Function
int find_sum(int a)
{
if (a!=0) {
return a + find_sum(a-1);
}
return 0;
}
int main() {
printf("Value of find_sum is %d\n", find_sum(5));
}
//-------------------------------------------------------------------------
//Expected Function at run time through Instrumentation pass
int find_sum(int a)
{
int (*fn_ptr)(int) = &find_sum; //should be an IR code
if (a!=0) {
return a + (*fn_ptr)(a-1); //should be an IR code
}
return 0;
}
int main() {
printf("Value of find_sum is %d\n", find_sum(5));
}
//--------------------------------------------------------