blob: 29e4a9909b5dd4f63fa343cbc5f79e564c07b158 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmParser.h"
Daniel Dunbar7c0a3342009-08-26 22:49:51 +000015#include "llvm/ADT/SmallString.h"
Matt Fleming924c5e52010-05-21 11:36:59 +000016#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarf9507ff2009-07-27 23:20:52 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000018#include "llvm/MC/MCContext.h"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000019#include "llvm/MC/MCExpr.h"
Chris Lattner29dfe7c2009-06-23 18:41:30 +000020#include "llvm/MC/MCInst.h"
Daniel Dunbar7a56fc22010-07-12 20:08:04 +000021#include "llvm/MC/MCSectionELF.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000022#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000023#include "llvm/MC/MCStreamer.h"
Daniel Dunbardce0f3c2009-06-29 23:43:14 +000024#include "llvm/MC/MCSymbol.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Bill Wendling9bc0af82009-12-28 01:34:57 +000026#include "llvm/Support/Compiler.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000027#include "llvm/Support/SourceMgr.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000028#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000029#include "llvm/Support/raw_ostream.h"
Daniel Dunbara3af3702009-07-20 18:55:04 +000030#include "llvm/Target/TargetAsmParser.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000031using namespace llvm;
32
Chris Lattneraaec2052010-01-19 19:46:13 +000033
Daniel Dunbar81ea00f2010-07-12 17:54:38 +000034namespace {
35
36/// \brief Generic implementations of directive handling, etc. which is shared
37/// (or the default, at least) for all assembler parser.
38class GenericAsmParser : public MCAsmParserExtension {
39public:
40 GenericAsmParser() {}
41
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
45
46 // Debugging directives.
47 Parser.AddDirectiveHandler(this, ".file", MCAsmParser::DirectiveHandler(
48 &GenericAsmParser::ParseDirectiveFile));
49 Parser.AddDirectiveHandler(this, ".line", MCAsmParser::DirectiveHandler(
50 &GenericAsmParser::ParseDirectiveLine));
51 Parser.AddDirectiveHandler(this, ".loc", MCAsmParser::DirectiveHandler(
52 &GenericAsmParser::ParseDirectiveLoc));
53 }
54
55 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
56 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
57 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
58};
59
Daniel Dunbare4749702010-07-12 18:12:02 +000060/// \brief Implementation of directive handling which is shared across all
61/// Darwin targets.
62class DarwinAsmParser : public MCAsmParserExtension {
Daniel Dunbar47f56082010-07-12 20:23:36 +000063 bool ParseSectionSwitch(const char *Segment, const char *Section,
64 unsigned TAA = 0, unsigned ImplicitAlign = 0,
65 unsigned StubSize = 0);
66
Daniel Dunbare4749702010-07-12 18:12:02 +000067public:
68 DarwinAsmParser() {}
69
70 virtual void Initialize(MCAsmParser &Parser) {
71 // Call the base implementation.
72 this->MCAsmParserExtension::Initialize(Parser);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000073
Daniel Dunbar492b7a22010-07-12 19:22:53 +000074 Parser.AddDirectiveHandler(this, ".desc", MCAsmParser::DirectiveHandler(
75 &DarwinAsmParser::ParseDirectiveDesc));
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +000076 Parser.AddDirectiveHandler(this, ".lsym", MCAsmParser::DirectiveHandler(
77 &DarwinAsmParser::ParseDirectiveLsym));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000078 Parser.AddDirectiveHandler(this, ".subsections_via_symbols",
79 MCAsmParser::DirectiveHandler(
80 &DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols));
81 Parser.AddDirectiveHandler(this, ".dump", MCAsmParser::DirectiveHandler(
82 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
83 Parser.AddDirectiveHandler(this, ".load", MCAsmParser::DirectiveHandler(
84 &DarwinAsmParser::ParseDirectiveDumpOrLoad));
Daniel Dunbar4d5fe972010-07-12 20:42:34 +000085 Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler(
86 &DarwinAsmParser::ParseDirectiveSection));
Daniel Dunbar9ac66b02010-07-12 18:49:22 +000087 Parser.AddDirectiveHandler(this, ".secure_log_unique",
88 MCAsmParser::DirectiveHandler(
89 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
90 Parser.AddDirectiveHandler(this, ".secure_log_reset",
91 MCAsmParser::DirectiveHandler(
92 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbarb6c3a602010-07-12 19:37:35 +000093 Parser.AddDirectiveHandler(this, ".tbss",
94 MCAsmParser::DirectiveHandler(
95 &DarwinAsmParser::ParseDirectiveTBSS));
96 Parser.AddDirectiveHandler(this, ".zerofill",
97 MCAsmParser::DirectiveHandler(
98 &DarwinAsmParser::ParseDirectiveZerofill));
Daniel Dunbar47f56082010-07-12 20:23:36 +000099
100 // Special section directives.
101 Parser.AddDirectiveHandler(this, ".const",
102 MCAsmParser::DirectiveHandler(
103 &DarwinAsmParser::ParseSectionDirectiveConst));
104 Parser.AddDirectiveHandler(this, ".const_data",
105 MCAsmParser::DirectiveHandler(
106 &DarwinAsmParser::ParseSectionDirectiveConstData));
107 Parser.AddDirectiveHandler(this, ".constructor",
108 MCAsmParser::DirectiveHandler(
109 &DarwinAsmParser::ParseSectionDirectiveConstructor));
110 Parser.AddDirectiveHandler(this, ".cstring",
111 MCAsmParser::DirectiveHandler(
112 &DarwinAsmParser::ParseSectionDirectiveCString));
113 Parser.AddDirectiveHandler(this, ".data",
114 MCAsmParser::DirectiveHandler(
115 &DarwinAsmParser::ParseSectionDirectiveData));
116 Parser.AddDirectiveHandler(this, ".destructor",
117 MCAsmParser::DirectiveHandler(
118 &DarwinAsmParser::ParseSectionDirectiveDestructor));
119 Parser.AddDirectiveHandler(this, ".dyld",
120 MCAsmParser::DirectiveHandler(
121 &DarwinAsmParser::ParseSectionDirectiveDyld));
122 Parser.AddDirectiveHandler(this, ".fvmlib_init0",
123 MCAsmParser::DirectiveHandler(
124 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit0));
125 Parser.AddDirectiveHandler(this, ".fvmlib_init1",
126 MCAsmParser::DirectiveHandler(
127 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit1));
128 Parser.AddDirectiveHandler(this, ".lazy_symbol_pointer",
129 MCAsmParser::DirectiveHandler(
130 &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers));
131 Parser.AddDirectiveHandler(this, ".literal16",
132 MCAsmParser::DirectiveHandler(
133 &DarwinAsmParser::ParseSectionDirectiveLiteral16));
134 Parser.AddDirectiveHandler(this, ".literal4",
135 MCAsmParser::DirectiveHandler(
136 &DarwinAsmParser::ParseSectionDirectiveLiteral4));
137 Parser.AddDirectiveHandler(this, ".literal8",
138 MCAsmParser::DirectiveHandler(
139 &DarwinAsmParser::ParseSectionDirectiveLiteral8));
140 Parser.AddDirectiveHandler(this, ".mod_init_func",
141 MCAsmParser::DirectiveHandler(
142 &DarwinAsmParser::ParseSectionDirectiveModInitFunc));
143 Parser.AddDirectiveHandler(this, ".mod_term_func",
144 MCAsmParser::DirectiveHandler(
145 &DarwinAsmParser::ParseSectionDirectiveModTermFunc));
146 Parser.AddDirectiveHandler(this, ".non_lazy_symbol_pointer",
147 MCAsmParser::DirectiveHandler(
148 &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers));
149 Parser.AddDirectiveHandler(this, ".objc_cat_cls_meth",
150 MCAsmParser::DirectiveHandler(
151 &DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth));
152 Parser.AddDirectiveHandler(this, ".objc_cat_inst_meth",
153 MCAsmParser::DirectiveHandler(
154 &DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth));
155 Parser.AddDirectiveHandler(this, ".objc_category",
156 MCAsmParser::DirectiveHandler(
157 &DarwinAsmParser::ParseSectionDirectiveObjCCategory));
158 Parser.AddDirectiveHandler(this, ".objc_class",
159 MCAsmParser::DirectiveHandler(
160 &DarwinAsmParser::ParseSectionDirectiveObjCClass));
161 Parser.AddDirectiveHandler(this, ".objc_class_names",
162 MCAsmParser::DirectiveHandler(
163 &DarwinAsmParser::ParseSectionDirectiveObjCClassNames));
164 Parser.AddDirectiveHandler(this, ".objc_class_vars",
165 MCAsmParser::DirectiveHandler(
166 &DarwinAsmParser::ParseSectionDirectiveObjCClassVars));
167 Parser.AddDirectiveHandler(this, ".objc_cls_meth",
168 MCAsmParser::DirectiveHandler(
169 &DarwinAsmParser::ParseSectionDirectiveObjCClsMeth));
170 Parser.AddDirectiveHandler(this, ".objc_cls_refs",
171 MCAsmParser::DirectiveHandler(
172 &DarwinAsmParser::ParseSectionDirectiveObjCClsRefs));
173 Parser.AddDirectiveHandler(this, ".objc_inst_meth",
174 MCAsmParser::DirectiveHandler(
175 &DarwinAsmParser::ParseSectionDirectiveObjCInstMeth));
176 Parser.AddDirectiveHandler(this, ".objc_instance_vars",
177 MCAsmParser::DirectiveHandler(
178 &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars));
179 Parser.AddDirectiveHandler(this, ".objc_message_refs",
180 MCAsmParser::DirectiveHandler(
181 &DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs));
182 Parser.AddDirectiveHandler(this, ".objc_meta_class",
183 MCAsmParser::DirectiveHandler(
184 &DarwinAsmParser::ParseSectionDirectiveObjCMetaClass));
185 Parser.AddDirectiveHandler(this, ".objc_meth_var_names",
186 MCAsmParser::DirectiveHandler(
187 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames));
188 Parser.AddDirectiveHandler(this, ".objc_meth_var_types",
189 MCAsmParser::DirectiveHandler(
190 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes));
191 Parser.AddDirectiveHandler(this, ".objc_module_info",
192 MCAsmParser::DirectiveHandler(
193 &DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo));
194 Parser.AddDirectiveHandler(this, ".objc_protocol",
195 MCAsmParser::DirectiveHandler(
196 &DarwinAsmParser::ParseSectionDirectiveObjCProtocol));
197 Parser.AddDirectiveHandler(this, ".objc_selector_strs",
198 MCAsmParser::DirectiveHandler(
199 &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs));
200 Parser.AddDirectiveHandler(this, ".objc_string_object",
201 MCAsmParser::DirectiveHandler(
202 &DarwinAsmParser::ParseSectionDirectiveObjCStringObject));
203 Parser.AddDirectiveHandler(this, ".objc_symbols",
204 MCAsmParser::DirectiveHandler(
205 &DarwinAsmParser::ParseSectionDirectiveObjCSymbols));
206 Parser.AddDirectiveHandler(this, ".picsymbol_stub",
207 MCAsmParser::DirectiveHandler(
208 &DarwinAsmParser::ParseSectionDirectivePICSymbolStub));
209 Parser.AddDirectiveHandler(this, ".static_const",
210 MCAsmParser::DirectiveHandler(
211 &DarwinAsmParser::ParseSectionDirectiveStaticConst));
212 Parser.AddDirectiveHandler(this, ".static_data",
213 MCAsmParser::DirectiveHandler(
214 &DarwinAsmParser::ParseSectionDirectiveStaticData));
215 Parser.AddDirectiveHandler(this, ".symbol_stub",
216 MCAsmParser::DirectiveHandler(
217 &DarwinAsmParser::ParseSectionDirectiveSymbolStub));
218 Parser.AddDirectiveHandler(this, ".tdata",
219 MCAsmParser::DirectiveHandler(
220 &DarwinAsmParser::ParseSectionDirectiveTData));
221 Parser.AddDirectiveHandler(this, ".text",
222 MCAsmParser::DirectiveHandler(
223 &DarwinAsmParser::ParseSectionDirectiveText));
224 Parser.AddDirectiveHandler(this, ".thread_init_func",
225 MCAsmParser::DirectiveHandler(
226 &DarwinAsmParser::ParseSectionDirectiveThreadInitFunc));
227 Parser.AddDirectiveHandler(this, ".tlv",
228 MCAsmParser::DirectiveHandler(
229 &DarwinAsmParser::ParseSectionDirectiveTLV));
Daniel Dunbare4749702010-07-12 18:12:02 +0000230 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000231
Daniel Dunbar492b7a22010-07-12 19:22:53 +0000232 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000233 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +0000234 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar4d5fe972010-07-12 20:42:34 +0000235 bool ParseDirectiveSection();
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000236 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +0000237 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
238 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbarb6c3a602010-07-12 19:37:35 +0000239 bool ParseDirectiveTBSS(StringRef, SMLoc);
240 bool ParseDirectiveZerofill(StringRef, SMLoc);
Daniel Dunbar47f56082010-07-12 20:23:36 +0000241
242 // Named Section Directive
243 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
244 return ParseSectionSwitch("__TEXT", "__const");
245 }
246 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
247 return ParseSectionSwitch("__TEXT", "__static_const");
248 }
249 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
250 return ParseSectionSwitch("__TEXT","__cstring",
251 MCSectionMachO::S_CSTRING_LITERALS);
252 }
253 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
254 return ParseSectionSwitch("__TEXT", "__literal4",
255 MCSectionMachO::S_4BYTE_LITERALS, 4);
256 }
257 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
258 return ParseSectionSwitch("__TEXT", "__literal8",
259 MCSectionMachO::S_8BYTE_LITERALS, 8);
260 }
261 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
262 return ParseSectionSwitch("__TEXT","__literal16",
263 MCSectionMachO::S_16BYTE_LITERALS, 16);
264 }
265 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
266 return ParseSectionSwitch("__TEXT","__constructor");
267 }
268 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
269 return ParseSectionSwitch("__TEXT","__destructor");
270 }
271 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
272 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
273 }
274 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
275 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
276 }
277 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
278 return ParseSectionSwitch("__TEXT","__symbol_stub",
279 MCSectionMachO::S_SYMBOL_STUBS |
280 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
281 // FIXME: Different on PPC and ARM.
282 0, 16);
283 }
284 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
285 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
286 MCSectionMachO::S_SYMBOL_STUBS |
287 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
288 }
289 bool ParseSectionDirectiveData(StringRef, SMLoc) {
290 return ParseSectionSwitch("__DATA", "__data");
291 }
292 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
293 return ParseSectionSwitch("__DATA", "__static_data");
294 }
295 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
296 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
297 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
298 }
299 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
300 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
301 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
302 }
303 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
304 return ParseSectionSwitch("__DATA", "__dyld");
305 }
306 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
307 return ParseSectionSwitch("__DATA", "__mod_init_func",
308 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
309 }
310 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
311 return ParseSectionSwitch("__DATA", "__mod_term_func",
312 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
313 }
314 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
315 return ParseSectionSwitch("__DATA", "__const");
316 }
317 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
318 return ParseSectionSwitch("__OBJC", "__class",
319 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
320 }
321 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
322 return ParseSectionSwitch("__OBJC", "__meta_class",
323 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
324 }
325 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
326 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
327 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
328 }
329 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
330 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
331 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
332 }
333 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
334 return ParseSectionSwitch("__OBJC", "__protocol",
335 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
336 }
337 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
338 return ParseSectionSwitch("__OBJC", "__string_object",
339 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
340 }
341 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
342 return ParseSectionSwitch("__OBJC", "__cls_meth",
343 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
344 }
345 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
346 return ParseSectionSwitch("__OBJC", "__inst_meth",
347 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
348 }
349 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
350 return ParseSectionSwitch("__OBJC", "__cls_refs",
351 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
352 MCSectionMachO::S_LITERAL_POINTERS, 4);
353 }
354 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
355 return ParseSectionSwitch("__OBJC", "__message_refs",
356 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
357 MCSectionMachO::S_LITERAL_POINTERS, 4);
358 }
359 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
360 return ParseSectionSwitch("__OBJC", "__symbols",
361 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
362 }
363 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
364 return ParseSectionSwitch("__OBJC", "__category",
365 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
366 }
367 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
368 return ParseSectionSwitch("__OBJC", "__class_vars",
369 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
370 }
371 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
372 return ParseSectionSwitch("__OBJC", "__instance_vars",
373 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
374 }
375 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
376 return ParseSectionSwitch("__OBJC", "__module_info",
377 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
378 }
379 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
380 return ParseSectionSwitch("__TEXT", "__cstring",
381 MCSectionMachO::S_CSTRING_LITERALS);
382 }
383 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
384 return ParseSectionSwitch("__TEXT", "__cstring",
385 MCSectionMachO::S_CSTRING_LITERALS);
386 }
387 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
388 return ParseSectionSwitch("__TEXT", "__cstring",
389 MCSectionMachO::S_CSTRING_LITERALS);
390 }
391 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
392 return ParseSectionSwitch("__OBJC", "__selector_strs",
393 MCSectionMachO::S_CSTRING_LITERALS);
394 }
395 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
396 return ParseSectionSwitch("__DATA", "__thread_data",
397 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
398 }
399 bool ParseSectionDirectiveText(StringRef, SMLoc) {
400 return ParseSectionSwitch("__TEXT", "__text",
401 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
402 }
403 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
404 return ParseSectionSwitch("__DATA", "__thread_vars",
405 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
406 }
407 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
408 return ParseSectionSwitch("__DATA", "__thread_init",
409 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
410 }
411
Daniel Dunbare4749702010-07-12 18:12:02 +0000412};
413
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000414class ELFAsmParser : public MCAsmParserExtension {
415 bool ParseSectionSwitch(StringRef Section, unsigned Type,
416 unsigned Flags, SectionKind Kind);
417
418public:
419 ELFAsmParser() {}
420
421 virtual void Initialize(MCAsmParser &Parser) {
422 // Call the base implementation.
423 this->MCAsmParserExtension::Initialize(Parser);
424
425 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
426 &ELFAsmParser::ParseSectionDirectiveData));
427 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
428 &ELFAsmParser::ParseSectionDirectiveText));
429 }
430
431 bool ParseSectionDirectiveData(StringRef, SMLoc) {
432 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
433 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
434 SectionKind::getDataRel());
435 }
436 bool ParseSectionDirectiveText(StringRef, SMLoc) {
437 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
438 MCSectionELF::SHF_EXECINSTR |
439 MCSectionELF::SHF_ALLOC, SectionKind::getText());
440 }
441};
442
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000443}
444
Chris Lattneraaec2052010-01-19 19:46:13 +0000445enum { DEFAULT_ADDRSPACE = 0 };
446
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000447AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
448 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000449 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000450 GenericParser(new GenericAsmParser), PlatformParser(0),
451 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000452 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000453
454 // Initialize the generic parser.
455 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000456
457 // Initialize the platform / file format parser.
458 //
459 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
460 // created.
461 if (_MAI.hasSubsectionsViaSymbols()) {
462 PlatformParser = new DarwinAsmParser;
463 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000464 } else {
465 PlatformParser = new ELFAsmParser;
466 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000467 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000468}
469
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000470AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000471 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000472 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000473}
474
Daniel Dunbar53131982010-07-12 17:27:45 +0000475void AsmParser::setTargetParser(TargetAsmParser &P) {
476 assert(!TargetParser && "Target parser is already initialized!");
477 TargetParser = &P;
478 TargetParser->Initialize(*this);
479}
480
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000481void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000482 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000483}
484
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000485bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000486 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000487 return true;
488}
489
Sean Callananbf2013e2010-01-20 23:19:55 +0000490void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
491 const char *Type) const {
492 SrcMgr.PrintMessage(Loc, Msg, Type);
493}
Sean Callananfd0b0282010-01-21 00:19:58 +0000494
495bool AsmParser::EnterIncludeFile(const std::string &Filename) {
496 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
497 if (NewBuf == -1)
498 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000499
Sean Callananfd0b0282010-01-21 00:19:58 +0000500 CurBuffer = NewBuf;
501
502 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
503
504 return false;
505}
506
507const AsmToken &AsmParser::Lex() {
508 const AsmToken *tok = &Lexer.Lex();
509
510 if (tok->is(AsmToken::Eof)) {
511 // If this is the end of an included file, pop the parent file off the
512 // include stack.
513 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
514 if (ParentIncludeLoc != SMLoc()) {
515 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
516 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
517 ParentIncludeLoc.getPointer());
518 tok = &Lexer.Lex();
519 }
520 }
521
522 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000523 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000524
Sean Callananfd0b0282010-01-21 00:19:58 +0000525 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000526}
527
Chris Lattner79180e22010-04-05 23:15:42 +0000528bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000529 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000530 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000531 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000532 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000533 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
535 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000536
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000537 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000538 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000539
Chris Lattnerb717fb02009-07-02 21:53:43 +0000540 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000541
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000542 AsmCond StartingCondState = TheCondState;
543
Chris Lattnerb717fb02009-07-02 21:53:43 +0000544 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000545 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000546 if (!ParseStatement()) continue;
547
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000548 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000549 HadError = true;
550 EatToEndOfStatement();
551 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000552
553 if (TheCondState.TheCond != StartingCondState.TheCond ||
554 TheCondState.Ignore != StartingCondState.Ignore)
555 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000556
Chris Lattner79180e22010-04-05 23:15:42 +0000557 // Finalize the output stream if there are no errors and if the client wants
558 // us to.
559 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000560 Out.Finish();
561
Chris Lattnerb717fb02009-07-02 21:53:43 +0000562 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000563}
564
Chris Lattner2cf5f142009-06-22 01:29:09 +0000565/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
566void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000567 while (Lexer.isNot(AsmToken::EndOfStatement) &&
568 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000569 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000570
571 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000572 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000573 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000574}
575
Chris Lattnerc4193832009-06-22 05:51:26 +0000576
Chris Lattner74ec1a32009-06-22 06:32:03 +0000577/// ParseParenExpr - Parse a paren expression and return it.
578/// NOTE: This assumes the leading '(' has already been consumed.
579///
580/// parenexpr ::= expr)
581///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000582bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000583 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000584 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000585 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000586 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000587 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000588 return false;
589}
Chris Lattnerc4193832009-06-22 05:51:26 +0000590
Chris Lattner74ec1a32009-06-22 06:32:03 +0000591/// ParsePrimaryExpr - Parse a primary expression and return it.
592/// primaryexpr ::= (parenexpr
593/// primaryexpr ::= symbol
594/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000595/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000596/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000597bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000598 switch (Lexer.getKind()) {
599 default:
600 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000601 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000602 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000603 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000604 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000605 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000606 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000607 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000608 case AsmToken::Identifier: {
609 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000610 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000611 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000612
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000613 // Mark the symbol as used in an expression.
614 Sym->setUsedInExpr(true);
615
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000616 // Lookup the symbol variant if used.
617 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
618 if (Split.first.size() != getTok().getIdentifier().size())
619 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
620
Chris Lattnerb4307b32010-01-15 19:28:38 +0000621 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000622 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000623
624 // If this is an absolute variable reference, substitute it now to preserve
625 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000626 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000627 if (Variant)
628 return Error(EndLoc, "unexpected modified on variable reference");
629
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000630 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000631 return false;
632 }
633
634 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000635 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000636 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000637 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000638 case AsmToken::Integer: {
639 SMLoc Loc = getTok().getLoc();
640 int64_t IntVal = getTok().getIntVal();
641 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000642 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000643 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000644 // Look for 'b' or 'f' following an Integer as a directional label
645 if (Lexer.getKind() == AsmToken::Identifier) {
646 StringRef IDVal = getTok().getString();
647 if (IDVal == "f" || IDVal == "b"){
648 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
649 IDVal == "f" ? 1 : 0);
650 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
651 getContext());
652 if(IDVal == "b" && Sym->isUndefined())
653 return Error(Loc, "invalid reference to undefined symbol");
654 EndLoc = Lexer.getLoc();
655 Lex(); // Eat identifier.
656 }
657 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000658 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000659 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000660 case AsmToken::Dot: {
661 // This is a '.' reference, which references the current PC. Emit a
662 // temporary label to the streamer and refer to it.
663 MCSymbol *Sym = Ctx.CreateTempSymbol();
664 Out.EmitLabel(Sym);
665 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
666 EndLoc = Lexer.getLoc();
667 Lex(); // Eat identifier.
668 return false;
669 }
670
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000673 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000674 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000675 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000676 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000677 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000678 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000679 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000680 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000681 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000682 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000683 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000684 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000685 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000686 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000687 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000688 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000689 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000690 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000691 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000692 }
693}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000694
Chris Lattnerb4307b32010-01-15 19:28:38 +0000695bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000696 SMLoc EndLoc;
697 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000698}
699
Chris Lattner74ec1a32009-06-22 06:32:03 +0000700/// ParseExpression - Parse an expression and return it.
701///
702/// expr ::= expr +,- expr -> lowest.
703/// expr ::= expr |,^,&,! expr -> middle.
704/// expr ::= expr *,/,%,<<,>> expr -> highest.
705/// expr ::= primaryexpr
706///
Chris Lattner54482b42010-01-15 19:39:23 +0000707bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000708 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000709 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000710 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
711 return true;
712
713 // Try to constant fold it up front, if possible.
714 int64_t Value;
715 if (Res->EvaluateAsAbsolute(Value))
716 Res = MCConstantExpr::Create(Value, getContext());
717
718 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000719}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000720
Chris Lattnerb4307b32010-01-15 19:28:38 +0000721bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000722 Res = 0;
723 return ParseParenExpr(Res, EndLoc) ||
724 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000725}
726
Daniel Dunbar475839e2009-06-29 20:37:27 +0000727bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000728 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000729
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000730 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000731 if (ParseExpression(Expr))
732 return true;
733
Daniel Dunbare00b0112009-10-16 01:57:52 +0000734 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000735 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000736
737 return false;
738}
739
Daniel Dunbar3f872332009-07-28 16:08:33 +0000740static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000741 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000742 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000743 default:
744 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000745
746 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000749 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000750 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000751 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000752 return 1;
753
754 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000756 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000757 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000759 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000760 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000761 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000762 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000763 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000764 case AsmToken::ExclaimEqual:
765 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000766 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000769 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000772 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000773 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000774 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000775 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000777 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000778 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000779 return 2;
780
781 // Intermediate Precedence: |, &, ^
782 //
783 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000785 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000786 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000790 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000791 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000792 return 3;
793
794 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000795 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000796 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000799 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000800 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000801 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000802 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000803 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000804 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000805 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000806 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000807 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000808 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000809 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000810 }
811}
812
813
814/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
815/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000816bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
817 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000818 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000819 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000820 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000821
822 // If the next token is lower precedence than we are allowed to eat, return
823 // successfully with what we ate already.
824 if (TokPrec < Precedence)
825 return false;
826
Sean Callanan79ed1a82010-01-19 20:22:31 +0000827 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000828
829 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000830 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000831 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000832
833 // If BinOp binds less tightly with RHS than the operator after RHS, let
834 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000835 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000836 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000837 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000838 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000839 }
840
Daniel Dunbar475839e2009-06-29 20:37:27 +0000841 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000842 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000843 }
844}
845
Chris Lattnerc4193832009-06-22 05:51:26 +0000846
847
848
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000849/// ParseStatement:
850/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000851/// ::= Label* Directive ...Operands... EndOfStatement
852/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000853bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000854 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000855 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000856 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000857 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000858 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000859
860 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000861 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000862 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000863 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000864 int64_t LocalLabelVal = -1;
865 // GUESS allow an integer followed by a ':' as a directional local label
866 if (Lexer.is(AsmToken::Integer)) {
867 LocalLabelVal = getTok().getIntVal();
868 if (LocalLabelVal < 0) {
869 if (!TheCondState.Ignore)
870 return TokError("unexpected token at start of statement");
871 IDVal = "";
872 }
873 else {
874 IDVal = getTok().getString();
875 Lex(); // Consume the integer token to be used as an identifier token.
876 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000877 if (!TheCondState.Ignore)
878 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000879 }
880 }
881 }
882 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000883 if (!TheCondState.Ignore)
884 return TokError("unexpected token at start of statement");
885 IDVal = "";
886 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000887
Chris Lattner7834fac2010-04-17 18:14:27 +0000888 // Handle conditional assembly here before checking for skipping. We
889 // have to do this so that .endif isn't skipped in a ".if 0" block for
890 // example.
891 if (IDVal == ".if")
892 return ParseDirectiveIf(IDLoc);
893 if (IDVal == ".elseif")
894 return ParseDirectiveElseIf(IDLoc);
895 if (IDVal == ".else")
896 return ParseDirectiveElse(IDLoc);
897 if (IDVal == ".endif")
898 return ParseDirectiveEndIf(IDLoc);
899
900 // If we are in a ".if 0" block, ignore this statement.
901 if (TheCondState.Ignore) {
902 EatToEndOfStatement();
903 return false;
904 }
905
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000906 // FIXME: Recurse on local labels?
907
908 // See what kind of statement we have.
909 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000910 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000911 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000912 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000913
914 // Diagnose attempt to use a variable as a label.
915 //
916 // FIXME: Diagnostics. Note the location of the definition as a label.
917 // FIXME: This doesn't diagnose assignment to a symbol which has been
918 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000919 MCSymbol *Sym;
920 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000921 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000922 else
923 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000924 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000925 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000926
Daniel Dunbar959fd882009-08-26 22:13:22 +0000927 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000928 Out.EmitLabel(Sym);
929
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000930 // Consume any end of statement token, if present, to avoid spurious
931 // AddBlankLine calls().
932 if (Lexer.is(AsmToken::EndOfStatement)) {
933 Lex();
934 if (Lexer.is(AsmToken::Eof))
935 return false;
936 }
937
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000938 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000939 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000940
Daniel Dunbar3f872332009-07-28 16:08:33 +0000941 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000942 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000943 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000944
Daniel Dunbare2ace502009-08-31 08:09:09 +0000945 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000946
947 default: // Normal instruction or directive.
948 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000949 }
950
951 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000952 if (IDVal[0] == '.') {
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000953 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000954 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000955 return ParseDirectiveSet();
956
Daniel Dunbara0d14262009-06-24 23:30:00 +0000957 // Data directives
958
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000959 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000960 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000961 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000962 return ParseDirectiveAscii(true);
963
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000964 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000965 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000966 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000967 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000968 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000969 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000970 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000971 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000972
973 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000974 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000975 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000976 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000977 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000978 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000979 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000980 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000981 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000982 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000983 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000984 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000985 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000986 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000987 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000988 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000989 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
990
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000991 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000992 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000993
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000994 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000995 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000996 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000997 return ParseDirectiveSpace();
998
Daniel Dunbard7b267b2009-06-30 00:33:19 +0000999 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001000
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001001 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001002 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001003 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001004 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001005 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001006 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001007 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001008 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001009 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001010 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001011 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001012 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001013 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001014 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001015 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001016 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001017 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001018 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +00001019 if (IDVal == ".type")
1020 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001021 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001022 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001023 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001024 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001025 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001026 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001027 if (IDVal == ".weak_def_can_be_hidden")
1028 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001029
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001030 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001031 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001032 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001033 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001034
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001035 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001036 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001037 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001038 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001039
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001040 // Look up the handler in the handler table.
1041 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1042 DirectiveMap.lookup(IDVal);
1043 if (Handler.first)
1044 return (Handler.first->*Handler.second)(IDVal, IDLoc);
1045
Kevin Enderby9c656452009-09-10 20:51:44 +00001046 // Target hook for parsing target specific directives.
1047 if (!getTargetParser().ParseDirective(ID))
1048 return false;
1049
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001050 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001051 EatToEndOfStatement();
1052 return false;
1053 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001054
Chris Lattnera7f13542010-05-19 23:34:33 +00001055 // Canonicalize the opcode to lower case.
1056 SmallString<128> Opcode;
1057 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1058 Opcode.push_back(tolower(IDVal[i]));
1059
Chris Lattner98986712010-01-14 22:21:20 +00001060 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001061 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001062 ParsedOperands);
1063 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
1064 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001065
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001066 // If parsing succeeded, match the instruction.
1067 if (!HadError) {
1068 MCInst Inst;
1069 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
1070 // Emit the instruction on success.
1071 Out.EmitInstruction(Inst);
1072 } else {
1073 // Otherwise emit a diagnostic about the match failure and set the error
1074 // flag.
1075 //
1076 // FIXME: We should give nicer diagnostics about the exact failure.
1077 Error(IDLoc, "unrecognized instruction");
1078 HadError = true;
1079 }
1080 }
Chris Lattner98986712010-01-14 22:21:20 +00001081
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001082 // If there was no error, consume the end-of-statement token. Otherwise this
1083 // will be done by our caller.
1084 if (!HadError)
1085 Lex();
Chris Lattner98986712010-01-14 22:21:20 +00001086
1087 // Free any parsed operands.
1088 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1089 delete ParsedOperands[i];
1090
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001091 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001092}
Chris Lattner9a023f72009-06-24 04:43:34 +00001093
Daniel Dunbare2ace502009-08-31 08:09:09 +00001094bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001095 // FIXME: Use better location, we should use proper tokens.
1096 SMLoc EqualLoc = Lexer.getLoc();
1097
Daniel Dunbar821e3332009-08-31 08:09:28 +00001098 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001099 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001100 return true;
1101
Daniel Dunbar3f872332009-07-28 16:08:33 +00001102 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001103 return TokError("unexpected token in assignment");
1104
1105 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001106 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001107
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001108 // Validate that the LHS is allowed to be a variable (either it has not been
1109 // used as a symbol, or it is an absolute symbol).
1110 MCSymbol *Sym = getContext().LookupSymbol(Name);
1111 if (Sym) {
1112 // Diagnose assignment to a label.
1113 //
1114 // FIXME: Diagnostics. Note the location of the definition as a label.
1115 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001116 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1117 ; // Allow redefinitions of undefined symbols only used in directives.
1118 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001119 return Error(EqualLoc, "redefinition of '" + Name + "'");
1120 else if (!Sym->isVariable())
1121 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001122 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001123 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1124 Name + "'");
1125 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001126 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001127
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001128 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001129
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001130 Sym->setUsedInExpr(true);
1131
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001132 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001133 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001134
1135 return false;
1136}
1137
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001138/// ParseIdentifier:
1139/// ::= identifier
1140/// ::= string
1141bool AsmParser::ParseIdentifier(StringRef &Res) {
1142 if (Lexer.isNot(AsmToken::Identifier) &&
1143 Lexer.isNot(AsmToken::String))
1144 return true;
1145
Sean Callanan18b83232010-01-19 21:44:56 +00001146 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001147
Sean Callanan79ed1a82010-01-19 20:22:31 +00001148 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001149
1150 return false;
1151}
1152
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001153/// ParseDirectiveSet:
1154/// ::= .set identifier ',' expression
1155bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001156 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001157
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001158 if (ParseIdentifier(Name))
1159 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001160
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001161 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001162 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001163 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001164
Daniel Dunbare2ace502009-08-31 08:09:09 +00001165 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001166}
1167
Chris Lattner9a023f72009-06-24 04:43:34 +00001168/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +00001169/// ::= .section identifier (',' identifier)*
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001170bool DarwinAsmParser::ParseDirectiveSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001171 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001172
Daniel Dunbarace63122009-08-11 03:42:33 +00001173 StringRef SectionName;
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001174 if (getParser().ParseIdentifier(SectionName))
Daniel Dunbarace63122009-08-11 03:42:33 +00001175 return Error(Loc, "expected identifier after '.section' directive");
1176
1177 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001178 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +00001179 return TokError("unexpected token in '.section' directive");
1180
Chris Lattnerff4bc462009-08-10 01:39:42 +00001181 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +00001182 SectionSpec += ",";
1183
1184 // Add all the tokens until the end of the line, ParseSectionSpecifier will
1185 // handle this.
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001186 StringRef EOL = getLexer().LexUntilEndOfStatement();
Chris Lattnerff4bc462009-08-10 01:39:42 +00001187 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001188
Sean Callanan79ed1a82010-01-19 20:22:31 +00001189 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001190 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001191 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001192 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001193
Chris Lattnerff4bc462009-08-10 01:39:42 +00001194
1195 StringRef Segment, Section;
1196 unsigned TAA, StubSize;
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001197 std::string ErrorStr =
Chris Lattnerff4bc462009-08-10 01:39:42 +00001198 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1199 TAA, StubSize);
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001200
Chris Lattnerff4bc462009-08-10 01:39:42 +00001201 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001202 return Error(Loc, ErrorStr.c_str());
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001203
Chris Lattner56594f92009-07-31 17:47:16 +00001204 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001205 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar4d5fe972010-07-12 20:42:34 +00001206 getStreamer().SwitchSection(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001207 Segment, Section, TAA, StubSize,
1208 isText ? SectionKind::getText()
1209 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001210 return false;
1211}
1212
Daniel Dunbar47f56082010-07-12 20:23:36 +00001213bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
1214 const char *Section,
1215 unsigned TAA, unsigned Align,
1216 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001217 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001218 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001219 Lex();
Daniel Dunbar47f56082010-07-12 20:23:36 +00001220
Chris Lattner56594f92009-07-31 17:47:16 +00001221 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001222 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar47f56082010-07-12 20:23:36 +00001223 getStreamer().SwitchSection(getContext().getMachOSection(
1224 Segment, Section, TAA, StubSize,
1225 isText ? SectionKind::getText()
1226 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001227
1228 // Set the implicit alignment, if any.
1229 //
1230 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1231 // alignment on the section (e.g., if one manually inserts bytes into the
1232 // section, then just issueing the section switch directive will not realign
1233 // the section. However, this is arguably more reasonable behavior, and there
1234 // is no good reason for someone to intentionally emit incorrectly sized
1235 // values into the implicitly aligned sections.
1236 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001237 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001238
Chris Lattner529fb542009-06-24 05:13:15 +00001239 return false;
1240}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001241
Daniel Dunbar7a56fc22010-07-12 20:08:04 +00001242bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
1243 unsigned Flags, SectionKind Kind) {
1244 if (getLexer().isNot(AsmToken::EndOfStatement))
1245 return TokError("unexpected token in section switching directive");
1246 Lex();
1247
1248 getStreamer().SwitchSection(getContext().getELFSection(
1249 Section, Type, Flags, Kind));
1250
1251 return false;
1252}
1253
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001254bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001255 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001256
1257 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001258 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001259 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1260 if (Str[i] != '\\') {
1261 Data += Str[i];
1262 continue;
1263 }
1264
1265 // Recognize escaped characters. Note that this escape semantics currently
1266 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1267 ++i;
1268 if (i == e)
1269 return TokError("unexpected backslash at end of string");
1270
1271 // Recognize octal sequences.
1272 if ((unsigned) (Str[i] - '0') <= 7) {
1273 // Consume up to three octal characters.
1274 unsigned Value = Str[i] - '0';
1275
1276 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1277 ++i;
1278 Value = Value * 8 + (Str[i] - '0');
1279
1280 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1281 ++i;
1282 Value = Value * 8 + (Str[i] - '0');
1283 }
1284 }
1285
1286 if (Value > 255)
1287 return TokError("invalid octal escape sequence (out of range)");
1288
1289 Data += (unsigned char) Value;
1290 continue;
1291 }
1292
1293 // Otherwise recognize individual escapes.
1294 switch (Str[i]) {
1295 default:
1296 // Just reject invalid escape sequences for now.
1297 return TokError("invalid escape sequence (unrecognized character)");
1298
1299 case 'b': Data += '\b'; break;
1300 case 'f': Data += '\f'; break;
1301 case 'n': Data += '\n'; break;
1302 case 'r': Data += '\r'; break;
1303 case 't': Data += '\t'; break;
1304 case '"': Data += '"'; break;
1305 case '\\': Data += '\\'; break;
1306 }
1307 }
1308
1309 return false;
1310}
1311
Daniel Dunbara0d14262009-06-24 23:30:00 +00001312/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001313/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001314bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001315 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001316 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001317 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001318 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001319
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001320 std::string Data;
1321 if (ParseEscapedString(Data))
1322 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001323
1324 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001325 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001326 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1327
Sean Callanan79ed1a82010-01-19 20:22:31 +00001328 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001329
1330 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001331 break;
1332
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001333 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001334 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001335 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001336 }
1337 }
1338
Sean Callanan79ed1a82010-01-19 20:22:31 +00001339 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001340 return false;
1341}
1342
1343/// ParseDirectiveValue
1344/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1345bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001346 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001347 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001348 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001349 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001350 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001351 return true;
1352
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001353 // Special case constant expressions to match code generator.
1354 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001355 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001356 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001357 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001358
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001359 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001360 break;
1361
1362 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001363 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001364 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001365 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001366 }
1367 }
1368
Sean Callanan79ed1a82010-01-19 20:22:31 +00001369 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001370 return false;
1371}
1372
1373/// ParseDirectiveSpace
1374/// ::= .space expression [ , expression ]
1375bool AsmParser::ParseDirectiveSpace() {
1376 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001377 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001378 return true;
1379
1380 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001381 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1382 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001383 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001384 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001385
Daniel Dunbar475839e2009-06-29 20:37:27 +00001386 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001387 return true;
1388
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001389 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001390 return TokError("unexpected token in '.space' directive");
1391 }
1392
Sean Callanan79ed1a82010-01-19 20:22:31 +00001393 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001394
1395 if (NumBytes <= 0)
1396 return TokError("invalid number of bytes in '.space' directive");
1397
1398 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001399 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001400
1401 return false;
1402}
1403
1404/// ParseDirectiveFill
1405/// ::= .fill expression , expression , expression
1406bool AsmParser::ParseDirectiveFill() {
1407 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001408 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001409 return true;
1410
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001411 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001412 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001413 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001414
1415 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001416 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001417 return true;
1418
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001419 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001420 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001421 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001422
1423 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001424 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001425 return true;
1426
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001427 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 return TokError("unexpected token in '.fill' directive");
1429
Sean Callanan79ed1a82010-01-19 20:22:31 +00001430 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001431
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001432 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1433 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001434
1435 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001436 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437
1438 return false;
1439}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001440
1441/// ParseDirectiveOrg
1442/// ::= .org expression [ , expression ]
1443bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001444 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001445 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001446 return true;
1447
1448 // Parse optional fill expression.
1449 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001450 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1451 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001452 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001453 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001454
Daniel Dunbar475839e2009-06-29 20:37:27 +00001455 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001456 return true;
1457
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001458 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001459 return TokError("unexpected token in '.org' directive");
1460 }
1461
Sean Callanan79ed1a82010-01-19 20:22:31 +00001462 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001463
1464 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1465 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001466 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001467
1468 return false;
1469}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001470
1471/// ParseDirectiveAlign
1472/// ::= {.align, ...} expression [ , expression [ , expression ]]
1473bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001474 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001475 int64_t Alignment;
1476 if (ParseAbsoluteExpression(Alignment))
1477 return true;
1478
1479 SMLoc MaxBytesLoc;
1480 bool HasFillExpr = false;
1481 int64_t FillExpr = 0;
1482 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001483 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1484 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001485 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001486 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001487
1488 // The fill expression can be omitted while specifying a maximum number of
1489 // alignment bytes, e.g:
1490 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001491 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001492 HasFillExpr = true;
1493 if (ParseAbsoluteExpression(FillExpr))
1494 return true;
1495 }
1496
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001497 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1498 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001499 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001500 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001501
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001502 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001503 if (ParseAbsoluteExpression(MaxBytesToFill))
1504 return true;
1505
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001506 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001507 return TokError("unexpected token in directive");
1508 }
1509 }
1510
Sean Callanan79ed1a82010-01-19 20:22:31 +00001511 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001512
Daniel Dunbar648ac512010-05-17 21:54:30 +00001513 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001514 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001515
1516 // Compute alignment in bytes.
1517 if (IsPow2) {
1518 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001519 if (Alignment >= 32) {
1520 Error(AlignmentLoc, "invalid alignment value");
1521 Alignment = 31;
1522 }
1523
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001524 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001525 }
1526
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001527 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001528 if (MaxBytesLoc.isValid()) {
1529 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001530 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1531 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001532 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001533 }
1534
1535 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001536 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1537 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001538 MaxBytesToFill = 0;
1539 }
1540 }
1541
Daniel Dunbar648ac512010-05-17 21:54:30 +00001542 // Check whether we should use optimal code alignment for this .align
1543 // directive.
1544 //
1545 // FIXME: This should be using a target hook.
1546 bool UseCodeAlign = false;
1547 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001548 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001549 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1550 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1551 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001552 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001553 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001554 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001556 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001557
1558 return false;
1559}
1560
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001561/// ParseDirectiveSymbolAttribute
1562/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001563bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001564 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001565 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001566 StringRef Name;
1567
1568 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001569 return TokError("expected identifier in directive");
1570
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001571 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001572
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001573 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001574
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001575 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001576 break;
1577
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001578 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001579 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001580 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001581 }
1582 }
1583
Sean Callanan79ed1a82010-01-19 20:22:31 +00001584 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001585 return false;
1586}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001587
Matt Fleming924c5e52010-05-21 11:36:59 +00001588/// ParseDirectiveELFType
1589/// ::= .type identifier , @attribute
1590bool AsmParser::ParseDirectiveELFType() {
1591 StringRef Name;
1592 if (ParseIdentifier(Name))
1593 return TokError("expected identifier in directive");
1594
1595 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001596 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001597
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001598 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001599 return TokError("unexpected token in '.type' directive");
1600 Lex();
1601
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001602 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001603 return TokError("expected '@' before type");
1604 Lex();
1605
1606 StringRef Type;
1607 SMLoc TypeLoc;
1608
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001609 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001610 if (ParseIdentifier(Type))
1611 return TokError("expected symbol type in directive");
1612
1613 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1614 .Case("function", MCSA_ELF_TypeFunction)
1615 .Case("object", MCSA_ELF_TypeObject)
1616 .Case("tls_object", MCSA_ELF_TypeTLS)
1617 .Case("common", MCSA_ELF_TypeCommon)
1618 .Case("notype", MCSA_ELF_TypeNoType)
1619 .Default(MCSA_Invalid);
1620
1621 if (Attr == MCSA_Invalid)
1622 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1623
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001624 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001625 return TokError("unexpected token in '.type' directive");
1626
1627 Lex();
1628
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001629 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001630
1631 return false;
1632}
1633
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001634/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001635/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001636bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001637 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001638 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001639 return TokError("expected identifier in directive");
1640
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001641 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001642 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001643
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001644 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001645 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001646 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001647
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001648 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001649 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001650 return true;
1651
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001652 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001653 return TokError("unexpected token in '.desc' directive");
1654
Sean Callanan79ed1a82010-01-19 20:22:31 +00001655 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001656
1657 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001658 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001659
1660 return false;
1661}
1662
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001663/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001664/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1665bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001666 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001667 StringRef Name;
1668 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001669 return TokError("expected identifier in directive");
1670
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001671 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001672 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001673
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001674 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001675 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001676 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001677
1678 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001679 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001680 if (ParseAbsoluteExpression(Size))
1681 return true;
1682
1683 int64_t Pow2Alignment = 0;
1684 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001685 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001686 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001687 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001688 if (ParseAbsoluteExpression(Pow2Alignment))
1689 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001690
1691 // If this target takes alignments in bytes (not log) validate and convert.
1692 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1693 if (!isPowerOf2_64(Pow2Alignment))
1694 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1695 Pow2Alignment = Log2_64(Pow2Alignment);
1696 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001697 }
1698
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001699 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001700 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001701
Sean Callanan79ed1a82010-01-19 20:22:31 +00001702 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001703
Chris Lattner1fc3d752009-07-09 17:25:12 +00001704 // NOTE: a size of zero for a .comm should create a undefined symbol
1705 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001706 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001707 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1708 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001709
Eric Christopherc260a3e2010-05-14 01:38:54 +00001710 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001711 // may internally end up wanting an alignment in bytes.
1712 // FIXME: Diagnose overflow.
1713 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001714 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1715 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001716
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001717 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001718 return Error(IDLoc, "invalid symbol redefinition");
1719
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001720 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001721 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001722 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001723 getStreamer().EmitZerofill(Ctx.getMachOSection(
1724 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1725 0, SectionKind::getBSS()),
1726 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001727 return false;
1728 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001729
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001730 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001731 return false;
1732}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001733
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001734/// ParseDirectiveZerofill
Chris Lattner9be3fee2009-07-10 22:20:30 +00001735/// ::= .zerofill segname , sectname [, identifier , size_expression [
1736/// , align_expression ]]
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001737bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001738 StringRef Segment;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001739 if (getParser().ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001740 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001741
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001742 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001743 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001744 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001745
1746 StringRef Section;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001747 if (getParser().ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001748 return TokError("expected section name after comma in '.zerofill' "
1749 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001750
Chris Lattner9be3fee2009-07-10 22:20:30 +00001751 // If this is the end of the line all that was wanted was to create the
1752 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001753 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001754 // Create the zerofill section but no symbol
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001755 getStreamer().EmitZerofill(getContext().getMachOSection(
1756 Segment, Section, MCSectionMachO::S_ZEROFILL,
1757 0, SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001758 return false;
1759 }
1760
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001761 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001762 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001763 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001764
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001765 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001766 StringRef IDStr;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001767 if (getParser().ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001768 return TokError("expected identifier in directive");
1769
1770 // handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001771 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001772
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001773 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001774 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001775 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001776
1777 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001778 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001779 if (getParser().ParseAbsoluteExpression(Size))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001780 return true;
1781
1782 int64_t Pow2Alignment = 0;
1783 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001784 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001785 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001786 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001787 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001788 return true;
1789 }
1790
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001791 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001792 return TokError("unexpected token in '.zerofill' directive");
1793
Sean Callanan79ed1a82010-01-19 20:22:31 +00001794 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001795
1796 if (Size < 0)
1797 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1798 "than zero");
1799
Eric Christopherc260a3e2010-05-14 01:38:54 +00001800 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001801 // may internally end up wanting an alignment in bytes.
1802 // FIXME: Diagnose overflow.
1803 if (Pow2Alignment < 0)
1804 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1805 "can't be less than zero");
1806
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001807 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001808 return Error(IDLoc, "invalid symbol redefinition");
1809
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001810 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001811 //
1812 // FIXME: Arch specific.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001813 getStreamer().EmitZerofill(getContext().getMachOSection(
1814 Segment, Section, MCSectionMachO::S_ZEROFILL,
1815 0, SectionKind::getBSS()),
1816 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001817
1818 return false;
1819}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001820
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001821/// ParseDirectiveTBSS
Eric Christopher482eba02010-05-14 01:50:28 +00001822/// ::= .tbss identifier, size, align
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001823bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001824 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001825 StringRef Name;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001826 if (getParser().ParseIdentifier(Name))
Eric Christopher482eba02010-05-14 01:50:28 +00001827 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001828
Eric Christopher482eba02010-05-14 01:50:28 +00001829 // Handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001830 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001831
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001832 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001833 return TokError("unexpected token in directive");
1834 Lex();
1835
1836 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001837 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001838 if (getParser().ParseAbsoluteExpression(Size))
Eric Christopher482eba02010-05-14 01:50:28 +00001839 return true;
1840
1841 int64_t Pow2Alignment = 0;
1842 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001843 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001844 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001845 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001846 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Eric Christopher482eba02010-05-14 01:50:28 +00001847 return true;
1848 }
1849
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001850 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001851 return TokError("unexpected token in '.tbss' directive");
1852
1853 Lex();
1854
1855 if (Size < 0)
1856 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1857 "zero");
1858
1859 // FIXME: Diagnose overflow.
1860 if (Pow2Alignment < 0)
1861 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1862 "than zero");
1863
1864 if (!Sym->isUndefined())
1865 return Error(IDLoc, "invalid symbol redefinition");
1866
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001867 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001868 "__DATA", "__thread_bss",
1869 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1870 0, SectionKind::getThreadBSS()),
1871 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001872
1873 return false;
1874}
1875
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001876/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001877/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001878bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001879 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001880 return TokError("unexpected token in '.subsections_via_symbols' directive");
1881
Sean Callanan79ed1a82010-01-19 20:22:31 +00001882 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001883
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001884 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001885
1886 return false;
1887}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001888
1889/// ParseDirectiveAbort
1890/// ::= .abort [ "abort_string" ]
1891bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001892 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001893 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001894
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001895 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001896 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1897 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001898 return TokError("expected string in '.abort' directive");
1899
Sean Callanan18b83232010-01-19 21:44:56 +00001900 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001901
Sean Callanan79ed1a82010-01-19 20:22:31 +00001902 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001903 }
1904
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001905 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001906 return TokError("unexpected token in '.abort' directive");
1907
Sean Callanan79ed1a82010-01-19 20:22:31 +00001908 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001909
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001910 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001911 if (Str.empty())
1912 Error(Loc, ".abort detected. Assembly stopping.");
1913 else
1914 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001915
1916 return false;
1917}
Kevin Enderby71148242009-07-14 21:35:03 +00001918
1919/// ParseDirectiveLsym
1920/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001921bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001922 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001923 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001924 return TokError("expected identifier in directive");
1925
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001926 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001927 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001928
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001929 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001930 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001931 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001932
Daniel Dunbar821e3332009-08-31 08:09:28 +00001933 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001934 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001935 return true;
1936
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001937 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001938 return TokError("unexpected token in '.lsym' directive");
1939
Sean Callanan79ed1a82010-01-19 20:22:31 +00001940 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001941
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001942 // We don't currently support this directive.
1943 //
1944 // FIXME: Diagnostic location!
1945 (void) Sym;
1946 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001947}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001948
1949/// ParseDirectiveInclude
1950/// ::= .include "filename"
1951bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001952 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001953 return TokError("expected string in '.include' directive");
1954
Sean Callanan18b83232010-01-19 21:44:56 +00001955 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001956 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001957 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001958
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001959 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001960 return TokError("unexpected token in '.include' directive");
1961
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001962 // Strip the quotes.
1963 Filename = Filename.substr(1, Filename.size()-2);
1964
1965 // Attempt to switch the lexer to the included file before consuming the end
1966 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001967 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001968 PrintMessage(IncludeLoc,
1969 "Could not find include file '" + Filename + "'",
1970 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001971 return true;
1972 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001973
1974 return false;
1975}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001976
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001977/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001978/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001979bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1980 SMLoc IDLoc) {
1981 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001982 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001983 return TokError("expected string in '.dump' or '.load' directive");
1984
Sean Callanan79ed1a82010-01-19 20:22:31 +00001985 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001986
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001987 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001988 return TokError("unexpected token in '.dump' or '.load' directive");
1989
Sean Callanan79ed1a82010-01-19 20:22:31 +00001990 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001991
Kevin Enderby5026ae42009-07-20 20:25:37 +00001992 // FIXME: If/when .dump and .load are implemented they will be done in the
1993 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001994 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001995 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001996 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00001997 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001998
1999 return false;
2000}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002001
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002002/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002003/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002004bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002005 std::string LogMessage;
2006
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002007 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002008 LogMessage = "";
2009 else{
2010 LogMessage = getTok().getString();
2011 Lex();
2012 }
2013
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002014 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002015 return TokError("unexpected token in '.secure_log_unique' directive");
2016
2017 if (getContext().getSecureLogUsed() != false)
2018 return Error(IDLoc, ".secure_log_unique specified multiple times");
2019
2020 char *SecureLogFile = getContext().getSecureLogFile();
2021 if (SecureLogFile == NULL)
2022 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
2023 "environment variable unset.");
2024
2025 raw_ostream *OS = getContext().getSecureLog();
2026 if (OS == NULL) {
2027 std::string Err;
2028 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
2029 if (!Err.empty()) {
2030 delete OS;
2031 return Error(IDLoc, Twine("can't open secure log file: ") +
2032 SecureLogFile + " (" + Err + ")");
2033 }
2034 getContext().setSecureLog(OS);
2035 }
2036
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002037 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
2038 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
2039 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002040 << LogMessage + "\n";
2041
2042 getContext().setSecureLogUsed(true);
2043
2044 return false;
2045}
2046
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002047/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002048/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002049bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002050 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002051 return TokError("unexpected token in '.secure_log_reset' directive");
2052
2053 Lex();
2054
2055 getContext().setSecureLogUsed(false);
2056
2057 return false;
2058}
2059
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002060/// ParseDirectiveIf
2061/// ::= .if expression
2062bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002063 TheCondStack.push_back(TheCondState);
2064 TheCondState.TheCond = AsmCond::IfCond;
2065 if(TheCondState.Ignore) {
2066 EatToEndOfStatement();
2067 }
2068 else {
2069 int64_t ExprValue;
2070 if (ParseAbsoluteExpression(ExprValue))
2071 return true;
2072
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002073 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002074 return TokError("unexpected token in '.if' directive");
2075
Sean Callanan79ed1a82010-01-19 20:22:31 +00002076 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002077
2078 TheCondState.CondMet = ExprValue;
2079 TheCondState.Ignore = !TheCondState.CondMet;
2080 }
2081
2082 return false;
2083}
2084
2085/// ParseDirectiveElseIf
2086/// ::= .elseif expression
2087bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2088 if (TheCondState.TheCond != AsmCond::IfCond &&
2089 TheCondState.TheCond != AsmCond::ElseIfCond)
2090 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2091 " an .elseif");
2092 TheCondState.TheCond = AsmCond::ElseIfCond;
2093
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002094 bool LastIgnoreState = false;
2095 if (!TheCondStack.empty())
2096 LastIgnoreState = TheCondStack.back().Ignore;
2097 if (LastIgnoreState || TheCondState.CondMet) {
2098 TheCondState.Ignore = true;
2099 EatToEndOfStatement();
2100 }
2101 else {
2102 int64_t ExprValue;
2103 if (ParseAbsoluteExpression(ExprValue))
2104 return true;
2105
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002106 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002107 return TokError("unexpected token in '.elseif' directive");
2108
Sean Callanan79ed1a82010-01-19 20:22:31 +00002109 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002110 TheCondState.CondMet = ExprValue;
2111 TheCondState.Ignore = !TheCondState.CondMet;
2112 }
2113
2114 return false;
2115}
2116
2117/// ParseDirectiveElse
2118/// ::= .else
2119bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002120 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002121 return TokError("unexpected token in '.else' directive");
2122
Sean Callanan79ed1a82010-01-19 20:22:31 +00002123 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002124
2125 if (TheCondState.TheCond != AsmCond::IfCond &&
2126 TheCondState.TheCond != AsmCond::ElseIfCond)
2127 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2128 ".elseif");
2129 TheCondState.TheCond = AsmCond::ElseCond;
2130 bool LastIgnoreState = false;
2131 if (!TheCondStack.empty())
2132 LastIgnoreState = TheCondStack.back().Ignore;
2133 if (LastIgnoreState || TheCondState.CondMet)
2134 TheCondState.Ignore = true;
2135 else
2136 TheCondState.Ignore = false;
2137
2138 return false;
2139}
2140
2141/// ParseDirectiveEndIf
2142/// ::= .endif
2143bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002144 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002145 return TokError("unexpected token in '.endif' directive");
2146
Sean Callanan79ed1a82010-01-19 20:22:31 +00002147 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002148
2149 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2150 TheCondStack.empty())
2151 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2152 ".else");
2153 if (!TheCondStack.empty()) {
2154 TheCondState = TheCondStack.back();
2155 TheCondStack.pop_back();
2156 }
2157
2158 return false;
2159}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002160
2161/// ParseDirectiveFile
2162/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002163bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002164 // FIXME: I'm not sure what this is.
2165 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00002166 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002167 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002168 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002169
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002170 if (FileNumber < 1)
2171 return TokError("file number less than one");
2172 }
2173
Daniel Dunbareceec052010-07-12 17:45:27 +00002174 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002175 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002176
Chris Lattnerd32e8032010-01-25 19:02:58 +00002177 StringRef Filename = getTok().getString();
2178 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002179 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002180
Daniel Dunbareceec052010-07-12 17:45:27 +00002181 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002182 return TokError("unexpected token in '.file' directive");
2183
Chris Lattnerd32e8032010-01-25 19:02:58 +00002184 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002185 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00002186 else
Daniel Dunbareceec052010-07-12 17:45:27 +00002187 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
2188
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002189 return false;
2190}
2191
2192/// ParseDirectiveLine
2193/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002194bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002195 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2196 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002197 return TokError("unexpected token in '.line' directive");
2198
Sean Callanan18b83232010-01-19 21:44:56 +00002199 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002200 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002201 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002202
2203 // FIXME: Do something with the .line.
2204 }
2205
Daniel Dunbareceec052010-07-12 17:45:27 +00002206 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002207 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002208
2209 return false;
2210}
2211
2212
2213/// ParseDirectiveLoc
2214/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002215bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002216 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002217 return TokError("unexpected token in '.loc' directive");
2218
2219 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002220 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002221 (void) FileNumber;
2222 // FIXME: Validate file.
2223
Sean Callanan79ed1a82010-01-19 20:22:31 +00002224 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002225 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2226 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002227 return TokError("unexpected token in '.loc' directive");
2228
Sean Callanan18b83232010-01-19 21:44:56 +00002229 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002230 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002231 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002232
Daniel Dunbareceec052010-07-12 17:45:27 +00002233 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2234 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002235 return TokError("unexpected token in '.loc' directive");
2236
Sean Callanan18b83232010-01-19 21:44:56 +00002237 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002238 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002239 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002240
2241 // FIXME: Do something with the .loc.
2242 }
2243 }
2244
Daniel Dunbareceec052010-07-12 17:45:27 +00002245 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002246 return TokError("unexpected token in '.file' directive");
2247
2248 return false;
2249}
2250