Recommit r310809 with a fix for the spill problem
[llvm-core.git] / bindings / go / llvm / bitreader.go
blobc3bf07a19044bed6b12f51830726ed0726a73cb8
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.
12 //===----------------------------------------------------------------------===//
14 package llvm
17 #include "llvm-c/BitReader.h"
18 #include "llvm-c/Core.h"
19 #include <stdlib.h>
21 import "C"
23 import (
24 "errors"
25 "unsafe"
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
41 defer C.LLVMDisposeMemoryBuffer(buf)
43 var m Module
44 if C.LLVMParseBitcode2(buf, &m.C) == 0 {
45 return m, nil
48 err := errors.New(C.GoString(errmsg))
49 C.free(unsafe.Pointer(errmsg))
50 return Module{}, err