blob: 582d43623e83aa17810e90cc9e7bd6c5ea334bee [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"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000013#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/Twine.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000015#include "llvm/MC/MCContext.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000016#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000019#include "llvm/MC/MCSectionMachO.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCSymbol.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000022#include "llvm/Support/FileSystem.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/SourceMgr.h"
25using namespace llvm;
26
27namespace {
28
29/// \brief Implementation of directive handling which is shared across all
30/// Darwin targets.
31class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000032 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000033 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000034 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
35 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000036 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000037 }
38
Jim Grosbach319026d2014-03-18 22:09:10 +000039 bool parseSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000040 unsigned TAA = 0, unsigned ImplicitAlign = 0,
41 unsigned StubSize = 0);
42
Tim Northover2d4d1612015-10-28 22:36:05 +000043 SMLoc LastVersionMinDirective;
44
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000045public:
46 DarwinAsmParser() {}
47
Craig Topper59be68f2014-03-08 07:14:16 +000048 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000049 // Call the base implementation.
50 this->MCAsmParserExtension::Initialize(Parser);
51
Jim Grosbach319026d2014-03-18 22:09:10 +000052 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
53 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
Kevin Enderby3aeada22013-08-28 17:50:59 +000054 ".indirect_symbol");
Jim Grosbach319026d2014-03-18 22:09:10 +000055 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
56 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000057 ".subsections_via_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +000058 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
59 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
60 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
61 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000062 ".pushsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000063 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000064 ".popsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000065 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
66 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000067 ".secure_log_unique");
Jim Grosbach319026d2014-03-18 22:09:10 +000068 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000069 ".secure_log_reset");
Jim Grosbach319026d2014-03-18 22:09:10 +000070 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000072
Jim Grosbach319026d2014-03-18 22:09:10 +000073 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000074 ".data_region");
Jim Grosbach319026d2014-03-18 22:09:10 +000075 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000076 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000077
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000078 // Special section directives.
Jim Grosbach319026d2014-03-18 22:09:10 +000079 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
80 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
81 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000082 ".const_data");
Jim Grosbach319026d2014-03-18 22:09:10 +000083 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000084 ".constructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000085 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000086 ".cstring");
Jim Grosbach319026d2014-03-18 22:09:10 +000087 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
88 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000089 ".destructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000090 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
91 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000092 ".fvmlib_init0");
Jim Grosbach319026d2014-03-18 22:09:10 +000093 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000094 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000095 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +000096 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000097 ".lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +000098 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +000099 ".linker_option");
Jim Grosbach319026d2014-03-18 22:09:10 +0000100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000101 ".literal16");
Jim Grosbach319026d2014-03-18 22:09:10 +0000102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000103 ".literal4");
Jim Grosbach319026d2014-03-18 22:09:10 +0000104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000105 ".literal8");
Jim Grosbach319026d2014-03-18 22:09:10 +0000106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000107 ".mod_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000109 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000110 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000111 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000112 ".non_lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000113 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000114 ".objc_cat_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000116 ".objc_cat_inst_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000118 ".objc_category");
Jim Grosbach319026d2014-03-18 22:09:10 +0000119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000120 ".objc_class");
Jim Grosbach319026d2014-03-18 22:09:10 +0000121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000122 ".objc_class_names");
Jim Grosbach319026d2014-03-18 22:09:10 +0000123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000124 ".objc_class_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000125 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000126 ".objc_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000127 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000128 ".objc_cls_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000129 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000130 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000131 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000132 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000133 ".objc_instance_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000134 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000135 ".objc_message_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000136 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000137 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000138 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000139 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000140 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000141 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000142 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000143 ".objc_meth_var_types");
Jim Grosbach319026d2014-03-18 22:09:10 +0000144 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000145 ".objc_module_info");
Jim Grosbach319026d2014-03-18 22:09:10 +0000146 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000147 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000148 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000149 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000150 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000151 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000152 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000153 ".objc_string_object");
Jim Grosbach319026d2014-03-18 22:09:10 +0000154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000155 ".objc_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +0000156 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000157 ".picsymbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000158 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000159 ".static_const");
Jim Grosbach319026d2014-03-18 22:09:10 +0000160 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000161 ".static_data");
Jim Grosbach319026d2014-03-18 22:09:10 +0000162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000163 ".symbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
165 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000167 ".thread_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000168 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000169
Jim Grosbach319026d2014-03-18 22:09:10 +0000170 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
Tim Northover2d4d1612015-10-28 22:36:05 +0000171 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
172 ".watchos_version_min");
173 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
Jim Grosbach319026d2014-03-18 22:09:10 +0000174 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
175 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
Jim Grosbach448334a2014-03-18 22:09:05 +0000176 ".macosx_version_min");
Tim Northover2d4d1612015-10-28 22:36:05 +0000177
178 LastVersionMinDirective = SMLoc();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000179 }
180
Jim Grosbach319026d2014-03-18 22:09:10 +0000181 bool parseDirectiveDesc(StringRef, SMLoc);
182 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
183 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
184 bool parseDirectiveLsym(StringRef, SMLoc);
185 bool parseDirectiveLinkerOption(StringRef, SMLoc);
186 bool parseDirectiveSection(StringRef, SMLoc);
187 bool parseDirectivePushSection(StringRef, SMLoc);
188 bool parseDirectivePopSection(StringRef, SMLoc);
189 bool parseDirectivePrevious(StringRef, SMLoc);
190 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
191 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
192 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
193 bool parseDirectiveTBSS(StringRef, SMLoc);
194 bool parseDirectiveZerofill(StringRef, SMLoc);
195 bool parseDirectiveDataRegion(StringRef, SMLoc);
196 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000197
198 // Named Section Directive
Jim Grosbach319026d2014-03-18 22:09:10 +0000199 bool parseSectionDirectiveBss(StringRef, SMLoc) {
200 return parseSectionSwitch("__DATA", "__bss");
Rafael Espindola3402c052013-10-02 14:09:29 +0000201 }
202
Jim Grosbach319026d2014-03-18 22:09:10 +0000203 bool parseSectionDirectiveConst(StringRef, SMLoc) {
204 return parseSectionSwitch("__TEXT", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000205 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000206 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
207 return parseSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000208 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000209 bool parseSectionDirectiveCString(StringRef, SMLoc) {
210 return parseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000211 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000212 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000213 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
214 return parseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000215 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000216 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000217 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
218 return parseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000219 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000220 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000221 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
222 return parseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000223 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000224 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000225 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
226 return parseSectionSwitch("__TEXT","__constructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000227 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000228 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
229 return parseSectionSwitch("__TEXT","__destructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000230 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000231 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
232 return parseSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000233 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000234 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
235 return parseSectionSwitch("__TEXT","__fvmlib_init1");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000236 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000237 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
238 return parseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000239 MachO::S_SYMBOL_STUBS |
240 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000241 // FIXME: Different on PPC and ARM.
242 0, 16);
243 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000244 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
245 return parseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000246 MachO::S_SYMBOL_STUBS |
247 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000248 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000249 bool parseSectionDirectiveData(StringRef, SMLoc) {
250 return parseSectionSwitch("__DATA", "__data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000251 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000252 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
253 return parseSectionSwitch("__DATA", "__static_data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000254 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000255 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
256 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000257 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000258 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000259 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
260 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000261 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000262 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000263 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
264 return parseSectionSwitch("__DATA", "__dyld");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000265 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000266 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
267 return parseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000268 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000269 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000270 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
271 return parseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000272 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000273 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000274 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
275 return parseSectionSwitch("__DATA", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000276 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000277 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
278 return parseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000279 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000280 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000281 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
282 return parseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000283 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000284 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000285 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
286 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000287 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000288 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000289 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
290 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000291 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000292 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000293 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
294 return parseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000295 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000296 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000297 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
298 return parseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000299 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000300 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000301 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
302 return parseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000303 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000304 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000305 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
306 return parseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000307 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000308 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000309 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
310 return parseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000311 MachO::S_ATTR_NO_DEAD_STRIP |
312 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000313 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000314 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
315 return parseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000316 MachO::S_ATTR_NO_DEAD_STRIP |
317 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000318 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000319 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
320 return parseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000321 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000322 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000323 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
324 return parseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000325 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000326 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000327 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
328 return parseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000329 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000330 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000331 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
332 return parseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000333 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000334 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000335 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
336 return parseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000337 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000338 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000339 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
340 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000341 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000342 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000343 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
344 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000345 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000346 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000347 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
348 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000349 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000350 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000351 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
352 return parseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000353 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000354 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000355 bool parseSectionDirectiveTData(StringRef, SMLoc) {
356 return parseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000357 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000358 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000359 bool parseSectionDirectiveText(StringRef, SMLoc) {
360 return parseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000361 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000362 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000363 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
364 return parseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000365 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000366 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000367 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
Jim Grosbach14be61a2011-03-08 19:17:19 +0000368 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000369 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000370 return false;
371 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000372 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
373 return parseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000374 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000375 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000376 bool parseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000377
378};
379
Bill Wendlingce4fe412012-08-08 06:30:30 +0000380} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000381
Jim Grosbach319026d2014-03-18 22:09:10 +0000382bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000383 const char *Section,
384 unsigned TAA, unsigned Align,
385 unsigned StubSize) {
386 if (getLexer().isNot(AsmToken::EndOfStatement))
387 return TokError("unexpected token in section switching directive");
388 Lex();
389
390 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000391 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000392 getStreamer().SwitchSection(getContext().getMachOSection(
393 Segment, Section, TAA, StubSize,
394 isText ? SectionKind::getText()
395 : SectionKind::getDataRel()));
396
397 // Set the implicit alignment, if any.
398 //
399 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
400 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000401 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000402 // the section. However, this is arguably more reasonable behavior, and there
403 // is no good reason for someone to intentionally emit incorrectly sized
404 // values into the implicitly aligned sections.
405 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000406 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000407
408 return false;
409}
410
Jim Grosbach319026d2014-03-18 22:09:10 +0000411/// parseDirectiveDesc
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000412/// ::= .desc identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000413bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000414 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000415 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000416 return TokError("expected identifier in directive");
417
418 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000419 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000420
421 if (getLexer().isNot(AsmToken::Comma))
422 return TokError("unexpected token in '.desc' directive");
423 Lex();
424
425 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000426 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000427 return true;
428
429 if (getLexer().isNot(AsmToken::EndOfStatement))
430 return TokError("unexpected token in '.desc' directive");
431
432 Lex();
433
434 // Set the n_desc field of this Symbol to this DescValue
435 getStreamer().EmitSymbolDesc(Sym, DescValue);
436
437 return false;
438}
439
Jim Grosbach319026d2014-03-18 22:09:10 +0000440/// parseDirectiveIndirectSymbol
Kevin Enderby3aeada22013-08-28 17:50:59 +0000441/// ::= .indirect_symbol identifier
Jim Grosbach319026d2014-03-18 22:09:10 +0000442bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
Kevin Enderby3aeada22013-08-28 17:50:59 +0000443 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
444 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000445 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000446 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
447 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
448 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000449 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
450 "section");
451
452 StringRef Name;
453 if (getParser().parseIdentifier(Name))
454 return TokError("expected identifier in .indirect_symbol directive");
455
Jim Grosbach6f482002015-05-18 18:43:14 +0000456 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000457
458 // Assembler local symbols don't make any sense here. Complain loudly.
459 if (Sym->isTemporary())
460 return TokError("non-local symbol required in directive");
461
462 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
463 return TokError("unable to emit indirect symbol attribute for: " + Name);
464
465 if (getLexer().isNot(AsmToken::EndOfStatement))
466 return TokError("unexpected token in '.indirect_symbol' directive");
467
468 Lex();
469
470 return false;
471}
472
Jim Grosbach319026d2014-03-18 22:09:10 +0000473/// parseDirectiveDumpOrLoad
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000474/// ::= ( .dump | .load ) "filename"
Jim Grosbach319026d2014-03-18 22:09:10 +0000475bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000476 SMLoc IDLoc) {
477 bool IsDump = Directive == ".dump";
478 if (getLexer().isNot(AsmToken::String))
479 return TokError("expected string in '.dump' or '.load' directive");
480
481 Lex();
482
483 if (getLexer().isNot(AsmToken::EndOfStatement))
484 return TokError("unexpected token in '.dump' or '.load' directive");
485
486 Lex();
487
488 // FIXME: If/when .dump and .load are implemented they will be done in the
489 // the assembly parser and not have any need for an MCStreamer API.
490 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000491 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000492 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000493 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000494}
495
Daniel Dunbar16004b82013-01-18 01:25:48 +0000496/// ParseDirectiveLinkerOption
497/// ::= .linker_option "string" ( , "string" )*
Jim Grosbach319026d2014-03-18 22:09:10 +0000498bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000499 SmallVector<std::string, 4> Args;
500 for (;;) {
501 if (getLexer().isNot(AsmToken::String))
502 return TokError("expected string in '" + Twine(IDVal) + "' directive");
503
504 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000505 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000506 return true;
507
508 Args.push_back(Data);
509
510 Lex();
511 if (getLexer().is(AsmToken::EndOfStatement))
512 break;
513
514 if (getLexer().isNot(AsmToken::Comma))
515 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
516 Lex();
517 }
518
519 getStreamer().EmitLinkerOptions(Args);
520 return false;
521}
522
Jim Grosbach319026d2014-03-18 22:09:10 +0000523/// parseDirectiveLsym
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000524/// ::= .lsym identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000525bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000526 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000527 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000528 return TokError("expected identifier in directive");
529
530 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000531 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000532
533 if (getLexer().isNot(AsmToken::Comma))
534 return TokError("unexpected token in '.lsym' directive");
535 Lex();
536
537 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000538 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000539 return true;
540
541 if (getLexer().isNot(AsmToken::EndOfStatement))
542 return TokError("unexpected token in '.lsym' directive");
543
544 Lex();
545
546 // We don't currently support this directive.
547 //
548 // FIXME: Diagnostic location!
549 (void) Sym;
550 return TokError("directive '.lsym' is unsupported");
551}
552
Jim Grosbach319026d2014-03-18 22:09:10 +0000553/// parseDirectiveSection:
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000554/// ::= .section identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000555bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000556 SMLoc Loc = getLexer().getLoc();
557
558 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000559 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000560 return Error(Loc, "expected identifier after '.section' directive");
561
562 // Verify there is a following comma.
563 if (!getLexer().is(AsmToken::Comma))
564 return TokError("unexpected token in '.section' directive");
565
566 std::string SectionSpec = SectionName;
567 SectionSpec += ",";
568
569 // Add all the tokens until the end of the line, ParseSectionSpecifier will
570 // handle this.
571 StringRef EOL = getLexer().LexUntilEndOfStatement();
572 SectionSpec.append(EOL.begin(), EOL.end());
573
574 Lex();
575 if (getLexer().isNot(AsmToken::EndOfStatement))
576 return TokError("unexpected token in '.section' directive");
577 Lex();
578
579
580 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000581 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000582 unsigned TAA;
583 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000584 std::string ErrorStr =
585 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000586 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000587
588 if (!ErrorStr.empty())
589 return Error(Loc, ErrorStr.c_str());
590
Akira Hatanaka8ad73992015-10-15 05:28:38 +0000591 // Issue a warning if the target is not powerpc and Section is a *coal* section.
592 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
593 Triple::ArchType ArchTy = TT.getArch();
594
595 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
596 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
597 .Case("__textcoal_nt", "__text")
598 .Case("__const_coal", "__const")
599 .Case("__datacoal_nt", "__data")
600 .Default(Section);
601
602 if (!Section.equals(NonCoalSection)) {
603 StringRef SectionVal(Loc.getPointer());
604 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
605 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
606 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
607 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
608 SMRange(BLoc, ELoc));
609 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
610 "\"", SMRange(BLoc, ELoc));
611 }
612 }
613
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000614 // FIXME: Arch specific.
615 bool isText = Segment == "__TEXT"; // FIXME: Hack.
616 getStreamer().SwitchSection(getContext().getMachOSection(
617 Segment, Section, TAA, StubSize,
618 isText ? SectionKind::getText()
619 : SectionKind::getDataRel()));
620 return false;
621}
622
Bill Wendlingce4fe412012-08-08 06:30:30 +0000623/// ParseDirectivePushSection:
624/// ::= .pushsection identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000625bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000626 getStreamer().PushSection();
627
Jim Grosbach319026d2014-03-18 22:09:10 +0000628 if (parseDirectiveSection(S, Loc)) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000629 getStreamer().PopSection();
630 return true;
631 }
632
633 return false;
634}
635
636/// ParseDirectivePopSection:
637/// ::= .popsection
Jim Grosbach319026d2014-03-18 22:09:10 +0000638bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000639 if (!getStreamer().PopSection())
640 return TokError(".popsection without corresponding .pushsection");
641 return false;
642}
643
644/// ParseDirectivePrevious:
645/// ::= .previous
Jim Grosbach319026d2014-03-18 22:09:10 +0000646bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000647 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000648 if (!PreviousSection.first)
649 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000650 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000651 return false;
652}
653
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000654/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000655/// ::= .secure_log_unique ... message ...
Jim Grosbach319026d2014-03-18 22:09:10 +0000656bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000657 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000658 if (getLexer().isNot(AsmToken::EndOfStatement))
659 return TokError("unexpected token in '.secure_log_unique' directive");
660
David Blaikiedc3f01e2015-03-09 01:57:13 +0000661 if (getContext().getSecureLogUsed())
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000662 return Error(IDLoc, ".secure_log_unique specified multiple times");
663
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000664 // Get the secure log path.
665 const char *SecureLogFile = getContext().getSecureLogFile();
Craig Topper353eda42014-04-24 06:44:33 +0000666 if (!SecureLogFile)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000667 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
668 "environment variable unset.");
669
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000670 // Open the secure log file if we haven't already.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000671 raw_ostream *OS = getContext().getSecureLog();
Craig Topper353eda42014-04-24 06:44:33 +0000672 if (!OS) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000673 std::error_code EC;
674 OS = new raw_fd_ostream(SecureLogFile, EC,
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000675 sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000676 if (EC) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000677 delete OS;
678 return Error(IDLoc, Twine("can't open secure log file: ") +
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000679 SecureLogFile + " (" + EC.message() + ")");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000680 }
681 getContext().setSecureLog(OS);
682 }
683
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000684 // Write the message.
Alp Tokera55b95b2014-07-06 10:33:31 +0000685 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000686 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
687 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
688 << LogMessage + "\n";
689
690 getContext().setSecureLogUsed(true);
691
692 return false;
693}
694
695/// ParseDirectiveSecureLogReset
696/// ::= .secure_log_reset
Jim Grosbach319026d2014-03-18 22:09:10 +0000697bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000698 if (getLexer().isNot(AsmToken::EndOfStatement))
699 return TokError("unexpected token in '.secure_log_reset' directive");
700
701 Lex();
702
703 getContext().setSecureLogUsed(false);
704
705 return false;
706}
707
Jim Grosbach319026d2014-03-18 22:09:10 +0000708/// parseDirectiveSubsectionsViaSymbols
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000709/// ::= .subsections_via_symbols
Jim Grosbach319026d2014-03-18 22:09:10 +0000710bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000711 if (getLexer().isNot(AsmToken::EndOfStatement))
712 return TokError("unexpected token in '.subsections_via_symbols' directive");
713
714 Lex();
715
716 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
717
718 return false;
719}
720
721/// ParseDirectiveTBSS
722/// ::= .tbss identifier, size, align
Jim Grosbach319026d2014-03-18 22:09:10 +0000723bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000724 SMLoc IDLoc = getLexer().getLoc();
725 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000726 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000727 return TokError("expected identifier in directive");
728
729 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000730 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000731
732 if (getLexer().isNot(AsmToken::Comma))
733 return TokError("unexpected token in directive");
734 Lex();
735
736 int64_t Size;
737 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000738 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000739 return true;
740
741 int64_t Pow2Alignment = 0;
742 SMLoc Pow2AlignmentLoc;
743 if (getLexer().is(AsmToken::Comma)) {
744 Lex();
745 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000746 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000747 return true;
748 }
749
750 if (getLexer().isNot(AsmToken::EndOfStatement))
751 return TokError("unexpected token in '.tbss' directive");
752
753 Lex();
754
755 if (Size < 0)
756 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
757 "zero");
758
759 // FIXME: Diagnose overflow.
760 if (Pow2Alignment < 0)
761 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
762 "than zero");
763
764 if (!Sym->isUndefined())
765 return Error(IDLoc, "invalid symbol redefinition");
766
767 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
768 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000769 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000770 0, SectionKind::getThreadBSS()),
771 Sym, Size, 1 << Pow2Alignment);
772
773 return false;
774}
775
776/// ParseDirectiveZerofill
777/// ::= .zerofill segname , sectname [, identifier , size_expression [
778/// , align_expression ]]
Jim Grosbach319026d2014-03-18 22:09:10 +0000779bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000780 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000781 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000782 return TokError("expected segment name after '.zerofill' directive");
783
784 if (getLexer().isNot(AsmToken::Comma))
785 return TokError("unexpected token in directive");
786 Lex();
787
788 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000789 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000790 return TokError("expected section name after comma in '.zerofill' "
791 "directive");
792
793 // If this is the end of the line all that was wanted was to create the
794 // the section but with no symbol.
795 if (getLexer().is(AsmToken::EndOfStatement)) {
796 // Create the zerofill section but no symbol
797 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000798 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000799 0, SectionKind::getBSS()));
800 return false;
801 }
802
803 if (getLexer().isNot(AsmToken::Comma))
804 return TokError("unexpected token in directive");
805 Lex();
806
807 SMLoc IDLoc = getLexer().getLoc();
808 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000809 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000810 return TokError("expected identifier in directive");
811
812 // handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000813 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000814
815 if (getLexer().isNot(AsmToken::Comma))
816 return TokError("unexpected token in directive");
817 Lex();
818
819 int64_t Size;
820 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000821 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000822 return true;
823
824 int64_t Pow2Alignment = 0;
825 SMLoc Pow2AlignmentLoc;
826 if (getLexer().is(AsmToken::Comma)) {
827 Lex();
828 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000829 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000830 return true;
831 }
832
833 if (getLexer().isNot(AsmToken::EndOfStatement))
834 return TokError("unexpected token in '.zerofill' directive");
835
836 Lex();
837
838 if (Size < 0)
839 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
840 "than zero");
841
842 // NOTE: The alignment in the directive is a power of 2 value, the assembler
843 // may internally end up wanting an alignment in bytes.
844 // FIXME: Diagnose overflow.
845 if (Pow2Alignment < 0)
846 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
847 "can't be less than zero");
848
849 if (!Sym->isUndefined())
850 return Error(IDLoc, "invalid symbol redefinition");
851
852 // Create the zerofill Symbol with Size and Pow2Alignment
853 //
854 // FIXME: Arch specific.
855 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000856 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000857 0, SectionKind::getBSS()),
858 Sym, Size, 1 << Pow2Alignment);
859
860 return false;
861}
862
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000863/// ParseDirectiveDataRegion
864/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
Jim Grosbach319026d2014-03-18 22:09:10 +0000865bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000866 if (getLexer().is(AsmToken::EndOfStatement)) {
867 Lex();
868 getStreamer().EmitDataRegion(MCDR_DataRegion);
869 return false;
870 }
871 StringRef RegionType;
872 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000873 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000874 return TokError("expected region type after '.data_region' directive");
875 int Kind = StringSwitch<int>(RegionType)
876 .Case("jt8", MCDR_DataRegionJT8)
877 .Case("jt16", MCDR_DataRegionJT16)
878 .Case("jt32", MCDR_DataRegionJT32)
879 .Default(-1);
880 if (Kind == -1)
881 return Error(Loc, "unknown region type in '.data_region' directive");
882 Lex();
883
884 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
885 return false;
886}
887
888/// ParseDirectiveDataRegionEnd
889/// ::= .end_data_region
Jim Grosbach319026d2014-03-18 22:09:10 +0000890bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000891 if (getLexer().isNot(AsmToken::EndOfStatement))
892 return TokError("unexpected token in '.end_data_region' directive");
893
894 Lex();
895 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
896 return false;
897}
898
Jim Grosbach319026d2014-03-18 22:09:10 +0000899/// parseVersionMin
Jim Grosbach448334a2014-03-18 22:09:05 +0000900/// ::= .ios_version_min major,minor[,update]
901/// ::= .macosx_version_min major,minor[,update]
Tim Northover2d4d1612015-10-28 22:36:05 +0000902bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
Jim Grosbach448334a2014-03-18 22:09:05 +0000903 int64_t Major = 0, Minor = 0, Update = 0;
904 int Kind = StringSwitch<int>(Directive)
Tim Northover2d4d1612015-10-28 22:36:05 +0000905 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
906 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
Jim Grosbach448334a2014-03-18 22:09:05 +0000907 .Case(".ios_version_min", MCVM_IOSVersionMin)
908 .Case(".macosx_version_min", MCVM_OSXVersionMin);
909 // Get the major version number.
910 if (getLexer().isNot(AsmToken::Integer))
911 return TokError("invalid OS major version number");
912 Major = getLexer().getTok().getIntVal();
913 if (Major > 65535 || Major <= 0)
914 return TokError("invalid OS major version number");
915 Lex();
916 if (getLexer().isNot(AsmToken::Comma))
917 return TokError("minor OS version number required, comma expected");
918 Lex();
919 // Get the minor version number.
920 if (getLexer().isNot(AsmToken::Integer))
921 return TokError("invalid OS minor version number");
922 Minor = getLexer().getTok().getIntVal();
923 if (Minor > 255 || Minor < 0)
924 return TokError("invalid OS minor version number");
925 Lex();
926 // Get the update level, if specified
927 if (getLexer().isNot(AsmToken::EndOfStatement)) {
928 if (getLexer().isNot(AsmToken::Comma))
929 return TokError("invalid update specifier, comma expected");
930 Lex();
931 if (getLexer().isNot(AsmToken::Integer))
932 return TokError("invalid OS update number");
933 Update = getLexer().getTok().getIntVal();
934 if (Update > 255 || Update < 0)
935 return TokError("invalid OS update number");
936 Lex();
937 }
938
Tim Northover2d4d1612015-10-28 22:36:05 +0000939 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
940 Triple::OSType ExpectedOS = Triple::UnknownOS;
941 switch ((MCVersionMinType)Kind) {
942 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
943 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
944 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
945 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
946 }
947 if (T.getOS() != ExpectedOS)
948 Warning(Loc, Directive + " should only be used for " +
949 Triple::getOSTypeName(ExpectedOS) + " targets");
950
951 if (LastVersionMinDirective.isValid()) {
952 Warning(Loc, "overriding previous version_min directive");
953 Note(LastVersionMinDirective, "previous definition is here");
954 }
955 LastVersionMinDirective = Loc;
956
Jim Grosbach448334a2014-03-18 22:09:05 +0000957 // We've parsed a correct version specifier, so send it to the streamer.
958 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
959
960 return false;
961}
962
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000963namespace llvm {
964
965MCAsmParserExtension *createDarwinAsmParser() {
966 return new DarwinAsmParser;
967}
968
Bill Wendlingce4fe412012-08-08 06:30:30 +0000969} // end llvm namespace