Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using DragonEgg


Dragonegg is a gcc plugin that allows gcc to make use of the LLVM optimizer and code generator instead of gcc's own optimizer and code generator.

Getting ready

You need to have gcc 4.5 or above, with the target machine being x86-32/x86-64 and an ARM processor. Also, you need to download the dragonegg source code and build the dragonegg.so file.

How to do It…

Do the following steps:

  1. Create a simple hello world program:

    $ cat testprog.c
    #include<stdio.h>
    int main() {
    printf("hello world");
    }
    
  2. Compile this program with your gcc; here we use gcc-4.5:

    $ gcc testprog.c -S -O1 -o -
      .file  " testprog.c"
      .section  .rodata.str1.1,"aMS",@progbits,1
    .LC0:
      .string  "Hello world!"
      .text
    .globl main
      .type  main, @function
    main:
      subq  $8, %rsp
      movl  $.LC0, %edi
      call  puts
      movl  $0, %eax
      addq  $8, %rsp
      ret
      .size  main, .-main
    
  3. Using the -fplugin=path/dragonegg.so flag in the command line of gcc makes gcc use LLVM's optimizer and LLVM codegen:

    $ gcc testprog.c -S -O1 -o - -fplugin=./dragonegg.so
      .file  " testprog.c"
    # Start of file scope inline assembly
      .ident  "GCC: (GNU) 4.5.0 20090928 (experimental) LLVM: 82450:82981"
    # End of file scope inline assembly
    
    
      .text
      .align  16
      .globl  main
      .type  main,@function
    main:
      subq  $8, %rsp
      movl  $.L.str, %edi
      call  puts
      xorl  %eax, %eax
      addq  $8, %rsp
      ret
      .size  main, .-main
      .type  .L.str,@object
      .section  .rodata.str1.1,"aMS",@progbits,1
    .L.str:
      .asciz  "Hello world!"
      .size  .L.str, 13
    
      .section  .note.GNU-stack,"",@progbits
    

See also