Document the maximum LLVM IR alignment, which is 1 << 29 or 0.5 GiB
[oota-llvm.git] / lib / IRReader / IRReader.cpp
1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IRReader/IRReader.h"
11 #include "llvm-c/Core.h"
12 #include "llvm-c/IRReader.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/Timer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <system_error>
22
23 using namespace llvm;
24
25 namespace llvm {
26   extern bool TimePassesIsEnabled;
27 }
28
29 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30 static const char *const TimeIRParsingName = "Parse IR";
31
32 static Module *getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
33                                LLVMContext &Context) {
34   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
35                 (const unsigned char *)Buffer->getBufferEnd())) {
36     std::string ErrMsg;
37     ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
38     if (std::error_code EC = ModuleOrErr.getError()) {
39       Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
40                          EC.message());
41       // getLazyBitcodeModule does not take ownership of the Buffer in the
42       // case of an error.
43       delete Buffer;
44       return nullptr;
45     }
46     return ModuleOrErr.get();
47   }
48
49   return ParseAssembly(Buffer, nullptr, Err, Context);
50 }
51
52 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
53                                   LLVMContext &Context) {
54   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55       MemoryBuffer::getFileOrSTDIN(Filename);
56   if (std::error_code EC = FileOrErr.getError()) {
57     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58                        "Could not open input file: " + EC.message());
59     return nullptr;
60   }
61
62   return getLazyIRModule(FileOrErr.get().release(), Err, Context);
63 }
64
65 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66                       LLVMContext &Context) {
67   NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
68                      TimePassesIsEnabled);
69   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70                 (const unsigned char *)Buffer->getBufferEnd())) {
71     ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
72     Module *M = nullptr;
73     if (std::error_code EC = ModuleOrErr.getError())
74       Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
75                          EC.message());
76     else
77       M = ModuleOrErr.get();
78     // parseBitcodeFile does not take ownership of the Buffer.
79     return M;
80   }
81
82   return ParseAssembly(MemoryBuffer::getMemBuffer(
83                            Buffer->getBuffer(), Buffer->getBufferIdentifier()),
84                        nullptr, Err, Context);
85 }
86
87 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
88                           LLVMContext &Context) {
89   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
90       MemoryBuffer::getFileOrSTDIN(Filename);
91   if (std::error_code EC = FileOrErr.getError()) {
92     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
93                        "Could not open input file: " + EC.message());
94     return nullptr;
95   }
96
97   return ParseIR(FileOrErr.get().get(), Err, Context);
98 }
99
100 //===----------------------------------------------------------------------===//
101 // C API.
102 //===----------------------------------------------------------------------===//
103
104 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
105                               LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
106                               char **OutMessage) {
107   SMDiagnostic Diag;
108
109   std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
110   *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));
111
112   if(!*OutM) {
113     if (OutMessage) {
114       std::string buf;
115       raw_string_ostream os(buf);
116
117       Diag.print(nullptr, os, false);
118       os.flush();
119
120       *OutMessage = strdup(buf.c_str());
121     }
122     return 1;
123   }
124
125   return 0;
126 }