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