zpu: wip - very crude stack slot pass
[llvm/zpu.git] / lib / Support / MemoryBuffer.cpp
blob542162d513b9f73c7b91be881b90416fb7a1973c
1 //===--- MemoryBuffer.cpp - Memory Buffer implementation ------------------===//
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 // This file implements the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/MathExtras.h"
18 #include "llvm/System/Errno.h"
19 #include "llvm/System/Path.h"
20 #include "llvm/System/Process.h"
21 #include "llvm/System/Program.h"
22 #include <cassert>
23 #include <cstdio>
24 #include <cstring>
25 #include <cerrno>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #if !defined(_MSC_VER) && !defined(__MINGW32__)
29 #include <unistd.h>
30 #include <sys/uio.h>
31 #else
32 #include <io.h>
33 #endif
34 #include <fcntl.h>
35 using namespace llvm;
37 //===----------------------------------------------------------------------===//
38 // MemoryBuffer implementation itself.
39 //===----------------------------------------------------------------------===//
41 MemoryBuffer::~MemoryBuffer() { }
43 /// init - Initialize this MemoryBuffer as a reference to externally allocated
44 /// memory, memory that we know is already null terminated.
45 void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
46 assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
47 BufferStart = BufStart;
48 BufferEnd = BufEnd;
51 //===----------------------------------------------------------------------===//
52 // MemoryBufferMem implementation.
53 //===----------------------------------------------------------------------===//
55 /// CopyStringRef - Copies contents of a StringRef into a block of memory and
56 /// null-terminates it.
57 static void CopyStringRef(char *Memory, StringRef Data) {
58 memcpy(Memory, Data.data(), Data.size());
59 Memory[Data.size()] = 0; // Null terminate string.
62 /// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it.
63 template <typename T>
64 static T* GetNamedBuffer(StringRef Buffer, StringRef Name) {
65 char *Mem = static_cast<char*>(operator new(sizeof(T) + Name.size() + 1));
66 CopyStringRef(Mem + sizeof(T), Name);
67 return new (Mem) T(Buffer);
70 namespace {
71 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
72 class MemoryBufferMem : public MemoryBuffer {
73 public:
74 MemoryBufferMem(StringRef InputData) {
75 init(InputData.begin(), InputData.end());
78 virtual const char *getBufferIdentifier() const {
79 // The name is stored after the class itself.
80 return reinterpret_cast<const char*>(this + 1);
85 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
86 /// that EndPtr[0] must be a null byte and be accessible!
87 MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
88 StringRef BufferName) {
89 return GetNamedBuffer<MemoryBufferMem>(InputData, BufferName);
92 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
93 /// copying the contents and taking ownership of it. This has no requirements
94 /// on EndPtr[0].
95 MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
96 StringRef BufferName) {
97 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
98 if (!Buf) return 0;
99 memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
100 InputData.size());
101 return Buf;
104 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
105 /// that is not initialized. Note that the caller should initialize the
106 /// memory allocated by this method. The memory is owned by the MemoryBuffer
107 /// object.
108 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
109 StringRef BufferName) {
110 // Allocate space for the MemoryBuffer, the data and the name. It is important
111 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
112 size_t AlignedStringLen =
113 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1,
114 sizeof(void*)); // TODO: Is sizeof(void*) enough?
115 size_t RealLen = AlignedStringLen + Size + 1;
116 char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
117 if (!Mem) return 0;
119 // The name is stored after the class itself.
120 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
122 // The buffer begins after the name and must be aligned.
123 char *Buf = Mem + AlignedStringLen;
124 Buf[Size] = 0; // Null terminate buffer.
126 return new (Mem) MemoryBufferMem(StringRef(Buf, Size));
129 /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
130 /// is completely initialized to zeros. Note that the caller should
131 /// initialize the memory allocated by this method. The memory is owned by
132 /// the MemoryBuffer object.
133 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
134 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
135 if (!SB) return 0;
136 memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
137 return SB;
141 /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
142 /// if the Filename is "-". If an error occurs, this returns null and fills
143 /// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
144 /// returns an empty buffer.
145 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
146 std::string *ErrStr,
147 int64_t FileSize,
148 struct stat *FileInfo) {
149 if (Filename == "-")
150 return getSTDIN(ErrStr);
151 return getFile(Filename, ErrStr, FileSize, FileInfo);
154 MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
155 std::string *ErrStr,
156 int64_t FileSize,
157 struct stat *FileInfo) {
158 if (strcmp(Filename, "-") == 0)
159 return getSTDIN(ErrStr);
160 return getFile(Filename, ErrStr, FileSize, FileInfo);
163 //===----------------------------------------------------------------------===//
164 // MemoryBuffer::getFile implementation.
165 //===----------------------------------------------------------------------===//
167 namespace {
168 /// MemoryBufferMMapFile - This represents a file that was mapped in with the
169 /// sys::Path::MapInFilePages method. When destroyed, it calls the
170 /// sys::Path::UnMapFilePages method.
171 class MemoryBufferMMapFile : public MemoryBufferMem {
172 public:
173 MemoryBufferMMapFile(StringRef Buffer)
174 : MemoryBufferMem(Buffer) { }
176 ~MemoryBufferMMapFile() {
177 sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
181 /// FileCloser - RAII object to make sure an FD gets closed properly.
182 class FileCloser {
183 int FD;
184 public:
185 explicit FileCloser(int FD) : FD(FD) {}
186 ~FileCloser() { ::close(FD); }
190 MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, std::string *ErrStr,
191 int64_t FileSize, struct stat *FileInfo) {
192 SmallString<256> PathBuf(Filename.begin(), Filename.end());
193 return MemoryBuffer::getFile(PathBuf.c_str(), ErrStr, FileSize, FileInfo);
196 MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
197 int64_t FileSize, struct stat *FileInfo) {
198 int OpenFlags = O_RDONLY;
199 #ifdef O_BINARY
200 OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
201 #endif
202 int FD = ::open(Filename, OpenFlags);
203 if (FD == -1) {
204 if (ErrStr) *ErrStr = sys::StrError();
205 return 0;
207 FileCloser FC(FD); // Close FD on return.
209 // If we don't know the file size, use fstat to find out. fstat on an open
210 // file descriptor is cheaper than stat on a random path.
211 if (FileSize == -1 || FileInfo) {
212 struct stat MyFileInfo;
213 struct stat *FileInfoPtr = FileInfo? FileInfo : &MyFileInfo;
215 // TODO: This should use fstat64 when available.
216 if (fstat(FD, FileInfoPtr) == -1) {
217 if (ErrStr) *ErrStr = sys::StrError();
218 return 0;
220 FileSize = FileInfoPtr->st_size;
224 // If the file is large, try to use mmap to read it in. We don't use mmap
225 // for small files, because this can severely fragment our address space. Also
226 // don't try to map files that are exactly a multiple of the system page size,
227 // as the file would not have the required null terminator.
229 // FIXME: Can we just mmap an extra page in the latter case?
230 if (FileSize >= 4096*4 &&
231 (FileSize & (sys::Process::GetPageSize()-1)) != 0) {
232 if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
233 return GetNamedBuffer<MemoryBufferMMapFile>(StringRef(Pages, FileSize),
234 Filename);
238 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(FileSize, Filename);
239 if (!Buf) {
240 // Failed to create a buffer.
241 if (ErrStr) *ErrStr = "could not allocate buffer";
242 return 0;
245 OwningPtr<MemoryBuffer> SB(Buf);
246 char *BufPtr = const_cast<char*>(SB->getBufferStart());
248 size_t BytesLeft = FileSize;
249 while (BytesLeft) {
250 ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
251 if (NumRead == -1) {
252 if (errno == EINTR)
253 continue;
254 // Error while reading.
255 if (ErrStr) *ErrStr = sys::StrError();
256 return 0;
257 } else if (NumRead == 0) {
258 // We hit EOF early, truncate and terminate buffer.
259 Buf->BufferEnd = BufPtr;
260 *BufPtr = 0;
261 return SB.take();
263 BytesLeft -= NumRead;
264 BufPtr += NumRead;
267 return SB.take();
270 //===----------------------------------------------------------------------===//
271 // MemoryBuffer::getSTDIN implementation.
272 //===----------------------------------------------------------------------===//
274 MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) {
275 // Read in all of the data from stdin, we cannot mmap stdin.
277 // FIXME: That isn't necessarily true, we should try to mmap stdin and
278 // fallback if it fails.
279 sys::Program::ChangeStdinToBinary();
281 const ssize_t ChunkSize = 4096*4;
282 SmallString<ChunkSize> Buffer;
283 ssize_t ReadBytes;
284 // Read into Buffer until we hit EOF.
285 do {
286 Buffer.reserve(Buffer.size() + ChunkSize);
287 ReadBytes = read(0, Buffer.end(), ChunkSize);
288 if (ReadBytes == -1) {
289 if (errno == EINTR) continue;
290 if (ErrStr) *ErrStr = sys::StrError();
291 return 0;
293 Buffer.set_size(Buffer.size() + ReadBytes);
294 } while (ReadBytes != 0);
296 return getMemBufferCopy(Buffer, "<stdin>");