Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 1 | //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===// |
| 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 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/ADT/StringSwitch.h" |
| 13 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 16 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCSectionMachO.h" |
| 18 | #include "llvm/MC/MCStreamer.h" |
| 19 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/SourceMgr.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | /// \brief Implementation of directive handling which is shared across all |
| 27 | /// Darwin targets. |
| 28 | class DarwinAsmParser : public MCAsmParserExtension { |
Eli Bendersky | 171192f | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 29 | template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)> |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 30 | void addDirectiveHandler(StringRef Directive) { |
Eli Bendersky | 171192f | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 31 | MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair( |
| 32 | this, HandleDirective<DarwinAsmParser, HandlerMethod>); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 33 | getParser().addDirectiveHandler(Directive, Handler); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 36 | bool ParseSectionSwitch(const char *Segment, const char *Section, |
| 37 | unsigned TAA = 0, unsigned ImplicitAlign = 0, |
| 38 | unsigned StubSize = 0); |
| 39 | |
| 40 | public: |
| 41 | DarwinAsmParser() {} |
| 42 | |
| 43 | virtual void Initialize(MCAsmParser &Parser) { |
| 44 | // Call the base implementation. |
| 45 | this->MCAsmParserExtension::Initialize(Parser); |
| 46 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 47 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc"); |
Kevin Enderby | 4f066b6 | 2013-08-28 17:50:59 +0000 | [diff] [blame^] | 48 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveIndirectSymbol>( |
| 49 | ".indirect_symbol"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 50 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym"); |
| 51 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>( |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 52 | ".subsections_via_symbols"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 53 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump"); |
| 54 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load"); |
| 55 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section"); |
| 56 | addDirectiveHandler<&DarwinAsmParser::ParseDirectivePushSection>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 57 | ".pushsection"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 58 | addDirectiveHandler<&DarwinAsmParser::ParseDirectivePopSection>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 59 | ".popsection"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 60 | addDirectiveHandler<&DarwinAsmParser::ParseDirectivePrevious>(".previous"); |
| 61 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>( |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 62 | ".secure_log_unique"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 63 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>( |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 64 | ".secure_log_reset"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 65 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss"); |
| 66 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill"); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 67 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 68 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegion>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 69 | ".data_region"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 70 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegionEnd>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 71 | ".end_data_region"); |
Jim Grosbach | 3e96531 | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 73 | // Special section directives. |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 74 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const"); |
| 75 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 76 | ".const_data"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 77 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 78 | ".constructor"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 79 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 80 | ".cstring"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 81 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data"); |
| 82 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 83 | ".destructor"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 84 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld"); |
| 85 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 86 | ".fvmlib_init0"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 87 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 88 | ".fvmlib_init1"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 89 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 90 | &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>( |
| 91 | ".lazy_symbol_pointer"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 92 | addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLinkerOption>( |
Daniel Dunbar | cddd236 | 2013-01-18 01:25:48 +0000 | [diff] [blame] | 93 | ".linker_option"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 94 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 95 | ".literal16"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 96 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 97 | ".literal4"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 98 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 99 | ".literal8"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 100 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 101 | ".mod_init_func"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 102 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 103 | ".mod_term_func"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 104 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 105 | &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>( |
| 106 | ".non_lazy_symbol_pointer"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 107 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 108 | ".objc_cat_cls_meth"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 109 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 110 | ".objc_cat_inst_meth"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 111 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 112 | ".objc_category"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 113 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 114 | ".objc_class"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 115 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 116 | ".objc_class_names"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 117 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 118 | ".objc_class_vars"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 119 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 120 | ".objc_cls_meth"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 121 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 122 | ".objc_cls_refs"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 123 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 124 | ".objc_inst_meth"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 125 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 126 | &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>( |
| 127 | ".objc_instance_vars"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 128 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 129 | ".objc_message_refs"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 130 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 131 | ".objc_meta_class"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 132 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 133 | &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>( |
| 134 | ".objc_meth_var_names"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 135 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 136 | &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>( |
| 137 | ".objc_meth_var_types"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 138 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 139 | ".objc_module_info"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 140 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 141 | ".objc_protocol"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 142 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 143 | &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>( |
| 144 | ".objc_selector_strs"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 145 | addDirectiveHandler< |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 146 | &DarwinAsmParser::ParseSectionDirectiveObjCStringObject>( |
| 147 | ".objc_string_object"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 148 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 149 | ".objc_symbols"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 150 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 151 | ".picsymbol_stub"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 152 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 153 | ".static_const"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 154 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 155 | ".static_data"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 156 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 157 | ".symbol_stub"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 158 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata"); |
| 159 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text"); |
| 160 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>( |
Daniel Dunbar | 9b6a447 | 2013-01-18 01:25:25 +0000 | [diff] [blame] | 161 | ".thread_init_func"); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 162 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv"); |
Jim Grosbach | 8270da8 | 2011-03-08 19:17:19 +0000 | [diff] [blame] | 163 | |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 164 | addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveIdent>(".ident"); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool ParseDirectiveDesc(StringRef, SMLoc); |
Kevin Enderby | 4f066b6 | 2013-08-28 17:50:59 +0000 | [diff] [blame^] | 168 | bool ParseDirectiveIndirectSymbol(StringRef, SMLoc); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 169 | bool ParseDirectiveDumpOrLoad(StringRef, SMLoc); |
| 170 | bool ParseDirectiveLsym(StringRef, SMLoc); |
Daniel Dunbar | cddd236 | 2013-01-18 01:25:48 +0000 | [diff] [blame] | 171 | bool ParseDirectiveLinkerOption(StringRef, SMLoc); |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 172 | bool ParseDirectiveSection(StringRef, SMLoc); |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 173 | bool ParseDirectivePushSection(StringRef, SMLoc); |
| 174 | bool ParseDirectivePopSection(StringRef, SMLoc); |
| 175 | bool ParseDirectivePrevious(StringRef, SMLoc); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 176 | bool ParseDirectiveSecureLogReset(StringRef, SMLoc); |
| 177 | bool ParseDirectiveSecureLogUnique(StringRef, SMLoc); |
| 178 | bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc); |
| 179 | bool ParseDirectiveTBSS(StringRef, SMLoc); |
| 180 | bool ParseDirectiveZerofill(StringRef, SMLoc); |
Jim Grosbach | 3e96531 | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 181 | bool ParseDirectiveDataRegion(StringRef, SMLoc); |
| 182 | bool ParseDirectiveDataRegionEnd(StringRef, SMLoc); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 183 | |
| 184 | // Named Section Directive |
| 185 | bool ParseSectionDirectiveConst(StringRef, SMLoc) { |
| 186 | return ParseSectionSwitch("__TEXT", "__const"); |
| 187 | } |
| 188 | bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) { |
| 189 | return ParseSectionSwitch("__TEXT", "__static_const"); |
| 190 | } |
| 191 | bool ParseSectionDirectiveCString(StringRef, SMLoc) { |
| 192 | return ParseSectionSwitch("__TEXT","__cstring", |
| 193 | MCSectionMachO::S_CSTRING_LITERALS); |
| 194 | } |
| 195 | bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) { |
| 196 | return ParseSectionSwitch("__TEXT", "__literal4", |
| 197 | MCSectionMachO::S_4BYTE_LITERALS, 4); |
| 198 | } |
| 199 | bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) { |
| 200 | return ParseSectionSwitch("__TEXT", "__literal8", |
| 201 | MCSectionMachO::S_8BYTE_LITERALS, 8); |
| 202 | } |
| 203 | bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) { |
| 204 | return ParseSectionSwitch("__TEXT","__literal16", |
| 205 | MCSectionMachO::S_16BYTE_LITERALS, 16); |
| 206 | } |
| 207 | bool ParseSectionDirectiveConstructor(StringRef, SMLoc) { |
| 208 | return ParseSectionSwitch("__TEXT","__constructor"); |
| 209 | } |
| 210 | bool ParseSectionDirectiveDestructor(StringRef, SMLoc) { |
| 211 | return ParseSectionSwitch("__TEXT","__destructor"); |
| 212 | } |
| 213 | bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) { |
| 214 | return ParseSectionSwitch("__TEXT","__fvmlib_init0"); |
| 215 | } |
| 216 | bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) { |
| 217 | return ParseSectionSwitch("__TEXT","__fvmlib_init1"); |
| 218 | } |
| 219 | bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) { |
| 220 | return ParseSectionSwitch("__TEXT","__symbol_stub", |
| 221 | MCSectionMachO::S_SYMBOL_STUBS | |
| 222 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 223 | // FIXME: Different on PPC and ARM. |
| 224 | 0, 16); |
| 225 | } |
| 226 | bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) { |
| 227 | return ParseSectionSwitch("__TEXT","__picsymbol_stub", |
| 228 | MCSectionMachO::S_SYMBOL_STUBS | |
| 229 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26); |
| 230 | } |
| 231 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
| 232 | return ParseSectionSwitch("__DATA", "__data"); |
| 233 | } |
| 234 | bool ParseSectionDirectiveStaticData(StringRef, SMLoc) { |
| 235 | return ParseSectionSwitch("__DATA", "__static_data"); |
| 236 | } |
| 237 | bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) { |
| 238 | return ParseSectionSwitch("__DATA", "__nl_symbol_ptr", |
| 239 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4); |
| 240 | } |
| 241 | bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) { |
| 242 | return ParseSectionSwitch("__DATA", "__la_symbol_ptr", |
| 243 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4); |
| 244 | } |
| 245 | bool ParseSectionDirectiveDyld(StringRef, SMLoc) { |
| 246 | return ParseSectionSwitch("__DATA", "__dyld"); |
| 247 | } |
| 248 | bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) { |
| 249 | return ParseSectionSwitch("__DATA", "__mod_init_func", |
| 250 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4); |
| 251 | } |
| 252 | bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) { |
| 253 | return ParseSectionSwitch("__DATA", "__mod_term_func", |
| 254 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4); |
| 255 | } |
| 256 | bool ParseSectionDirectiveConstData(StringRef, SMLoc) { |
| 257 | return ParseSectionSwitch("__DATA", "__const"); |
| 258 | } |
| 259 | bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) { |
| 260 | return ParseSectionSwitch("__OBJC", "__class", |
| 261 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 262 | } |
| 263 | bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) { |
| 264 | return ParseSectionSwitch("__OBJC", "__meta_class", |
| 265 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 266 | } |
| 267 | bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) { |
| 268 | return ParseSectionSwitch("__OBJC", "__cat_cls_meth", |
| 269 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 270 | } |
| 271 | bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) { |
| 272 | return ParseSectionSwitch("__OBJC", "__cat_inst_meth", |
| 273 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 274 | } |
| 275 | bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) { |
| 276 | return ParseSectionSwitch("__OBJC", "__protocol", |
| 277 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 278 | } |
| 279 | bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) { |
| 280 | return ParseSectionSwitch("__OBJC", "__string_object", |
| 281 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 282 | } |
| 283 | bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) { |
| 284 | return ParseSectionSwitch("__OBJC", "__cls_meth", |
| 285 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 286 | } |
| 287 | bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) { |
| 288 | return ParseSectionSwitch("__OBJC", "__inst_meth", |
| 289 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 290 | } |
| 291 | bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) { |
| 292 | return ParseSectionSwitch("__OBJC", "__cls_refs", |
| 293 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 294 | MCSectionMachO::S_LITERAL_POINTERS, 4); |
| 295 | } |
| 296 | bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) { |
| 297 | return ParseSectionSwitch("__OBJC", "__message_refs", |
| 298 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 299 | MCSectionMachO::S_LITERAL_POINTERS, 4); |
| 300 | } |
| 301 | bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) { |
| 302 | return ParseSectionSwitch("__OBJC", "__symbols", |
| 303 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 304 | } |
| 305 | bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) { |
| 306 | return ParseSectionSwitch("__OBJC", "__category", |
| 307 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 308 | } |
| 309 | bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) { |
| 310 | return ParseSectionSwitch("__OBJC", "__class_vars", |
| 311 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 312 | } |
| 313 | bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) { |
| 314 | return ParseSectionSwitch("__OBJC", "__instance_vars", |
| 315 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 316 | } |
| 317 | bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) { |
| 318 | return ParseSectionSwitch("__OBJC", "__module_info", |
| 319 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 320 | } |
| 321 | bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) { |
| 322 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 323 | MCSectionMachO::S_CSTRING_LITERALS); |
| 324 | } |
| 325 | bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) { |
| 326 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 327 | MCSectionMachO::S_CSTRING_LITERALS); |
| 328 | } |
| 329 | bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) { |
| 330 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 331 | MCSectionMachO::S_CSTRING_LITERALS); |
| 332 | } |
| 333 | bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) { |
| 334 | return ParseSectionSwitch("__OBJC", "__selector_strs", |
| 335 | MCSectionMachO::S_CSTRING_LITERALS); |
| 336 | } |
| 337 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
| 338 | return ParseSectionSwitch("__DATA", "__thread_data", |
| 339 | MCSectionMachO::S_THREAD_LOCAL_REGULAR); |
| 340 | } |
| 341 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
| 342 | return ParseSectionSwitch("__TEXT", "__text", |
| 343 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
| 344 | } |
| 345 | bool ParseSectionDirectiveTLV(StringRef, SMLoc) { |
| 346 | return ParseSectionSwitch("__DATA", "__thread_vars", |
| 347 | MCSectionMachO::S_THREAD_LOCAL_VARIABLES); |
| 348 | } |
Jim Grosbach | 8270da8 | 2011-03-08 19:17:19 +0000 | [diff] [blame] | 349 | bool ParseSectionDirectiveIdent(StringRef, SMLoc) { |
| 350 | // Darwin silently ignores the .ident directive. |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 351 | getParser().eatToEndOfStatement(); |
Jim Grosbach | 8270da8 | 2011-03-08 19:17:19 +0000 | [diff] [blame] | 352 | return false; |
| 353 | } |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 354 | bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) { |
| 355 | return ParseSectionSwitch("__DATA", "__thread_init", |
| 356 | MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS); |
| 357 | } |
| 358 | |
| 359 | }; |
| 360 | |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 361 | } // end anonymous namespace |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 362 | |
| 363 | bool DarwinAsmParser::ParseSectionSwitch(const char *Segment, |
| 364 | const char *Section, |
| 365 | unsigned TAA, unsigned Align, |
| 366 | unsigned StubSize) { |
| 367 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 368 | return TokError("unexpected token in section switching directive"); |
| 369 | Lex(); |
| 370 | |
| 371 | // FIXME: Arch specific. |
Tim Northover | 1c2b2f9 | 2012-12-17 17:59:32 +0000 | [diff] [blame] | 372 | bool isText = TAA & MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS; |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 373 | getStreamer().SwitchSection(getContext().getMachOSection( |
| 374 | Segment, Section, TAA, StubSize, |
| 375 | isText ? SectionKind::getText() |
| 376 | : SectionKind::getDataRel())); |
| 377 | |
| 378 | // Set the implicit alignment, if any. |
| 379 | // |
| 380 | // FIXME: This isn't really what 'as' does; I think it just uses the implicit |
| 381 | // alignment on the section (e.g., if one manually inserts bytes into the |
Bill Wendling | cf2561d | 2010-10-19 10:18:23 +0000 | [diff] [blame] | 382 | // section, then just issuing the section switch directive will not realign |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 383 | // the section. However, this is arguably more reasonable behavior, and there |
| 384 | // is no good reason for someone to intentionally emit incorrectly sized |
| 385 | // values into the implicitly aligned sections. |
| 386 | if (Align) |
| 387 | getStreamer().EmitValueToAlignment(Align, 0, 1, 0); |
| 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | /// ParseDirectiveDesc |
| 393 | /// ::= .desc identifier , expression |
| 394 | bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) { |
| 395 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 396 | if (getParser().parseIdentifier(Name)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 397 | return TokError("expected identifier in directive"); |
| 398 | |
| 399 | // Handle the identifier as the key symbol. |
| 400 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 401 | |
| 402 | if (getLexer().isNot(AsmToken::Comma)) |
| 403 | return TokError("unexpected token in '.desc' directive"); |
| 404 | Lex(); |
| 405 | |
| 406 | int64_t DescValue; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 407 | if (getParser().parseAbsoluteExpression(DescValue)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 408 | return true; |
| 409 | |
| 410 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 411 | return TokError("unexpected token in '.desc' directive"); |
| 412 | |
| 413 | Lex(); |
| 414 | |
| 415 | // Set the n_desc field of this Symbol to this DescValue |
| 416 | getStreamer().EmitSymbolDesc(Sym, DescValue); |
| 417 | |
| 418 | return false; |
| 419 | } |
| 420 | |
Kevin Enderby | 4f066b6 | 2013-08-28 17:50:59 +0000 | [diff] [blame^] | 421 | /// ParseDirectiveIndirectSymbol |
| 422 | /// ::= .indirect_symbol identifier |
| 423 | bool DarwinAsmParser::ParseDirectiveIndirectSymbol(StringRef, SMLoc Loc) { |
| 424 | const MCSectionMachO *Current = static_cast<const MCSectionMachO*>( |
| 425 | getStreamer().getCurrentSection().first); |
| 426 | unsigned SectionType = Current->getType(); |
| 427 | if (SectionType != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS && |
| 428 | SectionType != MCSectionMachO::S_LAZY_SYMBOL_POINTERS && |
| 429 | SectionType != MCSectionMachO::S_SYMBOL_STUBS) |
| 430 | return Error(Loc, "indirect symbol not in a symbol pointer or stub " |
| 431 | "section"); |
| 432 | |
| 433 | StringRef Name; |
| 434 | if (getParser().parseIdentifier(Name)) |
| 435 | return TokError("expected identifier in .indirect_symbol directive"); |
| 436 | |
| 437 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 438 | |
| 439 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 440 | if (Sym->isTemporary()) |
| 441 | return TokError("non-local symbol required in directive"); |
| 442 | |
| 443 | if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol)) |
| 444 | return TokError("unable to emit indirect symbol attribute for: " + Name); |
| 445 | |
| 446 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 447 | return TokError("unexpected token in '.indirect_symbol' directive"); |
| 448 | |
| 449 | Lex(); |
| 450 | |
| 451 | return false; |
| 452 | } |
| 453 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 454 | /// ParseDirectiveDumpOrLoad |
| 455 | /// ::= ( .dump | .load ) "filename" |
| 456 | bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive, |
| 457 | SMLoc IDLoc) { |
| 458 | bool IsDump = Directive == ".dump"; |
| 459 | if (getLexer().isNot(AsmToken::String)) |
| 460 | return TokError("expected string in '.dump' or '.load' directive"); |
| 461 | |
| 462 | Lex(); |
| 463 | |
| 464 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 465 | return TokError("unexpected token in '.dump' or '.load' directive"); |
| 466 | |
| 467 | Lex(); |
| 468 | |
| 469 | // FIXME: If/when .dump and .load are implemented they will be done in the |
| 470 | // the assembly parser and not have any need for an MCStreamer API. |
| 471 | if (IsDump) |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 472 | return Warning(IDLoc, "ignoring directive .dump for now"); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 473 | else |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 474 | return Warning(IDLoc, "ignoring directive .load for now"); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Daniel Dunbar | cddd236 | 2013-01-18 01:25:48 +0000 | [diff] [blame] | 477 | /// ParseDirectiveLinkerOption |
| 478 | /// ::= .linker_option "string" ( , "string" )* |
| 479 | bool DarwinAsmParser::ParseDirectiveLinkerOption(StringRef IDVal, SMLoc) { |
| 480 | SmallVector<std::string, 4> Args; |
| 481 | for (;;) { |
| 482 | if (getLexer().isNot(AsmToken::String)) |
| 483 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
| 484 | |
| 485 | std::string Data; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 486 | if (getParser().parseEscapedString(Data)) |
Daniel Dunbar | cddd236 | 2013-01-18 01:25:48 +0000 | [diff] [blame] | 487 | return true; |
| 488 | |
| 489 | Args.push_back(Data); |
| 490 | |
| 491 | Lex(); |
| 492 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 493 | break; |
| 494 | |
| 495 | if (getLexer().isNot(AsmToken::Comma)) |
| 496 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 497 | Lex(); |
| 498 | } |
| 499 | |
| 500 | getStreamer().EmitLinkerOptions(Args); |
| 501 | return false; |
| 502 | } |
| 503 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 504 | /// ParseDirectiveLsym |
| 505 | /// ::= .lsym identifier , expression |
| 506 | bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) { |
| 507 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 508 | if (getParser().parseIdentifier(Name)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 509 | return TokError("expected identifier in directive"); |
| 510 | |
| 511 | // Handle the identifier as the key symbol. |
| 512 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 513 | |
| 514 | if (getLexer().isNot(AsmToken::Comma)) |
| 515 | return TokError("unexpected token in '.lsym' directive"); |
| 516 | Lex(); |
| 517 | |
| 518 | const MCExpr *Value; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 519 | if (getParser().parseExpression(Value)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 520 | return true; |
| 521 | |
| 522 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 523 | return TokError("unexpected token in '.lsym' directive"); |
| 524 | |
| 525 | Lex(); |
| 526 | |
| 527 | // We don't currently support this directive. |
| 528 | // |
| 529 | // FIXME: Diagnostic location! |
| 530 | (void) Sym; |
| 531 | return TokError("directive '.lsym' is unsupported"); |
| 532 | } |
| 533 | |
| 534 | /// ParseDirectiveSection: |
| 535 | /// ::= .section identifier (',' identifier)* |
Daniel Dunbar | 1edf6ca | 2010-07-18 22:22:07 +0000 | [diff] [blame] | 536 | bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) { |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 537 | SMLoc Loc = getLexer().getLoc(); |
| 538 | |
| 539 | StringRef SectionName; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 540 | if (getParser().parseIdentifier(SectionName)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 541 | return Error(Loc, "expected identifier after '.section' directive"); |
| 542 | |
| 543 | // Verify there is a following comma. |
| 544 | if (!getLexer().is(AsmToken::Comma)) |
| 545 | return TokError("unexpected token in '.section' directive"); |
| 546 | |
| 547 | std::string SectionSpec = SectionName; |
| 548 | SectionSpec += ","; |
| 549 | |
| 550 | // Add all the tokens until the end of the line, ParseSectionSpecifier will |
| 551 | // handle this. |
| 552 | StringRef EOL = getLexer().LexUntilEndOfStatement(); |
| 553 | SectionSpec.append(EOL.begin(), EOL.end()); |
| 554 | |
| 555 | Lex(); |
| 556 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 557 | return TokError("unexpected token in '.section' directive"); |
| 558 | Lex(); |
| 559 | |
| 560 | |
| 561 | StringRef Segment, Section; |
Daniel Dunbar | 8d06ffc | 2011-03-17 16:25:24 +0000 | [diff] [blame] | 562 | unsigned StubSize; |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 563 | unsigned TAA; |
| 564 | bool TAAParsed; |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 565 | std::string ErrorStr = |
| 566 | MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section, |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 567 | TAA, TAAParsed, StubSize); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 568 | |
| 569 | if (!ErrorStr.empty()) |
| 570 | return Error(Loc, ErrorStr.c_str()); |
| 571 | |
| 572 | // FIXME: Arch specific. |
| 573 | bool isText = Segment == "__TEXT"; // FIXME: Hack. |
| 574 | getStreamer().SwitchSection(getContext().getMachOSection( |
| 575 | Segment, Section, TAA, StubSize, |
| 576 | isText ? SectionKind::getText() |
| 577 | : SectionKind::getDataRel())); |
| 578 | return false; |
| 579 | } |
| 580 | |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 581 | /// ParseDirectivePushSection: |
| 582 | /// ::= .pushsection identifier (',' identifier)* |
| 583 | bool DarwinAsmParser::ParseDirectivePushSection(StringRef S, SMLoc Loc) { |
| 584 | getStreamer().PushSection(); |
| 585 | |
| 586 | if (ParseDirectiveSection(S, Loc)) { |
| 587 | getStreamer().PopSection(); |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | /// ParseDirectivePopSection: |
| 595 | /// ::= .popsection |
| 596 | bool DarwinAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { |
| 597 | if (!getStreamer().PopSection()) |
| 598 | return TokError(".popsection without corresponding .pushsection"); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | /// ParseDirectivePrevious: |
| 603 | /// ::= .previous |
| 604 | bool DarwinAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 605 | MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); |
| 606 | if (PreviousSection.first == NULL) |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 607 | return TokError(".previous without corresponding .section"); |
Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 608 | getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 609 | return false; |
| 610 | } |
| 611 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 612 | /// ParseDirectiveSecureLogUnique |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 613 | /// ::= .secure_log_unique ... message ... |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 614 | bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) { |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 615 | StringRef LogMessage = getParser().parseStringToEndOfStatement(); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 616 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 617 | return TokError("unexpected token in '.secure_log_unique' directive"); |
| 618 | |
| 619 | if (getContext().getSecureLogUsed() != false) |
| 620 | return Error(IDLoc, ".secure_log_unique specified multiple times"); |
| 621 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 622 | // Get the secure log path. |
| 623 | const char *SecureLogFile = getContext().getSecureLogFile(); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 624 | if (SecureLogFile == NULL) |
| 625 | return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE " |
| 626 | "environment variable unset."); |
| 627 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 628 | // Open the secure log file if we haven't already. |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 629 | raw_ostream *OS = getContext().getSecureLog(); |
| 630 | if (OS == NULL) { |
| 631 | std::string Err; |
Rafael Espindola | c1b49b5 | 2013-07-16 19:44:17 +0000 | [diff] [blame] | 632 | OS = new raw_fd_ostream(SecureLogFile, Err, sys::fs::F_Append); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 633 | if (!Err.empty()) { |
| 634 | delete OS; |
| 635 | return Error(IDLoc, Twine("can't open secure log file: ") + |
| 636 | SecureLogFile + " (" + Err + ")"); |
| 637 | } |
| 638 | getContext().setSecureLog(OS); |
| 639 | } |
| 640 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 641 | // Write the message. |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 642 | int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc); |
| 643 | *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 644 | << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":" |
| 645 | << LogMessage + "\n"; |
| 646 | |
| 647 | getContext().setSecureLogUsed(true); |
| 648 | |
| 649 | return false; |
| 650 | } |
| 651 | |
| 652 | /// ParseDirectiveSecureLogReset |
| 653 | /// ::= .secure_log_reset |
| 654 | bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) { |
| 655 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 656 | return TokError("unexpected token in '.secure_log_reset' directive"); |
| 657 | |
| 658 | Lex(); |
| 659 | |
| 660 | getContext().setSecureLogUsed(false); |
| 661 | |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | /// ParseDirectiveSubsectionsViaSymbols |
| 666 | /// ::= .subsections_via_symbols |
| 667 | bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) { |
| 668 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 669 | return TokError("unexpected token in '.subsections_via_symbols' directive"); |
| 670 | |
| 671 | Lex(); |
| 672 | |
| 673 | getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); |
| 674 | |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | /// ParseDirectiveTBSS |
| 679 | /// ::= .tbss identifier, size, align |
| 680 | bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) { |
| 681 | SMLoc IDLoc = getLexer().getLoc(); |
| 682 | StringRef Name; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 683 | if (getParser().parseIdentifier(Name)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 684 | return TokError("expected identifier in directive"); |
| 685 | |
| 686 | // Handle the identifier as the key symbol. |
| 687 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 688 | |
| 689 | if (getLexer().isNot(AsmToken::Comma)) |
| 690 | return TokError("unexpected token in directive"); |
| 691 | Lex(); |
| 692 | |
| 693 | int64_t Size; |
| 694 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 695 | if (getParser().parseAbsoluteExpression(Size)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 696 | return true; |
| 697 | |
| 698 | int64_t Pow2Alignment = 0; |
| 699 | SMLoc Pow2AlignmentLoc; |
| 700 | if (getLexer().is(AsmToken::Comma)) { |
| 701 | Lex(); |
| 702 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 703 | if (getParser().parseAbsoluteExpression(Pow2Alignment)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 704 | return true; |
| 705 | } |
| 706 | |
| 707 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 708 | return TokError("unexpected token in '.tbss' directive"); |
| 709 | |
| 710 | Lex(); |
| 711 | |
| 712 | if (Size < 0) |
| 713 | return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than" |
| 714 | "zero"); |
| 715 | |
| 716 | // FIXME: Diagnose overflow. |
| 717 | if (Pow2Alignment < 0) |
| 718 | return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less" |
| 719 | "than zero"); |
| 720 | |
| 721 | if (!Sym->isUndefined()) |
| 722 | return Error(IDLoc, "invalid symbol redefinition"); |
| 723 | |
| 724 | getStreamer().EmitTBSSSymbol(getContext().getMachOSection( |
| 725 | "__DATA", "__thread_bss", |
| 726 | MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, |
| 727 | 0, SectionKind::getThreadBSS()), |
| 728 | Sym, Size, 1 << Pow2Alignment); |
| 729 | |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | /// ParseDirectiveZerofill |
| 734 | /// ::= .zerofill segname , sectname [, identifier , size_expression [ |
| 735 | /// , align_expression ]] |
| 736 | bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) { |
| 737 | StringRef Segment; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 738 | if (getParser().parseIdentifier(Segment)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 739 | return TokError("expected segment name after '.zerofill' directive"); |
| 740 | |
| 741 | if (getLexer().isNot(AsmToken::Comma)) |
| 742 | return TokError("unexpected token in directive"); |
| 743 | Lex(); |
| 744 | |
| 745 | StringRef Section; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 746 | if (getParser().parseIdentifier(Section)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 747 | return TokError("expected section name after comma in '.zerofill' " |
| 748 | "directive"); |
| 749 | |
| 750 | // If this is the end of the line all that was wanted was to create the |
| 751 | // the section but with no symbol. |
| 752 | if (getLexer().is(AsmToken::EndOfStatement)) { |
| 753 | // Create the zerofill section but no symbol |
| 754 | getStreamer().EmitZerofill(getContext().getMachOSection( |
| 755 | Segment, Section, MCSectionMachO::S_ZEROFILL, |
| 756 | 0, SectionKind::getBSS())); |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | if (getLexer().isNot(AsmToken::Comma)) |
| 761 | return TokError("unexpected token in directive"); |
| 762 | Lex(); |
| 763 | |
| 764 | SMLoc IDLoc = getLexer().getLoc(); |
| 765 | StringRef IDStr; |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 766 | if (getParser().parseIdentifier(IDStr)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 767 | return TokError("expected identifier in directive"); |
| 768 | |
| 769 | // handle the identifier as the key symbol. |
| 770 | MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr); |
| 771 | |
| 772 | if (getLexer().isNot(AsmToken::Comma)) |
| 773 | return TokError("unexpected token in directive"); |
| 774 | Lex(); |
| 775 | |
| 776 | int64_t Size; |
| 777 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 778 | if (getParser().parseAbsoluteExpression(Size)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 779 | return true; |
| 780 | |
| 781 | int64_t Pow2Alignment = 0; |
| 782 | SMLoc Pow2AlignmentLoc; |
| 783 | if (getLexer().is(AsmToken::Comma)) { |
| 784 | Lex(); |
| 785 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 786 | if (getParser().parseAbsoluteExpression(Pow2Alignment)) |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 787 | return true; |
| 788 | } |
| 789 | |
| 790 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 791 | return TokError("unexpected token in '.zerofill' directive"); |
| 792 | |
| 793 | Lex(); |
| 794 | |
| 795 | if (Size < 0) |
| 796 | return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " |
| 797 | "than zero"); |
| 798 | |
| 799 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
| 800 | // may internally end up wanting an alignment in bytes. |
| 801 | // FIXME: Diagnose overflow. |
| 802 | if (Pow2Alignment < 0) |
| 803 | return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, " |
| 804 | "can't be less than zero"); |
| 805 | |
| 806 | if (!Sym->isUndefined()) |
| 807 | return Error(IDLoc, "invalid symbol redefinition"); |
| 808 | |
| 809 | // Create the zerofill Symbol with Size and Pow2Alignment |
| 810 | // |
| 811 | // FIXME: Arch specific. |
| 812 | getStreamer().EmitZerofill(getContext().getMachOSection( |
| 813 | Segment, Section, MCSectionMachO::S_ZEROFILL, |
| 814 | 0, SectionKind::getBSS()), |
| 815 | Sym, Size, 1 << Pow2Alignment); |
| 816 | |
| 817 | return false; |
| 818 | } |
| 819 | |
Jim Grosbach | 3e96531 | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 820 | /// ParseDirectiveDataRegion |
| 821 | /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ] |
| 822 | bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) { |
| 823 | if (getLexer().is(AsmToken::EndOfStatement)) { |
| 824 | Lex(); |
| 825 | getStreamer().EmitDataRegion(MCDR_DataRegion); |
| 826 | return false; |
| 827 | } |
| 828 | StringRef RegionType; |
| 829 | SMLoc Loc = getParser().getTok().getLoc(); |
Jim Grosbach | cb2ae3d | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 830 | if (getParser().parseIdentifier(RegionType)) |
Jim Grosbach | 3e96531 | 2012-05-18 19:12:01 +0000 | [diff] [blame] | 831 | return TokError("expected region type after '.data_region' directive"); |
| 832 | int Kind = StringSwitch<int>(RegionType) |
| 833 | .Case("jt8", MCDR_DataRegionJT8) |
| 834 | .Case("jt16", MCDR_DataRegionJT16) |
| 835 | .Case("jt32", MCDR_DataRegionJT32) |
| 836 | .Default(-1); |
| 837 | if (Kind == -1) |
| 838 | return Error(Loc, "unknown region type in '.data_region' directive"); |
| 839 | Lex(); |
| 840 | |
| 841 | getStreamer().EmitDataRegion((MCDataRegionType)Kind); |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | /// ParseDirectiveDataRegionEnd |
| 846 | /// ::= .end_data_region |
| 847 | bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) { |
| 848 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 849 | return TokError("unexpected token in '.end_data_region' directive"); |
| 850 | |
| 851 | Lex(); |
| 852 | getStreamer().EmitDataRegion(MCDR_DataRegionEnd); |
| 853 | return false; |
| 854 | } |
| 855 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 856 | namespace llvm { |
| 857 | |
| 858 | MCAsmParserExtension *createDarwinAsmParser() { |
| 859 | return new DarwinAsmParser; |
| 860 | } |
| 861 | |
Bill Wendling | ef92055 | 2012-08-08 06:30:30 +0000 | [diff] [blame] | 862 | } // end llvm namespace |