blob: f033a1dfc7e9f57bc092a8392af106713423752d [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"
Rafael Espindolac49ac5e2015-12-16 23:49:14 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/StringSwitch.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000014#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Twine.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000016#include "llvm/MC/MCContext.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000017#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000020#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSymbol.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000023#include "llvm/Support/FileSystem.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000024#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/SourceMgr.h"
26using namespace llvm;
27
28namespace {
29
30/// \brief Implementation of directive handling which is shared across all
31/// Darwin targets.
32class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000033 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000034 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000035 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
36 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000037 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000038 }
39
Jim Grosbach319026d2014-03-18 22:09:10 +000040 bool parseSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000041 unsigned TAA = 0, unsigned ImplicitAlign = 0,
42 unsigned StubSize = 0);
43
Tim Northover2d4d1612015-10-28 22:36:05 +000044 SMLoc LastVersionMinDirective;
45
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000046public:
47 DarwinAsmParser() {}
48
Craig Topper59be68f2014-03-08 07:14:16 +000049 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000050 // Call the base implementation.
51 this->MCAsmParserExtension::Initialize(Parser);
52
Lang Hamesf9033bb2016-04-11 18:33:45 +000053 addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
Jim Grosbach319026d2014-03-18 22:09:10 +000054 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
55 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
Kevin Enderby3aeada22013-08-28 17:50:59 +000056 ".indirect_symbol");
Jim Grosbach319026d2014-03-18 22:09:10 +000057 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
58 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000059 ".subsections_via_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +000060 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
61 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
62 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
63 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000064 ".pushsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000065 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000066 ".popsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000067 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000069 ".secure_log_unique");
Jim Grosbach319026d2014-03-18 22:09:10 +000070 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000071 ".secure_log_reset");
Jim Grosbach319026d2014-03-18 22:09:10 +000072 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
73 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000074
Jim Grosbach319026d2014-03-18 22:09:10 +000075 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000076 ".data_region");
Jim Grosbach319026d2014-03-18 22:09:10 +000077 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000078 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000079
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000080 // Special section directives.
Jim Grosbach319026d2014-03-18 22:09:10 +000081 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
82 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
83 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000084 ".const_data");
Jim Grosbach319026d2014-03-18 22:09:10 +000085 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000086 ".constructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000087 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000088 ".cstring");
Jim Grosbach319026d2014-03-18 22:09:10 +000089 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
90 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000091 ".destructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000092 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
93 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000094 ".fvmlib_init0");
Jim Grosbach319026d2014-03-18 22:09:10 +000095 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000096 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000097 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +000098 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000099 ".lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000100 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +0000101 ".linker_option");
Jim Grosbach319026d2014-03-18 22:09:10 +0000102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000103 ".literal16");
Jim Grosbach319026d2014-03-18 22:09:10 +0000104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000105 ".literal4");
Jim Grosbach319026d2014-03-18 22:09:10 +0000106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000107 ".literal8");
Jim Grosbach319026d2014-03-18 22:09:10 +0000108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000109 ".mod_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000110 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000111 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000112 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000113 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000114 ".non_lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000116 ".objc_cat_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000118 ".objc_cat_inst_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000120 ".objc_category");
Jim Grosbach319026d2014-03-18 22:09:10 +0000121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000122 ".objc_class");
Jim Grosbach319026d2014-03-18 22:09:10 +0000123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000124 ".objc_class_names");
Jim Grosbach319026d2014-03-18 22:09:10 +0000125 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000126 ".objc_class_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000127 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000128 ".objc_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000129 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000130 ".objc_cls_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000131 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000132 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000133 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000134 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000135 ".objc_instance_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000136 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000137 ".objc_message_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000138 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000139 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000140 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000141 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000142 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000143 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000144 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000145 ".objc_meth_var_types");
Jim Grosbach319026d2014-03-18 22:09:10 +0000146 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000147 ".objc_module_info");
Jim Grosbach319026d2014-03-18 22:09:10 +0000148 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000149 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000150 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000151 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000152 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000153 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000154 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000155 ".objc_string_object");
Jim Grosbach319026d2014-03-18 22:09:10 +0000156 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000157 ".objc_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +0000158 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000159 ".picsymbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000160 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000161 ".static_const");
Jim Grosbach319026d2014-03-18 22:09:10 +0000162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000163 ".static_data");
Jim Grosbach319026d2014-03-18 22:09:10 +0000164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000165 ".symbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
167 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
168 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000169 ".thread_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000170 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000171
Jim Grosbach319026d2014-03-18 22:09:10 +0000172 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
Tim Northover2d4d1612015-10-28 22:36:05 +0000173 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
174 ".watchos_version_min");
175 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
Jim Grosbach319026d2014-03-18 22:09:10 +0000176 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
177 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
Jim Grosbach448334a2014-03-18 22:09:05 +0000178 ".macosx_version_min");
Tim Northover2d4d1612015-10-28 22:36:05 +0000179
180 LastVersionMinDirective = SMLoc();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000181 }
182
Lang Hamesf9033bb2016-04-11 18:33:45 +0000183 bool parseDirectiveAltEntry(StringRef, SMLoc);
Jim Grosbach319026d2014-03-18 22:09:10 +0000184 bool parseDirectiveDesc(StringRef, SMLoc);
185 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
186 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
187 bool parseDirectiveLsym(StringRef, SMLoc);
188 bool parseDirectiveLinkerOption(StringRef, SMLoc);
189 bool parseDirectiveSection(StringRef, SMLoc);
190 bool parseDirectivePushSection(StringRef, SMLoc);
191 bool parseDirectivePopSection(StringRef, SMLoc);
192 bool parseDirectivePrevious(StringRef, SMLoc);
193 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
194 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
195 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
196 bool parseDirectiveTBSS(StringRef, SMLoc);
197 bool parseDirectiveZerofill(StringRef, SMLoc);
198 bool parseDirectiveDataRegion(StringRef, SMLoc);
199 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000200
201 // Named Section Directive
Jim Grosbach319026d2014-03-18 22:09:10 +0000202 bool parseSectionDirectiveBss(StringRef, SMLoc) {
203 return parseSectionSwitch("__DATA", "__bss");
Rafael Espindola3402c052013-10-02 14:09:29 +0000204 }
205
Jim Grosbach319026d2014-03-18 22:09:10 +0000206 bool parseSectionDirectiveConst(StringRef, SMLoc) {
207 return parseSectionSwitch("__TEXT", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000208 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000209 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
210 return parseSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000211 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000212 bool parseSectionDirectiveCString(StringRef, SMLoc) {
213 return parseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000214 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000215 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000216 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
217 return parseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000218 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000219 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000220 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
221 return parseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000222 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000223 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000224 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
225 return parseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000226 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000227 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000228 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
229 return parseSectionSwitch("__TEXT","__constructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000230 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000231 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
232 return parseSectionSwitch("__TEXT","__destructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000233 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000234 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
235 return parseSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000236 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000237 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
238 return parseSectionSwitch("__TEXT","__fvmlib_init1");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000239 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000240 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
241 return parseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000242 MachO::S_SYMBOL_STUBS |
243 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000244 // FIXME: Different on PPC and ARM.
245 0, 16);
246 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000247 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
248 return parseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000249 MachO::S_SYMBOL_STUBS |
250 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000251 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000252 bool parseSectionDirectiveData(StringRef, SMLoc) {
253 return parseSectionSwitch("__DATA", "__data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000254 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000255 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
256 return parseSectionSwitch("__DATA", "__static_data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000257 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000258 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
259 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000260 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000261 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000262 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
263 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000264 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000265 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000266 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
267 return parseSectionSwitch("__DATA", "__dyld");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000268 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000269 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
270 return parseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000271 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000272 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000273 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
274 return parseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000275 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000276 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000277 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
278 return parseSectionSwitch("__DATA", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000279 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000280 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
281 return parseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000282 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000283 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000284 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
285 return parseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000286 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000287 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000288 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
289 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000290 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000291 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000292 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
293 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000294 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000295 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000296 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
297 return parseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000298 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000299 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000300 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
301 return parseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000302 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000303 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000304 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
305 return parseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000306 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000307 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000308 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
309 return parseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000310 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000311 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000312 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
313 return parseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000314 MachO::S_ATTR_NO_DEAD_STRIP |
315 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000316 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000317 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
318 return parseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000319 MachO::S_ATTR_NO_DEAD_STRIP |
320 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000321 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000322 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
323 return parseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000324 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000325 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000326 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
327 return parseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000328 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000329 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000330 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
331 return parseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000332 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000333 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000334 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
335 return parseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000336 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000337 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000338 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
339 return parseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000340 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000341 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000342 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
343 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000344 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000345 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000346 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
347 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000348 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000349 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000350 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
351 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000352 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000353 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000354 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
355 return parseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000356 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000357 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000358 bool parseSectionDirectiveTData(StringRef, SMLoc) {
359 return parseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000360 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000361 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000362 bool parseSectionDirectiveText(StringRef, SMLoc) {
363 return parseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000364 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000365 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000366 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
367 return parseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000368 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000369 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000370 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
Jim Grosbach14be61a2011-03-08 19:17:19 +0000371 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000372 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000373 return false;
374 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000375 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
376 return parseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000377 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000378 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000379 bool parseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000380
381};
382
Bill Wendlingce4fe412012-08-08 06:30:30 +0000383} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000384
Jim Grosbach319026d2014-03-18 22:09:10 +0000385bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000386 const char *Section,
387 unsigned TAA, unsigned Align,
388 unsigned StubSize) {
389 if (getLexer().isNot(AsmToken::EndOfStatement))
390 return TokError("unexpected token in section switching directive");
391 Lex();
392
393 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000394 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000395 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000396 Segment, Section, TAA, StubSize,
397 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000398
399 // Set the implicit alignment, if any.
400 //
401 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
402 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000403 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000404 // the section. However, this is arguably more reasonable behavior, and there
405 // is no good reason for someone to intentionally emit incorrectly sized
406 // values into the implicitly aligned sections.
407 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000408 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000409
410 return false;
411}
412
Lang Hamesf9033bb2016-04-11 18:33:45 +0000413/// parseDirectiveAltEntry
414/// ::= .alt_entry identifier
415bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
416 StringRef Name;
417 if (getParser().parseIdentifier(Name))
418 return TokError("expected identifier in directive");
419
420 // Look up symbol.
421 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
422
423 if (Sym->isDefined())
424 return TokError(".alt_entry must preceed symbol definition");
425
426 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
427 return TokError("unable to emit symbol attribute");
428
429 Lex();
430 return false;
431}
432
Jim Grosbach319026d2014-03-18 22:09:10 +0000433/// parseDirectiveDesc
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000434/// ::= .desc identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000435bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000436 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000437 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000438 return TokError("expected identifier in directive");
439
440 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000441 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000442
443 if (getLexer().isNot(AsmToken::Comma))
444 return TokError("unexpected token in '.desc' directive");
445 Lex();
446
447 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000448 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000449 return true;
450
451 if (getLexer().isNot(AsmToken::EndOfStatement))
452 return TokError("unexpected token in '.desc' directive");
453
454 Lex();
455
456 // Set the n_desc field of this Symbol to this DescValue
457 getStreamer().EmitSymbolDesc(Sym, DescValue);
458
459 return false;
460}
461
Jim Grosbach319026d2014-03-18 22:09:10 +0000462/// parseDirectiveIndirectSymbol
Kevin Enderby3aeada22013-08-28 17:50:59 +0000463/// ::= .indirect_symbol identifier
Jim Grosbach319026d2014-03-18 22:09:10 +0000464bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
Kevin Enderby3aeada22013-08-28 17:50:59 +0000465 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
466 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000467 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000468 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
469 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
470 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000471 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
472 "section");
473
474 StringRef Name;
475 if (getParser().parseIdentifier(Name))
476 return TokError("expected identifier in .indirect_symbol directive");
477
Jim Grosbach6f482002015-05-18 18:43:14 +0000478 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000479
480 // Assembler local symbols don't make any sense here. Complain loudly.
481 if (Sym->isTemporary())
482 return TokError("non-local symbol required in directive");
483
484 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
485 return TokError("unable to emit indirect symbol attribute for: " + Name);
486
487 if (getLexer().isNot(AsmToken::EndOfStatement))
488 return TokError("unexpected token in '.indirect_symbol' directive");
489
490 Lex();
491
492 return false;
493}
494
Jim Grosbach319026d2014-03-18 22:09:10 +0000495/// parseDirectiveDumpOrLoad
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000496/// ::= ( .dump | .load ) "filename"
Jim Grosbach319026d2014-03-18 22:09:10 +0000497bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000498 SMLoc IDLoc) {
499 bool IsDump = Directive == ".dump";
500 if (getLexer().isNot(AsmToken::String))
501 return TokError("expected string in '.dump' or '.load' directive");
502
503 Lex();
504
505 if (getLexer().isNot(AsmToken::EndOfStatement))
506 return TokError("unexpected token in '.dump' or '.load' directive");
507
508 Lex();
509
510 // FIXME: If/when .dump and .load are implemented they will be done in the
511 // the assembly parser and not have any need for an MCStreamer API.
512 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000513 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000514 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000515 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000516}
517
Daniel Dunbar16004b82013-01-18 01:25:48 +0000518/// ParseDirectiveLinkerOption
519/// ::= .linker_option "string" ( , "string" )*
Jim Grosbach319026d2014-03-18 22:09:10 +0000520bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000521 SmallVector<std::string, 4> Args;
522 for (;;) {
523 if (getLexer().isNot(AsmToken::String))
524 return TokError("expected string in '" + Twine(IDVal) + "' directive");
525
526 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000527 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000528 return true;
529
530 Args.push_back(Data);
531
532 Lex();
533 if (getLexer().is(AsmToken::EndOfStatement))
534 break;
535
536 if (getLexer().isNot(AsmToken::Comma))
537 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
538 Lex();
539 }
540
541 getStreamer().EmitLinkerOptions(Args);
542 return false;
543}
544
Jim Grosbach319026d2014-03-18 22:09:10 +0000545/// parseDirectiveLsym
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000546/// ::= .lsym identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000547bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000548 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000549 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000550 return TokError("expected identifier in directive");
551
552 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000553 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000554
555 if (getLexer().isNot(AsmToken::Comma))
556 return TokError("unexpected token in '.lsym' directive");
557 Lex();
558
559 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000560 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000561 return true;
562
563 if (getLexer().isNot(AsmToken::EndOfStatement))
564 return TokError("unexpected token in '.lsym' directive");
565
566 Lex();
567
568 // We don't currently support this directive.
569 //
570 // FIXME: Diagnostic location!
571 (void) Sym;
572 return TokError("directive '.lsym' is unsupported");
573}
574
Jim Grosbach319026d2014-03-18 22:09:10 +0000575/// parseDirectiveSection:
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000576/// ::= .section identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000577bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000578 SMLoc Loc = getLexer().getLoc();
579
580 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000581 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000582 return Error(Loc, "expected identifier after '.section' directive");
583
584 // Verify there is a following comma.
585 if (!getLexer().is(AsmToken::Comma))
586 return TokError("unexpected token in '.section' directive");
587
588 std::string SectionSpec = SectionName;
589 SectionSpec += ",";
590
591 // Add all the tokens until the end of the line, ParseSectionSpecifier will
592 // handle this.
593 StringRef EOL = getLexer().LexUntilEndOfStatement();
594 SectionSpec.append(EOL.begin(), EOL.end());
595
596 Lex();
597 if (getLexer().isNot(AsmToken::EndOfStatement))
598 return TokError("unexpected token in '.section' directive");
599 Lex();
600
601
602 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000603 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000604 unsigned TAA;
605 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000606 std::string ErrorStr =
607 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000608 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000609
610 if (!ErrorStr.empty())
611 return Error(Loc, ErrorStr.c_str());
612
Akira Hatanaka8ad73992015-10-15 05:28:38 +0000613 // Issue a warning if the target is not powerpc and Section is a *coal* section.
614 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
615 Triple::ArchType ArchTy = TT.getArch();
616
617 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
618 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
619 .Case("__textcoal_nt", "__text")
620 .Case("__const_coal", "__const")
621 .Case("__datacoal_nt", "__data")
622 .Default(Section);
623
624 if (!Section.equals(NonCoalSection)) {
625 StringRef SectionVal(Loc.getPointer());
626 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
627 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
628 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
629 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
630 SMRange(BLoc, ELoc));
631 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
632 "\"", SMRange(BLoc, ELoc));
633 }
634 }
635
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000636 // FIXME: Arch specific.
637 bool isText = Segment == "__TEXT"; // FIXME: Hack.
638 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000639 Segment, Section, TAA, StubSize,
640 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000641 return false;
642}
643
Bill Wendlingce4fe412012-08-08 06:30:30 +0000644/// ParseDirectivePushSection:
645/// ::= .pushsection identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000646bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000647 getStreamer().PushSection();
648
Jim Grosbach319026d2014-03-18 22:09:10 +0000649 if (parseDirectiveSection(S, Loc)) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000650 getStreamer().PopSection();
651 return true;
652 }
653
654 return false;
655}
656
657/// ParseDirectivePopSection:
658/// ::= .popsection
Jim Grosbach319026d2014-03-18 22:09:10 +0000659bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000660 if (!getStreamer().PopSection())
661 return TokError(".popsection without corresponding .pushsection");
662 return false;
663}
664
665/// ParseDirectivePrevious:
666/// ::= .previous
Jim Grosbach319026d2014-03-18 22:09:10 +0000667bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000668 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000669 if (!PreviousSection.first)
670 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000671 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000672 return false;
673}
674
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000675/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000676/// ::= .secure_log_unique ... message ...
Jim Grosbach319026d2014-03-18 22:09:10 +0000677bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000678 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000679 if (getLexer().isNot(AsmToken::EndOfStatement))
680 return TokError("unexpected token in '.secure_log_unique' directive");
681
David Blaikiedc3f01e2015-03-09 01:57:13 +0000682 if (getContext().getSecureLogUsed())
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000683 return Error(IDLoc, ".secure_log_unique specified multiple times");
684
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000685 // Get the secure log path.
686 const char *SecureLogFile = getContext().getSecureLogFile();
Craig Topper353eda42014-04-24 06:44:33 +0000687 if (!SecureLogFile)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000688 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
689 "environment variable unset.");
690
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000691 // Open the secure log file if we haven't already.
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000692 raw_fd_ostream *OS = getContext().getSecureLog();
Craig Topper353eda42014-04-24 06:44:33 +0000693 if (!OS) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000694 std::error_code EC;
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000695 auto NewOS = llvm::make_unique<raw_fd_ostream>(
696 SecureLogFile, EC, sys::fs::F_Append | sys::fs::F_Text);
697 if (EC)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000698 return Error(IDLoc, Twine("can't open secure log file: ") +
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000699 SecureLogFile + " (" + EC.message() + ")");
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000700 OS = NewOS.get();
701 getContext().setSecureLog(std::move(NewOS));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000702 }
703
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000704 // Write the message.
Alp Tokera55b95b2014-07-06 10:33:31 +0000705 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000706 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
707 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
708 << LogMessage + "\n";
709
710 getContext().setSecureLogUsed(true);
711
712 return false;
713}
714
715/// ParseDirectiveSecureLogReset
716/// ::= .secure_log_reset
Jim Grosbach319026d2014-03-18 22:09:10 +0000717bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000718 if (getLexer().isNot(AsmToken::EndOfStatement))
719 return TokError("unexpected token in '.secure_log_reset' directive");
720
721 Lex();
722
723 getContext().setSecureLogUsed(false);
724
725 return false;
726}
727
Jim Grosbach319026d2014-03-18 22:09:10 +0000728/// parseDirectiveSubsectionsViaSymbols
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000729/// ::= .subsections_via_symbols
Jim Grosbach319026d2014-03-18 22:09:10 +0000730bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000731 if (getLexer().isNot(AsmToken::EndOfStatement))
732 return TokError("unexpected token in '.subsections_via_symbols' directive");
733
734 Lex();
735
736 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
737
738 return false;
739}
740
741/// ParseDirectiveTBSS
742/// ::= .tbss identifier, size, align
Jim Grosbach319026d2014-03-18 22:09:10 +0000743bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000744 SMLoc IDLoc = getLexer().getLoc();
745 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000746 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000747 return TokError("expected identifier in directive");
748
749 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000750 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000751
752 if (getLexer().isNot(AsmToken::Comma))
753 return TokError("unexpected token in directive");
754 Lex();
755
756 int64_t Size;
757 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000758 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000759 return true;
760
761 int64_t Pow2Alignment = 0;
762 SMLoc Pow2AlignmentLoc;
763 if (getLexer().is(AsmToken::Comma)) {
764 Lex();
765 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000766 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000767 return true;
768 }
769
770 if (getLexer().isNot(AsmToken::EndOfStatement))
771 return TokError("unexpected token in '.tbss' directive");
772
773 Lex();
774
775 if (Size < 0)
776 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
777 "zero");
778
779 // FIXME: Diagnose overflow.
780 if (Pow2Alignment < 0)
781 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
782 "than zero");
783
784 if (!Sym->isUndefined())
785 return Error(IDLoc, "invalid symbol redefinition");
786
787 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
788 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000789 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000790 0, SectionKind::getThreadBSS()),
791 Sym, Size, 1 << Pow2Alignment);
792
793 return false;
794}
795
796/// ParseDirectiveZerofill
797/// ::= .zerofill segname , sectname [, identifier , size_expression [
798/// , align_expression ]]
Jim Grosbach319026d2014-03-18 22:09:10 +0000799bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000800 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000801 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000802 return TokError("expected segment name after '.zerofill' directive");
803
804 if (getLexer().isNot(AsmToken::Comma))
805 return TokError("unexpected token in directive");
806 Lex();
807
808 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000809 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000810 return TokError("expected section name after comma in '.zerofill' "
811 "directive");
812
813 // If this is the end of the line all that was wanted was to create the
814 // the section but with no symbol.
815 if (getLexer().is(AsmToken::EndOfStatement)) {
816 // Create the zerofill section but no symbol
817 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000818 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000819 0, SectionKind::getBSS()));
820 return false;
821 }
822
823 if (getLexer().isNot(AsmToken::Comma))
824 return TokError("unexpected token in directive");
825 Lex();
826
827 SMLoc IDLoc = getLexer().getLoc();
828 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000829 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000830 return TokError("expected identifier in directive");
831
832 // handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000833 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000834
835 if (getLexer().isNot(AsmToken::Comma))
836 return TokError("unexpected token in directive");
837 Lex();
838
839 int64_t Size;
840 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000841 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000842 return true;
843
844 int64_t Pow2Alignment = 0;
845 SMLoc Pow2AlignmentLoc;
846 if (getLexer().is(AsmToken::Comma)) {
847 Lex();
848 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000849 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000850 return true;
851 }
852
853 if (getLexer().isNot(AsmToken::EndOfStatement))
854 return TokError("unexpected token in '.zerofill' directive");
855
856 Lex();
857
858 if (Size < 0)
859 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
860 "than zero");
861
862 // NOTE: The alignment in the directive is a power of 2 value, the assembler
863 // may internally end up wanting an alignment in bytes.
864 // FIXME: Diagnose overflow.
865 if (Pow2Alignment < 0)
866 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
867 "can't be less than zero");
868
869 if (!Sym->isUndefined())
870 return Error(IDLoc, "invalid symbol redefinition");
871
872 // Create the zerofill Symbol with Size and Pow2Alignment
873 //
874 // FIXME: Arch specific.
875 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000876 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000877 0, SectionKind::getBSS()),
878 Sym, Size, 1 << Pow2Alignment);
879
880 return false;
881}
882
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000883/// ParseDirectiveDataRegion
884/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
Jim Grosbach319026d2014-03-18 22:09:10 +0000885bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000886 if (getLexer().is(AsmToken::EndOfStatement)) {
887 Lex();
888 getStreamer().EmitDataRegion(MCDR_DataRegion);
889 return false;
890 }
891 StringRef RegionType;
892 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000893 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000894 return TokError("expected region type after '.data_region' directive");
895 int Kind = StringSwitch<int>(RegionType)
896 .Case("jt8", MCDR_DataRegionJT8)
897 .Case("jt16", MCDR_DataRegionJT16)
898 .Case("jt32", MCDR_DataRegionJT32)
899 .Default(-1);
900 if (Kind == -1)
901 return Error(Loc, "unknown region type in '.data_region' directive");
902 Lex();
903
904 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
905 return false;
906}
907
908/// ParseDirectiveDataRegionEnd
909/// ::= .end_data_region
Jim Grosbach319026d2014-03-18 22:09:10 +0000910bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000911 if (getLexer().isNot(AsmToken::EndOfStatement))
912 return TokError("unexpected token in '.end_data_region' directive");
913
914 Lex();
915 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
916 return false;
917}
918
Jim Grosbach319026d2014-03-18 22:09:10 +0000919/// parseVersionMin
Jim Grosbach448334a2014-03-18 22:09:05 +0000920/// ::= .ios_version_min major,minor[,update]
921/// ::= .macosx_version_min major,minor[,update]
Tim Northover2d4d1612015-10-28 22:36:05 +0000922bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
Jim Grosbach448334a2014-03-18 22:09:05 +0000923 int64_t Major = 0, Minor = 0, Update = 0;
924 int Kind = StringSwitch<int>(Directive)
Tim Northover2d4d1612015-10-28 22:36:05 +0000925 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
926 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
Jim Grosbach448334a2014-03-18 22:09:05 +0000927 .Case(".ios_version_min", MCVM_IOSVersionMin)
928 .Case(".macosx_version_min", MCVM_OSXVersionMin);
929 // Get the major version number.
930 if (getLexer().isNot(AsmToken::Integer))
931 return TokError("invalid OS major version number");
932 Major = getLexer().getTok().getIntVal();
933 if (Major > 65535 || Major <= 0)
934 return TokError("invalid OS major version number");
935 Lex();
936 if (getLexer().isNot(AsmToken::Comma))
937 return TokError("minor OS version number required, comma expected");
938 Lex();
939 // Get the minor version number.
940 if (getLexer().isNot(AsmToken::Integer))
941 return TokError("invalid OS minor version number");
942 Minor = getLexer().getTok().getIntVal();
943 if (Minor > 255 || Minor < 0)
944 return TokError("invalid OS minor version number");
945 Lex();
946 // Get the update level, if specified
947 if (getLexer().isNot(AsmToken::EndOfStatement)) {
948 if (getLexer().isNot(AsmToken::Comma))
949 return TokError("invalid update specifier, comma expected");
950 Lex();
951 if (getLexer().isNot(AsmToken::Integer))
952 return TokError("invalid OS update number");
953 Update = getLexer().getTok().getIntVal();
954 if (Update > 255 || Update < 0)
955 return TokError("invalid OS update number");
956 Lex();
957 }
958
Tim Northover2d4d1612015-10-28 22:36:05 +0000959 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
960 Triple::OSType ExpectedOS = Triple::UnknownOS;
961 switch ((MCVersionMinType)Kind) {
962 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
963 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
964 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
965 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
966 }
967 if (T.getOS() != ExpectedOS)
968 Warning(Loc, Directive + " should only be used for " +
969 Triple::getOSTypeName(ExpectedOS) + " targets");
970
971 if (LastVersionMinDirective.isValid()) {
972 Warning(Loc, "overriding previous version_min directive");
973 Note(LastVersionMinDirective, "previous definition is here");
974 }
975 LastVersionMinDirective = Loc;
976
Jim Grosbach448334a2014-03-18 22:09:05 +0000977 // We've parsed a correct version specifier, so send it to the streamer.
978 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
979
980 return false;
981}
982
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000983namespace llvm {
984
985MCAsmParserExtension *createDarwinAsmParser() {
986 return new DarwinAsmParser;
987}
988
Bill Wendlingce4fe412012-08-08 06:30:30 +0000989} // end llvm namespace