45 : _module(m), _target(t),
46 _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), NULL),
53 sys::fs::file_magic::bitcode;
60 return type == sys::fs::file_magic::bitcode;
66 const char *triplePrefix) {
70 return isTargetMatch(buffer, triplePrefix);
74 const char *triplePrefix) {
76 if (MemoryBuffer::getFile(path, buffer))
78 return isTargetMatch(buffer.
take(), triplePrefix);
83 bool LTOModule::isTargetMatch(
MemoryBuffer *buffer,
const char *triplePrefix) {
86 return strncmp(Triple.c_str(), triplePrefix,
strlen(triplePrefix)) == 0;
92 std::string &errMsg) {
94 if (
error_code ec = MemoryBuffer::getFile(path, buffer)) {
95 errMsg = ec.message();
103 std::string &errMsg) {
111 std::string &errMsg) {
114 MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
115 errMsg = ec.message();
123 std::string &errMsg) {
132 std::string &errMsg) {
141 std::string TripleStr = m->getTargetTriple();
142 if (TripleStr.empty())
147 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
154 std::string FeatureStr = Features.
getString();
157 if (Triple.isOSDarwin()) {
166 m->MaterializeAllPermanently();
169 if (Ret->parseSymbols(errMsg)) {
178 MemoryBuffer *LTOModule::makeBuffer(
const void *mem,
size_t length) {
179 const char *startPtr = (
const char*)mem;
180 return MemoryBuffer::getMemBuffer(
StringRef(startPtr, length),
"",
false);
185 LTOModule::objcClassNameFromExpression(
const Constant *c, std::string &name) {
186 if (
const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
189 Constant *cn = gvn->getInitializer();
191 if (ca->isCString()) {
192 name =
".objc_class_name_" + ca->getAsCString().str();
207 std::string superclassName;
208 if (objcClassNameFromExpression(c->
getOperand(1), superclassName)) {
209 NameAndAttributes
info;
212 if (!entry.getValue().name) {
213 const char *symbolName = entry.getKey().data();
214 info.name = symbolName;
216 info.isFunction =
false;
218 entry.setValue(info);
223 std::string className;
224 if (objcClassNameFromExpression(c->
getOperand(2), className)) {
228 NameAndAttributes
info;
229 info.name = entry.getKey().data();
232 info.isFunction =
false;
234 _symbols.push_back(info);
244 std::string targetclassName;
245 if (!objcClassNameFromExpression(c->
getOperand(1), targetclassName))
248 NameAndAttributes
info;
252 if (entry.getValue().name)
255 const char *symbolName = entry.getKey().data();
256 info.name = symbolName;
258 info.isFunction =
false;
260 entry.setValue(info);
265 std::string targetclassName;
266 if (!objcClassNameFromExpression(clgv->
getInitializer(), targetclassName))
269 NameAndAttributes
info;
272 if (entry.getValue().name)
275 const char *symbolName = entry.getKey().data();
276 info.name = symbolName;
278 info.isFunction =
false;
280 entry.setValue(info);
284 void LTOModule::addDefinedDataSymbol(
const GlobalValue *v) {
286 addDefinedSymbol(v,
false);
312 if (v->
getSection().compare(0, 15,
"__OBJC,__class,") == 0) {
319 else if (v->
getSection().compare(0, 18,
"__OBJC,__category,") == 0) {
326 else if (v->
getSection().compare(0, 18,
"__OBJC,__cls_refs,") == 0) {
334 void LTOModule::addDefinedFunctionSymbol(
const Function *f) {
336 addDefinedSymbol(f,
true);
342 if (L != GlobalValue::LinkOnceODRLinkage)
349 if (GlobalStatus::analyzeGlobal(GV, GS))
356 void LTOModule::addDefinedSymbol(
const GlobalValue *def,
bool isFunction) {
407 NameAndAttributes
info;
409 info.name = Name.
data();
410 assert(info.name[Name.
size()] ==
'\0');
411 info.attributes = attr;
412 info.isFunction = isFunction;
416 _symbols.push_back(info);
421 void LTOModule::addAsmGlobalSymbol(
const char *name,
426 if (entry.getValue())
431 NameAndAttributes &info = _undefines[entry.getKey().data()];
433 if (info.symbol == 0) {
443 info.name = entry.getKey().data();
446 info.isFunction =
false;
450 _symbols.push_back(info);
455 addDefinedFunctionSymbol(cast<Function>(info.symbol));
457 addDefinedDataSymbol(info.symbol);
460 _symbols.back().attributes |= scope;
465 void LTOModule::addAsmGlobalSymbolUndef(
const char *name) {
469 _asm_undefines.push_back(entry.getKey().data());
472 if (entry.getValue().name)
477 NameAndAttributes
info;
478 info.name = entry.getKey().data();
479 info.attributes = attr;
480 info.isFunction =
false;
483 entry.setValue(info);
489 LTOModule::addPotentialUndefinedSymbol(
const GlobalValue *decl,
bool isFunc) {
495 if (isa<GlobalAlias>(decl))
505 if (entry.getValue().name)
508 NameAndAttributes
info;
510 info.name = entry.getKey().data();
517 info.isFunction = isFunc;
520 entry.setValue(info);
526 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used };
532 State &S = Symbols[Symbol.
getName()];
545 void markGlobal(
const MCSymbol &Symbol) {
546 State &S = Symbols[Symbol.
getName()];
560 void markUsed(
const MCSymbol &Symbol) {
561 State &S = Symbols[Symbol.
getName()];
585 case MCExpr::Binary: {
587 AddValueSymbols(BE->
getLHS());
588 AddValueSymbols(BE->
getRHS());
592 case MCExpr::SymbolRef:
593 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
597 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
605 const_iterator
begin() {
606 return Symbols.begin();
609 const_iterator
end() {
610 return Symbols.end();
623 markDefined(*Symbol);
630 markDefined(*Symbol);
639 markDefined(*Symbol);
643 markDefined(*Symbol);
652 const MCExpr *Subsection) {}
674 unsigned MaxBytesToEmit) {}
676 unsigned MaxBytesToEmit) {}
678 unsigned char Value ) {
return false; }
683 unsigned PointerSize) {}
693 bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
695 if (inlineAsm.empty())
699 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
713 errMsg =
"target " + std::string(T.
getName()) +
714 " does not define AsmParser.";
718 Parser->setTargetParser(*TAP);
719 if (Parser->Run(
false))
723 e = Streamer->end(); i != e; ++i) {
725 RecordStreamer::State Value = i->second;
726 if (Value == RecordStreamer::DefinedGlobal)
728 else if (Value == RecordStreamer::Defined)
730 else if (Value == RecordStreamer::Global ||
731 Value == RecordStreamer::Used)
732 addAsmGlobalSymbolUndef(Key.
data());
751 bool LTOModule::parseSymbols(std::string &errMsg) {
755 addPotentialUndefinedSymbol(f,
true);
757 addDefinedFunctionSymbol(f);
764 addPotentialUndefinedSymbol(v,
false);
766 addDefinedDataSymbol(v);
770 if (addAsmGlobalSymbols(errMsg))
778 addPotentialUndefinedSymbol(a,
false);
780 addDefinedDataSymbol(a);
785 e = _undefines.
end(); u != e; ++u) {
788 if (_defines.
count(u->getKey()))
continue;
789 NameAndAttributes info = u->getValue();
790 _symbols.push_back(info);
virtual void EmitULEB128Value(const MCExpr *Value)=0
const_iterator end(StringRef path)
Get end iterator over path.
LinkageTypes getLinkage() const
virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize)=0
size_t size() const
size - Get the string size.
const char * getName() const
getName - Get the target name.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
Module * getLazyBitcodeModule(MemoryBuffer *Buffer, LLVMContext &Context, std::string *ErrMsg=0)
The main container class for the LLVM Intermediate Representation.
unsigned getAlignment() const
void setValue(const ValueTy &V)
virtual void InitSections()=0
InitSections - Create the default sections and set the initial one.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, bool isImplicitlyPrivate, bool UseGlobalPrefix=true)
std::string getDefaultTargetTriple()
virtual void EmitValueImpl(const MCExpr *Value, unsigned Size)=0
virtual void EmitFileDirective(StringRef Filename)=0
const StringRef getTargetFeatureString() const
const StringRef getTargetCPU() const
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol=0, uint64_t Size=0, unsigned ByteAlignment=0)=0
static bool isBitcodeFileForTarget(const void *mem, size_t length, const char *triplePrefix)
bool hasAvailableExternallyLinkage() const
virtual void EmitInstruction(const MCInst &Inst)=0
const_iterator begin(StringRef path)
Get begin iterator over path.
virtual void EmitBundleAlignMode(unsigned AlignPow2)=0
Set the bundle alignment mode from now on in the section. The argument is the power of 2 to which the...
virtual void EmitThumbFunc(MCSymbol *Func)=0
const Constant * getInitializer() const
COFF::SymbolStorageClass StorageClass
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)=0
StringRef getName() const
MCInstrInfo * createMCInstrInfo() const
bool hasCommonLinkage() const
const MCAsmInfo * getMCAsmInfo() const
static bool isDeclaration(const GlobalValue &V)
isDeclaration - Return 'true' if the global value is a declaration.
bool hasLinkerPrivateWeakLinkage() const
MCSectionSubPair getCurrentSection() const
virtual void EmitBytes(StringRef Data)=0
size_type count(StringRef Key) const
std::string getBitcodeTargetTriple(MemoryBuffer *Buffer, LLVMContext &Context, std::string *ErrMsg=0)
global_iterator global_begin()
const char * data() const
virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame)
enable_if_c< std::numeric_limits< T >::is_integer &&!std::numeric_limits< T >::is_signed, std::size_t >::type countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
bool isMaterializable() const
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol)=0
static bool isBitcodeFile(const void *mem, size_t length)
const MCExpr * getExpr() const
static bool canBeHidden(const GlobalValue *GV)
const MCExpr * getLHS() const
getLHS - Get the left-hand side expression of the binary operator.
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag)=0
EmitAssemblerFlag - Note in the output the specified Flag.
alias_iterator alias_end()
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment)=0
virtual void EmitSLEB128Value(const MCExpr *Value)=0
LLVM Constant Representation.
bool hasHiddenVisibility() const
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value)=0
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute)=0
EmitSymbolAttribute - Add the given Attribute to Symbol.
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &)
Create an MCAsmParser instance.
Value * getOperand(unsigned i) const
const std::string & getSection() const
virtual void EmitBundleLock(bool AlignToEnd)=0
The following instructions are a bundle-locked group.
bool hasWeakLinkage() const
MCBinaryExpr - Binary assembler expressions.
std::string getString() const
Features string accessors.
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)=0
MCSubtargetInfo * createMCSubtargetInfo(StringRef Triple, StringRef CPU, StringRef Features) const
virtual void FinishImpl()=0
FinishImpl - Streamer specific finalization.
const std::string & getModuleInlineAsm() const
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
global_iterator global_end()
TargetMachine * createTargetMachine(StringRef Triple, StringRef CPU, StringRef Features, const TargetOptions &Options, Reloc::Model RM=Reloc::Default, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
bool hasExternalWeakLinkage() const
bool hasExternalLinkage() const
virtual void EmitCOFFSymbolType(int Type)=0
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
virtual void EmitLabel(MCSymbol *Symbol)
MCTargetAsmParser * createMCAsmParser(MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII) const
virtual void EmitDebugLabel(MCSymbol *Symbol)
StringMapEntry< uint8_t > value_type
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment=0)=0
alias_iterator alias_begin()
size_t strlen(const char *s);
LinkageTypes
An enumeration for the kinds of linkage for global values.
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue)=0
virtual bool EmitValueToOffset(const MCExpr *Offset, unsigned char Value=0)=0
virtual void EmitBundleUnlock()=0
Ends a bundle-locked group.
const StringRef getTargetTriple() const
virtual void EndCOFFSymbolDef()=0
EndCOFFSymbolDef - Marks the end of the symbol definition.
const MCExpr * getRHS() const
getRHS - Get the right-hand side expression of the binary operator.
MapEntryTy & GetOrCreateValue(StringRef Key, InitTy Val)
bool hasLinkOnceLinkage() const
StringRef getName() const
getName - Get the symbol name.
bool IsCompared
True if the global's address is used in a comparison.
bool isDeclaration() const
void RecordProcEnd(MCDwarfFrameInfo &Frame)
virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol)=0
unsigned getNumOperands() const
void setSection(const MCSection &S)
setSection - Mark the symbol as defined in the section S.
bool hasProtectedVisibility() const
static LTOModule * makeLTOModule(const char *path, llvm::TargetOptions options, std::string &errMsg)
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment)=0
LLVM Value Representation.
bool hasUnnamedAddr() const
virtual void ChangeSection(const MCSection *, const MCExpr *)=0
virtual void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit=0)=0
const Target & getTarget() const
int strncmp(const char *s1, const char *s2, size_t n);
Represents a location in source code.
virtual void EmitCOFFSymbolStorageClass(int StorageClass)=0
size_t AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc)
virtual void InitToTextSection()=0
InitToTextSection - Create a text section and switch the streamer to it.
LLVMContext & getGlobalContext()
const MCOperand & getOperand(unsigned i) const