LLVM如何获取指令的返回值

时间:2016-09-11 13:14:42

标签: c++ llvm

我有一个程序从堆栈中分配内存,如下所示:

%x = alloca i32, align 4

在我的传递中,我想获得在运行时指向此分配内存的实际内存指针。这应该是%x。如何在通行证中获得指针?

Instruction* I;
if (AllocaInst* AI = dyn_cast<AllocaInst>(I)) {
    //How to get %x?
} 

2 个答案:

答案 0 :(得分:4)

您可以使用指令*作为值*(并且指令继承自值),然后您正在处理该指令的结果/返回值。我已经调整了LLVM Pass中的一些代码,以演示使用alloca分配空间然后存储到该位置。请注意,指令的结果可以直接传递给其他指令,因为它们是值。

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int course, numberOfClasses; //declare variables
    double gradePointTotal = 0, gradePointAve; //initialize to 0
    string grade;

    cout << "GPA Calculator \n";
    cout << "\n Enter the number of classes ";
    cin >> numberOfClasses; // enter number of classes

    for (course = 1; course <= numberOfClasses; course++ ) // define loop
    {
        cout << "\n Enter a letter grade for class number " << course << ": ";
        cin >> grade; //Enter grade
        if ( grade == "A" || grade == "a") //accepts upper and lower case
            gradePointTotal = gradePointTotal + 4;
        else if ( grade == "B" || grade == "b")
            gradePointTotal = gradePointTotal + 3;
        else if ( grade == "C" || grade == "c")
            gradePointTotal = gradePointTotal + 2;
        else if ( grade == "D" || grade == "d")
            gradePointTotal = gradePointTotal + 1;
        else if ( grade == "F" || grade == "f")
            gradePointTotal = gradePointTotal + 0;

        gradePointAve = gradePointTotal / numberOfClasses; // calculate the GPA
        cout << "\n Your GPA is: " << gradePointAve << endl; // display GPA

    }
}

答案 1 :(得分:0)

如果您想存储或加​​载到%x,您只需使用商店或盖子说明

如果需要指针的数值,请使用ptrtoint指令。

相关问题