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