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/StringSwitch.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/SourceMgr.h"
26 /// \brief Implementation of directive handling which is shared across all
28 class DarwinAsmParser : public MCAsmParserExtension {
29 template<bool (DarwinAsmParser::*Handler)(StringRef, SMLoc)>
30 void AddDirectiveHandler(StringRef Directive) {
31 getParser().AddDirectiveHandler(this, Directive,
32 HandleDirective<DarwinAsmParser, Handler>);
35 bool ParseSectionSwitch(const char *Segment, const char *Section,
36 unsigned TAA = 0, unsigned ImplicitAlign = 0,
37 unsigned StubSize = 0);
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
46 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc");
47 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym");
48 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>(
49 ".subsections_via_symbols");
50 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump");
51 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load");
52 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section");
53 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>(
54 ".secure_log_unique");
55 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>(
57 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss");
58 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill");
60 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegion>(".data_region");
61 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegionEnd>(".end_data_region");
63 // Special section directives.
64 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const");
65 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>(".const_data");
66 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>(".constructor");
67 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>(".cstring");
68 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data");
69 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>(".destructor");
70 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld");
71 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>(".fvmlib_init0");
72 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>(".fvmlib_init1");
73 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>(".lazy_symbol_pointer");
74 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>(".literal16");
75 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>(".literal4");
76 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>(".literal8");
77 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>(".mod_init_func");
78 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>(".mod_term_func");
79 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>(".non_lazy_symbol_pointer");
80 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>(".objc_cat_cls_meth");
81 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>(".objc_cat_inst_meth");
82 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>(".objc_category");
83 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>(".objc_class");
84 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>(".objc_class_names");
85 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>(".objc_class_vars");
86 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>(".objc_cls_meth");
87 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>(".objc_cls_refs");
88 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>(".objc_inst_meth");
89 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>(".objc_instance_vars");
90 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>(".objc_message_refs");
91 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>(".objc_meta_class");
92 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>(".objc_meth_var_names");
93 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>(".objc_meth_var_types");
94 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>(".objc_module_info");
95 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>(".objc_protocol");
96 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>(".objc_selector_strs");
97 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCStringObject>(".objc_string_object");
98 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>(".objc_symbols");
99 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>(".picsymbol_stub");
100 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>(".static_const");
101 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>(".static_data");
102 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>(".symbol_stub");
103 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata");
104 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text");
105 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>(".thread_init_func");
106 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv");
108 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveIdent>(".ident");
111 bool ParseDirectiveDesc(StringRef, SMLoc);
112 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
113 bool ParseDirectiveLsym(StringRef, SMLoc);
114 bool ParseDirectiveSection(StringRef, SMLoc);
115 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
116 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
117 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
118 bool ParseDirectiveTBSS(StringRef, SMLoc);
119 bool ParseDirectiveZerofill(StringRef, SMLoc);
120 bool ParseDirectiveDataRegion(StringRef, SMLoc);
121 bool ParseDirectiveDataRegionEnd(StringRef, SMLoc);
123 // Named Section Directive
124 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
125 return ParseSectionSwitch("__TEXT", "__const");
127 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
128 return ParseSectionSwitch("__TEXT", "__static_const");
130 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
131 return ParseSectionSwitch("__TEXT","__cstring",
132 MCSectionMachO::S_CSTRING_LITERALS);
134 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
135 return ParseSectionSwitch("__TEXT", "__literal4",
136 MCSectionMachO::S_4BYTE_LITERALS, 4);
138 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
139 return ParseSectionSwitch("__TEXT", "__literal8",
140 MCSectionMachO::S_8BYTE_LITERALS, 8);
142 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
143 return ParseSectionSwitch("__TEXT","__literal16",
144 MCSectionMachO::S_16BYTE_LITERALS, 16);
146 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
147 return ParseSectionSwitch("__TEXT","__constructor");
149 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
150 return ParseSectionSwitch("__TEXT","__destructor");
152 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
153 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
155 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
156 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
158 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
159 return ParseSectionSwitch("__TEXT","__symbol_stub",
160 MCSectionMachO::S_SYMBOL_STUBS |
161 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
162 // FIXME: Different on PPC and ARM.
165 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
166 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
167 MCSectionMachO::S_SYMBOL_STUBS |
168 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
170 bool ParseSectionDirectiveData(StringRef, SMLoc) {
171 return ParseSectionSwitch("__DATA", "__data");
173 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
174 return ParseSectionSwitch("__DATA", "__static_data");
176 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
177 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
178 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
180 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
181 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
182 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
184 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
185 return ParseSectionSwitch("__DATA", "__dyld");
187 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
188 return ParseSectionSwitch("__DATA", "__mod_init_func",
189 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
191 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
192 return ParseSectionSwitch("__DATA", "__mod_term_func",
193 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
195 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
196 return ParseSectionSwitch("__DATA", "__const");
198 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
199 return ParseSectionSwitch("__OBJC", "__class",
200 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
202 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
203 return ParseSectionSwitch("__OBJC", "__meta_class",
204 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
206 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
207 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
208 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
210 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
211 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
212 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
214 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
215 return ParseSectionSwitch("__OBJC", "__protocol",
216 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
218 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
219 return ParseSectionSwitch("__OBJC", "__string_object",
220 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
222 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
223 return ParseSectionSwitch("__OBJC", "__cls_meth",
224 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
226 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
227 return ParseSectionSwitch("__OBJC", "__inst_meth",
228 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
230 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
231 return ParseSectionSwitch("__OBJC", "__cls_refs",
232 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
233 MCSectionMachO::S_LITERAL_POINTERS, 4);
235 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
236 return ParseSectionSwitch("__OBJC", "__message_refs",
237 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
238 MCSectionMachO::S_LITERAL_POINTERS, 4);
240 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
241 return ParseSectionSwitch("__OBJC", "__symbols",
242 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
244 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
245 return ParseSectionSwitch("__OBJC", "__category",
246 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
248 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
249 return ParseSectionSwitch("__OBJC", "__class_vars",
250 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
252 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
253 return ParseSectionSwitch("__OBJC", "__instance_vars",
254 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
256 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
257 return ParseSectionSwitch("__OBJC", "__module_info",
258 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
260 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
261 return ParseSectionSwitch("__TEXT", "__cstring",
262 MCSectionMachO::S_CSTRING_LITERALS);
264 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
265 return ParseSectionSwitch("__TEXT", "__cstring",
266 MCSectionMachO::S_CSTRING_LITERALS);
268 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
269 return ParseSectionSwitch("__TEXT", "__cstring",
270 MCSectionMachO::S_CSTRING_LITERALS);
272 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
273 return ParseSectionSwitch("__OBJC", "__selector_strs",
274 MCSectionMachO::S_CSTRING_LITERALS);
276 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
277 return ParseSectionSwitch("__DATA", "__thread_data",
278 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
280 bool ParseSectionDirectiveText(StringRef, SMLoc) {
281 return ParseSectionSwitch("__TEXT", "__text",
282 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
284 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
285 return ParseSectionSwitch("__DATA", "__thread_vars",
286 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
288 bool ParseSectionDirectiveIdent(StringRef, SMLoc) {
289 // Darwin silently ignores the .ident directive.
290 getParser().EatToEndOfStatement();
293 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
294 return ParseSectionSwitch("__DATA", "__thread_init",
295 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
302 bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
304 unsigned TAA, unsigned Align,
306 if (getLexer().isNot(AsmToken::EndOfStatement))
307 return TokError("unexpected token in section switching directive");
310 // FIXME: Arch specific.
311 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
312 getStreamer().SwitchSection(getContext().getMachOSection(
313 Segment, Section, TAA, StubSize,
314 isText ? SectionKind::getText()
315 : SectionKind::getDataRel()));
317 // Set the implicit alignment, if any.
319 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
320 // alignment on the section (e.g., if one manually inserts bytes into the
321 // section, then just issuing the section switch directive will not realign
322 // the section. However, this is arguably more reasonable behavior, and there
323 // is no good reason for someone to intentionally emit incorrectly sized
324 // values into the implicitly aligned sections.
326 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
331 /// ParseDirectiveDesc
332 /// ::= .desc identifier , expression
333 bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
335 if (getParser().ParseIdentifier(Name))
336 return TokError("expected identifier in directive");
338 // Handle the identifier as the key symbol.
339 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
341 if (getLexer().isNot(AsmToken::Comma))
342 return TokError("unexpected token in '.desc' directive");
346 if (getParser().ParseAbsoluteExpression(DescValue))
349 if (getLexer().isNot(AsmToken::EndOfStatement))
350 return TokError("unexpected token in '.desc' directive");
354 // Set the n_desc field of this Symbol to this DescValue
355 getStreamer().EmitSymbolDesc(Sym, DescValue);
360 /// ParseDirectiveDumpOrLoad
361 /// ::= ( .dump | .load ) "filename"
362 bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
364 bool IsDump = Directive == ".dump";
365 if (getLexer().isNot(AsmToken::String))
366 return TokError("expected string in '.dump' or '.load' directive");
370 if (getLexer().isNot(AsmToken::EndOfStatement))
371 return TokError("unexpected token in '.dump' or '.load' directive");
375 // FIXME: If/when .dump and .load are implemented they will be done in the
376 // the assembly parser and not have any need for an MCStreamer API.
378 return Warning(IDLoc, "ignoring directive .dump for now");
380 return Warning(IDLoc, "ignoring directive .load for now");
383 /// ParseDirectiveLsym
384 /// ::= .lsym identifier , expression
385 bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
387 if (getParser().ParseIdentifier(Name))
388 return TokError("expected identifier in directive");
390 // Handle the identifier as the key symbol.
391 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
393 if (getLexer().isNot(AsmToken::Comma))
394 return TokError("unexpected token in '.lsym' directive");
398 if (getParser().ParseExpression(Value))
401 if (getLexer().isNot(AsmToken::EndOfStatement))
402 return TokError("unexpected token in '.lsym' directive");
406 // We don't currently support this directive.
408 // FIXME: Diagnostic location!
410 return TokError("directive '.lsym' is unsupported");
413 /// ParseDirectiveSection:
414 /// ::= .section identifier (',' identifier)*
415 bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
416 SMLoc Loc = getLexer().getLoc();
418 StringRef SectionName;
419 if (getParser().ParseIdentifier(SectionName))
420 return Error(Loc, "expected identifier after '.section' directive");
422 // Verify there is a following comma.
423 if (!getLexer().is(AsmToken::Comma))
424 return TokError("unexpected token in '.section' directive");
426 std::string SectionSpec = SectionName;
429 // Add all the tokens until the end of the line, ParseSectionSpecifier will
431 StringRef EOL = getLexer().LexUntilEndOfStatement();
432 SectionSpec.append(EOL.begin(), EOL.end());
435 if (getLexer().isNot(AsmToken::EndOfStatement))
436 return TokError("unexpected token in '.section' directive");
440 StringRef Segment, Section;
444 std::string ErrorStr =
445 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
446 TAA, TAAParsed, StubSize);
448 if (!ErrorStr.empty())
449 return Error(Loc, ErrorStr.c_str());
451 // FIXME: Arch specific.
452 bool isText = Segment == "__TEXT"; // FIXME: Hack.
453 getStreamer().SwitchSection(getContext().getMachOSection(
454 Segment, Section, TAA, StubSize,
455 isText ? SectionKind::getText()
456 : SectionKind::getDataRel()));
460 /// ParseDirectiveSecureLogUnique
461 /// ::= .secure_log_unique ... message ...
462 bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
463 StringRef LogMessage = getParser().ParseStringToEndOfStatement();
464 if (getLexer().isNot(AsmToken::EndOfStatement))
465 return TokError("unexpected token in '.secure_log_unique' directive");
467 if (getContext().getSecureLogUsed() != false)
468 return Error(IDLoc, ".secure_log_unique specified multiple times");
470 // Get the secure log path.
471 const char *SecureLogFile = getContext().getSecureLogFile();
472 if (SecureLogFile == NULL)
473 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
474 "environment variable unset.");
476 // Open the secure log file if we haven't already.
477 raw_ostream *OS = getContext().getSecureLog();
480 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
483 return Error(IDLoc, Twine("can't open secure log file: ") +
484 SecureLogFile + " (" + Err + ")");
486 getContext().setSecureLog(OS);
489 // Write the message.
490 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
491 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
492 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
493 << LogMessage + "\n";
495 getContext().setSecureLogUsed(true);
500 /// ParseDirectiveSecureLogReset
501 /// ::= .secure_log_reset
502 bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
503 if (getLexer().isNot(AsmToken::EndOfStatement))
504 return TokError("unexpected token in '.secure_log_reset' directive");
508 getContext().setSecureLogUsed(false);
513 /// ParseDirectiveSubsectionsViaSymbols
514 /// ::= .subsections_via_symbols
515 bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
516 if (getLexer().isNot(AsmToken::EndOfStatement))
517 return TokError("unexpected token in '.subsections_via_symbols' directive");
521 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
526 /// ParseDirectiveTBSS
527 /// ::= .tbss identifier, size, align
528 bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
529 SMLoc IDLoc = getLexer().getLoc();
531 if (getParser().ParseIdentifier(Name))
532 return TokError("expected identifier in directive");
534 // Handle the identifier as the key symbol.
535 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
537 if (getLexer().isNot(AsmToken::Comma))
538 return TokError("unexpected token in directive");
542 SMLoc SizeLoc = getLexer().getLoc();
543 if (getParser().ParseAbsoluteExpression(Size))
546 int64_t Pow2Alignment = 0;
547 SMLoc Pow2AlignmentLoc;
548 if (getLexer().is(AsmToken::Comma)) {
550 Pow2AlignmentLoc = getLexer().getLoc();
551 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
555 if (getLexer().isNot(AsmToken::EndOfStatement))
556 return TokError("unexpected token in '.tbss' directive");
561 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
564 // FIXME: Diagnose overflow.
565 if (Pow2Alignment < 0)
566 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
569 if (!Sym->isUndefined())
570 return Error(IDLoc, "invalid symbol redefinition");
572 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
573 "__DATA", "__thread_bss",
574 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
575 0, SectionKind::getThreadBSS()),
576 Sym, Size, 1 << Pow2Alignment);
581 /// ParseDirectiveZerofill
582 /// ::= .zerofill segname , sectname [, identifier , size_expression [
583 /// , align_expression ]]
584 bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
586 if (getParser().ParseIdentifier(Segment))
587 return TokError("expected segment name after '.zerofill' directive");
589 if (getLexer().isNot(AsmToken::Comma))
590 return TokError("unexpected token in directive");
594 if (getParser().ParseIdentifier(Section))
595 return TokError("expected section name after comma in '.zerofill' "
598 // If this is the end of the line all that was wanted was to create the
599 // the section but with no symbol.
600 if (getLexer().is(AsmToken::EndOfStatement)) {
601 // Create the zerofill section but no symbol
602 getStreamer().EmitZerofill(getContext().getMachOSection(
603 Segment, Section, MCSectionMachO::S_ZEROFILL,
604 0, SectionKind::getBSS()));
608 if (getLexer().isNot(AsmToken::Comma))
609 return TokError("unexpected token in directive");
612 SMLoc IDLoc = getLexer().getLoc();
614 if (getParser().ParseIdentifier(IDStr))
615 return TokError("expected identifier in directive");
617 // handle the identifier as the key symbol.
618 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
620 if (getLexer().isNot(AsmToken::Comma))
621 return TokError("unexpected token in directive");
625 SMLoc SizeLoc = getLexer().getLoc();
626 if (getParser().ParseAbsoluteExpression(Size))
629 int64_t Pow2Alignment = 0;
630 SMLoc Pow2AlignmentLoc;
631 if (getLexer().is(AsmToken::Comma)) {
633 Pow2AlignmentLoc = getLexer().getLoc();
634 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
638 if (getLexer().isNot(AsmToken::EndOfStatement))
639 return TokError("unexpected token in '.zerofill' directive");
644 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
647 // NOTE: The alignment in the directive is a power of 2 value, the assembler
648 // may internally end up wanting an alignment in bytes.
649 // FIXME: Diagnose overflow.
650 if (Pow2Alignment < 0)
651 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
652 "can't be less than zero");
654 if (!Sym->isUndefined())
655 return Error(IDLoc, "invalid symbol redefinition");
657 // Create the zerofill Symbol with Size and Pow2Alignment
659 // FIXME: Arch specific.
660 getStreamer().EmitZerofill(getContext().getMachOSection(
661 Segment, Section, MCSectionMachO::S_ZEROFILL,
662 0, SectionKind::getBSS()),
663 Sym, Size, 1 << Pow2Alignment);
668 /// ParseDirectiveDataRegion
669 /// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
670 bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) {
671 if (getLexer().is(AsmToken::EndOfStatement)) {
673 getStreamer().EmitDataRegion(MCDR_DataRegion);
676 StringRef RegionType;
677 SMLoc Loc = getParser().getTok().getLoc();
678 if (getParser().ParseIdentifier(RegionType))
679 return TokError("expected region type after '.data_region' directive");
680 int Kind = StringSwitch<int>(RegionType)
681 .Case("jt8", MCDR_DataRegionJT8)
682 .Case("jt16", MCDR_DataRegionJT16)
683 .Case("jt32", MCDR_DataRegionJT32)
686 return Error(Loc, "unknown region type in '.data_region' directive");
689 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
693 /// ParseDirectiveDataRegionEnd
694 /// ::= .end_data_region
695 bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) {
696 if (getLexer().isNot(AsmToken::EndOfStatement))
697 return TokError("unexpected token in '.end_data_region' directive");
700 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
706 MCAsmParserExtension *createDarwinAsmParser() {
707 return new DarwinAsmParser;