Fixed some bugs.
[llvm/zpu.git] / lib / MC / MCParser / DarwinAsmParser.cpp
blob44f234566afdcdbe1a94b8a420adf95786ffb002
1 //===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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"
21 using namespace llvm;
23 namespace {
25 /// \brief Implementation of directive handling which is shared across all
26 /// Darwin targets.
27 class DarwinAsmParser : public MCAsmParserExtension {
28 template<bool (DarwinAsmParser::*Handler)(StringRef, SMLoc)>
29 void AddDirectiveHandler(StringRef Directive) {
30 getParser().AddDirectiveHandler(this, Directive,
31 HandleDirective<DarwinAsmParser, Handler>);
34 bool ParseSectionSwitch(const char *Segment, const char *Section,
35 unsigned TAA = 0, unsigned ImplicitAlign = 0,
36 unsigned StubSize = 0);
38 public:
39 DarwinAsmParser() {}
41 virtual void Initialize(MCAsmParser &Parser) {
42 // Call the base implementation.
43 this->MCAsmParserExtension::Initialize(Parser);
45 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc");
46 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym");
47 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>(
48 ".subsections_via_symbols");
49 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump");
50 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load");
51 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section");
52 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>(
53 ".secure_log_unique");
54 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>(
55 ".secure_log_reset");
56 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss");
57 AddDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill");
59 // Special section directives.
60 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const");
61 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>(".const_data");
62 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>(".constructor");
63 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>(".cstring");
64 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data");
65 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>(".destructor");
66 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld");
67 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>(".fvmlib_init0");
68 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>(".fvmlib_init1");
69 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>(".lazy_symbol_pointer");
70 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>(".literal16");
71 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>(".literal4");
72 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>(".literal8");
73 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>(".mod_init_func");
74 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>(".mod_term_func");
75 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>(".non_lazy_symbol_pointer");
76 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>(".objc_cat_cls_meth");
77 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>(".objc_cat_inst_meth");
78 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>(".objc_category");
79 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>(".objc_class");
80 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>(".objc_class_names");
81 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>(".objc_class_vars");
82 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>(".objc_cls_meth");
83 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>(".objc_cls_refs");
84 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>(".objc_inst_meth");
85 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>(".objc_instance_vars");
86 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>(".objc_message_refs");
87 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>(".objc_meta_class");
88 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>(".objc_meth_var_names");
89 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>(".objc_meth_var_types");
90 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>(".objc_module_info");
91 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>(".objc_protocol");
92 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>(".objc_selector_strs");
93 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCStringObject>(".objc_string_object");
94 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>(".objc_symbols");
95 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>(".picsymbol_stub");
96 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>(".static_const");
97 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>(".static_data");
98 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>(".symbol_stub");
99 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata");
100 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text");
101 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>(".thread_init_func");
102 AddDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv");
105 bool ParseDirectiveDesc(StringRef, SMLoc);
106 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
107 bool ParseDirectiveLsym(StringRef, SMLoc);
108 bool ParseDirectiveSection(StringRef, SMLoc);
109 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
110 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
111 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
112 bool ParseDirectiveTBSS(StringRef, SMLoc);
113 bool ParseDirectiveZerofill(StringRef, SMLoc);
115 // Named Section Directive
116 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
117 return ParseSectionSwitch("__TEXT", "__const");
119 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
120 return ParseSectionSwitch("__TEXT", "__static_const");
122 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
123 return ParseSectionSwitch("__TEXT","__cstring",
124 MCSectionMachO::S_CSTRING_LITERALS);
126 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
127 return ParseSectionSwitch("__TEXT", "__literal4",
128 MCSectionMachO::S_4BYTE_LITERALS, 4);
130 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
131 return ParseSectionSwitch("__TEXT", "__literal8",
132 MCSectionMachO::S_8BYTE_LITERALS, 8);
134 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
135 return ParseSectionSwitch("__TEXT","__literal16",
136 MCSectionMachO::S_16BYTE_LITERALS, 16);
138 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
139 return ParseSectionSwitch("__TEXT","__constructor");
141 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
142 return ParseSectionSwitch("__TEXT","__destructor");
144 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
145 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
147 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
148 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
150 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
151 return ParseSectionSwitch("__TEXT","__symbol_stub",
152 MCSectionMachO::S_SYMBOL_STUBS |
153 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
154 // FIXME: Different on PPC and ARM.
155 0, 16);
157 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
158 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
159 MCSectionMachO::S_SYMBOL_STUBS |
160 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
162 bool ParseSectionDirectiveData(StringRef, SMLoc) {
163 return ParseSectionSwitch("__DATA", "__data");
165 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
166 return ParseSectionSwitch("__DATA", "__static_data");
168 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
169 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
170 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
172 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
173 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
174 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
176 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
177 return ParseSectionSwitch("__DATA", "__dyld");
179 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
180 return ParseSectionSwitch("__DATA", "__mod_init_func",
181 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
183 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
184 return ParseSectionSwitch("__DATA", "__mod_term_func",
185 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
187 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
188 return ParseSectionSwitch("__DATA", "__const");
190 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
191 return ParseSectionSwitch("__OBJC", "__class",
192 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
194 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
195 return ParseSectionSwitch("__OBJC", "__meta_class",
196 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
198 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
199 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
200 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
202 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
203 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
204 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
206 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
207 return ParseSectionSwitch("__OBJC", "__protocol",
208 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
210 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
211 return ParseSectionSwitch("__OBJC", "__string_object",
212 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
214 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
215 return ParseSectionSwitch("__OBJC", "__cls_meth",
216 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
218 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
219 return ParseSectionSwitch("__OBJC", "__inst_meth",
220 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
222 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
223 return ParseSectionSwitch("__OBJC", "__cls_refs",
224 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
225 MCSectionMachO::S_LITERAL_POINTERS, 4);
227 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
228 return ParseSectionSwitch("__OBJC", "__message_refs",
229 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
230 MCSectionMachO::S_LITERAL_POINTERS, 4);
232 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
233 return ParseSectionSwitch("__OBJC", "__symbols",
234 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
236 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
237 return ParseSectionSwitch("__OBJC", "__category",
238 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
240 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
241 return ParseSectionSwitch("__OBJC", "__class_vars",
242 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
244 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
245 return ParseSectionSwitch("__OBJC", "__instance_vars",
246 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
248 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
249 return ParseSectionSwitch("__OBJC", "__module_info",
250 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
252 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
253 return ParseSectionSwitch("__TEXT", "__cstring",
254 MCSectionMachO::S_CSTRING_LITERALS);
256 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
257 return ParseSectionSwitch("__TEXT", "__cstring",
258 MCSectionMachO::S_CSTRING_LITERALS);
260 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
261 return ParseSectionSwitch("__TEXT", "__cstring",
262 MCSectionMachO::S_CSTRING_LITERALS);
264 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
265 return ParseSectionSwitch("__OBJC", "__selector_strs",
266 MCSectionMachO::S_CSTRING_LITERALS);
268 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
269 return ParseSectionSwitch("__DATA", "__thread_data",
270 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
272 bool ParseSectionDirectiveText(StringRef, SMLoc) {
273 return ParseSectionSwitch("__TEXT", "__text",
274 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
276 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
277 return ParseSectionSwitch("__DATA", "__thread_vars",
278 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
280 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
281 return ParseSectionSwitch("__DATA", "__thread_init",
282 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
289 bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
290 const char *Section,
291 unsigned TAA, unsigned Align,
292 unsigned StubSize) {
293 if (getLexer().isNot(AsmToken::EndOfStatement))
294 return TokError("unexpected token in section switching directive");
295 Lex();
297 // FIXME: Arch specific.
298 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
299 getStreamer().SwitchSection(getContext().getMachOSection(
300 Segment, Section, TAA, StubSize,
301 isText ? SectionKind::getText()
302 : SectionKind::getDataRel()));
304 // Set the implicit alignment, if any.
306 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
307 // alignment on the section (e.g., if one manually inserts bytes into the
308 // section, then just issuing the section switch directive will not realign
309 // the section. However, this is arguably more reasonable behavior, and there
310 // is no good reason for someone to intentionally emit incorrectly sized
311 // values into the implicitly aligned sections.
312 if (Align)
313 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
315 return false;
318 /// ParseDirectiveDesc
319 /// ::= .desc identifier , expression
320 bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
321 StringRef Name;
322 if (getParser().ParseIdentifier(Name))
323 return TokError("expected identifier in directive");
325 // Handle the identifier as the key symbol.
326 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
328 if (getLexer().isNot(AsmToken::Comma))
329 return TokError("unexpected token in '.desc' directive");
330 Lex();
332 int64_t DescValue;
333 if (getParser().ParseAbsoluteExpression(DescValue))
334 return true;
336 if (getLexer().isNot(AsmToken::EndOfStatement))
337 return TokError("unexpected token in '.desc' directive");
339 Lex();
341 // Set the n_desc field of this Symbol to this DescValue
342 getStreamer().EmitSymbolDesc(Sym, DescValue);
344 return false;
347 /// ParseDirectiveDumpOrLoad
348 /// ::= ( .dump | .load ) "filename"
349 bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
350 SMLoc IDLoc) {
351 bool IsDump = Directive == ".dump";
352 if (getLexer().isNot(AsmToken::String))
353 return TokError("expected string in '.dump' or '.load' directive");
355 Lex();
357 if (getLexer().isNot(AsmToken::EndOfStatement))
358 return TokError("unexpected token in '.dump' or '.load' directive");
360 Lex();
362 // FIXME: If/when .dump and .load are implemented they will be done in the
363 // the assembly parser and not have any need for an MCStreamer API.
364 if (IsDump)
365 Warning(IDLoc, "ignoring directive .dump for now");
366 else
367 Warning(IDLoc, "ignoring directive .load for now");
369 return false;
372 /// ParseDirectiveLsym
373 /// ::= .lsym identifier , expression
374 bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
375 StringRef Name;
376 if (getParser().ParseIdentifier(Name))
377 return TokError("expected identifier in directive");
379 // Handle the identifier as the key symbol.
380 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
382 if (getLexer().isNot(AsmToken::Comma))
383 return TokError("unexpected token in '.lsym' directive");
384 Lex();
386 const MCExpr *Value;
387 if (getParser().ParseExpression(Value))
388 return true;
390 if (getLexer().isNot(AsmToken::EndOfStatement))
391 return TokError("unexpected token in '.lsym' directive");
393 Lex();
395 // We don't currently support this directive.
397 // FIXME: Diagnostic location!
398 (void) Sym;
399 return TokError("directive '.lsym' is unsupported");
402 /// ParseDirectiveSection:
403 /// ::= .section identifier (',' identifier)*
404 bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
405 SMLoc Loc = getLexer().getLoc();
407 StringRef SectionName;
408 if (getParser().ParseIdentifier(SectionName))
409 return Error(Loc, "expected identifier after '.section' directive");
411 // Verify there is a following comma.
412 if (!getLexer().is(AsmToken::Comma))
413 return TokError("unexpected token in '.section' directive");
415 std::string SectionSpec = SectionName;
416 SectionSpec += ",";
418 // Add all the tokens until the end of the line, ParseSectionSpecifier will
419 // handle this.
420 StringRef EOL = getLexer().LexUntilEndOfStatement();
421 SectionSpec.append(EOL.begin(), EOL.end());
423 Lex();
424 if (getLexer().isNot(AsmToken::EndOfStatement))
425 return TokError("unexpected token in '.section' directive");
426 Lex();
429 StringRef Segment, Section;
430 unsigned TAA, StubSize;
431 std::string ErrorStr =
432 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
433 TAA, StubSize);
435 if (!ErrorStr.empty())
436 return Error(Loc, ErrorStr.c_str());
438 // FIXME: Arch specific.
439 bool isText = Segment == "__TEXT"; // FIXME: Hack.
440 getStreamer().SwitchSection(getContext().getMachOSection(
441 Segment, Section, TAA, StubSize,
442 isText ? SectionKind::getText()
443 : SectionKind::getDataRel()));
444 return false;
447 /// ParseDirectiveSecureLogUnique
448 /// ::= .secure_log_unique ... message ...
449 bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
450 StringRef LogMessage = getParser().ParseStringToEndOfStatement();
451 if (getLexer().isNot(AsmToken::EndOfStatement))
452 return TokError("unexpected token in '.secure_log_unique' directive");
454 if (getContext().getSecureLogUsed() != false)
455 return Error(IDLoc, ".secure_log_unique specified multiple times");
457 // Get the secure log path.
458 const char *SecureLogFile = getContext().getSecureLogFile();
459 if (SecureLogFile == NULL)
460 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
461 "environment variable unset.");
463 // Open the secure log file if we haven't already.
464 raw_ostream *OS = getContext().getSecureLog();
465 if (OS == NULL) {
466 std::string Err;
467 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
468 if (!Err.empty()) {
469 delete OS;
470 return Error(IDLoc, Twine("can't open secure log file: ") +
471 SecureLogFile + " (" + Err + ")");
473 getContext().setSecureLog(OS);
476 // Write the message.
477 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
478 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
479 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
480 << LogMessage + "\n";
482 getContext().setSecureLogUsed(true);
484 return false;
487 /// ParseDirectiveSecureLogReset
488 /// ::= .secure_log_reset
489 bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
490 if (getLexer().isNot(AsmToken::EndOfStatement))
491 return TokError("unexpected token in '.secure_log_reset' directive");
493 Lex();
495 getContext().setSecureLogUsed(false);
497 return false;
500 /// ParseDirectiveSubsectionsViaSymbols
501 /// ::= .subsections_via_symbols
502 bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
503 if (getLexer().isNot(AsmToken::EndOfStatement))
504 return TokError("unexpected token in '.subsections_via_symbols' directive");
506 Lex();
508 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
510 return false;
513 /// ParseDirectiveTBSS
514 /// ::= .tbss identifier, size, align
515 bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
516 SMLoc IDLoc = getLexer().getLoc();
517 StringRef Name;
518 if (getParser().ParseIdentifier(Name))
519 return TokError("expected identifier in directive");
521 // Handle the identifier as the key symbol.
522 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
524 if (getLexer().isNot(AsmToken::Comma))
525 return TokError("unexpected token in directive");
526 Lex();
528 int64_t Size;
529 SMLoc SizeLoc = getLexer().getLoc();
530 if (getParser().ParseAbsoluteExpression(Size))
531 return true;
533 int64_t Pow2Alignment = 0;
534 SMLoc Pow2AlignmentLoc;
535 if (getLexer().is(AsmToken::Comma)) {
536 Lex();
537 Pow2AlignmentLoc = getLexer().getLoc();
538 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
539 return true;
542 if (getLexer().isNot(AsmToken::EndOfStatement))
543 return TokError("unexpected token in '.tbss' directive");
545 Lex();
547 if (Size < 0)
548 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
549 "zero");
551 // FIXME: Diagnose overflow.
552 if (Pow2Alignment < 0)
553 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
554 "than zero");
556 if (!Sym->isUndefined())
557 return Error(IDLoc, "invalid symbol redefinition");
559 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
560 "__DATA", "__thread_bss",
561 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
562 0, SectionKind::getThreadBSS()),
563 Sym, Size, 1 << Pow2Alignment);
565 return false;
568 /// ParseDirectiveZerofill
569 /// ::= .zerofill segname , sectname [, identifier , size_expression [
570 /// , align_expression ]]
571 bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
572 StringRef Segment;
573 if (getParser().ParseIdentifier(Segment))
574 return TokError("expected segment name after '.zerofill' directive");
576 if (getLexer().isNot(AsmToken::Comma))
577 return TokError("unexpected token in directive");
578 Lex();
580 StringRef Section;
581 if (getParser().ParseIdentifier(Section))
582 return TokError("expected section name after comma in '.zerofill' "
583 "directive");
585 // If this is the end of the line all that was wanted was to create the
586 // the section but with no symbol.
587 if (getLexer().is(AsmToken::EndOfStatement)) {
588 // Create the zerofill section but no symbol
589 getStreamer().EmitZerofill(getContext().getMachOSection(
590 Segment, Section, MCSectionMachO::S_ZEROFILL,
591 0, SectionKind::getBSS()));
592 return false;
595 if (getLexer().isNot(AsmToken::Comma))
596 return TokError("unexpected token in directive");
597 Lex();
599 SMLoc IDLoc = getLexer().getLoc();
600 StringRef IDStr;
601 if (getParser().ParseIdentifier(IDStr))
602 return TokError("expected identifier in directive");
604 // handle the identifier as the key symbol.
605 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
607 if (getLexer().isNot(AsmToken::Comma))
608 return TokError("unexpected token in directive");
609 Lex();
611 int64_t Size;
612 SMLoc SizeLoc = getLexer().getLoc();
613 if (getParser().ParseAbsoluteExpression(Size))
614 return true;
616 int64_t Pow2Alignment = 0;
617 SMLoc Pow2AlignmentLoc;
618 if (getLexer().is(AsmToken::Comma)) {
619 Lex();
620 Pow2AlignmentLoc = getLexer().getLoc();
621 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
622 return true;
625 if (getLexer().isNot(AsmToken::EndOfStatement))
626 return TokError("unexpected token in '.zerofill' directive");
628 Lex();
630 if (Size < 0)
631 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
632 "than zero");
634 // NOTE: The alignment in the directive is a power of 2 value, the assembler
635 // may internally end up wanting an alignment in bytes.
636 // FIXME: Diagnose overflow.
637 if (Pow2Alignment < 0)
638 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
639 "can't be less than zero");
641 if (!Sym->isUndefined())
642 return Error(IDLoc, "invalid symbol redefinition");
644 // Create the zerofill Symbol with Size and Pow2Alignment
646 // FIXME: Arch specific.
647 getStreamer().EmitZerofill(getContext().getMachOSection(
648 Segment, Section, MCSectionMachO::S_ZEROFILL,
649 0, SectionKind::getBSS()),
650 Sym, Size, 1 << Pow2Alignment);
652 return false;
655 namespace llvm {
657 MCAsmParserExtension *createDarwinAsmParser() {
658 return new DarwinAsmParser;