1//===- DarwinAsmParser.cpp - Darwin (Mach-O) Assembly Parser --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/SmallVector.h"
10#include "llvm/ADT/StringRef.h"
11#include "llvm/ADT/StringSwitch.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/BinaryFormat/MachO.h"
14#include "llvm/MC/MCContext.h"
15#include "llvm/MC/MCDirectives.h"
16#include "llvm/MC/MCParser/AsmLexer.h"
17#include "llvm/MC/MCParser/MCAsmParser.h"
18#include "llvm/MC/MCParser/MCAsmParserExtension.h"
19#include "llvm/MC/MCSectionMachO.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/MC/SectionKind.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SMLoc.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/TargetParser/Triple.h"
30#include <cstddef>
31#include <cstdint>
32#include <string>
33#include <system_error>
34#include <utility>
35
36using namespace llvm;
37
38namespace {
39
40/// Implementation of directive handling which is shared across all
41/// Darwin targets.
42class DarwinAsmParser : public MCAsmParserExtension {
43 template<bool (DarwinAsmParser::*HandlerMethod)(StringRef, SMLoc)>
44 void addDirectiveHandler(StringRef Directive) {
45 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
46 this, HandleDirective<DarwinAsmParser, HandlerMethod>);
47 getParser().addDirectiveHandler(Directive, Handler);
48 }
49
50 bool parseSectionSwitch(StringRef Segment, StringRef Section,
51 unsigned TAA = 0, unsigned ImplicitAlign = 0,
52 unsigned StubSize = 0);
53
54 SMLoc LastVersionDirective;
55
56public:
57 DarwinAsmParser() = default;
58
59 void Initialize(MCAsmParser &Parser) override {
60 // Call the base implementation.
61 this->MCAsmParserExtension::Initialize(Parser);
62
63 addDirectiveHandler<&DarwinAsmParser::parseDirectiveAltEntry>(Directive: ".alt_entry");
64 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDesc>(Directive: ".desc");
65 addDirectiveHandler<&DarwinAsmParser::parseDirectiveIndirectSymbol>(
66 Directive: ".indirect_symbol");
67 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLsym>(Directive: ".lsym");
68 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSubsectionsViaSymbols>(
69 Directive: ".subsections_via_symbols");
70 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(Directive: ".dump");
71 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDumpOrLoad>(Directive: ".load");
72 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSection>(Directive: ".section");
73 addDirectiveHandler<&DarwinAsmParser::parseDirectivePushSection>(
74 Directive: ".pushsection");
75 addDirectiveHandler<&DarwinAsmParser::parseDirectivePopSection>(
76 Directive: ".popsection");
77 addDirectiveHandler<&DarwinAsmParser::parseDirectivePrevious>(Directive: ".previous");
78 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogUnique>(
79 Directive: ".secure_log_unique");
80 addDirectiveHandler<&DarwinAsmParser::parseDirectiveSecureLogReset>(
81 Directive: ".secure_log_reset");
82 addDirectiveHandler<&DarwinAsmParser::parseDirectiveTBSS>(Directive: ".tbss");
83 addDirectiveHandler<&DarwinAsmParser::parseDirectiveZerofill>(Directive: ".zerofill");
84
85 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegion>(
86 Directive: ".data_region");
87 addDirectiveHandler<&DarwinAsmParser::parseDirectiveDataRegionEnd>(
88 Directive: ".end_data_region");
89
90 // Special section directives.
91 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveBss>(Directive: ".bss");
92 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConst>(Directive: ".const");
93 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstData>(
94 Directive: ".const_data");
95 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveConstructor>(
96 Directive: ".constructor");
97 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveCString>(
98 Directive: ".cstring");
99 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveData>(Directive: ".data");
100 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDestructor>(
101 Directive: ".destructor");
102 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveDyld>(Directive: ".dyld");
103 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit0>(
104 Directive: ".fvmlib_init0");
105 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveFVMLibInit1>(
106 Directive: ".fvmlib_init1");
107 addDirectiveHandler<
108 &DarwinAsmParser::parseSectionDirectiveLazySymbolPointers>(
109 Directive: ".lazy_symbol_pointer");
110 addDirectiveHandler<&DarwinAsmParser::parseDirectiveLinkerOption>(
111 Directive: ".linker_option");
112 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral16>(
113 Directive: ".literal16");
114 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral4>(
115 Directive: ".literal4");
116 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveLiteral8>(
117 Directive: ".literal8");
118 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModInitFunc>(
119 Directive: ".mod_init_func");
120 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveModTermFunc>(
121 Directive: ".mod_term_func");
122 addDirectiveHandler<
123 &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
124 Directive: ".non_lazy_symbol_pointer");
125 addDirectiveHandler<
126 &DarwinAsmParser::parseSectionDirectiveThreadLocalVariablePointers>(
127 Directive: ".thread_local_variable_pointer");
128 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
129 Directive: ".objc_cat_cls_meth");
130 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
131 Directive: ".objc_cat_inst_meth");
132 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCategory>(
133 Directive: ".objc_category");
134 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClass>(
135 Directive: ".objc_class");
136 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassNames>(
137 Directive: ".objc_class_names");
138 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClassVars>(
139 Directive: ".objc_class_vars");
140 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsMeth>(
141 Directive: ".objc_cls_meth");
142 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCClsRefs>(
143 Directive: ".objc_cls_refs");
144 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCInstMeth>(
145 Directive: ".objc_inst_meth");
146 addDirectiveHandler<
147 &DarwinAsmParser::parseSectionDirectiveObjCInstanceVars>(
148 Directive: ".objc_instance_vars");
149 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMessageRefs>(
150 Directive: ".objc_message_refs");
151 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCMetaClass>(
152 Directive: ".objc_meta_class");
153 addDirectiveHandler<
154 &DarwinAsmParser::parseSectionDirectiveObjCMethVarNames>(
155 Directive: ".objc_meth_var_names");
156 addDirectiveHandler<
157 &DarwinAsmParser::parseSectionDirectiveObjCMethVarTypes>(
158 Directive: ".objc_meth_var_types");
159 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCModuleInfo>(
160 Directive: ".objc_module_info");
161 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCProtocol>(
162 Directive: ".objc_protocol");
163 addDirectiveHandler<
164 &DarwinAsmParser::parseSectionDirectiveObjCSelectorStrs>(
165 Directive: ".objc_selector_strs");
166 addDirectiveHandler<
167 &DarwinAsmParser::parseSectionDirectiveObjCStringObject>(
168 Directive: ".objc_string_object");
169 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCSymbols>(
170 Directive: ".objc_symbols");
171 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectivePICSymbolStub>(
172 Directive: ".picsymbol_stub");
173 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticConst>(
174 Directive: ".static_const");
175 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveStaticData>(
176 Directive: ".static_data");
177 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveSymbolStub>(
178 Directive: ".symbol_stub");
179 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTData>(Directive: ".tdata");
180 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveText>(Directive: ".text");
181 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveThreadInitFunc>(
182 Directive: ".thread_init_func");
183 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(Directive: ".tlv");
184
185 addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(Directive: ".ident");
186 addDirectiveHandler<&DarwinAsmParser::parseWatchOSVersionMin>(
187 Directive: ".watchos_version_min");
188 addDirectiveHandler<&DarwinAsmParser::parseTvOSVersionMin>(
189 Directive: ".tvos_version_min");
190 addDirectiveHandler<&DarwinAsmParser::parseIOSVersionMin>(
191 Directive: ".ios_version_min");
192 addDirectiveHandler<&DarwinAsmParser::parseMacOSXVersionMin>(
193 Directive: ".macosx_version_min");
194 addDirectiveHandler<&DarwinAsmParser::parseBuildVersion>(Directive: ".build_version");
195 addDirectiveHandler<&DarwinAsmParser::parseTargetTripleDirective>(
196 Directive: ".target_triple");
197 addDirectiveHandler<&DarwinAsmParser::parseDirectiveCGProfile>(
198 Directive: ".cg_profile");
199
200 LastVersionDirective = SMLoc();
201 }
202
203 bool parseDirectiveAltEntry(StringRef, SMLoc);
204 bool parseDirectiveDesc(StringRef, SMLoc);
205 bool parseDirectiveIndirectSymbol(StringRef, SMLoc);
206 bool parseDirectiveDumpOrLoad(StringRef, SMLoc);
207 bool parseDirectiveLsym(StringRef, SMLoc);
208 bool parseDirectiveLinkerOption(StringRef, SMLoc);
209 bool parseDirectiveSection(StringRef, SMLoc);
210 bool parseDirectivePushSection(StringRef, SMLoc);
211 bool parseDirectivePopSection(StringRef, SMLoc);
212 bool parseDirectivePrevious(StringRef, SMLoc);
213 bool parseDirectiveSecureLogReset(StringRef, SMLoc);
214 bool parseDirectiveSecureLogUnique(StringRef, SMLoc);
215 bool parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc);
216 bool parseDirectiveTBSS(StringRef, SMLoc);
217 bool parseDirectiveZerofill(StringRef, SMLoc);
218 bool parseDirectiveDataRegion(StringRef, SMLoc);
219 bool parseDirectiveDataRegionEnd(StringRef, SMLoc);
220
221 // Named Section Directive
222 bool parseSectionDirectiveBss(StringRef, SMLoc) {
223 return parseSectionSwitch(Segment: "__DATA", Section: "__bss");
224 }
225
226 bool parseSectionDirectiveConst(StringRef, SMLoc) {
227 return parseSectionSwitch(Segment: "__TEXT", Section: "__const");
228 }
229
230 bool parseSectionDirectiveStaticConst(StringRef, SMLoc) {
231 return parseSectionSwitch(Segment: "__TEXT", Section: "__static_const");
232 }
233
234 bool parseSectionDirectiveCString(StringRef, SMLoc) {
235 return parseSectionSwitch(Segment: "__TEXT",Section: "__cstring",
236 TAA: MachO::S_CSTRING_LITERALS);
237 }
238
239 bool parseSectionDirectiveLiteral4(StringRef, SMLoc) {
240 return parseSectionSwitch(Segment: "__TEXT", Section: "__literal4",
241 TAA: MachO::S_4BYTE_LITERALS, ImplicitAlign: 4);
242 }
243
244 bool parseSectionDirectiveLiteral8(StringRef, SMLoc) {
245 return parseSectionSwitch(Segment: "__TEXT", Section: "__literal8",
246 TAA: MachO::S_8BYTE_LITERALS, ImplicitAlign: 8);
247 }
248
249 bool parseSectionDirectiveLiteral16(StringRef, SMLoc) {
250 return parseSectionSwitch(Segment: "__TEXT",Section: "__literal16",
251 TAA: MachO::S_16BYTE_LITERALS, ImplicitAlign: 16);
252 }
253
254 bool parseSectionDirectiveConstructor(StringRef, SMLoc) {
255 return parseSectionSwitch(Segment: "__TEXT",Section: "__constructor");
256 }
257
258 bool parseSectionDirectiveDestructor(StringRef, SMLoc) {
259 return parseSectionSwitch(Segment: "__TEXT",Section: "__destructor");
260 }
261
262 bool parseSectionDirectiveFVMLibInit0(StringRef, SMLoc) {
263 return parseSectionSwitch(Segment: "__TEXT",Section: "__fvmlib_init0");
264 }
265
266 bool parseSectionDirectiveFVMLibInit1(StringRef, SMLoc) {
267 return parseSectionSwitch(Segment: "__TEXT",Section: "__fvmlib_init1");
268 }
269
270 bool parseSectionDirectiveSymbolStub(StringRef, SMLoc) {
271 return parseSectionSwitch(Segment: "__TEXT",Section: "__symbol_stub",
272 TAA: MachO::S_SYMBOL_STUBS |
273 MachO::S_ATTR_PURE_INSTRUCTIONS,
274 // FIXME: Different on PPC and ARM.
275 ImplicitAlign: 0, StubSize: 16);
276 }
277
278 bool parseSectionDirectivePICSymbolStub(StringRef, SMLoc) {
279 return parseSectionSwitch(Segment: "__TEXT",Section: "__picsymbol_stub",
280 TAA: MachO::S_SYMBOL_STUBS |
281 MachO::S_ATTR_PURE_INSTRUCTIONS, ImplicitAlign: 0, StubSize: 26);
282 }
283
284 bool parseSectionDirectiveData(StringRef, SMLoc) {
285 return parseSectionSwitch(Segment: "__DATA", Section: "__data");
286 }
287
288 bool parseSectionDirectiveStaticData(StringRef, SMLoc) {
289 return parseSectionSwitch(Segment: "__DATA", Section: "__static_data");
290 }
291
292 bool parseSectionDirectiveNonLazySymbolPointers(StringRef, SMLoc) {
293 return parseSectionSwitch(Segment: "__DATA", Section: "__nl_symbol_ptr",
294 TAA: MachO::S_NON_LAZY_SYMBOL_POINTERS, ImplicitAlign: 4);
295 }
296
297 bool parseSectionDirectiveLazySymbolPointers(StringRef, SMLoc) {
298 return parseSectionSwitch(Segment: "__DATA", Section: "__la_symbol_ptr",
299 TAA: MachO::S_LAZY_SYMBOL_POINTERS, ImplicitAlign: 4);
300 }
301
302 bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
303 return parseSectionSwitch(Segment: "__DATA", Section: "__thread_ptr",
304 TAA: MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, ImplicitAlign: 4);
305 }
306
307 bool parseSectionDirectiveDyld(StringRef, SMLoc) {
308 return parseSectionSwitch(Segment: "__DATA", Section: "__dyld");
309 }
310
311 bool parseSectionDirectiveModInitFunc(StringRef, SMLoc) {
312 return parseSectionSwitch(Segment: "__DATA", Section: "__mod_init_func",
313 TAA: MachO::S_MOD_INIT_FUNC_POINTERS, ImplicitAlign: 4);
314 }
315
316 bool parseSectionDirectiveModTermFunc(StringRef, SMLoc) {
317 return parseSectionSwitch(Segment: "__DATA", Section: "__mod_term_func",
318 TAA: MachO::S_MOD_TERM_FUNC_POINTERS, ImplicitAlign: 4);
319 }
320
321 bool parseSectionDirectiveConstData(StringRef, SMLoc) {
322 return parseSectionSwitch(Segment: "__DATA", Section: "__const");
323 }
324
325 bool parseSectionDirectiveObjCClass(StringRef, SMLoc) {
326 return parseSectionSwitch(Segment: "__OBJC", Section: "__class",
327 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
328 }
329
330 bool parseSectionDirectiveObjCMetaClass(StringRef, SMLoc) {
331 return parseSectionSwitch(Segment: "__OBJC", Section: "__meta_class",
332 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
333 }
334
335 bool parseSectionDirectiveObjCCatClsMeth(StringRef, SMLoc) {
336 return parseSectionSwitch(Segment: "__OBJC", Section: "__cat_cls_meth",
337 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
338 }
339
340 bool parseSectionDirectiveObjCCatInstMeth(StringRef, SMLoc) {
341 return parseSectionSwitch(Segment: "__OBJC", Section: "__cat_inst_meth",
342 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
343 }
344
345 bool parseSectionDirectiveObjCProtocol(StringRef, SMLoc) {
346 return parseSectionSwitch(Segment: "__OBJC", Section: "__protocol",
347 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
348 }
349
350 bool parseSectionDirectiveObjCStringObject(StringRef, SMLoc) {
351 return parseSectionSwitch(Segment: "__OBJC", Section: "__string_object",
352 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
353 }
354
355 bool parseSectionDirectiveObjCClsMeth(StringRef, SMLoc) {
356 return parseSectionSwitch(Segment: "__OBJC", Section: "__cls_meth",
357 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
358 }
359
360 bool parseSectionDirectiveObjCInstMeth(StringRef, SMLoc) {
361 return parseSectionSwitch(Segment: "__OBJC", Section: "__inst_meth",
362 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
363 }
364
365 bool parseSectionDirectiveObjCClsRefs(StringRef, SMLoc) {
366 return parseSectionSwitch(Segment: "__OBJC", Section: "__cls_refs",
367 TAA: MachO::S_ATTR_NO_DEAD_STRIP |
368 MachO::S_LITERAL_POINTERS, ImplicitAlign: 4);
369 }
370
371 bool parseSectionDirectiveObjCMessageRefs(StringRef, SMLoc) {
372 return parseSectionSwitch(Segment: "__OBJC", Section: "__message_refs",
373 TAA: MachO::S_ATTR_NO_DEAD_STRIP |
374 MachO::S_LITERAL_POINTERS, ImplicitAlign: 4);
375 }
376
377 bool parseSectionDirectiveObjCSymbols(StringRef, SMLoc) {
378 return parseSectionSwitch(Segment: "__OBJC", Section: "__symbols",
379 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
380 }
381
382 bool parseSectionDirectiveObjCCategory(StringRef, SMLoc) {
383 return parseSectionSwitch(Segment: "__OBJC", Section: "__category",
384 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
385 }
386
387 bool parseSectionDirectiveObjCClassVars(StringRef, SMLoc) {
388 return parseSectionSwitch(Segment: "__OBJC", Section: "__class_vars",
389 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
390 }
391
392 bool parseSectionDirectiveObjCInstanceVars(StringRef, SMLoc) {
393 return parseSectionSwitch(Segment: "__OBJC", Section: "__instance_vars",
394 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
395 }
396
397 bool parseSectionDirectiveObjCModuleInfo(StringRef, SMLoc) {
398 return parseSectionSwitch(Segment: "__OBJC", Section: "__module_info",
399 TAA: MachO::S_ATTR_NO_DEAD_STRIP);
400 }
401
402 bool parseSectionDirectiveObjCClassNames(StringRef, SMLoc) {
403 return parseSectionSwitch(Segment: "__TEXT", Section: "__cstring",
404 TAA: MachO::S_CSTRING_LITERALS);
405 }
406
407 bool parseSectionDirectiveObjCMethVarTypes(StringRef, SMLoc) {
408 return parseSectionSwitch(Segment: "__TEXT", Section: "__cstring",
409 TAA: MachO::S_CSTRING_LITERALS);
410 }
411
412 bool parseSectionDirectiveObjCMethVarNames(StringRef, SMLoc) {
413 return parseSectionSwitch(Segment: "__TEXT", Section: "__cstring",
414 TAA: MachO::S_CSTRING_LITERALS);
415 }
416
417 bool parseSectionDirectiveObjCSelectorStrs(StringRef, SMLoc) {
418 return parseSectionSwitch(Segment: "__OBJC", Section: "__selector_strs",
419 TAA: MachO::S_CSTRING_LITERALS);
420 }
421
422 bool parseSectionDirectiveTData(StringRef, SMLoc) {
423 return parseSectionSwitch(Segment: "__DATA", Section: "__thread_data",
424 TAA: MachO::S_THREAD_LOCAL_REGULAR);
425 }
426
427 bool parseSectionDirectiveText(StringRef, SMLoc) {
428 return parseSectionSwitch(Segment: "__TEXT", Section: "__text",
429 TAA: MachO::S_ATTR_PURE_INSTRUCTIONS);
430 }
431
432 bool parseSectionDirectiveTLV(StringRef, SMLoc) {
433 return parseSectionSwitch(Segment: "__DATA", Section: "__thread_vars",
434 TAA: MachO::S_THREAD_LOCAL_VARIABLES);
435 }
436
437 bool parseSectionDirectiveIdent(StringRef, SMLoc) {
438 // Darwin silently ignores the .ident directive.
439 getParser().eatToEndOfStatement();
440 return false;
441 }
442
443 bool parseSectionDirectiveThreadInitFunc(StringRef, SMLoc) {
444 return parseSectionSwitch(Segment: "__DATA", Section: "__thread_init",
445 TAA: MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
446 }
447
448 bool parseWatchOSVersionMin(StringRef Directive, SMLoc Loc) {
449 return parseVersionMin(Directive, Loc, Type: MCVM_WatchOSVersionMin);
450 }
451 bool parseTvOSVersionMin(StringRef Directive, SMLoc Loc) {
452 return parseVersionMin(Directive, Loc, Type: MCVM_TvOSVersionMin);
453 }
454 bool parseIOSVersionMin(StringRef Directive, SMLoc Loc) {
455 return parseVersionMin(Directive, Loc, Type: MCVM_IOSVersionMin);
456 }
457 bool parseMacOSXVersionMin(StringRef Directive, SMLoc Loc) {
458 return parseVersionMin(Directive, Loc, Type: MCVM_OSXVersionMin);
459 }
460
461 bool parseBuildVersion(StringRef Directive, SMLoc Loc);
462 bool parseTargetTripleDirective(StringRef Directive, SMLoc Loc);
463 bool parseVersionMin(StringRef Directive, SMLoc Loc, MCVersionMinType Type);
464 bool parseMajorMinorVersionComponent(unsigned *Major, unsigned *Minor,
465 const char *VersionName);
466 bool parseOptionalTrailingVersionComponent(unsigned *Component,
467 const char *ComponentName);
468 bool parseVersion(unsigned *Major, unsigned *Minor, unsigned *Update);
469 bool parseSDKVersion(VersionTuple &SDKVersion);
470 void checkVersion(StringRef Directive, StringRef Arg, SMLoc Loc,
471 Triple::OSType ExpectedOS);
472 bool parseDirectiveCGProfile(StringRef Directive, SMLoc Loc);
473};
474
475} // end anonymous namespace
476
477bool DarwinAsmParser::parseSectionSwitch(StringRef Segment, StringRef Section,
478 unsigned TAA, unsigned Alignment,
479 unsigned StubSize) {
480 if (getLexer().isNot(K: AsmToken::EndOfStatement))
481 return TokError(Msg: "unexpected token in section switching directive");
482 Lex();
483
484 // FIXME: Arch specific.
485 bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS;
486 getStreamer().switchSection(Section: getContext().getMachOSection(
487 Segment, Section, TypeAndAttributes: TAA, Reserved2: StubSize,
488 K: isText ? SectionKind::getText() : SectionKind::getData()));
489
490 // Set the implicit alignment, if any.
491 //
492 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
493 // alignment on the section (e.g., if one manually inserts bytes into the
494 // section, then just issuing the section switch directive will not realign
495 // the section. However, this is arguably more reasonable behavior, and there
496 // is no good reason for someone to intentionally emit incorrectly sized
497 // values into the implicitly aligned sections.
498 if (Alignment)
499 getStreamer().emitValueToAlignment(Alignment: Align(Alignment));
500
501 return false;
502}
503
504/// parseDirectiveAltEntry
505/// ::= .alt_entry identifier
506bool DarwinAsmParser::parseDirectiveAltEntry(StringRef, SMLoc) {
507 MCSymbol *Sym;
508 if (getParser().parseSymbol(Res&: Sym))
509 return TokError(Msg: "expected identifier in directive");
510
511 if (Sym->isDefined())
512 return TokError(Msg: ".alt_entry must preceed symbol definition");
513
514 if (!getStreamer().emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_AltEntry))
515 return TokError(Msg: "unable to emit symbol attribute");
516
517 Lex();
518 return false;
519}
520
521/// parseDirectiveDesc
522/// ::= .desc identifier , expression
523bool DarwinAsmParser::parseDirectiveDesc(StringRef, SMLoc) {
524 MCSymbol *Sym;
525 if (getParser().parseSymbol(Res&: Sym))
526 return TokError(Msg: "expected identifier in directive");
527
528 if (getLexer().isNot(K: AsmToken::Comma))
529 return TokError(Msg: "unexpected token in '.desc' directive");
530 Lex();
531
532 int64_t DescValue;
533 if (getParser().parseAbsoluteExpression(Res&: DescValue))
534 return true;
535
536 if (getLexer().isNot(K: AsmToken::EndOfStatement))
537 return TokError(Msg: "unexpected token in '.desc' directive");
538
539 Lex();
540
541 // Set the n_desc field of this Symbol to this DescValue
542 getStreamer().emitSymbolDesc(Symbol: Sym, DescValue);
543
544 return false;
545}
546
547/// parseDirectiveIndirectSymbol
548/// ::= .indirect_symbol identifier
549bool DarwinAsmParser::parseDirectiveIndirectSymbol(StringRef, SMLoc Loc) {
550 const MCSectionMachO *Current = static_cast<const MCSectionMachO *>(
551 getStreamer().getCurrentSectionOnly());
552 MachO::SectionType SectionType = Current->getType();
553 if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
554 SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
555 SectionType != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
556 SectionType != MachO::S_SYMBOL_STUBS)
557 return Error(L: Loc, Msg: "indirect symbol not in a symbol pointer or stub "
558 "section");
559
560 MCSymbol *Sym;
561 if (getParser().parseSymbol(Res&: Sym))
562 return TokError(Msg: "expected identifier in .indirect_symbol directive");
563
564 // Assembler local symbols don't make any sense here. Complain loudly.
565 if (Sym->isTemporary())
566 return TokError(Msg: "non-local symbol required in directive");
567
568 if (!getStreamer().emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_IndirectSymbol))
569 return TokError(Msg: "unable to emit indirect symbol attribute for: " +
570 Sym->getName());
571
572 if (getLexer().isNot(K: AsmToken::EndOfStatement))
573 return TokError(Msg: "unexpected token in '.indirect_symbol' directive");
574
575 Lex();
576
577 return false;
578}
579
580/// parseDirectiveDumpOrLoad
581/// ::= ( .dump | .load ) "filename"
582bool DarwinAsmParser::parseDirectiveDumpOrLoad(StringRef Directive,
583 SMLoc IDLoc) {
584 bool IsDump = Directive == ".dump";
585 if (getLexer().isNot(K: AsmToken::String))
586 return TokError(Msg: "expected string in '.dump' or '.load' directive");
587
588 Lex();
589
590 if (getLexer().isNot(K: AsmToken::EndOfStatement))
591 return TokError(Msg: "unexpected token in '.dump' or '.load' directive");
592
593 Lex();
594
595 // FIXME: If/when .dump and .load are implemented they will be done in the
596 // the assembly parser and not have any need for an MCStreamer API.
597 if (IsDump)
598 return Warning(L: IDLoc, Msg: "ignoring directive .dump for now");
599 else
600 return Warning(L: IDLoc, Msg: "ignoring directive .load for now");
601}
602
603/// ParseDirectiveLinkerOption
604/// ::= .linker_option "string" ( , "string" )*
605bool DarwinAsmParser::parseDirectiveLinkerOption(StringRef IDVal, SMLoc) {
606 SmallVector<std::string, 4> Args;
607 while (true) {
608 if (getLexer().isNot(K: AsmToken::String))
609 return TokError(Msg: "expected string in '" + Twine(IDVal) + "' directive");
610
611 std::string Data;
612 if (getParser().parseEscapedString(Data))
613 return true;
614
615 Args.push_back(Elt: Data);
616
617 if (getLexer().is(K: AsmToken::EndOfStatement))
618 break;
619
620 if (getLexer().isNot(K: AsmToken::Comma))
621 return TokError(Msg: "unexpected token in '" + Twine(IDVal) + "' directive");
622 Lex();
623 }
624
625 getStreamer().emitLinkerOptions(Kind: Args);
626 return false;
627}
628
629/// parseDirectiveLsym
630/// ::= .lsym identifier , expression
631bool DarwinAsmParser::parseDirectiveLsym(StringRef, SMLoc) {
632 MCSymbol *Sym;
633 if (getParser().parseSymbol(Res&: Sym))
634 return TokError(Msg: "expected identifier in directive");
635
636 if (getLexer().isNot(K: AsmToken::Comma))
637 return TokError(Msg: "unexpected token in '.lsym' directive");
638 Lex();
639
640 const MCExpr *Value;
641 if (getParser().parseExpression(Res&: Value))
642 return true;
643
644 if (getLexer().isNot(K: AsmToken::EndOfStatement))
645 return TokError(Msg: "unexpected token in '.lsym' directive");
646
647 Lex();
648
649 // We don't currently support this directive.
650 //
651 // FIXME: Diagnostic location!
652 (void) Sym;
653 return TokError(Msg: "directive '.lsym' is unsupported");
654}
655
656/// parseDirectiveSection:
657/// ::= .section identifier (',' identifier)*
658bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) {
659 SMLoc Loc = getLexer().getLoc();
660
661 StringRef SectionName;
662 if (getParser().parseIdentifier(Res&: SectionName))
663 return Error(L: Loc, Msg: "expected identifier after '.section' directive");
664
665 // Verify there is a following comma.
666 if (!getLexer().is(K: AsmToken::Comma))
667 return TokError(Msg: "unexpected token in '.section' directive");
668
669 std::string SectionSpec = std::string(SectionName);
670 SectionSpec += ",";
671
672 // Add all the tokens until the end of the line, ParseSectionSpecifier will
673 // handle this.
674 StringRef EOL = getLexer().LexUntilEndOfStatement();
675 SectionSpec.append(first: EOL.begin(), last: EOL.end());
676
677 Lex();
678 if (getLexer().isNot(K: AsmToken::EndOfStatement))
679 return TokError(Msg: "unexpected token in '.section' directive");
680 Lex();
681
682 StringRef Segment, Section;
683 unsigned StubSize;
684 unsigned TAA;
685 bool TAAParsed;
686 if (class Error E = MCSectionMachO::ParseSectionSpecifier(
687 Spec: SectionSpec, Segment, Section, TAA, TAAParsed, StubSize))
688 return Error(L: Loc, Msg: toString(E: std::move(E)));
689
690 // Issue a warning if the target is not powerpc and Section is a *coal* section.
691 Triple TT = getParser().getContext().getTargetTriple();
692 Triple::ArchType ArchTy = TT.getArch();
693
694 if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) {
695 StringRef NonCoalSection = StringSwitch<StringRef>(Section)
696 .Case(S: "__textcoal_nt", Value: "__text")
697 .Case(S: "__const_coal", Value: "__const")
698 .Case(S: "__datacoal_nt", Value: "__data")
699 .Default(Value: Section);
700
701 if (Section != NonCoalSection) {
702 StringRef SectionVal(Loc.getPointer());
703 size_t B = SectionVal.find(C: ',') + 1, E = SectionVal.find(C: ',', From: B);
704 SMLoc BLoc = SMLoc::getFromPointer(Ptr: SectionVal.data() + B);
705 SMLoc ELoc = SMLoc::getFromPointer(Ptr: SectionVal.data() + E);
706 getParser().Warning(L: Loc, Msg: "section \"" + Section + "\" is deprecated",
707 Range: SMRange(BLoc, ELoc));
708 getParser().Note(L: Loc, Msg: "change section name to \"" + NonCoalSection +
709 "\"", Range: SMRange(BLoc, ELoc));
710 }
711 }
712
713 // FIXME: Arch specific.
714 bool isText = Segment == "__TEXT"; // FIXME: Hack.
715 getStreamer().switchSection(Section: getContext().getMachOSection(
716 Segment, Section, TypeAndAttributes: TAA, Reserved2: StubSize,
717 K: isText ? SectionKind::getText() : SectionKind::getData()));
718 return false;
719}
720
721/// ParseDirectivePushSection:
722/// ::= .pushsection identifier (',' identifier)*
723bool DarwinAsmParser::parseDirectivePushSection(StringRef S, SMLoc Loc) {
724 getStreamer().pushSection();
725
726 if (parseDirectiveSection(S, Loc)) {
727 getStreamer().popSection();
728 return true;
729 }
730
731 return false;
732}
733
734/// ParseDirectivePopSection:
735/// ::= .popsection
736bool DarwinAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
737 if (!getStreamer().popSection())
738 return TokError(Msg: ".popsection without corresponding .pushsection");
739 return false;
740}
741
742/// ParseDirectivePrevious:
743/// ::= .previous
744bool DarwinAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
745 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
746 if (!PreviousSection.first)
747 return TokError(Msg: ".previous without corresponding .section");
748 getStreamer().switchSection(Section: PreviousSection.first, Subsec: PreviousSection.second);
749 return false;
750}
751
752/// ParseDirectiveSecureLogUnique
753/// ::= .secure_log_unique ... message ...
754bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
755 StringRef LogMessage = getParser().parseStringToEndOfStatement();
756 if (getLexer().isNot(K: AsmToken::EndOfStatement))
757 return TokError(Msg: "unexpected token in '.secure_log_unique' directive");
758
759 if (getContext().getSecureLogUsed())
760 return Error(L: IDLoc, Msg: ".secure_log_unique specified multiple times");
761
762 // Get the secure log path.
763 StringRef SecureLogFile = getContext().getSecureLogFile();
764 if (SecureLogFile.empty())
765 return Error(L: IDLoc, Msg: ".secure_log_unique used but AS_SECURE_LOG_FILE "
766 "environment variable unset.");
767
768 // Open the secure log file if we haven't already.
769 raw_fd_ostream *OS = getContext().getSecureLog();
770 if (!OS) {
771 std::error_code EC;
772 auto NewOS = std::make_unique<raw_fd_ostream>(
773 args&: SecureLogFile, args&: EC, args: sys::fs::OF_Append | sys::fs::OF_TextWithCRLF);
774 if (EC)
775 return Error(L: IDLoc, Msg: Twine("can't open secure log file: ") +
776 SecureLogFile + " (" + EC.message() + ")");
777 OS = NewOS.get();
778 getContext().setSecureLog(std::move(NewOS));
779 }
780
781 // Write the message.
782 unsigned CurBuf = getSourceManager().FindBufferContainingLoc(Loc: IDLoc);
783 *OS << getSourceManager().getBufferInfo(i: CurBuf).Buffer->getBufferIdentifier()
784 << ":" << getSourceManager().FindLineNumber(Loc: IDLoc, BufferID: CurBuf) << ":"
785 << LogMessage + "\n";
786
787 getContext().setSecureLogUsed(true);
788
789 return false;
790}
791
792/// ParseDirectiveSecureLogReset
793/// ::= .secure_log_reset
794bool DarwinAsmParser::parseDirectiveSecureLogReset(StringRef, SMLoc IDLoc) {
795 if (getLexer().isNot(K: AsmToken::EndOfStatement))
796 return TokError(Msg: "unexpected token in '.secure_log_reset' directive");
797
798 Lex();
799
800 getContext().setSecureLogUsed(false);
801
802 return false;
803}
804
805/// parseDirectiveSubsectionsViaSymbols
806/// ::= .subsections_via_symbols
807bool DarwinAsmParser::parseDirectiveSubsectionsViaSymbols(StringRef, SMLoc) {
808 if (getLexer().isNot(K: AsmToken::EndOfStatement))
809 return TokError(Msg: "unexpected token in '.subsections_via_symbols' directive");
810
811 Lex();
812
813 getStreamer().emitSubsectionsViaSymbols();
814
815 return false;
816}
817
818/// ParseDirectiveTBSS
819/// ::= .tbss identifier, size, align
820bool DarwinAsmParser::parseDirectiveTBSS(StringRef, SMLoc) {
821 SMLoc IDLoc = getLexer().getLoc();
822 MCSymbol *Sym;
823 if (getParser().parseSymbol(Res&: Sym))
824 return TokError(Msg: "expected identifier in directive");
825
826 if (getLexer().isNot(K: AsmToken::Comma))
827 return TokError(Msg: "unexpected token in directive");
828 Lex();
829
830 int64_t Size;
831 SMLoc SizeLoc = getLexer().getLoc();
832 if (getParser().parseAbsoluteExpression(Res&: Size))
833 return true;
834
835 int64_t Pow2Alignment = 0;
836 SMLoc Pow2AlignmentLoc;
837 if (getLexer().is(K: AsmToken::Comma)) {
838 Lex();
839 Pow2AlignmentLoc = getLexer().getLoc();
840 if (getParser().parseAbsoluteExpression(Res&: Pow2Alignment))
841 return true;
842 }
843
844 if (getLexer().isNot(K: AsmToken::EndOfStatement))
845 return TokError(Msg: "unexpected token in '.tbss' directive");
846
847 Lex();
848
849 if (Size < 0)
850 return Error(L: SizeLoc, Msg: "invalid '.tbss' directive size, can't be less than"
851 "zero");
852
853 if (Pow2Alignment < 0 || Pow2Alignment > 63)
854 return Error(L: Pow2AlignmentLoc, Msg: "invalid '.tbss' alignment, must be in the "
855 "range [0, 63]");
856
857 if (!Sym->isUndefined())
858 return Error(L: IDLoc, Msg: "invalid symbol redefinition");
859
860 getStreamer().emitTBSSSymbol(
861 Section: getContext().getMachOSection(Segment: "__DATA", Section: "__thread_bss",
862 TypeAndAttributes: MachO::S_THREAD_LOCAL_ZEROFILL, Reserved2: 0,
863 K: SectionKind::getThreadBSS()),
864 Symbol: Sym, Size, ByteAlignment: Align::fromLog2(Log2Value: Pow2Alignment));
865
866 return false;
867}
868
869/// ParseDirectiveZerofill
870/// ::= .zerofill segname , sectname [, identifier , size_expression [
871/// , align_expression ]]
872bool DarwinAsmParser::parseDirectiveZerofill(StringRef, SMLoc) {
873 StringRef Segment;
874 if (getParser().parseIdentifier(Res&: Segment))
875 return TokError(Msg: "expected segment name after '.zerofill' directive");
876
877 if (getLexer().isNot(K: AsmToken::Comma))
878 return TokError(Msg: "unexpected token in directive");
879 Lex();
880
881 StringRef Section;
882 SMLoc SectionLoc = getLexer().getLoc();
883 if (getParser().parseIdentifier(Res&: Section))
884 return TokError(Msg: "expected section name after comma in '.zerofill' "
885 "directive");
886
887 // If this is the end of the line all that was wanted was to create the
888 // the section but with no symbol.
889 if (getLexer().is(K: AsmToken::EndOfStatement)) {
890 // Create the zerofill section but no symbol
891 getStreamer().emitZerofill(
892 Section: getContext().getMachOSection(Segment, Section, TypeAndAttributes: MachO::S_ZEROFILL, Reserved2: 0,
893 K: SectionKind::getBSS()),
894 /*Symbol=*/nullptr, /*Size=*/0, ByteAlignment: Align(1), Loc: SectionLoc);
895 return false;
896 }
897
898 if (getLexer().isNot(K: AsmToken::Comma))
899 return TokError(Msg: "unexpected token in directive");
900 Lex();
901
902 SMLoc IDLoc = getLexer().getLoc();
903 MCSymbol *Sym;
904 if (getParser().parseSymbol(Res&: Sym))
905 return TokError(Msg: "expected identifier in directive");
906
907 if (getLexer().isNot(K: AsmToken::Comma))
908 return TokError(Msg: "unexpected token in directive");
909 Lex();
910
911 int64_t Size;
912 SMLoc SizeLoc = getLexer().getLoc();
913 if (getParser().parseAbsoluteExpression(Res&: Size))
914 return true;
915
916 int64_t Pow2Alignment = 0;
917 SMLoc Pow2AlignmentLoc;
918 if (getLexer().is(K: AsmToken::Comma)) {
919 Lex();
920 Pow2AlignmentLoc = getLexer().getLoc();
921 if (getParser().parseAbsoluteExpression(Res&: Pow2Alignment))
922 return true;
923 }
924
925 if (getLexer().isNot(K: AsmToken::EndOfStatement))
926 return TokError(Msg: "unexpected token in '.zerofill' directive");
927
928 Lex();
929
930 if (Size < 0)
931 return Error(L: SizeLoc, Msg: "invalid '.zerofill' directive size, can't be less "
932 "than zero");
933
934 // NOTE: The alignment in the directive is a power of 2 value, the assembler
935 // may internally end up wanting an alignment in bytes.
936 if (Pow2Alignment < 0 || Pow2Alignment > 63)
937 return Error(L: Pow2AlignmentLoc, Msg: "invalid '.zerofill' directive alignment, "
938 "must be in the range [0, 63]");
939
940 if (!Sym->isUndefined())
941 return Error(L: IDLoc, Msg: "invalid symbol redefinition");
942
943 // Create the zerofill Symbol with Size and Pow2Alignment
944 //
945 // FIXME: Arch specific.
946 getStreamer().emitZerofill(
947 Section: getContext().getMachOSection(Segment, Section, TypeAndAttributes: MachO::S_ZEROFILL, Reserved2: 0,
948 K: SectionKind::getBSS()),
949 Symbol: Sym, Size, ByteAlignment: Align::fromLog2(Log2Value: Pow2Alignment), Loc: SectionLoc);
950
951 return false;
952}
953
954/// ParseDirectiveDataRegion
955/// ::= .data_region [ ( jt8 | jt16 | jt32 ) ]
956bool DarwinAsmParser::parseDirectiveDataRegion(StringRef, SMLoc) {
957 if (getLexer().is(K: AsmToken::EndOfStatement)) {
958 Lex();
959 getStreamer().emitDataRegion(Kind: MCDR_DataRegion);
960 return false;
961 }
962 StringRef RegionType;
963 SMLoc Loc = getParser().getTok().getLoc();
964 if (getParser().parseIdentifier(Res&: RegionType))
965 return TokError(Msg: "expected region type after '.data_region' directive");
966 int Kind = StringSwitch<int>(RegionType)
967 .Case(S: "jt8", Value: MCDR_DataRegionJT8)
968 .Case(S: "jt16", Value: MCDR_DataRegionJT16)
969 .Case(S: "jt32", Value: MCDR_DataRegionJT32)
970 .Default(Value: -1);
971 if (Kind == -1)
972 return Error(L: Loc, Msg: "unknown region type in '.data_region' directive");
973 Lex();
974
975 getStreamer().emitDataRegion(Kind: (MCDataRegionType)Kind);
976 return false;
977}
978
979/// ParseDirectiveDataRegionEnd
980/// ::= .end_data_region
981bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) {
982 if (getLexer().isNot(K: AsmToken::EndOfStatement))
983 return TokError(Msg: "unexpected token in '.end_data_region' directive");
984
985 Lex();
986 getStreamer().emitDataRegion(Kind: MCDR_DataRegionEnd);
987 return false;
988}
989
990static bool isSDKVersionToken(const AsmToken &Tok) {
991 return Tok.is(K: AsmToken::Identifier) && Tok.getIdentifier() == "sdk_version";
992}
993
994/// parseMajorMinorVersionComponent ::= major, minor
995bool DarwinAsmParser::parseMajorMinorVersionComponent(unsigned *Major,
996 unsigned *Minor,
997 const char *VersionName) {
998 // Get the major version number.
999 if (getLexer().isNot(K: AsmToken::Integer))
1000 return TokError(Msg: Twine("invalid ") + VersionName +
1001 " major version number, integer expected");
1002 int64_t MajorVal = getLexer().getTok().getIntVal();
1003 if (MajorVal > 65535 || MajorVal <= 0)
1004 return TokError(Msg: Twine("invalid ") + VersionName + " major version number");
1005 *Major = (unsigned)MajorVal;
1006 Lex();
1007 if (getLexer().isNot(K: AsmToken::Comma))
1008 return TokError(Msg: Twine(VersionName) +
1009 " minor version number required, comma expected");
1010 Lex();
1011 // Get the minor version number.
1012 if (getLexer().isNot(K: AsmToken::Integer))
1013 return TokError(Msg: Twine("invalid ") + VersionName +
1014 " minor version number, integer expected");
1015 int64_t MinorVal = getLexer().getTok().getIntVal();
1016 if (MinorVal > 255 || MinorVal < 0)
1017 return TokError(Msg: Twine("invalid ") + VersionName + " minor version number");
1018 *Minor = MinorVal;
1019 Lex();
1020 return false;
1021}
1022
1023/// parseOptionalTrailingVersionComponent ::= , version_number
1024bool DarwinAsmParser::parseOptionalTrailingVersionComponent(
1025 unsigned *Component, const char *ComponentName) {
1026 assert(getLexer().is(AsmToken::Comma) && "comma expected");
1027 Lex();
1028 if (getLexer().isNot(K: AsmToken::Integer))
1029 return TokError(Msg: Twine("invalid ") + ComponentName +
1030 " version number, integer expected");
1031 int64_t Val = getLexer().getTok().getIntVal();
1032 if (Val > 255 || Val < 0)
1033 return TokError(Msg: Twine("invalid ") + ComponentName + " version number");
1034 *Component = Val;
1035 Lex();
1036 return false;
1037}
1038
1039/// parseVersion ::= parseMajorMinorVersionComponent
1040/// parseOptionalTrailingVersionComponent
1041bool DarwinAsmParser::parseVersion(unsigned *Major, unsigned *Minor,
1042 unsigned *Update) {
1043 if (parseMajorMinorVersionComponent(Major, Minor, VersionName: "OS"))
1044 return true;
1045
1046 // Get the update level, if specified
1047 *Update = 0;
1048 if (getLexer().is(K: AsmToken::EndOfStatement) ||
1049 isSDKVersionToken(Tok: getLexer().getTok()))
1050 return false;
1051 if (getLexer().isNot(K: AsmToken::Comma))
1052 return TokError(Msg: "invalid OS update specifier, comma expected");
1053 if (parseOptionalTrailingVersionComponent(Component: Update, ComponentName: "OS update"))
1054 return true;
1055 return false;
1056}
1057
1058bool DarwinAsmParser::parseSDKVersion(VersionTuple &SDKVersion) {
1059 assert(isSDKVersionToken(getLexer().getTok()) && "expected sdk_version");
1060 Lex();
1061 unsigned Major, Minor;
1062 if (parseMajorMinorVersionComponent(Major: &Major, Minor: &Minor, VersionName: "SDK"))
1063 return true;
1064 SDKVersion = VersionTuple(Major, Minor);
1065
1066 // Get the subminor version, if specified.
1067 if (getLexer().is(K: AsmToken::Comma)) {
1068 unsigned Subminor;
1069 if (parseOptionalTrailingVersionComponent(Component: &Subminor, ComponentName: "SDK subminor"))
1070 return true;
1071 SDKVersion = VersionTuple(Major, Minor, Subminor);
1072 }
1073 return false;
1074}
1075
1076void DarwinAsmParser::checkVersion(StringRef Directive, StringRef Arg,
1077 SMLoc Loc, Triple::OSType ExpectedOS) {
1078 const Triple &Target = getContext().getTargetTriple();
1079 if (Target.getOS() != ExpectedOS)
1080 Warning(L: Loc, Msg: Twine(Directive) +
1081 (Arg.empty() ? Twine() : Twine(' ') + Arg) +
1082 " used while targeting " + Target.getOSName());
1083
1084 if (LastVersionDirective.isValid()) {
1085 Warning(L: Loc, Msg: "overriding previous version directive");
1086 Note(L: LastVersionDirective, Msg: "previous definition is here");
1087 }
1088 LastVersionDirective = Loc;
1089}
1090
1091static Triple::OSType getOSTypeFromMCVM(MCVersionMinType Type) {
1092 switch (Type) {
1093 case MCVM_WatchOSVersionMin: return Triple::WatchOS;
1094 case MCVM_TvOSVersionMin: return Triple::TvOS;
1095 case MCVM_IOSVersionMin: return Triple::IOS;
1096 case MCVM_OSXVersionMin: return Triple::MacOSX;
1097 }
1098 llvm_unreachable("Invalid mc version min type");
1099}
1100
1101/// parseVersionMin
1102/// ::= .ios_version_min parseVersion parseSDKVersion
1103/// | .macosx_version_min parseVersion parseSDKVersion
1104/// | .tvos_version_min parseVersion parseSDKVersion
1105/// | .watchos_version_min parseVersion parseSDKVersion
1106bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc,
1107 MCVersionMinType Type) {
1108 unsigned Major;
1109 unsigned Minor;
1110 unsigned Update;
1111 if (parseVersion(Major: &Major, Minor: &Minor, Update: &Update))
1112 return true;
1113
1114 VersionTuple SDKVersion;
1115 if (isSDKVersionToken(Tok: getLexer().getTok()) && parseSDKVersion(SDKVersion))
1116 return true;
1117
1118 if (parseEOL())
1119 return addErrorSuffix(Suffix: Twine(" in '") + Directive + "' directive");
1120
1121 Triple::OSType ExpectedOS = getOSTypeFromMCVM(Type);
1122 checkVersion(Directive, Arg: StringRef(), Loc, ExpectedOS);
1123 getStreamer().emitVersionMin(Type, Major, Minor, Update, SDKVersion);
1124 return false;
1125}
1126
1127static Triple::OSType getOSTypeFromPlatform(MachO::PlatformType Type) {
1128 switch (Type) {
1129 case MachO::PLATFORM_UNKNOWN: /* silence warning */
1130 break;
1131 case MachO::PLATFORM_MACOS: return Triple::MacOSX;
1132 case MachO::PLATFORM_IOS: return Triple::IOS;
1133 case MachO::PLATFORM_TVOS: return Triple::TvOS;
1134 case MachO::PLATFORM_WATCHOS: return Triple::WatchOS;
1135 case MachO::PLATFORM_XROS: return Triple::XROS;
1136 case MachO::PLATFORM_BRIDGEOS: /* silence warning */ break;
1137 case MachO::PLATFORM_DRIVERKIT:
1138 return Triple::DriverKit;
1139 case MachO::PLATFORM_MACCATALYST: return Triple::IOS;
1140 case MachO::PLATFORM_IOSSIMULATOR: /* silence warning */ break;
1141 case MachO::PLATFORM_TVOSSIMULATOR: /* silence warning */ break;
1142 case MachO::PLATFORM_WATCHOSSIMULATOR: /* silence warning */ break;
1143 case MachO::PLATFORM_XROS_SIMULATOR: /* silence warning */ break;
1144 }
1145 llvm_unreachable("Invalid mach-o platform type");
1146}
1147
1148/// parseBuildVersion
1149/// ::= .build_version (macos|ios|tvos|watchos), parseVersion parseSDKVersion
1150bool DarwinAsmParser::parseBuildVersion(StringRef Directive, SMLoc Loc) {
1151 StringRef PlatformName;
1152 SMLoc PlatformLoc = getTok().getLoc();
1153 if (getParser().parseIdentifier(Res&: PlatformName))
1154 return TokError(Msg: "platform name expected");
1155
1156 unsigned Platform = StringSwitch<unsigned>(PlatformName)
1157#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
1158 marketing) \
1159 .Case(#build_name, MachO::PLATFORM_##platform)
1160#include "llvm/BinaryFormat/MachO.def"
1161 .Default(Value: MachO::PLATFORM_UNKNOWN);
1162
1163 if (Platform == MachO::PLATFORM_UNKNOWN)
1164 return Error(L: PlatformLoc, Msg: "unknown platform name");
1165
1166 if (getLexer().isNot(K: AsmToken::Comma))
1167 return TokError(Msg: "version number required, comma expected");
1168 Lex();
1169
1170 unsigned Major;
1171 unsigned Minor;
1172 unsigned Update;
1173 if (parseVersion(Major: &Major, Minor: &Minor, Update: &Update))
1174 return true;
1175
1176 VersionTuple SDKVersion;
1177 if (isSDKVersionToken(Tok: getLexer().getTok()) && parseSDKVersion(SDKVersion))
1178 return true;
1179
1180 if (parseEOL())
1181 return addErrorSuffix(Suffix: " in '.build_version' directive");
1182
1183 Triple::OSType ExpectedOS
1184 = getOSTypeFromPlatform(Type: (MachO::PlatformType)Platform);
1185 checkVersion(Directive, Arg: PlatformName, Loc, ExpectedOS);
1186 getStreamer().emitBuildVersion(Platform, Major, Minor, Update, SDKVersion);
1187 return false;
1188}
1189
1190/// parseTargetTripleDirective
1191/// ::= .target_triple parseTargetTriple
1192bool DarwinAsmParser::parseTargetTripleDirective(StringRef Directive,
1193 SMLoc Loc) {
1194 StringRef TargetTriple;
1195 SMLoc TargetTripleLoc = getTok().getLoc();
1196 if (getParser().parseIdentifier(Res&: TargetTriple))
1197 return TokError(Msg: "target triple expected");
1198 std::string NormalizedTriple = Triple::normalize(Str: TargetTriple);
1199 if (!Triple(NormalizedTriple).isOSDarwin())
1200 return Error(L: TargetTripleLoc, Msg: "non-Darwin target triple");
1201
1202 if (parseEOL())
1203 return addErrorSuffix(Suffix: " in '.target_triple' directive");
1204
1205 getStreamer().emitTargetTriple(TargetTriple: NormalizedTriple);
1206 return false;
1207}
1208
1209/// parseDirectiveCGProfile
1210/// ::= .cg_profile from, to, count
1211bool DarwinAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) {
1212 return MCAsmParserExtension::parseDirectiveCGProfile(S, Loc);
1213}
1214
1215MCAsmParserExtension *llvm::createDarwinAsmParser() {
1216 return new DarwinAsmParser;
1217}
1218