Posts LLVM - BasicBlock 다루기, 출력해보기
Post
Cancel

LLVM - BasicBlock 다루기, 출력해보기

오늘의 목표

  • BasicBlock 관련해서 이름 출력해보기

~(task list가 있다는걸 알고 사용해봤다ㅋㅋ)~


1. Basicblock 관련 출력해보기

Basicblock의 단위나 나눠지는 구분? 같은 게 직접 출력해봐야 이해가 될 것 같아서 간단하게 이름이랑 몇 가지 출력할만한 것들을 출력해보기로 했다.

다음과 같이 test03.c코드를 작성하고.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
  int num01=0;
  int num02=10;
  int result;
  if(num01==0){
      for(int i=0;i<6;i++){
          num01++;
      }
      result=num01;
  }
  else{
      result=num02;
  }
  printf("test : %d", result);
}

pass의 내용을 다음과 같이 작성한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  struct BasicPass : public FunctionPass {
    static char ID;
    BasicPass() : FunctionPass(ID) {}

    virtual bool runOnFunction(Function &F) {
      errs() << "- Start of function [" << F.getName() << "]\n";

      for (BasicBlock &BB : F)
        errs() << "- Start of Basicblock ["<< BB.getName() << "], num of instructions ["
                   << BB.size() << "] instructions.\n";

      return false;
    }
  };

출력 결과는 다음과 같이, function의 이름과 그 안의 basicblock들에 대해서 출력이 된 것을 볼 수 있다.

img

해 보니까 instructions에 대해서도 궁금해져서 이 부분도 출력을 해 보는 코드로 보완하고 출력해 보았다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct BasicPass : public FunctionPass {
  static char ID;
  BasicPass() : FunctionPass(ID) {}

  virtual bool runOnFunction(Function &F) {
    errs() << "- Start of function [" << F.getName() << "]\n";

    for (BasicBlock &BB : F){
      errs() << "- Start of Basicblock ["<< BB.getName() << "], num of instructions ["
                 << BB.size() << "] instructions.\n";
      for (Instruction &I : BB)
        errs() << "- Instruction : " << I << "\n";
    }

    return false;
  }
};

img 다음과 같은 결과가 나온다. 코드에 존재하는 if, for문들을 쪼개고, 그 구성요소인 instructions들이 쭉 나타난 것을 볼 수 있다.

-- BasicBlock과 Instruction을 출력하는 코드는 LLVM Programmer’s Manual를 참고하였다.

This post is licensed under CC BY 4.0 by the author.

Contents