如何使用LLVM C ++ API进行类型检查? [英] How to do type checking with the LLVM C++ API?

查看:162
本文介绍了如何使用LLVM C ++ API进行类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习LLVM C ++ API,并且对如何进行类型检查感到有些困惑.我的讲师提供了一个有关将变量存储在堆栈存储器中的示例,如下所示:

I've just started learning the LLVM C++ API, and I'm a bit confused about how to do type checking. There's an example my instructor has provided to me about storing a variable in stack memory as follows:

llvm::AllocaInst *Alloca;
Alloca = llvm::Builder.CreateAlloca(llvm::IntegerType::get(getGlobalContext(), 32), nullptr, "variable_name");

我理解这一点,但是在下一部分中,它将讨论在将值分配给变量之前进行类型检查. To assign a value in a Decaf statement of the type lvalue = rvalue you should get the location of lvalue from the symbol table. You can check the type of rvalue using the following API call:

I understand this, but in the next part it talks about type checking before assigning a value to a variable. To assign a value in a Decaf statement of the type lvalue = rvalue you should get the location of lvalue from the symbol table. You can check the type of rvalue using the following API call:

const llvm::PointerType *ptrTy = rvalue->getType()->getPointerTo();
ptrTy == Alloca->getType()

我非常困惑为什么需要进行类型检查.我已经阅读了文档,getPointerTo返回一个PointerType对象.所以我的第一个问题是,Alloca的Type对象是IntegerType,那么为什么要创建PointerType对象?在我看来,这似乎完全超出了领域.

I am utterly confused at why this needs to be done for type checking. I've read the documentation and getPointerTo returns a PointerType object. So my first question is, the Type object of Alloca is IntegerType, so why are we creating an object of PointerType? This seems completely out of left field to me.

我的第二个问题是,为什么我们然后将这个PointerType对象与Alloca Type对象(一个IntegerType对象)进行比较?是否存在某种==运算符重载?因为我正在搜索文档,所以找不到任何东西.

The second question I have is, why are we then comparing this PointerType object with the Alloca Type object which is an IntegerType object? Is there some sort of == operator overloading? Because I was searching through the documentation and I couldn't find any.

推荐答案

您似乎正在关注

You appear to be following this tutorial. Your question is missing some valuable context which I reproduce below:

例如,以下代码使用LLVM API创建alloca指令,以在堆栈中存储整数(LLVM类型为i32).该存储空间用于存储值并从堆栈上的存储位置加载值.

For example the following code uses the LLVM API to create an alloca instruction to store integers (LLVM type i32) on the stack. This storage space is used to store values and to load values from the memory locations on the stack.

llvm::AllocaInst *Alloca; 
// unlike CreateEntryBlockAlloca the following will 
// create the alloca instr at the current insertion point
// rather than at the start of the block
Alloca = llvm::Builder.CreateAlloca(llvm::IntegerType::get(getGlobalContext(), 32), nullptr, "variable_name");

然后应将此指针存储到符号表中,以用于 标识符"NAME".您可以使用以下方式访问类型为TYPE的指针 Alloca->getType()当您要为该位置分配值时.

You should then store this pointer into the symbol table for the identifier NAME. You can access the pointer to the type TYPE using Alloca->getType() when you want to assign a value to this location.

因此,Alloca->getType()向您提供了一个PointerType对象,该对象表示指向i32的指针",尤其是指向为i32分配的堆栈中的位置的指针.但是rvaluei32,而不是指向i32的指针.即使这样,我们仍然可以使用Alloca->getType()lvalue(i32)的类型与某物(即rvalue的类型)进行比较:

So Alloca->getType() is providing you a PointerType object representing "pointer to i32," in particular a pointer to the location in the stack that was allocated for the i32. But rvalue is an i32, not a pointer to i32. Even so, we can still use Alloca->getType() to compare the type of the lvalue (i32) to something, namely the type of rvalue:

要在类型为lvalue = rvalue的Decaf语句中分配值,您可以 应该从符号表中获取lvalue的位置.你可以检查一下 使用以下API调用的rvalue类型:

To assign a value in a Decaf statement of the type lvalue = rvalue you should get the location of lvalue from the symbol table. You can check the type of rvalue using the following API call:

const llvm::PointerType *ptrTy = rvalue->getType()->getPointerTo();

我们首先获得rvalue的类型.我们无法将其直接与lvalue的类型进行比较,因为我们只有类型指向lvalue的指针".因此,我们要做的就是将从rvalue获得的类型对象转换为将指针转换为rvalue的类型",也就是说,我们将其从i32转换为*i32.

We first obtain the type of rvalue. We can't compare it directly to the type of lvalue, because we only have the type "pointer to type of lvalue". So what we have to do is convert the type object obtained from rvalue to "pointer to type of rvalue"—that is, we convert it from i32 to a *i32.

换句话说,如果我们所要比较的只是*i32类型,我们如何检查rvalue具有类型i32?答案是,我们只是将rvalue的类型提升为指针类型,即采用其i32并使其成为指针*i32.

In other words, how do we check that rvalue has type i32 if all we have to compare it to is the type *i32? The answer is, we just promote rvalue's type to a pointer type, i.e. take its i32 and make it a pointer *i32.

此时,ptrTy包含类型指向rvalue的指针",而我们也知道Alloca->getType()给我们提供了指向lvalue的指针"的类型.为了将rvalue分配给lvalue,我们检查以下两种类型是否一致:

At this point ptrTy contains the type "pointer to type of rvalue", while we also know that Alloca->getType() gives us the type "pointer to type of lvalue." In order to assign the rvalue to lvalue, we check that these two types coincide:

并检查左值的Alloca位置的类型是否相同 类型:

And check that the type of the Alloca location for lvalue has the same type:

ptrTy == Alloca->getType()

上面的表达式是有条件的;如果类型相同,则为true,否则为false.假设是真的,您可以像这样分配值:

The expression above is the conditional; it is true if the types are the same and false otherwise. Assuming it's true, you assign the values like so:

llvm::Value *val = Builder.CreateStore(rvalue, Alloca)

这篇关于如何使用LLVM C ++ API进行类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