blob: dd7eccb2378bf64049c4a35d9f0d6119acca4dd6 [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");
Jim Grosbach448334a2014-03-18 22:09:05 +0000166 addDirectiveHandler<&DarwinAsmParser::ParseVersionMin>(".ios_version_min");
167 addDirectiveHandler<&DarwinAsmParser::ParseVersionMin>(
168 ".macosx_version_min");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000169 }
170
171 bool ParseDirectiveDesc(StringRef, SMLoc);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000172 bool ParseDirectiveIndirectSymbol(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000173 bool ParseDirectiveDumpOrLoad(StringRef, SMLoc);
174 bool ParseDirectiveLsym(StringRef, SMLoc);
Daniel Dunbar16004b82013-01-18 01:25:48 +0000175 bool ParseDirectiveLinkerOption(StringRef, SMLoc);
Daniel Dunbar8897d472010-07-18 22:22:07 +0000176 bool ParseDirectiveSection(StringRef, SMLoc);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000177 bool ParseDirectivePushSection(StringRef, SMLoc);
178 bool ParseDirectivePopSection(StringRef, SMLoc);
179 bool ParseDirectivePrevious(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000180 bool ParseDirectiveSecureLogReset(StringRef, SMLoc);
181 bool ParseDirectiveSecureLogUnique(StringRef, SMLoc);
182 bool ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
183 bool ParseDirectiveTBSS(StringRef, SMLoc);
184 bool ParseDirectiveZerofill(StringRef, SMLoc);
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000185 bool ParseDirectiveDataRegion(StringRef, SMLoc);
186 bool ParseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000187
188 // Named Section Directive
Rafael Espindola3402c052013-10-02 14:09:29 +0000189 bool ParseSectionDirectiveBss(StringRef, SMLoc) {
190 return ParseSectionSwitch("__DATA", "__bss");
191 }
192
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000193 bool ParseSectionDirectiveConst(StringRef, SMLoc) {
194 return ParseSectionSwitch("__TEXT", "__const");
195 }
196 bool ParseSectionDirectiveStaticConst(StringRef, SMLoc) {
197 return ParseSectionSwitch("__TEXT", "__static_const");
198 }
199 bool ParseSectionDirectiveCString(StringRef, SMLoc) {
200 return ParseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000201 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000202 }
203 bool ParseSectionDirectiveLiteral4(StringRef, SMLoc) {
204 return ParseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000205 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000206 }
207 bool ParseSectionDirectiveLiteral8(StringRef, SMLoc) {
208 return ParseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000209 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000210 }
211 bool ParseSectionDirectiveLiteral16(StringRef, SMLoc) {
212 return ParseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000213 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000214 }
215 bool ParseSectionDirectiveConstructor(StringRef, SMLoc) {
216 return ParseSectionSwitch("__TEXT","__constructor");
217 }
218 bool ParseSectionDirectiveDestructor(StringRef, SMLoc) {
219 return ParseSectionSwitch("__TEXT","__destructor");
220 }
221 bool ParseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
222 return ParseSectionSwitch("__TEXT","__fvmlib_init0");
223 }
224 bool ParseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
225 return ParseSectionSwitch("__TEXT","__fvmlib_init1");
226 }
227 bool ParseSectionDirectiveSymbolStub(StringRef, SMLoc) {
228 return ParseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000229 MachO::S_SYMBOL_STUBS |
230 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000231 // FIXME: Different on PPC and ARM.
232 0, 16);
233 }
234 bool ParseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
235 return ParseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000236 MachO::S_SYMBOL_STUBS |
237 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000238 }
239 bool ParseSectionDirectiveData(StringRef, SMLoc) {
240 return ParseSectionSwitch("__DATA", "__data");
241 }
242 bool ParseSectionDirectiveStaticData(StringRef, SMLoc) {
243 return ParseSectionSwitch("__DATA", "__static_data");
244 }
245 bool ParseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
246 return ParseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000247 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000248 }
249 bool ParseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
250 return ParseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000251 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000252 }
253 bool ParseSectionDirectiveDyld(StringRef, SMLoc) {
254 return ParseSectionSwitch("__DATA", "__dyld");
255 }
256 bool ParseSectionDirectiveModInitFunc(StringRef, SMLoc) {
257 return ParseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000258 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000259 }
260 bool ParseSectionDirectiveModTermFunc(StringRef, SMLoc) {
261 return ParseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000262 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000263 }
264 bool ParseSectionDirectiveConstData(StringRef, SMLoc) {
265 return ParseSectionSwitch("__DATA", "__const");
266 }
267 bool ParseSectionDirectiveObjCClass(StringRef, SMLoc) {
268 return ParseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000269 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000270 }
271 bool ParseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
272 return ParseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000273 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000274 }
275 bool ParseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
276 return ParseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000277 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000278 }
279 bool ParseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
280 return ParseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000281 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000282 }
283 bool ParseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
284 return ParseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000285 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000286 }
287 bool ParseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
288 return ParseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000289 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000290 }
291 bool ParseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
292 return ParseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000293 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000294 }
295 bool ParseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
296 return ParseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000297 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000298 }
299 bool ParseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
300 return ParseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000301 MachO::S_ATTR_NO_DEAD_STRIP |
302 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000303 }
304 bool ParseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
305 return ParseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000306 MachO::S_ATTR_NO_DEAD_STRIP |
307 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000308 }
309 bool ParseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
310 return ParseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000311 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000312 }
313 bool ParseSectionDirectiveObjCCategory(StringRef, SMLoc) {
314 return ParseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000315 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000316 }
317 bool ParseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
318 return ParseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000319 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000320 }
321 bool ParseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
322 return ParseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000323 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000324 }
325 bool ParseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
326 return ParseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000327 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000328 }
329 bool ParseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
330 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000331 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000332 }
333 bool ParseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
334 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000335 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000336 }
337 bool ParseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
338 return ParseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000339 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000340 }
341 bool ParseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
342 return ParseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000343 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000344 }
345 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
346 return ParseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000347 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000348 }
349 bool ParseSectionDirectiveText(StringRef, SMLoc) {
350 return ParseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000351 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000352 }
353 bool ParseSectionDirectiveTLV(StringRef, SMLoc) {
354 return ParseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000355 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000356 }
Jim Grosbach14be61a2011-03-08 19:17:19 +0000357 bool ParseSectionDirectiveIdent(StringRef, SMLoc) {
358 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000359 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000360 return false;
361 }
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000362 bool ParseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
363 return ParseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000364 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000365 }
Jim Grosbach448334a2014-03-18 22:09:05 +0000366 bool ParseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000367
368};
369
Bill Wendlingce4fe412012-08-08 06:30:30 +0000370} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000371
372bool DarwinAsmParser::ParseSectionSwitch(const char *Segment,
373 const char *Section,
374 unsigned TAA, unsigned Align,
375 unsigned StubSize) {
376 if (getLexer().isNot(AsmToken::EndOfStatement))
377 return TokError("unexpected token in section switching directive");
378 Lex();
379
380 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000381 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000382 getStreamer().SwitchSection(getContext().getMachOSection(
383 Segment, Section, TAA, StubSize,
384 isText ? SectionKind::getText()
385 : SectionKind::getDataRel()));
386
387 // Set the implicit alignment, if any.
388 //
389 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
390 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000391 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000392 // the section. However, this is arguably more reasonable behavior, and there
393 // is no good reason for someone to intentionally emit incorrectly sized
394 // values into the implicitly aligned sections.
395 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000396 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000397
398 return false;
399}
400
401/// ParseDirectiveDesc
402/// ::= .desc identifier , expression
403bool DarwinAsmParser::ParseDirectiveDesc(StringRef, SMLoc) {
404 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000405 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000406 return TokError("expected identifier in directive");
407
408 // Handle the identifier as the key symbol.
409 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
410
411 if (getLexer().isNot(AsmToken::Comma))
412 return TokError("unexpected token in '.desc' directive");
413 Lex();
414
415 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000416 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000417 return true;
418
419 if (getLexer().isNot(AsmToken::EndOfStatement))
420 return TokError("unexpected token in '.desc' directive");
421
422 Lex();
423
424 // Set the n_desc field of this Symbol to this DescValue
425 getStreamer().EmitSymbolDesc(Sym, DescValue);
426
427 return false;
428}
429
Kevin Enderby3aeada22013-08-28 17:50:59 +0000430/// ParseDirectiveIndirectSymbol
431/// ::= .indirect_symbol identifier
432bool DarwinAsmParser::ParseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
433 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
434 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000435 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000436 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
437 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
438 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000439 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
440 "section");
441
442 StringRef Name;
443 if (getParser().parseIdentifier(Name))
444 return TokError("expected identifier in .indirect_symbol directive");
445
446 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
447
448 // Assembler local symbols don't make any sense here. Complain loudly.
449 if (Sym->isTemporary())
450 return TokError("non-local symbol required in directive");
451
452 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
453 return TokError("unable to emit indirect symbol attribute for: " + Name);
454
455 if (getLexer().isNot(AsmToken::EndOfStatement))
456 return TokError("unexpected token in '.indirect_symbol' directive");
457
458 Lex();
459
460 return false;
461}
462
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000463/// ParseDirectiveDumpOrLoad
464/// ::= ( .dump | .load ) "filename"
465bool DarwinAsmParser::ParseDirectiveDumpOrLoad(StringRef Directive,
466 SMLoc IDLoc) {
467 bool IsDump = Directive == ".dump";
468 if (getLexer().isNot(AsmToken::String))
469 return TokError("expected string in '.dump' or '.load' directive");
470
471 Lex();
472
473 if (getLexer().isNot(AsmToken::EndOfStatement))
474 return TokError("unexpected token in '.dump' or '.load' directive");
475
476 Lex();
477
478 // FIXME: If/when .dump and .load are implemented they will be done in the
479 // the assembly parser and not have any need for an MCStreamer API.
480 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000481 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000482 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000483 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000484}
485
Daniel Dunbar16004b82013-01-18 01:25:48 +0000486/// ParseDirectiveLinkerOption
487/// ::= .linker_option "string" ( , "string" )*
488bool DarwinAsmParser::ParseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
489 SmallVector<std::string, 4> Args;
490 for (;;) {
491 if (getLexer().isNot(AsmToken::String))
492 return TokError("expected string in '" + Twine(IDVal) + "' directive");
493
494 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000495 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000496 return true;
497
498 Args.push_back(Data);
499
500 Lex();
501 if (getLexer().is(AsmToken::EndOfStatement))
502 break;
503
504 if (getLexer().isNot(AsmToken::Comma))
505 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
506 Lex();
507 }
508
509 getStreamer().EmitLinkerOptions(Args);
510 return false;
511}
512
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000513/// ParseDirectiveLsym
514/// ::= .lsym identifier , expression
515bool DarwinAsmParser::ParseDirectiveLsym(StringRef, SMLoc) {
516 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000517 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000518 return TokError("expected identifier in directive");
519
520 // Handle the identifier as the key symbol.
521 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
522
523 if (getLexer().isNot(AsmToken::Comma))
524 return TokError("unexpected token in '.lsym' directive");
525 Lex();
526
527 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000528 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000529 return true;
530
531 if (getLexer().isNot(AsmToken::EndOfStatement))
532 return TokError("unexpected token in '.lsym' directive");
533
534 Lex();
535
536 // We don't currently support this directive.
537 //
538 // FIXME: Diagnostic location!
539 (void) Sym;
540 return TokError("directive '.lsym' is unsupported");
541}
542
543/// ParseDirectiveSection:
544/// ::= .section identifier (',' identifier)*
Daniel Dunbar8897d472010-07-18 22:22:07 +0000545bool DarwinAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000546 SMLoc Loc = getLexer().getLoc();
547
548 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000549 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000550 return Error(Loc, "expected identifier after '.section' directive");
551
552 // Verify there is a following comma.
553 if (!getLexer().is(AsmToken::Comma))
554 return TokError("unexpected token in '.section' directive");
555
556 std::string SectionSpec = SectionName;
557 SectionSpec += ",";
558
559 // Add all the tokens until the end of the line, ParseSectionSpecifier will
560 // handle this.
561 StringRef EOL = getLexer().LexUntilEndOfStatement();
562 SectionSpec.append(EOL.begin(), EOL.end());
563
564 Lex();
565 if (getLexer().isNot(AsmToken::EndOfStatement))
566 return TokError("unexpected token in '.section' directive");
567 Lex();
568
569
570 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000571 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000572 unsigned TAA;
573 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000574 std::string ErrorStr =
575 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000576 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000577
578 if (!ErrorStr.empty())
579 return Error(Loc, ErrorStr.c_str());
580
581 // FIXME: Arch specific.
582 bool isText = Segment == "__TEXT"; // FIXME: Hack.
583 getStreamer().SwitchSection(getContext().getMachOSection(
584 Segment, Section, TAA, StubSize,
585 isText ? SectionKind::getText()
586 : SectionKind::getDataRel()));
587 return false;
588}
589
Bill Wendlingce4fe412012-08-08 06:30:30 +0000590/// ParseDirectivePushSection:
591/// ::= .pushsection identifier (',' identifier)*
592bool DarwinAsmParser::ParseDirectivePushSection(StringRef S, SMLoc Loc) {
593 getStreamer().PushSection();
594
595 if (ParseDirectiveSection(S, Loc)) {
596 getStreamer().PopSection();
597 return true;
598 }
599
600 return false;
601}
602
603/// ParseDirectivePopSection:
604/// ::= .popsection
605bool DarwinAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
606 if (!getStreamer().PopSection())
607 return TokError(".popsection without corresponding .pushsection");
608 return false;
609}
610
611/// ParseDirectivePrevious:
612/// ::= .previous
613bool DarwinAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000614 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
615 if (PreviousSection.first == NULL)
Bill Wendlingce4fe412012-08-08 06:30:30 +0000616 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000617 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000618 return false;
619}
620
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000621/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000622/// ::= .secure_log_unique ... message ...
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000623bool DarwinAsmParser::ParseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000624 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000625 if (getLexer().isNot(AsmToken::EndOfStatement))
626 return TokError("unexpected token in '.secure_log_unique' directive");
627
628 if (getContext().getSecureLogUsed() != false)
629 return Error(IDLoc, ".secure_log_unique specified multiple times");
630
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000631 // Get the secure log path.
632 const char *SecureLogFile = getContext().getSecureLogFile();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000633 if (SecureLogFile == NULL)
634 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
635 "environment variable unset.");
636
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000637 // Open the secure log file if we haven't already.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000638 raw_ostream *OS = getContext().getSecureLog();
639 if (OS == NULL) {
640 std::string Err;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000641 OS = new raw_fd_ostream(SecureLogFile, Err,
642 sys::fs::F_Append | sys::fs::F_Text);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000643 if (!Err.empty()) {
644 delete OS;
645 return Error(IDLoc, Twine("can't open secure log file: ") +
646 SecureLogFile + " (" + Err + ")");
647 }
648 getContext().setSecureLog(OS);
649 }
650
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000651 // Write the message.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000652 int CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
653 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
654 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
655 << LogMessage + "\n";
656
657 getContext().setSecureLogUsed(true);
658
659 return false;
660}
661
662/// ParseDirectiveSecureLogReset
663/// ::= .secure_log_reset
664bool DarwinAsmParser::ParseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
665 if (getLexer().isNot(AsmToken::EndOfStatement))
666 return TokError("unexpected token in '.secure_log_reset' directive");
667
668 Lex();
669
670 getContext().setSecureLogUsed(false);
671
672 return false;
673}
674
675/// ParseDirectiveSubsectionsViaSymbols
676/// ::= .subsections_via_symbols
677bool DarwinAsmParser::ParseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
678 if (getLexer().isNot(AsmToken::EndOfStatement))
679 return TokError("unexpected token in '.subsections_via_symbols' directive");
680
681 Lex();
682
683 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
684
685 return false;
686}
687
688/// ParseDirectiveTBSS
689/// ::= .tbss identifier, size, align
690bool DarwinAsmParser::ParseDirectiveTBSS(StringRef, SMLoc) {
691 SMLoc IDLoc = getLexer().getLoc();
692 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000693 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000694 return TokError("expected identifier in directive");
695
696 // Handle the identifier as the key symbol.
697 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
698
699 if (getLexer().isNot(AsmToken::Comma))
700 return TokError("unexpected token in directive");
701 Lex();
702
703 int64_t Size;
704 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000705 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000706 return true;
707
708 int64_t Pow2Alignment = 0;
709 SMLoc Pow2AlignmentLoc;
710 if (getLexer().is(AsmToken::Comma)) {
711 Lex();
712 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000713 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000714 return true;
715 }
716
717 if (getLexer().isNot(AsmToken::EndOfStatement))
718 return TokError("unexpected token in '.tbss' directive");
719
720 Lex();
721
722 if (Size < 0)
723 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
724 "zero");
725
726 // FIXME: Diagnose overflow.
727 if (Pow2Alignment < 0)
728 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
729 "than zero");
730
731 if (!Sym->isUndefined())
732 return Error(IDLoc, "invalid symbol redefinition");
733
734 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
735 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000736 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000737 0, SectionKind::getThreadBSS()),
738 Sym, Size, 1 << Pow2Alignment);
739
740 return false;
741}
742
743/// ParseDirectiveZerofill
744/// ::= .zerofill segname , sectname [, identifier , size_expression [
745/// , align_expression ]]
746bool DarwinAsmParser::ParseDirectiveZerofill(StringRef, SMLoc) {
747 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000748 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000749 return TokError("expected segment name after '.zerofill' directive");
750
751 if (getLexer().isNot(AsmToken::Comma))
752 return TokError("unexpected token in directive");
753 Lex();
754
755 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000756 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000757 return TokError("expected section name after comma in '.zerofill' "
758 "directive");
759
760 // If this is the end of the line all that was wanted was to create the
761 // the section but with no symbol.
762 if (getLexer().is(AsmToken::EndOfStatement)) {
763 // Create the zerofill section but no symbol
764 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000765 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000766 0, SectionKind::getBSS()));
767 return false;
768 }
769
770 if (getLexer().isNot(AsmToken::Comma))
771 return TokError("unexpected token in directive");
772 Lex();
773
774 SMLoc IDLoc = getLexer().getLoc();
775 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000776 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000777 return TokError("expected identifier in directive");
778
779 // handle the identifier as the key symbol.
780 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
781
782 if (getLexer().isNot(AsmToken::Comma))
783 return TokError("unexpected token in directive");
784 Lex();
785
786 int64_t Size;
787 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000788 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000789 return true;
790
791 int64_t Pow2Alignment = 0;
792 SMLoc Pow2AlignmentLoc;
793 if (getLexer().is(AsmToken::Comma)) {
794 Lex();
795 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000796 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000797 return true;
798 }
799
800 if (getLexer().isNot(AsmToken::EndOfStatement))
801 return TokError("unexpected token in '.zerofill' directive");
802
803 Lex();
804
805 if (Size < 0)
806 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
807 "than zero");
808
809 // NOTE: The alignment in the directive is a power of 2 value, the assembler
810 // may internally end up wanting an alignment in bytes.
811 // FIXME: Diagnose overflow.
812 if (Pow2Alignment < 0)
813 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
814 "can't be less than zero");
815
816 if (!Sym->isUndefined())
817 return Error(IDLoc, "invalid symbol redefinition");
818
819 // Create the zerofill Symbol with Size and Pow2Alignment
820 //
821 // FIXME: Arch specific.
822 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000823 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000824 0, SectionKind::getBSS()),
825 Sym, Size, 1 << Pow2Alignment);
826
827 return false;
828}
829
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000830/// ParseDirectiveDataRegion
831/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
832bool DarwinAsmParser::ParseDirectiveDataRegion(StringRef, SMLoc) {
833 if (getLexer().is(AsmToken::EndOfStatement)) {
834 Lex();
835 getStreamer().EmitDataRegion(MCDR_DataRegion);
836 return false;
837 }
838 StringRef RegionType;
839 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000840 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000841 return TokError("expected region type after '.data_region' directive");
842 int Kind = StringSwitch<int>(RegionType)
843 .Case("jt8", MCDR_DataRegionJT8)
844 .Case("jt16", MCDR_DataRegionJT16)
845 .Case("jt32", MCDR_DataRegionJT32)
846 .Default(-1);
847 if (Kind == -1)
848 return Error(Loc, "unknown region type in '.data_region' directive");
849 Lex();
850
851 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
852 return false;
853}
854
855/// ParseDirectiveDataRegionEnd
856/// ::= .end_data_region
857bool DarwinAsmParser::ParseDirectiveDataRegionEnd(StringRef, SMLoc) {
858 if (getLexer().isNot(AsmToken::EndOfStatement))
859 return TokError("unexpected token in '.end_data_region' directive");
860
861 Lex();
862 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
863 return false;
864}
865
Jim Grosbach448334a2014-03-18 22:09:05 +0000866/// ParseVersionMin
867/// ::= .ios_version_min major,minor[,update]
868/// ::= .macosx_version_min major,minor[,update]
869bool DarwinAsmParser::ParseVersionMin(StringRef Directive, SMLoc) {
870 int64_t Major = 0, Minor = 0, Update = 0;
871 int Kind = StringSwitch<int>(Directive)
872 .Case(".ios_version_min", MCVM_IOSVersionMin)
873 .Case(".macosx_version_min", MCVM_OSXVersionMin);
874 // Get the major version number.
875 if (getLexer().isNot(AsmToken::Integer))
876 return TokError("invalid OS major version number");
877 Major = getLexer().getTok().getIntVal();
878 if (Major > 65535 || Major <= 0)
879 return TokError("invalid OS major version number");
880 Lex();
881 if (getLexer().isNot(AsmToken::Comma))
882 return TokError("minor OS version number required, comma expected");
883 Lex();
884 // Get the minor version number.
885 if (getLexer().isNot(AsmToken::Integer))
886 return TokError("invalid OS minor version number");
887 Minor = getLexer().getTok().getIntVal();
888 if (Minor > 255 || Minor < 0)
889 return TokError("invalid OS minor version number");
890 Lex();
891 // Get the update level, if specified
892 if (getLexer().isNot(AsmToken::EndOfStatement)) {
893 if (getLexer().isNot(AsmToken::Comma))
894 return TokError("invalid update specifier, comma expected");
895 Lex();
896 if (getLexer().isNot(AsmToken::Integer))
897 return TokError("invalid OS update number");
898 Update = getLexer().getTok().getIntVal();
899 if (Update > 255 || Update < 0)
900 return TokError("invalid OS update number");
901 Lex();
902 }
903
904 // We've parsed a correct version specifier, so send it to the streamer.
905 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
906
907 return false;
908}
909
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000910namespace llvm {
911
912MCAsmParserExtension *createDarwinAsmParser() {
913 return new DarwinAsmParser;
914}
915
Bill Wendlingce4fe412012-08-08 06:30:30 +0000916} // end llvm namespace