blob: 218c694f2e4790d34365c040a4cc1d1c287f4084 [file] [log] [blame]
Misha Brukman12c29d12003-09-22 23:38:23 +00001#include "ReaderInternals.h"
2#include "Support/StringExtras.h"
3#include "Config/fcntl.h"
4#include "Config/unistd.h"
5#include "Config/sys/mman.h"
6
7#define CHECK_ALIGN32(begin,end) \
8 if (align32(begin,end)) \
9 throw std::string("Alignment error: ReaderWrappers.cpp:" + \
10 utostr((unsigned)__LINE__));
11
12namespace {
13
14 /// BytecodeFileReader - parses a bytecode file from a file
15 ///
16 class BytecodeFileReader : public BytecodeParser {
17 private:
18 unsigned char *Buffer;
19 int Length;
20
21 BytecodeFileReader(const BytecodeFileReader&); // Do not implement
22 void operator=(BytecodeFileReader &BFR); // Do not implement
23
24 public:
25 BytecodeFileReader(const std::string &Filename);
26 ~BytecodeFileReader();
27
28 };
29
30 /// BytecodeStdinReader - parses a bytecode file from stdin
31 ///
32 class BytecodeStdinReader : public BytecodeParser {
33 private:
34 std::vector<unsigned char> FileData;
35 unsigned char *FileBuf;
36
37 BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
38 void operator=(BytecodeStdinReader &BFR); // Do not implement
39
40 public:
41 BytecodeStdinReader();
42 ~BytecodeStdinReader();
43 };
44
45 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
46 /// when the object is destroyed.
47 ///
48 class FDHandle {
49 int FD;
50 public:
51 FDHandle(int fd) : FD(fd) {}
52 operator int() const { return FD; }
53 ~FDHandle() {
54 if (FD != -1) close(FD);
55 }
56 };
57}
58
59BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
60 FDHandle FD = open(Filename.c_str(), O_RDONLY);
61 if (FD == -1)
62 throw std::string("Error opening file!");
63
64 // Stat the file to get its length...
65 struct stat StatBuf;
66 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
67 throw std::string("Error stat'ing file!");
68
69 // mmap in the file all at once...
70 Length = StatBuf.st_size;
71 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
72 MAP_PRIVATE, FD, 0);
73 if (Buffer == (unsigned char*)MAP_FAILED)
74 throw std::string("Error mmapping file!");
75
76 // Parse the bytecode we mmapped in
77 ParseBytecode(Buffer, Length, Filename);
78}
79
80BytecodeFileReader::~BytecodeFileReader() {
81 // Unmmap the bytecode...
82 munmap((char*)Buffer, Length);
83}
84
85
86#define ALIGN_PTRS 0
87
88BytecodeStdinReader::BytecodeStdinReader() {
89 int BlockSize;
90 unsigned char Buffer[4096*4];
91
92 // Read in all of the data from stdin, we cannot mmap stdin...
93 while ((BlockSize = read(0 /*stdin*/, Buffer, 4096*4))) {
94 if (BlockSize == -1)
95 throw std::string("Error reading from stdin!");
96
97 FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
98 }
99
100 if (FileData.empty())
101 throw std::string("Standard Input empty!");
102
103#if ALIGN_PTRS
104 FileBuf = (unsigned char*)mmap(0, FileData.size(), PROT_READ|PROT_WRITE,
105 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
106 assert((Buf != (unsigned char*)-1) && "mmap returned error!");
107 memcpy(Buf, &FileData[0], FileData.size());
108#else
109 FileBuf = &FileData[0];
110#endif
111
112#if 0
113 // Allocate a new buffer to hold the bytecode...
114 unsigned char *ParseBegin=0;
115 unsigned Offset=0;
116 if ((intptr_t)Buffer & 3) {
117 delete [] Buffer;
118 Buffer = new unsigned char[Length+4];
119 Offset = 4-((intptr_t)Buffer & 3); // Make sure it's aligned
120 }
121 memcpy(Buffer+Offset, Buf, Length); // Copy it over
122 ParseBegin = Buffer+Offset;
123#endif
124
125 ParseBytecode(FileBuf, FileData.size(), "<stdin>");
126}
127
128BytecodeStdinReader::~BytecodeStdinReader() {
129#if ALIGN_PTRS
130 munmap((char*)FileBuf, FileData.size()); // Free mmap'd data area
131#endif
132}
133
134///
135///
136AbstractModuleProvider*
137getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length,
138 const std::string &ModuleID) {
139 CHECK_ALIGN32(Buffer, Buffer+Length);
140 BytecodeParser *Parser = new BytecodeParser();
141 Parser->ParseBytecode(Buffer, Length, ModuleID);
142 return Parser;
143}
144
145Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
146 const std::string &ModuleID, std::string *ErrorStr){
147 AbstractModuleProvider *AMP =
148 getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
149 Module *M = AMP->releaseModule();
150 delete AMP;
151 return M;
152}
153
154
155/// Parse and return a class file...
156///
157AbstractModuleProvider*
158getBytecodeModuleProvider(const std::string &Filename) {
159 if (Filename != std::string("-")) // Read from a file...
160 return new BytecodeFileReader(Filename);
161 else // Read from stdin
162 return new BytecodeStdinReader();
163}
164
165Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
166 AbstractModuleProvider *AMP = getBytecodeModuleProvider(Filename);
167 Module *M = AMP->releaseModule();
168 delete AMP;
169 return M;
170}