0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 #include "llvm/IRReader/IRReader.h"
|
77
|
11 #include "llvm-c/Core.h"
|
|
12 #include "llvm-c/IRReader.h"
|
|
13 #include "llvm/AsmParser/Parser.h"
|
120
|
14 #include "llvm/Bitcode/BitcodeReader.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/IR/LLVMContext.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/IR/Module.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/Support/MemoryBuffer.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/Support/SourceMgr.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/Support/Timer.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/Support/raw_ostream.h"
|
77
|
21 #include <system_error>
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 namespace llvm {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 extern bool TimePassesIsEnabled;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28
|
120
|
29 static const char *const TimeIRParsingGroupName = "irparse";
|
|
30 static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
|
|
31 static const char *const TimeIRParsingName = "parse";
|
|
32 static const char *const TimeIRParsingDescription = "Parse IR";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33
|
77
|
34 static std::unique_ptr<Module>
|
|
35 getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
|
100
|
36 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 (const unsigned char *)Buffer->getBufferEnd())) {
|
120
|
39 Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
|
100
|
40 std::move(Buffer), Context, ShouldLazyLoadMetadata);
|
120
|
41 if (Error E = ModuleOrErr.takeError()) {
|
|
42 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
43 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
44 EIB.message());
|
|
45 });
|
77
|
46 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 }
|
95
|
48 return std::move(ModuleOrErr.get());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50
|
77
|
51 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53
|
77
|
54 std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
|
|
55 SMDiagnostic &Err,
|
100
|
56 LLVMContext &Context,
|
|
57 bool ShouldLazyLoadMetadata) {
|
77
|
58 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
59 MemoryBuffer::getFileOrSTDIN(Filename);
|
|
60 if (std::error_code EC = FileOrErr.getError()) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
77
|
62 "Could not open input file: " + EC.message());
|
|
63 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65
|
100
|
66 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
|
|
67 ShouldLazyLoadMetadata);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69
|
77
|
70 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
|
121
|
71 LLVMContext &Context,
|
|
72 bool UpgradeDebugInfo) {
|
120
|
73 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
|
|
74 TimeIRParsingGroupName, TimeIRParsingGroupDescription,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 TimePassesIsEnabled);
|
77
|
76 if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
|
|
77 (const unsigned char *)Buffer.getBufferEnd())) {
|
120
|
78 Expected<std::unique_ptr<Module>> ModuleOrErr =
|
95
|
79 parseBitcodeFile(Buffer, Context);
|
120
|
80 if (Error E = ModuleOrErr.takeError()) {
|
|
81 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
82 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
|
|
83 EIB.message());
|
|
84 });
|
77
|
85 return nullptr;
|
|
86 }
|
95
|
87 return std::move(ModuleOrErr.get());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89
|
121
|
90 return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92
|
77
|
93 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
|
121
|
94 LLVMContext &Context,
|
|
95 bool UpgradeDebugInfo) {
|
77
|
96 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
97 MemoryBuffer::getFileOrSTDIN(Filename);
|
|
98 if (std::error_code EC = FileOrErr.getError()) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
77
|
100 "Could not open input file: " + EC.message());
|
|
101 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103
|
121
|
104 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
|
|
105 UpgradeDebugInfo);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 // C API.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 char **OutMessage) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 SMDiagnostic Diag;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116
|
77
|
117 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
|
|
118 *OutM =
|
|
119 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 if(!*OutM) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 if (OutMessage) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123 std::string buf;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 raw_string_ostream os(buf);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125
|
77
|
126 Diag.print(nullptr, os, false);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
127 os.flush();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 *OutMessage = strdup(buf.c_str());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 return 1;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 return 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135 }
|