yhara.jp

Recent Posts

LLVM + Boehm GC

2016-11-23
Tech

Though LLVM itself does not have a GC algorithm, it is not so difficult to use existing GC library, especially conservatice GCs like Boehm GC.

  1. Link Boehm GC
  2. Call GC_malloc instead of malloc

Prerequisites

  • LLVM (eg. brew install llvm)
  • Boehm GC (eg. brew install boehmgc)

Example

declare void @GC_init()
declare i8* @GC_malloc(i64)  ; Use i32 if you are using 32bit OS

; A function that allocates some memory
define i8* @foo(){
  %reg1 = call i8* @GC_malloc(i64 8)
  ret i8* %reg1
}

define i32 @main(){
  ; 0. Init BoehmGC
  call void @GC_init()
  ; 1. Allocate some memory
  %reg1 = call i8* @foo()
  ; 2. Write some data(123)
  store i8 123, i8* %reg1  
  ; 3. Read it
  %reg2 = load i8, i8* %reg1
  ; 4. Return it
  %reg3 = zext i8 %reg2 to i32
  ret i32 %reg3
}

Save this as bgc.ll and you can run it like this.

$ llc bgc.ll
$ cc -I/usr/local/Cellar/bdw-gc/7.6.0/include/ \
    -L/usr/local/Cellar/bdw-gc/7.6.0/lib/ -lgc \
        -o bgc bgc.s
$ ./bgc
$ echo $?
123

More posts

Posts

(more...)

Articles

(more...)

Category

Ads

About

About the author