blob: 81c2cf021c1090829a140671d2adab4c271b1cd3 [file] [log] [blame]
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +00001//===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/StringSwitch.h"
13#include "llvm/ADT/Twine.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000014#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000017#include "llvm/MC/MCSectionMachO.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSymbol.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000020#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/SourceMgr.h"
22using namespace llvm;
23
24namespace {
25
26/// \brief Implementation of directive handling which is shared across all
27/// Darwin targets.
28class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000029 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000030 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000031 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
32 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000033 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000034 }
35
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000036 bool ParseSectionSwitch(const char *Segment, const char *Section,
37 unsigned TAA = 0, unsigned ImplicitAlign = 0,
38 unsigned StubSize = 0);
39
40public:
41 DarwinAsmParser() {}
42
Craig Topper59be68f2014-03-08 07:14:16 +000043 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000044 // Call the base implementation.
45 this->MCAsmParserExtension::Initialize(Parser);
46
Jim Grosbachd2037eb2013-02-20 22:21:35 +000047 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDesc>(".desc");
Kevin Enderby3aeada22013-08-28 17:50:59 +000048 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveIndirectSymbol>(
49 ".indirect_symbol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000050 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLsym>(".lsym");
51 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000052 ".subsections_via_symbols");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000053 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".dump");
54 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDumpOrLoad>(".load");
55 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSection>(".section");
56 addDirectiveHandler<&DarwinAsmParser::ParseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000057 ".pushsection");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000058 addDirectiveHandler<&DarwinAsmParser::ParseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000059 ".popsection");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000060 addDirectiveHandler<&DarwinAsmParser::ParseDirectivePrevious>(".previous");
61 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000062 ".secure_log_unique");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000063 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000064 ".secure_log_reset");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000065 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveTBSS>(".tbss");
66 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000067
Jim Grosbachd2037eb2013-02-20 22:21:35 +000068 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000069 ".data_region");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000070 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000071 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000072
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000073 // Special section directives.
Rafael Espindola3402c052013-10-02 14:09:29 +000074 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveBss>(".bss");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000075 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConst>(".const");
76 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000077 ".const_data");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000078 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000079 ".constructor");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000080 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000081 ".cstring");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000082 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveData>(".data");
83 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000084 ".destructor");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000085 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveDyld>(".dyld");
86 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000087 ".fvmlib_init0");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000088 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000089 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000090 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +000091 &DarwinAsmParser::ParseSectionDirectiveLazySymbolPointers>(
92 ".lazy_symbol_pointer");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000093 addDirectiveHandler<&DarwinAsmParser::ParseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +000094 ".linker_option");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000095 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000096 ".literal16");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000097 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000098 ".literal4");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000099 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000100 ".literal8");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000101 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000102 ".mod_init_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000103 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000104 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000105 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000106 &DarwinAsmParser::ParseSectionDirectiveNonLazySymbolPointers>(
107 ".non_lazy_symbol_pointer");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000108 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000109 ".objc_cat_cls_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000110 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000111 ".objc_cat_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000112 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000113 ".objc_category");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000114 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000115 ".objc_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000116 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000117 ".objc_class_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000118 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000119 ".objc_class_vars");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000120 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000121 ".objc_cls_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000122 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000123 ".objc_cls_refs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000124 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000125 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000126 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000127 &DarwinAsmParser::ParseSectionDirectiveObjCInstanceVars>(
128 ".objc_instance_vars");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000129 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000130 ".objc_message_refs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000131 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000132 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000133 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000134 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarNames>(
135 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000136 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000137 &DarwinAsmParser::ParseSectionDirectiveObjCMethVarTypes>(
138 ".objc_meth_var_types");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000139 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000140 ".objc_module_info");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000141 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000142 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000144 &DarwinAsmParser::ParseSectionDirectiveObjCSelectorStrs>(
145 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000146 addDirectiveHandler<
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000147 &DarwinAsmParser::ParseSectionDirectiveObjCStringObject>(
148 ".objc_string_object");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000149 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000150 ".objc_symbols");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000151 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000152 ".picsymbol_stub");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000153 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000154 ".static_const");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000155 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000156 ".static_data");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000157 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000158 ".symbol_stub");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000159 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTData>(".tdata");
160 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveText>(".text");
161 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000162 ".thread_init_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000163 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000164
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000165 addDirectiveHandler<&DarwinAsmParser::ParseSectionDirectiveIdent>(".ident");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000166 }
167
168 bool ParseDirectiveDesc(StringRef, SMLoc);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000169 bool ParseDirectiveIndirectSymbol(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000170 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
171 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar16004b82013-01-18 01:25:48 +0000172 bool ParseDirectiveLinkerOption(StringRef, SMLoc);
Daniel Dunbar8897d472010-07-18 22:22:07 +0000173 bool ParseDirectiveSection(StringRef, SMLoc);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000174 bool ParseDirectivePushSection(StringRef, SMLoc);
175 bool ParseDirectivePopSection(StringRef, SMLoc);
176 bool ParseDirectivePrevious(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000177 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
178 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
179 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
180 bool ParseDirectiveTBSS(StringRef, SMLoc);
181 bool ParseDirectiveZerofill(StringRef, SMLoc);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000182 bool ParseDirectiveDataRegion(StringRef, SMLoc);
183 bool ParseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000184
185 // Named Section Directive
Rafael Espindola3402c052013-10-02 14:09:29 +0000186 bool ParseSectionDirectiveBss(StringRef, SMLoc) {
187 return ParseSectionSwitch("__DATA", "__bss");
188 }
189
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000190 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
191 return ParseSectionSwitch("__TEXT", "__const");
192 }
193 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
194 return ParseSectionSwitch("__TEXT", "__static_const");
195 }
196 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
197 return ParseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000198 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000199 }
200 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
201 return ParseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000202 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000203 }
204 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
205 return ParseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000206 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000207 }
208 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
209 return ParseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000210 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000211 }
212 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
213 return ParseSectionSwitch("__TEXT","__constructor");
214 }
215 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
216 return ParseSectionSwitch("__TEXT","__destructor");
217 }
218 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
219 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
220 }
221 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
222 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
223 }
224 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
225 return ParseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000226 MachO::S_SYMBOL_STUBS |
227 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000228 // FIXME: Different on PPC and ARM.
229 0, 16);
230 }
231 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
232 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000233 MachO::S_SYMBOL_STUBS |
234 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000235 }
236 bool ParseSectionDirectiveData(StringRef, SMLoc) {
237 return ParseSectionSwitch("__DATA", "__data");
238 }
239 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
240 return ParseSectionSwitch("__DATA", "__static_data");
241 }
242 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
243 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000244 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000245 }
246 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
247 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000248 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000249 }
250 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
251 return ParseSectionSwitch("__DATA", "__dyld");
252 }
253 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
254 return ParseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000255 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000256 }
257 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
258 return ParseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000259 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000260 }
261 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
262 return ParseSectionSwitch("__DATA", "__const");
263 }
264 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
265 return ParseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000266 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000267 }
268 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
269 return ParseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000270 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000271 }
272 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
273 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000274 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000275 }
276 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
277 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000278 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000279 }
280 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
281 return ParseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000282 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000283 }
284 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
285 return ParseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000286 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000287 }
288 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
289 return ParseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000290 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000291 }
292 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
293 return ParseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000294 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000295 }
296 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
297 return ParseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000298 MachO::S_ATTR_NO_DEAD_STRIP |
299 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000300 }
301 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
302 return ParseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000303 MachO::S_ATTR_NO_DEAD_STRIP |
304 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000305 }
306 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
307 return ParseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000308 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000309 }
310 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
311 return ParseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000312 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000313 }
314 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
315 return ParseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000316 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000317 }
318 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
319 return ParseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000320 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000321 }
322 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
323 return ParseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000324 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000325 }
326 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
327 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000328 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000329 }
330 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
331 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000332 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000333 }
334 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
335 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000336 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000337 }
338 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
339 return ParseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000340 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000341 }
342 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
343 return ParseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000344 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000345 }
346 bool ParseSectionDirectiveText(StringRef, SMLoc) {
347 return ParseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000348 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000349 }
350 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
351 return ParseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000352 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000353 }
Jim Grosbach14be61a2011-03-08 19:17:19 +0000354 bool ParseSectionDirectiveIdent(StringRef, SMLoc) {
355 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000356 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000357 return false;
358 }
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000359 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
360 return ParseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000361 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000362 }
363
364};
365
Bill Wendlingce4fe412012-08-08 06:30:30 +0000366} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000367
368bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
369 const char *Section,
370 unsigned TAA, unsigned Align,
371 unsigned StubSize) {
372 if (getLexer().isNot(AsmToken::EndOfStatement))
373 return TokError("unexpected token in section switching directive");
374 Lex();
375
376 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000377 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000378 getStreamer().SwitchSection(getContext().getMachOSection(
379 Segment, Section, TAA, StubSize,
380 isText ? SectionKind::getText()
381 : SectionKind::getDataRel()));
382
383 // Set the implicit alignment, if any.
384 //
385 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
386 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000387 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000388 // the section. However, this is arguably more reasonable behavior, and there
389 // is no good reason for someone to intentionally emit incorrectly sized
390 // values into the implicitly aligned sections.
391 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000392 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000393
394 return false;
395}
396
397/// ParseDirectiveDesc
398/// ::= .desc identifier , expression
399bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
400 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000401 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000402 return TokError("expected identifier in directive");
403
404 // Handle the identifier as the key symbol.
405 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
406
407 if (getLexer().isNot(AsmToken::Comma))
408 return TokError("unexpected token in '.desc' directive");
409 Lex();
410
411 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000412 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000413 return true;
414
415 if (getLexer().isNot(AsmToken::EndOfStatement))
416 return TokError("unexpected token in '.desc' directive");
417
418 Lex();
419
420 // Set the n_desc field of this Symbol to this DescValue
421 getStreamer().EmitSymbolDesc(Sym, DescValue);
422
423 return false;
424}
425
Kevin Enderby3aeada22013-08-28 17:50:59 +0000426/// ParseDirectiveIndirectSymbol
427/// ::= .indirect_symbol identifier
428bool DarwinAsmParser::ParseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
429 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
430 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000431 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000432 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
433 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
434 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000435 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
436 "section");
437
438 StringRef Name;
439 if (getParser().parseIdentifier(Name))
440 return TokError("expected identifier in .indirect_symbol directive");
441
442 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
443
444 // Assembler local symbols don't make any sense here. Complain loudly.
445 if (Sym->isTemporary())
446 return TokError("non-local symbol required in directive");
447
448 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
449 return TokError("unable to emit indirect symbol attribute for: " + Name);
450
451 if (getLexer().isNot(AsmToken::EndOfStatement))
452 return TokError("unexpected token in '.indirect_symbol' directive");
453
454 Lex();
455
456 return false;
457}
458
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000459/// ParseDirectiveDumpOrLoad
460/// ::= ( .dump | .load ) "filename"
461bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
462 SMLoc IDLoc) {
463 bool IsDump = Directive == ".dump";
464 if (getLexer().isNot(AsmToken::String))
465 return TokError("expected string in '.dump' or '.load' directive");
466
467 Lex();
468
469 if (getLexer().isNot(AsmToken::EndOfStatement))
470 return TokError("unexpected token in '.dump' or '.load' directive");
471
472 Lex();
473
474 // FIXME: If/when .dump and .load are implemented they will be done in the
475 // the assembly parser and not have any need for an MCStreamer API.
476 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000477 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000478 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000479 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000480}
481
Daniel Dunbar16004b82013-01-18 01:25:48 +0000482/// ParseDirectiveLinkerOption
483/// ::= .linker_option "string" ( , "string" )*
484bool DarwinAsmParser::ParseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
485 SmallVector<std::string, 4> Args;
486 for (;;) {
487 if (getLexer().isNot(AsmToken::String))
488 return TokError("expected string in '" + Twine(IDVal) + "' directive");
489
490 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000491 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000492 return true;
493
494 Args.push_back(Data);
495
496 Lex();
497 if (getLexer().is(AsmToken::EndOfStatement))
498 break;
499
500 if (getLexer().isNot(AsmToken::Comma))
501 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
502 Lex();
503 }
504
505 getStreamer().EmitLinkerOptions(Args);
506 return false;
507}
508
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000509/// ParseDirectiveLsym
510/// ::= .lsym identifier , expression
511bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
512 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000513 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000514 return TokError("expected identifier in directive");
515
516 // Handle the identifier as the key symbol.
517 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
518
519 if (getLexer().isNot(AsmToken::Comma))
520 return TokError("unexpected token in '.lsym' directive");
521 Lex();
522
523 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000524 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000525 return true;
526
527 if (getLexer().isNot(AsmToken::EndOfStatement))
528 return TokError("unexpected token in '.lsym' directive");
529
530 Lex();
531
532 // We don't currently support this directive.
533 //
534 // FIXME: Diagnostic location!
535 (void) Sym;
536 return TokError("directive '.lsym' is unsupported");
537}
538
539/// ParseDirectiveSection:
540/// ::= .section identifier (',' identifier)*
Daniel Dunbar8897d472010-07-18 22:22:07 +0000541bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000542 SMLoc Loc = getLexer().getLoc();
543
544 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000545 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000546 return Error(Loc, "expected identifier after '.section' directive");
547
548 // Verify there is a following comma.
549 if (!getLexer().is(AsmToken::Comma))
550 return TokError("unexpected token in '.section' directive");
551
552 std::string SectionSpec = SectionName;
553 SectionSpec += ",";
554
555 // Add all the tokens until the end of the line, ParseSectionSpecifier will
556 // handle this.
557 StringRef EOL = getLexer().LexUntilEndOfStatement();
558 SectionSpec.append(EOL.begin(), EOL.end());
559
560 Lex();
561 if (getLexer().isNot(AsmToken::EndOfStatement))
562 return TokError("unexpected token in '.section' directive");
563 Lex();
564
565
566 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000567 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000568 unsigned TAA;
569 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000570 std::string ErrorStr =
571 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000572 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000573
574 if (!ErrorStr.empty())
575 return Error(Loc, ErrorStr.c_str());
576
577 // FIXME: Arch specific.
578 bool isText = Segment == "__TEXT"; // FIXME: Hack.
579 getStreamer().SwitchSection(getContext().getMachOSection(
580 Segment, Section, TAA, StubSize,
581 isText ? SectionKind::getText()
582 : SectionKind::getDataRel()));
583 return false;
584}
585
Bill Wendlingce4fe412012-08-08 06:30:30 +0000586/// ParseDirectivePushSection:
587/// ::= .pushsection identifier (',' identifier)*
588bool DarwinAsmParser::ParseDirectivePushSection(StringRef S, SMLoc Loc) {
589 getStreamer().PushSection();
590
591 if (ParseDirectiveSection(S, Loc)) {
592 getStreamer().PopSection();
593 return true;
594 }
595
596 return false;
597}
598
599/// ParseDirectivePopSection:
600/// ::= .popsection
601bool DarwinAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
602 if (!getStreamer().PopSection())
603 return TokError(".popsection without corresponding .pushsection");
604 return false;
605}
606
607/// ParseDirectivePrevious:
608/// ::= .previous
609bool DarwinAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000610 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
611 if (PreviousSection.first == NULL)
Bill Wendlingce4fe412012-08-08 06:30:30 +0000612 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000613 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000614 return false;
615}
616
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000617/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000618/// ::= .secure_log_unique ... message ...
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000619bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000620 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000621 if (getLexer().isNot(AsmToken::EndOfStatement))
622 return TokError("unexpected token in '.secure_log_unique' directive");
623
624 if (getContext().getSecureLogUsed() != false)
625 return Error(IDLoc, ".secure_log_unique specified multiple times");
626
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000627 // Get the secure log path.
628 const char *SecureLogFile = getContext().getSecureLogFile();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000629 if (SecureLogFile == NULL)
630 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
631 "environment variable unset.");
632
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000633 // Open the secure log file if we haven't already.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000634 raw_ostream *OS = getContext().getSecureLog();
635 if (OS == NULL) {
636 std::string Err;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000637 OS = new raw_fd_ostream(SecureLogFile, Err,
638 sys::fs::F_Append | sys::fs::F_Text);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000639 if (!Err.empty()) {
640 delete OS;
641 return Error(IDLoc, Twine("can't open secure log file: ") +
642 SecureLogFile + " (" + Err + ")");
643 }
644 getContext().setSecureLog(OS);
645 }
646
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000647 // Write the message.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000648 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
649 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
650 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
651 << LogMessage + "\n";
652
653 getContext().setSecureLogUsed(true);
654
655 return false;
656}
657
658/// ParseDirectiveSecureLogReset
659/// ::= .secure_log_reset
660bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
661 if (getLexer().isNot(AsmToken::EndOfStatement))
662 return TokError("unexpected token in '.secure_log_reset' directive");
663
664 Lex();
665
666 getContext().setSecureLogUsed(false);
667
668 return false;
669}
670
671/// ParseDirectiveSubsectionsViaSymbols
672/// ::= .subsections_via_symbols
673bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
674 if (getLexer().isNot(AsmToken::EndOfStatement))
675 return TokError("unexpected token in '.subsections_via_symbols' directive");
676
677 Lex();
678
679 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
680
681 return false;
682}
683
684/// ParseDirectiveTBSS
685/// ::= .tbss identifier, size, align
686bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
687 SMLoc IDLoc = getLexer().getLoc();
688 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000689 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000690 return TokError("expected identifier in directive");
691
692 // Handle the identifier as the key symbol.
693 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
694
695 if (getLexer().isNot(AsmToken::Comma))
696 return TokError("unexpected token in directive");
697 Lex();
698
699 int64_t Size;
700 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000701 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000702 return true;
703
704 int64_t Pow2Alignment = 0;
705 SMLoc Pow2AlignmentLoc;
706 if (getLexer().is(AsmToken::Comma)) {
707 Lex();
708 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000709 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000710 return true;
711 }
712
713 if (getLexer().isNot(AsmToken::EndOfStatement))
714 return TokError("unexpected token in '.tbss' directive");
715
716 Lex();
717
718 if (Size < 0)
719 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
720 "zero");
721
722 // FIXME: Diagnose overflow.
723 if (Pow2Alignment < 0)
724 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
725 "than zero");
726
727 if (!Sym->isUndefined())
728 return Error(IDLoc, "invalid symbol redefinition");
729
730 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
731 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000732 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000733 0, SectionKind::getThreadBSS()),
734 Sym, Size, 1 << Pow2Alignment);
735
736 return false;
737}
738
739/// ParseDirectiveZerofill
740/// ::= .zerofill segname , sectname [, identifier , size_expression [
741/// , align_expression ]]
742bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
743 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000744 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000745 return TokError("expected segment name after '.zerofill' directive");
746
747 if (getLexer().isNot(AsmToken::Comma))
748 return TokError("unexpected token in directive");
749 Lex();
750
751 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000752 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000753 return TokError("expected section name after comma in '.zerofill' "
754 "directive");
755
756 // If this is the end of the line all that was wanted was to create the
757 // the section but with no symbol.
758 if (getLexer().is(AsmToken::EndOfStatement)) {
759 // Create the zerofill section but no symbol
760 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000761 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000762 0, SectionKind::getBSS()));
763 return false;
764 }
765
766 if (getLexer().isNot(AsmToken::Comma))
767 return TokError("unexpected token in directive");
768 Lex();
769
770 SMLoc IDLoc = getLexer().getLoc();
771 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000772 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000773 return TokError("expected identifier in directive");
774
775 // handle the identifier as the key symbol.
776 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
777
778 if (getLexer().isNot(AsmToken::Comma))
779 return TokError("unexpected token in directive");
780 Lex();
781
782 int64_t Size;
783 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000784 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000785 return true;
786
787 int64_t Pow2Alignment = 0;
788 SMLoc Pow2AlignmentLoc;
789 if (getLexer().is(AsmToken::Comma)) {
790 Lex();
791 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000792 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000793 return true;
794 }
795
796 if (getLexer().isNot(AsmToken::EndOfStatement))
797 return TokError("unexpected token in '.zerofill' directive");
798
799 Lex();
800
801 if (Size < 0)
802 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
803 "than zero");
804
805 // NOTE: The alignment in the directive is a power of 2 value, the assembler
806 // may internally end up wanting an alignment in bytes.
807 // FIXME: Diagnose overflow.
808 if (Pow2Alignment < 0)
809 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
810 "can't be less than zero");
811
812 if (!Sym->isUndefined())
813 return Error(IDLoc, "invalid symbol redefinition");
814
815 // Create the zerofill Symbol with Size and Pow2Alignment
816 //
817 // FIXME: Arch specific.
818 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000819 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000820 0, SectionKind::getBSS()),
821 Sym, Size, 1 << Pow2Alignment);
822
823 return false;
824}
825
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000826/// ParseDirectiveDataRegion
827/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
828bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) {
829 if (getLexer().is(AsmToken::EndOfStatement)) {
830 Lex();
831 getStreamer().EmitDataRegion(MCDR_DataRegion);
832 return false;
833 }
834 StringRef RegionType;
835 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000836 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000837 return TokError("expected region type after '.data_region' directive");
838 int Kind = StringSwitch<int>(RegionType)
839 .Case("jt8", MCDR_DataRegionJT8)
840 .Case("jt16", MCDR_DataRegionJT16)
841 .Case("jt32", MCDR_DataRegionJT32)
842 .Default(-1);
843 if (Kind == -1)
844 return Error(Loc, "unknown region type in '.data_region' directive");
845 Lex();
846
847 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
848 return false;
849}
850
851/// ParseDirectiveDataRegionEnd
852/// ::= .end_data_region
853bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) {
854 if (getLexer().isNot(AsmToken::EndOfStatement))
855 return TokError("unexpected token in '.end_data_region' directive");
856
857 Lex();
858 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
859 return false;
860}
861
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000862namespace llvm {
863
864MCAsmParserExtension *createDarwinAsmParser() {
865 return new DarwinAsmParser;
866}
867
Bill Wendlingce4fe412012-08-08 06:30:30 +0000868} // end llvm namespace