blob: 3b86390da4296a781b3438697e3bf7ea2dd76b0b [file] [log] [blame]
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +00001//===- 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//===----------------------------------------------------------------------===//
9
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"
21using namespace llvm;
22
23namespace {
24
25/// \brief Implementation of directive handling which is shared across all
26/// Darwin targets.
27class DarwinAsmParser : public MCAsmParserExtension {
28 bool ParseSectionSwitch(const char *Segment, const char *Section,
29 unsigned TAA = 0, unsigned ImplicitAlign = 0,
30 unsigned StubSize = 0);
31
32public:
33 DarwinAsmParser() {}
34
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
38
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));
64
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));
195 }
196
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);
206
207 // Named Section Directive
208 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
209 return ParseSectionSwitch("__TEXT", "__const");
210 }
211 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
212 return ParseSectionSwitch("__TEXT", "__static_const");
213 }
214 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
215 return ParseSectionSwitch("__TEXT","__cstring",
216 MCSectionMachO::S_CSTRING_LITERALS);
217 }
218 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
219 return ParseSectionSwitch("__TEXT", "__literal4",
220 MCSectionMachO::S_4BYTE_LITERALS, 4);
221 }
222 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
223 return ParseSectionSwitch("__TEXT", "__literal8",
224 MCSectionMachO::S_8BYTE_LITERALS, 8);
225 }
226 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
227 return ParseSectionSwitch("__TEXT","__literal16",
228 MCSectionMachO::S_16BYTE_LITERALS, 16);
229 }
230 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
231 return ParseSectionSwitch("__TEXT","__constructor");
232 }
233 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
234 return ParseSectionSwitch("__TEXT","__destructor");
235 }
236 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
237 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
238 }
239 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
240 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
241 }
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.
247 0, 16);
248 }
249 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
250 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
251 MCSectionMachO::S_SYMBOL_STUBS |
252 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
253 }
254 bool ParseSectionDirectiveData(StringRef, SMLoc) {
255 return ParseSectionSwitch("__DATA", "__data");
256 }
257 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
258 return ParseSectionSwitch("__DATA", "__static_data");
259 }
260 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
261 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
262 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
263 }
264 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
265 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
266 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
267 }
268 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
269 return ParseSectionSwitch("__DATA", "__dyld");
270 }
271 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
272 return ParseSectionSwitch("__DATA", "__mod_init_func",
273 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
274 }
275 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
276 return ParseSectionSwitch("__DATA", "__mod_term_func",
277 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
278 }
279 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
280 return ParseSectionSwitch("__DATA", "__const");
281 }
282 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
283 return ParseSectionSwitch("__OBJC", "__class",
284 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
285 }
286 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
287 return ParseSectionSwitch("__OBJC", "__meta_class",
288 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
289 }
290 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
291 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
292 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
293 }
294 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
295 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
296 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
297 }
298 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
299 return ParseSectionSwitch("__OBJC", "__protocol",
300 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
301 }
302 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
303 return ParseSectionSwitch("__OBJC", "__string_object",
304 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
305 }
306 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
307 return ParseSectionSwitch("__OBJC", "__cls_meth",
308 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
309 }
310 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
311 return ParseSectionSwitch("__OBJC", "__inst_meth",
312 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
313 }
314 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
315 return ParseSectionSwitch("__OBJC", "__cls_refs",
316 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
317 MCSectionMachO::S_LITERAL_POINTERS, 4);
318 }
319 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
320 return ParseSectionSwitch("__OBJC", "__message_refs",
321 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
322 MCSectionMachO::S_LITERAL_POINTERS, 4);
323 }
324 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
325 return ParseSectionSwitch("__OBJC", "__symbols",
326 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
327 }
328 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
329 return ParseSectionSwitch("__OBJC", "__category",
330 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
331 }
332 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
333 return ParseSectionSwitch("__OBJC", "__class_vars",
334 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
335 }
336 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
337 return ParseSectionSwitch("__OBJC", "__instance_vars",
338 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
339 }
340 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
341 return ParseSectionSwitch("__OBJC", "__module_info",
342 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
343 }
344 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
345 return ParseSectionSwitch("__TEXT", "__cstring",
346 MCSectionMachO::S_CSTRING_LITERALS);
347 }
348 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
349 return ParseSectionSwitch("__TEXT", "__cstring",
350 MCSectionMachO::S_CSTRING_LITERALS);
351 }
352 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
353 return ParseSectionSwitch("__TEXT", "__cstring",
354 MCSectionMachO::S_CSTRING_LITERALS);
355 }
356 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
357 return ParseSectionSwitch("__OBJC", "__selector_strs",
358 MCSectionMachO::S_CSTRING_LITERALS);
359 }
360 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
361 return ParseSectionSwitch("__DATA", "__thread_data",
362 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
363 }
364 bool ParseSectionDirectiveText(StringRef, SMLoc) {
365 return ParseSectionSwitch("__TEXT", "__text",
366 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
367 }
368 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
369 return ParseSectionSwitch("__DATA", "__thread_vars",
370 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
371 }
372 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
373 return ParseSectionSwitch("__DATA", "__thread_init",
374 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
375 }
376
377};
378
379}
380
381bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
382 const char *Section,
383 unsigned TAA, unsigned Align,
384 unsigned StubSize) {
385 if (getLexer().isNot(AsmToken::EndOfStatement))
386 return TokError("unexpected token in section switching directive");
387 Lex();
388
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()));
395
396 // Set the implicit alignment, if any.
397 //
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.
404 if (Align)
405 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
406
407 return false;
408}
409
410/// ParseDirectiveDesc
411/// ::= .desc identifier , expression
412bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
413 StringRef Name;
414 if (getParser().ParseIdentifier(Name))
415 return TokError("expected identifier in directive");
416
417 // Handle the identifier as the key symbol.
418 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
419
420 if (getLexer().isNot(AsmToken::Comma))
421 return TokError("unexpected token in '.desc' directive");
422 Lex();
423
424 int64_t DescValue;
425 if (getParser().ParseAbsoluteExpression(DescValue))
426 return true;
427
428 if (getLexer().isNot(AsmToken::EndOfStatement))
429 return TokError("unexpected token in '.desc' directive");
430
431 Lex();
432
433 // Set the n_desc field of this Symbol to this DescValue
434 getStreamer().EmitSymbolDesc(Sym, DescValue);
435
436 return false;
437}
438
439/// ParseDirectiveDumpOrLoad
440/// ::= ( .dump | .load ) "filename"
441bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
442 SMLoc IDLoc) {
443 bool IsDump = Directive == ".dump";
444 if (getLexer().isNot(AsmToken::String))
445 return TokError("expected string in '.dump' or '.load' directive");
446
447 Lex();
448
449 if (getLexer().isNot(AsmToken::EndOfStatement))
450 return TokError("unexpected token in '.dump' or '.load' directive");
451
452 Lex();
453
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.
456 if (IsDump)
457 Warning(IDLoc, "ignoring directive .dump for now");
458 else
459 Warning(IDLoc, "ignoring directive .load for now");
460
461 return false;
462}
463
464/// ParseDirectiveLsym
465/// ::= .lsym identifier , expression
466bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
467 StringRef Name;
468 if (getParser().ParseIdentifier(Name))
469 return TokError("expected identifier in directive");
470
471 // Handle the identifier as the key symbol.
472 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
473
474 if (getLexer().isNot(AsmToken::Comma))
475 return TokError("unexpected token in '.lsym' directive");
476 Lex();
477
478 const MCExpr *Value;
479 if (getParser().ParseExpression(Value))
480 return true;
481
482 if (getLexer().isNot(AsmToken::EndOfStatement))
483 return TokError("unexpected token in '.lsym' directive");
484
485 Lex();
486
487 // We don't currently support this directive.
488 //
489 // FIXME: Diagnostic location!
490 (void) Sym;
491 return TokError("directive '.lsym' is unsupported");
492}
493
494/// ParseDirectiveSection:
495/// ::= .section identifier (',' identifier)*
496bool DarwinAsmParser::ParseDirectiveSection() {
497 SMLoc Loc = getLexer().getLoc();
498
499 StringRef SectionName;
500 if (getParser().ParseIdentifier(SectionName))
501 return Error(Loc, "expected identifier after '.section' directive");
502
503 // Verify there is a following comma.
504 if (!getLexer().is(AsmToken::Comma))
505 return TokError("unexpected token in '.section' directive");
506
507 std::string SectionSpec = SectionName;
508 SectionSpec += ",";
509
510 // Add all the tokens until the end of the line, ParseSectionSpecifier will
511 // handle this.
512 StringRef EOL = getLexer().LexUntilEndOfStatement();
513 SectionSpec.append(EOL.begin(), EOL.end());
514
515 Lex();
516 if (getLexer().isNot(AsmToken::EndOfStatement))
517 return TokError("unexpected token in '.section' directive");
518 Lex();
519
520
521 StringRef Segment, Section;
522 unsigned TAA, StubSize;
523 std::string ErrorStr =
524 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
525 TAA, StubSize);
526
527 if (!ErrorStr.empty())
528 return Error(Loc, ErrorStr.c_str());
529
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()));
536 return false;
537}
538
539/// ParseDirectiveSecureLogUnique
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000540/// ::= .secure_log_unique ... message ...
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000541bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000542 StringRef LogMessage = getParser().ParseStringToEndOfStatement();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000543 if (getLexer().isNot(AsmToken::EndOfStatement))
544 return TokError("unexpected token in '.secure_log_unique' directive");
545
546 if (getContext().getSecureLogUsed() != false)
547 return Error(IDLoc, ".secure_log_unique specified multiple times");
548
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000549 // Get the secure log path.
550 const char *SecureLogFile = getContext().getSecureLogFile();
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000551 if (SecureLogFile == NULL)
552 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
553 "environment variable unset.");
554
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000555 // Open the secure log file if we haven't already.
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000556 raw_ostream *OS = getContext().getSecureLog();
557 if (OS == NULL) {
558 std::string Err;
559 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
560 if (!Err.empty()) {
561 delete OS;
562 return Error(IDLoc, Twine("can't open secure log file: ") +
563 SecureLogFile + " (" + Err + ")");
564 }
565 getContext().setSecureLog(OS);
566 }
567
Daniel Dunbar6a46d572010-07-18 20:15:59 +0000568 // Write the message.
Daniel Dunbar9c23d7f2010-07-12 20:51:51 +0000569 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
570 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
571 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
572 << LogMessage + "\n";
573
574 getContext().setSecureLogUsed(true);
575
576 return false;
577}
578
579/// ParseDirectiveSecureLogReset
580/// ::= .secure_log_reset
581bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
582 if (getLexer().isNot(AsmToken::EndOfStatement))
583 return TokError("unexpected token in '.secure_log_reset' directive");
584
585 Lex();
586
587 getContext().setSecureLogUsed(false);
588
589 return false;
590}
591
592/// ParseDirectiveSubsectionsViaSymbols
593/// ::= .subsections_via_symbols
594bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
595 if (getLexer().isNot(AsmToken::EndOfStatement))
596 return TokError("unexpected token in '.subsections_via_symbols' directive");
597
598 Lex();
599
600 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
601
602 return false;
603}
604
605/// ParseDirectiveTBSS
606/// ::= .tbss identifier, size, align
607bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
608 SMLoc IDLoc = getLexer().getLoc();
609 StringRef Name;
610 if (getParser().ParseIdentifier(Name))
611 return TokError("expected identifier in directive");
612
613 // Handle the identifier as the key symbol.
614 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
615
616 if (getLexer().isNot(AsmToken::Comma))
617 return TokError("unexpected token in directive");
618 Lex();
619
620 int64_t Size;
621 SMLoc SizeLoc = getLexer().getLoc();
622 if (getParser().ParseAbsoluteExpression(Size))
623 return true;
624
625 int64_t Pow2Alignment = 0;
626 SMLoc Pow2AlignmentLoc;
627 if (getLexer().is(AsmToken::Comma)) {
628 Lex();
629 Pow2AlignmentLoc = getLexer().getLoc();
630 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
631 return true;
632 }
633
634 if (getLexer().isNot(AsmToken::EndOfStatement))
635 return TokError("unexpected token in '.tbss' directive");
636
637 Lex();
638
639 if (Size < 0)
640 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
641 "zero");
642
643 // FIXME: Diagnose overflow.
644 if (Pow2Alignment < 0)
645 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
646 "than zero");
647
648 if (!Sym->isUndefined())
649 return Error(IDLoc, "invalid symbol redefinition");
650
651 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
652 "__DATA", "__thread_bss",
653 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
654 0, SectionKind::getThreadBSS()),
655 Sym, Size, 1 << Pow2Alignment);
656
657 return false;
658}
659
660/// ParseDirectiveZerofill
661/// ::= .zerofill segname , sectname [, identifier , size_expression [
662/// , align_expression ]]
663bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
664 StringRef Segment;
665 if (getParser().ParseIdentifier(Segment))
666 return TokError("expected segment name after '.zerofill' directive");
667
668 if (getLexer().isNot(AsmToken::Comma))
669 return TokError("unexpected token in directive");
670 Lex();
671
672 StringRef Section;
673 if (getParser().ParseIdentifier(Section))
674 return TokError("expected section name after comma in '.zerofill' "
675 "directive");
676
677 // If this is the end of the line all that was wanted was to create the
678 // the section but with no symbol.
679 if (getLexer().is(AsmToken::EndOfStatement)) {
680 // Create the zerofill section but no symbol
681 getStreamer().EmitZerofill(getContext().getMachOSection(
682 Segment, Section, MCSectionMachO::S_ZEROFILL,
683 0, SectionKind::getBSS()));
684 return false;
685 }
686
687 if (getLexer().isNot(AsmToken::Comma))
688 return TokError("unexpected token in directive");
689 Lex();
690
691 SMLoc IDLoc = getLexer().getLoc();
692 StringRef IDStr;
693 if (getParser().ParseIdentifier(IDStr))
694 return TokError("expected identifier in directive");
695
696 // handle the identifier as the key symbol.
697 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
698
699 if (getLexer().isNot(AsmToken::Comma))
700 return TokError("unexpected token in directive");
701 Lex();
702
703 int64_t Size;
704 SMLoc SizeLoc = getLexer().getLoc();
705 if (getParser().ParseAbsoluteExpression(Size))
706 return true;
707
708 int64_t Pow2Alignment = 0;
709 SMLoc Pow2AlignmentLoc;
710 if (getLexer().is(AsmToken::Comma)) {
711 Lex();
712 Pow2AlignmentLoc = getLexer().getLoc();
713 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
714 return true;
715 }
716
717 if (getLexer().isNot(AsmToken::EndOfStatement))
718 return TokError("unexpected token in '.zerofill' directive");
719
720 Lex();
721
722 if (Size < 0)
723 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
724 "than zero");
725
726 // NOTE: The alignment in the directive is a power of 2 value, the assembler
727 // may internally end up wanting an alignment in bytes.
728 // FIXME: Diagnose overflow.
729 if (Pow2Alignment < 0)
730 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
731 "can't be less than zero");
732
733 if (!Sym->isUndefined())
734 return Error(IDLoc, "invalid symbol redefinition");
735
736 // Create the zerofill Symbol with Size and Pow2Alignment
737 //
738 // FIXME: Arch specific.
739 getStreamer().EmitZerofill(getContext().getMachOSection(
740 Segment, Section, MCSectionMachO::S_ZEROFILL,
741 0, SectionKind::getBSS()),
742 Sym, Size, 1 << Pow2Alignment);
743
744 return false;
745}
746
747namespace llvm {
748
749MCAsmParserExtension *createDarwinAsmParser() {
750 return new DarwinAsmParser;
751}
752
753}