bitwriter.go raw
1 //go:build !purego
2
3 //===- bitwriter.go - Bindings for bitwriter ------------------------------===//
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 bitwriter component.
12 //
13 //===----------------------------------------------------------------------===//
14
15 package llvm
16
17 /*
18 #include "llvm-c/BitWriter.h"
19 #include "backports.h"
20 #include <stdlib.h>
21 */
22 import "C"
23 import "os"
24 import "errors"
25
26 var writeBitcodeToFileErr = errors.New("Failed to write bitcode to file")
27
28 func WriteBitcodeToFile(m Module, file *os.File) error {
29 fail := C.LLVMWriteBitcodeToFD(m.C, C.int(file.Fd()), C.int(0), C.int(0))
30 if fail != 0 {
31 return writeBitcodeToFileErr
32 }
33 return nil
34 }
35
36 func WriteBitcodeToMemoryBuffer(m Module) MemoryBuffer {
37 mb := C.LLVMWriteBitcodeToMemoryBuffer(m.C)
38 return MemoryBuffer{mb}
39 }
40
41 func WriteThinLTOBitcodeToMemoryBuffer(m Module) MemoryBuffer {
42 mb := C.LLVMGoWriteThinLTOBitcodeToMemoryBuffer(m.C)
43 return MemoryBuffer{mb}
44 }
45
46 // TODO(nsf): Figure out way how to make it work with io.Writer
47