blob: 9102dc397567b17f94bdd4c71fa40d8049e541c3 [file] [log] [blame]
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +00001//===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/StringSwitch.h"
13#include "llvm/ADT/Twine.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000014#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000017#include "llvm/MC/MCSectionMachO.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSymbol.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000020#include "llvm/Support/FileSystem.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000021#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/SourceMgr.h"
23using namespace llvm;
24
25namespace {
26
27/// \brief Implementation of directive handling which is shared across all
28/// Darwin targets.
29class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000030 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000031 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000032 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
33 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000034 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000035 }
36
Jim Grosbach319026d2014-03-18 22:09:10 +000037 bool parseSectionSwitch(const char *Segment, const char *Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000038 unsigned TAA = 0, unsigned ImplicitAlign = 0,
39 unsigned StubSize = 0);
40
41public:
42 DarwinAsmParser() {}
43
Craig Topper59be68f2014-03-08 07:14:16 +000044 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000045 // Call the base implementation.
46 this->MCAsmParserExtension::Initialize(Parser);
47
Jim Grosbach319026d2014-03-18 22:09:10 +000048 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
49 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
Kevin Enderby3aeada22013-08-28 17:50:59 +000050 ".indirect_symbol");
Jim Grosbach319026d2014-03-18 22:09:10 +000051 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
52 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000053 ".subsections_via_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +000054 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
55 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
56 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
57 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000058 ".pushsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000059 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000060 ".popsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000061 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
62 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000063 ".secure_log_unique");
Jim Grosbach319026d2014-03-18 22:09:10 +000064 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000065 ".secure_log_reset");
Jim Grosbach319026d2014-03-18 22:09:10 +000066 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
67 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000068
Jim Grosbach319026d2014-03-18 22:09:10 +000069 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000070 ".data_region");
Jim Grosbach319026d2014-03-18 22:09:10 +000071 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000072 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000073
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000074 // Special section directives.
Jim Grosbach319026d2014-03-18 22:09:10 +000075 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
76 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
77 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000078 ".const_data");
Jim Grosbach319026d2014-03-18 22:09:10 +000079 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000080 ".constructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000081 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000082 ".cstring");
Jim Grosbach319026d2014-03-18 22:09:10 +000083 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
84 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000085 ".destructor");
Jim Grosbach319026d2014-03-18 22:09:10 +000086 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
87 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000088 ".fvmlib_init0");
Jim Grosbach319026d2014-03-18 22:09:10 +000089 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000090 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000091 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +000092 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000093 ".lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +000094 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +000095 ".linker_option");
Jim Grosbach319026d2014-03-18 22:09:10 +000096 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000097 ".literal16");
Jim Grosbach319026d2014-03-18 22:09:10 +000098 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000099 ".literal4");
Jim Grosbach319026d2014-03-18 22:09:10 +0000100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000101 ".literal8");
Jim Grosbach319026d2014-03-18 22:09:10 +0000102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000103 ".mod_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000104 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000105 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000106 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000107 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000108 ".non_lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000109 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000110 ".objc_cat_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000111 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000112 ".objc_cat_inst_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000113 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000114 ".objc_category");
Jim Grosbach319026d2014-03-18 22:09:10 +0000115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000116 ".objc_class");
Jim Grosbach319026d2014-03-18 22:09:10 +0000117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000118 ".objc_class_names");
Jim Grosbach319026d2014-03-18 22:09:10 +0000119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000120 ".objc_class_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000122 ".objc_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000124 ".objc_cls_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000125 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000126 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000127 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000128 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000129 ".objc_instance_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000130 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000131 ".objc_message_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000132 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000133 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000134 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000135 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000136 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000137 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000138 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000139 ".objc_meth_var_types");
Jim Grosbach319026d2014-03-18 22:09:10 +0000140 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000141 ".objc_module_info");
Jim Grosbach319026d2014-03-18 22:09:10 +0000142 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000143 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000144 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000145 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000146 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000147 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000148 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000149 ".objc_string_object");
Jim Grosbach319026d2014-03-18 22:09:10 +0000150 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000151 ".objc_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +0000152 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000153 ".picsymbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000155 ".static_const");
Jim Grosbach319026d2014-03-18 22:09:10 +0000156 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000157 ".static_data");
Jim Grosbach319026d2014-03-18 22:09:10 +0000158 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000159 ".symbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000160 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
161 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000163 ".thread_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000165
Jim Grosbach319026d2014-03-18 22:09:10 +0000166 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
167 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
168 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
Jim Grosbach448334a2014-03-18 22:09:05 +0000169 ".macosx_version_min");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000170 }
171
Jim Grosbach319026d2014-03-18 22:09:10 +0000172 bool parseDirectiveDesc(StringRef, SMLoc);
173 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
174 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
175 bool parseDirectiveLsym(StringRef, SMLoc);
176 bool parseDirectiveLinkerOption(StringRef, SMLoc);
177 bool parseDirectiveSection(StringRef, SMLoc);
178 bool parseDirectivePushSection(StringRef, SMLoc);
179 bool parseDirectivePopSection(StringRef, SMLoc);
180 bool parseDirectivePrevious(StringRef, SMLoc);
181 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
182 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
183 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
184 bool parseDirectiveTBSS(StringRef, SMLoc);
185 bool parseDirectiveZerofill(StringRef, SMLoc);
186 bool parseDirectiveDataRegion(StringRef, SMLoc);
187 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000188
189 // Named Section Directive
Jim Grosbach319026d2014-03-18 22:09:10 +0000190 bool parseSectionDirectiveBss(StringRef, SMLoc) {
191 return parseSectionSwitch("__DATA", "__bss");
Rafael Espindola3402c052013-10-02 14:09:29 +0000192 }
193
Jim Grosbach319026d2014-03-18 22:09:10 +0000194 bool parseSectionDirectiveConst(StringRef, SMLoc) {
195 return parseSectionSwitch("__TEXT", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000196 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000197 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
198 return parseSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000199 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000200 bool parseSectionDirectiveCString(StringRef, SMLoc) {
201 return parseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000202 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000203 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000204 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
205 return parseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000206 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000207 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000208 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
209 return parseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000210 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000211 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000212 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
213 return parseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000214 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000215 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000216 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
217 return parseSectionSwitch("__TEXT","__constructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000218 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000219 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
220 return parseSectionSwitch("__TEXT","__destructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000221 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000222 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
223 return parseSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000224 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000225 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
226 return parseSectionSwitch("__TEXT","__fvmlib_init1");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000227 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000228 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
229 return parseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000230 MachO::S_SYMBOL_STUBS |
231 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000232 // FIXME: Different on PPC and ARM.
233 0, 16);
234 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000235 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
236 return parseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000237 MachO::S_SYMBOL_STUBS |
238 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000239 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000240 bool parseSectionDirectiveData(StringRef, SMLoc) {
241 return parseSectionSwitch("__DATA", "__data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000242 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000243 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
244 return parseSectionSwitch("__DATA", "__static_data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000245 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000246 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
247 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000248 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000249 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000250 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
251 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000252 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000253 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000254 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
255 return parseSectionSwitch("__DATA", "__dyld");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000256 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000257 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
258 return parseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000259 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000260 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000261 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
262 return parseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000263 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000264 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000265 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
266 return parseSectionSwitch("__DATA", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000267 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000268 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
269 return parseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000270 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000271 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000272 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
273 return parseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000274 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000275 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000276 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
277 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000278 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000279 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000280 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
281 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
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 parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
285 return parseSectionSwitch("__OBJC", "__protocol",
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 parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
289 return parseSectionSwitch("__OBJC", "__string_object",
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 parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
293 return parseSectionSwitch("__OBJC", "__cls_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 parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
297 return parseSectionSwitch("__OBJC", "__inst_meth",
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 parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
301 return parseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000302 MachO::S_ATTR_NO_DEAD_STRIP |
303 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000304 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000305 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
306 return parseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000307 MachO::S_ATTR_NO_DEAD_STRIP |
308 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000309 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000310 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
311 return parseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000312 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000313 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000314 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
315 return parseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000316 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000317 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000318 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
319 return parseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000320 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000321 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000322 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
323 return parseSectionSwitch("__OBJC", "__instance_vars",
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 parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
327 return parseSectionSwitch("__OBJC", "__module_info",
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 parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
331 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000332 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000333 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000334 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
335 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000336 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000337 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000338 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
339 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000340 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000341 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000342 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
343 return parseSectionSwitch("__OBJC", "__selector_strs",
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 parseSectionDirectiveTData(StringRef, SMLoc) {
347 return parseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000348 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000349 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000350 bool parseSectionDirectiveText(StringRef, SMLoc) {
351 return parseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000352 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000353 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000354 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
355 return parseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000356 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000357 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000358 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
Jim Grosbach14be61a2011-03-08 19:17:19 +0000359 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000360 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000361 return false;
362 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000363 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
364 return parseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000365 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000366 }
Jim Grosbach319026d2014-03-18 22:09:10 +0000367 bool parseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000368
369};
370
Bill Wendlingce4fe412012-08-08 06:30:30 +0000371} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000372
Jim Grosbach319026d2014-03-18 22:09:10 +0000373bool DarwinAsmParser::parseSectionSwitch(const char *Segment,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000374 const char *Section,
375 unsigned TAA, unsigned Align,
376 unsigned StubSize) {
377 if (getLexer().isNot(AsmToken::EndOfStatement))
378 return TokError("unexpected token in section switching directive");
379 Lex();
380
381 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000382 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000383 getStreamer().SwitchSection(getContext().getMachOSection(
384 Segment, Section, TAA, StubSize,
385 isText ? SectionKind::getText()
386 : SectionKind::getDataRel()));
387
388 // Set the implicit alignment, if any.
389 //
390 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
391 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000392 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000393 // the section. However, this is arguably more reasonable behavior, and there
394 // is no good reason for someone to intentionally emit incorrectly sized
395 // values into the implicitly aligned sections.
396 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000397 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000398
399 return false;
400}
401
Jim Grosbach319026d2014-03-18 22:09:10 +0000402/// parseDirectiveDesc
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000403/// ::= .desc identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000404bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000405 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000406 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000407 return TokError("expected identifier in directive");
408
409 // Handle the identifier as the key symbol.
410 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
411
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("unexpected token in '.desc' directive");
414 Lex();
415
416 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000417 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000418 return true;
419
420 if (getLexer().isNot(AsmToken::EndOfStatement))
421 return TokError("unexpected token in '.desc' directive");
422
423 Lex();
424
425 // Set the n_desc field of this Symbol to this DescValue
426 getStreamer().EmitSymbolDesc(Sym, DescValue);
427
428 return false;
429}
430
Jim Grosbach319026d2014-03-18 22:09:10 +0000431/// parseDirectiveIndirectSymbol
Kevin Enderby3aeada22013-08-28 17:50:59 +0000432/// ::= .indirect_symbol identifier
Jim Grosbach319026d2014-03-18 22:09:10 +0000433bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
Kevin Enderby3aeada22013-08-28 17:50:59 +0000434 const MCSectionMachO *Current = static_cast<const MCSectionMachO*>(
435 getStreamer().getCurrentSection().first);
David Majnemercd481d32014-03-07 18:49:54 +0000436 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000437 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
438 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
439 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000440 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
441 "section");
442
443 StringRef Name;
444 if (getParser().parseIdentifier(Name))
445 return TokError("expected identifier in .indirect_symbol directive");
446
447 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
448
449 // Assembler local symbols don't make any sense here. Complain loudly.
450 if (Sym->isTemporary())
451 return TokError("non-local symbol required in directive");
452
453 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
454 return TokError("unable to emit indirect symbol attribute for: " + Name);
455
456 if (getLexer().isNot(AsmToken::EndOfStatement))
457 return TokError("unexpected token in '.indirect_symbol' directive");
458
459 Lex();
460
461 return false;
462}
463
Jim Grosbach319026d2014-03-18 22:09:10 +0000464/// parseDirectiveDumpOrLoad
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000465/// ::= ( .dump | .load ) "filename"
Jim Grosbach319026d2014-03-18 22:09:10 +0000466bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000467 SMLoc IDLoc) {
468 bool IsDump = Directive == ".dump";
469 if (getLexer().isNot(AsmToken::String))
470 return TokError("expected string in '.dump' or '.load' directive");
471
472 Lex();
473
474 if (getLexer().isNot(AsmToken::EndOfStatement))
475 return TokError("unexpected token in '.dump' or '.load' directive");
476
477 Lex();
478
479 // FIXME: If/when .dump and .load are implemented they will be done in the
480 // the assembly parser and not have any need for an MCStreamer API.
481 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000482 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000483 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000484 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000485}
486
Daniel Dunbar16004b82013-01-18 01:25:48 +0000487/// ParseDirectiveLinkerOption
488/// ::= .linker_option "string" ( , "string" )*
Jim Grosbach319026d2014-03-18 22:09:10 +0000489bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000490 SmallVector<std::string, 4> Args;
491 for (;;) {
492 if (getLexer().isNot(AsmToken::String))
493 return TokError("expected string in '" + Twine(IDVal) + "' directive");
494
495 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000496 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000497 return true;
498
499 Args.push_back(Data);
500
501 Lex();
502 if (getLexer().is(AsmToken::EndOfStatement))
503 break;
504
505 if (getLexer().isNot(AsmToken::Comma))
506 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
507 Lex();
508 }
509
510 getStreamer().EmitLinkerOptions(Args);
511 return false;
512}
513
Jim Grosbach319026d2014-03-18 22:09:10 +0000514/// parseDirectiveLsym
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000515/// ::= .lsym identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000516bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000517 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000518 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000519 return TokError("expected identifier in directive");
520
521 // Handle the identifier as the key symbol.
522 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
523
524 if (getLexer().isNot(AsmToken::Comma))
525 return TokError("unexpected token in '.lsym' directive");
526 Lex();
527
528 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000529 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000530 return true;
531
532 if (getLexer().isNot(AsmToken::EndOfStatement))
533 return TokError("unexpected token in '.lsym' directive");
534
535 Lex();
536
537 // We don't currently support this directive.
538 //
539 // FIXME: Diagnostic location!
540 (void) Sym;
541 return TokError("directive '.lsym' is unsupported");
542}
543
Jim Grosbach319026d2014-03-18 22:09:10 +0000544/// parseDirectiveSection:
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000545/// ::= .section identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000546bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000547 SMLoc Loc = getLexer().getLoc();
548
549 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000550 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000551 return Error(Loc, "expected identifier after '.section' directive");
552
553 // Verify there is a following comma.
554 if (!getLexer().is(AsmToken::Comma))
555 return TokError("unexpected token in '.section' directive");
556
557 std::string SectionSpec = SectionName;
558 SectionSpec += ",";
559
560 // Add all the tokens until the end of the line, ParseSectionSpecifier will
561 // handle this.
562 StringRef EOL = getLexer().LexUntilEndOfStatement();
563 SectionSpec.append(EOL.begin(), EOL.end());
564
565 Lex();
566 if (getLexer().isNot(AsmToken::EndOfStatement))
567 return TokError("unexpected token in '.section' directive");
568 Lex();
569
570
571 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000572 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000573 unsigned TAA;
574 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000575 std::string ErrorStr =
576 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000577 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000578
579 if (!ErrorStr.empty())
580 return Error(Loc, ErrorStr.c_str());
581
582 // FIXME: Arch specific.
583 bool isText = Segment == "__TEXT"; // FIXME: Hack.
584 getStreamer().SwitchSection(getContext().getMachOSection(
585 Segment, Section, TAA, StubSize,
586 isText ? SectionKind::getText()
587 : SectionKind::getDataRel()));
588 return false;
589}
590
Bill Wendlingce4fe412012-08-08 06:30:30 +0000591/// ParseDirectivePushSection:
592/// ::= .pushsection identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000593bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000594 getStreamer().PushSection();
595
Jim Grosbach319026d2014-03-18 22:09:10 +0000596 if (parseDirectiveSection(S, Loc)) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000597 getStreamer().PopSection();
598 return true;
599 }
600
601 return false;
602}
603
604/// ParseDirectivePopSection:
605/// ::= .popsection
Jim Grosbach319026d2014-03-18 22:09:10 +0000606bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000607 if (!getStreamer().PopSection())
608 return TokError(".popsection without corresponding .pushsection");
609 return false;
610}
611
612/// ParseDirectivePrevious:
613/// ::= .previous
Jim Grosbach319026d2014-03-18 22:09:10 +0000614bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000615 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000616 if (!PreviousSection.first)
617 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000618 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000619 return false;
620}
621
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000622/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000623/// ::= .secure_log_unique ... message ...
Jim Grosbach319026d2014-03-18 22:09:10 +0000624bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000625 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000626 if (getLexer().isNot(AsmToken::EndOfStatement))
627 return TokError("unexpected token in '.secure_log_unique' directive");
628
David Blaikiedc3f01e2015-03-09 01:57:13 +0000629 if (getContext().getSecureLogUsed())
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000630 return Error(IDLoc, ".secure_log_unique specified multiple times");
631
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000632 // Get the secure log path.
633 const char *SecureLogFile = getContext().getSecureLogFile();
Craig Topper353eda42014-04-24 06:44:33 +0000634 if (!SecureLogFile)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000635 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
636 "environment variable unset.");
637
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000638 // Open the secure log file if we haven't already.
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000639 raw_ostream *OS = getContext().getSecureLog();
Craig Topper353eda42014-04-24 06:44:33 +0000640 if (!OS) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000641 std::error_code EC;
642 OS = new raw_fd_ostream(SecureLogFile, EC,
Rafael Espindola90c7f1c2014-02-24 18:20:12 +0000643 sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000644 if (EC) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000645 delete OS;
646 return Error(IDLoc, Twine("can't open secure log file: ") +
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000647 SecureLogFile + " (" + EC.message() + ")");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000648 }
649 getContext().setSecureLog(OS);
650 }
651
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000652 // Write the message.
Alp Tokera55b95b2014-07-06 10:33:31 +0000653 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000654 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
655 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
656 << LogMessage + "\n";
657
658 getContext().setSecureLogUsed(true);
659
660 return false;
661}
662
663/// ParseDirectiveSecureLogReset
664/// ::= .secure_log_reset
Jim Grosbach319026d2014-03-18 22:09:10 +0000665bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000666 if (getLexer().isNot(AsmToken::EndOfStatement))
667 return TokError("unexpected token in '.secure_log_reset' directive");
668
669 Lex();
670
671 getContext().setSecureLogUsed(false);
672
673 return false;
674}
675
Jim Grosbach319026d2014-03-18 22:09:10 +0000676/// parseDirectiveSubsectionsViaSymbols
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000677/// ::= .subsections_via_symbols
Jim Grosbach319026d2014-03-18 22:09:10 +0000678bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000679 if (getLexer().isNot(AsmToken::EndOfStatement))
680 return TokError("unexpected token in '.subsections_via_symbols' directive");
681
682 Lex();
683
684 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
685
686 return false;
687}
688
689/// ParseDirectiveTBSS
690/// ::= .tbss identifier, size, align
Jim Grosbach319026d2014-03-18 22:09:10 +0000691bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000692 SMLoc IDLoc = getLexer().getLoc();
693 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000694 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000695 return TokError("expected identifier in directive");
696
697 // Handle the identifier as the key symbol.
698 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
699
700 if (getLexer().isNot(AsmToken::Comma))
701 return TokError("unexpected token in directive");
702 Lex();
703
704 int64_t Size;
705 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000706 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000707 return true;
708
709 int64_t Pow2Alignment = 0;
710 SMLoc Pow2AlignmentLoc;
711 if (getLexer().is(AsmToken::Comma)) {
712 Lex();
713 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000714 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000715 return true;
716 }
717
718 if (getLexer().isNot(AsmToken::EndOfStatement))
719 return TokError("unexpected token in '.tbss' directive");
720
721 Lex();
722
723 if (Size < 0)
724 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
725 "zero");
726
727 // FIXME: Diagnose overflow.
728 if (Pow2Alignment < 0)
729 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
730 "than zero");
731
732 if (!Sym->isUndefined())
733 return Error(IDLoc, "invalid symbol redefinition");
734
735 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
736 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000737 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000738 0, SectionKind::getThreadBSS()),
739 Sym, Size, 1 << Pow2Alignment);
740
741 return false;
742}
743
744/// ParseDirectiveZerofill
745/// ::= .zerofill segname , sectname [, identifier , size_expression [
746/// , align_expression ]]
Jim Grosbach319026d2014-03-18 22:09:10 +0000747bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000748 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000749 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000750 return TokError("expected segment name after '.zerofill' directive");
751
752 if (getLexer().isNot(AsmToken::Comma))
753 return TokError("unexpected token in directive");
754 Lex();
755
756 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000757 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000758 return TokError("expected section name after comma in '.zerofill' "
759 "directive");
760
761 // If this is the end of the line all that was wanted was to create the
762 // the section but with no symbol.
763 if (getLexer().is(AsmToken::EndOfStatement)) {
764 // Create the zerofill section but no symbol
765 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000766 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000767 0, SectionKind::getBSS()));
768 return false;
769 }
770
771 if (getLexer().isNot(AsmToken::Comma))
772 return TokError("unexpected token in directive");
773 Lex();
774
775 SMLoc IDLoc = getLexer().getLoc();
776 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000777 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000778 return TokError("expected identifier in directive");
779
780 // handle the identifier as the key symbol.
781 MCSymbol *Sym = getContext().GetOrCreateSymbol(IDStr);
782
783 if (getLexer().isNot(AsmToken::Comma))
784 return TokError("unexpected token in directive");
785 Lex();
786
787 int64_t Size;
788 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000789 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000790 return true;
791
792 int64_t Pow2Alignment = 0;
793 SMLoc Pow2AlignmentLoc;
794 if (getLexer().is(AsmToken::Comma)) {
795 Lex();
796 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000797 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000798 return true;
799 }
800
801 if (getLexer().isNot(AsmToken::EndOfStatement))
802 return TokError("unexpected token in '.zerofill' directive");
803
804 Lex();
805
806 if (Size < 0)
807 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
808 "than zero");
809
810 // NOTE: The alignment in the directive is a power of 2 value, the assembler
811 // may internally end up wanting an alignment in bytes.
812 // FIXME: Diagnose overflow.
813 if (Pow2Alignment < 0)
814 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
815 "can't be less than zero");
816
817 if (!Sym->isUndefined())
818 return Error(IDLoc, "invalid symbol redefinition");
819
820 // Create the zerofill Symbol with Size and Pow2Alignment
821 //
822 // FIXME: Arch specific.
823 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000824 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000825 0, SectionKind::getBSS()),
826 Sym, Size, 1 << Pow2Alignment);
827
828 return false;
829}
830
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000831/// ParseDirectiveDataRegion
832/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
Jim Grosbach319026d2014-03-18 22:09:10 +0000833bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000834 if (getLexer().is(AsmToken::EndOfStatement)) {
835 Lex();
836 getStreamer().EmitDataRegion(MCDR_DataRegion);
837 return false;
838 }
839 StringRef RegionType;
840 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000841 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000842 return TokError("expected region type after '.data_region' directive");
843 int Kind = StringSwitch<int>(RegionType)
844 .Case("jt8", MCDR_DataRegionJT8)
845 .Case("jt16", MCDR_DataRegionJT16)
846 .Case("jt32", MCDR_DataRegionJT32)
847 .Default(-1);
848 if (Kind == -1)
849 return Error(Loc, "unknown region type in '.data_region' directive");
850 Lex();
851
852 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
853 return false;
854}
855
856/// ParseDirectiveDataRegionEnd
857/// ::= .end_data_region
Jim Grosbach319026d2014-03-18 22:09:10 +0000858bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000859 if (getLexer().isNot(AsmToken::EndOfStatement))
860 return TokError("unexpected token in '.end_data_region' directive");
861
862 Lex();
863 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
864 return false;
865}
866
Jim Grosbach319026d2014-03-18 22:09:10 +0000867/// parseVersionMin
Jim Grosbach448334a2014-03-18 22:09:05 +0000868/// ::= .ios_version_min major,minor[,update]
869/// ::= .macosx_version_min major,minor[,update]
Jim Grosbach319026d2014-03-18 22:09:10 +0000870bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc) {
Jim Grosbach448334a2014-03-18 22:09:05 +0000871 int64_t Major = 0, Minor = 0, Update = 0;
872 int Kind = StringSwitch<int>(Directive)
873 .Case(".ios_version_min", MCVM_IOSVersionMin)
874 .Case(".macosx_version_min", MCVM_OSXVersionMin);
875 // Get the major version number.
876 if (getLexer().isNot(AsmToken::Integer))
877 return TokError("invalid OS major version number");
878 Major = getLexer().getTok().getIntVal();
879 if (Major > 65535 || Major <= 0)
880 return TokError("invalid OS major version number");
881 Lex();
882 if (getLexer().isNot(AsmToken::Comma))
883 return TokError("minor OS version number required, comma expected");
884 Lex();
885 // Get the minor version number.
886 if (getLexer().isNot(AsmToken::Integer))
887 return TokError("invalid OS minor version number");
888 Minor = getLexer().getTok().getIntVal();
889 if (Minor > 255 || Minor < 0)
890 return TokError("invalid OS minor version number");
891 Lex();
892 // Get the update level, if specified
893 if (getLexer().isNot(AsmToken::EndOfStatement)) {
894 if (getLexer().isNot(AsmToken::Comma))
895 return TokError("invalid update specifier, comma expected");
896 Lex();
897 if (getLexer().isNot(AsmToken::Integer))
898 return TokError("invalid OS update number");
899 Update = getLexer().getTok().getIntVal();
900 if (Update > 255 || Update < 0)
901 return TokError("invalid OS update number");
902 Lex();
903 }
904
905 // We've parsed a correct version specifier, so send it to the streamer.
906 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
907
908 return false;
909}
910
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000911namespace llvm {
912
913MCAsmParserExtension *createDarwinAsmParser() {
914 return new DarwinAsmParser;
915}
916
Bill Wendlingce4fe412012-08-08 06:30:30 +0000917} // end llvm namespace