LLVM系列第十七章:控制流语句for

系列文章目录

LLVM系列第一章:编译LLVM源码
LLVM系列第二章:模块Module
LLVM系列第三章:函数Function
LLVM系列第四章:逻辑代码块Block
LLVM系列第五章:全局变量Global Variable
LLVM系列第六章:函数返回值Return
LLVM系列第七章:函数参数Function Arguments
LLVM系列第八章:算术运算语句Arithmetic Statement
LLVM系列第九章:控制流语句if-else
LLVM系列第十章:控制流语句if-else-phi
LLVM系列第十一章:写一个Hello World
LLVM系列第十二章:写一个简单的词法分析器Lexer
LLVM系列第十三章:写一个简单的语法分析器Parser
LLVM系列第十四章:写一个简单的语义分析器Semantic Analyzer
LLVM系列第十五章:写一个简单的中间代码生成器IR Generator
LLVM系列第十六章:写一个简单的编译器
LLVM系列第十七章:for循环
LLVM系列第十八章:写一个简单的IR处理流程Pass
LLVM系列第十九章:写一个简单的Module Pass
LLVM系列第二十章:写一个简单的Function Pass
LLVM系列第二十一章:写一个简单的Loop Pass
LLVM系列第二十二章:写一个简单的编译时函数调用统计器(Pass)
LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
LLVM系列第二十四章:用Xcode编译调试LLVM源码
LLVM系列第二十五章:简单统计一下LLVM源码行数
LLVM系列第二十六章:理解LLVMContext
LLVM系列第二十七章:理解IRBuilder
LLVM系列第二十八章:写一个JIT Hello World
LLVM系列第二十九章:写一个简单的常量加法“消除”工具(Pass)

flex&bison系列



前言

在此记录下用LLVM创建控制流语句(for循环语句)的过程,以备查阅。

开发环境的配置请参考第一章 《LLVM系列第一章:编译LLVM源码》。

我们知道,一个for语句包含了条件判断以及循环执行的任务。具体循环多少次,取决于条件判断的结果。而“条件”则包含了循环的起点、终止条件、步进长度。

这里的for循环语句IR,其实跟前面章节的if-else语句有些相似的地方。它们都需要用到比较指令、跳转指令、栈上的变量等。

本章,我们就来创建简单的for控制流语句。

一、Hello For Loop

为简单起见,我们就来为以下这个简单的C函数生成IR代码(示例):

// Test.c

int Test(int a)
{
    int b = 0;

    for (int i = 0; i < a; i++)
    {
        b = b + i;
    }

    return b;
}

这里有个可变变量b,我们可以用alloca 指令在栈上申明一个局部变量b。注意用alloca 指令申明的变量,其实得到是变量的地址。如果要访问它,我们需要用store和load指令。关于这两个指令的用法,请参考前面章节。

以下就是我们调用LLVM C++ API来生成IR的代码(示例):

// HelloLoop.cpp

#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"

#include <vector>

using namespace llvm;

int main(int argc, char* argv[])
{
    LLVMContext context;
    IRBuilder<> builder(context);

    // Create a module
    Module* module = new Module("Test.c", context);

    // Add a function with argument
    //   int Test(int a)
    std::vector<Type*> parameters(1, builder.getInt32Ty());
    FunctionType* functionType = FunctionType::get(builder.getInt32Ty(), parameters, false);
    Function* function = Function::Create(functionType, GlobalValue::ExternalLinkage, "Test", module);
    Value* a = function->getArg(0);
    a->setName("a");

    // Add some basic blocks to the function
    BasicBlock* entryBlock = BasicBlock::Create(context, "entry", function);
    BasicBlock* forConditionBlock = BasicBlock::Create(context, "for.condition", function);
    BasicBlock* forBodyBlock = BasicBlock::Create(context, "for.body", function);
    BasicBlock* forIncrementBlock = BasicBlock::Create(context, "for.increment", function);
    BasicBlock* forEndBlock = BasicBlock::Create(context, "for.end", function);

    // Fill the "entry" block (1):
    //   int b = 0;
    builder.SetInsertPoint(entryBlock);
    Value* bPtr = builder.CreateAlloca(builder.getInt32Ty(), nullptr, "b.address");
    builder.CreateStore(builder.getInt32(1), bPtr);

    // Fill the "entry" block (2):
    //   for (int i = 0; ...)
    Value* iPtr = builder.CreateAlloca(builder.getInt32Ty(), nullptr, "i.address");
    builder.CreateStore(builder.getInt32(0), iPtr);
    builder.CreateBr(forConditionBlock);

    // Fill the "for.condition" block:
    //   for (... i < a; ...)
    builder.SetInsertPoint(forConditionBlock);
    Value* i0 = builder.CreateLoad(iPtr);
    Value* forConditionCompare = builder.CreateICmpSLT(i0, a, "for.condition.compare.result");
    builder.CreateCondBr(forConditionCompare, forBodyBlock, forEndBlock);

    // Fill the "for.body" block:
    //   b = b + i;
    builder.SetInsertPoint(forBodyBlock);
    Value* b0 = builder.CreateLoad(bPtr);
    Value* i1 = builder.CreateLoad(iPtr);
    Value* addResult = builder.CreateAdd(b0, i1, "add.result");
    builder.CreateStore(addResult, bPtr);
    builder.CreateBr(forIncrementBlock);

    // Fill the "for.increment" block:
    //   for (... i++)
    builder.SetInsertPoint(forIncrementBlock);
    Value* i2 = builder.CreateLoad(iPtr);
    Value* incrementedI = builder.CreateAdd(i2, builder.getInt32(1), "i.incremented");
    builder.CreateStore(incrementedI, iPtr);
    builder.CreateBr(forConditionBlock);

    // Fill the "for.end" block:
    //   return b;
    builder.SetInsertPoint(forEndBlock);
    Value* returnValue = builder.CreateLoad(bPtr, "return.value");
    builder.CreateRet(returnValue);

    // Print the IR
    verifyFunction(*function);
    module->print(outs(), nullptr);

    return 0;
}

