blob: 2d602bb7855f3a9fea246c8b2f5aca06143f0de1 [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));
85 Parser.AddDirectiveHandler(this, ".secure_log_unique",
86 MCAsmParser::DirectiveHandler(
87 &DarwinAsmParser::ParseDirectiveSecureLogUnique));
88 Parser.AddDirectiveHandler(this, ".secure_log_reset",
89 MCAsmParser::DirectiveHandler(
90 &DarwinAsmParser::ParseDirectiveSecureLogReset));
Daniel Dunbarb6c3a602010-07-12 19:37:35 +000091 Parser.AddDirectiveHandler(this, ".tbss",
92 MCAsmParser::DirectiveHandler(
93 &DarwinAsmParser::ParseDirectiveTBSS));
94 Parser.AddDirectiveHandler(this, ".zerofill",
95 MCAsmParser::DirectiveHandler(
96 &DarwinAsmParser::ParseDirectiveZerofill));
Daniel Dunbar47f56082010-07-12 20:23:36 +000097
98 // Special section directives.
99 Parser.AddDirectiveHandler(this, ".const",
100 MCAsmParser::DirectiveHandler(
101 &DarwinAsmParser::ParseSectionDirectiveConst));
102 Parser.AddDirectiveHandler(this, ".const_data",
103 MCAsmParser::DirectiveHandler(
104 &DarwinAsmParser::ParseSectionDirectiveConstData));
105 Parser.AddDirectiveHandler(this, ".constructor",
106 MCAsmParser::DirectiveHandler(
107 &DarwinAsmParser::ParseSectionDirectiveConstructor));
108 Parser.AddDirectiveHandler(this, ".cstring",
109 MCAsmParser::DirectiveHandler(
110 &DarwinAsmParser::ParseSectionDirectiveCString));
111 Parser.AddDirectiveHandler(this, ".data",
112 MCAsmParser::DirectiveHandler(
113 &DarwinAsmParser::ParseSectionDirectiveData));
114 Parser.AddDirectiveHandler(this, ".destructor",
115 MCAsmParser::DirectiveHandler(
116 &DarwinAsmParser::ParseSectionDirectiveDestructor));
117 Parser.AddDirectiveHandler(this, ".dyld",
118 MCAsmParser::DirectiveHandler(
119 &DarwinAsmParser::ParseSectionDirectiveDyld));
120 Parser.AddDirectiveHandler(this, ".fvmlib_init0",
121 MCAsmParser::DirectiveHandler(
122 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit0));
123 Parser.AddDirectiveHandler(this, ".fvmlib_init1",
124 MCAsmParser::DirectiveHandler(
125 &DarwinAsmParser::ParseSectionDirectiveFVMLibInit1));
126 Parser.AddDirectiveHandler(this, ".lazy_symbol_pointer",
127 MCAsmParser::DirectiveHandler(
128 &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers));
129 Parser.AddDirectiveHandler(this, ".literal16",
130 MCAsmParser::DirectiveHandler(
131 &DarwinAsmParser::ParseSectionDirectiveLiteral16));
132 Parser.AddDirectiveHandler(this, ".literal4",
133 MCAsmParser::DirectiveHandler(
134 &DarwinAsmParser::ParseSectionDirectiveLiteral4));
135 Parser.AddDirectiveHandler(this, ".literal8",
136 MCAsmParser::DirectiveHandler(
137 &DarwinAsmParser::ParseSectionDirectiveLiteral8));
138 Parser.AddDirectiveHandler(this, ".mod_init_func",
139 MCAsmParser::DirectiveHandler(
140 &DarwinAsmParser::ParseSectionDirectiveModInitFunc));
141 Parser.AddDirectiveHandler(this, ".mod_term_func",
142 MCAsmParser::DirectiveHandler(
143 &DarwinAsmParser::ParseSectionDirectiveModTermFunc));
144 Parser.AddDirectiveHandler(this, ".non_lazy_symbol_pointer",
145 MCAsmParser::DirectiveHandler(
146 &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers));
147 Parser.AddDirectiveHandler(this, ".objc_cat_cls_meth",
148 MCAsmParser::DirectiveHandler(
149 &DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth));
150 Parser.AddDirectiveHandler(this, ".objc_cat_inst_meth",
151 MCAsmParser::DirectiveHandler(
152 &DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth));
153 Parser.AddDirectiveHandler(this, ".objc_category",
154 MCAsmParser::DirectiveHandler(
155 &DarwinAsmParser::ParseSectionDirectiveObjCCategory));
156 Parser.AddDirectiveHandler(this, ".objc_class",
157 MCAsmParser::DirectiveHandler(
158 &DarwinAsmParser::ParseSectionDirectiveObjCClass));
159 Parser.AddDirectiveHandler(this, ".objc_class_names",
160 MCAsmParser::DirectiveHandler(
161 &DarwinAsmParser::ParseSectionDirectiveObjCClassNames));
162 Parser.AddDirectiveHandler(this, ".objc_class_vars",
163 MCAsmParser::DirectiveHandler(
164 &DarwinAsmParser::ParseSectionDirectiveObjCClassVars));
165 Parser.AddDirectiveHandler(this, ".objc_cls_meth",
166 MCAsmParser::DirectiveHandler(
167 &DarwinAsmParser::ParseSectionDirectiveObjCClsMeth));
168 Parser.AddDirectiveHandler(this, ".objc_cls_refs",
169 MCAsmParser::DirectiveHandler(
170 &DarwinAsmParser::ParseSectionDirectiveObjCClsRefs));
171 Parser.AddDirectiveHandler(this, ".objc_inst_meth",
172 MCAsmParser::DirectiveHandler(
173 &DarwinAsmParser::ParseSectionDirectiveObjCInstMeth));
174 Parser.AddDirectiveHandler(this, ".objc_instance_vars",
175 MCAsmParser::DirectiveHandler(
176 &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars));
177 Parser.AddDirectiveHandler(this, ".objc_message_refs",
178 MCAsmParser::DirectiveHandler(
179 &DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs));
180 Parser.AddDirectiveHandler(this, ".objc_meta_class",
181 MCAsmParser::DirectiveHandler(
182 &DarwinAsmParser::ParseSectionDirectiveObjCMetaClass));
183 Parser.AddDirectiveHandler(this, ".objc_meth_var_names",
184 MCAsmParser::DirectiveHandler(
185 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames));
186 Parser.AddDirectiveHandler(this, ".objc_meth_var_types",
187 MCAsmParser::DirectiveHandler(
188 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes));
189 Parser.AddDirectiveHandler(this, ".objc_module_info",
190 MCAsmParser::DirectiveHandler(
191 &DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo));
192 Parser.AddDirectiveHandler(this, ".objc_protocol",
193 MCAsmParser::DirectiveHandler(
194 &DarwinAsmParser::ParseSectionDirectiveObjCProtocol));
195 Parser.AddDirectiveHandler(this, ".objc_selector_strs",
196 MCAsmParser::DirectiveHandler(
197 &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs));
198 Parser.AddDirectiveHandler(this, ".objc_string_object",
199 MCAsmParser::DirectiveHandler(
200 &DarwinAsmParser::ParseSectionDirectiveObjCStringObject));
201 Parser.AddDirectiveHandler(this, ".objc_symbols",
202 MCAsmParser::DirectiveHandler(
203 &DarwinAsmParser::ParseSectionDirectiveObjCSymbols));
204 Parser.AddDirectiveHandler(this, ".picsymbol_stub",
205 MCAsmParser::DirectiveHandler(
206 &DarwinAsmParser::ParseSectionDirectivePICSymbolStub));
207 Parser.AddDirectiveHandler(this, ".static_const",
208 MCAsmParser::DirectiveHandler(
209 &DarwinAsmParser::ParseSectionDirectiveStaticConst));
210 Parser.AddDirectiveHandler(this, ".static_data",
211 MCAsmParser::DirectiveHandler(
212 &DarwinAsmParser::ParseSectionDirectiveStaticData));
213 Parser.AddDirectiveHandler(this, ".symbol_stub",
214 MCAsmParser::DirectiveHandler(
215 &DarwinAsmParser::ParseSectionDirectiveSymbolStub));
216 Parser.AddDirectiveHandler(this, ".tdata",
217 MCAsmParser::DirectiveHandler(
218 &DarwinAsmParser::ParseSectionDirectiveTData));
219 Parser.AddDirectiveHandler(this, ".text",
220 MCAsmParser::DirectiveHandler(
221 &DarwinAsmParser::ParseSectionDirectiveText));
222 Parser.AddDirectiveHandler(this, ".thread_init_func",
223 MCAsmParser::DirectiveHandler(
224 &DarwinAsmParser::ParseSectionDirectiveThreadInitFunc));
225 Parser.AddDirectiveHandler(this, ".tlv",
226 MCAsmParser::DirectiveHandler(
227 &DarwinAsmParser::ParseSectionDirectiveTLV));
Daniel Dunbare4749702010-07-12 18:12:02 +0000228 }
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000229
Daniel Dunbar492b7a22010-07-12 19:22:53 +0000230 bool ParseDirectiveDesc(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000231 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +0000232 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar9ac66b02010-07-12 18:49:22 +0000233 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +0000234 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
235 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
Daniel Dunbarb6c3a602010-07-12 19:37:35 +0000236 bool ParseDirectiveTBSS(StringRef, SMLoc);
237 bool ParseDirectiveZerofill(StringRef, SMLoc);
Daniel Dunbar47f56082010-07-12 20:23:36 +0000238
239 // Named Section Directive
240 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
241 return ParseSectionSwitch("__TEXT", "__const");
242 }
243 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
244 return ParseSectionSwitch("__TEXT", "__static_const");
245 }
246 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
247 return ParseSectionSwitch("__TEXT","__cstring",
248 MCSectionMachO::S_CSTRING_LITERALS);
249 }
250 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
251 return ParseSectionSwitch("__TEXT", "__literal4",
252 MCSectionMachO::S_4BYTE_LITERALS, 4);
253 }
254 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
255 return ParseSectionSwitch("__TEXT", "__literal8",
256 MCSectionMachO::S_8BYTE_LITERALS, 8);
257 }
258 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
259 return ParseSectionSwitch("__TEXT","__literal16",
260 MCSectionMachO::S_16BYTE_LITERALS, 16);
261 }
262 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
263 return ParseSectionSwitch("__TEXT","__constructor");
264 }
265 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
266 return ParseSectionSwitch("__TEXT","__destructor");
267 }
268 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
269 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
270 }
271 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
272 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
273 }
274 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
275 return ParseSectionSwitch("__TEXT","__symbol_stub",
276 MCSectionMachO::S_SYMBOL_STUBS |
277 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
278 // FIXME: Different on PPC and ARM.
279 0, 16);
280 }
281 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
282 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
283 MCSectionMachO::S_SYMBOL_STUBS |
284 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
285 }
286 bool ParseSectionDirectiveData(StringRef, SMLoc) {
287 return ParseSectionSwitch("__DATA", "__data");
288 }
289 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
290 return ParseSectionSwitch("__DATA", "__static_data");
291 }
292 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
293 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
294 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
295 }
296 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
297 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
298 MCSectionMachO::S_LAZY_SYMBOL_POINTERS, 4);
299 }
300 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
301 return ParseSectionSwitch("__DATA", "__dyld");
302 }
303 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
304 return ParseSectionSwitch("__DATA", "__mod_init_func",
305 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS, 4);
306 }
307 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
308 return ParseSectionSwitch("__DATA", "__mod_term_func",
309 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS, 4);
310 }
311 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
312 return ParseSectionSwitch("__DATA", "__const");
313 }
314 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
315 return ParseSectionSwitch("__OBJC", "__class",
316 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
317 }
318 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
319 return ParseSectionSwitch("__OBJC", "__meta_class",
320 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
321 }
322 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
323 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
324 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
325 }
326 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
327 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
328 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
329 }
330 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
331 return ParseSectionSwitch("__OBJC", "__protocol",
332 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
333 }
334 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
335 return ParseSectionSwitch("__OBJC", "__string_object",
336 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
337 }
338 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
339 return ParseSectionSwitch("__OBJC", "__cls_meth",
340 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
341 }
342 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
343 return ParseSectionSwitch("__OBJC", "__inst_meth",
344 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
345 }
346 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
347 return ParseSectionSwitch("__OBJC", "__cls_refs",
348 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
349 MCSectionMachO::S_LITERAL_POINTERS, 4);
350 }
351 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
352 return ParseSectionSwitch("__OBJC", "__message_refs",
353 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
354 MCSectionMachO::S_LITERAL_POINTERS, 4);
355 }
356 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
357 return ParseSectionSwitch("__OBJC", "__symbols",
358 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
359 }
360 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
361 return ParseSectionSwitch("__OBJC", "__category",
362 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
363 }
364 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
365 return ParseSectionSwitch("__OBJC", "__class_vars",
366 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
367 }
368 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
369 return ParseSectionSwitch("__OBJC", "__instance_vars",
370 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
371 }
372 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
373 return ParseSectionSwitch("__OBJC", "__module_info",
374 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
375 }
376 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
377 return ParseSectionSwitch("__TEXT", "__cstring",
378 MCSectionMachO::S_CSTRING_LITERALS);
379 }
380 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
381 return ParseSectionSwitch("__TEXT", "__cstring",
382 MCSectionMachO::S_CSTRING_LITERALS);
383 }
384 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
385 return ParseSectionSwitch("__TEXT", "__cstring",
386 MCSectionMachO::S_CSTRING_LITERALS);
387 }
388 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
389 return ParseSectionSwitch("__OBJC", "__selector_strs",
390 MCSectionMachO::S_CSTRING_LITERALS);
391 }
392 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
393 return ParseSectionSwitch("__DATA", "__thread_data",
394 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
395 }
396 bool ParseSectionDirectiveText(StringRef, SMLoc) {
397 return ParseSectionSwitch("__TEXT", "__text",
398 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
399 }
400 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
401 return ParseSectionSwitch("__DATA", "__thread_vars",
402 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
403 }
404 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
405 return ParseSectionSwitch("__DATA", "__thread_init",
406 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
407 }
408
Daniel Dunbare4749702010-07-12 18:12:02 +0000409};
410
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000411class ELFAsmParser : public MCAsmParserExtension {
412 bool ParseSectionSwitch(StringRef Section, unsigned Type,
413 unsigned Flags, SectionKind Kind);
414
415public:
416 ELFAsmParser() {}
417
418 virtual void Initialize(MCAsmParser &Parser) {
419 // Call the base implementation.
420 this->MCAsmParserExtension::Initialize(Parser);
421
422 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
423 &ELFAsmParser::ParseSectionDirectiveData));
424 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
425 &ELFAsmParser::ParseSectionDirectiveText));
426 }
427
428 bool ParseSectionDirectiveData(StringRef, SMLoc) {
429 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
430 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
431 SectionKind::getDataRel());
432 }
433 bool ParseSectionDirectiveText(StringRef, SMLoc) {
434 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
435 MCSectionELF::SHF_EXECINSTR |
436 MCSectionELF::SHF_ALLOC, SectionKind::getText());
437 }
438};
439
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000440}
441
Chris Lattneraaec2052010-01-19 19:46:13 +0000442enum { DEFAULT_ADDRSPACE = 0 };
443
Daniel Dunbar9186fa62010-07-01 20:41:56 +0000444AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
445 MCStreamer &_Out, const MCAsmInfo &_MAI)
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000446 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM),
Daniel Dunbare4749702010-07-12 18:12:02 +0000447 GenericParser(new GenericAsmParser), PlatformParser(0),
448 TargetParser(0), CurBuffer(0) {
Sean Callananfd0b0282010-01-21 00:19:58 +0000449 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000450
451 // Initialize the generic parser.
452 GenericParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000453
454 // Initialize the platform / file format parser.
455 //
456 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are
457 // created.
458 if (_MAI.hasSubsectionsViaSymbols()) {
459 PlatformParser = new DarwinAsmParser;
460 PlatformParser->Initialize(*this);
Daniel Dunbar7a56fc22010-07-12 20:08:04 +0000461 } else {
462 PlatformParser = new ELFAsmParser;
463 PlatformParser->Initialize(*this);
Daniel Dunbare4749702010-07-12 18:12:02 +0000464 }
Chris Lattnerebb89b42009-09-27 21:16:52 +0000465}
466
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000467AsmParser::~AsmParser() {
Daniel Dunbare4749702010-07-12 18:12:02 +0000468 delete PlatformParser;
Daniel Dunbar81ea00f2010-07-12 17:54:38 +0000469 delete GenericParser;
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000470}
471
Daniel Dunbar53131982010-07-12 17:27:45 +0000472void AsmParser::setTargetParser(TargetAsmParser &P) {
473 assert(!TargetParser && "Target parser is already initialized!");
474 TargetParser = &P;
475 TargetParser->Initialize(*this);
476}
477
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000478void AsmParser::Warning(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000479 PrintMessage(L, Msg.str(), "warning");
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000480}
481
Daniel Dunbarf9507ff2009-07-27 23:20:52 +0000482bool AsmParser::Error(SMLoc L, const Twine &Msg) {
Sean Callananbf2013e2010-01-20 23:19:55 +0000483 PrintMessage(L, Msg.str(), "error");
Chris Lattner14ee48a2009-06-21 21:22:11 +0000484 return true;
485}
486
Sean Callananbf2013e2010-01-20 23:19:55 +0000487void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
488 const char *Type) const {
489 SrcMgr.PrintMessage(Loc, Msg, Type);
490}
Sean Callananfd0b0282010-01-21 00:19:58 +0000491
492bool AsmParser::EnterIncludeFile(const std::string &Filename) {
493 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
494 if (NewBuf == -1)
495 return true;
Sean Callanan79036e42010-01-20 22:18:24 +0000496
Sean Callananfd0b0282010-01-21 00:19:58 +0000497 CurBuffer = NewBuf;
498
499 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
500
501 return false;
502}
503
504const AsmToken &AsmParser::Lex() {
505 const AsmToken *tok = &Lexer.Lex();
506
507 if (tok->is(AsmToken::Eof)) {
508 // If this is the end of an included file, pop the parent file off the
509 // include stack.
510 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
511 if (ParentIncludeLoc != SMLoc()) {
512 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
513 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
514 ParentIncludeLoc.getPointer());
515 tok = &Lexer.Lex();
516 }
517 }
518
519 if (tok->is(AsmToken::Error))
Sean Callananbf2013e2010-01-20 23:19:55 +0000520 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
Sean Callanan79036e42010-01-20 22:18:24 +0000521
Sean Callananfd0b0282010-01-21 00:19:58 +0000522 return *tok;
Sean Callanan79ed1a82010-01-19 20:22:31 +0000523}
524
Chris Lattner79180e22010-04-05 23:15:42 +0000525bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000526 // Create the initial section, if requested.
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000527 //
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000528 // FIXME: Target hook & command line option for initial section.
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000529 if (!NoInitialTextSection)
Chris Lattnerf0559e42010-04-08 20:30:37 +0000530 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
Daniel Dunbar5e6a7a22010-03-13 02:20:57 +0000531 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
532 0, SectionKind::getText()));
Daniel Dunbar7c0a3342009-08-26 22:49:51 +0000533
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000534 // Prime the lexer.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000535 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000536
Chris Lattnerb717fb02009-07-02 21:53:43 +0000537 bool HadError = false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000538
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000539 AsmCond StartingCondState = TheCondState;
540
Chris Lattnerb717fb02009-07-02 21:53:43 +0000541 // While we have input, parse each statement.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000542 while (Lexer.isNot(AsmToken::Eof)) {
Chris Lattnerb717fb02009-07-02 21:53:43 +0000543 if (!ParseStatement()) continue;
544
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000545 // We had an error, remember it and recover by skipping to the next line.
Chris Lattnerb717fb02009-07-02 21:53:43 +0000546 HadError = true;
547 EatToEndOfStatement();
548 }
Kevin Enderbyc114ed72009-08-07 22:46:00 +0000549
550 if (TheCondState.TheCond != StartingCondState.TheCond ||
551 TheCondState.Ignore != StartingCondState.Ignore)
552 return TokError("unmatched .ifs or .elses");
Chris Lattnerb717fb02009-07-02 21:53:43 +0000553
Chris Lattner79180e22010-04-05 23:15:42 +0000554 // Finalize the output stream if there are no errors and if the client wants
555 // us to.
556 if (!HadError && !NoFinalize)
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000557 Out.Finish();
558
Chris Lattnerb717fb02009-07-02 21:53:43 +0000559 return HadError;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000560}
561
Chris Lattner2cf5f142009-06-22 01:29:09 +0000562/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
563void AsmParser::EatToEndOfStatement() {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000564 while (Lexer.isNot(AsmToken::EndOfStatement) &&
565 Lexer.isNot(AsmToken::Eof))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000566 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000567
568 // Eat EOL.
Daniel Dunbar3f872332009-07-28 16:08:33 +0000569 if (Lexer.is(AsmToken::EndOfStatement))
Sean Callanan79ed1a82010-01-19 20:22:31 +0000570 Lex();
Chris Lattner2cf5f142009-06-22 01:29:09 +0000571}
572
Chris Lattnerc4193832009-06-22 05:51:26 +0000573
Chris Lattner74ec1a32009-06-22 06:32:03 +0000574/// ParseParenExpr - Parse a paren expression and return it.
575/// NOTE: This assumes the leading '(' has already been consumed.
576///
577/// parenexpr ::= expr)
578///
Chris Lattnerb4307b32010-01-15 19:28:38 +0000579bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner74ec1a32009-06-22 06:32:03 +0000580 if (ParseExpression(Res)) return true;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000581 if (Lexer.isNot(AsmToken::RParen))
Chris Lattner74ec1a32009-06-22 06:32:03 +0000582 return TokError("expected ')' in parentheses expression");
Chris Lattnerb4307b32010-01-15 19:28:38 +0000583 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000584 Lex();
Chris Lattner74ec1a32009-06-22 06:32:03 +0000585 return false;
586}
Chris Lattnerc4193832009-06-22 05:51:26 +0000587
Chris Lattner74ec1a32009-06-22 06:32:03 +0000588/// ParsePrimaryExpr - Parse a primary expression and return it.
589/// primaryexpr ::= (parenexpr
590/// primaryexpr ::= symbol
591/// primaryexpr ::= number
Chris Lattnerd3050352010-04-14 04:40:28 +0000592/// primaryexpr ::= '.'
Chris Lattner74ec1a32009-06-22 06:32:03 +0000593/// primaryexpr ::= ~,+,- primaryexpr
Chris Lattnerb4307b32010-01-15 19:28:38 +0000594bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000595 switch (Lexer.getKind()) {
596 default:
597 return TokError("unknown token in expression");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000598 case AsmToken::Exclaim:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000599 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000600 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000601 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000602 Res = MCUnaryExpr::CreateLNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000603 return false;
Daniel Dunbar76c4d762009-07-31 21:55:09 +0000604 case AsmToken::String:
Daniel Dunbarfffff912009-10-16 01:34:54 +0000605 case AsmToken::Identifier: {
606 // This is a symbol reference.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000607 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000608 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first);
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000609
Daniel Dunbar525a3a62010-05-17 17:46:23 +0000610 // Mark the symbol as used in an expression.
611 Sym->setUsedInExpr(true);
612
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000613 // Lookup the symbol variant if used.
614 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
615 if (Split.first.size() != getTok().getIdentifier().size())
616 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
617
Chris Lattnerb4307b32010-01-15 19:28:38 +0000618 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000619 Lex(); // Eat identifier.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000620
621 // If this is an absolute variable reference, substitute it now to preserve
622 // semantics in the face of reassignment.
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000623 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000624 if (Variant)
625 return Error(EndLoc, "unexpected modified on variable reference");
626
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000627 Res = Sym->getVariableValue();
Daniel Dunbarfffff912009-10-16 01:34:54 +0000628 return false;
629 }
630
631 // Otherwise create a symbol ref.
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000632 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
Chris Lattnerc4193832009-06-22 05:51:26 +0000633 return false;
Daniel Dunbarfffff912009-10-16 01:34:54 +0000634 }
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000635 case AsmToken::Integer: {
636 SMLoc Loc = getTok().getLoc();
637 int64_t IntVal = getTok().getIntVal();
638 Res = MCConstantExpr::Create(IntVal, getContext());
Chris Lattnerb4307b32010-01-15 19:28:38 +0000639 EndLoc = Lexer.getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000640 Lex(); // Eat token.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000641 // Look for 'b' or 'f' following an Integer as a directional label
642 if (Lexer.getKind() == AsmToken::Identifier) {
643 StringRef IDVal = getTok().getString();
644 if (IDVal == "f" || IDVal == "b"){
645 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
646 IDVal == "f" ? 1 : 0);
647 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
648 getContext());
649 if(IDVal == "b" && Sym->isUndefined())
650 return Error(Loc, "invalid reference to undefined symbol");
651 EndLoc = Lexer.getLoc();
652 Lex(); // Eat identifier.
653 }
654 }
Chris Lattnerc4193832009-06-22 05:51:26 +0000655 return false;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000656 }
Chris Lattnerd3050352010-04-14 04:40:28 +0000657 case AsmToken::Dot: {
658 // This is a '.' reference, which references the current PC. Emit a
659 // temporary label to the streamer and refer to it.
660 MCSymbol *Sym = Ctx.CreateTempSymbol();
661 Out.EmitLabel(Sym);
662 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
663 EndLoc = Lexer.getLoc();
664 Lex(); // Eat identifier.
665 return false;
666 }
667
Daniel Dunbar3f872332009-07-28 16:08:33 +0000668 case AsmToken::LParen:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000669 Lex(); // Eat the '('.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000670 return ParseParenExpr(Res, EndLoc);
Daniel Dunbar3f872332009-07-28 16:08:33 +0000671 case AsmToken::Minus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000672 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000673 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000674 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000675 Res = MCUnaryExpr::CreateMinus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000676 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000677 case AsmToken::Plus:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000678 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000679 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000680 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000681 Res = MCUnaryExpr::CreatePlus(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000682 return false;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000683 case AsmToken::Tilde:
Sean Callanan79ed1a82010-01-19 20:22:31 +0000684 Lex(); // Eat the operator.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000685 if (ParsePrimaryExpr(Res, EndLoc))
Daniel Dunbar475839e2009-06-29 20:37:27 +0000686 return true;
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000687 Res = MCUnaryExpr::CreateNot(Res, getContext());
Daniel Dunbar475839e2009-06-29 20:37:27 +0000688 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000689 }
690}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000691
Chris Lattnerb4307b32010-01-15 19:28:38 +0000692bool AsmParser::ParseExpression(const MCExpr *&Res) {
Chris Lattner54482b42010-01-15 19:39:23 +0000693 SMLoc EndLoc;
694 return ParseExpression(Res, EndLoc);
Chris Lattnerb4307b32010-01-15 19:28:38 +0000695}
696
Chris Lattner74ec1a32009-06-22 06:32:03 +0000697/// ParseExpression - Parse an expression and return it.
698///
699/// expr ::= expr +,- expr -> lowest.
700/// expr ::= expr |,^,&,! expr -> middle.
701/// expr ::= expr *,/,%,<<,>> expr -> highest.
702/// expr ::= primaryexpr
703///
Chris Lattner54482b42010-01-15 19:39:23 +0000704bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000705 // Parse the expression.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000706 Res = 0;
Daniel Dunbare9a60eb2010-02-13 01:28:07 +0000707 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
708 return true;
709
710 // Try to constant fold it up front, if possible.
711 int64_t Value;
712 if (Res->EvaluateAsAbsolute(Value))
713 Res = MCConstantExpr::Create(Value, getContext());
714
715 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000716}
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000717
Chris Lattnerb4307b32010-01-15 19:28:38 +0000718bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
Chris Lattner75f265f2010-01-24 01:07:33 +0000719 Res = 0;
720 return ParseParenExpr(Res, EndLoc) ||
721 ParseBinOpRHS(1, Res, EndLoc);
Daniel Dunbarc18274b2009-08-31 08:08:17 +0000722}
723
Daniel Dunbar475839e2009-06-29 20:37:27 +0000724bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000725 const MCExpr *Expr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000726
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000727 SMLoc StartLoc = Lexer.getLoc();
Daniel Dunbar475839e2009-06-29 20:37:27 +0000728 if (ParseExpression(Expr))
729 return true;
730
Daniel Dunbare00b0112009-10-16 01:57:52 +0000731 if (!Expr->EvaluateAsAbsolute(Res))
Daniel Dunbarf4b830f2009-06-30 02:10:03 +0000732 return Error(StartLoc, "expected absolute expression");
Daniel Dunbar475839e2009-06-29 20:37:27 +0000733
734 return false;
735}
736
Daniel Dunbar3f872332009-07-28 16:08:33 +0000737static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000738 MCBinaryExpr::Opcode &Kind) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000739 switch (K) {
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000740 default:
741 return 0; // not a binop.
Daniel Dunbar475839e2009-06-29 20:37:27 +0000742
743 // Lowest Precedence: &&, ||
Daniel Dunbar3f872332009-07-28 16:08:33 +0000744 case AsmToken::AmpAmp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000745 Kind = MCBinaryExpr::LAnd;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000746 return 1;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000747 case AsmToken::PipePipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000748 Kind = MCBinaryExpr::LOr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000749 return 1;
750
751 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
Daniel Dunbar3f872332009-07-28 16:08:33 +0000752 case AsmToken::Plus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000753 Kind = MCBinaryExpr::Add;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000754 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000755 case AsmToken::Minus:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000756 Kind = MCBinaryExpr::Sub;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000757 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000758 case AsmToken::EqualEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000759 Kind = MCBinaryExpr::EQ;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000760 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000761 case AsmToken::ExclaimEqual:
762 case AsmToken::LessGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000763 Kind = MCBinaryExpr::NE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000764 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000765 case AsmToken::Less:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000766 Kind = MCBinaryExpr::LT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000767 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000768 case AsmToken::LessEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000769 Kind = MCBinaryExpr::LTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000770 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000771 case AsmToken::Greater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000772 Kind = MCBinaryExpr::GT;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000773 return 2;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000774 case AsmToken::GreaterEqual:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000775 Kind = MCBinaryExpr::GTE;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000776 return 2;
777
778 // Intermediate Precedence: |, &, ^
779 //
780 // FIXME: gas seems to support '!' as an infix operator?
Daniel Dunbar3f872332009-07-28 16:08:33 +0000781 case AsmToken::Pipe:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000782 Kind = MCBinaryExpr::Or;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000783 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000784 case AsmToken::Caret:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000785 Kind = MCBinaryExpr::Xor;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000786 return 3;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000787 case AsmToken::Amp:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000788 Kind = MCBinaryExpr::And;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000789 return 3;
790
791 // Highest Precedence: *, /, %, <<, >>
Daniel Dunbar3f872332009-07-28 16:08:33 +0000792 case AsmToken::Star:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000793 Kind = MCBinaryExpr::Mul;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000794 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000795 case AsmToken::Slash:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000796 Kind = MCBinaryExpr::Div;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000797 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000798 case AsmToken::Percent:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000799 Kind = MCBinaryExpr::Mod;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000800 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000801 case AsmToken::LessLess:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000802 Kind = MCBinaryExpr::Shl;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000803 return 4;
Daniel Dunbar3f872332009-07-28 16:08:33 +0000804 case AsmToken::GreaterGreater:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000805 Kind = MCBinaryExpr::Shr;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000806 return 4;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000807 }
808}
809
810
811/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
812/// Res contains the LHS of the expression on input.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000813bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
814 SMLoc &EndLoc) {
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000815 while (1) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000816 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000817 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000818
819 // If the next token is lower precedence than we are allowed to eat, return
820 // successfully with what we ate already.
821 if (TokPrec < Precedence)
822 return false;
823
Sean Callanan79ed1a82010-01-19 20:22:31 +0000824 Lex();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000825
826 // Eat the next primary expression.
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000827 const MCExpr *RHS;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000828 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000829
830 // If BinOp binds less tightly with RHS than the operator after RHS, let
831 // the pending operator take RHS as its LHS.
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000832 MCBinaryExpr::Opcode Dummy;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000833 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000834 if (TokPrec < NextTokPrec) {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000835 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000836 }
837
Daniel Dunbar475839e2009-06-29 20:37:27 +0000838 // Merge LHS and RHS according to operator.
Daniel Dunbar6ce004d2009-08-31 08:07:44 +0000839 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000840 }
841}
842
Chris Lattnerc4193832009-06-22 05:51:26 +0000843
844
845
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000846/// ParseStatement:
847/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000848/// ::= Label* Directive ...Operands... EndOfStatement
849/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000850bool AsmParser::ParseStatement() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000851 if (Lexer.is(AsmToken::EndOfStatement)) {
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000852 Out.AddBlankLine();
Sean Callanan79ed1a82010-01-19 20:22:31 +0000853 Lex();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000854 return false;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000855 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000856
857 // Statements always start with an identifier.
Sean Callanan18b83232010-01-19 21:44:56 +0000858 AsmToken ID = getTok();
Daniel Dunbar419aded2009-07-28 16:38:40 +0000859 SMLoc IDLoc = ID.getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000860 StringRef IDVal;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000861 int64_t LocalLabelVal = -1;
862 // GUESS allow an integer followed by a ':' as a directional local label
863 if (Lexer.is(AsmToken::Integer)) {
864 LocalLabelVal = getTok().getIntVal();
865 if (LocalLabelVal < 0) {
866 if (!TheCondState.Ignore)
867 return TokError("unexpected token at start of statement");
868 IDVal = "";
869 }
870 else {
871 IDVal = getTok().getString();
872 Lex(); // Consume the integer token to be used as an identifier token.
873 if (Lexer.getKind() != AsmToken::Colon) {
Duncan Sands34727662010-07-12 08:16:59 +0000874 if (!TheCondState.Ignore)
875 return TokError("unexpected token at start of statement");
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000876 }
877 }
878 }
879 else if (ParseIdentifier(IDVal)) {
Chris Lattner7834fac2010-04-17 18:14:27 +0000880 if (!TheCondState.Ignore)
881 return TokError("unexpected token at start of statement");
882 IDVal = "";
883 }
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000884
Chris Lattner7834fac2010-04-17 18:14:27 +0000885 // Handle conditional assembly here before checking for skipping. We
886 // have to do this so that .endif isn't skipped in a ".if 0" block for
887 // example.
888 if (IDVal == ".if")
889 return ParseDirectiveIf(IDLoc);
890 if (IDVal == ".elseif")
891 return ParseDirectiveElseIf(IDLoc);
892 if (IDVal == ".else")
893 return ParseDirectiveElse(IDLoc);
894 if (IDVal == ".endif")
895 return ParseDirectiveEndIf(IDLoc);
896
897 // If we are in a ".if 0" block, ignore this statement.
898 if (TheCondState.Ignore) {
899 EatToEndOfStatement();
900 return false;
901 }
902
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +0000903 // FIXME: Recurse on local labels?
904
905 // See what kind of statement we have.
906 switch (Lexer.getKind()) {
Daniel Dunbar3f872332009-07-28 16:08:33 +0000907 case AsmToken::Colon: {
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000908 // identifier ':' -> Label.
Sean Callanan79ed1a82010-01-19 20:22:31 +0000909 Lex();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000910
911 // Diagnose attempt to use a variable as a label.
912 //
913 // FIXME: Diagnostics. Note the location of the definition as a label.
914 // FIXME: This doesn't diagnose assignment to a symbol which has been
915 // implicitly marked as external.
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000916 MCSymbol *Sym;
917 if (LocalLabelVal == -1)
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +0000918 Sym = getContext().GetOrCreateSymbol(IDVal);
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +0000919 else
920 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
Daniel Dunbarc3047182010-05-05 19:01:00 +0000921 if (!Sym->isUndefined() || Sym->isVariable())
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000922 return Error(IDLoc, "invalid symbol redefinition");
Chris Lattnerc69485e2009-06-24 04:31:49 +0000923
Daniel Dunbar959fd882009-08-26 22:13:22 +0000924 // Emit the label.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000925 Out.EmitLabel(Sym);
926
Daniel Dunbar01777ff2010-05-23 18:36:34 +0000927 // Consume any end of statement token, if present, to avoid spurious
928 // AddBlankLine calls().
929 if (Lexer.is(AsmToken::EndOfStatement)) {
930 Lex();
931 if (Lexer.is(AsmToken::Eof))
932 return false;
933 }
934
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000935 return ParseStatement();
Daniel Dunbardce0f3c2009-06-29 23:43:14 +0000936 }
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000937
Daniel Dunbar3f872332009-07-28 16:08:33 +0000938 case AsmToken::Equal:
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000939 // identifier '=' ... -> assignment statement
Sean Callanan79ed1a82010-01-19 20:22:31 +0000940 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000941
Daniel Dunbare2ace502009-08-31 08:09:09 +0000942 return ParseAssignment(IDVal);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000943
944 default: // Normal instruction or directive.
945 break;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000946 }
947
948 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000949 if (IDVal[0] == '.') {
Chris Lattner529fb542009-06-24 05:13:15 +0000950 // FIXME: This should be driven based on a hash lookup and callback.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000951 if (IDVal == ".section")
Chris Lattner529fb542009-06-24 05:13:15 +0000952 return ParseDirectiveDarwinSection();
Daniel Dunbarb3f3c032009-08-21 08:34:18 +0000953
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000954 // Assembler features
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000955 if (IDVal == ".set")
Daniel Dunbar8f780cd2009-06-25 21:56:11 +0000956 return ParseDirectiveSet();
957
Daniel Dunbara0d14262009-06-24 23:30:00 +0000958 // Data directives
959
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000960 if (IDVal == ".ascii")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000961 return ParseDirectiveAscii(false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000962 if (IDVal == ".asciz")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000963 return ParseDirectiveAscii(true);
964
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000965 if (IDVal == ".byte")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000966 return ParseDirectiveValue(1);
Kevin Enderby9c656452009-09-10 20:51:44 +0000967 if (IDVal == ".short")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000968 return ParseDirectiveValue(2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000969 if (IDVal == ".long")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000970 return ParseDirectiveValue(4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000971 if (IDVal == ".quad")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000972 return ParseDirectiveValue(8);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000973
974 // FIXME: Target hooks for IsPow2.
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000975 if (IDVal == ".align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000976 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000977 if (IDVal == ".align32")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000978 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000979 if (IDVal == ".balign")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000980 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000981 if (IDVal == ".balignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000982 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000983 if (IDVal == ".balignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000984 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000985 if (IDVal == ".p2align")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000986 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000987 if (IDVal == ".p2alignw")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000988 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000989 if (IDVal == ".p2alignl")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000990 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
991
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000992 if (IDVal == ".org")
Daniel Dunbarc238b582009-06-25 22:44:51 +0000993 return ParseDirectiveOrg();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000994
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000995 if (IDVal == ".fill")
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000996 return ParseDirectiveFill();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000997 if (IDVal == ".space")
Daniel Dunbara0d14262009-06-24 23:30:00 +0000998 return ParseDirectiveSpace();
999
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001000 // Symbol attribute directives
Daniel Dunbard0c14d62009-08-11 04:24:50 +00001001
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001002 if (IDVal == ".globl" || IDVal == ".global")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001003 return ParseDirectiveSymbolAttribute(MCSA_Global);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001004 if (IDVal == ".hidden")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001005 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001006 if (IDVal == ".indirect_symbol")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001007 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001008 if (IDVal == ".internal")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001009 return ParseDirectiveSymbolAttribute(MCSA_Internal);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001010 if (IDVal == ".lazy_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001011 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001012 if (IDVal == ".no_dead_strip")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001013 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001014 if (IDVal == ".private_extern")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001015 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001016 if (IDVal == ".protected")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001017 return ParseDirectiveSymbolAttribute(MCSA_Protected);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001018 if (IDVal == ".reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001019 return ParseDirectiveSymbolAttribute(MCSA_Reference);
Matt Fleming924c5e52010-05-21 11:36:59 +00001020 if (IDVal == ".type")
1021 return ParseDirectiveELFType();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001022 if (IDVal == ".weak")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001023 return ParseDirectiveSymbolAttribute(MCSA_Weak);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001024 if (IDVal == ".weak_definition")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001025 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001026 if (IDVal == ".weak_reference")
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001027 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
Kevin Enderbyf59cac52010-07-08 17:22:42 +00001028 if (IDVal == ".weak_def_can_be_hidden")
1029 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001030
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001031 if (IDVal == ".comm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001032 return ParseDirectiveComm(/*IsLocal=*/false);
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001033 if (IDVal == ".lcomm")
Chris Lattner1fc3d752009-07-09 17:25:12 +00001034 return ParseDirectiveComm(/*IsLocal=*/true);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001035
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001036 if (IDVal == ".abort")
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001037 return ParseDirectiveAbort();
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001038 if (IDVal == ".include")
Kevin Enderby1f049b22009-07-14 23:21:55 +00001039 return ParseDirectiveInclude();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001040
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00001041 // Look up the handler in the handler table.
1042 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler =
1043 DirectiveMap.lookup(IDVal);
1044 if (Handler.first)
1045 return (Handler.first->*Handler.second)(IDVal, IDLoc);
1046
Kevin Enderby9c656452009-09-10 20:51:44 +00001047 // Target hook for parsing target specific directives.
1048 if (!getTargetParser().ParseDirective(ID))
1049 return false;
1050
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001051 Warning(IDLoc, "ignoring directive for now");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001052 EatToEndOfStatement();
1053 return false;
1054 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +00001055
Chris Lattnera7f13542010-05-19 23:34:33 +00001056 // Canonicalize the opcode to lower case.
1057 SmallString<128> Opcode;
1058 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
1059 Opcode.push_back(tolower(IDVal[i]));
1060
Chris Lattner98986712010-01-14 22:21:20 +00001061 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
Chris Lattnera7f13542010-05-19 23:34:33 +00001062 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001063 ParsedOperands);
1064 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
1065 HadError = TokError("unexpected token in argument list");
Chris Lattner2cf5f142009-06-22 01:29:09 +00001066
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001067 // If parsing succeeded, match the instruction.
1068 if (!HadError) {
1069 MCInst Inst;
1070 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
1071 // Emit the instruction on success.
1072 Out.EmitInstruction(Inst);
1073 } else {
1074 // Otherwise emit a diagnostic about the match failure and set the error
1075 // flag.
1076 //
1077 // FIXME: We should give nicer diagnostics about the exact failure.
1078 Error(IDLoc, "unrecognized instruction");
1079 HadError = true;
1080 }
1081 }
Chris Lattner98986712010-01-14 22:21:20 +00001082
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001083 // If there was no error, consume the end-of-statement token. Otherwise this
1084 // will be done by our caller.
1085 if (!HadError)
1086 Lex();
Chris Lattner98986712010-01-14 22:21:20 +00001087
1088 // Free any parsed operands.
1089 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
1090 delete ParsedOperands[i];
1091
Daniel Dunbar31e8e1d2010-05-04 00:33:07 +00001092 return HadError;
Chris Lattner27aa7d22009-06-21 20:16:42 +00001093}
Chris Lattner9a023f72009-06-24 04:43:34 +00001094
Daniel Dunbare2ace502009-08-31 08:09:09 +00001095bool AsmParser::ParseAssignment(const StringRef &Name) {
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001096 // FIXME: Use better location, we should use proper tokens.
1097 SMLoc EqualLoc = Lexer.getLoc();
1098
Daniel Dunbar821e3332009-08-31 08:09:28 +00001099 const MCExpr *Value;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001100 if (ParseExpression(Value))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001101 return true;
1102
Daniel Dunbar3f872332009-07-28 16:08:33 +00001103 if (Lexer.isNot(AsmToken::EndOfStatement))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001104 return TokError("unexpected token in assignment");
1105
1106 // Eat the end of statement marker.
Sean Callanan79ed1a82010-01-19 20:22:31 +00001107 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001108
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001109 // Validate that the LHS is allowed to be a variable (either it has not been
1110 // used as a symbol, or it is an absolute symbol).
1111 MCSymbol *Sym = getContext().LookupSymbol(Name);
1112 if (Sym) {
1113 // Diagnose assignment to a label.
1114 //
1115 // FIXME: Diagnostics. Note the location of the definition as a label.
1116 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001117 if (Sym->isUndefined() && !Sym->isUsedInExpr())
1118 ; // Allow redefinitions of undefined symbols only used in directives.
1119 else if (!Sym->isUndefined() && !Sym->isAbsolute())
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001120 return Error(EqualLoc, "redefinition of '" + Name + "'");
1121 else if (!Sym->isVariable())
1122 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
Daniel Dunbar08a408a2010-05-05 17:41:00 +00001123 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001124 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
1125 Name + "'");
1126 } else
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001127 Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbar75773ff2009-10-16 01:57:39 +00001128
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001129 // FIXME: Handle '.'.
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001130
Daniel Dunbar525a3a62010-05-17 17:46:23 +00001131 Sym->setUsedInExpr(true);
1132
Daniel Dunbardce0f3c2009-06-29 23:43:14 +00001133 // Do the assignment.
Daniel Dunbare2ace502009-08-31 08:09:09 +00001134 Out.EmitAssignment(Sym, Value);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001135
1136 return false;
1137}
1138
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001139/// ParseIdentifier:
1140/// ::= identifier
1141/// ::= string
1142bool AsmParser::ParseIdentifier(StringRef &Res) {
1143 if (Lexer.isNot(AsmToken::Identifier) &&
1144 Lexer.isNot(AsmToken::String))
1145 return true;
1146
Sean Callanan18b83232010-01-19 21:44:56 +00001147 Res = getTok().getIdentifier();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001148
Sean Callanan79ed1a82010-01-19 20:22:31 +00001149 Lex(); // Consume the identifier token.
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001150
1151 return false;
1152}
1153
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001154/// ParseDirectiveSet:
1155/// ::= .set identifier ',' expression
1156bool AsmParser::ParseDirectiveSet() {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001157 StringRef Name;
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001158
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001159 if (ParseIdentifier(Name))
1160 return TokError("expected identifier after '.set' directive");
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001161
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001162 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001163 return TokError("unexpected token in '.set'");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001164 Lex();
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001165
Daniel Dunbare2ace502009-08-31 08:09:09 +00001166 return ParseAssignment(Name);
Daniel Dunbar8f780cd2009-06-25 21:56:11 +00001167}
1168
Chris Lattner9a023f72009-06-24 04:43:34 +00001169/// ParseDirectiveSection:
Chris Lattner529fb542009-06-24 05:13:15 +00001170/// ::= .section identifier (',' identifier)*
1171/// FIXME: This should actually parse out the segment, section, attributes and
1172/// sizeof_stub fields.
1173bool AsmParser::ParseDirectiveDarwinSection() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001174 SMLoc Loc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001175
Daniel Dunbarace63122009-08-11 03:42:33 +00001176 StringRef SectionName;
1177 if (ParseIdentifier(SectionName))
1178 return Error(Loc, "expected identifier after '.section' directive");
1179
1180 // Verify there is a following comma.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001181 if (!getLexer().is(AsmToken::Comma))
Daniel Dunbarace63122009-08-11 03:42:33 +00001182 return TokError("unexpected token in '.section' directive");
1183
Chris Lattnerff4bc462009-08-10 01:39:42 +00001184 std::string SectionSpec = SectionName;
Daniel Dunbarace63122009-08-11 03:42:33 +00001185 SectionSpec += ",";
1186
1187 // Add all the tokens until the end of the line, ParseSectionSpecifier will
1188 // handle this.
Chris Lattnerff4bc462009-08-10 01:39:42 +00001189 StringRef EOL = Lexer.LexUntilEndOfStatement();
1190 SectionSpec.append(EOL.begin(), EOL.end());
Daniel Dunbarace63122009-08-11 03:42:33 +00001191
Sean Callanan79ed1a82010-01-19 20:22:31 +00001192 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001193 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9a023f72009-06-24 04:43:34 +00001194 return TokError("unexpected token in '.section' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001195 Lex();
Chris Lattner9a023f72009-06-24 04:43:34 +00001196
Chris Lattnerff4bc462009-08-10 01:39:42 +00001197
1198 StringRef Segment, Section;
1199 unsigned TAA, StubSize;
1200 std::string ErrorStr =
1201 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
1202 TAA, StubSize);
1203
1204 if (!ErrorStr.empty())
Daniel Dunbarace63122009-08-11 03:42:33 +00001205 return Error(Loc, ErrorStr.c_str());
Chris Lattnerff4bc462009-08-10 01:39:42 +00001206
Chris Lattner56594f92009-07-31 17:47:16 +00001207 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001208 bool isText = Segment == "__TEXT"; // FIXME: Hack.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001209 getStreamer().SwitchSection(Ctx.getMachOSection(
1210 Segment, Section, TAA, StubSize,
1211 isText ? SectionKind::getText()
1212 : SectionKind::getDataRel()));
Chris Lattner9a023f72009-06-24 04:43:34 +00001213 return false;
1214}
1215
Daniel Dunbar47f56082010-07-12 20:23:36 +00001216bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
1217 const char *Section,
1218 unsigned TAA, unsigned Align,
1219 unsigned StubSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001220 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner529fb542009-06-24 05:13:15 +00001221 return TokError("unexpected token in section switching directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001222 Lex();
Daniel Dunbar47f56082010-07-12 20:23:36 +00001223
Chris Lattner56594f92009-07-31 17:47:16 +00001224 // FIXME: Arch specific.
Chris Lattnerf60e9bb2010-02-26 18:32:26 +00001225 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
Daniel Dunbar47f56082010-07-12 20:23:36 +00001226 getStreamer().SwitchSection(getContext().getMachOSection(
1227 Segment, Section, TAA, StubSize,
1228 isText ? SectionKind::getText()
1229 : SectionKind::getDataRel()));
Daniel Dunbar2330df62009-08-21 23:30:15 +00001230
1231 // Set the implicit alignment, if any.
1232 //
1233 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
1234 // alignment on the section (e.g., if one manually inserts bytes into the
1235 // section, then just issueing the section switch directive will not realign
1236 // the section. However, this is arguably more reasonable behavior, and there
1237 // is no good reason for someone to intentionally emit incorrectly sized
1238 // values into the implicitly aligned sections.
1239 if (Align)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001240 getStreamer().EmitValueToAlignment(Align, 0, 1, 0);
Daniel Dunbar2330df62009-08-21 23:30:15 +00001241
Chris Lattner529fb542009-06-24 05:13:15 +00001242 return false;
1243}
Daniel Dunbara0d14262009-06-24 23:30:00 +00001244
Daniel Dunbar7a56fc22010-07-12 20:08:04 +00001245bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
1246 unsigned Flags, SectionKind Kind) {
1247 if (getLexer().isNot(AsmToken::EndOfStatement))
1248 return TokError("unexpected token in section switching directive");
1249 Lex();
1250
1251 getStreamer().SwitchSection(getContext().getELFSection(
1252 Section, Type, Flags, Kind));
1253
1254 return false;
1255}
1256
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001257bool AsmParser::ParseEscapedString(std::string &Data) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001258 assert(getLexer().is(AsmToken::String) && "Unexpected current token!");
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001259
1260 Data = "";
Sean Callanan18b83232010-01-19 21:44:56 +00001261 StringRef Str = getTok().getStringContents();
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001262 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1263 if (Str[i] != '\\') {
1264 Data += Str[i];
1265 continue;
1266 }
1267
1268 // Recognize escaped characters. Note that this escape semantics currently
1269 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1270 ++i;
1271 if (i == e)
1272 return TokError("unexpected backslash at end of string");
1273
1274 // Recognize octal sequences.
1275 if ((unsigned) (Str[i] - '0') <= 7) {
1276 // Consume up to three octal characters.
1277 unsigned Value = Str[i] - '0';
1278
1279 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1280 ++i;
1281 Value = Value * 8 + (Str[i] - '0');
1282
1283 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1284 ++i;
1285 Value = Value * 8 + (Str[i] - '0');
1286 }
1287 }
1288
1289 if (Value > 255)
1290 return TokError("invalid octal escape sequence (out of range)");
1291
1292 Data += (unsigned char) Value;
1293 continue;
1294 }
1295
1296 // Otherwise recognize individual escapes.
1297 switch (Str[i]) {
1298 default:
1299 // Just reject invalid escape sequences for now.
1300 return TokError("invalid escape sequence (unrecognized character)");
1301
1302 case 'b': Data += '\b'; break;
1303 case 'f': Data += '\f'; break;
1304 case 'n': Data += '\n'; break;
1305 case 'r': Data += '\r'; break;
1306 case 't': Data += '\t'; break;
1307 case '"': Data += '"'; break;
1308 case '\\': Data += '\\'; break;
1309 }
1310 }
1311
1312 return false;
1313}
1314
Daniel Dunbara0d14262009-06-24 23:30:00 +00001315/// ParseDirectiveAscii:
Daniel Dunbar475839e2009-06-29 20:37:27 +00001316/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
Daniel Dunbara0d14262009-06-24 23:30:00 +00001317bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001318 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001319 for (;;) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001320 if (getLexer().isNot(AsmToken::String))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001321 return TokError("expected string in '.ascii' or '.asciz' directive");
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001322
Daniel Dunbar1ab75942009-08-14 18:19:52 +00001323 std::string Data;
1324 if (ParseEscapedString(Data))
1325 return true;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001326
1327 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001328 if (ZeroTerminated)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001329 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1330
Sean Callanan79ed1a82010-01-19 20:22:31 +00001331 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001332
1333 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001334 break;
1335
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001336 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001337 return TokError("unexpected token in '.ascii' or '.asciz' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001338 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001339 }
1340 }
1341
Sean Callanan79ed1a82010-01-19 20:22:31 +00001342 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001343 return false;
1344}
1345
1346/// ParseDirectiveValue
1347/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1348bool AsmParser::ParseDirectiveValue(unsigned Size) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001349 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbara0d14262009-06-24 23:30:00 +00001350 for (;;) {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001351 const MCExpr *Value;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001352 SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
Daniel Dunbar821e3332009-08-31 08:09:28 +00001353 if (ParseExpression(Value))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001354 return true;
1355
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001356 // Special case constant expressions to match code generator.
1357 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001358 getStreamer().EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
Daniel Dunbar414c0c42010-05-23 18:36:38 +00001359 else
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001360 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001361
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001362 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001363 break;
1364
1365 // FIXME: Improve diagnostic.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001366 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001367 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001368 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001369 }
1370 }
1371
Sean Callanan79ed1a82010-01-19 20:22:31 +00001372 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001373 return false;
1374}
1375
1376/// ParseDirectiveSpace
1377/// ::= .space expression [ , expression ]
1378bool AsmParser::ParseDirectiveSpace() {
1379 int64_t NumBytes;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001380 if (ParseAbsoluteExpression(NumBytes))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001381 return true;
1382
1383 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001384 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1385 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001386 return TokError("unexpected token in '.space' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001387 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001388
Daniel Dunbar475839e2009-06-29 20:37:27 +00001389 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001390 return true;
1391
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001392 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001393 return TokError("unexpected token in '.space' directive");
1394 }
1395
Sean Callanan79ed1a82010-01-19 20:22:31 +00001396 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001397
1398 if (NumBytes <= 0)
1399 return TokError("invalid number of bytes in '.space' directive");
1400
1401 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001402 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001403
1404 return false;
1405}
1406
1407/// ParseDirectiveFill
1408/// ::= .fill expression , expression , expression
1409bool AsmParser::ParseDirectiveFill() {
1410 int64_t NumValues;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001411 if (ParseAbsoluteExpression(NumValues))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001412 return true;
1413
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001414 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001415 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001416 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001417
1418 int64_t FillSize;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001419 if (ParseAbsoluteExpression(FillSize))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001420 return true;
1421
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001422 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001423 return TokError("unexpected token in '.fill' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001424 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001425
1426 int64_t FillExpr;
Daniel Dunbar475839e2009-06-29 20:37:27 +00001427 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001428 return true;
1429
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001430 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbara0d14262009-06-24 23:30:00 +00001431 return TokError("unexpected token in '.fill' directive");
1432
Sean Callanan79ed1a82010-01-19 20:22:31 +00001433 Lex();
Daniel Dunbara0d14262009-06-24 23:30:00 +00001434
Daniel Dunbarbc38ca72009-08-21 15:43:35 +00001435 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1436 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
Daniel Dunbara0d14262009-06-24 23:30:00 +00001437
1438 for (uint64_t i = 0, e = NumValues; i != e; ++i)
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001439 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
Daniel Dunbara0d14262009-06-24 23:30:00 +00001440
1441 return false;
1442}
Daniel Dunbarc238b582009-06-25 22:44:51 +00001443
1444/// ParseDirectiveOrg
1445/// ::= .org expression [ , expression ]
1446bool AsmParser::ParseDirectiveOrg() {
Daniel Dunbar821e3332009-08-31 08:09:28 +00001447 const MCExpr *Offset;
Daniel Dunbar821e3332009-08-31 08:09:28 +00001448 if (ParseExpression(Offset))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001449 return true;
1450
1451 // Parse optional fill expression.
1452 int64_t FillExpr = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001453 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1454 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001455 return TokError("unexpected token in '.org' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001456 Lex();
Daniel Dunbarc238b582009-06-25 22:44:51 +00001457
Daniel Dunbar475839e2009-06-29 20:37:27 +00001458 if (ParseAbsoluteExpression(FillExpr))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001459 return true;
1460
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001461 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc238b582009-06-25 22:44:51 +00001462 return TokError("unexpected token in '.org' directive");
1463 }
1464
Sean Callanan79ed1a82010-01-19 20:22:31 +00001465 Lex();
Daniel Dunbarf4b830f2009-06-30 02:10:03 +00001466
1467 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1468 // has to be relative to the current section.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001469 getStreamer().EmitValueToOffset(Offset, FillExpr);
Daniel Dunbarc238b582009-06-25 22:44:51 +00001470
1471 return false;
1472}
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001473
1474/// ParseDirectiveAlign
1475/// ::= {.align, ...} expression [ , expression [ , expression ]]
1476bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001477 SMLoc AlignmentLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001478 int64_t Alignment;
1479 if (ParseAbsoluteExpression(Alignment))
1480 return true;
1481
1482 SMLoc MaxBytesLoc;
1483 bool HasFillExpr = false;
1484 int64_t FillExpr = 0;
1485 int64_t MaxBytesToFill = 0;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001486 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1487 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001488 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001489 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001490
1491 // The fill expression can be omitted while specifying a maximum number of
1492 // alignment bytes, e.g:
1493 // .align 3,,4
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001494 if (getLexer().isNot(AsmToken::Comma)) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001495 HasFillExpr = true;
1496 if (ParseAbsoluteExpression(FillExpr))
1497 return true;
1498 }
1499
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001500 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1501 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001502 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001503 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001504
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001505 MaxBytesLoc = getLexer().getLoc();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001506 if (ParseAbsoluteExpression(MaxBytesToFill))
1507 return true;
1508
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001509 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001510 return TokError("unexpected token in directive");
1511 }
1512 }
1513
Sean Callanan79ed1a82010-01-19 20:22:31 +00001514 Lex();
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001515
Daniel Dunbar648ac512010-05-17 21:54:30 +00001516 if (!HasFillExpr)
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001517 FillExpr = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001518
1519 // Compute alignment in bytes.
1520 if (IsPow2) {
1521 // FIXME: Diagnose overflow.
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001522 if (Alignment >= 32) {
1523 Error(AlignmentLoc, "invalid alignment value");
1524 Alignment = 31;
1525 }
1526
Benjamin Kramer12fd7672009-09-06 09:35:10 +00001527 Alignment = 1ULL << Alignment;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001528 }
1529
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001530 // Diagnose non-sensical max bytes to align.
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001531 if (MaxBytesLoc.isValid()) {
1532 if (MaxBytesToFill < 1) {
Daniel Dunbarb58a8042009-08-26 09:16:34 +00001533 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1534 "many bytes, ignoring maximum bytes expression");
Daniel Dunbar0afb9f52009-08-21 23:01:53 +00001535 MaxBytesToFill = 0;
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001536 }
1537
1538 if (MaxBytesToFill >= Alignment) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +00001539 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1540 "has no effect");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001541 MaxBytesToFill = 0;
1542 }
1543 }
1544
Daniel Dunbar648ac512010-05-17 21:54:30 +00001545 // Check whether we should use optimal code alignment for this .align
1546 // directive.
1547 //
1548 // FIXME: This should be using a target hook.
1549 bool UseCodeAlign = false;
1550 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001551 getStreamer().getCurrentSection()))
Daniel Dunbar648ac512010-05-17 21:54:30 +00001552 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1553 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1554 ValueSize == 1 && UseCodeAlign) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001555 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001556 } else {
Kevin Enderbyd74acb02010-02-25 18:46:04 +00001557 // FIXME: Target specific behavior about how the "extra" bytes are filled.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001558 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
Daniel Dunbar648ac512010-05-17 21:54:30 +00001559 }
Daniel Dunbarc29dfa72009-06-29 23:46:59 +00001560
1561 return false;
1562}
1563
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001564/// ParseDirectiveSymbolAttribute
1565/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001566bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001567 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001568 for (;;) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001569 StringRef Name;
1570
1571 if (ParseIdentifier(Name))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001572 return TokError("expected identifier in directive");
1573
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001574 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001575
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001576 getStreamer().EmitSymbolAttribute(Sym, Attr);
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001577
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001578 if (getLexer().is(AsmToken::EndOfStatement))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001579 break;
1580
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001581 if (getLexer().isNot(AsmToken::Comma))
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001582 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001583 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001584 }
1585 }
1586
Sean Callanan79ed1a82010-01-19 20:22:31 +00001587 Lex();
Daniel Dunbard7b267b2009-06-30 00:33:19 +00001588 return false;
1589}
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001590
Matt Fleming924c5e52010-05-21 11:36:59 +00001591/// ParseDirectiveELFType
1592/// ::= .type identifier , @attribute
1593bool AsmParser::ParseDirectiveELFType() {
1594 StringRef Name;
1595 if (ParseIdentifier(Name))
1596 return TokError("expected identifier in directive");
1597
1598 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001599 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Matt Fleming924c5e52010-05-21 11:36:59 +00001600
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001601 if (getLexer().isNot(AsmToken::Comma))
Matt Fleming924c5e52010-05-21 11:36:59 +00001602 return TokError("unexpected token in '.type' directive");
1603 Lex();
1604
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001605 if (getLexer().isNot(AsmToken::At))
Matt Fleming924c5e52010-05-21 11:36:59 +00001606 return TokError("expected '@' before type");
1607 Lex();
1608
1609 StringRef Type;
1610 SMLoc TypeLoc;
1611
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001612 TypeLoc = getLexer().getLoc();
Matt Fleming924c5e52010-05-21 11:36:59 +00001613 if (ParseIdentifier(Type))
1614 return TokError("expected symbol type in directive");
1615
1616 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1617 .Case("function", MCSA_ELF_TypeFunction)
1618 .Case("object", MCSA_ELF_TypeObject)
1619 .Case("tls_object", MCSA_ELF_TypeTLS)
1620 .Case("common", MCSA_ELF_TypeCommon)
1621 .Case("notype", MCSA_ELF_TypeNoType)
1622 .Default(MCSA_Invalid);
1623
1624 if (Attr == MCSA_Invalid)
1625 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1626
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001627 if (getLexer().isNot(AsmToken::EndOfStatement))
Matt Fleming924c5e52010-05-21 11:36:59 +00001628 return TokError("unexpected token in '.type' directive");
1629
1630 Lex();
1631
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001632 getStreamer().EmitSymbolAttribute(Sym, Attr);
Matt Fleming924c5e52010-05-21 11:36:59 +00001633
1634 return false;
1635}
1636
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001637/// ParseDirectiveDesc
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001638/// ::= .desc identifier , expression
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001639bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001640 StringRef Name;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001641 if (getParser().ParseIdentifier(Name))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001642 return TokError("expected identifier in directive");
1643
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001644 // Handle the identifier as the key symbol.
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001645 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001646
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001647 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001648 return TokError("unexpected token in '.desc' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001649 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001650
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001651 int64_t DescValue;
Daniel Dunbar492b7a22010-07-12 19:22:53 +00001652 if (getParser().ParseAbsoluteExpression(DescValue))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001653 return true;
1654
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001655 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001656 return TokError("unexpected token in '.desc' directive");
1657
Sean Callanan79ed1a82010-01-19 20:22:31 +00001658 Lex();
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001659
1660 // Set the n_desc field of this Symbol to this DescValue
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001661 getStreamer().EmitSymbolDesc(Sym, DescValue);
Kevin Enderby95cf30c2009-07-14 18:17:10 +00001662
1663 return false;
1664}
1665
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001666/// ParseDirectiveComm
Chris Lattner1fc3d752009-07-09 17:25:12 +00001667/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1668bool AsmParser::ParseDirectiveComm(bool IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001669 SMLoc IDLoc = getLexer().getLoc();
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001670 StringRef Name;
1671 if (ParseIdentifier(Name))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001672 return TokError("expected identifier in directive");
1673
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001674 // Handle the identifier as the key symbol.
Daniel Dunbar4c7c08b2010-07-12 19:52:10 +00001675 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001676
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001677 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001678 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001679 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001680
1681 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001682 SMLoc SizeLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001683 if (ParseAbsoluteExpression(Size))
1684 return true;
1685
1686 int64_t Pow2Alignment = 0;
1687 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001688 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001689 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001690 Pow2AlignmentLoc = getLexer().getLoc();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001691 if (ParseAbsoluteExpression(Pow2Alignment))
1692 return true;
Chris Lattner258281d2010-01-19 06:22:22 +00001693
1694 // If this target takes alignments in bytes (not log) validate and convert.
1695 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1696 if (!isPowerOf2_64(Pow2Alignment))
1697 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1698 Pow2Alignment = Log2_64(Pow2Alignment);
1699 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001700 }
1701
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001702 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner1fc3d752009-07-09 17:25:12 +00001703 return TokError("unexpected token in '.comm' or '.lcomm' directive");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001704
Sean Callanan79ed1a82010-01-19 20:22:31 +00001705 Lex();
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001706
Chris Lattner1fc3d752009-07-09 17:25:12 +00001707 // NOTE: a size of zero for a .comm should create a undefined symbol
1708 // but a size of .lcomm creates a bss symbol of size zero.
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001709 if (Size < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001710 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1711 "be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001712
Eric Christopherc260a3e2010-05-14 01:38:54 +00001713 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001714 // may internally end up wanting an alignment in bytes.
1715 // FIXME: Diagnose overflow.
1716 if (Pow2Alignment < 0)
Chris Lattner1fc3d752009-07-09 17:25:12 +00001717 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1718 "alignment, can't be less than zero");
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001719
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001720 if (!Sym->isUndefined())
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001721 return Error(IDLoc, "invalid symbol redefinition");
1722
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001723 // '.lcomm' is equivalent to '.zerofill'.
Chris Lattner1fc3d752009-07-09 17:25:12 +00001724 // Create the Symbol as a common or local common with Size and Pow2Alignment
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001725 if (IsLocal) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001726 getStreamer().EmitZerofill(Ctx.getMachOSection(
1727 "__DATA", "__bss", MCSectionMachO::S_ZEROFILL,
1728 0, SectionKind::getBSS()),
1729 Sym, Size, 1 << Pow2Alignment);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001730 return false;
1731 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001732
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001733 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +00001734 return false;
1735}
Chris Lattner9be3fee2009-07-10 22:20:30 +00001736
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001737/// ParseDirectiveZerofill
Chris Lattner9be3fee2009-07-10 22:20:30 +00001738/// ::= .zerofill segname , sectname [, identifier , size_expression [
1739/// , align_expression ]]
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001740bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
Chris Lattner7bb7c552010-05-13 00:10:34 +00001741 StringRef Segment;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001742 if (getParser().ParseIdentifier(Segment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001743 return TokError("expected segment name after '.zerofill' directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001744
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001745 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001746 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001747 Lex();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001748
1749 StringRef Section;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001750 if (getParser().ParseIdentifier(Section))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001751 return TokError("expected section name after comma in '.zerofill' "
1752 "directive");
Chris Lattner9be3fee2009-07-10 22:20:30 +00001753
Chris Lattner9be3fee2009-07-10 22:20:30 +00001754 // If this is the end of the line all that was wanted was to create the
1755 // the section but with no symbol.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001756 if (getLexer().is(AsmToken::EndOfStatement)) {
Chris Lattner9be3fee2009-07-10 22:20:30 +00001757 // Create the zerofill section but no symbol
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001758 getStreamer().EmitZerofill(getContext().getMachOSection(
1759 Segment, Section, MCSectionMachO::S_ZEROFILL,
1760 0, SectionKind::getBSS()));
Chris Lattner9be3fee2009-07-10 22:20:30 +00001761 return false;
1762 }
1763
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001764 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001765 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001766 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001767
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001768 SMLoc IDLoc = getLexer().getLoc();
Chris Lattner7bb7c552010-05-13 00:10:34 +00001769 StringRef IDStr;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001770 if (getParser().ParseIdentifier(IDStr))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001771 return TokError("expected identifier in directive");
1772
1773 // handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001774 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001775
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001776 if (getLexer().isNot(AsmToken::Comma))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001777 return TokError("unexpected token in directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001778 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001779
1780 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001781 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001782 if (getParser().ParseAbsoluteExpression(Size))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001783 return true;
1784
1785 int64_t Pow2Alignment = 0;
1786 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001787 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan79ed1a82010-01-19 20:22:31 +00001788 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001789 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001790 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001791 return true;
1792 }
1793
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001794 if (getLexer().isNot(AsmToken::EndOfStatement))
Chris Lattner9be3fee2009-07-10 22:20:30 +00001795 return TokError("unexpected token in '.zerofill' directive");
1796
Sean Callanan79ed1a82010-01-19 20:22:31 +00001797 Lex();
Chris Lattner9be3fee2009-07-10 22:20:30 +00001798
1799 if (Size < 0)
1800 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1801 "than zero");
1802
Eric Christopherc260a3e2010-05-14 01:38:54 +00001803 // NOTE: The alignment in the directive is a power of 2 value, the assembler
Chris Lattner9be3fee2009-07-10 22:20:30 +00001804 // may internally end up wanting an alignment in bytes.
1805 // FIXME: Diagnose overflow.
1806 if (Pow2Alignment < 0)
1807 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1808 "can't be less than zero");
1809
Daniel Dunbar8906ff12009-08-22 07:22:36 +00001810 if (!Sym->isUndefined())
Chris Lattner9be3fee2009-07-10 22:20:30 +00001811 return Error(IDLoc, "invalid symbol redefinition");
1812
Daniel Dunbarbdee6df2009-08-27 23:58:10 +00001813 // Create the zerofill Symbol with Size and Pow2Alignment
Daniel Dunbar2e152922009-08-28 05:48:29 +00001814 //
1815 // FIXME: Arch specific.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001816 getStreamer().EmitZerofill(getContext().getMachOSection(
1817 Segment, Section, MCSectionMachO::S_ZEROFILL,
1818 0, SectionKind::getBSS()),
1819 Sym, Size, 1 << Pow2Alignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +00001820
1821 return false;
1822}
Kevin Enderbya5c78322009-07-13 21:03:15 +00001823
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001824/// ParseDirectiveTBSS
Eric Christopher482eba02010-05-14 01:50:28 +00001825/// ::= .tbss identifier, size, align
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001826bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001827 SMLoc IDLoc = getLexer().getLoc();
Eric Christopher482eba02010-05-14 01:50:28 +00001828 StringRef Name;
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001829 if (getParser().ParseIdentifier(Name))
Eric Christopher482eba02010-05-14 01:50:28 +00001830 return TokError("expected identifier in directive");
Eric Christopherd04d98d2010-05-17 02:13:02 +00001831
Eric Christopher482eba02010-05-14 01:50:28 +00001832 // Handle the identifier as the key symbol.
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001833 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eric Christopher482eba02010-05-14 01:50:28 +00001834
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001835 if (getLexer().isNot(AsmToken::Comma))
Eric Christopher482eba02010-05-14 01:50:28 +00001836 return TokError("unexpected token in directive");
1837 Lex();
1838
1839 int64_t Size;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001840 SMLoc SizeLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001841 if (getParser().ParseAbsoluteExpression(Size))
Eric Christopher482eba02010-05-14 01:50:28 +00001842 return true;
1843
1844 int64_t Pow2Alignment = 0;
1845 SMLoc Pow2AlignmentLoc;
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001846 if (getLexer().is(AsmToken::Comma)) {
Eric Christopher482eba02010-05-14 01:50:28 +00001847 Lex();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001848 Pow2AlignmentLoc = getLexer().getLoc();
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001849 if (getParser().ParseAbsoluteExpression(Pow2Alignment))
Eric Christopher482eba02010-05-14 01:50:28 +00001850 return true;
1851 }
1852
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001853 if (getLexer().isNot(AsmToken::EndOfStatement))
Eric Christopher482eba02010-05-14 01:50:28 +00001854 return TokError("unexpected token in '.tbss' directive");
1855
1856 Lex();
1857
1858 if (Size < 0)
1859 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1860 "zero");
1861
1862 // FIXME: Diagnose overflow.
1863 if (Pow2Alignment < 0)
1864 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1865 "than zero");
1866
1867 if (!Sym->isUndefined())
1868 return Error(IDLoc, "invalid symbol redefinition");
1869
Daniel Dunbarb6c3a602010-07-12 19:37:35 +00001870 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001871 "__DATA", "__thread_bss",
1872 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1873 0, SectionKind::getThreadBSS()),
1874 Sym, Size, 1 << Pow2Alignment);
Eric Christopher482eba02010-05-14 01:50:28 +00001875
1876 return false;
1877}
1878
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001879/// ParseDirectiveSubsectionsViaSymbols
Kevin Enderbya5c78322009-07-13 21:03:15 +00001880/// ::= .subsections_via_symbols
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001881bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001882 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbya5c78322009-07-13 21:03:15 +00001883 return TokError("unexpected token in '.subsections_via_symbols' directive");
1884
Sean Callanan79ed1a82010-01-19 20:22:31 +00001885 Lex();
Kevin Enderbya5c78322009-07-13 21:03:15 +00001886
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001887 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Kevin Enderbya5c78322009-07-13 21:03:15 +00001888
1889 return false;
1890}
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001891
1892/// ParseDirectiveAbort
1893/// ::= .abort [ "abort_string" ]
1894bool AsmParser::ParseDirectiveAbort() {
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001895 // FIXME: Use loc from directive.
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001896 SMLoc Loc = getLexer().getLoc();
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001897
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001898 StringRef Str = "";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001899 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1900 if (getLexer().isNot(AsmToken::String))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001901 return TokError("expected string in '.abort' directive");
1902
Sean Callanan18b83232010-01-19 21:44:56 +00001903 Str = getTok().getString();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001904
Sean Callanan79ed1a82010-01-19 20:22:31 +00001905 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001906 }
1907
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001908 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001909 return TokError("unexpected token in '.abort' directive");
1910
Sean Callanan79ed1a82010-01-19 20:22:31 +00001911 Lex();
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001912
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +00001913 // FIXME: Handle here.
Daniel Dunbarf9507ff2009-07-27 23:20:52 +00001914 if (Str.empty())
1915 Error(Loc, ".abort detected. Assembly stopping.");
1916 else
1917 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
Kevin Enderby5f1f0b82009-07-13 23:15:14 +00001918
1919 return false;
1920}
Kevin Enderby71148242009-07-14 21:35:03 +00001921
1922/// ParseDirectiveLsym
1923/// ::= .lsym identifier , expression
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001924bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbara6b3c5d2009-08-01 00:48:30 +00001925 StringRef Name;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001926 if (getParser().ParseIdentifier(Name))
Kevin Enderby71148242009-07-14 21:35:03 +00001927 return TokError("expected identifier in directive");
1928
Daniel Dunbar76c4d762009-07-31 21:55:09 +00001929 // Handle the identifier as the key symbol.
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001930 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Kevin Enderby71148242009-07-14 21:35:03 +00001931
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001932 if (getLexer().isNot(AsmToken::Comma))
Kevin Enderby71148242009-07-14 21:35:03 +00001933 return TokError("unexpected token in '.lsym' directive");
Sean Callanan79ed1a82010-01-19 20:22:31 +00001934 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001935
Daniel Dunbar821e3332009-08-31 08:09:28 +00001936 const MCExpr *Value;
Daniel Dunbar38a4e2a2010-07-12 19:08:25 +00001937 if (getParser().ParseExpression(Value))
Kevin Enderby71148242009-07-14 21:35:03 +00001938 return true;
1939
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001940 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby71148242009-07-14 21:35:03 +00001941 return TokError("unexpected token in '.lsym' directive");
1942
Sean Callanan79ed1a82010-01-19 20:22:31 +00001943 Lex();
Kevin Enderby71148242009-07-14 21:35:03 +00001944
Daniel Dunbar7092c7e2009-08-30 06:17:16 +00001945 // We don't currently support this directive.
1946 //
1947 // FIXME: Diagnostic location!
1948 (void) Sym;
1949 return TokError("directive '.lsym' is unsupported");
Kevin Enderby71148242009-07-14 21:35:03 +00001950}
Kevin Enderby1f049b22009-07-14 23:21:55 +00001951
1952/// ParseDirectiveInclude
1953/// ::= .include "filename"
1954bool AsmParser::ParseDirectiveInclude() {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001955 if (getLexer().isNot(AsmToken::String))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001956 return TokError("expected string in '.include' directive");
1957
Sean Callanan18b83232010-01-19 21:44:56 +00001958 std::string Filename = getTok().getString();
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001959 SMLoc IncludeLoc = getLexer().getLoc();
Sean Callanan79ed1a82010-01-19 20:22:31 +00001960 Lex();
Kevin Enderby1f049b22009-07-14 23:21:55 +00001961
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001962 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby1f049b22009-07-14 23:21:55 +00001963 return TokError("unexpected token in '.include' directive");
1964
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001965 // Strip the quotes.
1966 Filename = Filename.substr(1, Filename.size()-2);
1967
1968 // Attempt to switch the lexer to the included file before consuming the end
1969 // of statement to avoid losing it when we switch.
Sean Callananfd0b0282010-01-21 00:19:58 +00001970 if (EnterIncludeFile(Filename)) {
Sean Callananbf2013e2010-01-20 23:19:55 +00001971 PrintMessage(IncludeLoc,
1972 "Could not find include file '" + Filename + "'",
1973 "error");
Chris Lattner8e25e2d2009-07-16 06:14:39 +00001974 return true;
1975 }
Kevin Enderby1f049b22009-07-14 23:21:55 +00001976
1977 return false;
1978}
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001979
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001980/// ParseDirectiveDumpOrLoad
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001981/// ::= ( .dump | .load ) "filename"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00001982bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
1983 SMLoc IDLoc) {
1984 bool IsDump = Directive == ".dump";
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001985 if (getLexer().isNot(AsmToken::String))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001986 return TokError("expected string in '.dump' or '.load' directive");
1987
Sean Callanan79ed1a82010-01-19 20:22:31 +00001988 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001989
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00001990 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001991 return TokError("unexpected token in '.dump' or '.load' directive");
1992
Sean Callanan79ed1a82010-01-19 20:22:31 +00001993 Lex();
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001994
Kevin Enderby5026ae42009-07-20 20:25:37 +00001995 // FIXME: If/when .dump and .load are implemented they will be done in the
1996 // the assembly parser and not have any need for an MCStreamer API.
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001997 if (IsDump)
Kevin Enderby5026ae42009-07-20 20:25:37 +00001998 Warning(IDLoc, "ignoring directive .dump for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00001999 else
Kevin Enderby5026ae42009-07-20 20:25:37 +00002000 Warning(IDLoc, "ignoring directive .load for now");
Kevin Enderby6e68cd92009-07-15 15:30:11 +00002001
2002 return false;
2003}
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002004
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002005/// ParseDirectiveSecureLogUnique
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002006/// ::= .secure_log_unique "log message"
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002007bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002008 std::string LogMessage;
2009
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002010 if (getLexer().isNot(AsmToken::String))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002011 LogMessage = "";
2012 else{
2013 LogMessage = getTok().getString();
2014 Lex();
2015 }
2016
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002017 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002018 return TokError("unexpected token in '.secure_log_unique' directive");
2019
2020 if (getContext().getSecureLogUsed() != false)
2021 return Error(IDLoc, ".secure_log_unique specified multiple times");
2022
2023 char *SecureLogFile = getContext().getSecureLogFile();
2024 if (SecureLogFile == NULL)
2025 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
2026 "environment variable unset.");
2027
2028 raw_ostream *OS = getContext().getSecureLog();
2029 if (OS == NULL) {
2030 std::string Err;
2031 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
2032 if (!Err.empty()) {
2033 delete OS;
2034 return Error(IDLoc, Twine("can't open secure log file: ") +
2035 SecureLogFile + " (" + Err + ")");
2036 }
2037 getContext().setSecureLog(OS);
2038 }
2039
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002040 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
2041 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
2042 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002043 << LogMessage + "\n";
2044
2045 getContext().setSecureLogUsed(true);
2046
2047 return false;
2048}
2049
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002050/// ParseDirectiveSecureLogReset
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002051/// ::= .secure_log_reset
Daniel Dunbar9ac66b02010-07-12 18:49:22 +00002052bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002053 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyf187ac52010-06-28 21:45:58 +00002054 return TokError("unexpected token in '.secure_log_reset' directive");
2055
2056 Lex();
2057
2058 getContext().setSecureLogUsed(false);
2059
2060 return false;
2061}
2062
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002063/// ParseDirectiveIf
2064/// ::= .if expression
2065bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002066 TheCondStack.push_back(TheCondState);
2067 TheCondState.TheCond = AsmCond::IfCond;
2068 if(TheCondState.Ignore) {
2069 EatToEndOfStatement();
2070 }
2071 else {
2072 int64_t ExprValue;
2073 if (ParseAbsoluteExpression(ExprValue))
2074 return true;
2075
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002076 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002077 return TokError("unexpected token in '.if' directive");
2078
Sean Callanan79ed1a82010-01-19 20:22:31 +00002079 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002080
2081 TheCondState.CondMet = ExprValue;
2082 TheCondState.Ignore = !TheCondState.CondMet;
2083 }
2084
2085 return false;
2086}
2087
2088/// ParseDirectiveElseIf
2089/// ::= .elseif expression
2090bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
2091 if (TheCondState.TheCond != AsmCond::IfCond &&
2092 TheCondState.TheCond != AsmCond::ElseIfCond)
2093 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
2094 " an .elseif");
2095 TheCondState.TheCond = AsmCond::ElseIfCond;
2096
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002097 bool LastIgnoreState = false;
2098 if (!TheCondStack.empty())
2099 LastIgnoreState = TheCondStack.back().Ignore;
2100 if (LastIgnoreState || TheCondState.CondMet) {
2101 TheCondState.Ignore = true;
2102 EatToEndOfStatement();
2103 }
2104 else {
2105 int64_t ExprValue;
2106 if (ParseAbsoluteExpression(ExprValue))
2107 return true;
2108
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002109 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002110 return TokError("unexpected token in '.elseif' directive");
2111
Sean Callanan79ed1a82010-01-19 20:22:31 +00002112 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002113 TheCondState.CondMet = ExprValue;
2114 TheCondState.Ignore = !TheCondState.CondMet;
2115 }
2116
2117 return false;
2118}
2119
2120/// ParseDirectiveElse
2121/// ::= .else
2122bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002123 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002124 return TokError("unexpected token in '.else' directive");
2125
Sean Callanan79ed1a82010-01-19 20:22:31 +00002126 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002127
2128 if (TheCondState.TheCond != AsmCond::IfCond &&
2129 TheCondState.TheCond != AsmCond::ElseIfCond)
2130 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
2131 ".elseif");
2132 TheCondState.TheCond = AsmCond::ElseCond;
2133 bool LastIgnoreState = false;
2134 if (!TheCondStack.empty())
2135 LastIgnoreState = TheCondStack.back().Ignore;
2136 if (LastIgnoreState || TheCondState.CondMet)
2137 TheCondState.Ignore = true;
2138 else
2139 TheCondState.Ignore = false;
2140
2141 return false;
2142}
2143
2144/// ParseDirectiveEndIf
2145/// ::= .endif
2146bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
Daniel Dunbar8f34bea2010-07-12 18:03:11 +00002147 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002148 return TokError("unexpected token in '.endif' directive");
2149
Sean Callanan79ed1a82010-01-19 20:22:31 +00002150 Lex();
Kevin Enderbyc114ed72009-08-07 22:46:00 +00002151
2152 if ((TheCondState.TheCond == AsmCond::NoCond) ||
2153 TheCondStack.empty())
2154 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
2155 ".else");
2156 if (!TheCondStack.empty()) {
2157 TheCondState = TheCondStack.back();
2158 TheCondStack.pop_back();
2159 }
2160
2161 return false;
2162}
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002163
2164/// ParseDirectiveFile
2165/// ::= .file [number] string
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002166bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002167 // FIXME: I'm not sure what this is.
2168 int64_t FileNumber = -1;
Daniel Dunbareceec052010-07-12 17:45:27 +00002169 if (getLexer().is(AsmToken::Integer)) {
Sean Callanan18b83232010-01-19 21:44:56 +00002170 FileNumber = getTok().getIntVal();
Sean Callanan79ed1a82010-01-19 20:22:31 +00002171 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002172
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002173 if (FileNumber < 1)
2174 return TokError("file number less than one");
2175 }
2176
Daniel Dunbareceec052010-07-12 17:45:27 +00002177 if (getLexer().isNot(AsmToken::String))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002178 return TokError("unexpected token in '.file' directive");
Daniel Dunbareceec052010-07-12 17:45:27 +00002179
Chris Lattnerd32e8032010-01-25 19:02:58 +00002180 StringRef Filename = getTok().getString();
2181 Filename = Filename.substr(1, Filename.size()-2);
Sean Callanan79ed1a82010-01-19 20:22:31 +00002182 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002183
Daniel Dunbareceec052010-07-12 17:45:27 +00002184 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002185 return TokError("unexpected token in '.file' directive");
2186
Chris Lattnerd32e8032010-01-25 19:02:58 +00002187 if (FileNumber == -1)
Daniel Dunbareceec052010-07-12 17:45:27 +00002188 getStreamer().EmitFileDirective(Filename);
Chris Lattnerd32e8032010-01-25 19:02:58 +00002189 else
Daniel Dunbareceec052010-07-12 17:45:27 +00002190 getStreamer().EmitDwarfFileDirective(FileNumber, Filename);
2191
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002192 return false;
2193}
2194
2195/// ParseDirectiveLine
2196/// ::= .line [number]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002197bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002198 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2199 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002200 return TokError("unexpected token in '.line' directive");
2201
Sean Callanan18b83232010-01-19 21:44:56 +00002202 int64_t LineNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002203 (void) LineNumber;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002204 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002205
2206 // FIXME: Do something with the .line.
2207 }
2208
Daniel Dunbareceec052010-07-12 17:45:27 +00002209 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbar839348a2010-07-01 20:20:01 +00002210 return TokError("unexpected token in '.line' directive");
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002211
2212 return false;
2213}
2214
2215
2216/// ParseDirectiveLoc
2217/// ::= .loc number [number [number]]
Daniel Dunbar81ea00f2010-07-12 17:54:38 +00002218bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
Daniel Dunbareceec052010-07-12 17:45:27 +00002219 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002220 return TokError("unexpected token in '.loc' directive");
2221
2222 // FIXME: What are these fields?
Sean Callanan18b83232010-01-19 21:44:56 +00002223 int64_t FileNumber = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002224 (void) FileNumber;
2225 // FIXME: Validate file.
2226
Sean Callanan79ed1a82010-01-19 20:22:31 +00002227 Lex();
Daniel Dunbareceec052010-07-12 17:45:27 +00002228 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2229 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002230 return TokError("unexpected token in '.loc' directive");
2231
Sean Callanan18b83232010-01-19 21:44:56 +00002232 int64_t Param2 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002233 (void) Param2;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002234 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002235
Daniel Dunbareceec052010-07-12 17:45:27 +00002236 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2237 if (getLexer().isNot(AsmToken::Integer))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002238 return TokError("unexpected token in '.loc' directive");
2239
Sean Callanan18b83232010-01-19 21:44:56 +00002240 int64_t Param3 = getTok().getIntVal();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002241 (void) Param3;
Sean Callanan79ed1a82010-01-19 20:22:31 +00002242 Lex();
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002243
2244 // FIXME: Do something with the .loc.
2245 }
2246 }
2247
Daniel Dunbareceec052010-07-12 17:45:27 +00002248 if (getLexer().isNot(AsmToken::EndOfStatement))
Daniel Dunbard0c14d62009-08-11 04:24:50 +00002249 return TokError("unexpected token in '.file' directive");
2250
2251 return false;
2252}
2253