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