blob: f4152a9067a06238f4b60571c253f5466d2699e2 [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
Rafael Espindolac49ac5e2015-12-16 23:49:14 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/StringSwitch.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000014#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000016#include "llvm/BinaryFormat/MachO.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000017#include "llvm/MC/MCContext.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000018#include "llvm/MC/MCDirectives.h"
Akira Hatanaka8ad73992015-10-15 05:28:38 +000019#include "llvm/MC/MCObjectFileInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCParser/MCAsmLexer.h"
21#include "llvm/MC/MCParser/MCAsmParser.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000022#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000023#include "llvm/MC/MCSectionMachO.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/MC/MCSymbol.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000026#include "llvm/MC/SectionKind.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000027#include "llvm/Support/FileSystem.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000029#include "llvm/Support/SMLoc.h"
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000030#include "llvm/Support/SourceMgr.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000032#include <algorithm>
33#include <cstddef>
34#include <cstdint>
35#include <string>
36#include <system_error>
37#include <utility>
38
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000039using namespace llvm;
40
41namespace {
42
43/// \brief Implementation of directive handling which is shared across all
44/// Darwin targets.
45class DarwinAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000046 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000047 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000048 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
49 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
Jim Grosbachd2037eb2013-02-20 22:21:35 +000050 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000051 }
52
Mehdi Amini215ff8d2016-10-05 01:02:22 +000053 bool parseSectionSwitch(StringRef Segment, StringRef Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000054 unsigned TAA = 0, unsigned ImplicitAlign = 0,
55 unsigned StubSize = 0);
56
Tim Northover2d4d1612015-10-28 22:36:05 +000057 SMLoc LastVersionMinDirective;
58
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000059public:
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +000060 DarwinAsmParser() = default;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000061
Craig Topper59be68f2014-03-08 07:14:16 +000062 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000063 // Call the base implementation.
64 this->MCAsmParserExtension::Initialize(Parser);
65
Lang Hamesf9033bb2016-04-11 18:33:45 +000066 addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(".alt_entry");
Jim Grosbach319026d2014-03-18 22:09:10 +000067 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(".desc");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
Kevin Enderby3aeada22013-08-28 17:50:59 +000069 ".indirect_symbol");
Jim Grosbach319026d2014-03-18 22:09:10 +000070 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(".lsym");
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000072 ".subsections_via_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +000073 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".dump");
74 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(".load");
75 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(".section");
76 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000077 ".pushsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000078 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000079 ".popsection");
Jim Grosbach319026d2014-03-18 22:09:10 +000080 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(".previous");
81 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000082 ".secure_log_unique");
Jim Grosbach319026d2014-03-18 22:09:10 +000083 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
Daniel Dunbar8897d472010-07-18 22:22:07 +000084 ".secure_log_reset");
Jim Grosbach319026d2014-03-18 22:09:10 +000085 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(".tbss");
86 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(".zerofill");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000087
Jim Grosbach319026d2014-03-18 22:09:10 +000088 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000089 ".data_region");
Jim Grosbach319026d2014-03-18 22:09:10 +000090 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000091 ".end_data_region");
Jim Grosbach4b63d2a2012-05-18 19:12:01 +000092
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +000093 // Special section directives.
Jim Grosbach319026d2014-03-18 22:09:10 +000094 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(".bss");
95 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(".const");
96 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000097 ".const_data");
Jim Grosbach319026d2014-03-18 22:09:10 +000098 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +000099 ".constructor");
Jim Grosbach319026d2014-03-18 22:09:10 +0000100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000101 ".cstring");
Jim Grosbach319026d2014-03-18 22:09:10 +0000102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(".data");
103 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000104 ".destructor");
Jim Grosbach319026d2014-03-18 22:09:10 +0000105 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(".dyld");
106 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000107 ".fvmlib_init0");
Jim Grosbach319026d2014-03-18 22:09:10 +0000108 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000109 ".fvmlib_init1");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000110 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000111 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000112 ".lazy_symbol_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000113 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
Daniel Dunbar16004b82013-01-18 01:25:48 +0000114 ".linker_option");
Jim Grosbach319026d2014-03-18 22:09:10 +0000115 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000116 ".literal16");
Jim Grosbach319026d2014-03-18 22:09:10 +0000117 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000118 ".literal4");
Jim Grosbach319026d2014-03-18 22:09:10 +0000119 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000120 ".literal8");
Jim Grosbach319026d2014-03-18 22:09:10 +0000121 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000122 ".mod_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000123 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000124 ".mod_term_func");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000125 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000126 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000127 ".non_lazy_symbol_pointer");
Tim Northover5c3140f2016-04-25 21:12:04 +0000128 addDirectiveHandler<
129 &DarwinAsmParser::parseSectionDirectiveThreadLocalVariablePointers>(
130 ".thread_local_variable_pointer");
Jim Grosbach319026d2014-03-18 22:09:10 +0000131 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000132 ".objc_cat_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000133 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000134 ".objc_cat_inst_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000135 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000136 ".objc_category");
Jim Grosbach319026d2014-03-18 22:09:10 +0000137 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000138 ".objc_class");
Jim Grosbach319026d2014-03-18 22:09:10 +0000139 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000140 ".objc_class_names");
Jim Grosbach319026d2014-03-18 22:09:10 +0000141 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000142 ".objc_class_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000143 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000144 ".objc_cls_meth");
Jim Grosbach319026d2014-03-18 22:09:10 +0000145 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000146 ".objc_cls_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000147 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000148 ".objc_inst_meth");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000149 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000150 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000151 ".objc_instance_vars");
Jim Grosbach319026d2014-03-18 22:09:10 +0000152 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000153 ".objc_message_refs");
Jim Grosbach319026d2014-03-18 22:09:10 +0000154 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000155 ".objc_meta_class");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000156 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000157 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000158 ".objc_meth_var_names");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000159 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000160 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000161 ".objc_meth_var_types");
Jim Grosbach319026d2014-03-18 22:09:10 +0000162 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000163 ".objc_module_info");
Jim Grosbach319026d2014-03-18 22:09:10 +0000164 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000165 ".objc_protocol");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000166 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000167 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000168 ".objc_selector_strs");
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000169 addDirectiveHandler<
Jim Grosbach319026d2014-03-18 22:09:10 +0000170 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000171 ".objc_string_object");
Jim Grosbach319026d2014-03-18 22:09:10 +0000172 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000173 ".objc_symbols");
Jim Grosbach319026d2014-03-18 22:09:10 +0000174 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000175 ".picsymbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000176 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000177 ".static_const");
Jim Grosbach319026d2014-03-18 22:09:10 +0000178 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000179 ".static_data");
Jim Grosbach319026d2014-03-18 22:09:10 +0000180 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000181 ".symbol_stub");
Jim Grosbach319026d2014-03-18 22:09:10 +0000182 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(".tdata");
183 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(".text");
184 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
Daniel Dunbarb94c5782013-01-18 01:25:25 +0000185 ".thread_init_func");
Jim Grosbach319026d2014-03-18 22:09:10 +0000186 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv");
Jim Grosbach14be61a2011-03-08 19:17:19 +0000187
Jim Grosbach319026d2014-03-18 22:09:10 +0000188 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident");
Tim Northover2d4d1612015-10-28 22:36:05 +0000189 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
190 ".watchos_version_min");
191 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min");
Jim Grosbach319026d2014-03-18 22:09:10 +0000192 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min");
193 addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(
Jim Grosbach448334a2014-03-18 22:09:05 +0000194 ".macosx_version_min");
Tim Northover2d4d1612015-10-28 22:36:05 +0000195
196 LastVersionMinDirective = SMLoc();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000197 }
198
Lang Hamesf9033bb2016-04-11 18:33:45 +0000199 bool parseDirectiveAltEntry(StringRef, SMLoc);
Jim Grosbach319026d2014-03-18 22:09:10 +0000200 bool parseDirectiveDesc(StringRef, SMLoc);
201 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
202 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
203 bool parseDirectiveLsym(StringRef, SMLoc);
204 bool parseDirectiveLinkerOption(StringRef, SMLoc);
205 bool parseDirectiveSection(StringRef, SMLoc);
206 bool parseDirectivePushSection(StringRef, SMLoc);
207 bool parseDirectivePopSection(StringRef, SMLoc);
208 bool parseDirectivePrevious(StringRef, SMLoc);
209 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
210 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
211 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
212 bool parseDirectiveTBSS(StringRef, SMLoc);
213 bool parseDirectiveZerofill(StringRef, SMLoc);
214 bool parseDirectiveDataRegion(StringRef, SMLoc);
215 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000216
217 // Named Section Directive
Jim Grosbach319026d2014-03-18 22:09:10 +0000218 bool parseSectionDirectiveBss(StringRef, SMLoc) {
219 return parseSectionSwitch("__DATA", "__bss");
Rafael Espindola3402c052013-10-02 14:09:29 +0000220 }
221
Jim Grosbach319026d2014-03-18 22:09:10 +0000222 bool parseSectionDirectiveConst(StringRef, SMLoc) {
223 return parseSectionSwitch("__TEXT", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000224 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000225
Jim Grosbach319026d2014-03-18 22:09:10 +0000226 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
227 return parseSectionSwitch("__TEXT", "__static_const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000228 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000229
Jim Grosbach319026d2014-03-18 22:09:10 +0000230 bool parseSectionDirectiveCString(StringRef, SMLoc) {
231 return parseSectionSwitch("__TEXT","__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000232 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000233 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000234
Jim Grosbach319026d2014-03-18 22:09:10 +0000235 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
236 return parseSectionSwitch("__TEXT", "__literal4",
David Majnemer7b583052014-03-07 07:36:05 +0000237 MachO::S_4BYTE_LITERALS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000238 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000239
Jim Grosbach319026d2014-03-18 22:09:10 +0000240 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
241 return parseSectionSwitch("__TEXT", "__literal8",
David Majnemer7b583052014-03-07 07:36:05 +0000242 MachO::S_8BYTE_LITERALS, 8);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000243 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000244
Jim Grosbach319026d2014-03-18 22:09:10 +0000245 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
246 return parseSectionSwitch("__TEXT","__literal16",
David Majnemer7b583052014-03-07 07:36:05 +0000247 MachO::S_16BYTE_LITERALS, 16);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000248 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000249
Jim Grosbach319026d2014-03-18 22:09:10 +0000250 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
251 return parseSectionSwitch("__TEXT","__constructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000252 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000253
Jim Grosbach319026d2014-03-18 22:09:10 +0000254 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
255 return parseSectionSwitch("__TEXT","__destructor");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000256 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000257
Jim Grosbach319026d2014-03-18 22:09:10 +0000258 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
259 return parseSectionSwitch("__TEXT","__fvmlib_init0");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000260 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000261
Jim Grosbach319026d2014-03-18 22:09:10 +0000262 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
263 return parseSectionSwitch("__TEXT","__fvmlib_init1");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000264 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000265
Jim Grosbach319026d2014-03-18 22:09:10 +0000266 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
267 return parseSectionSwitch("__TEXT","__symbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000268 MachO::S_SYMBOL_STUBS |
269 MachO::S_ATTR_PURE_INSTRUCTIONS,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000270 // FIXME: Different on PPC and ARM.
271 0, 16);
272 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000273
Jim Grosbach319026d2014-03-18 22:09:10 +0000274 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
275 return parseSectionSwitch("__TEXT","__picsymbol_stub",
David Majnemer7b583052014-03-07 07:36:05 +0000276 MachO::S_SYMBOL_STUBS |
277 MachO::S_ATTR_PURE_INSTRUCTIONS, 0, 26);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000278 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000279
Jim Grosbach319026d2014-03-18 22:09:10 +0000280 bool parseSectionDirectiveData(StringRef, SMLoc) {
281 return parseSectionSwitch("__DATA", "__data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000282 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000283
Jim Grosbach319026d2014-03-18 22:09:10 +0000284 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
285 return parseSectionSwitch("__DATA", "__static_data");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000286 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000287
Jim Grosbach319026d2014-03-18 22:09:10 +0000288 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
289 return parseSectionSwitch("__DATA", "__nl_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000290 MachO::S_NON_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000291 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000292
Jim Grosbach319026d2014-03-18 22:09:10 +0000293 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
294 return parseSectionSwitch("__DATA", "__la_symbol_ptr",
David Majnemer7b583052014-03-07 07:36:05 +0000295 MachO::S_LAZY_SYMBOL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000296 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000297
Tim Northover5c3140f2016-04-25 21:12:04 +0000298 bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
299 return parseSectionSwitch("__DATA", "__thread_ptr",
300 MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
301 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000302
Jim Grosbach319026d2014-03-18 22:09:10 +0000303 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
304 return parseSectionSwitch("__DATA", "__dyld");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000305 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000306
Jim Grosbach319026d2014-03-18 22:09:10 +0000307 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
308 return parseSectionSwitch("__DATA", "__mod_init_func",
David Majnemer7b583052014-03-07 07:36:05 +0000309 MachO::S_MOD_INIT_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000310 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000311
Jim Grosbach319026d2014-03-18 22:09:10 +0000312 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
313 return parseSectionSwitch("__DATA", "__mod_term_func",
David Majnemer7b583052014-03-07 07:36:05 +0000314 MachO::S_MOD_TERM_FUNC_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000315 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000316
Jim Grosbach319026d2014-03-18 22:09:10 +0000317 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
318 return parseSectionSwitch("__DATA", "__const");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000319 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000320
Jim Grosbach319026d2014-03-18 22:09:10 +0000321 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
322 return parseSectionSwitch("__OBJC", "__class",
David Majnemer7b583052014-03-07 07:36:05 +0000323 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000324 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000325
Jim Grosbach319026d2014-03-18 22:09:10 +0000326 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
327 return parseSectionSwitch("__OBJC", "__meta_class",
David Majnemer7b583052014-03-07 07:36:05 +0000328 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000329 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000330
Jim Grosbach319026d2014-03-18 22:09:10 +0000331 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
332 return parseSectionSwitch("__OBJC", "__cat_cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000333 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000334 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000335
Jim Grosbach319026d2014-03-18 22:09:10 +0000336 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
337 return parseSectionSwitch("__OBJC", "__cat_inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000338 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000339 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000340
Jim Grosbach319026d2014-03-18 22:09:10 +0000341 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
342 return parseSectionSwitch("__OBJC", "__protocol",
David Majnemer7b583052014-03-07 07:36:05 +0000343 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000344 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000345
Jim Grosbach319026d2014-03-18 22:09:10 +0000346 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
347 return parseSectionSwitch("__OBJC", "__string_object",
David Majnemer7b583052014-03-07 07:36:05 +0000348 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000349 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000350
Jim Grosbach319026d2014-03-18 22:09:10 +0000351 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
352 return parseSectionSwitch("__OBJC", "__cls_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000353 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000354 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000355
Jim Grosbach319026d2014-03-18 22:09:10 +0000356 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
357 return parseSectionSwitch("__OBJC", "__inst_meth",
David Majnemer7b583052014-03-07 07:36:05 +0000358 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000359 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000360
Jim Grosbach319026d2014-03-18 22:09:10 +0000361 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
362 return parseSectionSwitch("__OBJC", "__cls_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000363 MachO::S_ATTR_NO_DEAD_STRIP |
364 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000365 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000366
Jim Grosbach319026d2014-03-18 22:09:10 +0000367 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
368 return parseSectionSwitch("__OBJC", "__message_refs",
David Majnemer7b583052014-03-07 07:36:05 +0000369 MachO::S_ATTR_NO_DEAD_STRIP |
370 MachO::S_LITERAL_POINTERS, 4);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000371 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000372
Jim Grosbach319026d2014-03-18 22:09:10 +0000373 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
374 return parseSectionSwitch("__OBJC", "__symbols",
David Majnemer7b583052014-03-07 07:36:05 +0000375 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000376 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000377
Jim Grosbach319026d2014-03-18 22:09:10 +0000378 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
379 return parseSectionSwitch("__OBJC", "__category",
David Majnemer7b583052014-03-07 07:36:05 +0000380 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000381 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000382
Jim Grosbach319026d2014-03-18 22:09:10 +0000383 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
384 return parseSectionSwitch("__OBJC", "__class_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000385 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000386 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000387
Jim Grosbach319026d2014-03-18 22:09:10 +0000388 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
389 return parseSectionSwitch("__OBJC", "__instance_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000390 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000391 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000392
Jim Grosbach319026d2014-03-18 22:09:10 +0000393 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
394 return parseSectionSwitch("__OBJC", "__module_info",
David Majnemer7b583052014-03-07 07:36:05 +0000395 MachO::S_ATTR_NO_DEAD_STRIP);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000396 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000397
Jim Grosbach319026d2014-03-18 22:09:10 +0000398 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
399 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000400 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000401 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000402
Jim Grosbach319026d2014-03-18 22:09:10 +0000403 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
404 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000405 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000406 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000407
Jim Grosbach319026d2014-03-18 22:09:10 +0000408 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
409 return parseSectionSwitch("__TEXT", "__cstring",
David Majnemer7b583052014-03-07 07:36:05 +0000410 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000411 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000412
Jim Grosbach319026d2014-03-18 22:09:10 +0000413 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
414 return parseSectionSwitch("__OBJC", "__selector_strs",
David Majnemer7b583052014-03-07 07:36:05 +0000415 MachO::S_CSTRING_LITERALS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000416 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000417
Jim Grosbach319026d2014-03-18 22:09:10 +0000418 bool parseSectionDirectiveTData(StringRef, SMLoc) {
419 return parseSectionSwitch("__DATA", "__thread_data",
David Majnemer7b583052014-03-07 07:36:05 +0000420 MachO::S_THREAD_LOCAL_REGULAR);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000421 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000422
Jim Grosbach319026d2014-03-18 22:09:10 +0000423 bool parseSectionDirectiveText(StringRef, SMLoc) {
424 return parseSectionSwitch("__TEXT", "__text",
David Majnemer7b583052014-03-07 07:36:05 +0000425 MachO::S_ATTR_PURE_INSTRUCTIONS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000426 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000427
Jim Grosbach319026d2014-03-18 22:09:10 +0000428 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
429 return parseSectionSwitch("__DATA", "__thread_vars",
David Majnemer7b583052014-03-07 07:36:05 +0000430 MachO::S_THREAD_LOCAL_VARIABLES);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000431 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000432
Jim Grosbach319026d2014-03-18 22:09:10 +0000433 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
Jim Grosbach14be61a2011-03-08 19:17:19 +0000434 // Darwin silently ignores the .ident directive.
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000435 getParser().eatToEndOfStatement();
Jim Grosbach14be61a2011-03-08 19:17:19 +0000436 return false;
437 }
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000438
Jim Grosbach319026d2014-03-18 22:09:10 +0000439 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
440 return parseSectionSwitch("__DATA", "__thread_init",
David Majnemer7b583052014-03-07 07:36:05 +0000441 MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000442 }
443
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000444 bool parseVersionMin(StringRef, SMLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000445};
446
Bill Wendlingce4fe412012-08-08 06:30:30 +0000447} // end anonymous namespace
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000448
Mehdi Amini215ff8d2016-10-05 01:02:22 +0000449bool DarwinAsmParser::parseSectionSwitch(StringRef Segment, StringRef Section,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000450 unsigned TAA, unsigned Align,
451 unsigned StubSize) {
452 if (getLexer().isNot(AsmToken::EndOfStatement))
453 return TokError("unexpected token in section switching directive");
454 Lex();
455
456 // FIXME: Arch specific.
David Majnemer7b583052014-03-07 07:36:05 +0000457 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000458 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000459 Segment, Section, TAA, StubSize,
460 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000461
462 // Set the implicit alignment, if any.
463 //
464 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
465 // alignment on the section (e.g., if one manually inserts bytes into the
Bill Wendling5391eb62010-10-19 10:18:23 +0000466 // section, then just issuing the section switch directive will not realign
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000467 // the section. However, this is arguably more reasonable behavior, and there
468 // is no good reason for someone to intentionally emit incorrectly sized
469 // values into the implicitly aligned sections.
470 if (Align)
Rafael Espindola7b514962014-02-04 18:34:04 +0000471 getStreamer().EmitValueToAlignment(Align);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000472
473 return false;
474}
475
Lang Hamesf9033bb2016-04-11 18:33:45 +0000476/// parseDirectiveAltEntry
477/// ::= .alt_entry identifier
478bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
479 StringRef Name;
480 if (getParser().parseIdentifier(Name))
481 return TokError("expected identifier in directive");
482
483 // Look up symbol.
484 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
485
486 if (Sym->isDefined())
487 return TokError(".alt_entry must preceed symbol definition");
488
489 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_AltEntry))
490 return TokError("unable to emit symbol attribute");
491
492 Lex();
493 return false;
494}
495
Jim Grosbach319026d2014-03-18 22:09:10 +0000496/// parseDirectiveDesc
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000497/// ::= .desc identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000498bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000499 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000500 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000501 return TokError("expected identifier in directive");
502
503 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000504 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000505
506 if (getLexer().isNot(AsmToken::Comma))
507 return TokError("unexpected token in '.desc' directive");
508 Lex();
509
510 int64_t DescValue;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000511 if (getParser().parseAbsoluteExpression(DescValue))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000512 return true;
513
514 if (getLexer().isNot(AsmToken::EndOfStatement))
515 return TokError("unexpected token in '.desc' directive");
516
517 Lex();
518
519 // Set the n_desc field of this Symbol to this DescValue
520 getStreamer().EmitSymbolDesc(Sym, DescValue);
521
522 return false;
523}
524
Jim Grosbach319026d2014-03-18 22:09:10 +0000525/// parseDirectiveIndirectSymbol
Kevin Enderby3aeada22013-08-28 17:50:59 +0000526/// ::= .indirect_symbol identifier
Jim Grosbach319026d2014-03-18 22:09:10 +0000527bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
Eric Christopher445c9522016-10-14 05:47:37 +0000528 const MCSectionMachO *Current = static_cast<const MCSectionMachO *>(
529 getStreamer().getCurrentSectionOnly());
David Majnemercd481d32014-03-07 18:49:54 +0000530 MachO::SectionType SectionType = Current->getType();
David Majnemer7b583052014-03-07 07:36:05 +0000531 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
532 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
Tim Northover5c3140f2016-04-25 21:12:04 +0000533 SectionType != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
David Majnemer7b583052014-03-07 07:36:05 +0000534 SectionType != MachO::S_SYMBOL_STUBS)
Kevin Enderby3aeada22013-08-28 17:50:59 +0000535 return Error(Loc, "indirect symbol not in a symbol pointer or stub "
536 "section");
537
538 StringRef Name;
539 if (getParser().parseIdentifier(Name))
540 return TokError("expected identifier in .indirect_symbol directive");
541
Jim Grosbach6f482002015-05-18 18:43:14 +0000542 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Kevin Enderby3aeada22013-08-28 17:50:59 +0000543
544 // Assembler local symbols don't make any sense here. Complain loudly.
545 if (Sym->isTemporary())
546 return TokError("non-local symbol required in directive");
547
548 if (!getStreamer().EmitSymbolAttribute(Sym, MCSA_IndirectSymbol))
549 return TokError("unable to emit indirect symbol attribute for: " + Name);
550
551 if (getLexer().isNot(AsmToken::EndOfStatement))
552 return TokError("unexpected token in '.indirect_symbol' directive");
553
554 Lex();
555
556 return false;
557}
558
Jim Grosbach319026d2014-03-18 22:09:10 +0000559/// parseDirectiveDumpOrLoad
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000560/// ::= ( .dump | .load ) "filename"
Jim Grosbach319026d2014-03-18 22:09:10 +0000561bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000562 SMLoc IDLoc) {
563 bool IsDump = Directive == ".dump";
564 if (getLexer().isNot(AsmToken::String))
565 return TokError("expected string in '.dump' or '.load' directive");
566
567 Lex();
568
569 if (getLexer().isNot(AsmToken::EndOfStatement))
570 return TokError("unexpected token in '.dump' or '.load' directive");
571
572 Lex();
573
574 // FIXME: If/when .dump and .load are implemented they will be done in the
575 // the assembly parser and not have any need for an MCStreamer API.
576 if (IsDump)
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000577 return Warning(IDLoc, "ignoring directive .dump for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000578 else
Joerg Sonnenberger74ba2622011-05-19 18:00:13 +0000579 return Warning(IDLoc, "ignoring directive .load for now");
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000580}
581
Daniel Dunbar16004b82013-01-18 01:25:48 +0000582/// ParseDirectiveLinkerOption
583/// ::= .linker_option "string" ( , "string" )*
Jim Grosbach319026d2014-03-18 22:09:10 +0000584bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000585 SmallVector<std::string, 4> Args;
Eugene Zelenko4b6ff6b2017-02-10 01:33:54 +0000586 while (true) {
Daniel Dunbar16004b82013-01-18 01:25:48 +0000587 if (getLexer().isNot(AsmToken::String))
588 return TokError("expected string in '" + Twine(IDVal) + "' directive");
589
590 std::string Data;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000591 if (getParser().parseEscapedString(Data))
Daniel Dunbar16004b82013-01-18 01:25:48 +0000592 return true;
593
594 Args.push_back(Data);
595
Daniel Dunbar16004b82013-01-18 01:25:48 +0000596 if (getLexer().is(AsmToken::EndOfStatement))
597 break;
598
599 if (getLexer().isNot(AsmToken::Comma))
600 return TokError("unexpected token in '" + Twine(IDVal) + "' directive");
601 Lex();
602 }
603
604 getStreamer().EmitLinkerOptions(Args);
605 return false;
606}
607
Jim Grosbach319026d2014-03-18 22:09:10 +0000608/// parseDirectiveLsym
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000609/// ::= .lsym identifier , expression
Jim Grosbach319026d2014-03-18 22:09:10 +0000610bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000611 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000612 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000613 return TokError("expected identifier in directive");
614
615 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000616 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000617
618 if (getLexer().isNot(AsmToken::Comma))
619 return TokError("unexpected token in '.lsym' directive");
620 Lex();
621
622 const MCExpr *Value;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000623 if (getParser().parseExpression(Value))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000624 return true;
625
626 if (getLexer().isNot(AsmToken::EndOfStatement))
627 return TokError("unexpected token in '.lsym' directive");
628
629 Lex();
630
631 // We don't currently support this directive.
632 //
633 // FIXME: Diagnostic location!
634 (void) Sym;
635 return TokError("directive '.lsym' is unsupported");
636}
637
Jim Grosbach319026d2014-03-18 22:09:10 +0000638/// parseDirectiveSection:
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000639/// ::= .section identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000640bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000641 SMLoc Loc = getLexer().getLoc();
642
643 StringRef SectionName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000644 if (getParser().parseIdentifier(SectionName))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000645 return Error(Loc, "expected identifier after '.section' directive");
646
647 // Verify there is a following comma.
648 if (!getLexer().is(AsmToken::Comma))
649 return TokError("unexpected token in '.section' directive");
650
651 std::string SectionSpec = SectionName;
652 SectionSpec += ",";
653
654 // Add all the tokens until the end of the line, ParseSectionSpecifier will
655 // handle this.
656 StringRef EOL = getLexer().LexUntilEndOfStatement();
657 SectionSpec.append(EOL.begin(), EOL.end());
658
659 Lex();
660 if (getLexer().isNot(AsmToken::EndOfStatement))
661 return TokError("unexpected token in '.section' directive");
662 Lex();
663
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000664 StringRef Segment, Section;
Daniel Dunbarf1d62cf2011-03-17 16:25:24 +0000665 unsigned StubSize;
Stuart Hastings12d53122011-03-19 02:42:31 +0000666 unsigned TAA;
667 bool TAAParsed;
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000668 std::string ErrorStr =
669 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
Stuart Hastings12d53122011-03-19 02:42:31 +0000670 TAA, TAAParsed, StubSize);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000671
672 if (!ErrorStr.empty())
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000673 return Error(Loc, ErrorStr);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000674
Akira Hatanaka8ad73992015-10-15 05:28:38 +0000675 // Issue a warning if the target is not powerpc and Section is a *coal* section.
676 Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple();
677 Triple::ArchType ArchTy = TT.getArch();
678
679 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
680 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
681 .Case("__textcoal_nt", "__text")
682 .Case("__const_coal", "__const")
683 .Case("__datacoal_nt", "__data")
684 .Default(Section);
685
686 if (!Section.equals(NonCoalSection)) {
687 StringRef SectionVal(Loc.getPointer());
688 size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B);
689 SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B);
690 SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E);
691 getParser().Warning(Loc, "section \"" + Section + "\" is deprecated",
692 SMRange(BLoc, ELoc));
693 getParser().Note(Loc, "change section name to \"" + NonCoalSection +
694 "\"", SMRange(BLoc, ELoc));
695 }
696 }
697
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000698 // FIXME: Arch specific.
699 bool isText = Segment == "__TEXT"; // FIXME: Hack.
700 getStreamer().SwitchSection(getContext().getMachOSection(
Rafael Espindola449711c2015-11-18 06:02:15 +0000701 Segment, Section, TAA, StubSize,
702 isText ? SectionKind::getText() : SectionKind::getData()));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000703 return false;
704}
705
Bill Wendlingce4fe412012-08-08 06:30:30 +0000706/// ParseDirectivePushSection:
707/// ::= .pushsection identifier (',' identifier)*
Jim Grosbach319026d2014-03-18 22:09:10 +0000708bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000709 getStreamer().PushSection();
710
Jim Grosbach319026d2014-03-18 22:09:10 +0000711 if (parseDirectiveSection(S, Loc)) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000712 getStreamer().PopSection();
713 return true;
714 }
715
716 return false;
717}
718
719/// ParseDirectivePopSection:
720/// ::= .popsection
Jim Grosbach319026d2014-03-18 22:09:10 +0000721bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
Bill Wendlingce4fe412012-08-08 06:30:30 +0000722 if (!getStreamer().PopSection())
723 return TokError(".popsection without corresponding .pushsection");
724 return false;
725}
726
727/// ParseDirectivePrevious:
728/// ::= .previous
Jim Grosbach319026d2014-03-18 22:09:10 +0000729bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000730 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000731 if (!PreviousSection.first)
732 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000733 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Bill Wendlingce4fe412012-08-08 06:30:30 +0000734 return false;
735}
736
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000737/// ParseDirectiveSecureLogUnique
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000738/// ::= .secure_log_unique ... message ...
Jim Grosbach319026d2014-03-18 22:09:10 +0000739bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000740 StringRef LogMessage = getParser().parseStringToEndOfStatement();
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000741 if (getLexer().isNot(AsmToken::EndOfStatement))
742 return TokError("unexpected token in '.secure_log_unique' directive");
743
David Blaikiedc3f01e2015-03-09 01:57:13 +0000744 if (getContext().getSecureLogUsed())
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000745 return Error(IDLoc, ".secure_log_unique specified multiple times");
746
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000747 // Get the secure log path.
748 const char *SecureLogFile = getContext().getSecureLogFile();
Craig Topper353eda42014-04-24 06:44:33 +0000749 if (!SecureLogFile)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000750 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
751 "environment variable unset.");
752
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000753 // Open the secure log file if we haven't already.
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000754 raw_fd_ostream *OS = getContext().getSecureLog();
Craig Topper353eda42014-04-24 06:44:33 +0000755 if (!OS) {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000756 std::error_code EC;
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000757 auto NewOS = llvm::make_unique<raw_fd_ostream>(
Mehdi Amini215ff8d2016-10-05 01:02:22 +0000758 StringRef(SecureLogFile), EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000759 if (EC)
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000760 return Error(IDLoc, Twine("can't open secure log file: ") +
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000761 SecureLogFile + " (" + EC.message() + ")");
Rafael Espindolac49ac5e2015-12-16 23:49:14 +0000762 OS = NewOS.get();
763 getContext().setSecureLog(std::move(NewOS));
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000764 }
765
Daniel Dunbar40a564f2010-07-18 20:15:59 +0000766 // Write the message.
Alp Tokera55b95b2014-07-06 10:33:31 +0000767 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(IDLoc);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000768 *OS << getSourceManager().getBufferInfo(CurBuf).Buffer->getBufferIdentifier()
769 << ":" << getSourceManager().FindLineNumber(IDLoc, CurBuf) << ":"
770 << LogMessage + "\n";
771
772 getContext().setSecureLogUsed(true);
773
774 return false;
775}
776
777/// ParseDirectiveSecureLogReset
778/// ::= .secure_log_reset
Jim Grosbach319026d2014-03-18 22:09:10 +0000779bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000780 if (getLexer().isNot(AsmToken::EndOfStatement))
781 return TokError("unexpected token in '.secure_log_reset' directive");
782
783 Lex();
784
785 getContext().setSecureLogUsed(false);
786
787 return false;
788}
789
Jim Grosbach319026d2014-03-18 22:09:10 +0000790/// parseDirectiveSubsectionsViaSymbols
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000791/// ::= .subsections_via_symbols
Jim Grosbach319026d2014-03-18 22:09:10 +0000792bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000793 if (getLexer().isNot(AsmToken::EndOfStatement))
794 return TokError("unexpected token in '.subsections_via_symbols' directive");
795
796 Lex();
797
798 getStreamer().EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
799
800 return false;
801}
802
803/// ParseDirectiveTBSS
804/// ::= .tbss identifier, size, align
Jim Grosbach319026d2014-03-18 22:09:10 +0000805bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000806 SMLoc IDLoc = getLexer().getLoc();
807 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000808 if (getParser().parseIdentifier(Name))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000809 return TokError("expected identifier in directive");
810
811 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000812 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000813
814 if (getLexer().isNot(AsmToken::Comma))
815 return TokError("unexpected token in directive");
816 Lex();
817
818 int64_t Size;
819 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000820 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000821 return true;
822
823 int64_t Pow2Alignment = 0;
824 SMLoc Pow2AlignmentLoc;
825 if (getLexer().is(AsmToken::Comma)) {
826 Lex();
827 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000828 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000829 return true;
830 }
831
832 if (getLexer().isNot(AsmToken::EndOfStatement))
833 return TokError("unexpected token in '.tbss' directive");
834
835 Lex();
836
837 if (Size < 0)
838 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
839 "zero");
840
841 // FIXME: Diagnose overflow.
842 if (Pow2Alignment < 0)
843 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
844 "than zero");
845
846 if (!Sym->isUndefined())
847 return Error(IDLoc, "invalid symbol redefinition");
848
849 getStreamer().EmitTBSSSymbol(getContext().getMachOSection(
850 "__DATA", "__thread_bss",
David Majnemer7b583052014-03-07 07:36:05 +0000851 MachO::S_THREAD_LOCAL_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000852 0, SectionKind::getThreadBSS()),
853 Sym, Size, 1 << Pow2Alignment);
854
855 return false;
856}
857
858/// ParseDirectiveZerofill
859/// ::= .zerofill segname , sectname [, identifier , size_expression [
860/// , align_expression ]]
Jim Grosbach319026d2014-03-18 22:09:10 +0000861bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000862 StringRef Segment;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000863 if (getParser().parseIdentifier(Segment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000864 return TokError("expected segment name after '.zerofill' directive");
865
866 if (getLexer().isNot(AsmToken::Comma))
867 return TokError("unexpected token in directive");
868 Lex();
869
870 StringRef Section;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000871 if (getParser().parseIdentifier(Section))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000872 return TokError("expected section name after comma in '.zerofill' "
873 "directive");
874
875 // If this is the end of the line all that was wanted was to create the
876 // the section but with no symbol.
877 if (getLexer().is(AsmToken::EndOfStatement)) {
878 // Create the zerofill section but no symbol
879 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000880 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000881 0, SectionKind::getBSS()));
882 return false;
883 }
884
885 if (getLexer().isNot(AsmToken::Comma))
886 return TokError("unexpected token in directive");
887 Lex();
888
889 SMLoc IDLoc = getLexer().getLoc();
890 StringRef IDStr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000891 if (getParser().parseIdentifier(IDStr))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000892 return TokError("expected identifier in directive");
893
894 // handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000895 MCSymbol *Sym = getContext().getOrCreateSymbol(IDStr);
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000896
897 if (getLexer().isNot(AsmToken::Comma))
898 return TokError("unexpected token in directive");
899 Lex();
900
901 int64_t Size;
902 SMLoc SizeLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000903 if (getParser().parseAbsoluteExpression(Size))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000904 return true;
905
906 int64_t Pow2Alignment = 0;
907 SMLoc Pow2AlignmentLoc;
908 if (getLexer().is(AsmToken::Comma)) {
909 Lex();
910 Pow2AlignmentLoc = getLexer().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000911 if (getParser().parseAbsoluteExpression(Pow2Alignment))
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000912 return true;
913 }
914
915 if (getLexer().isNot(AsmToken::EndOfStatement))
916 return TokError("unexpected token in '.zerofill' directive");
917
918 Lex();
919
920 if (Size < 0)
921 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
922 "than zero");
923
924 // NOTE: The alignment in the directive is a power of 2 value, the assembler
925 // may internally end up wanting an alignment in bytes.
926 // FIXME: Diagnose overflow.
927 if (Pow2Alignment < 0)
928 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
929 "can't be less than zero");
930
931 if (!Sym->isUndefined())
932 return Error(IDLoc, "invalid symbol redefinition");
933
934 // Create the zerofill Symbol with Size and Pow2Alignment
935 //
936 // FIXME: Arch specific.
937 getStreamer().EmitZerofill(getContext().getMachOSection(
David Majnemer7b583052014-03-07 07:36:05 +0000938 Segment, Section, MachO::S_ZEROFILL,
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +0000939 0, SectionKind::getBSS()),
940 Sym, Size, 1 << Pow2Alignment);
941
942 return false;
943}
944
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000945/// ParseDirectiveDataRegion
946/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
Jim Grosbach319026d2014-03-18 22:09:10 +0000947bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000948 if (getLexer().is(AsmToken::EndOfStatement)) {
949 Lex();
950 getStreamer().EmitDataRegion(MCDR_DataRegion);
951 return false;
952 }
953 StringRef RegionType;
954 SMLoc Loc = getParser().getTok().getLoc();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000955 if (getParser().parseIdentifier(RegionType))
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000956 return TokError("expected region type after '.data_region' directive");
957 int Kind = StringSwitch<int>(RegionType)
958 .Case("jt8", MCDR_DataRegionJT8)
959 .Case("jt16", MCDR_DataRegionJT16)
960 .Case("jt32", MCDR_DataRegionJT32)
961 .Default(-1);
962 if (Kind == -1)
963 return Error(Loc, "unknown region type in '.data_region' directive");
964 Lex();
965
966 getStreamer().EmitDataRegion((MCDataRegionType)Kind);
967 return false;
968}
969
970/// ParseDirectiveDataRegionEnd
971/// ::= .end_data_region
Jim Grosbach319026d2014-03-18 22:09:10 +0000972bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
Jim Grosbach4b63d2a2012-05-18 19:12:01 +0000973 if (getLexer().isNot(AsmToken::EndOfStatement))
974 return TokError("unexpected token in '.end_data_region' directive");
975
976 Lex();
977 getStreamer().EmitDataRegion(MCDR_DataRegionEnd);
978 return false;
979}
980
Jim Grosbach319026d2014-03-18 22:09:10 +0000981/// parseVersionMin
Jim Grosbach448334a2014-03-18 22:09:05 +0000982/// ::= .ios_version_min major,minor[,update]
983/// ::= .macosx_version_min major,minor[,update]
Tim Northover2d4d1612015-10-28 22:36:05 +0000984bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) {
Jim Grosbach448334a2014-03-18 22:09:05 +0000985 int64_t Major = 0, Minor = 0, Update = 0;
986 int Kind = StringSwitch<int>(Directive)
Tim Northover2d4d1612015-10-28 22:36:05 +0000987 .Case(".watchos_version_min", MCVM_WatchOSVersionMin)
988 .Case(".tvos_version_min", MCVM_TvOSVersionMin)
Jim Grosbach448334a2014-03-18 22:09:05 +0000989 .Case(".ios_version_min", MCVM_IOSVersionMin)
990 .Case(".macosx_version_min", MCVM_OSXVersionMin);
991 // Get the major version number.
992 if (getLexer().isNot(AsmToken::Integer))
993 return TokError("invalid OS major version number");
994 Major = getLexer().getTok().getIntVal();
995 if (Major > 65535 || Major <= 0)
996 return TokError("invalid OS major version number");
997 Lex();
998 if (getLexer().isNot(AsmToken::Comma))
999 return TokError("minor OS version number required, comma expected");
1000 Lex();
1001 // Get the minor version number.
1002 if (getLexer().isNot(AsmToken::Integer))
1003 return TokError("invalid OS minor version number");
1004 Minor = getLexer().getTok().getIntVal();
1005 if (Minor > 255 || Minor < 0)
1006 return TokError("invalid OS minor version number");
1007 Lex();
1008 // Get the update level, if specified
1009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1010 if (getLexer().isNot(AsmToken::Comma))
1011 return TokError("invalid update specifier, comma expected");
1012 Lex();
1013 if (getLexer().isNot(AsmToken::Integer))
1014 return TokError("invalid OS update number");
1015 Update = getLexer().getTok().getIntVal();
Davide Italianoa8d89f32016-07-05 16:56:09 +00001016 if (Update > 255 || Update < 0)
1017 return TokError("invalid OS update number");
Jim Grosbach448334a2014-03-18 22:09:05 +00001018 Lex();
1019 }
1020
Tim Northover2d4d1612015-10-28 22:36:05 +00001021 const Triple &T = getContext().getObjectFileInfo()->getTargetTriple();
1022 Triple::OSType ExpectedOS = Triple::UnknownOS;
1023 switch ((MCVersionMinType)Kind) {
1024 case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break;
1025 case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break;
1026 case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break;
1027 case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break;
1028 }
1029 if (T.getOS() != ExpectedOS)
1030 Warning(Loc, Directive + " should only be used for " +
1031 Triple::getOSTypeName(ExpectedOS) + " targets");
1032
1033 if (LastVersionMinDirective.isValid()) {
1034 Warning(Loc, "overriding previous version_min directive");
1035 Note(LastVersionMinDirective, "previous definition is here");
1036 }
1037 LastVersionMinDirective = Loc;
1038
Jim Grosbach448334a2014-03-18 22:09:05 +00001039 // We've parsed a correct version specifier, so send it to the streamer.
1040 getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update);
1041
1042 return false;
1043}
1044
Daniel Dunbar0cb91cf2010-07-12 20:51:51 +00001045namespace llvm {
1046
1047MCAsmParserExtension *createDarwinAsmParser() {
1048 return new DarwinAsmParser;
1049}
1050
Bill Wendlingce4fe412012-08-08 06:30:30 +00001051} // end llvm namespace