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