二、编译

用clang++进行编译(示例):

# Set up C++ standard library and header path
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)

# Compile
clang++ -w -o HelloLoop `llvm-config --cxxflags --ldflags --system-libs --libs core` HelloLoop.cpp

以上命令会生成一个名为HelloLoop的可执行程序。

三、运行

运行HelloLoop(示例):

./HelloLoop

输出结果如下(示例):

; ModuleID = 'Test.c'
source_filename = "Test.c"

define i32 @Test(i32 %a) {
entry:
  %b.address = alloca i32, align 4
  store i32 1, i32* %b.address, align 4
  %i.address = alloca i32, align 4
  store i32 0, i32* %i.address, align 4
  br label %for.condition

for.condition:                                    ; preds = %for.increment, %entry
  %0 = load i32, i32* %i.address, align 4
  %for.condition.compare.result = icmp slt i32 %0, %a
  br i1 %for.condition.compare.result, label %for.body, label %for.end

for.body:                                         ; preds = %for.condition
  %1 = load i32, i32* %b.address, align 4
  %2 = load i32, i32* %i.address, align 4
  %add.result = add i32 %1, %2
  store i32 %add.result, i32* %b.address, align 4
  br label %for.increment

for.increment:                                    ; preds = %for.body
  %3 = load i32, i32* %i.address, align 4
  %i.incremented = add i32 %3, 1
  store i32 %i.incremented, i32* %i.address, align 4
  br label %for.condition

for.end:                                          ; preds = %for.condition
  %return.value = load i32, i32* %b.address, align 4
  ret i32 %return.value
}

四、总结

我们用LLVM提供的C++ API,创建了简单的for循环语句,并打印出了它的IR代码。完整源码示例请参看:
https://github.com/wuzhanglin/llvm-IR-examples

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
LLVM拓展控制流主要是通过引入新的指令来实现的。LLVM提供了多种控制流指令,如条件分支、无条件分支、switch语句等,但是在某些情况下,这些指令可能无法满足程序员的需要。例如,在一些高级语言中,存在一些控制流结构,如异常处理、goto语句、try-catch语句等,这些结构无法直接转换为LLVM指令。 为了解决这个问题,LLVM引入了拓展控制流指令。拓展控制流指令可以模拟出高级语言中的控制流结构,从而实现对高级语言的支持。例如,LLVM引入了invoke指令来实现函数调用的异常处理,引入了indirectbr指令来实现goto语句,引入了landingpad指令来实现异常处理等。 关于LLVM拓展控制流的编译实验过程,一般可以分为以下几个步骤: 1. 实现拓展控制流指令的前端语言支持。首先需要在前端语言中支持相应的控制流结构,例如在C++中支持异常处理、goto语句等。 2. 实现拓展控制流指令的中间表示(IR)支持。接下来需要在LLVM IR中引入相应的拓展控制流指令,例如invoke、indirectbr、landingpad等。 3. 实现拓展控制流指令的后端支持。最后需要在LLVM后端中实现相应的指令转换和代码生成,以便于将LLVM IR转换为目标代码。 在实际的编译实验中,需要根据具体的拓展控制流指令来进行相应的实现,具体的实现细节可以参考LLVM官方文档。同时,也需要进行相应的测试和验证,以确保拓展控制流指令的正确性和可用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值