llvm.org GIT mirror llvm / e07c15c
add bitcode support git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36856 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
4 changed file(s) with 88 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
1212
1313 #include "ArchiveInternals.h"
1414 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/Support/Compressor.h"
17 #include "llvm/Support/MemoryBuffer.h"
1618 #include
1719 using namespace llvm;
20
21 static bool Bitcode = false;
1822
1923 /// Read a variable-bit-rate encoded unsigned integer
2024 inline unsigned readInteger(const char*&At, const char*End){
204208
205209 // Determine if this is a bytecode file
206210 switch (sys::IdentifyFileType(At, 4)) {
211 case sys::Bitcode_FileType:
207212 case sys::Bytecode_FileType:
208213 flags |= ArchiveMember::BytecodeFlag;
209214 break;
349354 if (I->isBytecode() || I->isCompressedBytecode()) {
350355 std::string FullMemberName = archPath.toString() +
351356 "(" + I->getPath().toString() + ")";
352 Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
353 I->getSize(), FullMemberName,
354 Compressor::decompressToNewBuffer,
355 ErrMessage);
357 Module *M;
358
359 if (Bitcode) {
360 MemoryBuffer *Buffer =
361 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
362 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
363
364 M = ParseBitcodeFile(Buffer, ErrMessage);
365 delete Buffer;
366 } else {
367 M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
368 I->getSize(), FullMemberName,
369 Compressor::decompressToNewBuffer,
370 ErrMessage);
371 }
356372 if (!M)
357373 return true;
358374
485501 // Now, load the bytecode module to get the ModuleProvider
486502 std::string FullMemberName = archPath.toString() + "(" +
487503 mbr->getPath().toString() + ")";
488 ModuleProvider* mp = getBytecodeBufferModuleProvider(
504 ModuleProvider* mp;
505 if (Bitcode) {
506 MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
507 FullMemberName.c_str());
508 memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
509
510 mp = getBitcodeModuleProvider(Buffer, ErrMsg);
511 } else
512 mp = getBytecodeBufferModuleProvider(
489513 (const unsigned char*) mbr->getData(), mbr->getSize(),
490514 FullMemberName, Decompressor, ErrMsg, 0);
491515 if (!mp)
611635
612636 std::string FullMemberName =
613637 archPath.toString() + "(" + I->getPath().toString() + ")";
614 Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
615 I->getSize(), FullMemberName);
638 Module *M;
639
640 if (Bitcode) {
641 MemoryBuffer *Buffer =
642 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
643 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
644 M = ParseBitcodeFile(Buffer);
645 delete Buffer;
646 } else {
647 M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
648 I->getSize(), FullMemberName);
649 }
616650 if (!M)
617651 return false; // Couldn't parse bytecode, not a bytecode archive.
618652 delete M;
1212
1313 #include "ArchiveInternals.h"
1414 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/Support/Compressor.h"
1617 #include "llvm/System/Signals.h"
1718 #include "llvm/System/Process.h"
177178 std::string magic;
178179 mbr->path.getMagicNumber(magic,4);
179180 switch (sys::IdentifyFileType(magic.c_str(),4)) {
181 case sys::Bitcode_FileType:
180182 case sys::Bytecode_FileType:
181183 flags |= ArchiveMember::BytecodeFlag;
182184 break;
260262 }
261263 }
262264
263 // Determine if we actually should compress this member
264 bool willCompress =
265 (ShouldCompress &&
266 !member.isCompressed() &&
267 !member.isCompressedBytecode() &&
268 !member.isLLVMSymbolTable() &&
269 !member.isSVR4SymbolTable() &&
270 !member.isBSD4SymbolTable());
271
272 // Perform the compression. Note that if the file is uncompressed bytecode
273 // then we turn the file into compressed bytecode rather than treating it as
274 // compressed data. This is necessary since it allows us to determine that the
275 // file contains bytecode instead of looking like a regular compressed data
276 // member. A compressed bytecode file has its content compressed but has a
277 // magic number of "llvc". This acounts for the +/-4 arithmetic in the code
278 // below.
279 int hdrSize;
280 if (willCompress) {
281 char* output = 0;
282 if (member.isBytecode()) {
283 data +=4;
284 fSize -= 4;
285 }
286 fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
287 if (fSize == 0)
288 return true;
289 data = output;
290 if (member.isBytecode())
291 hdrSize = -fSize-4;
292 else
293 hdrSize = -fSize;
294 } else {
295 hdrSize = fSize;
296 }
265 int hdrSize = fSize;
297266
298267 // Compute the fields of the header
299268 ArchiveMemberHeader Hdr;
308277 member.getPath().toString().length());
309278 }
310279
311 // Make sure we write the compressed bytecode magic number if we should.
312 if (willCompress && member.isBytecode())
313 ARFile.write("llvc",4);
314
315280 // Write the (possibly compressed) member's content to the file.
316281 ARFile.write(data,fSize);
317282
318283 // Make sure the member is an even length
319284 if ((ARFile.tellp() & 1) == 1)
320285 ARFile << ARFILE_PAD;
321
322 // Free the compressed data, if necessary
323 if (willCompress) {
324 free((void*)data);
325 }
326286
327287 // Close the mapped file if it was opened
328288 if (mFile != 0) {
1212
1313 #include "ArchiveInternals.h"
1414 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/Support/Compressor.h"
17 #include "llvm/Support/MemoryBuffer.h"
1618 #include
1719 using namespace llvm;
20
21 static bool Bitcode = false;
1822
1923 /// Read a variable-bit-rate encoded unsigned integer
2024 inline unsigned readInteger(const char*&At, const char*End){
204208
205209 // Determine if this is a bytecode file
206210 switch (sys::IdentifyFileType(At, 4)) {
211 case sys::Bitcode_FileType:
207212 case sys::Bytecode_FileType:
208213 flags |= ArchiveMember::BytecodeFlag;
209214 break;
349354 if (I->isBytecode() || I->isCompressedBytecode()) {
350355 std::string FullMemberName = archPath.toString() +
351356 "(" + I->getPath().toString() + ")";
352 Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
353 I->getSize(), FullMemberName,
354 Compressor::decompressToNewBuffer,
355 ErrMessage);
357 Module *M;
358
359 if (Bitcode) {
360 MemoryBuffer *Buffer =
361 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
362 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
363
364 M = ParseBitcodeFile(Buffer, ErrMessage);
365 delete Buffer;
366 } else {
367 M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
368 I->getSize(), FullMemberName,
369 Compressor::decompressToNewBuffer,
370 ErrMessage);
371 }
356372 if (!M)
357373 return true;
358374
485501 // Now, load the bytecode module to get the ModuleProvider
486502 std::string FullMemberName = archPath.toString() + "(" +
487503 mbr->getPath().toString() + ")";
488 ModuleProvider* mp = getBytecodeBufferModuleProvider(
504 ModuleProvider* mp;
505 if (Bitcode) {
506 MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
507 FullMemberName.c_str());
508 memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
509
510 mp = getBitcodeModuleProvider(Buffer, ErrMsg);
511 } else
512 mp = getBytecodeBufferModuleProvider(
489513 (const unsigned char*) mbr->getData(), mbr->getSize(),
490514 FullMemberName, Decompressor, ErrMsg, 0);
491515 if (!mp)
611635
612636 std::string FullMemberName =
613637 archPath.toString() + "(" + I->getPath().toString() + ")";
614 Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
615 I->getSize(), FullMemberName);
638 Module *M;
639
640 if (Bitcode) {
641 MemoryBuffer *Buffer =
642 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
643 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
644 M = ParseBitcodeFile(Buffer);
645 delete Buffer;
646 } else {
647 M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
648 I->getSize(), FullMemberName);
649 }
616650 if (!M)
617651 return false; // Couldn't parse bytecode, not a bytecode archive.
618652 delete M;
1212
1313 #include "ArchiveInternals.h"
1414 #include "llvm/Bytecode/Reader.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
1516 #include "llvm/Support/Compressor.h"
1617 #include "llvm/System/Signals.h"
1718 #include "llvm/System/Process.h"
177178 std::string magic;
178179 mbr->path.getMagicNumber(magic,4);
179180 switch (sys::IdentifyFileType(magic.c_str(),4)) {
181 case sys::Bitcode_FileType:
180182 case sys::Bytecode_FileType:
181183 flags |= ArchiveMember::BytecodeFlag;
182184 break;
260262 }
261263 }
262264
263 // Determine if we actually should compress this member
264 bool willCompress =
265 (ShouldCompress &&
266 !member.isCompressed() &&
267 !member.isCompressedBytecode() &&
268 !member.isLLVMSymbolTable() &&
269 !member.isSVR4SymbolTable() &&
270 !member.isBSD4SymbolTable());
271
272 // Perform the compression. Note that if the file is uncompressed bytecode
273 // then we turn the file into compressed bytecode rather than treating it as
274 // compressed data. This is necessary since it allows us to determine that the
275 // file contains bytecode instead of looking like a regular compressed data
276 // member. A compressed bytecode file has its content compressed but has a
277 // magic number of "llvc". This acounts for the +/-4 arithmetic in the code
278 // below.
279 int hdrSize;
280 if (willCompress) {
281 char* output = 0;
282 if (member.isBytecode()) {
283 data +=4;
284 fSize -= 4;
285 }
286 fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
287 if (fSize == 0)
288 return true;
289 data = output;
290 if (member.isBytecode())
291 hdrSize = -fSize-4;
292 else
293 hdrSize = -fSize;
294 } else {
295 hdrSize = fSize;
296 }
265 int hdrSize = fSize;
297266
298267 // Compute the fields of the header
299268 ArchiveMemberHeader Hdr;
308277 member.getPath().toString().length());
309278 }
310279
311 // Make sure we write the compressed bytecode magic number if we should.
312 if (willCompress && member.isBytecode())
313 ARFile.write("llvc",4);
314
315280 // Write the (possibly compressed) member's content to the file.
316281 ARFile.write(data,fSize);
317282
318283 // Make sure the member is an even length
319284 if ((ARFile.tellp() & 1) == 1)
320285 ARFile << ARFILE_PAD;
321
322 // Free the compressed data, if necessary
323 if (willCompress) {
324 free((void*)data);
325 }
326286
327287 // Close the mapped file if it was opened
328288 if (mFile != 0) {