Home | History | Annotate | Download | only in llvm
      1 //===- bitreader.go - Bindings for bitreader ------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines bindings for the bitreader component.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 package llvm
     15 
     16 /*
     17 #include "llvm-c/BitReader.h"
     18 #include "llvm-c/Core.h"
     19 #include <stdlib.h>
     20 */
     21 import "C"
     22 
     23 import (
     24 	"errors"
     25 	"unsafe"
     26 )
     27 
     28 // ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the
     29 // specified name, and returns a new LLVM module.
     30 func ParseBitcodeFile(name string) (Module, error) {
     31 	var buf C.LLVMMemoryBufferRef
     32 	var errmsg *C.char
     33 	var cfilename *C.char = C.CString(name)
     34 	defer C.free(unsafe.Pointer(cfilename))
     35 	result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
     36 	if result != 0 {
     37 		err := errors.New(C.GoString(errmsg))
     38 		C.free(unsafe.Pointer(errmsg))
     39 		return Module{}, err
     40 	}
     41 	defer C.LLVMDisposeMemoryBuffer(buf)
     42 
     43 	var m Module
     44 	if C.LLVMParseBitcode2(buf, &m.C) == 0 {
     45 		return m, nil
     46 	}
     47 
     48 	err := errors.New(C.GoString(errmsg))
     49 	C.free(unsafe.Pointer(errmsg))
     50 	return Module{}, err
     51 }
     52