1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCParser/AsmParser.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetAsmParser.h"
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 {
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
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));
55 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
56 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
57 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
60 /// \brief Implementation of directive handling which is shared across all
62 class DarwinAsmParser : public MCAsmParserExtension {
63 bool ParseSectionSwitch(const char *Segment, const char *Section,
64 unsigned TAA = 0, unsigned ImplicitAlign = 0,
65 unsigned StubSize = 0);
70 virtual void Initialize(MCAsmParser &Parser) {
71 // Call the base implementation.
72 this->MCAsmParserExtension::Initialize(Parser);
74 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveDesc));
76 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseDirectiveLsym));
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));
91 Parser.AddDirectiveHandler(this, ".tbss",
92 MCAsmParser::DirectiveHandler(
93 &DarwinAsmParser::ParseDirectiveTBSS));
94 Parser.AddDirectiveHandler(this, ".zerofill",
95 MCAsmParser::DirectiveHandler(
96 &DarwinAsmParser::ParseDirectiveZerofill));
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));
230 bool ParseDirectiveDesc(StringRef, SMLoc);
231 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
232 bool ParseDirectiveLsym(StringRef, SMLoc);
233 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
234 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
235 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
236 bool ParseDirectiveTBSS(StringRef, SMLoc);
237 bool ParseDirectiveZerofill(StringRef, SMLoc);
239 // Named Section Directive
240 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
241 return ParseSectionSwitch("__TEXT", "__const");
243 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
244 return ParseSectionSwitch("__TEXT", "__static_const");
246 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
247 return ParseSectionSwitch("__TEXT","__cstring",
248 MCSectionMachO::S_CSTRING_LITERALS);
250 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
251 return ParseSectionSwitch("__TEXT", "__literal4",
252 MCSectionMachO::S_4BYTE_LITERALS, 4);
254 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
255 return ParseSectionSwitch("__TEXT", "__literal8",
256 MCSectionMachO::S_8BYTE_LITERALS, 8);
258 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
259 return ParseSectionSwitch("__TEXT","__literal16",
260 MCSectionMachO::S_16BYTE_LITERALS, 16);
262 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
263 return ParseSectionSwitch("__TEXT","__constructor");
265 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
266 return ParseSectionSwitch("__TEXT","__destructor");
268 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
269 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
271 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
272 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
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.
281 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
282 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
283 MCSectionMachO::S_SYMBOL_STUBS |
284 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
286 bool ParseSectionDirectiveData(StringRef, SMLoc) {
287 return ParseSectionSwitch("__DATA", "__data");
289 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
290 return ParseSectionSwitch("__DATA", "__static_data");
292 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
293 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
294 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
296 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
297 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
298 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
300 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
301 return ParseSectionSwitch("__DATA", "__dyld");
303 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
304 return ParseSectionSwitch("__DATA", "__mod_init_func",
305 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
307 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
308 return ParseSectionSwitch("__DATA", "__mod_term_func",
309 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
311 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
312 return ParseSectionSwitch("__DATA", "__const");
314 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
315 return ParseSectionSwitch("__OBJC", "__class",
316 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
318 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
319 return ParseSectionSwitch("__OBJC", "__meta_class",
320 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
322 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
323 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
324 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
326 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
327 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
328 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
330 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
331 return ParseSectionSwitch("__OBJC", "__protocol",
332 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
334 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
335 return ParseSectionSwitch("__OBJC", "__string_object",
336 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
338 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
339 return ParseSectionSwitch("__OBJC", "__cls_meth",
340 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
342 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
343 return ParseSectionSwitch("__OBJC", "__inst_meth",
344 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
346 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
347 return ParseSectionSwitch("__OBJC", "__cls_refs",
348 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
349 MCSectionMachO::S_LITERAL_POINTERS, 4);
351 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
352 return ParseSectionSwitch("__OBJC", "__message_refs",
353 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
354 MCSectionMachO::S_LITERAL_POINTERS, 4);
356 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
357 return ParseSectionSwitch("__OBJC", "__symbols",
358 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
360 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
361 return ParseSectionSwitch("__OBJC", "__category",
362 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
364 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
365 return ParseSectionSwitch("__OBJC", "__class_vars",
366 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
368 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
369 return ParseSectionSwitch("__OBJC", "__instance_vars",
370 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
372 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
373 return ParseSectionSwitch("__OBJC", "__module_info",
374 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
376 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
377 return ParseSectionSwitch("__TEXT", "__cstring",
378 MCSectionMachO::S_CSTRING_LITERALS);
380 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
381 return ParseSectionSwitch("__TEXT", "__cstring",
382 MCSectionMachO::S_CSTRING_LITERALS);
384 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
385 return ParseSectionSwitch("__TEXT", "__cstring",
386 MCSectionMachO::S_CSTRING_LITERALS);
388 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
389 return ParseSectionSwitch("__OBJC", "__selector_strs",
390 MCSectionMachO::S_CSTRING_LITERALS);
392 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
393 return ParseSectionSwitch("__DATA", "__thread_data",
394 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
396 bool ParseSectionDirectiveText(StringRef, SMLoc) {
397 return ParseSectionSwitch("__TEXT", "__text",
398 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
400 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
401 return ParseSectionSwitch("__DATA", "__thread_vars",
402 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
404 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
405 return ParseSectionSwitch("__DATA", "__thread_init",
406 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
411 class ELFAsmParser : public MCAsmParserExtension {
412 bool ParseSectionSwitch(StringRef Section, unsigned Type,
413 unsigned Flags, SectionKind Kind);
418 virtual void Initialize(MCAsmParser &Parser) {
419 // Call the base implementation.
420 this->MCAsmParserExtension::Initialize(Parser);
422 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
423 &ELFAsmParser::ParseSectionDirectiveData));
424 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
425 &ELFAsmParser::ParseSectionDirectiveText));
428 bool ParseSectionDirectiveData(StringRef, SMLoc) {
429 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
430 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
431 SectionKind::getDataRel());
433 bool ParseSectionDirectiveText(StringRef, SMLoc) {
434 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
435 MCSectionELF::SHF_EXECINSTR |
436 MCSectionELF::SHF_ALLOC, SectionKind::getText());
442 enum { DEFAULT_ADDRSPACE = 0 };
444 AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
445 MCStreamer &_Out, const MCAsmInfo &_MAI)
446 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
447 GenericParser(new GenericAsmParser), PlatformParser(0),
448 TargetParser(0), CurBuffer(0) {
449 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
451 // Initialize the generic parser.
452 GenericParser->Initialize(*this);
454 // Initialize the platform / file format parser.
456 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
458 if (_MAI.hasSubsectionsViaSymbols()) {
459 PlatformParser = new DarwinAsmParser;
460 PlatformParser->Initialize(*this);
462 PlatformParser = new ELFAsmParser;
463 PlatformParser->Initialize(*this);
467 AsmParser::~AsmParser() {
468 delete PlatformParser;
469 delete GenericParser;
472 void AsmParser::setTargetParser(TargetAsmParser &P) {
473 assert(!TargetParser && "Target parser is already initialized!");
475 TargetParser->Initialize(*this);
478 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
479 PrintMessage(L, Msg.str(), "warning");
482 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
483 PrintMessage(L, Msg.str(), "error");
487 void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
488 const char *Type) const {
489 SrcMgr.PrintMessage(Loc, Msg, Type);
492 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
493 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
499 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
504 const AsmToken &AsmParser::Lex() {
505 const AsmToken *tok = &Lexer.Lex();
507 if (tok->is(AsmToken::Eof)) {
508 // If this is the end of an included file, pop the parent file off the
510 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
511 if (ParentIncludeLoc != SMLoc()) {
512 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
513 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
514 ParentIncludeLoc.getPointer());
519 if (tok->is(AsmToken::Error))
520 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
525 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
526 // Create the initial section, if requested.
528 // FIXME: Target hook & command line option for initial section.
529 if (!NoInitialTextSection)
530 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
532 0, SectionKind::getText()));
537 bool HadError = false;
539 AsmCond StartingCondState = TheCondState;
541 // While we have input, parse each statement.
542 while (Lexer.isNot(AsmToken::Eof)) {
543 if (!ParseStatement()) continue;
545 // We had an error, remember it and recover by skipping to the next line.
547 EatToEndOfStatement();
550 if (TheCondState.TheCond != StartingCondState.TheCond ||
551 TheCondState.Ignore != StartingCondState.Ignore)
552 return TokError("unmatched .ifs or .elses");
554 // Finalize the output stream if there are no errors and if the client wants
556 if (!HadError && !NoFinalize)
562 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
563 void AsmParser::EatToEndOfStatement() {
564 while (Lexer.isNot(AsmToken::EndOfStatement) &&
565 Lexer.isNot(AsmToken::Eof))
569 if (Lexer.is(AsmToken::EndOfStatement))
574 /// ParseParenExpr - Parse a paren expression and return it.
575 /// NOTE: This assumes the leading '(' has already been consumed.
577 /// parenexpr ::= expr)
579 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
580 if (ParseExpression(Res)) return true;
581 if (Lexer.isNot(AsmToken::RParen))
582 return TokError("expected ')' in parentheses expression");
583 EndLoc = Lexer.getLoc();
588 /// ParsePrimaryExpr - Parse a primary expression and return it.
589 /// primaryexpr ::= (parenexpr
590 /// primaryexpr ::= symbol
591 /// primaryexpr ::= number
592 /// primaryexpr ::= '.'
593 /// primaryexpr ::= ~,+,- primaryexpr
594 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
595 switch (Lexer.getKind()) {
597 return TokError("unknown token in expression");
598 case AsmToken::Exclaim:
599 Lex(); // Eat the operator.
600 if (ParsePrimaryExpr(Res, EndLoc))
602 Res = MCUnaryExpr::CreateLNot(Res, getContext());
604 case AsmToken::String:
605 case AsmToken::Identifier: {
606 // This is a symbol reference.
607 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
608 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
610 // Mark the symbol as used in an expression.
611 Sym->setUsedInExpr(true);
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);
618 EndLoc = Lexer.getLoc();
619 Lex(); // Eat identifier.
621 // If this is an absolute variable reference, substitute it now to preserve
622 // semantics in the face of reassignment.
623 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
625 return Error(EndLoc, "unexpected modified on variable reference");
627 Res = Sym->getVariableValue();
631 // Otherwise create a symbol ref.
632 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
635 case AsmToken::Integer: {
636 SMLoc Loc = getTok().getLoc();
637 int64_t IntVal = getTok().getIntVal();
638 Res = MCConstantExpr::Create(IntVal, getContext());
639 EndLoc = Lexer.getLoc();
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,
649 if(IDVal == "b" && Sym->isUndefined())
650 return Error(Loc, "invalid reference to undefined symbol");
651 EndLoc = Lexer.getLoc();
652 Lex(); // Eat identifier.
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();
662 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
663 EndLoc = Lexer.getLoc();
664 Lex(); // Eat identifier.
668 case AsmToken::LParen:
669 Lex(); // Eat the '('.
670 return ParseParenExpr(Res, EndLoc);
671 case AsmToken::Minus:
672 Lex(); // Eat the operator.
673 if (ParsePrimaryExpr(Res, EndLoc))
675 Res = MCUnaryExpr::CreateMinus(Res, getContext());
678 Lex(); // Eat the operator.
679 if (ParsePrimaryExpr(Res, EndLoc))
681 Res = MCUnaryExpr::CreatePlus(Res, getContext());
683 case AsmToken::Tilde:
684 Lex(); // Eat the operator.
685 if (ParsePrimaryExpr(Res, EndLoc))
687 Res = MCUnaryExpr::CreateNot(Res, getContext());
692 bool AsmParser::ParseExpression(const MCExpr *&Res) {
694 return ParseExpression(Res, EndLoc);
697 /// ParseExpression - Parse an expression and return it.
699 /// expr ::= expr +,- expr -> lowest.
700 /// expr ::= expr |,^,&,! expr -> middle.
701 /// expr ::= expr *,/,%,<<,>> expr -> highest.
702 /// expr ::= primaryexpr
704 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
705 // Parse the expression.
707 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
710 // Try to constant fold it up front, if possible.
712 if (Res->EvaluateAsAbsolute(Value))
713 Res = MCConstantExpr::Create(Value, getContext());
718 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
720 return ParseParenExpr(Res, EndLoc) ||
721 ParseBinOpRHS(1, Res, EndLoc);
724 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
727 SMLoc StartLoc = Lexer.getLoc();
728 if (ParseExpression(Expr))
731 if (!Expr->EvaluateAsAbsolute(Res))
732 return Error(StartLoc, "expected absolute expression");
737 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
738 MCBinaryExpr::Opcode &Kind) {
741 return 0; // not a binop.
743 // Lowest Precedence: &&, ||
744 case AsmToken::AmpAmp:
745 Kind = MCBinaryExpr::LAnd;
747 case AsmToken::PipePipe:
748 Kind = MCBinaryExpr::LOr;
751 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
753 Kind = MCBinaryExpr::Add;
755 case AsmToken::Minus:
756 Kind = MCBinaryExpr::Sub;
758 case AsmToken::EqualEqual:
759 Kind = MCBinaryExpr::EQ;
761 case AsmToken::ExclaimEqual:
762 case AsmToken::LessGreater:
763 Kind = MCBinaryExpr::NE;
766 Kind = MCBinaryExpr::LT;
768 case AsmToken::LessEqual:
769 Kind = MCBinaryExpr::LTE;
771 case AsmToken::Greater:
772 Kind = MCBinaryExpr::GT;
774 case AsmToken::GreaterEqual:
775 Kind = MCBinaryExpr::GTE;
778 // Intermediate Precedence: |, &, ^
780 // FIXME: gas seems to support '!' as an infix operator?
782 Kind = MCBinaryExpr::Or;
784 case AsmToken::Caret:
785 Kind = MCBinaryExpr::Xor;
788 Kind = MCBinaryExpr::And;
791 // Highest Precedence: *, /, %, <<, >>
793 Kind = MCBinaryExpr::Mul;
795 case AsmToken::Slash:
796 Kind = MCBinaryExpr::Div;
798 case AsmToken::Percent:
799 Kind = MCBinaryExpr::Mod;
801 case AsmToken::LessLess:
802 Kind = MCBinaryExpr::Shl;
804 case AsmToken::GreaterGreater:
805 Kind = MCBinaryExpr::Shr;
811 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
812 /// Res contains the LHS of the expression on input.
813 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
816 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
817 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
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)
826 // Eat the next primary expression.
828 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
830 // If BinOp binds less tightly with RHS than the operator after RHS, let
831 // the pending operator take RHS as its LHS.
832 MCBinaryExpr::Opcode Dummy;
833 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
834 if (TokPrec < NextTokPrec) {
835 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
838 // Merge LHS and RHS according to operator.
839 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
847 /// ::= EndOfStatement
848 /// ::= Label* Directive ...Operands... EndOfStatement
849 /// ::= Label* Identifier OperandList* EndOfStatement
850 bool AsmParser::ParseStatement() {
851 if (Lexer.is(AsmToken::EndOfStatement)) {
857 // Statements always start with an identifier.
858 AsmToken ID = getTok();
859 SMLoc IDLoc = ID.getLoc();
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");
871 IDVal = getTok().getString();
872 Lex(); // Consume the integer token to be used as an identifier token.
873 if (Lexer.getKind() != AsmToken::Colon) {
874 if (!TheCondState.Ignore)
875 return TokError("unexpected token at start of statement");
879 else if (ParseIdentifier(IDVal)) {
880 if (!TheCondState.Ignore)
881 return TokError("unexpected token at start of statement");
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
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);
897 // If we are in a ".if 0" block, ignore this statement.
898 if (TheCondState.Ignore) {
899 EatToEndOfStatement();
903 // FIXME: Recurse on local labels?
905 // See what kind of statement we have.
906 switch (Lexer.getKind()) {
907 case AsmToken::Colon: {
908 // identifier ':' -> Label.
911 // Diagnose attempt to use a variable as a label.
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.
917 if (LocalLabelVal == -1)
918 Sym = getContext().GetOrCreateSymbol(IDVal);
920 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
921 if (!Sym->isUndefined() || Sym->isVariable())
922 return Error(IDLoc, "invalid symbol redefinition");
927 // Consume any end of statement token, if present, to avoid spurious
928 // AddBlankLine calls().
929 if (Lexer.is(AsmToken::EndOfStatement)) {
931 if (Lexer.is(AsmToken::Eof))
935 return ParseStatement();
938 case AsmToken::Equal:
939 // identifier '=' ... -> assignment statement
942 return ParseAssignment(IDVal);
944 default: // Normal instruction or directive.
948 // Otherwise, we have a normal instruction or directive.
949 if (IDVal[0] == '.') {
950 // FIXME: This should be driven based on a hash lookup and callback.
951 if (IDVal == ".section")
952 return ParseDirectiveDarwinSection();
954 // Assembler features
956 return ParseDirectiveSet();
960 if (IDVal == ".ascii")
961 return ParseDirectiveAscii(false);
962 if (IDVal == ".asciz")
963 return ParseDirectiveAscii(true);
965 if (IDVal == ".byte")
966 return ParseDirectiveValue(1);
967 if (IDVal == ".short")
968 return ParseDirectiveValue(2);
969 if (IDVal == ".long")
970 return ParseDirectiveValue(4);
971 if (IDVal == ".quad")
972 return ParseDirectiveValue(8);
974 // FIXME: Target hooks for IsPow2.
975 if (IDVal == ".align")
976 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
977 if (IDVal == ".align32")
978 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
979 if (IDVal == ".balign")
980 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
981 if (IDVal == ".balignw")
982 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
983 if (IDVal == ".balignl")
984 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
985 if (IDVal == ".p2align")
986 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
987 if (IDVal == ".p2alignw")
988 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
989 if (IDVal == ".p2alignl")
990 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
993 return ParseDirectiveOrg();
995 if (IDVal == ".fill")
996 return ParseDirectiveFill();
997 if (IDVal == ".space")
998 return ParseDirectiveSpace();
1000 // Symbol attribute directives
1002 if (IDVal == ".globl" || IDVal == ".global")
1003 return ParseDirectiveSymbolAttribute(MCSA_Global);
1004 if (IDVal == ".hidden")
1005 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
1006 if (IDVal == ".indirect_symbol")
1007 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
1008 if (IDVal == ".internal")
1009 return ParseDirectiveSymbolAttribute(MCSA_Internal);
1010 if (IDVal == ".lazy_reference")
1011 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
1012 if (IDVal == ".no_dead_strip")
1013 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
1014 if (IDVal == ".private_extern")
1015 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
1016 if (IDVal == ".protected")
1017 return ParseDirectiveSymbolAttribute(MCSA_Protected);
1018 if (IDVal == ".reference")
1019 return ParseDirectiveSymbolAttribute(MCSA_Reference);
1020 if (IDVal == ".type")
1021 return ParseDirectiveELFType();
1022 if (IDVal == ".weak")
1023 return ParseDirectiveSymbolAttribute(MCSA_Weak);
1024 if (IDVal == ".weak_definition")
1025 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
1026 if (IDVal == ".weak_reference")
1027 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
1028 if (IDVal == ".weak_def_can_be_hidden")
1029 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
1031 if (IDVal == ".comm")
1032 return ParseDirectiveComm(/*IsLocal=*/false);
1033 if (IDVal == ".lcomm")
1034 return ParseDirectiveComm(/*IsLocal=*/true);
1036 if (IDVal == ".abort")
1037 return ParseDirectiveAbort();
1038 if (IDVal == ".include")
1039 return ParseDirectiveInclude();
1041 // Look up the handler in the handler table.
1042 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1043 DirectiveMap.lookup(IDVal);
1045 return (Handler.first->*Handler.second)(IDVal, IDLoc);
1047 // Target hook for parsing target specific directives.
1048 if (!getTargetParser().ParseDirective(ID))
1051 Warning(IDLoc, "ignoring directive for now");
1052 EatToEndOfStatement();
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]));
1061 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
1062 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
1064 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
1065 HadError = TokError("unexpected token in argument list");
1067 // If parsing succeeded, match the instruction.
1070 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
1071 // Emit the instruction on success.
1072 Out.EmitInstruction(Inst);
1074 // Otherwise emit a diagnostic about the match failure and set the error
1077 // FIXME: We should give nicer diagnostics about the exact failure.
1078 Error(IDLoc, "unrecognized instruction");
1083 // If there was no error, consume the end-of-statement token. Otherwise this
1084 // will be done by our caller.
1088 // Free any parsed operands.
1089 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1090 delete ParsedOperands[i];
1095 bool AsmParser::ParseAssignment(const StringRef &Name) {
1096 // FIXME: Use better location, we should use proper tokens.
1097 SMLoc EqualLoc = Lexer.getLoc();
1099 const MCExpr *Value;
1100 if (ParseExpression(Value))
1103 if (Lexer.isNot(AsmToken::EndOfStatement))
1104 return TokError("unexpected token in assignment");
1106 // Eat the end of statement marker.
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);
1113 // Diagnose assignment to a label.
1115 // FIXME: Diagnostics. Note the location of the definition as a label.
1116 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
1117 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1118 ; // Allow redefinitions of undefined symbols only used in directives.
1119 else if (!Sym->isUndefined() && !Sym->isAbsolute())
1120 return Error(EqualLoc, "redefinition of '" + Name + "'");
1121 else if (!Sym->isVariable())
1122 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
1123 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
1124 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1127 Sym = getContext().GetOrCreateSymbol(Name);
1129 // FIXME: Handle '.'.
1131 Sym->setUsedInExpr(true);
1133 // Do the assignment.
1134 Out.EmitAssignment(Sym, Value);
1139 /// ParseIdentifier:
1142 bool AsmParser::ParseIdentifier(StringRef &Res) {
1143 if (Lexer.isNot(AsmToken::Identifier) &&
1144 Lexer.isNot(AsmToken::String))
1147 Res = getTok().getIdentifier();
1149 Lex(); // Consume the identifier token.
1154 /// ParseDirectiveSet:
1155 /// ::= .set identifier ',' expression
1156 bool AsmParser::ParseDirectiveSet() {
1159 if (ParseIdentifier(Name))
1160 return TokError("expected identifier after '.set' directive");
1162 if (getLexer().isNot(AsmToken::Comma))
1163 return TokError("unexpected token in '.set'");
1166 return ParseAssignment(Name);
1169 /// ParseDirectiveSection:
1170 /// ::= .section identifier (',' identifier)*
1171 /// FIXME: This should actually parse out the segment, section, attributes and
1172 /// sizeof_stub fields.
1173 bool AsmParser::ParseDirectiveDarwinSection() {
1174 SMLoc Loc = getLexer().getLoc();
1176 StringRef SectionName;
1177 if (ParseIdentifier(SectionName))
1178 return Error(Loc, "expected identifier after '.section' directive");
1180 // Verify there is a following comma.
1181 if (!getLexer().is(AsmToken::Comma))
1182 return TokError("unexpected token in '.section' directive");
1184 std::string SectionSpec = SectionName;
1187 // Add all the tokens until the end of the line, ParseSectionSpecifier will
1189 StringRef EOL = Lexer.LexUntilEndOfStatement();
1190 SectionSpec.append(EOL.begin(), EOL.end());
1193 if (getLexer().isNot(AsmToken::EndOfStatement))
1194 return TokError("unexpected token in '.section' directive");
1198 StringRef Segment, Section;
1199 unsigned TAA, StubSize;
1200 std::string ErrorStr =
1201 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1204 if (!ErrorStr.empty())
1205 return Error(Loc, ErrorStr.c_str());
1207 // FIXME: Arch specific.
1208 bool isText = Segment == "__TEXT"; // FIXME: Hack.
1209 getStreamer().SwitchSection(Ctx.getMachOSection(
1210 Segment, Section, TAA, StubSize,
1211 isText ? SectionKind::getText()
1212 : SectionKind::getDataRel()));
1216 bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
1217 const char *Section,
1218 unsigned TAA, unsigned Align,
1219 unsigned StubSize) {
1220 if (getLexer().isNot(AsmToken::EndOfStatement))
1221 return TokError("unexpected token in section switching directive");
1224 // FIXME: Arch specific.
1225 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
1226 getStreamer().SwitchSection(getContext().getMachOSection(
1227 Segment, Section, TAA, StubSize,
1228 isText ? SectionKind::getText()
1229 : SectionKind::getDataRel()));
1231 // Set the implicit alignment, if any.
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.
1240 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
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");
1251 getStreamer().SwitchSection(getContext().getELFSection(
1252 Section, Type, Flags, Kind));
1257 bool AsmParser::ParseEscapedString(std::string &Data) {
1258 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
1261 StringRef Str = getTok().getStringContents();
1262 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1263 if (Str[i] != '\\') {
1268 // Recognize escaped characters. Note that this escape semantics currently
1269 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1272 return TokError("unexpected backslash at end of string");
1274 // Recognize octal sequences.
1275 if ((unsigned) (Str[i] - '0') <= 7) {
1276 // Consume up to three octal characters.
1277 unsigned Value = Str[i] - '0';
1279 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1281 Value = Value * 8 + (Str[i] - '0');
1283 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1285 Value = Value * 8 + (Str[i] - '0');
1290 return TokError("invalid octal escape sequence (out of range)");
1292 Data += (unsigned char) Value;
1296 // Otherwise recognize individual escapes.
1299 // Just reject invalid escape sequences for now.
1300 return TokError("invalid escape sequence (unrecognized character)");
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;
1315 /// ParseDirectiveAscii:
1316 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
1317 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
1318 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1320 if (getLexer().isNot(AsmToken::String))
1321 return TokError("expected string in '.ascii' or '.asciz' directive");
1324 if (ParseEscapedString(Data))
1327 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
1329 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1333 if (getLexer().is(AsmToken::EndOfStatement))
1336 if (getLexer().isNot(AsmToken::Comma))
1337 return TokError("unexpected token in '.ascii' or '.asciz' directive");
1346 /// ParseDirectiveValue
1347 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1348 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1349 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1351 const MCExpr *Value;
1352 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
1353 if (ParseExpression(Value))
1356 // Special case constant expressions to match code generator.
1357 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1358 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1360 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1362 if (getLexer().is(AsmToken::EndOfStatement))
1365 // FIXME: Improve diagnostic.
1366 if (getLexer().isNot(AsmToken::Comma))
1367 return TokError("unexpected token in directive");
1376 /// ParseDirectiveSpace
1377 /// ::= .space expression [ , expression ]
1378 bool AsmParser::ParseDirectiveSpace() {
1380 if (ParseAbsoluteExpression(NumBytes))
1383 int64_t FillExpr = 0;
1384 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1385 if (getLexer().isNot(AsmToken::Comma))
1386 return TokError("unexpected token in '.space' directive");
1389 if (ParseAbsoluteExpression(FillExpr))
1392 if (getLexer().isNot(AsmToken::EndOfStatement))
1393 return TokError("unexpected token in '.space' directive");
1399 return TokError("invalid number of bytes in '.space' directive");
1401 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1402 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1407 /// ParseDirectiveFill
1408 /// ::= .fill expression , expression , expression
1409 bool AsmParser::ParseDirectiveFill() {
1411 if (ParseAbsoluteExpression(NumValues))
1414 if (getLexer().isNot(AsmToken::Comma))
1415 return TokError("unexpected token in '.fill' directive");
1419 if (ParseAbsoluteExpression(FillSize))
1422 if (getLexer().isNot(AsmToken::Comma))
1423 return TokError("unexpected token in '.fill' directive");
1427 if (ParseAbsoluteExpression(FillExpr))
1430 if (getLexer().isNot(AsmToken::EndOfStatement))
1431 return TokError("unexpected token in '.fill' directive");
1435 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1436 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1438 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1439 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1444 /// ParseDirectiveOrg
1445 /// ::= .org expression [ , expression ]
1446 bool AsmParser::ParseDirectiveOrg() {
1447 const MCExpr *Offset;
1448 if (ParseExpression(Offset))
1451 // Parse optional fill expression.
1452 int64_t FillExpr = 0;
1453 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1454 if (getLexer().isNot(AsmToken::Comma))
1455 return TokError("unexpected token in '.org' directive");
1458 if (ParseAbsoluteExpression(FillExpr))
1461 if (getLexer().isNot(AsmToken::EndOfStatement))
1462 return TokError("unexpected token in '.org' directive");
1467 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1468 // has to be relative to the current section.
1469 getStreamer().EmitValueToOffset(Offset, FillExpr);
1474 /// ParseDirectiveAlign
1475 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1476 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1477 SMLoc AlignmentLoc = getLexer().getLoc();
1479 if (ParseAbsoluteExpression(Alignment))
1483 bool HasFillExpr = false;
1484 int64_t FillExpr = 0;
1485 int64_t MaxBytesToFill = 0;
1486 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1487 if (getLexer().isNot(AsmToken::Comma))
1488 return TokError("unexpected token in directive");
1491 // The fill expression can be omitted while specifying a maximum number of
1492 // alignment bytes, e.g:
1494 if (getLexer().isNot(AsmToken::Comma)) {
1496 if (ParseAbsoluteExpression(FillExpr))
1500 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1501 if (getLexer().isNot(AsmToken::Comma))
1502 return TokError("unexpected token in directive");
1505 MaxBytesLoc = getLexer().getLoc();
1506 if (ParseAbsoluteExpression(MaxBytesToFill))
1509 if (getLexer().isNot(AsmToken::EndOfStatement))
1510 return TokError("unexpected token in directive");
1519 // Compute alignment in bytes.
1521 // FIXME: Diagnose overflow.
1522 if (Alignment >= 32) {
1523 Error(AlignmentLoc, "invalid alignment value");
1527 Alignment = 1ULL << Alignment;
1530 // Diagnose non-sensical max bytes to align.
1531 if (MaxBytesLoc.isValid()) {
1532 if (MaxBytesToFill < 1) {
1533 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1534 "many bytes, ignoring maximum bytes expression");
1538 if (MaxBytesToFill >= Alignment) {
1539 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1545 // Check whether we should use optimal code alignment for this .align
1548 // FIXME: This should be using a target hook.
1549 bool UseCodeAlign = false;
1550 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1551 getStreamer().getCurrentSection()))
1552 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1553 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1554 ValueSize == 1 && UseCodeAlign) {
1555 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
1557 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1558 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1564 /// ParseDirectiveSymbolAttribute
1565 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1566 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1571 if (ParseIdentifier(Name))
1572 return TokError("expected identifier in directive");
1574 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1576 getStreamer().EmitSymbolAttribute(Sym, Attr);
1578 if (getLexer().is(AsmToken::EndOfStatement))
1581 if (getLexer().isNot(AsmToken::Comma))
1582 return TokError("unexpected token in directive");
1591 /// ParseDirectiveELFType
1592 /// ::= .type identifier , @attribute
1593 bool AsmParser::ParseDirectiveELFType() {
1595 if (ParseIdentifier(Name))
1596 return TokError("expected identifier in directive");
1598 // Handle the identifier as the key symbol.
1599 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1601 if (getLexer().isNot(AsmToken::Comma))
1602 return TokError("unexpected token in '.type' directive");
1605 if (getLexer().isNot(AsmToken::At))
1606 return TokError("expected '@' before type");
1612 TypeLoc = getLexer().getLoc();
1613 if (ParseIdentifier(Type))
1614 return TokError("expected symbol type in directive");
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);
1624 if (Attr == MCSA_Invalid)
1625 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1627 if (getLexer().isNot(AsmToken::EndOfStatement))
1628 return TokError("unexpected token in '.type' directive");
1632 getStreamer().EmitSymbolAttribute(Sym, Attr);
1637 /// ParseDirectiveDesc
1638 /// ::= .desc identifier , expression
1639 bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
1641 if (getParser().ParseIdentifier(Name))
1642 return TokError("expected identifier in directive");
1644 // Handle the identifier as the key symbol.
1645 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1647 if (getLexer().isNot(AsmToken::Comma))
1648 return TokError("unexpected token in '.desc' directive");
1652 if (getParser().ParseAbsoluteExpression(DescValue))
1655 if (getLexer().isNot(AsmToken::EndOfStatement))
1656 return TokError("unexpected token in '.desc' directive");
1660 // Set the n_desc field of this Symbol to this DescValue
1661 getStreamer().EmitSymbolDesc(Sym, DescValue);
1666 /// ParseDirectiveComm
1667 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1668 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1669 SMLoc IDLoc = getLexer().getLoc();
1671 if (ParseIdentifier(Name))
1672 return TokError("expected identifier in directive");
1674 // Handle the identifier as the key symbol.
1675 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1677 if (getLexer().isNot(AsmToken::Comma))
1678 return TokError("unexpected token in directive");
1682 SMLoc SizeLoc = getLexer().getLoc();
1683 if (ParseAbsoluteExpression(Size))
1686 int64_t Pow2Alignment = 0;
1687 SMLoc Pow2AlignmentLoc;
1688 if (getLexer().is(AsmToken::Comma)) {
1690 Pow2AlignmentLoc = getLexer().getLoc();
1691 if (ParseAbsoluteExpression(Pow2Alignment))
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);
1702 if (getLexer().isNot(AsmToken::EndOfStatement))
1703 return TokError("unexpected token in '.comm' or '.lcomm' directive");
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.
1710 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1711 "be less than zero");
1713 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1714 // may internally end up wanting an alignment in bytes.
1715 // FIXME: Diagnose overflow.
1716 if (Pow2Alignment < 0)
1717 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1718 "alignment, can't be less than zero");
1720 if (!Sym->isUndefined())
1721 return Error(IDLoc, "invalid symbol redefinition");
1723 // '.lcomm' is equivalent to '.zerofill'.
1724 // Create the Symbol as a common or local common with Size and Pow2Alignment
1726 getStreamer().EmitZerofill(Ctx.getMachOSection(
1727 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1728 0, SectionKind::getBSS()),
1729 Sym, Size, 1 << Pow2Alignment);
1733 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1737 /// ParseDirectiveZerofill
1738 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1739 /// , align_expression ]]
1740 bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
1742 if (getParser().ParseIdentifier(Segment))
1743 return TokError("expected segment name after '.zerofill' directive");
1745 if (getLexer().isNot(AsmToken::Comma))
1746 return TokError("unexpected token in directive");
1750 if (getParser().ParseIdentifier(Section))
1751 return TokError("expected section name after comma in '.zerofill' "
1754 // If this is the end of the line all that was wanted was to create the
1755 // the section but with no symbol.
1756 if (getLexer().is(AsmToken::EndOfStatement)) {
1757 // Create the zerofill section but no symbol
1758 getStreamer().EmitZerofill(getContext().getMachOSection(
1759 Segment, Section, MCSectionMachO::S_ZEROFILL,
1760 0, SectionKind::getBSS()));
1764 if (getLexer().isNot(AsmToken::Comma))
1765 return TokError("unexpected token in directive");
1768 SMLoc IDLoc = getLexer().getLoc();
1770 if (getParser().ParseIdentifier(IDStr))
1771 return TokError("expected identifier in directive");
1773 // handle the identifier as the key symbol.
1774 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
1776 if (getLexer().isNot(AsmToken::Comma))
1777 return TokError("unexpected token in directive");
1781 SMLoc SizeLoc = getLexer().getLoc();
1782 if (getParser().ParseAbsoluteExpression(Size))
1785 int64_t Pow2Alignment = 0;
1786 SMLoc Pow2AlignmentLoc;
1787 if (getLexer().is(AsmToken::Comma)) {
1789 Pow2AlignmentLoc = getLexer().getLoc();
1790 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
1794 if (getLexer().isNot(AsmToken::EndOfStatement))
1795 return TokError("unexpected token in '.zerofill' directive");
1800 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1803 // NOTE: The alignment in the directive is a power of 2 value, the assembler
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");
1810 if (!Sym->isUndefined())
1811 return Error(IDLoc, "invalid symbol redefinition");
1813 // Create the zerofill Symbol with Size and Pow2Alignment
1815 // FIXME: Arch specific.
1816 getStreamer().EmitZerofill(getContext().getMachOSection(
1817 Segment, Section, MCSectionMachO::S_ZEROFILL,
1818 0, SectionKind::getBSS()),
1819 Sym, Size, 1 << Pow2Alignment);
1824 /// ParseDirectiveTBSS
1825 /// ::= .tbss identifier, size, align
1826 bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
1827 SMLoc IDLoc = getLexer().getLoc();
1829 if (getParser().ParseIdentifier(Name))
1830 return TokError("expected identifier in directive");
1832 // Handle the identifier as the key symbol.
1833 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1835 if (getLexer().isNot(AsmToken::Comma))
1836 return TokError("unexpected token in directive");
1840 SMLoc SizeLoc = getLexer().getLoc();
1841 if (getParser().ParseAbsoluteExpression(Size))
1844 int64_t Pow2Alignment = 0;
1845 SMLoc Pow2AlignmentLoc;
1846 if (getLexer().is(AsmToken::Comma)) {
1848 Pow2AlignmentLoc = getLexer().getLoc();
1849 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
1853 if (getLexer().isNot(AsmToken::EndOfStatement))
1854 return TokError("unexpected token in '.tbss' directive");
1859 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1862 // FIXME: Diagnose overflow.
1863 if (Pow2Alignment < 0)
1864 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1867 if (!Sym->isUndefined())
1868 return Error(IDLoc, "invalid symbol redefinition");
1870 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
1871 "__DATA", "__thread_bss",
1872 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1873 0, SectionKind::getThreadBSS()),
1874 Sym, Size, 1 << Pow2Alignment);
1879 /// ParseDirectiveSubsectionsViaSymbols
1880 /// ::= .subsections_via_symbols
1881 bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
1882 if (getLexer().isNot(AsmToken::EndOfStatement))
1883 return TokError("unexpected token in '.subsections_via_symbols' directive");
1887 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1892 /// ParseDirectiveAbort
1893 /// ::= .abort [ "abort_string" ]
1894 bool AsmParser::ParseDirectiveAbort() {
1895 // FIXME: Use loc from directive.
1896 SMLoc Loc = getLexer().getLoc();
1899 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1900 if (getLexer().isNot(AsmToken::String))
1901 return TokError("expected string in '.abort' directive");
1903 Str = getTok().getString();
1908 if (getLexer().isNot(AsmToken::EndOfStatement))
1909 return TokError("unexpected token in '.abort' directive");
1913 // FIXME: Handle here.
1915 Error(Loc, ".abort detected. Assembly stopping.");
1917 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1922 /// ParseDirectiveLsym
1923 /// ::= .lsym identifier , expression
1924 bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
1926 if (getParser().ParseIdentifier(Name))
1927 return TokError("expected identifier in directive");
1929 // Handle the identifier as the key symbol.
1930 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
1932 if (getLexer().isNot(AsmToken::Comma))
1933 return TokError("unexpected token in '.lsym' directive");
1936 const MCExpr *Value;
1937 if (getParser().ParseExpression(Value))
1940 if (getLexer().isNot(AsmToken::EndOfStatement))
1941 return TokError("unexpected token in '.lsym' directive");
1945 // We don't currently support this directive.
1947 // FIXME: Diagnostic location!
1949 return TokError("directive '.lsym' is unsupported");
1952 /// ParseDirectiveInclude
1953 /// ::= .include "filename"
1954 bool AsmParser::ParseDirectiveInclude() {
1955 if (getLexer().isNot(AsmToken::String))
1956 return TokError("expected string in '.include' directive");
1958 std::string Filename = getTok().getString();
1959 SMLoc IncludeLoc = getLexer().getLoc();
1962 if (getLexer().isNot(AsmToken::EndOfStatement))
1963 return TokError("unexpected token in '.include' directive");
1965 // Strip the quotes.
1966 Filename = Filename.substr(1, Filename.size()-2);
1968 // Attempt to switch the lexer to the included file before consuming the end
1969 // of statement to avoid losing it when we switch.
1970 if (EnterIncludeFile(Filename)) {
1971 PrintMessage(IncludeLoc,
1972 "Could not find include file '" + Filename + "'",
1980 /// ParseDirectiveDumpOrLoad
1981 /// ::= ( .dump | .load ) "filename"
1982 bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1984 bool IsDump = Directive == ".dump";
1985 if (getLexer().isNot(AsmToken::String))
1986 return TokError("expected string in '.dump' or '.load' directive");
1990 if (getLexer().isNot(AsmToken::EndOfStatement))
1991 return TokError("unexpected token in '.dump' or '.load' directive");
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.
1998 Warning(IDLoc, "ignoring directive .dump for now");
2000 Warning(IDLoc, "ignoring directive .load for now");
2005 /// ParseDirectiveSecureLogUnique
2006 /// ::= .secure_log_unique "log message"
2007 bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
2008 std::string LogMessage;
2010 if (getLexer().isNot(AsmToken::String))
2013 LogMessage = getTok().getString();
2017 if (getLexer().isNot(AsmToken::EndOfStatement))
2018 return TokError("unexpected token in '.secure_log_unique' directive");
2020 if (getContext().getSecureLogUsed() != false)
2021 return Error(IDLoc, ".secure_log_unique specified multiple times");
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.");
2028 raw_ostream *OS = getContext().getSecureLog();
2031 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
2034 return Error(IDLoc, Twine("can't open secure log file: ") +
2035 SecureLogFile + " (" + Err + ")");
2037 getContext().setSecureLog(OS);
2040 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
2041 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
2042 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
2043 << LogMessage + "\n";
2045 getContext().setSecureLogUsed(true);
2050 /// ParseDirectiveSecureLogReset
2051 /// ::= .secure_log_reset
2052 bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
2053 if (getLexer().isNot(AsmToken::EndOfStatement))
2054 return TokError("unexpected token in '.secure_log_reset' directive");
2058 getContext().setSecureLogUsed(false);
2063 /// ParseDirectiveIf
2064 /// ::= .if expression
2065 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
2066 TheCondStack.push_back(TheCondState);
2067 TheCondState.TheCond = AsmCond::IfCond;
2068 if(TheCondState.Ignore) {
2069 EatToEndOfStatement();
2073 if (ParseAbsoluteExpression(ExprValue))
2076 if (getLexer().isNot(AsmToken::EndOfStatement))
2077 return TokError("unexpected token in '.if' directive");
2081 TheCondState.CondMet = ExprValue;
2082 TheCondState.Ignore = !TheCondState.CondMet;
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 "
2095 TheCondState.TheCond = AsmCond::ElseIfCond;
2097 bool LastIgnoreState = false;
2098 if (!TheCondStack.empty())
2099 LastIgnoreState = TheCondStack.back().Ignore;
2100 if (LastIgnoreState || TheCondState.CondMet) {
2101 TheCondState.Ignore = true;
2102 EatToEndOfStatement();
2106 if (ParseAbsoluteExpression(ExprValue))
2109 if (getLexer().isNot(AsmToken::EndOfStatement))
2110 return TokError("unexpected token in '.elseif' directive");
2113 TheCondState.CondMet = ExprValue;
2114 TheCondState.Ignore = !TheCondState.CondMet;
2120 /// ParseDirectiveElse
2122 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
2123 if (getLexer().isNot(AsmToken::EndOfStatement))
2124 return TokError("unexpected token in '.else' directive");
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 "
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;
2139 TheCondState.Ignore = false;
2144 /// ParseDirectiveEndIf
2146 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
2147 if (getLexer().isNot(AsmToken::EndOfStatement))
2148 return TokError("unexpected token in '.endif' directive");
2152 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2153 TheCondStack.empty())
2154 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2156 if (!TheCondStack.empty()) {
2157 TheCondState = TheCondStack.back();
2158 TheCondStack.pop_back();
2164 /// ParseDirectiveFile
2165 /// ::= .file [number] string
2166 bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
2167 // FIXME: I'm not sure what this is.
2168 int64_t FileNumber = -1;
2169 if (getLexer().is(AsmToken::Integer)) {
2170 FileNumber = getTok().getIntVal();
2174 return TokError("file number less than one");
2177 if (getLexer().isNot(AsmToken::String))
2178 return TokError("unexpected token in '.file' directive");
2180 StringRef Filename = getTok().getString();
2181 Filename = Filename.substr(1, Filename.size()-2);
2184 if (getLexer().isNot(AsmToken::EndOfStatement))
2185 return TokError("unexpected token in '.file' directive");
2187 if (FileNumber == -1)
2188 getStreamer().EmitFileDirective(Filename);
2190 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
2195 /// ParseDirectiveLine
2196 /// ::= .line [number]
2197 bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
2198 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2199 if (getLexer().isNot(AsmToken::Integer))
2200 return TokError("unexpected token in '.line' directive");
2202 int64_t LineNumber = getTok().getIntVal();
2206 // FIXME: Do something with the .line.
2209 if (getLexer().isNot(AsmToken::EndOfStatement))
2210 return TokError("unexpected token in '.line' directive");
2216 /// ParseDirectiveLoc
2217 /// ::= .loc number [number [number]]
2218 bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
2219 if (getLexer().isNot(AsmToken::Integer))
2220 return TokError("unexpected token in '.loc' directive");
2222 // FIXME: What are these fields?
2223 int64_t FileNumber = getTok().getIntVal();
2225 // FIXME: Validate file.
2228 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2229 if (getLexer().isNot(AsmToken::Integer))
2230 return TokError("unexpected token in '.loc' directive");
2232 int64_t Param2 = getTok().getIntVal();
2236 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2237 if (getLexer().isNot(AsmToken::Integer))
2238 return TokError("unexpected token in '.loc' directive");
2240 int64_t Param3 = getTok().getIntVal();
2244 // FIXME: Do something with the .loc.
2248 if (getLexer().isNot(AsmToken::EndOfStatement))
2249 return TokError("unexpected token in '.file' directive");