1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
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 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCSectionMachO.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmParser.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
25 /// \brief Implementation of directive handling which is shared across all
27 class DarwinAsmParser : public MCAsmParserExtension {
28 bool ParseSectionSwitch(const char *Segment, const char *Section,
29 unsigned TAA = 0, unsigned ImplicitAlign = 0,
30 unsigned StubSize = 0);
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
39 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
40 &DarwinAsmParser::ParseDirectiveDesc));
41 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
42 &DarwinAsmParser::ParseDirectiveLsym));
43 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
44 MCAsmParser::DirectiveHandler(
45 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
46 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
47 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
48 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
49 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
50 Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler(
51 &DarwinAsmParser::ParseDirectiveSection));
52 Parser.AddDirectiveHandler(this, ".secure_log_unique",
53 MCAsmParser::DirectiveHandler(
54 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
55 Parser.AddDirectiveHandler(this, ".secure_log_reset",
56 MCAsmParser::DirectiveHandler(
57 &DarwinAsmParser::ParseDirectiveSecureLogReset));
58 Parser.AddDirectiveHandler(this, ".tbss",
59 MCAsmParser::DirectiveHandler(
60 &DarwinAsmParser::ParseDirectiveTBSS));
61 Parser.AddDirectiveHandler(this, ".zerofill",
62 MCAsmParser::DirectiveHandler(
63 &DarwinAsmParser::ParseDirectiveZerofill));
65 // Special section directives.
66 Parser.AddDirectiveHandler(this, ".const",
67 MCAsmParser::DirectiveHandler(
68 &DarwinAsmParser::ParseSectionDirectiveConst));
69 Parser.AddDirectiveHandler(this, ".const_data",
70 MCAsmParser::DirectiveHandler(
71 &DarwinAsmParser::ParseSectionDirectiveConstData));
72 Parser.AddDirectiveHandler(this, ".constructor",
73 MCAsmParser::DirectiveHandler(
74 &DarwinAsmParser::ParseSectionDirectiveConstructor));
75 Parser.AddDirectiveHandler(this, ".cstring",
76 MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseSectionDirectiveCString));
78 Parser.AddDirectiveHandler(this, ".data",
79 MCAsmParser::DirectiveHandler(
80 &DarwinAsmParser::ParseSectionDirectiveData));
81 Parser.AddDirectiveHandler(this, ".destructor",
82 MCAsmParser::DirectiveHandler(
83 &DarwinAsmParser::ParseSectionDirectiveDestructor));
84 Parser.AddDirectiveHandler(this, ".dyld",
85 MCAsmParser::DirectiveHandler(
86 &DarwinAsmParser::ParseSectionDirectiveDyld));
87 Parser.AddDirectiveHandler(this, ".fvmlib_init0",
88 MCAsmParser::DirectiveHandler(
89 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit0));
90 Parser.AddDirectiveHandler(this, ".fvmlib_init1",
91 MCAsmParser::DirectiveHandler(
92 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit1));
93 Parser.AddDirectiveHandler(this, ".lazy_symbol_pointer",
94 MCAsmParser::DirectiveHandler(
95 &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers));
96 Parser.AddDirectiveHandler(this, ".literal16",
97 MCAsmParser::DirectiveHandler(
98 &DarwinAsmParser::ParseSectionDirectiveLiteral16));
99 Parser.AddDirectiveHandler(this, ".literal4",
100 MCAsmParser::DirectiveHandler(
101 &DarwinAsmParser::ParseSectionDirectiveLiteral4));
102 Parser.AddDirectiveHandler(this, ".literal8",
103 MCAsmParser::DirectiveHandler(
104 &DarwinAsmParser::ParseSectionDirectiveLiteral8));
105 Parser.AddDirectiveHandler(this, ".mod_init_func",
106 MCAsmParser::DirectiveHandler(
107 &DarwinAsmParser::ParseSectionDirectiveModInitFunc));
108 Parser.AddDirectiveHandler(this, ".mod_term_func",
109 MCAsmParser::DirectiveHandler(
110 &DarwinAsmParser::ParseSectionDirectiveModTermFunc));
111 Parser.AddDirectiveHandler(this, ".non_lazy_symbol_pointer",
112 MCAsmParser::DirectiveHandler(
113 &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers));
114 Parser.AddDirectiveHandler(this, ".objc_cat_cls_meth",
115 MCAsmParser::DirectiveHandler(
116 &DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth));
117 Parser.AddDirectiveHandler(this, ".objc_cat_inst_meth",
118 MCAsmParser::DirectiveHandler(
119 &DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth));
120 Parser.AddDirectiveHandler(this, ".objc_category",
121 MCAsmParser::DirectiveHandler(
122 &DarwinAsmParser::ParseSectionDirectiveObjCCategory));
123 Parser.AddDirectiveHandler(this, ".objc_class",
124 MCAsmParser::DirectiveHandler(
125 &DarwinAsmParser::ParseSectionDirectiveObjCClass));
126 Parser.AddDirectiveHandler(this, ".objc_class_names",
127 MCAsmParser::DirectiveHandler(
128 &DarwinAsmParser::ParseSectionDirectiveObjCClassNames));
129 Parser.AddDirectiveHandler(this, ".objc_class_vars",
130 MCAsmParser::DirectiveHandler(
131 &DarwinAsmParser::ParseSectionDirectiveObjCClassVars));
132 Parser.AddDirectiveHandler(this, ".objc_cls_meth",
133 MCAsmParser::DirectiveHandler(
134 &DarwinAsmParser::ParseSectionDirectiveObjCClsMeth));
135 Parser.AddDirectiveHandler(this, ".objc_cls_refs",
136 MCAsmParser::DirectiveHandler(
137 &DarwinAsmParser::ParseSectionDirectiveObjCClsRefs));
138 Parser.AddDirectiveHandler(this, ".objc_inst_meth",
139 MCAsmParser::DirectiveHandler(
140 &DarwinAsmParser::ParseSectionDirectiveObjCInstMeth));
141 Parser.AddDirectiveHandler(this, ".objc_instance_vars",
142 MCAsmParser::DirectiveHandler(
143 &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars));
144 Parser.AddDirectiveHandler(this, ".objc_message_refs",
145 MCAsmParser::DirectiveHandler(
146 &DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs));
147 Parser.AddDirectiveHandler(this, ".objc_meta_class",
148 MCAsmParser::DirectiveHandler(
149 &DarwinAsmParser::ParseSectionDirectiveObjCMetaClass));
150 Parser.AddDirectiveHandler(this, ".objc_meth_var_names",
151 MCAsmParser::DirectiveHandler(
152 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames));
153 Parser.AddDirectiveHandler(this, ".objc_meth_var_types",
154 MCAsmParser::DirectiveHandler(
155 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes));
156 Parser.AddDirectiveHandler(this, ".objc_module_info",
157 MCAsmParser::DirectiveHandler(
158 &DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo));
159 Parser.AddDirectiveHandler(this, ".objc_protocol",
160 MCAsmParser::DirectiveHandler(
161 &DarwinAsmParser::ParseSectionDirectiveObjCProtocol));
162 Parser.AddDirectiveHandler(this, ".objc_selector_strs",
163 MCAsmParser::DirectiveHandler(
164 &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs));
165 Parser.AddDirectiveHandler(this, ".objc_string_object",
166 MCAsmParser::DirectiveHandler(
167 &DarwinAsmParser::ParseSectionDirectiveObjCStringObject));
168 Parser.AddDirectiveHandler(this, ".objc_symbols",
169 MCAsmParser::DirectiveHandler(
170 &DarwinAsmParser::ParseSectionDirectiveObjCSymbols));
171 Parser.AddDirectiveHandler(this, ".picsymbol_stub",
172 MCAsmParser::DirectiveHandler(
173 &DarwinAsmParser::ParseSectionDirectivePICSymbolStub));
174 Parser.AddDirectiveHandler(this, ".static_const",
175 MCAsmParser::DirectiveHandler(
176 &DarwinAsmParser::ParseSectionDirectiveStaticConst));
177 Parser.AddDirectiveHandler(this, ".static_data",
178 MCAsmParser::DirectiveHandler(
179 &DarwinAsmParser::ParseSectionDirectiveStaticData));
180 Parser.AddDirectiveHandler(this, ".symbol_stub",
181 MCAsmParser::DirectiveHandler(
182 &DarwinAsmParser::ParseSectionDirectiveSymbolStub));
183 Parser.AddDirectiveHandler(this, ".tdata",
184 MCAsmParser::DirectiveHandler(
185 &DarwinAsmParser::ParseSectionDirectiveTData));
186 Parser.AddDirectiveHandler(this, ".text",
187 MCAsmParser::DirectiveHandler(
188 &DarwinAsmParser::ParseSectionDirectiveText));
189 Parser.AddDirectiveHandler(this, ".thread_init_func",
190 MCAsmParser::DirectiveHandler(
191 &DarwinAsmParser::ParseSectionDirectiveThreadInitFunc));
192 Parser.AddDirectiveHandler(this, ".tlv",
193 MCAsmParser::DirectiveHandler(
194 &DarwinAsmParser::ParseSectionDirectiveTLV));
197 bool ParseDirectiveDesc(StringRef, SMLoc);
198 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
199 bool ParseDirectiveLsym(StringRef, SMLoc);
200 bool ParseDirectiveSection();
201 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
202 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
203 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
204 bool ParseDirectiveTBSS(StringRef, SMLoc);
205 bool ParseDirectiveZerofill(StringRef, SMLoc);
207 // Named Section Directive
208 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
209 return ParseSectionSwitch("__TEXT", "__const");
211 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
212 return ParseSectionSwitch("__TEXT", "__static_const");
214 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
215 return ParseSectionSwitch("__TEXT","__cstring",
216 MCSectionMachO::S_CSTRING_LITERALS);
218 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
219 return ParseSectionSwitch("__TEXT", "__literal4",
220 MCSectionMachO::S_4BYTE_LITERALS, 4);
222 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
223 return ParseSectionSwitch("__TEXT", "__literal8",
224 MCSectionMachO::S_8BYTE_LITERALS, 8);
226 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
227 return ParseSectionSwitch("__TEXT","__literal16",
228 MCSectionMachO::S_16BYTE_LITERALS, 16);
230 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
231 return ParseSectionSwitch("__TEXT","__constructor");
233 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
234 return ParseSectionSwitch("__TEXT","__destructor");
236 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
237 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
239 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
240 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
242 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
243 return ParseSectionSwitch("__TEXT","__symbol_stub",
244 MCSectionMachO::S_SYMBOL_STUBS |
245 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
246 // FIXME: Different on PPC and ARM.
249 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
250 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
251 MCSectionMachO::S_SYMBOL_STUBS |
252 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
254 bool ParseSectionDirectiveData(StringRef, SMLoc) {
255 return ParseSectionSwitch("__DATA", "__data");
257 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
258 return ParseSectionSwitch("__DATA", "__static_data");
260 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
261 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
262 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
264 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
265 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
266 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
268 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
269 return ParseSectionSwitch("__DATA", "__dyld");
271 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
272 return ParseSectionSwitch("__DATA", "__mod_init_func",
273 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
275 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
276 return ParseSectionSwitch("__DATA", "__mod_term_func",
277 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
279 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
280 return ParseSectionSwitch("__DATA", "__const");
282 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
283 return ParseSectionSwitch("__OBJC", "__class",
284 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
286 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
287 return ParseSectionSwitch("__OBJC", "__meta_class",
288 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
290 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
291 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
292 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
294 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
295 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
296 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
298 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
299 return ParseSectionSwitch("__OBJC", "__protocol",
300 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
302 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
303 return ParseSectionSwitch("__OBJC", "__string_object",
304 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
306 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
307 return ParseSectionSwitch("__OBJC", "__cls_meth",
308 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
310 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
311 return ParseSectionSwitch("__OBJC", "__inst_meth",
312 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
314 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
315 return ParseSectionSwitch("__OBJC", "__cls_refs",
316 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
317 MCSectionMachO::S_LITERAL_POINTERS, 4);
319 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
320 return ParseSectionSwitch("__OBJC", "__message_refs",
321 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
322 MCSectionMachO::S_LITERAL_POINTERS, 4);
324 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
325 return ParseSectionSwitch("__OBJC", "__symbols",
326 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
328 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
329 return ParseSectionSwitch("__OBJC", "__category",
330 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
332 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
333 return ParseSectionSwitch("__OBJC", "__class_vars",
334 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
336 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
337 return ParseSectionSwitch("__OBJC", "__instance_vars",
338 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
340 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
341 return ParseSectionSwitch("__OBJC", "__module_info",
342 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
344 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
345 return ParseSectionSwitch("__TEXT", "__cstring",
346 MCSectionMachO::S_CSTRING_LITERALS);
348 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
349 return ParseSectionSwitch("__TEXT", "__cstring",
350 MCSectionMachO::S_CSTRING_LITERALS);
352 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
353 return ParseSectionSwitch("__TEXT", "__cstring",
354 MCSectionMachO::S_CSTRING_LITERALS);
356 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
357 return ParseSectionSwitch("__OBJC", "__selector_strs",
358 MCSectionMachO::S_CSTRING_LITERALS);
360 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
361 return ParseSectionSwitch("__DATA", "__thread_data",
362 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
364 bool ParseSectionDirectiveText(StringRef, SMLoc) {
365 return ParseSectionSwitch("__TEXT", "__text",
366 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
368 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
369 return ParseSectionSwitch("__DATA", "__thread_vars",
370 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
372 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
373 return ParseSectionSwitch("__DATA", "__thread_init",
374 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
381 bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
383 unsigned TAA, unsigned Align,
385 if (getLexer().isNot(AsmToken::EndOfStatement))
386 return TokError("unexpected token in section switching directive");
389 // FIXME: Arch specific.
390 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
391 getStreamer().SwitchSection(getContext().getMachOSection(
392 Segment, Section, TAA, StubSize,
393 isText ? SectionKind::getText()
394 : SectionKind::getDataRel()));
396 // Set the implicit alignment, if any.
398 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
399 // alignment on the section (e.g., if one manually inserts bytes into the
400 // section, then just issueing the section switch directive will not realign
401 // the section. However, this is arguably more reasonable behavior, and there
402 // is no good reason for someone to intentionally emit incorrectly sized
403 // values into the implicitly aligned sections.
405 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
410 /// ParseDirectiveDesc
411 /// ::= .desc identifier , expression
412 bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
414 if (getParser().ParseIdentifier(Name))
415 return TokError("expected identifier in directive");
417 // Handle the identifier as the key symbol.
418 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
420 if (getLexer().isNot(AsmToken::Comma))
421 return TokError("unexpected token in '.desc' directive");
425 if (getParser().ParseAbsoluteExpression(DescValue))
428 if (getLexer().isNot(AsmToken::EndOfStatement))
429 return TokError("unexpected token in '.desc' directive");
433 // Set the n_desc field of this Symbol to this DescValue
434 getStreamer().EmitSymbolDesc(Sym, DescValue);
439 /// ParseDirectiveDumpOrLoad
440 /// ::= ( .dump | .load ) "filename"
441 bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
443 bool IsDump = Directive == ".dump";
444 if (getLexer().isNot(AsmToken::String))
445 return TokError("expected string in '.dump' or '.load' directive");
449 if (getLexer().isNot(AsmToken::EndOfStatement))
450 return TokError("unexpected token in '.dump' or '.load' directive");
454 // FIXME: If/when .dump and .load are implemented they will be done in the
455 // the assembly parser and not have any need for an MCStreamer API.
457 Warning(IDLoc, "ignoring directive .dump for now");
459 Warning(IDLoc, "ignoring directive .load for now");
464 /// ParseDirectiveLsym
465 /// ::= .lsym identifier , expression
466 bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
468 if (getParser().ParseIdentifier(Name))
469 return TokError("expected identifier in directive");
471 // Handle the identifier as the key symbol.
472 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
474 if (getLexer().isNot(AsmToken::Comma))
475 return TokError("unexpected token in '.lsym' directive");
479 if (getParser().ParseExpression(Value))
482 if (getLexer().isNot(AsmToken::EndOfStatement))
483 return TokError("unexpected token in '.lsym' directive");
487 // We don't currently support this directive.
489 // FIXME: Diagnostic location!
491 return TokError("directive '.lsym' is unsupported");
494 /// ParseDirectiveSection:
495 /// ::= .section identifier (',' identifier)*
496 bool DarwinAsmParser::ParseDirectiveSection() {
497 SMLoc Loc = getLexer().getLoc();
499 StringRef SectionName;
500 if (getParser().ParseIdentifier(SectionName))
501 return Error(Loc, "expected identifier after '.section' directive");
503 // Verify there is a following comma.
504 if (!getLexer().is(AsmToken::Comma))
505 return TokError("unexpected token in '.section' directive");
507 std::string SectionSpec = SectionName;
510 // Add all the tokens until the end of the line, ParseSectionSpecifier will
512 StringRef EOL = getLexer().LexUntilEndOfStatement();
513 SectionSpec.append(EOL.begin(), EOL.end());
516 if (getLexer().isNot(AsmToken::EndOfStatement))
517 return TokError("unexpected token in '.section' directive");
521 StringRef Segment, Section;
522 unsigned TAA, StubSize;
523 std::string ErrorStr =
524 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
527 if (!ErrorStr.empty())
528 return Error(Loc, ErrorStr.c_str());
530 // FIXME: Arch specific.
531 bool isText = Segment == "__TEXT"; // FIXME: Hack.
532 getStreamer().SwitchSection(getContext().getMachOSection(
533 Segment, Section, TAA, StubSize,
534 isText ? SectionKind::getText()
535 : SectionKind::getDataRel()));
539 /// ParseDirectiveSecureLogUnique
540 /// ::= .secure_log_unique "log message"
541 bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
542 std::string LogMessage;
544 if (getLexer().isNot(AsmToken::String))
547 LogMessage = getTok().getString();
551 if (getLexer().isNot(AsmToken::EndOfStatement))
552 return TokError("unexpected token in '.secure_log_unique' directive");
554 if (getContext().getSecureLogUsed() != false)
555 return Error(IDLoc, ".secure_log_unique specified multiple times");
557 char *SecureLogFile = getContext().getSecureLogFile();
558 if (SecureLogFile == NULL)
559 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
560 "environment variable unset.");
562 raw_ostream *OS = getContext().getSecureLog();
565 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
568 return Error(IDLoc, Twine("can't open secure log file: ") +
569 SecureLogFile + " (" + Err + ")");
571 getContext().setSecureLog(OS);
574 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
575 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
576 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
577 << LogMessage + "\n";
579 getContext().setSecureLogUsed(true);
584 /// ParseDirectiveSecureLogReset
585 /// ::= .secure_log_reset
586 bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
587 if (getLexer().isNot(AsmToken::EndOfStatement))
588 return TokError("unexpected token in '.secure_log_reset' directive");
592 getContext().setSecureLogUsed(false);
597 /// ParseDirectiveSubsectionsViaSymbols
598 /// ::= .subsections_via_symbols
599 bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
600 if (getLexer().isNot(AsmToken::EndOfStatement))
601 return TokError("unexpected token in '.subsections_via_symbols' directive");
605 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
610 /// ParseDirectiveTBSS
611 /// ::= .tbss identifier, size, align
612 bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
613 SMLoc IDLoc = getLexer().getLoc();
615 if (getParser().ParseIdentifier(Name))
616 return TokError("expected identifier in directive");
618 // Handle the identifier as the key symbol.
619 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
621 if (getLexer().isNot(AsmToken::Comma))
622 return TokError("unexpected token in directive");
626 SMLoc SizeLoc = getLexer().getLoc();
627 if (getParser().ParseAbsoluteExpression(Size))
630 int64_t Pow2Alignment = 0;
631 SMLoc Pow2AlignmentLoc;
632 if (getLexer().is(AsmToken::Comma)) {
634 Pow2AlignmentLoc = getLexer().getLoc();
635 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
639 if (getLexer().isNot(AsmToken::EndOfStatement))
640 return TokError("unexpected token in '.tbss' directive");
645 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
648 // FIXME: Diagnose overflow.
649 if (Pow2Alignment < 0)
650 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
653 if (!Sym->isUndefined())
654 return Error(IDLoc, "invalid symbol redefinition");
656 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
657 "__DATA", "__thread_bss",
658 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
659 0, SectionKind::getThreadBSS()),
660 Sym, Size, 1 << Pow2Alignment);
665 /// ParseDirectiveZerofill
666 /// ::= .zerofill segname , sectname [, identifier , size_expression [
667 /// , align_expression ]]
668 bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
670 if (getParser().ParseIdentifier(Segment))
671 return TokError("expected segment name after '.zerofill' directive");
673 if (getLexer().isNot(AsmToken::Comma))
674 return TokError("unexpected token in directive");
678 if (getParser().ParseIdentifier(Section))
679 return TokError("expected section name after comma in '.zerofill' "
682 // If this is the end of the line all that was wanted was to create the
683 // the section but with no symbol.
684 if (getLexer().is(AsmToken::EndOfStatement)) {
685 // Create the zerofill section but no symbol
686 getStreamer().EmitZerofill(getContext().getMachOSection(
687 Segment, Section, MCSectionMachO::S_ZEROFILL,
688 0, SectionKind::getBSS()));
692 if (getLexer().isNot(AsmToken::Comma))
693 return TokError("unexpected token in directive");
696 SMLoc IDLoc = getLexer().getLoc();
698 if (getParser().ParseIdentifier(IDStr))
699 return TokError("expected identifier in directive");
701 // handle the identifier as the key symbol.
702 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
704 if (getLexer().isNot(AsmToken::Comma))
705 return TokError("unexpected token in directive");
709 SMLoc SizeLoc = getLexer().getLoc();
710 if (getParser().ParseAbsoluteExpression(Size))
713 int64_t Pow2Alignment = 0;
714 SMLoc Pow2AlignmentLoc;
715 if (getLexer().is(AsmToken::Comma)) {
717 Pow2AlignmentLoc = getLexer().getLoc();
718 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
722 if (getLexer().isNot(AsmToken::EndOfStatement))
723 return TokError("unexpected token in '.zerofill' directive");
728 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
731 // NOTE: The alignment in the directive is a power of 2 value, the assembler
732 // may internally end up wanting an alignment in bytes.
733 // FIXME: Diagnose overflow.
734 if (Pow2Alignment < 0)
735 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
736 "can't be less than zero");
738 if (!Sym->isUndefined())
739 return Error(IDLoc, "invalid symbol redefinition");
741 // Create the zerofill Symbol with Size and Pow2Alignment
743 // FIXME: Arch specific.
744 getStreamer().EmitZerofill(getContext().getMachOSection(
745 Segment, Section, MCSectionMachO::S_ZEROFILL,
746 0, SectionKind::getBSS()),
747 Sym, Size, 1 << Pow2Alignment);
754 MCAsmParserExtension *createDarwinAsmParser() {
755 return new DarwinAsmParser;