Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class implements the parser for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | be343b3 | 2010-01-22 01:58:08 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCParser/AsmParser.h" |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Bill Wendling | 9bc0af8 | 2009-12-28 01:34:57 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SourceMgr.h" |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetAsmParser.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
| 36 | /// \brief Generic implementations of directive handling, etc. which is shared |
| 37 | /// (or the default, at least) for all assembler parser. |
| 38 | class GenericAsmParser : public MCAsmParserExtension { |
| 39 | public: |
| 40 | GenericAsmParser() {} |
| 41 | |
| 42 | virtual void Initialize(MCAsmParser &Parser) { |
| 43 | // Call the base implementation. |
| 44 | this->MCAsmParserExtension::Initialize(Parser); |
| 45 | |
| 46 | // Debugging directives. |
| 47 | Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler( |
| 48 | &GenericAsmParser::ParseDirectiveFile)); |
| 49 | Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler( |
| 50 | &GenericAsmParser::ParseDirectiveLine)); |
| 51 | Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler( |
| 52 | &GenericAsmParser::ParseDirectiveLoc)); |
| 53 | } |
| 54 | |
| 55 | bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file" |
| 56 | bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line" |
| 57 | bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc" |
| 58 | }; |
| 59 | |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 60 | /// \brief Implementation of directive handling which is shared across all |
| 61 | /// Darwin targets. |
| 62 | class DarwinAsmParser : public MCAsmParserExtension { |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 63 | bool ParseSectionSwitch(const char *Segment, const char *Section, |
| 64 | unsigned TAA = 0, unsigned ImplicitAlign = 0, |
| 65 | unsigned StubSize = 0); |
| 66 | |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 67 | public: |
| 68 | DarwinAsmParser() {} |
| 69 | |
| 70 | virtual void Initialize(MCAsmParser &Parser) { |
| 71 | // Call the base implementation. |
| 72 | this->MCAsmParserExtension::Initialize(Parser); |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 73 | |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 74 | Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler( |
| 75 | &DarwinAsmParser::ParseDirectiveDesc)); |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 76 | Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler( |
| 77 | &DarwinAsmParser::ParseDirectiveLsym)); |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 78 | Parser.AddDirectiveHandler(this, ".subsections_via_symbols", |
| 79 | MCAsmParser::DirectiveHandler( |
| 80 | &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols)); |
| 81 | Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler( |
| 82 | &DarwinAsmParser::ParseDirectiveDumpOrLoad)); |
| 83 | Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler( |
| 84 | &DarwinAsmParser::ParseDirectiveDumpOrLoad)); |
| 85 | Parser.AddDirectiveHandler(this, ".secure_log_unique", |
| 86 | MCAsmParser::DirectiveHandler( |
| 87 | &DarwinAsmParser::ParseDirectiveSecureLogUnique)); |
| 88 | Parser.AddDirectiveHandler(this, ".secure_log_reset", |
| 89 | MCAsmParser::DirectiveHandler( |
| 90 | &DarwinAsmParser::ParseDirectiveSecureLogReset)); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 91 | Parser.AddDirectiveHandler(this, ".tbss", |
| 92 | MCAsmParser::DirectiveHandler( |
| 93 | &DarwinAsmParser::ParseDirectiveTBSS)); |
| 94 | Parser.AddDirectiveHandler(this, ".zerofill", |
| 95 | MCAsmParser::DirectiveHandler( |
| 96 | &DarwinAsmParser::ParseDirectiveZerofill)); |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 97 | |
| 98 | // Special section directives. |
| 99 | Parser.AddDirectiveHandler(this, ".const", |
| 100 | MCAsmParser::DirectiveHandler( |
| 101 | &DarwinAsmParser::ParseSectionDirectiveConst)); |
| 102 | Parser.AddDirectiveHandler(this, ".const_data", |
| 103 | MCAsmParser::DirectiveHandler( |
| 104 | &DarwinAsmParser::ParseSectionDirectiveConstData)); |
| 105 | Parser.AddDirectiveHandler(this, ".constructor", |
| 106 | MCAsmParser::DirectiveHandler( |
| 107 | &DarwinAsmParser::ParseSectionDirectiveConstructor)); |
| 108 | Parser.AddDirectiveHandler(this, ".cstring", |
| 109 | MCAsmParser::DirectiveHandler( |
| 110 | &DarwinAsmParser::ParseSectionDirectiveCString)); |
| 111 | Parser.AddDirectiveHandler(this, ".data", |
| 112 | MCAsmParser::DirectiveHandler( |
| 113 | &DarwinAsmParser::ParseSectionDirectiveData)); |
| 114 | Parser.AddDirectiveHandler(this, ".destructor", |
| 115 | MCAsmParser::DirectiveHandler( |
| 116 | &DarwinAsmParser::ParseSectionDirectiveDestructor)); |
| 117 | Parser.AddDirectiveHandler(this, ".dyld", |
| 118 | MCAsmParser::DirectiveHandler( |
| 119 | &DarwinAsmParser::ParseSectionDirectiveDyld)); |
| 120 | Parser.AddDirectiveHandler(this, ".fvmlib_init0", |
| 121 | MCAsmParser::DirectiveHandler( |
| 122 | &DarwinAsmParser::ParseSectionDirectiveFVMLibInit0)); |
| 123 | Parser.AddDirectiveHandler(this, ".fvmlib_init1", |
| 124 | MCAsmParser::DirectiveHandler( |
| 125 | &DarwinAsmParser::ParseSectionDirectiveFVMLibInit1)); |
| 126 | Parser.AddDirectiveHandler(this, ".lazy_symbol_pointer", |
| 127 | MCAsmParser::DirectiveHandler( |
| 128 | &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers)); |
| 129 | Parser.AddDirectiveHandler(this, ".literal16", |
| 130 | MCAsmParser::DirectiveHandler( |
| 131 | &DarwinAsmParser::ParseSectionDirectiveLiteral16)); |
| 132 | Parser.AddDirectiveHandler(this, ".literal4", |
| 133 | MCAsmParser::DirectiveHandler( |
| 134 | &DarwinAsmParser::ParseSectionDirectiveLiteral4)); |
| 135 | Parser.AddDirectiveHandler(this, ".literal8", |
| 136 | MCAsmParser::DirectiveHandler( |
| 137 | &DarwinAsmParser::ParseSectionDirectiveLiteral8)); |
| 138 | Parser.AddDirectiveHandler(this, ".mod_init_func", |
| 139 | MCAsmParser::DirectiveHandler( |
| 140 | &DarwinAsmParser::ParseSectionDirectiveModInitFunc)); |
| 141 | Parser.AddDirectiveHandler(this, ".mod_term_func", |
| 142 | MCAsmParser::DirectiveHandler( |
| 143 | &DarwinAsmParser::ParseSectionDirectiveModTermFunc)); |
| 144 | Parser.AddDirectiveHandler(this, ".non_lazy_symbol_pointer", |
| 145 | MCAsmParser::DirectiveHandler( |
| 146 | &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers)); |
| 147 | Parser.AddDirectiveHandler(this, ".objc_cat_cls_meth", |
| 148 | MCAsmParser::DirectiveHandler( |
| 149 | &DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth)); |
| 150 | Parser.AddDirectiveHandler(this, ".objc_cat_inst_meth", |
| 151 | MCAsmParser::DirectiveHandler( |
| 152 | &DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth)); |
| 153 | Parser.AddDirectiveHandler(this, ".objc_category", |
| 154 | MCAsmParser::DirectiveHandler( |
| 155 | &DarwinAsmParser::ParseSectionDirectiveObjCCategory)); |
| 156 | Parser.AddDirectiveHandler(this, ".objc_class", |
| 157 | MCAsmParser::DirectiveHandler( |
| 158 | &DarwinAsmParser::ParseSectionDirectiveObjCClass)); |
| 159 | Parser.AddDirectiveHandler(this, ".objc_class_names", |
| 160 | MCAsmParser::DirectiveHandler( |
| 161 | &DarwinAsmParser::ParseSectionDirectiveObjCClassNames)); |
| 162 | Parser.AddDirectiveHandler(this, ".objc_class_vars", |
| 163 | MCAsmParser::DirectiveHandler( |
| 164 | &DarwinAsmParser::ParseSectionDirectiveObjCClassVars)); |
| 165 | Parser.AddDirectiveHandler(this, ".objc_cls_meth", |
| 166 | MCAsmParser::DirectiveHandler( |
| 167 | &DarwinAsmParser::ParseSectionDirectiveObjCClsMeth)); |
| 168 | Parser.AddDirectiveHandler(this, ".objc_cls_refs", |
| 169 | MCAsmParser::DirectiveHandler( |
| 170 | &DarwinAsmParser::ParseSectionDirectiveObjCClsRefs)); |
| 171 | Parser.AddDirectiveHandler(this, ".objc_inst_meth", |
| 172 | MCAsmParser::DirectiveHandler( |
| 173 | &DarwinAsmParser::ParseSectionDirectiveObjCInstMeth)); |
| 174 | Parser.AddDirectiveHandler(this, ".objc_instance_vars", |
| 175 | MCAsmParser::DirectiveHandler( |
| 176 | &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars)); |
| 177 | Parser.AddDirectiveHandler(this, ".objc_message_refs", |
| 178 | MCAsmParser::DirectiveHandler( |
| 179 | &DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs)); |
| 180 | Parser.AddDirectiveHandler(this, ".objc_meta_class", |
| 181 | MCAsmParser::DirectiveHandler( |
| 182 | &DarwinAsmParser::ParseSectionDirectiveObjCMetaClass)); |
| 183 | Parser.AddDirectiveHandler(this, ".objc_meth_var_names", |
| 184 | MCAsmParser::DirectiveHandler( |
| 185 | &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames)); |
| 186 | Parser.AddDirectiveHandler(this, ".objc_meth_var_types", |
| 187 | MCAsmParser::DirectiveHandler( |
| 188 | &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes)); |
| 189 | Parser.AddDirectiveHandler(this, ".objc_module_info", |
| 190 | MCAsmParser::DirectiveHandler( |
| 191 | &DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo)); |
| 192 | Parser.AddDirectiveHandler(this, ".objc_protocol", |
| 193 | MCAsmParser::DirectiveHandler( |
| 194 | &DarwinAsmParser::ParseSectionDirectiveObjCProtocol)); |
| 195 | Parser.AddDirectiveHandler(this, ".objc_selector_strs", |
| 196 | MCAsmParser::DirectiveHandler( |
| 197 | &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs)); |
| 198 | Parser.AddDirectiveHandler(this, ".objc_string_object", |
| 199 | MCAsmParser::DirectiveHandler( |
| 200 | &DarwinAsmParser::ParseSectionDirectiveObjCStringObject)); |
| 201 | Parser.AddDirectiveHandler(this, ".objc_symbols", |
| 202 | MCAsmParser::DirectiveHandler( |
| 203 | &DarwinAsmParser::ParseSectionDirectiveObjCSymbols)); |
| 204 | Parser.AddDirectiveHandler(this, ".picsymbol_stub", |
| 205 | MCAsmParser::DirectiveHandler( |
| 206 | &DarwinAsmParser::ParseSectionDirectivePICSymbolStub)); |
| 207 | Parser.AddDirectiveHandler(this, ".static_const", |
| 208 | MCAsmParser::DirectiveHandler( |
| 209 | &DarwinAsmParser::ParseSectionDirectiveStaticConst)); |
| 210 | Parser.AddDirectiveHandler(this, ".static_data", |
| 211 | MCAsmParser::DirectiveHandler( |
| 212 | &DarwinAsmParser::ParseSectionDirectiveStaticData)); |
| 213 | Parser.AddDirectiveHandler(this, ".symbol_stub", |
| 214 | MCAsmParser::DirectiveHandler( |
| 215 | &DarwinAsmParser::ParseSectionDirectiveSymbolStub)); |
| 216 | Parser.AddDirectiveHandler(this, ".tdata", |
| 217 | MCAsmParser::DirectiveHandler( |
| 218 | &DarwinAsmParser::ParseSectionDirectiveTData)); |
| 219 | Parser.AddDirectiveHandler(this, ".text", |
| 220 | MCAsmParser::DirectiveHandler( |
| 221 | &DarwinAsmParser::ParseSectionDirectiveText)); |
| 222 | Parser.AddDirectiveHandler(this, ".thread_init_func", |
| 223 | MCAsmParser::DirectiveHandler( |
| 224 | &DarwinAsmParser::ParseSectionDirectiveThreadInitFunc)); |
| 225 | Parser.AddDirectiveHandler(this, ".tlv", |
| 226 | MCAsmParser::DirectiveHandler( |
| 227 | &DarwinAsmParser::ParseSectionDirectiveTLV)); |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 228 | } |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 229 | |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 230 | bool ParseDirectiveDesc(StringRef, SMLoc); |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 231 | bool ParseDirectiveDumpOrLoad(StringRef, SMLoc); |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 232 | bool ParseDirectiveLsym(StringRef, SMLoc); |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 233 | bool ParseDirectiveSecureLogReset(StringRef, SMLoc); |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 234 | bool ParseDirectiveSecureLogUnique(StringRef, SMLoc); |
| 235 | bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 236 | bool ParseDirectiveTBSS(StringRef, SMLoc); |
| 237 | bool ParseDirectiveZerofill(StringRef, SMLoc); |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 238 | |
| 239 | // Named Section Directive |
| 240 | bool ParseSectionDirectiveConst(StringRef, SMLoc) { |
| 241 | return ParseSectionSwitch("__TEXT", "__const"); |
| 242 | } |
| 243 | bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) { |
| 244 | return ParseSectionSwitch("__TEXT", "__static_const"); |
| 245 | } |
| 246 | bool ParseSectionDirectiveCString(StringRef, SMLoc) { |
| 247 | return ParseSectionSwitch("__TEXT","__cstring", |
| 248 | MCSectionMachO::S_CSTRING_LITERALS); |
| 249 | } |
| 250 | bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) { |
| 251 | return ParseSectionSwitch("__TEXT", "__literal4", |
| 252 | MCSectionMachO::S_4BYTE_LITERALS, 4); |
| 253 | } |
| 254 | bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) { |
| 255 | return ParseSectionSwitch("__TEXT", "__literal8", |
| 256 | MCSectionMachO::S_8BYTE_LITERALS, 8); |
| 257 | } |
| 258 | bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) { |
| 259 | return ParseSectionSwitch("__TEXT","__literal16", |
| 260 | MCSectionMachO::S_16BYTE_LITERALS, 16); |
| 261 | } |
| 262 | bool ParseSectionDirectiveConstructor(StringRef, SMLoc) { |
| 263 | return ParseSectionSwitch("__TEXT","__constructor"); |
| 264 | } |
| 265 | bool ParseSectionDirectiveDestructor(StringRef, SMLoc) { |
| 266 | return ParseSectionSwitch("__TEXT","__destructor"); |
| 267 | } |
| 268 | bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) { |
| 269 | return ParseSectionSwitch("__TEXT","__fvmlib_init0"); |
| 270 | } |
| 271 | bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) { |
| 272 | return ParseSectionSwitch("__TEXT","__fvmlib_init1"); |
| 273 | } |
| 274 | bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) { |
| 275 | return ParseSectionSwitch("__TEXT","__symbol_stub", |
| 276 | MCSectionMachO::S_SYMBOL_STUBS | |
| 277 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 278 | // FIXME: Different on PPC and ARM. |
| 279 | 0, 16); |
| 280 | } |
| 281 | bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) { |
| 282 | return ParseSectionSwitch("__TEXT","__picsymbol_stub", |
| 283 | MCSectionMachO::S_SYMBOL_STUBS | |
| 284 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26); |
| 285 | } |
| 286 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
| 287 | return ParseSectionSwitch("__DATA", "__data"); |
| 288 | } |
| 289 | bool ParseSectionDirectiveStaticData(StringRef, SMLoc) { |
| 290 | return ParseSectionSwitch("__DATA", "__static_data"); |
| 291 | } |
| 292 | bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) { |
| 293 | return ParseSectionSwitch("__DATA", "__nl_symbol_ptr", |
| 294 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4); |
| 295 | } |
| 296 | bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) { |
| 297 | return ParseSectionSwitch("__DATA", "__la_symbol_ptr", |
| 298 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4); |
| 299 | } |
| 300 | bool ParseSectionDirectiveDyld(StringRef, SMLoc) { |
| 301 | return ParseSectionSwitch("__DATA", "__dyld"); |
| 302 | } |
| 303 | bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) { |
| 304 | return ParseSectionSwitch("__DATA", "__mod_init_func", |
| 305 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4); |
| 306 | } |
| 307 | bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) { |
| 308 | return ParseSectionSwitch("__DATA", "__mod_term_func", |
| 309 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4); |
| 310 | } |
| 311 | bool ParseSectionDirectiveConstData(StringRef, SMLoc) { |
| 312 | return ParseSectionSwitch("__DATA", "__const"); |
| 313 | } |
| 314 | bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) { |
| 315 | return ParseSectionSwitch("__OBJC", "__class", |
| 316 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 317 | } |
| 318 | bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) { |
| 319 | return ParseSectionSwitch("__OBJC", "__meta_class", |
| 320 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 321 | } |
| 322 | bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) { |
| 323 | return ParseSectionSwitch("__OBJC", "__cat_cls_meth", |
| 324 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 325 | } |
| 326 | bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) { |
| 327 | return ParseSectionSwitch("__OBJC", "__cat_inst_meth", |
| 328 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 329 | } |
| 330 | bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) { |
| 331 | return ParseSectionSwitch("__OBJC", "__protocol", |
| 332 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 333 | } |
| 334 | bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) { |
| 335 | return ParseSectionSwitch("__OBJC", "__string_object", |
| 336 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 337 | } |
| 338 | bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) { |
| 339 | return ParseSectionSwitch("__OBJC", "__cls_meth", |
| 340 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 341 | } |
| 342 | bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) { |
| 343 | return ParseSectionSwitch("__OBJC", "__inst_meth", |
| 344 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 345 | } |
| 346 | bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) { |
| 347 | return ParseSectionSwitch("__OBJC", "__cls_refs", |
| 348 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 349 | MCSectionMachO::S_LITERAL_POINTERS, 4); |
| 350 | } |
| 351 | bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) { |
| 352 | return ParseSectionSwitch("__OBJC", "__message_refs", |
| 353 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP | |
| 354 | MCSectionMachO::S_LITERAL_POINTERS, 4); |
| 355 | } |
| 356 | bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) { |
| 357 | return ParseSectionSwitch("__OBJC", "__symbols", |
| 358 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 359 | } |
| 360 | bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) { |
| 361 | return ParseSectionSwitch("__OBJC", "__category", |
| 362 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 363 | } |
| 364 | bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) { |
| 365 | return ParseSectionSwitch("__OBJC", "__class_vars", |
| 366 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 367 | } |
| 368 | bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) { |
| 369 | return ParseSectionSwitch("__OBJC", "__instance_vars", |
| 370 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 371 | } |
| 372 | bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) { |
| 373 | return ParseSectionSwitch("__OBJC", "__module_info", |
| 374 | MCSectionMachO::S_ATTR_NO_DEAD_STRIP); |
| 375 | } |
| 376 | bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) { |
| 377 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 378 | MCSectionMachO::S_CSTRING_LITERALS); |
| 379 | } |
| 380 | bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) { |
| 381 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 382 | MCSectionMachO::S_CSTRING_LITERALS); |
| 383 | } |
| 384 | bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) { |
| 385 | return ParseSectionSwitch("__TEXT", "__cstring", |
| 386 | MCSectionMachO::S_CSTRING_LITERALS); |
| 387 | } |
| 388 | bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) { |
| 389 | return ParseSectionSwitch("__OBJC", "__selector_strs", |
| 390 | MCSectionMachO::S_CSTRING_LITERALS); |
| 391 | } |
| 392 | bool ParseSectionDirectiveTData(StringRef, SMLoc) { |
| 393 | return ParseSectionSwitch("__DATA", "__thread_data", |
| 394 | MCSectionMachO::S_THREAD_LOCAL_REGULAR); |
| 395 | } |
| 396 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
| 397 | return ParseSectionSwitch("__TEXT", "__text", |
| 398 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
| 399 | } |
| 400 | bool ParseSectionDirectiveTLV(StringRef, SMLoc) { |
| 401 | return ParseSectionSwitch("__DATA", "__thread_vars", |
| 402 | MCSectionMachO::S_THREAD_LOCAL_VARIABLES); |
| 403 | } |
| 404 | bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) { |
| 405 | return ParseSectionSwitch("__DATA", "__thread_init", |
| 406 | MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS); |
| 407 | } |
| 408 | |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 409 | }; |
| 410 | |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 411 | class ELFAsmParser : public MCAsmParserExtension { |
| 412 | bool ParseSectionSwitch(StringRef Section, unsigned Type, |
| 413 | unsigned Flags, SectionKind Kind); |
| 414 | |
| 415 | public: |
| 416 | ELFAsmParser() {} |
| 417 | |
| 418 | virtual void Initialize(MCAsmParser &Parser) { |
| 419 | // Call the base implementation. |
| 420 | this->MCAsmParserExtension::Initialize(Parser); |
| 421 | |
| 422 | Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler( |
| 423 | &ELFAsmParser::ParseSectionDirectiveData)); |
| 424 | Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler( |
| 425 | &ELFAsmParser::ParseSectionDirectiveText)); |
| 426 | } |
| 427 | |
| 428 | bool ParseSectionDirectiveData(StringRef, SMLoc) { |
| 429 | return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS, |
| 430 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 431 | SectionKind::getDataRel()); |
| 432 | } |
| 433 | bool ParseSectionDirectiveText(StringRef, SMLoc) { |
| 434 | return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS, |
| 435 | MCSectionELF::SHF_EXECINSTR | |
| 436 | MCSectionELF::SHF_ALLOC, SectionKind::getText()); |
| 437 | } |
| 438 | }; |
| 439 | |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 442 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 443 | |
Daniel Dunbar | 9186fa6 | 2010-07-01 20:41:56 +0000 | [diff] [blame] | 444 | AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx, |
| 445 | MCStreamer &_Out, const MCAsmInfo &_MAI) |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 446 | : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 447 | GenericParser(new GenericAsmParser), PlatformParser(0), |
| 448 | TargetParser(0), CurBuffer(0) { |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 449 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 450 | |
| 451 | // Initialize the generic parser. |
| 452 | GenericParser->Initialize(*this); |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 453 | |
| 454 | // Initialize the platform / file format parser. |
| 455 | // |
| 456 | // FIXME: This is a hack, we need to (majorly) cleanup how these objects are |
| 457 | // created. |
| 458 | if (_MAI.hasSubsectionsViaSymbols()) { |
| 459 | PlatformParser = new DarwinAsmParser; |
| 460 | PlatformParser->Initialize(*this); |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 461 | } else { |
| 462 | PlatformParser = new ELFAsmParser; |
| 463 | PlatformParser->Initialize(*this); |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 464 | } |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 467 | AsmParser::~AsmParser() { |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 468 | delete PlatformParser; |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 469 | delete GenericParser; |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Daniel Dunbar | 5313198 | 2010-07-12 17:27:45 +0000 | [diff] [blame] | 472 | void AsmParser::setTargetParser(TargetAsmParser &P) { |
| 473 | assert(!TargetParser && "Target parser is already initialized!"); |
| 474 | TargetParser = &P; |
| 475 | TargetParser->Initialize(*this); |
| 476 | } |
| 477 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 478 | void AsmParser::Warning(SMLoc L, const Twine &Msg) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 479 | PrintMessage(L, Msg.str(), "warning"); |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 482 | bool AsmParser::Error(SMLoc L, const Twine &Msg) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 483 | PrintMessage(L, Msg.str(), "error"); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 487 | void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg, |
| 488 | const char *Type) const { |
| 489 | SrcMgr.PrintMessage(Loc, Msg, Type); |
| 490 | } |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 491 | |
| 492 | bool AsmParser::EnterIncludeFile(const std::string &Filename) { |
| 493 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc()); |
| 494 | if (NewBuf == -1) |
| 495 | return true; |
Sean Callanan | 79036e4 | 2010-01-20 22:18:24 +0000 | [diff] [blame] | 496 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 497 | CurBuffer = NewBuf; |
| 498 | |
| 499 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 500 | |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | const AsmToken &AsmParser::Lex() { |
| 505 | const AsmToken *tok = &Lexer.Lex(); |
| 506 | |
| 507 | if (tok->is(AsmToken::Eof)) { |
| 508 | // If this is the end of an included file, pop the parent file off the |
| 509 | // include stack. |
| 510 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 511 | if (ParentIncludeLoc != SMLoc()) { |
| 512 | CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc); |
| 513 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), |
| 514 | ParentIncludeLoc.getPointer()); |
| 515 | tok = &Lexer.Lex(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (tok->is(AsmToken::Error)) |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 520 | PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error"); |
Sean Callanan | 79036e4 | 2010-01-20 22:18:24 +0000 | [diff] [blame] | 521 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 522 | return *tok; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 525 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 526 | // Create the initial section, if requested. |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 527 | // |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 528 | // FIXME: Target hook & command line option for initial section. |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 529 | if (!NoInitialTextSection) |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 530 | Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text", |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 531 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 532 | 0, SectionKind::getText())); |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 533 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 534 | // Prime the lexer. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 535 | Lex(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 536 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 537 | bool HadError = false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 538 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 539 | AsmCond StartingCondState = TheCondState; |
| 540 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 541 | // While we have input, parse each statement. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 542 | while (Lexer.isNot(AsmToken::Eof)) { |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 543 | if (!ParseStatement()) continue; |
| 544 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 545 | // We had an error, remember it and recover by skipping to the next line. |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 546 | HadError = true; |
| 547 | EatToEndOfStatement(); |
| 548 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 549 | |
| 550 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 551 | TheCondState.Ignore != StartingCondState.Ignore) |
| 552 | return TokError("unmatched .ifs or .elses"); |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 554 | // Finalize the output stream if there are no errors and if the client wants |
| 555 | // us to. |
| 556 | if (!HadError && !NoFinalize) |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 557 | Out.Finish(); |
| 558 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 559 | return HadError; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 562 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 563 | void AsmParser::EatToEndOfStatement() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 564 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 565 | Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 566 | Lex(); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 567 | |
| 568 | // Eat EOL. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 569 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 570 | Lex(); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 574 | /// ParseParenExpr - Parse a paren expression and return it. |
| 575 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 576 | /// |
| 577 | /// parenexpr ::= expr) |
| 578 | /// |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 579 | bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 580 | if (ParseExpression(Res)) return true; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 581 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 582 | return TokError("expected ')' in parentheses expression"); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 583 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 584 | Lex(); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 585 | return false; |
| 586 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 588 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 589 | /// primaryexpr ::= (parenexpr |
| 590 | /// primaryexpr ::= symbol |
| 591 | /// primaryexpr ::= number |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 592 | /// primaryexpr ::= '.' |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 593 | /// primaryexpr ::= ~,+,- primaryexpr |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 594 | bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 595 | switch (Lexer.getKind()) { |
| 596 | default: |
| 597 | return TokError("unknown token in expression"); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 598 | case AsmToken::Exclaim: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 599 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 600 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 601 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 602 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 603 | return false; |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 604 | case AsmToken::String: |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 605 | case AsmToken::Identifier: { |
| 606 | // This is a symbol reference. |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 607 | std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@'); |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 608 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first); |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 609 | |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 610 | // Mark the symbol as used in an expression. |
| 611 | Sym->setUsedInExpr(true); |
| 612 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 613 | // Lookup the symbol variant if used. |
| 614 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 615 | if (Split.first.size() != getTok().getIdentifier().size()) |
| 616 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
| 617 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 618 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 619 | Lex(); // Eat identifier. |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 620 | |
| 621 | // If this is an absolute variable reference, substitute it now to preserve |
| 622 | // semantics in the face of reassignment. |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 623 | if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 624 | if (Variant) |
| 625 | return Error(EndLoc, "unexpected modified on variable reference"); |
| 626 | |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 627 | Res = Sym->getVariableValue(); |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | |
| 631 | // Otherwise create a symbol ref. |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 632 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 633 | return false; |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 634 | } |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 635 | case AsmToken::Integer: { |
| 636 | SMLoc Loc = getTok().getLoc(); |
| 637 | int64_t IntVal = getTok().getIntVal(); |
| 638 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 639 | EndLoc = Lexer.getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 640 | Lex(); // Eat token. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 641 | // Look for 'b' or 'f' following an Integer as a directional label |
| 642 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 643 | StringRef IDVal = getTok().getString(); |
| 644 | if (IDVal == "f" || IDVal == "b"){ |
| 645 | MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal, |
| 646 | IDVal == "f" ? 1 : 0); |
| 647 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 648 | getContext()); |
| 649 | if(IDVal == "b" && Sym->isUndefined()) |
| 650 | return Error(Loc, "invalid reference to undefined symbol"); |
| 651 | EndLoc = Lexer.getLoc(); |
| 652 | Lex(); // Eat identifier. |
| 653 | } |
| 654 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 655 | return false; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 657 | case AsmToken::Dot: { |
| 658 | // This is a '.' reference, which references the current PC. Emit a |
| 659 | // temporary label to the streamer and refer to it. |
| 660 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 661 | Out.EmitLabel(Sym); |
| 662 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
| 663 | EndLoc = Lexer.getLoc(); |
| 664 | Lex(); // Eat identifier. |
| 665 | return false; |
| 666 | } |
| 667 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 668 | case AsmToken::LParen: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 669 | Lex(); // Eat the '('. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 670 | return ParseParenExpr(Res, EndLoc); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 671 | case AsmToken::Minus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 672 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 673 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 674 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 675 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 676 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 677 | case AsmToken::Plus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 678 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 679 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 680 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 681 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 682 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 683 | case AsmToken::Tilde: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 684 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 685 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 686 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 687 | Res = MCUnaryExpr::CreateNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 688 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 691 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 692 | bool AsmParser::ParseExpression(const MCExpr *&Res) { |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 693 | SMLoc EndLoc; |
| 694 | return ParseExpression(Res, EndLoc); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 697 | /// ParseExpression - Parse an expression and return it. |
| 698 | /// |
| 699 | /// expr ::= expr +,- expr -> lowest. |
| 700 | /// expr ::= expr |,^,&,! expr -> middle. |
| 701 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 702 | /// expr ::= primaryexpr |
| 703 | /// |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 704 | bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 705 | // Parse the expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 706 | Res = 0; |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 707 | if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc)) |
| 708 | return true; |
| 709 | |
| 710 | // Try to constant fold it up front, if possible. |
| 711 | int64_t Value; |
| 712 | if (Res->EvaluateAsAbsolute(Value)) |
| 713 | Res = MCConstantExpr::Create(Value, getContext()); |
| 714 | |
| 715 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 716 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 717 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 718 | bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 75f265f | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 719 | Res = 0; |
| 720 | return ParseParenExpr(Res, EndLoc) || |
| 721 | ParseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | c18274b | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 724 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 725 | const MCExpr *Expr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 727 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 728 | if (ParseExpression(Expr)) |
| 729 | return true; |
| 730 | |
Daniel Dunbar | e00b011 | 2009-10-16 01:57:52 +0000 | [diff] [blame] | 731 | if (!Expr->EvaluateAsAbsolute(Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 732 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 733 | |
| 734 | return false; |
| 735 | } |
| 736 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 737 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 738 | MCBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 739 | switch (K) { |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 740 | default: |
| 741 | return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 742 | |
| 743 | // Lowest Precedence: &&, || |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 744 | case AsmToken::AmpAmp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 745 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 746 | return 1; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 747 | case AsmToken::PipePipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 748 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 749 | return 1; |
| 750 | |
| 751 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 752 | case AsmToken::Plus: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 753 | Kind = MCBinaryExpr::Add; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 754 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 755 | case AsmToken::Minus: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 756 | Kind = MCBinaryExpr::Sub; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 757 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 758 | case AsmToken::EqualEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 759 | Kind = MCBinaryExpr::EQ; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 760 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 761 | case AsmToken::ExclaimEqual: |
| 762 | case AsmToken::LessGreater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 763 | Kind = MCBinaryExpr::NE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 764 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 765 | case AsmToken::Less: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 766 | Kind = MCBinaryExpr::LT; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 767 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 768 | case AsmToken::LessEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 769 | Kind = MCBinaryExpr::LTE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 770 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 771 | case AsmToken::Greater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 772 | Kind = MCBinaryExpr::GT; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 773 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 774 | case AsmToken::GreaterEqual: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 775 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 776 | return 2; |
| 777 | |
| 778 | // Intermediate Precedence: |, &, ^ |
| 779 | // |
| 780 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 781 | case AsmToken::Pipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 782 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 783 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 784 | case AsmToken::Caret: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 785 | Kind = MCBinaryExpr::Xor; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 786 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 787 | case AsmToken::Amp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 788 | Kind = MCBinaryExpr::And; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 789 | return 3; |
| 790 | |
| 791 | // Highest Precedence: *, /, %, <<, >> |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 792 | case AsmToken::Star: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 793 | Kind = MCBinaryExpr::Mul; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 794 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 795 | case AsmToken::Slash: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 796 | Kind = MCBinaryExpr::Div; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 797 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 798 | case AsmToken::Percent: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 799 | Kind = MCBinaryExpr::Mod; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 800 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 801 | case AsmToken::LessLess: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 802 | Kind = MCBinaryExpr::Shl; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 803 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 804 | case AsmToken::GreaterGreater: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 805 | Kind = MCBinaryExpr::Shr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 806 | return 4; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
| 810 | |
| 811 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 812 | /// Res contains the LHS of the expression on input. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 813 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
| 814 | SMLoc &EndLoc) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 815 | while (1) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 816 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 817 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 818 | |
| 819 | // If the next token is lower precedence than we are allowed to eat, return |
| 820 | // successfully with what we ate already. |
| 821 | if (TokPrec < Precedence) |
| 822 | return false; |
| 823 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 824 | Lex(); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 825 | |
| 826 | // Eat the next primary expression. |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 827 | const MCExpr *RHS; |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 828 | if (ParsePrimaryExpr(RHS, EndLoc)) return true; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 829 | |
| 830 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 831 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 832 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 833 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 834 | if (TokPrec < NextTokPrec) { |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 835 | if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 838 | // Merge LHS and RHS according to operator. |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 839 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 843 | |
| 844 | |
| 845 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 846 | /// ParseStatement: |
| 847 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 848 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 849 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 850 | bool AsmParser::ParseStatement() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 851 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 852 | Out.AddBlankLine(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 853 | Lex(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 854 | return false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 855 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 856 | |
| 857 | // Statements always start with an identifier. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 858 | AsmToken ID = getTok(); |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 859 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 860 | StringRef IDVal; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 861 | int64_t LocalLabelVal = -1; |
| 862 | // GUESS allow an integer followed by a ':' as a directional local label |
| 863 | if (Lexer.is(AsmToken::Integer)) { |
| 864 | LocalLabelVal = getTok().getIntVal(); |
| 865 | if (LocalLabelVal < 0) { |
| 866 | if (!TheCondState.Ignore) |
| 867 | return TokError("unexpected token at start of statement"); |
| 868 | IDVal = ""; |
| 869 | } |
| 870 | else { |
| 871 | IDVal = getTok().getString(); |
| 872 | Lex(); // Consume the integer token to be used as an identifier token. |
| 873 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 874 | if (!TheCondState.Ignore) |
| 875 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | else if (ParseIdentifier(IDVal)) { |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 880 | if (!TheCondState.Ignore) |
| 881 | return TokError("unexpected token at start of statement"); |
| 882 | IDVal = ""; |
| 883 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 884 | |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 885 | // Handle conditional assembly here before checking for skipping. We |
| 886 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 887 | // example. |
| 888 | if (IDVal == ".if") |
| 889 | return ParseDirectiveIf(IDLoc); |
| 890 | if (IDVal == ".elseif") |
| 891 | return ParseDirectiveElseIf(IDLoc); |
| 892 | if (IDVal == ".else") |
| 893 | return ParseDirectiveElse(IDLoc); |
| 894 | if (IDVal == ".endif") |
| 895 | return ParseDirectiveEndIf(IDLoc); |
| 896 | |
| 897 | // If we are in a ".if 0" block, ignore this statement. |
| 898 | if (TheCondState.Ignore) { |
| 899 | EatToEndOfStatement(); |
| 900 | return false; |
| 901 | } |
| 902 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 903 | // FIXME: Recurse on local labels? |
| 904 | |
| 905 | // See what kind of statement we have. |
| 906 | switch (Lexer.getKind()) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 907 | case AsmToken::Colon: { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 908 | // identifier ':' -> Label. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 909 | Lex(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 910 | |
| 911 | // Diagnose attempt to use a variable as a label. |
| 912 | // |
| 913 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 914 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 915 | // implicitly marked as external. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 916 | MCSymbol *Sym; |
| 917 | if (LocalLabelVal == -1) |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 918 | Sym = getContext().GetOrCreateSymbol(IDVal); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 919 | else |
| 920 | Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 921 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 922 | return Error(IDLoc, "invalid symbol redefinition"); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 923 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 924 | // Emit the label. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 925 | Out.EmitLabel(Sym); |
| 926 | |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 927 | // Consume any end of statement token, if present, to avoid spurious |
| 928 | // AddBlankLine calls(). |
| 929 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 930 | Lex(); |
| 931 | if (Lexer.is(AsmToken::Eof)) |
| 932 | return false; |
| 933 | } |
| 934 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 935 | return ParseStatement(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 936 | } |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 937 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 938 | case AsmToken::Equal: |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 939 | // identifier '=' ... -> assignment statement |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 940 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 941 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 942 | return ParseAssignment(IDVal); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 943 | |
| 944 | default: // Normal instruction or directive. |
| 945 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 949 | if (IDVal[0] == '.') { |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 950 | // FIXME: This should be driven based on a hash lookup and callback. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 951 | if (IDVal == ".section") |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 952 | return ParseDirectiveDarwinSection(); |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 953 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 954 | // Assembler features |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 955 | if (IDVal == ".set") |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 956 | return ParseDirectiveSet(); |
| 957 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 958 | // Data directives |
| 959 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 960 | if (IDVal == ".ascii") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 961 | return ParseDirectiveAscii(false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 962 | if (IDVal == ".asciz") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 963 | return ParseDirectiveAscii(true); |
| 964 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 965 | if (IDVal == ".byte") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 966 | return ParseDirectiveValue(1); |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 967 | if (IDVal == ".short") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 968 | return ParseDirectiveValue(2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 969 | if (IDVal == ".long") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 970 | return ParseDirectiveValue(4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 971 | if (IDVal == ".quad") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 972 | return ParseDirectiveValue(8); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 973 | |
| 974 | // FIXME: Target hooks for IsPow2. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 975 | if (IDVal == ".align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 976 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 977 | if (IDVal == ".align32") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 978 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 979 | if (IDVal == ".balign") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 980 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 981 | if (IDVal == ".balignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 982 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 983 | if (IDVal == ".balignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 984 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 985 | if (IDVal == ".p2align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 986 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 987 | if (IDVal == ".p2alignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 988 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 989 | if (IDVal == ".p2alignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 990 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 991 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 992 | if (IDVal == ".org") |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 993 | return ParseDirectiveOrg(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 994 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 995 | if (IDVal == ".fill") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 996 | return ParseDirectiveFill(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 997 | if (IDVal == ".space") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 998 | return ParseDirectiveSpace(); |
| 999 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1000 | // Symbol attribute directives |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1001 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1002 | if (IDVal == ".globl" || IDVal == ".global") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1003 | return ParseDirectiveSymbolAttribute(MCSA_Global); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1004 | if (IDVal == ".hidden") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1005 | return ParseDirectiveSymbolAttribute(MCSA_Hidden); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1006 | if (IDVal == ".indirect_symbol") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1007 | return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1008 | if (IDVal == ".internal") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1009 | return ParseDirectiveSymbolAttribute(MCSA_Internal); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1010 | if (IDVal == ".lazy_reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1011 | return ParseDirectiveSymbolAttribute(MCSA_LazyReference); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1012 | if (IDVal == ".no_dead_strip") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1013 | return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1014 | if (IDVal == ".private_extern") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1015 | return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1016 | if (IDVal == ".protected") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1017 | return ParseDirectiveSymbolAttribute(MCSA_Protected); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1018 | if (IDVal == ".reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1019 | return ParseDirectiveSymbolAttribute(MCSA_Reference); |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1020 | if (IDVal == ".type") |
| 1021 | return ParseDirectiveELFType(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1022 | if (IDVal == ".weak") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1023 | return ParseDirectiveSymbolAttribute(MCSA_Weak); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1024 | if (IDVal == ".weak_definition") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1025 | return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1026 | if (IDVal == ".weak_reference") |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1027 | return ParseDirectiveSymbolAttribute(MCSA_WeakReference); |
Kevin Enderby | f59cac5 | 2010-07-08 17:22:42 +0000 | [diff] [blame] | 1028 | if (IDVal == ".weak_def_can_be_hidden") |
| 1029 | return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1030 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1031 | if (IDVal == ".comm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1032 | return ParseDirectiveComm(/*IsLocal=*/false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1033 | if (IDVal == ".lcomm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1034 | return ParseDirectiveComm(/*IsLocal=*/true); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1036 | if (IDVal == ".abort") |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1037 | return ParseDirectiveAbort(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1038 | if (IDVal == ".include") |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1039 | return ParseDirectiveInclude(); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1040 | |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 1041 | // Look up the handler in the handler table. |
| 1042 | std::pair<MCAsmParserExtension*, DirectiveHandler> Handler = |
| 1043 | DirectiveMap.lookup(IDVal); |
| 1044 | if (Handler.first) |
| 1045 | return (Handler.first->*Handler.second)(IDVal, IDLoc); |
| 1046 | |
Kevin Enderby | 9c65645 | 2009-09-10 20:51:44 +0000 | [diff] [blame] | 1047 | // Target hook for parsing target specific directives. |
| 1048 | if (!getTargetParser().ParseDirective(ID)) |
| 1049 | return false; |
| 1050 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1051 | Warning(IDLoc, "ignoring directive for now"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1052 | EatToEndOfStatement(); |
| 1053 | return false; |
| 1054 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | a7f1354 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1056 | // Canonicalize the opcode to lower case. |
| 1057 | SmallString<128> Opcode; |
| 1058 | for (unsigned i = 0, e = IDVal.size(); i != e; ++i) |
| 1059 | Opcode.push_back(tolower(IDVal[i])); |
| 1060 | |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1061 | SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; |
Chris Lattner | a7f1354 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1062 | bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc, |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1063 | ParsedOperands); |
| 1064 | if (!HadError && Lexer.isNot(AsmToken::EndOfStatement)) |
| 1065 | HadError = TokError("unexpected token in argument list"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1066 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1067 | // If parsing succeeded, match the instruction. |
| 1068 | if (!HadError) { |
| 1069 | MCInst Inst; |
| 1070 | if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) { |
| 1071 | // Emit the instruction on success. |
| 1072 | Out.EmitInstruction(Inst); |
| 1073 | } else { |
| 1074 | // Otherwise emit a diagnostic about the match failure and set the error |
| 1075 | // flag. |
| 1076 | // |
| 1077 | // FIXME: We should give nicer diagnostics about the exact failure. |
| 1078 | Error(IDLoc, "unrecognized instruction"); |
| 1079 | HadError = true; |
| 1080 | } |
| 1081 | } |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1082 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1083 | // If there was no error, consume the end-of-statement token. Otherwise this |
| 1084 | // will be done by our caller. |
| 1085 | if (!HadError) |
| 1086 | Lex(); |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Free any parsed operands. |
| 1089 | for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) |
| 1090 | delete ParsedOperands[i]; |
| 1091 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1092 | return HadError; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1094 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 1095 | bool AsmParser::ParseAssignment(const StringRef &Name) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1096 | // FIXME: Use better location, we should use proper tokens. |
| 1097 | SMLoc EqualLoc = Lexer.getLoc(); |
| 1098 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1099 | const MCExpr *Value; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1100 | if (ParseExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1101 | return true; |
| 1102 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1103 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1104 | return TokError("unexpected token in assignment"); |
| 1105 | |
| 1106 | // Eat the end of statement marker. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1107 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1108 | |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1109 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 1110 | // used as a symbol, or it is an absolute symbol). |
| 1111 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 1112 | if (Sym) { |
| 1113 | // Diagnose assignment to a label. |
| 1114 | // |
| 1115 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1116 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 1117 | if (Sym->isUndefined() && !Sym->isUsedInExpr()) |
| 1118 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 1119 | else if (!Sym->isUndefined() && !Sym->isAbsolute()) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1120 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 1121 | else if (!Sym->isVariable()) |
| 1122 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 1123 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1124 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
| 1125 | Name + "'"); |
| 1126 | } else |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1127 | Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1128 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1129 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1130 | |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 1131 | Sym->setUsedInExpr(true); |
| 1132 | |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1133 | // Do the assignment. |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 1134 | Out.EmitAssignment(Sym, Value); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1135 | |
| 1136 | return false; |
| 1137 | } |
| 1138 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1139 | /// ParseIdentifier: |
| 1140 | /// ::= identifier |
| 1141 | /// ::= string |
| 1142 | bool AsmParser::ParseIdentifier(StringRef &Res) { |
| 1143 | if (Lexer.isNot(AsmToken::Identifier) && |
| 1144 | Lexer.isNot(AsmToken::String)) |
| 1145 | return true; |
| 1146 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1147 | Res = getTok().getIdentifier(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1148 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1149 | Lex(); // Consume the identifier token. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1150 | |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1154 | /// ParseDirectiveSet: |
| 1155 | /// ::= .set identifier ',' expression |
| 1156 | bool AsmParser::ParseDirectiveSet() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1157 | StringRef Name; |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1158 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1159 | if (ParseIdentifier(Name)) |
| 1160 | return TokError("expected identifier after '.set' directive"); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1161 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1162 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1163 | return TokError("unexpected token in '.set'"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1164 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1165 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 1166 | return ParseAssignment(Name); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1169 | /// ParseDirectiveSection: |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 1170 | /// ::= .section identifier (',' identifier)* |
| 1171 | /// FIXME: This should actually parse out the segment, section, attributes and |
| 1172 | /// sizeof_stub fields. |
| 1173 | bool AsmParser::ParseDirectiveDarwinSection() { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1174 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1175 | |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 1176 | StringRef SectionName; |
| 1177 | if (ParseIdentifier(SectionName)) |
| 1178 | return Error(Loc, "expected identifier after '.section' directive"); |
| 1179 | |
| 1180 | // Verify there is a following comma. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1181 | if (!getLexer().is(AsmToken::Comma)) |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 1182 | return TokError("unexpected token in '.section' directive"); |
| 1183 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1184 | std::string SectionSpec = SectionName; |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 1185 | SectionSpec += ","; |
| 1186 | |
| 1187 | // Add all the tokens until the end of the line, ParseSectionSpecifier will |
| 1188 | // handle this. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1189 | StringRef EOL = Lexer.LexUntilEndOfStatement(); |
| 1190 | SectionSpec.append(EOL.begin(), EOL.end()); |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 1191 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1192 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1193 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1194 | return TokError("unexpected token in '.section' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1195 | Lex(); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1197 | |
| 1198 | StringRef Segment, Section; |
| 1199 | unsigned TAA, StubSize; |
| 1200 | std::string ErrorStr = |
| 1201 | MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section, |
| 1202 | TAA, StubSize); |
| 1203 | |
| 1204 | if (!ErrorStr.empty()) |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 1205 | return Error(Loc, ErrorStr.c_str()); |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 1207 | // FIXME: Arch specific. |
Chris Lattner | f60e9bb | 2010-02-26 18:32:26 +0000 | [diff] [blame] | 1208 | bool isText = Segment == "__TEXT"; // FIXME: Hack. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1209 | getStreamer().SwitchSection(Ctx.getMachOSection( |
| 1210 | Segment, Section, TAA, StubSize, |
| 1211 | isText ? SectionKind::getText() |
| 1212 | : SectionKind::getDataRel())); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1213 | return false; |
| 1214 | } |
| 1215 | |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 1216 | bool DarwinAsmParser::ParseSectionSwitch(const char *Segment, |
| 1217 | const char *Section, |
| 1218 | unsigned TAA, unsigned Align, |
| 1219 | unsigned StubSize) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1220 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 1221 | return TokError("unexpected token in section switching directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1222 | Lex(); |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 1224 | // FIXME: Arch specific. |
Chris Lattner | f60e9bb | 2010-02-26 18:32:26 +0000 | [diff] [blame] | 1225 | bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack. |
Daniel Dunbar | 47f5608 | 2010-07-12 20:23:36 +0000 | [diff] [blame] | 1226 | getStreamer().SwitchSection(getContext().getMachOSection( |
| 1227 | Segment, Section, TAA, StubSize, |
| 1228 | isText ? SectionKind::getText() |
| 1229 | : SectionKind::getDataRel())); |
Daniel Dunbar | 2330df6 | 2009-08-21 23:30:15 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Set the implicit alignment, if any. |
| 1232 | // |
| 1233 | // FIXME: This isn't really what 'as' does; I think it just uses the implicit |
| 1234 | // alignment on the section (e.g., if one manually inserts bytes into the |
| 1235 | // section, then just issueing the section switch directive will not realign |
| 1236 | // the section. However, this is arguably more reasonable behavior, and there |
| 1237 | // is no good reason for someone to intentionally emit incorrectly sized |
| 1238 | // values into the implicitly aligned sections. |
| 1239 | if (Align) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1240 | getStreamer().EmitValueToAlignment(Align, 0, 1, 0); |
Daniel Dunbar | 2330df6 | 2009-08-21 23:30:15 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 1242 | return false; |
| 1243 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1244 | |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 1245 | bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, |
| 1246 | unsigned Flags, SectionKind Kind) { |
| 1247 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 1248 | return TokError("unexpected token in section switching directive"); |
| 1249 | Lex(); |
| 1250 | |
| 1251 | getStreamer().SwitchSection(getContext().getELFSection( |
| 1252 | Section, Type, Flags, Kind)); |
| 1253 | |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1257 | bool AsmParser::ParseEscapedString(std::string &Data) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1258 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1259 | |
| 1260 | Data = ""; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1261 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1262 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 1263 | if (Str[i] != '\\') { |
| 1264 | Data += Str[i]; |
| 1265 | continue; |
| 1266 | } |
| 1267 | |
| 1268 | // Recognize escaped characters. Note that this escape semantics currently |
| 1269 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 1270 | ++i; |
| 1271 | if (i == e) |
| 1272 | return TokError("unexpected backslash at end of string"); |
| 1273 | |
| 1274 | // Recognize octal sequences. |
| 1275 | if ((unsigned) (Str[i] - '0') <= 7) { |
| 1276 | // Consume up to three octal characters. |
| 1277 | unsigned Value = Str[i] - '0'; |
| 1278 | |
| 1279 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 1280 | ++i; |
| 1281 | Value = Value * 8 + (Str[i] - '0'); |
| 1282 | |
| 1283 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 1284 | ++i; |
| 1285 | Value = Value * 8 + (Str[i] - '0'); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | if (Value > 255) |
| 1290 | return TokError("invalid octal escape sequence (out of range)"); |
| 1291 | |
| 1292 | Data += (unsigned char) Value; |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | // Otherwise recognize individual escapes. |
| 1297 | switch (Str[i]) { |
| 1298 | default: |
| 1299 | // Just reject invalid escape sequences for now. |
| 1300 | return TokError("invalid escape sequence (unrecognized character)"); |
| 1301 | |
| 1302 | case 'b': Data += '\b'; break; |
| 1303 | case 'f': Data += '\f'; break; |
| 1304 | case 'n': Data += '\n'; break; |
| 1305 | case 'r': Data += '\r'; break; |
| 1306 | case 't': Data += '\t'; break; |
| 1307 | case '"': Data += '"'; break; |
| 1308 | case '\\': Data += '\\'; break; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | return false; |
| 1313 | } |
| 1314 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1315 | /// ParseDirectiveAscii: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1316 | /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1317 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1318 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1319 | for (;;) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1320 | if (getLexer().isNot(AsmToken::String)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1321 | return TokError("expected string in '.ascii' or '.asciz' directive"); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1322 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1323 | std::string Data; |
| 1324 | if (ParseEscapedString(Data)) |
| 1325 | return true; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1326 | |
| 1327 | getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1328 | if (ZeroTerminated) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1329 | getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); |
| 1330 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1331 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1336 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1337 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1338 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1342 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | /// ParseDirectiveValue |
| 1347 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 1348 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1349 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1350 | for (;;) { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1351 | const MCExpr *Value; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1352 | SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1353 | if (ParseExpression(Value)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1354 | return true; |
| 1355 | |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 1356 | // Special case constant expressions to match code generator. |
| 1357 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1358 | getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE); |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 1359 | else |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1360 | getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1361 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1362 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1363 | break; |
| 1364 | |
| 1365 | // FIXME: Improve diagnostic. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1366 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1367 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1368 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1372 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | /// ParseDirectiveSpace |
| 1377 | /// ::= .space expression [ , expression ] |
| 1378 | bool AsmParser::ParseDirectiveSpace() { |
| 1379 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1380 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1381 | return true; |
| 1382 | |
| 1383 | int64_t FillExpr = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1384 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1385 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1386 | return TokError("unexpected token in '.space' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1387 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1388 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1389 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1390 | return true; |
| 1391 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1392 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1393 | return TokError("unexpected token in '.space' directive"); |
| 1394 | } |
| 1395 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1396 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (NumBytes <= 0) |
| 1399 | return TokError("invalid number of bytes in '.space' directive"); |
| 1400 | |
| 1401 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1402 | getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1403 | |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | /// ParseDirectiveFill |
| 1408 | /// ::= .fill expression , expression , expression |
| 1409 | bool AsmParser::ParseDirectiveFill() { |
| 1410 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1411 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1412 | return true; |
| 1413 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1414 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1415 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1416 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1417 | |
| 1418 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1419 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1420 | return true; |
| 1421 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1422 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1423 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1424 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1425 | |
| 1426 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1427 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1428 | return true; |
| 1429 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1430 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1431 | return TokError("unexpected token in '.fill' directive"); |
| 1432 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1433 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1434 | |
Daniel Dunbar | bc38ca7 | 2009-08-21 15:43:35 +0000 | [diff] [blame] | 1435 | if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) |
| 1436 | return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1437 | |
| 1438 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1439 | getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 1440 | |
| 1441 | return false; |
| 1442 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1443 | |
| 1444 | /// ParseDirectiveOrg |
| 1445 | /// ::= .org expression [ , expression ] |
| 1446 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1447 | const MCExpr *Offset; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1448 | if (ParseExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1449 | return true; |
| 1450 | |
| 1451 | // Parse optional fill expression. |
| 1452 | int64_t FillExpr = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1453 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1454 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1455 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1456 | Lex(); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1457 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1458 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1459 | return true; |
| 1460 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1461 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1462 | return TokError("unexpected token in '.org' directive"); |
| 1463 | } |
| 1464 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1465 | Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1466 | |
| 1467 | // FIXME: Only limited forms of relocatable expressions are accepted here, it |
| 1468 | // has to be relative to the current section. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1469 | getStreamer().EmitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 1470 | |
| 1471 | return false; |
| 1472 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1473 | |
| 1474 | /// ParseDirectiveAlign |
| 1475 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 1476 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1477 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1478 | int64_t Alignment; |
| 1479 | if (ParseAbsoluteExpression(Alignment)) |
| 1480 | return true; |
| 1481 | |
| 1482 | SMLoc MaxBytesLoc; |
| 1483 | bool HasFillExpr = false; |
| 1484 | int64_t FillExpr = 0; |
| 1485 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1486 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1487 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1488 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1489 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1490 | |
| 1491 | // The fill expression can be omitted while specifying a maximum number of |
| 1492 | // alignment bytes, e.g: |
| 1493 | // .align 3,,4 |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1494 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1495 | HasFillExpr = true; |
| 1496 | if (ParseAbsoluteExpression(FillExpr)) |
| 1497 | return true; |
| 1498 | } |
| 1499 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1500 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1501 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1502 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1503 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1504 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1505 | MaxBytesLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1506 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 1507 | return true; |
| 1508 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1509 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1510 | return TokError("unexpected token in directive"); |
| 1511 | } |
| 1512 | } |
| 1513 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1514 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1515 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1516 | if (!HasFillExpr) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1517 | FillExpr = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Compute alignment in bytes. |
| 1520 | if (IsPow2) { |
| 1521 | // FIXME: Diagnose overflow. |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1522 | if (Alignment >= 32) { |
| 1523 | Error(AlignmentLoc, "invalid alignment value"); |
| 1524 | Alignment = 31; |
| 1525 | } |
| 1526 | |
Benjamin Kramer | 12fd767 | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 1527 | Alignment = 1ULL << Alignment; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1530 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1531 | if (MaxBytesLoc.isValid()) { |
| 1532 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 1533 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 1534 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 0afb9f5 | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 1535 | MaxBytesToFill = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1539 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 1540 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1541 | MaxBytesToFill = 0; |
| 1542 | } |
| 1543 | } |
| 1544 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1545 | // Check whether we should use optimal code alignment for this .align |
| 1546 | // directive. |
| 1547 | // |
| 1548 | // FIXME: This should be using a target hook. |
| 1549 | bool UseCodeAlign = false; |
| 1550 | if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>( |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1551 | getStreamer().getCurrentSection())) |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1552 | UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
| 1553 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 1554 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1555 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1556 | } else { |
Kevin Enderby | d74acb0 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 1557 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1558 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 1559 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1560 | |
| 1561 | return false; |
| 1562 | } |
| 1563 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1564 | /// ParseDirectiveSymbolAttribute |
| 1565 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 1566 | bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1567 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1568 | for (;;) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1569 | StringRef Name; |
| 1570 | |
| 1571 | if (ParseIdentifier(Name)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1572 | return TokError("expected identifier in directive"); |
| 1573 | |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1574 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1575 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1576 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1577 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1578 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1581 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1582 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1583 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1587 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1588 | return false; |
| 1589 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1590 | |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1591 | /// ParseDirectiveELFType |
| 1592 | /// ::= .type identifier , @attribute |
| 1593 | bool AsmParser::ParseDirectiveELFType() { |
| 1594 | StringRef Name; |
| 1595 | if (ParseIdentifier(Name)) |
| 1596 | return TokError("expected identifier in directive"); |
| 1597 | |
| 1598 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1599 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1600 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1601 | if (getLexer().isNot(AsmToken::Comma)) |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1602 | return TokError("unexpected token in '.type' directive"); |
| 1603 | Lex(); |
| 1604 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1605 | if (getLexer().isNot(AsmToken::At)) |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1606 | return TokError("expected '@' before type"); |
| 1607 | Lex(); |
| 1608 | |
| 1609 | StringRef Type; |
| 1610 | SMLoc TypeLoc; |
| 1611 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1612 | TypeLoc = getLexer().getLoc(); |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1613 | if (ParseIdentifier(Type)) |
| 1614 | return TokError("expected symbol type in directive"); |
| 1615 | |
| 1616 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type) |
| 1617 | .Case("function", MCSA_ELF_TypeFunction) |
| 1618 | .Case("object", MCSA_ELF_TypeObject) |
| 1619 | .Case("tls_object", MCSA_ELF_TypeTLS) |
| 1620 | .Case("common", MCSA_ELF_TypeCommon) |
| 1621 | .Case("notype", MCSA_ELF_TypeNoType) |
| 1622 | .Default(MCSA_Invalid); |
| 1623 | |
| 1624 | if (Attr == MCSA_Invalid) |
| 1625 | return Error(TypeLoc, "unsupported attribute in '.type' directive"); |
| 1626 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1627 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1628 | return TokError("unexpected token in '.type' directive"); |
| 1629 | |
| 1630 | Lex(); |
| 1631 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1632 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
Matt Fleming | 924c5e5 | 2010-05-21 11:36:59 +0000 | [diff] [blame] | 1633 | |
| 1634 | return false; |
| 1635 | } |
| 1636 | |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 1637 | /// ParseDirectiveDesc |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1638 | /// ::= .desc identifier , expression |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 1639 | bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1640 | StringRef Name; |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 1641 | if (getParser().ParseIdentifier(Name)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1642 | return TokError("expected identifier in directive"); |
| 1643 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1644 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 1645 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1646 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1647 | if (getLexer().isNot(AsmToken::Comma)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1648 | return TokError("unexpected token in '.desc' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1649 | Lex(); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1650 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1651 | int64_t DescValue; |
Daniel Dunbar | 492b7a2 | 2010-07-12 19:22:53 +0000 | [diff] [blame] | 1652 | if (getParser().ParseAbsoluteExpression(DescValue)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1653 | return true; |
| 1654 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1655 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1656 | return TokError("unexpected token in '.desc' directive"); |
| 1657 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1658 | Lex(); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1659 | |
| 1660 | // Set the n_desc field of this Symbol to this DescValue |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1661 | getStreamer().EmitSymbolDesc(Sym, DescValue); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1662 | |
| 1663 | return false; |
| 1664 | } |
| 1665 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1666 | /// ParseDirectiveComm |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1667 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
| 1668 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1669 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1670 | StringRef Name; |
| 1671 | if (ParseIdentifier(Name)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1672 | return TokError("expected identifier in directive"); |
| 1673 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1674 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1675 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1676 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1677 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1678 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1679 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1680 | |
| 1681 | int64_t Size; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1682 | SMLoc SizeLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1683 | if (ParseAbsoluteExpression(Size)) |
| 1684 | return true; |
| 1685 | |
| 1686 | int64_t Pow2Alignment = 0; |
| 1687 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1688 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1689 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1690 | Pow2AlignmentLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1691 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1692 | return true; |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 1693 | |
| 1694 | // If this target takes alignments in bytes (not log) validate and convert. |
| 1695 | if (Lexer.getMAI().getAlignmentIsInBytes()) { |
| 1696 | if (!isPowerOf2_64(Pow2Alignment)) |
| 1697 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 1698 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 1699 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1702 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1703 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1704 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1705 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1707 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 1708 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1709 | if (Size < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1710 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
| 1711 | "be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1712 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 1713 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1714 | // may internally end up wanting an alignment in bytes. |
| 1715 | // FIXME: Diagnose overflow. |
| 1716 | if (Pow2Alignment < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1717 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
| 1718 | "alignment, can't be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1719 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1720 | if (!Sym->isUndefined()) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1721 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1722 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1723 | // '.lcomm' is equivalent to '.zerofill'. |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1724 | // Create the Symbol as a common or local common with Size and Pow2Alignment |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1725 | if (IsLocal) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1726 | getStreamer().EmitZerofill(Ctx.getMachOSection( |
| 1727 | "__DATA", "__bss", MCSectionMachO::S_ZEROFILL, |
| 1728 | 0, SectionKind::getBSS()), |
| 1729 | Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1730 | return false; |
| 1731 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1732 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1733 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1734 | return false; |
| 1735 | } |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1736 | |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1737 | /// ParseDirectiveZerofill |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1738 | /// ::= .zerofill segname , sectname [, identifier , size_expression [ |
| 1739 | /// , align_expression ]] |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1740 | bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) { |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1741 | StringRef Segment; |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1742 | if (getParser().ParseIdentifier(Segment)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1743 | return TokError("expected segment name after '.zerofill' directive"); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1744 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1745 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1746 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1747 | Lex(); |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1748 | |
| 1749 | StringRef Section; |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1750 | if (getParser().ParseIdentifier(Section)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1751 | return TokError("expected section name after comma in '.zerofill' " |
| 1752 | "directive"); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1754 | // If this is the end of the line all that was wanted was to create the |
| 1755 | // the section but with no symbol. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1756 | if (getLexer().is(AsmToken::EndOfStatement)) { |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1757 | // Create the zerofill section but no symbol |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1758 | getStreamer().EmitZerofill(getContext().getMachOSection( |
| 1759 | Segment, Section, MCSectionMachO::S_ZEROFILL, |
| 1760 | 0, SectionKind::getBSS())); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1761 | return false; |
| 1762 | } |
| 1763 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1764 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1765 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1766 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1767 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1768 | SMLoc IDLoc = getLexer().getLoc(); |
Chris Lattner | 7bb7c55 | 2010-05-13 00:10:34 +0000 | [diff] [blame] | 1769 | StringRef IDStr; |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1770 | if (getParser().ParseIdentifier(IDStr)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1771 | return TokError("expected identifier in directive"); |
| 1772 | |
| 1773 | // handle the identifier as the key symbol. |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1774 | MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1775 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1776 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1777 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1778 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1779 | |
| 1780 | int64_t Size; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1781 | SMLoc SizeLoc = getLexer().getLoc(); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1782 | if (getParser().ParseAbsoluteExpression(Size)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1783 | return true; |
| 1784 | |
| 1785 | int64_t Pow2Alignment = 0; |
| 1786 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1787 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1788 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1789 | Pow2AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1790 | if (getParser().ParseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1791 | return true; |
| 1792 | } |
| 1793 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1794 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1795 | return TokError("unexpected token in '.zerofill' directive"); |
| 1796 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1797 | Lex(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (Size < 0) |
| 1800 | return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " |
| 1801 | "than zero"); |
| 1802 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 1803 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1804 | // may internally end up wanting an alignment in bytes. |
| 1805 | // FIXME: Diagnose overflow. |
| 1806 | if (Pow2Alignment < 0) |
| 1807 | return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, " |
| 1808 | "can't be less than zero"); |
| 1809 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 1810 | if (!Sym->isUndefined()) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1811 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1812 | |
Daniel Dunbar | bdee6df | 2009-08-27 23:58:10 +0000 | [diff] [blame] | 1813 | // Create the zerofill Symbol with Size and Pow2Alignment |
Daniel Dunbar | 2e15292 | 2009-08-28 05:48:29 +0000 | [diff] [blame] | 1814 | // |
| 1815 | // FIXME: Arch specific. |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1816 | getStreamer().EmitZerofill(getContext().getMachOSection( |
| 1817 | Segment, Section, MCSectionMachO::S_ZEROFILL, |
| 1818 | 0, SectionKind::getBSS()), |
| 1819 | Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1820 | |
| 1821 | return false; |
| 1822 | } |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1823 | |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1824 | /// ParseDirectiveTBSS |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1825 | /// ::= .tbss identifier, size, align |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1826 | bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1827 | SMLoc IDLoc = getLexer().getLoc(); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1828 | StringRef Name; |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1829 | if (getParser().ParseIdentifier(Name)) |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1830 | return TokError("expected identifier in directive"); |
Eric Christopher | d04d98d | 2010-05-17 02:13:02 +0000 | [diff] [blame] | 1831 | |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1832 | // Handle the identifier as the key symbol. |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1833 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1834 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1835 | if (getLexer().isNot(AsmToken::Comma)) |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1836 | return TokError("unexpected token in directive"); |
| 1837 | Lex(); |
| 1838 | |
| 1839 | int64_t Size; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1840 | SMLoc SizeLoc = getLexer().getLoc(); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1841 | if (getParser().ParseAbsoluteExpression(Size)) |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1842 | return true; |
| 1843 | |
| 1844 | int64_t Pow2Alignment = 0; |
| 1845 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1846 | if (getLexer().is(AsmToken::Comma)) { |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1847 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1848 | Pow2AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1849 | if (getParser().ParseAbsoluteExpression(Pow2Alignment)) |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1850 | return true; |
| 1851 | } |
| 1852 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1853 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1854 | return TokError("unexpected token in '.tbss' directive"); |
| 1855 | |
| 1856 | Lex(); |
| 1857 | |
| 1858 | if (Size < 0) |
| 1859 | return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than" |
| 1860 | "zero"); |
| 1861 | |
| 1862 | // FIXME: Diagnose overflow. |
| 1863 | if (Pow2Alignment < 0) |
| 1864 | return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less" |
| 1865 | "than zero"); |
| 1866 | |
| 1867 | if (!Sym->isUndefined()) |
| 1868 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1869 | |
Daniel Dunbar | b6c3a60 | 2010-07-12 19:37:35 +0000 | [diff] [blame] | 1870 | getStreamer().EmitTBSSSymbol(getContext().getMachOSection( |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1871 | "__DATA", "__thread_bss", |
| 1872 | MCSectionMachO::S_THREAD_LOCAL_ZEROFILL, |
| 1873 | 0, SectionKind::getThreadBSS()), |
| 1874 | Sym, Size, 1 << Pow2Alignment); |
Eric Christopher | 482eba0 | 2010-05-14 01:50:28 +0000 | [diff] [blame] | 1875 | |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 1879 | /// ParseDirectiveSubsectionsViaSymbols |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1880 | /// ::= .subsections_via_symbols |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 1881 | bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1882 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1883 | return TokError("unexpected token in '.subsections_via_symbols' directive"); |
| 1884 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1885 | Lex(); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1886 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1887 | getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1888 | |
| 1889 | return false; |
| 1890 | } |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1891 | |
| 1892 | /// ParseDirectiveAbort |
| 1893 | /// ::= .abort [ "abort_string" ] |
| 1894 | bool AsmParser::ParseDirectiveAbort() { |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1895 | // FIXME: Use loc from directive. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1896 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1897 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1898 | StringRef Str = ""; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1899 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1900 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1901 | return TokError("expected string in '.abort' directive"); |
| 1902 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1903 | Str = getTok().getString(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1904 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1905 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1908 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1909 | return TokError("unexpected token in '.abort' directive"); |
| 1910 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1911 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1912 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1913 | // FIXME: Handle here. |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1914 | if (Str.empty()) |
| 1915 | Error(Loc, ".abort detected. Assembly stopping."); |
| 1916 | else |
| 1917 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1918 | |
| 1919 | return false; |
| 1920 | } |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1921 | |
| 1922 | /// ParseDirectiveLsym |
| 1923 | /// ::= .lsym identifier , expression |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 1924 | bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1925 | StringRef Name; |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 1926 | if (getParser().ParseIdentifier(Name)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1927 | return TokError("expected identifier in directive"); |
| 1928 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1929 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 1930 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1931 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1932 | if (getLexer().isNot(AsmToken::Comma)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1933 | return TokError("unexpected token in '.lsym' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1934 | Lex(); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1935 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1936 | const MCExpr *Value; |
Daniel Dunbar | 38a4e2a | 2010-07-12 19:08:25 +0000 | [diff] [blame] | 1937 | if (getParser().ParseExpression(Value)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1938 | return true; |
| 1939 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1940 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1941 | return TokError("unexpected token in '.lsym' directive"); |
| 1942 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1943 | Lex(); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1944 | |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 1945 | // We don't currently support this directive. |
| 1946 | // |
| 1947 | // FIXME: Diagnostic location! |
| 1948 | (void) Sym; |
| 1949 | return TokError("directive '.lsym' is unsupported"); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1950 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1951 | |
| 1952 | /// ParseDirectiveInclude |
| 1953 | /// ::= .include "filename" |
| 1954 | bool AsmParser::ParseDirectiveInclude() { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1955 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1956 | return TokError("expected string in '.include' directive"); |
| 1957 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1958 | std::string Filename = getTok().getString(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1959 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1960 | Lex(); |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1961 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1962 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1963 | return TokError("unexpected token in '.include' directive"); |
| 1964 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1965 | // Strip the quotes. |
| 1966 | Filename = Filename.substr(1, Filename.size()-2); |
| 1967 | |
| 1968 | // Attempt to switch the lexer to the included file before consuming the end |
| 1969 | // of statement to avoid losing it when we switch. |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 1970 | if (EnterIncludeFile(Filename)) { |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 1971 | PrintMessage(IncludeLoc, |
| 1972 | "Could not find include file '" + Filename + "'", |
| 1973 | "error"); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1974 | return true; |
| 1975 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1976 | |
| 1977 | return false; |
| 1978 | } |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1979 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 1980 | /// ParseDirectiveDumpOrLoad |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1981 | /// ::= ( .dump | .load ) "filename" |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 1982 | bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive, |
| 1983 | SMLoc IDLoc) { |
| 1984 | bool IsDump = Directive == ".dump"; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1985 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1986 | return TokError("expected string in '.dump' or '.load' directive"); |
| 1987 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1988 | Lex(); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1989 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1990 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1991 | return TokError("unexpected token in '.dump' or '.load' directive"); |
| 1992 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1993 | Lex(); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1994 | |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1995 | // FIXME: If/when .dump and .load are implemented they will be done in the |
| 1996 | // the assembly parser and not have any need for an MCStreamer API. |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1997 | if (IsDump) |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1998 | Warning(IDLoc, "ignoring directive .dump for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1999 | else |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 2000 | Warning(IDLoc, "ignoring directive .load for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 2001 | |
| 2002 | return false; |
| 2003 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2004 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2005 | /// ParseDirectiveSecureLogUnique |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2006 | /// ::= .secure_log_unique "log message" |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2007 | bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) { |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2008 | std::string LogMessage; |
| 2009 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2010 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2011 | LogMessage = ""; |
| 2012 | else{ |
| 2013 | LogMessage = getTok().getString(); |
| 2014 | Lex(); |
| 2015 | } |
| 2016 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2017 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2018 | return TokError("unexpected token in '.secure_log_unique' directive"); |
| 2019 | |
| 2020 | if (getContext().getSecureLogUsed() != false) |
| 2021 | return Error(IDLoc, ".secure_log_unique specified multiple times"); |
| 2022 | |
| 2023 | char *SecureLogFile = getContext().getSecureLogFile(); |
| 2024 | if (SecureLogFile == NULL) |
| 2025 | return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE " |
| 2026 | "environment variable unset."); |
| 2027 | |
| 2028 | raw_ostream *OS = getContext().getSecureLog(); |
| 2029 | if (OS == NULL) { |
| 2030 | std::string Err; |
| 2031 | OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append); |
| 2032 | if (!Err.empty()) { |
| 2033 | delete OS; |
| 2034 | return Error(IDLoc, Twine("can't open secure log file: ") + |
| 2035 | SecureLogFile + " (" + Err + ")"); |
| 2036 | } |
| 2037 | getContext().setSecureLog(OS); |
| 2038 | } |
| 2039 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2040 | int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc); |
| 2041 | *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier() |
| 2042 | << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":" |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2043 | << LogMessage + "\n"; |
| 2044 | |
| 2045 | getContext().setSecureLogUsed(true); |
| 2046 | |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2050 | /// ParseDirectiveSecureLogReset |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2051 | /// ::= .secure_log_reset |
Daniel Dunbar | 9ac66b0 | 2010-07-12 18:49:22 +0000 | [diff] [blame] | 2052 | bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2053 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 2054 | return TokError("unexpected token in '.secure_log_reset' directive"); |
| 2055 | |
| 2056 | Lex(); |
| 2057 | |
| 2058 | getContext().setSecureLogUsed(false); |
| 2059 | |
| 2060 | return false; |
| 2061 | } |
| 2062 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2063 | /// ParseDirectiveIf |
| 2064 | /// ::= .if expression |
| 2065 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2066 | TheCondStack.push_back(TheCondState); |
| 2067 | TheCondState.TheCond = AsmCond::IfCond; |
| 2068 | if(TheCondState.Ignore) { |
| 2069 | EatToEndOfStatement(); |
| 2070 | } |
| 2071 | else { |
| 2072 | int64_t ExprValue; |
| 2073 | if (ParseAbsoluteExpression(ExprValue)) |
| 2074 | return true; |
| 2075 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2076 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2077 | return TokError("unexpected token in '.if' directive"); |
| 2078 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2079 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2080 | |
| 2081 | TheCondState.CondMet = ExprValue; |
| 2082 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2083 | } |
| 2084 | |
| 2085 | return false; |
| 2086 | } |
| 2087 | |
| 2088 | /// ParseDirectiveElseIf |
| 2089 | /// ::= .elseif expression |
| 2090 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { |
| 2091 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 2092 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 2093 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 2094 | " an .elseif"); |
| 2095 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 2096 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2097 | bool LastIgnoreState = false; |
| 2098 | if (!TheCondStack.empty()) |
| 2099 | LastIgnoreState = TheCondStack.back().Ignore; |
| 2100 | if (LastIgnoreState || TheCondState.CondMet) { |
| 2101 | TheCondState.Ignore = true; |
| 2102 | EatToEndOfStatement(); |
| 2103 | } |
| 2104 | else { |
| 2105 | int64_t ExprValue; |
| 2106 | if (ParseAbsoluteExpression(ExprValue)) |
| 2107 | return true; |
| 2108 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2109 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2110 | return TokError("unexpected token in '.elseif' directive"); |
| 2111 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2112 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2113 | TheCondState.CondMet = ExprValue; |
| 2114 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2115 | } |
| 2116 | |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
| 2120 | /// ParseDirectiveElse |
| 2121 | /// ::= .else |
| 2122 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2123 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2124 | return TokError("unexpected token in '.else' directive"); |
| 2125 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2126 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2127 | |
| 2128 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 2129 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 2130 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 2131 | ".elseif"); |
| 2132 | TheCondState.TheCond = AsmCond::ElseCond; |
| 2133 | bool LastIgnoreState = false; |
| 2134 | if (!TheCondStack.empty()) |
| 2135 | LastIgnoreState = TheCondStack.back().Ignore; |
| 2136 | if (LastIgnoreState || TheCondState.CondMet) |
| 2137 | TheCondState.Ignore = true; |
| 2138 | else |
| 2139 | TheCondState.Ignore = false; |
| 2140 | |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
| 2144 | /// ParseDirectiveEndIf |
| 2145 | /// ::= .endif |
| 2146 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2147 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2148 | return TokError("unexpected token in '.endif' directive"); |
| 2149 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2150 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2151 | |
| 2152 | if ((TheCondState.TheCond == AsmCond::NoCond) || |
| 2153 | TheCondStack.empty()) |
| 2154 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 2155 | ".else"); |
| 2156 | if (!TheCondStack.empty()) { |
| 2157 | TheCondState = TheCondStack.back(); |
| 2158 | TheCondStack.pop_back(); |
| 2159 | } |
| 2160 | |
| 2161 | return false; |
| 2162 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2163 | |
| 2164 | /// ParseDirectiveFile |
| 2165 | /// ::= .file [number] string |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2166 | bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2167 | // FIXME: I'm not sure what this is. |
| 2168 | int64_t FileNumber = -1; |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2169 | if (getLexer().is(AsmToken::Integer)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2170 | FileNumber = getTok().getIntVal(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2171 | Lex(); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2172 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2173 | if (FileNumber < 1) |
| 2174 | return TokError("file number less than one"); |
| 2175 | } |
| 2176 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2177 | if (getLexer().isNot(AsmToken::String)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2178 | return TokError("unexpected token in '.file' directive"); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2179 | |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 2180 | StringRef Filename = getTok().getString(); |
| 2181 | Filename = Filename.substr(1, Filename.size()-2); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2182 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2183 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2184 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2185 | return TokError("unexpected token in '.file' directive"); |
| 2186 | |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 2187 | if (FileNumber == -1) |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2188 | getStreamer().EmitFileDirective(Filename); |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 2189 | else |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2190 | getStreamer().EmitDwarfFileDirective(FileNumber, Filename); |
| 2191 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2192 | return false; |
| 2193 | } |
| 2194 | |
| 2195 | /// ParseDirectiveLine |
| 2196 | /// ::= .line [number] |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2197 | bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2198 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2199 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2200 | return TokError("unexpected token in '.line' directive"); |
| 2201 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2202 | int64_t LineNumber = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2203 | (void) LineNumber; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2204 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2205 | |
| 2206 | // FIXME: Do something with the .line. |
| 2207 | } |
| 2208 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2209 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 839348a | 2010-07-01 20:20:01 +0000 | [diff] [blame] | 2210 | return TokError("unexpected token in '.line' directive"); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2211 | |
| 2212 | return false; |
| 2213 | } |
| 2214 | |
| 2215 | |
| 2216 | /// ParseDirectiveLoc |
| 2217 | /// ::= .loc number [number [number]] |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2218 | bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2219 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2220 | return TokError("unexpected token in '.loc' directive"); |
| 2221 | |
| 2222 | // FIXME: What are these fields? |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2223 | int64_t FileNumber = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2224 | (void) FileNumber; |
| 2225 | // FIXME: Validate file. |
| 2226 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2227 | Lex(); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2228 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2229 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2230 | return TokError("unexpected token in '.loc' directive"); |
| 2231 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2232 | int64_t Param2 = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2233 | (void) Param2; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2234 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2235 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2236 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2237 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2238 | return TokError("unexpected token in '.loc' directive"); |
| 2239 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2240 | int64_t Param3 = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2241 | (void) Param3; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2242 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2243 | |
| 2244 | // FIXME: Do something with the .loc. |
| 2245 | } |
| 2246 | } |
| 2247 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2248 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2249 | return TokError("unexpected token in '.file' directive"); |
| 2250 | |
| 2251 | return false; |
| 2252 | } |
| 2253 | |