bitreader.go raw
1 //go:build !purego
2
3 //===- bitreader.go - Bindings for bitreader ------------------------------===//
4 //
5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6 // See https://llvm.org/LICENSE.txt for license information.
7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file defines bindings for the bitreader component.
12 //
13 //===----------------------------------------------------------------------===//
14
15 package llvm
16
17 /*
18 #include "llvm-c/BitReader.h"
19 #include "llvm-c/Core.h"
20 #include <stdlib.h>
21 */
22 import "C"
23
24 import (
25 "errors"
26 "unsafe"
27 )
28
29 // ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the specified
30 // name, and returns a new LLVM module.
31 func (c Context) ParseBitcodeFile(name string) (Module, error) {
32 var buf C.LLVMMemoryBufferRef
33 var errmsg *C.char
34 var cfilename *C.char = C.CString(name)
35 defer C.free(unsafe.Pointer(cfilename))
36 result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
37 if result != 0 {
38 err := errors.New(C.GoString(errmsg))
39 C.free(unsafe.Pointer(errmsg))
40 return Module{}, err
41 }
42 defer C.LLVMDisposeMemoryBuffer(buf)
43
44 var m Module
45 if C.LLVMParseBitcodeInContext2(c.C, buf, &m.C) == 0 {
46 return m, nil
47 }
48
49 err := errors.New(C.GoString(errmsg))
50 C.free(unsafe.Pointer(errmsg))
51 return Module{}, err
52 }
53