bitreader.go raw

   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  
  13  package llvm
  14  
  15  /*
  16  #include "llvm-c/BitReader.h"
  17  #include "llvm-c/Core.h"
  18  #include <stdlib.h>
  19  */
  20  import "C"
  21  
  22  import (
  23  	"errors"
  24  	"unsafe"
  25  )
  26  
  27  // ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the specified
  28  // name, and returns a new LLVM module.
  29  func (c Context) 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.LLVMParseBitcodeInContext2(c.C, 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