LLVM 24.0.0git
LoongArchAsmParser.cpp
Go to the documentation of this file.
1// LoongArchAsmParser.cpp - Parse LoongArch assembly to MCInst instructions -=//
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
17#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCStreamer.h"
26#include "llvm/MC/MCValue.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "loongarch-asm-parser"
34
35namespace {
36class LoongArchAsmParser : public MCTargetAsmParser {
37 SmallVector<FeatureBitset, 4> FeatureBitStack;
38
39 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
40 bool is64Bit() const { return getSTI().hasFeature(LoongArch::Feature64Bit); }
41 bool has32S() const { return getSTI().hasFeature(LoongArch::Feature32S); }
42 LoongArchTargetStreamer &getTargetStreamer() {
43 assert(getParser().getStreamer().getTargetStreamer() &&
44 "do not have a target streamer");
45 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
46 return static_cast<LoongArchTargetStreamer &>(TS);
47 }
48
49 struct Inst {
50 unsigned Opc;
51 uint16_t Specifier;
52 Inst(unsigned Opc, uint16_t VK = 0) : Opc(Opc), Specifier(VK) {}
53 };
54 using InstSeq = SmallVector<Inst>;
55
56 /// Parse a register as used in CFI directives.
57 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
58 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
59 SMLoc &EndLoc) override;
60
61 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
62 SMLoc NameLoc, OperandVector &Operands) override;
63
64 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
67 bool MatchingInlineAsm) override;
68
69 unsigned checkTargetMatchPredicate(MCInst &Inst) override;
70
71 unsigned validateTargetOperandClass(const MCParsedAsmOperand &Op,
72 unsigned Kind) override;
73
74 ParseStatus parseDirective(AsmToken DirectiveID) override;
75
76 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
77 int64_t Lower, int64_t Upper,
78 const Twine &Msg);
79
80 /// Helper for processing MC instructions that have been successfully matched
81 /// by matchAndEmitInstruction.
82 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
83 MCStreamer &Out);
84
85// Auto-generated instruction matching functions.
86#define GET_ASSEMBLER_HEADER
87#include "LoongArchGenAsmMatcher.inc"
88
89 ParseStatus parseRegister(OperandVector &Operands);
91 ParseStatus parseOperandWithModifier(OperandVector &Operands);
92 ParseStatus parseSImm26Operand(OperandVector &Operands);
93 ParseStatus parseAtomicMemOp(OperandVector &Operands);
94
95 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
96
97 bool parseDirectiveOption();
98 bool parseDirectiveDtpRelWord();
99 bool parseDirectiveDtpRelDWord();
100
101 void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
102 if (!(getSTI().hasFeature(Feature))) {
103 MCSubtargetInfo &STI = copySTI();
104 setAvailableFeatures(
105 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
106 }
107 }
108
109 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
110 if (getSTI().hasFeature(Feature)) {
111 MCSubtargetInfo &STI = copySTI();
112 setAvailableFeatures(
113 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
114 }
115 }
116
117 void pushFeatureBits() {
118 FeatureBitStack.push_back(getSTI().getFeatureBits());
119 }
120
121 bool popFeatureBits() {
122 if (FeatureBitStack.empty())
123 return true;
124
125 FeatureBitset FeatureBits = FeatureBitStack.pop_back_val();
126 copySTI().setFeatureBits(FeatureBits);
127 setAvailableFeatures(ComputeAvailableFeatures(FeatureBits));
128
129 return false;
130 }
131
132 // Helper to emit the sequence of instructions generated by the
133 // "emitLoadAddress*" functions.
134 void emitLAInstSeq(MCRegister DestReg, MCRegister TmpReg,
135 const MCExpr *Symbol, SmallVectorImpl<Inst> &Insts,
136 SMLoc IDLoc, MCStreamer &Out, bool RelaxHint = false);
137
138 // Helper to emit pseudo instruction "la.abs $rd, sym".
139 void emitLoadAddressAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
140
141 // Helper to emit pseudo instruction "la.pcrel $rd, sym".
142 void emitLoadAddressPcrel(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
143 // Helper to emit pseudo instruction "la.pcrel $rd, $rj, sym".
144 void emitLoadAddressPcrelLarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
145
146 // Helper to emit pseudo instruction "la.got $rd, sym".
147 void emitLoadAddressGot(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
148 // Helper to emit pseudo instruction "la.got $rd, $rj, sym".
149 void emitLoadAddressGotLarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
150
151 // Helper to emit pseudo instruction "la.tls.le $rd, sym".
152 void emitLoadAddressTLSLE(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
153
154 // Helper to emit pseudo instruction "la.tls.ie $rd, sym".
155 void emitLoadAddressTLSIE(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
156 // Helper to emit pseudo instruction "la.tls.ie $rd, $rj, sym".
157 void emitLoadAddressTLSIELarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
158
159 // Helper to emit pseudo instruction "la.tls.ld $rd, sym".
160 void emitLoadAddressTLSLD(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
161 // Helper to emit pseudo instruction "la.tls.ld $rd, $rj, sym".
162 void emitLoadAddressTLSLDLarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
163
164 // Helper to emit pseudo instruction "la.tls.gd $rd, sym".
165 void emitLoadAddressTLSGD(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
166 // Helper to emit pseudo instruction "la.tls.gd $rd, $rj, sym".
167 void emitLoadAddressTLSGDLarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
168
169 // Helper to emit pseudo instruction "la.tls.desc $rd, sym".
170 void emitLoadAddressTLSDesc(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
171 // Helper to emit pseudo instruction "la.tls.desc $rd, $rj, sym".
172 void emitLoadAddressTLSDescLarge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
173
174 // Helper to emit pseudo instruction "li.w/d $rd, $imm".
175 void emitLoadImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
176
177 // Helper to emit pseudo instruction "call{3x} sym" or "tail{3x} $rj, sym".
178 void emitFuncCall(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, bool IsTailCall,
179 bool IsCall36);
180
181public:
182 enum LoongArchMatchResultTy {
183 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
184 Match_RequiresMsbNotLessThanLsb,
185 Match_RequiresOpnd2NotR0R1,
186 Match_RequiresAMORdDifferRkRj,
187 Match_RequiresLAORdDifferRj,
188 Match_RequiresLAORdR4,
189#define GET_OPERAND_DIAGNOSTIC_TYPES
190#include "LoongArchGenAsmMatcher.inc"
191#undef GET_OPERAND_DIAGNOSTIC_TYPES
192 };
193
194 static bool classifySymbolRef(const MCExpr *Expr,
196
197 LoongArchAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
198 const MCInstrInfo &MII)
199 : MCTargetAsmParser(STI, MII) {
200 Parser.addAliasForDirective(".half", ".2byte");
201 Parser.addAliasForDirective(".hword", ".2byte");
202 Parser.addAliasForDirective(".word", ".4byte");
203 Parser.addAliasForDirective(".dword", ".8byte");
204
205 // Initialize the set of available features.
206 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
207 }
208};
209
210// Instances of this class represent a parsed LoongArch machine instruction.
211class LoongArchOperand : public MCParsedAsmOperand {
212 enum class KindTy {
213 Token,
214 Register,
215 Immediate,
216 } Kind;
217
218 struct RegOp {
219 MCRegister RegNum;
220 };
221
222 struct ImmOp {
223 const MCExpr *Val;
224 };
225
226 SMLoc StartLoc, EndLoc;
227 union {
228 StringRef Tok;
229 struct RegOp Reg;
230 struct ImmOp Imm;
231 };
232
233public:
234 LoongArchOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
235
236 bool isToken() const override { return Kind == KindTy::Token; }
237 bool isReg() const override { return Kind == KindTy::Register; }
238 bool isImm() const override { return Kind == KindTy::Immediate; }
239 bool isMem() const override { return false; }
240 bool isGPR() const {
241 return Kind == KindTy::Register &&
242 getLoongArchMCRegisterClass(LoongArch::GPRRegClassID)
243 .contains(Reg.RegNum);
244 }
245
246 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
248 if (auto *LE = dyn_cast<LoongArchMCExpr>(Expr)) {
249 VK = LE->getSpecifier();
250 return false;
251 }
252
253 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
254 Imm = CE->getValue();
255 return true;
256 }
257
258 return false;
259 }
260
261 template <unsigned N, int P = 0> bool isUImm() const {
262 if (!isImm())
263 return false;
264
265 int64_t Imm;
267 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
268 return IsConstantImm && isUInt<N>(Imm - P) &&
270 }
271
272 template <unsigned N, unsigned S = 0> bool isSImm() const {
273 if (!isImm())
274 return false;
275
276 int64_t Imm;
278 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
279 return IsConstantImm && isShiftedInt<N, S>(Imm) &&
281 }
282
283 bool isBareSymbol() const {
284 int64_t Imm;
286 // Must be of 'immediate' type but not a constant.
287 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
288 return false;
289 return LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
291 }
292
293 bool isTPRelAddSymbol() const {
294 int64_t Imm;
296 // Must be of 'immediate' type but not a constant.
297 if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
298 return false;
299 return LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
300 VK == ELF::R_LARCH_TLS_LE_ADD_R;
301 }
302
303 bool isUImm1() const { return isUImm<1>(); }
304 bool isUImm2() const { return isUImm<2>(); }
305 bool isUImm2plus1() const { return isUImm<2, 1>(); }
306 bool isUImm3() const { return isUImm<3>(); }
307 bool isUImm4() const { return isUImm<4>(); }
308 bool isSImm5() const { return isSImm<5>(); }
309 bool isUImm5() const { return isUImm<5>(); }
310 bool isUImm6() const { return isUImm<6>(); }
311 bool isUImm7() const { return isUImm<7>(); }
312 bool isSImm8() const { return isSImm<8>(); }
313 bool isSImm8lsl1() const { return isSImm<8, 1>(); }
314 bool isSImm8lsl2() const { return isSImm<8, 2>(); }
315 bool isSImm8lsl3() const { return isSImm<8, 3>(); }
316 bool isUImm8() const { return isUImm<8>(); }
317 bool isSImm9lsl3() const { return isSImm<9, 3>(); }
318 bool isSImm10() const { return isSImm<10>(); }
319 bool isSImm10lsl2() const { return isSImm<10, 2>(); }
320 bool isSImm11lsl1() const { return isSImm<11, 1>(); }
321 bool isSImm12() const { return isSImm<12>(); }
322
323 bool isSImm12addlike() const {
324 if (!isImm())
325 return false;
326
327 int64_t Imm;
329 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
330 bool IsValidKind =
331 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_PCALA_LO12 ||
332 VK == ELF::R_LARCH_GOT_PC_LO12 || VK == ELF::R_LARCH_TLS_IE_PC_LO12 ||
333 VK == ELF::R_LARCH_TLS_LE_LO12_R || VK == ELF::R_LARCH_TLS_DESC_LD ||
334 VK == ELF::R_LARCH_TLS_DESC_PC_LO12 || VK == ELF::R_LARCH_PCADD_LO12 ||
335 VK == ELF::R_LARCH_GOT_PCADD_LO12 ||
336 VK == ELF::R_LARCH_TLS_IE_PCADD_LO12 ||
337 VK == ELF::R_LARCH_TLS_LD_PCADD_LO12 ||
338 VK == ELF::R_LARCH_TLS_GD_PCADD_LO12 ||
339 VK == ELF::R_LARCH_TLS_DESC_PCADD_LO12;
340 return IsConstantImm
341 ? isInt<12>(Imm) && IsValidKind
342 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
343 IsValidKind;
344 }
345
346 bool isSImm12lu52id() const {
347 if (!isImm())
348 return false;
349
350 int64_t Imm;
352 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
353 bool IsValidKind =
354 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_ABS64_HI12 ||
355 VK == ELF::R_LARCH_PCALA64_HI12 || VK == ELF::R_LARCH_GOT64_HI12 ||
356 VK == ELF::R_LARCH_GOT64_PC_HI12 || VK == ELF::R_LARCH_TLS_LE64_HI12 ||
357 VK == ELF::R_LARCH_TLS_IE64_HI12 ||
358 VK == ELF::R_LARCH_TLS_IE64_PC_HI12 ||
359 VK == ELF::R_LARCH_TLS_DESC64_HI12 ||
360 VK == ELF::R_LARCH_TLS_DESC64_PC_HI12;
361 return IsConstantImm
362 ? isInt<12>(Imm) && IsValidKind
363 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
364 IsValidKind;
365 }
366
367 bool isUImm12() const { return isUImm<12>(); }
368
369 bool isUImm12ori() const {
370 if (!isImm())
371 return false;
372
373 int64_t Imm;
375 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
376 bool IsValidKind =
377 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_ABS_LO12 ||
378 VK == ELF::R_LARCH_PCALA_LO12 || VK == ELF::R_LARCH_GOT_LO12 ||
379 VK == ELF::R_LARCH_GOT_PC_LO12 || VK == ELF::R_LARCH_TLS_LE_LO12 ||
380 VK == ELF::R_LARCH_TLS_IE_LO12 || VK == ELF::R_LARCH_TLS_IE_PC_LO12 ||
381 VK == ELF::R_LARCH_TLS_DESC_LO12;
382 return IsConstantImm
383 ? isUInt<12>(Imm) && IsValidKind
384 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
385 IsValidKind;
386 }
387
388 bool isSImm13() const { return isSImm<13>(); }
389 bool isUImm14() const { return isUImm<14>(); }
390 bool isUImm15() const { return isUImm<15>(); }
391
392 bool isSImm14lsl2() const { return isSImm<14, 2>(); }
393 bool isSImm16() const { return isSImm<16>(); }
394
395 bool isSImm16lsl2() const {
396 if (!isImm())
397 return false;
398
399 int64_t Imm;
401 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
402 bool IsValidKind =
403 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_B16 ||
404 VK == ELF::R_LARCH_PCALA_LO12 || VK == ELF::R_LARCH_TLS_DESC_CALL;
405 return IsConstantImm
406 ? isShiftedInt<16, 2>(Imm) && IsValidKind
407 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
408 IsValidKind;
409 }
410
411 bool isSImm20() const { return isSImm<20>(); }
412
413 bool isSImm20pcalau12i() const {
414 if (!isImm())
415 return false;
416
417 int64_t Imm;
419 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
420 bool IsValidKind =
421 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_PCALA_HI20 ||
422 VK == ELF::R_LARCH_GOT_PC_HI20 || VK == ELF::R_LARCH_TLS_IE_PC_HI20 ||
423 VK == ELF::R_LARCH_TLS_LD_PC_HI20 ||
424 VK == ELF::R_LARCH_TLS_GD_PC_HI20 ||
425 VK == ELF::R_LARCH_TLS_DESC_PC_HI20;
426 return IsConstantImm
427 ? isInt<20>(Imm) && IsValidKind
428 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
429 IsValidKind;
430 }
431
432 bool isSImm20lu12iw() const {
433 if (!isImm())
434 return false;
435
436 int64_t Imm;
438 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
439 bool IsValidKind =
440 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_ABS_HI20 ||
441 VK == ELF::R_LARCH_GOT_HI20 || VK == ELF::R_LARCH_TLS_GD_HI20 ||
442 VK == ELF::R_LARCH_TLS_LD_HI20 || VK == ELF::R_LARCH_TLS_IE_HI20 ||
443 VK == ELF::R_LARCH_TLS_LE_HI20 || VK == ELF::R_LARCH_TLS_LE_HI20_R ||
444 VK == ELF::R_LARCH_TLS_DESC_HI20;
445 return IsConstantImm
446 ? isInt<20>(Imm) && IsValidKind
447 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
448 IsValidKind;
449 }
450
451 bool isSImm20lu32id() const {
452 if (!isImm())
453 return false;
454
455 int64_t Imm;
457 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
458 bool IsValidKind =
459 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_ABS64_LO20 ||
460 VK == ELF::R_LARCH_PCALA64_LO20 || VK == ELF::R_LARCH_GOT64_LO20 ||
461 VK == ELF::R_LARCH_GOT64_PC_LO20 || VK == ELF::R_LARCH_TLS_IE64_LO20 ||
462 VK == ELF::R_LARCH_TLS_IE64_PC_LO20 ||
463 VK == ELF::R_LARCH_TLS_LE64_LO20 ||
464 VK == ELF::R_LARCH_TLS_DESC64_PC_LO20 ||
465 VK == ELF::R_LARCH_TLS_DESC64_LO20;
466
467 return IsConstantImm
468 ? isInt<20>(Imm) && IsValidKind
469 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
470 IsValidKind;
471 }
472
473 bool isSImm20pcaddu12i() const {
474 if (!isImm())
475 return false;
476
477 int64_t Imm;
479 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
480 bool IsValidKind =
481 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_CALL30 ||
482 VK == ELF::R_LARCH_PCADD_HI20 || VK == ELF::R_LARCH_GOT_PCADD_HI20 ||
483 VK == ELF::R_LARCH_TLS_IE_PCADD_HI20 ||
484 VK == ELF::R_LARCH_TLS_LD_PCADD_HI20 ||
485 VK == ELF::R_LARCH_TLS_GD_PCADD_HI20 ||
486 VK == ELF::R_LARCH_TLS_DESC_PCADD_HI20;
487
488 return IsConstantImm
489 ? isInt<20>(Imm) && IsValidKind
490 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
491 IsValidKind;
492 }
493
494 bool isSImm20pcaddu18i() const {
495 if (!isImm())
496 return false;
497
498 int64_t Imm;
500 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
501 bool IsValidKind =
502 VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_CALL36;
503
504 return IsConstantImm
505 ? isInt<20>(Imm) && IsValidKind
506 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
507 IsValidKind;
508 }
509
510 bool isSImm20pcaddi() const {
511 if (!isImm())
512 return false;
513
514 int64_t Imm;
516 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
517 bool IsValidKind = VK == LoongArchMCExpr::VK_None ||
518 VK == ELF::R_LARCH_PCREL20_S2 ||
519 VK == ELF::R_LARCH_TLS_LD_PCREL20_S2 ||
520 VK == ELF::R_LARCH_TLS_GD_PCREL20_S2 ||
521 VK == ELF::R_LARCH_TLS_DESC_PCREL20_S2;
522 return IsConstantImm
523 ? isInt<20>(Imm) && IsValidKind
524 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
525 IsValidKind;
526 }
527
528 bool isSImm21lsl2() const {
529 if (!isImm())
530 return false;
531
532 int64_t Imm;
534 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
535 bool IsValidKind = VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_B21;
536 return IsConstantImm
537 ? isShiftedInt<21, 2>(Imm) && IsValidKind
538 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
539 IsValidKind;
540 }
541
542 bool isSImm26Operand() const {
543 if (!isImm())
544 return false;
545
546 int64_t Imm;
548 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
549 bool IsValidKind = VK == LoongArchMCExpr::VK_None || VK == ELF::R_LARCH_B26;
550 return IsConstantImm
551 ? isShiftedInt<26, 2>(Imm) && IsValidKind
552 : LoongArchAsmParser::classifySymbolRef(getImm(), VK) &&
553 IsValidKind;
554 }
555
556 bool isImm32() const { return isSImm<32>() || isUImm<32>(); }
557 bool isImm64() const {
558 if (!isImm())
559 return false;
560 int64_t Imm;
562 bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
563 return IsConstantImm && VK == LoongArchMCExpr::VK_None;
564 }
565
566 /// Gets location of the first token of this operand.
567 SMLoc getStartLoc() const override { return StartLoc; }
568 /// Gets location of the last token of this operand.
569 SMLoc getEndLoc() const override { return EndLoc; }
570
571 MCRegister getReg() const override {
572 assert(Kind == KindTy::Register && "Invalid type access!");
573 return Reg.RegNum;
574 }
575
576 const MCExpr *getImm() const {
577 assert(Kind == KindTy::Immediate && "Invalid type access!");
578 return Imm.Val;
579 }
580
581 StringRef getToken() const {
582 assert(Kind == KindTy::Token && "Invalid type access!");
583 return Tok;
584 }
585
586 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
587 auto RegName = [](MCRegister Reg) {
588 if (Reg)
590 else
591 return "noreg";
592 };
593
594 switch (Kind) {
595 case KindTy::Immediate:
596 MAI.printExpr(OS, *getImm());
597 break;
598 case KindTy::Register:
599 OS << "<register " << RegName(getReg()) << ">";
600 break;
601 case KindTy::Token:
602 OS << "'" << getToken() << "'";
603 break;
604 }
605 }
606
607 static std::unique_ptr<LoongArchOperand> createToken(StringRef Str, SMLoc S) {
608 auto Op = std::make_unique<LoongArchOperand>(KindTy::Token);
609 Op->Tok = Str;
610 Op->StartLoc = S;
611 Op->EndLoc = S;
612 return Op;
613 }
614
615 static std::unique_ptr<LoongArchOperand> createReg(MCRegister Reg, SMLoc S,
616 SMLoc E) {
617 auto Op = std::make_unique<LoongArchOperand>(KindTy::Register);
618 Op->Reg.RegNum = Reg;
619 Op->StartLoc = S;
620 Op->EndLoc = E;
621 return Op;
622 }
623
624 static std::unique_ptr<LoongArchOperand> createImm(const MCExpr *Val, SMLoc S,
625 SMLoc E) {
626 auto Op = std::make_unique<LoongArchOperand>(KindTy::Immediate);
627 Op->Imm.Val = Val;
628 Op->StartLoc = S;
629 Op->EndLoc = E;
630 return Op;
631 }
632
633 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
634 if (auto CE = dyn_cast<MCConstantExpr>(Expr))
635 Inst.addOperand(MCOperand::createImm(CE->getValue()));
636 else
638 }
639
640 // Used by the TableGen Code.
641 void addRegOperands(MCInst &Inst, unsigned N) const {
642 assert(N == 1 && "Invalid number of operands!");
644 }
645 void addImmOperands(MCInst &Inst, unsigned N) const {
646 assert(N == 1 && "Invalid number of operands!");
647 addExpr(Inst, getImm());
648 }
649};
650} // end namespace
651
652#define GET_REGISTER_MATCHER
653#define GET_SUBTARGET_FEATURE_NAME
654#define GET_MATCHER_IMPLEMENTATION
655#define GET_MNEMONIC_SPELL_CHECKER
656#include "LoongArchGenAsmMatcher.inc"
657
658// Attempts to match Name as a register (either using the default name or
659// alternative ABI names), setting RegNo to the matching register. Upon
660// failure, returns true and sets RegNo to 0.
662 RegNo = MatchRegisterName(Name);
663 // The 32-bit and 64-bit FPRs have the same asm name. Check that the initial
664 // match always matches the 32-bit variant, and not the 64-bit one.
665 assert(!(RegNo >= LoongArch::F0_64 && RegNo <= LoongArch::F31_64));
666 // The default FPR register class is based on the tablegen enum ordering.
667 static_assert(LoongArch::F0 < LoongArch::F0_64,
668 "FPR matching must be updated");
669 if (RegNo == LoongArch::NoRegister)
670 RegNo = MatchRegisterAltName(Name);
671
672 return RegNo == LoongArch::NoRegister;
673}
674
675bool LoongArchAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
676 SMLoc &EndLoc) {
677 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
678 return Error(getLoc(), "invalid register name");
679
680 if (!getLoongArchMCRegisterClass(LoongArch::GPRRegClassID).contains(Reg) &&
681 !getLoongArchMCRegisterClass(LoongArch::FPR32RegClassID).contains(Reg))
682 return Error(getLoc(), "invalid register name");
683
684 return false;
685}
686
687ParseStatus LoongArchAsmParser::tryParseRegister(MCRegister &Reg,
688 SMLoc &StartLoc,
689 SMLoc &EndLoc) {
690 const AsmToken &Tok = getParser().getTok();
691 StartLoc = Tok.getLoc();
692 EndLoc = Tok.getEndLoc();
693
694 parseOptionalToken(AsmToken::Dollar);
695 if (getLexer().getKind() != AsmToken::Identifier)
697
698 StringRef Name = Tok.getIdentifier();
699 if (matchRegisterNameHelper(Reg, Name))
701
702 getParser().Lex(); // Eat identifier token.
704}
705
706bool LoongArchAsmParser::classifySymbolRef(const MCExpr *Expr,
709
710 if (const LoongArchMCExpr *RE = dyn_cast<LoongArchMCExpr>(Expr)) {
711 Kind = RE->getSpecifier();
712 Expr = RE->getSubExpr();
713 }
714
715 MCValue Res;
716 if (Expr->evaluateAsRelocatable(Res, nullptr))
718 return false;
719}
720
721ParseStatus LoongArchAsmParser::parseRegister(OperandVector &Operands) {
722 if (!parseOptionalToken(AsmToken::Dollar))
724 if (getLexer().getKind() != AsmToken::Identifier)
726
727 StringRef Name = getLexer().getTok().getIdentifier();
728 MCRegister RegNo;
729 matchRegisterNameHelper(RegNo, Name);
730 if (RegNo == LoongArch::NoRegister)
732
733 SMLoc S = getLoc();
734 SMLoc E = SMLoc::getFromPointer(S.getPointer() + Name.size());
735 getLexer().Lex();
736 Operands.push_back(LoongArchOperand::createReg(RegNo, S, E));
737
739}
740
741ParseStatus LoongArchAsmParser::parseImmediate(OperandVector &Operands) {
742 SMLoc S = getLoc();
743 SMLoc E;
744 const MCExpr *Res;
745
746 switch (getLexer().getKind()) {
747 default:
749 case AsmToken::LParen:
750 case AsmToken::Dot:
751 case AsmToken::Minus:
752 case AsmToken::Plus:
754 case AsmToken::Tilde:
756 case AsmToken::String:
758 if (getParser().parseExpression(Res, E))
760 break;
762 return parseOperandWithModifier(Operands);
763 }
764
765 Operands.push_back(LoongArchOperand::createImm(Res, S, E));
767}
768
769ParseStatus
770LoongArchAsmParser::parseOperandWithModifier(OperandVector &Operands) {
771 SMLoc S = getLoc();
772 SMLoc E;
773
774 if (getLexer().getKind() != AsmToken::Percent)
775 return Error(getLoc(), "expected '%' for operand modifier");
776
777 getParser().Lex(); // Eat '%'
778
779 if (getLexer().getKind() != AsmToken::Identifier)
780 return Error(getLoc(), "expected valid identifier for operand modifier");
781 StringRef Identifier = getParser().getTok().getIdentifier();
782 auto VK = LoongArch::parseSpecifier(Identifier);
783 if (VK == LoongArchMCExpr::VK_None)
784 return Error(getLoc(), "invalid relocation specifier");
785
786 getParser().Lex(); // Eat the identifier
787 if (getLexer().getKind() != AsmToken::LParen)
788 return Error(getLoc(), "expected '('");
789 getParser().Lex(); // Eat '('
790
791 const MCExpr *SubExpr;
792 if (getParser().parseParenExpression(SubExpr, E))
794
795 const MCExpr *ModExpr = LoongArchMCExpr::create(SubExpr, VK, getContext());
796 Operands.push_back(LoongArchOperand::createImm(ModExpr, S, E));
798}
799
800ParseStatus LoongArchAsmParser::parseSImm26Operand(OperandVector &Operands) {
801 SMLoc S = getLoc();
802 const MCExpr *Res;
803
804 if (getLexer().getKind() == AsmToken::Percent)
805 return parseOperandWithModifier(Operands);
806
807 if (getLexer().getKind() != AsmToken::Identifier)
809
810 StringRef Identifier;
811 if (getParser().parseIdentifier(Identifier))
813
814 SMLoc E = SMLoc::getFromPointer(S.getPointer() + Identifier.size());
815
816 MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
818 Operands.push_back(LoongArchOperand::createImm(Res, S, E));
820}
821
822ParseStatus LoongArchAsmParser::parseAtomicMemOp(OperandVector &Operands) {
823 // Parse "$r*".
824 if (!parseRegister(Operands).isSuccess())
826
827 // If there is a next operand and it is 0, ignore it. Otherwise print a
828 // diagnostic message.
829 if (parseOptionalToken(AsmToken::Comma)) {
830 int64_t ImmVal;
831 SMLoc ImmStart = getLoc();
832 if (getParser().parseIntToken(ImmVal, "expected optional integer offset"))
834 if (ImmVal)
835 return Error(ImmStart, "optional integer offset must be 0");
836 }
837
839}
840/// Looks at a token type and creates the relevant operand from this
841/// information, adding to Operands. Return true upon an error.
842bool LoongArchAsmParser::parseOperand(OperandVector &Operands,
843 StringRef Mnemonic) {
844 // Check if the current operand has a custom associated parser, if so, try to
845 // custom parse the operand, or fallback to the general approach.
846 ParseStatus Result =
847 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
848 if (Result.isSuccess())
849 return false;
850 if (Result.isFailure())
851 return true;
852
853 if (parseRegister(Operands).isSuccess() ||
854 parseImmediate(Operands).isSuccess())
855 return false;
856
857 // Finally we have exhausted all options and must declare defeat.
858 return Error(getLoc(), "unknown operand");
859}
860
861bool LoongArchAsmParser::parseInstruction(ParseInstructionInfo &Info,
862 StringRef Name, SMLoc NameLoc,
864 // First operand in MCInst is instruction mnemonic.
865 Operands.push_back(LoongArchOperand::createToken(Name, NameLoc));
866
867 // If there are no more operands, then finish.
868 if (parseOptionalToken(AsmToken::EndOfStatement))
869 return false;
870
871 // Parse first operand.
872 if (parseOperand(Operands, Name))
873 return true;
874
875 // Parse until end of statement, consuming commas between operands.
876 while (parseOptionalToken(AsmToken::Comma))
877 if (parseOperand(Operands, Name))
878 return true;
879
880 // Parse end of statement and return successfully.
881 if (parseOptionalToken(AsmToken::EndOfStatement))
882 return false;
883
884 SMLoc Loc = getLexer().getLoc();
885 getParser().eatToEndOfStatement();
886 return Error(Loc, "unexpected token");
887}
888
889void LoongArchAsmParser::emitLAInstSeq(MCRegister DestReg, MCRegister TmpReg,
890 const MCExpr *Symbol,
891 SmallVectorImpl<Inst> &Insts,
892 SMLoc IDLoc, MCStreamer &Out,
893 bool RelaxHint) {
894 MCContext &Ctx = getContext();
895 MCSymbol *PCALabel = nullptr;
896 for (LoongArchAsmParser::Inst &Inst : Insts) {
897 unsigned Opc = Inst.Opc;
898 auto VK = LoongArchMCExpr::Specifier(Inst.Specifier);
899 const LoongArchMCExpr *LE =
900 LoongArchMCExpr::create(Symbol, VK, Ctx, RelaxHint);
901 switch (Opc) {
902 default:
903 llvm_unreachable("unexpected opcode");
904 case LoongArch::PCADDU12I:
905 PCALabel = Ctx.createNamedTempSymbol("pcadd_hi");
906 Out.emitLabel(PCALabel);
908 case LoongArch::PCALAU12I:
909 case LoongArch::LU12I_W:
910 Out.emitInstruction(MCInstBuilder(Opc).addReg(DestReg).addExpr(LE),
911 getSTI());
912 break;
913 case LoongArch::ORI:
914 case LoongArch::ADDI_W:
915 case LoongArch::LD_W:
916 case LoongArch::LD_D: {
917 if (VK == LoongArchMCExpr::VK_None) {
918 Out.emitInstruction(
919 MCInstBuilder(Opc).addReg(DestReg).addReg(DestReg).addImm(0),
920 getSTI());
921 continue;
922 } else if (VK == ELF::R_LARCH_TLS_DESC_LD) {
923 Out.emitInstruction(MCInstBuilder(Opc)
924 .addReg(LoongArch::R1)
925 .addReg(DestReg)
926 .addExpr(LE),
927 getSTI());
928 continue;
929 } else if (VK == ELF::R_LARCH_PCADD_LO12 ||
930 VK == ELF::R_LARCH_GOT_PCADD_LO12 ||
931 VK == ELF::R_LARCH_TLS_IE_PCADD_LO12 ||
932 VK == ELF::R_LARCH_TLS_LD_PCADD_LO12 ||
933 VK == ELF::R_LARCH_TLS_GD_PCADD_LO12 ||
934 VK == ELF::R_LARCH_TLS_DESC_PCADD_LO12) {
935 Out.emitInstruction(
936 MCInstBuilder(Opc).addReg(DestReg).addReg(DestReg).addExpr(
938 VK, Ctx, RelaxHint)),
939 getSTI());
940 continue;
941 }
942 Out.emitInstruction(
943 MCInstBuilder(Opc).addReg(DestReg).addReg(DestReg).addExpr(LE),
944 getSTI());
945 break;
946 }
947 case LoongArch::LU32I_D:
948 Out.emitInstruction(MCInstBuilder(Opc)
949 .addReg(DestReg == TmpReg ? DestReg : TmpReg)
950 .addReg(DestReg == TmpReg ? DestReg : TmpReg)
951 .addExpr(LE),
952 getSTI());
953 break;
954 case LoongArch::LU52I_D:
955 Out.emitInstruction(
956 MCInstBuilder(Opc).addReg(TmpReg).addReg(TmpReg).addExpr(LE),
957 getSTI());
958 break;
959 case LoongArch::ADDI_D:
960 Out.emitInstruction(
961 MCInstBuilder(Opc)
962 .addReg(TmpReg)
963 .addReg(DestReg == TmpReg ? TmpReg : LoongArch::R0)
964 .addExpr(LE),
965 getSTI());
966 break;
967 case LoongArch::ADD_D:
968 case LoongArch::LDX_D:
969 Out.emitInstruction(
970 MCInstBuilder(Opc).addReg(DestReg).addReg(DestReg).addReg(TmpReg),
971 getSTI());
972 break;
973 case LoongArch::JIRL:
974 Out.emitInstruction(MCInstBuilder(Opc)
975 .addReg(LoongArch::R1)
976 .addReg(LoongArch::R1)
977 .addExpr(LE),
978 getSTI());
979 break;
980 }
981 }
982}
983
984void LoongArchAsmParser::emitLoadAddressAbs(MCInst &Inst, SMLoc IDLoc,
985 MCStreamer &Out) {
986 // la.abs $rd, sym
987 // expands to:
988 // lu12i.w $rd, %abs_hi20(sym)
989 // ori $rd, $rd, %abs_lo12(sym)
990 //
991 // for 64bit appends:
992 // lu32i.d $rd, %abs64_lo20(sym)
993 // lu52i.d $rd, $rd, %abs64_hi12(sym)
994 MCRegister DestReg = Inst.getOperand(0).getReg();
995 const MCExpr *Symbol = Inst.getOpcode() == LoongArch::PseudoLA_ABS
996 ? Inst.getOperand(1).getExpr()
997 : Inst.getOperand(2).getExpr();
998 InstSeq Insts;
999
1000 // To distinguish between la.abs and %abs_hi20, la.abs will generate
1001 // R_LARCH_MARK_LA and R_LARCH_ABS_HI20 relocations.
1002 Insts.push_back(
1003 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_MARK_LA));
1004 Insts.push_back(
1005 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_ABS_LO12));
1006
1007 if (is64Bit()) {
1008 Insts.push_back(
1009 LoongArchAsmParser::Inst(LoongArch::LU32I_D, ELF::R_LARCH_ABS64_LO20));
1010 Insts.push_back(
1011 LoongArchAsmParser::Inst(LoongArch::LU52I_D, ELF::R_LARCH_ABS64_HI12));
1012 }
1013
1014 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1015}
1016
1017void LoongArchAsmParser::emitLoadAddressPcrel(MCInst &Inst, SMLoc IDLoc,
1018 MCStreamer &Out) {
1019 // la.pcrel $rd, sym
1020 //
1021 // for la32r expands to:
1022 // .Lpcadd_hi:
1023 // pcaddu12i $rd, %pcadd_hi20(sym)
1024 // addi.w $rd, rd, %pcadd_lo12(.Lpcadd_hi)
1025 //
1026 // for la32s and la64 expands to:
1027 // pcalau12i $rd, %pc_hi20(sym)
1028 // addi.w/d $rd, rd, %pc_lo12(sym)
1029 MCRegister DestReg = Inst.getOperand(0).getReg();
1030 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1031 InstSeq Insts;
1032 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1033 unsigned ADDI = is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
1034 unsigned PCAIRel =
1035 has32S() ? ELF::R_LARCH_PCALA_HI20 : ELF::R_LARCH_PCADD_HI20;
1036 unsigned ADDIRel =
1037 has32S() ? ELF::R_LARCH_PCALA_LO12 : ELF::R_LARCH_PCADD_LO12;
1038
1039 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1040 Insts.push_back(LoongArchAsmParser::Inst(ADDI, ADDIRel));
1041
1042 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1043 /*RelaxHint=*/true);
1044}
1045
1046void LoongArchAsmParser::emitLoadAddressPcrelLarge(MCInst &Inst, SMLoc IDLoc,
1047 MCStreamer &Out) {
1048 // la.pcrel $rd, $rj, sym
1049 // expands to:
1050 // pcalau12i $rd, %pc_hi20(sym)
1051 // addi.d $rj, $r0, %pc_lo12(sym)
1052 // lu32i.d $rj, %pc64_lo20(sym)
1053 // lu52i.d $rj, $rj, %pc64_hi12(sym)
1054 // add.d $rd, $rd, $rj
1055 MCRegister DestReg = Inst.getOperand(0).getReg();
1056 MCRegister TmpReg = Inst.getOperand(1).getReg();
1057 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1058 InstSeq Insts;
1059
1060 Insts.push_back(
1061 LoongArchAsmParser::Inst(LoongArch::PCALAU12I, ELF::R_LARCH_PCALA_HI20));
1062 Insts.push_back(
1063 LoongArchAsmParser::Inst(LoongArch::ADDI_D, ELF::R_LARCH_PCALA_LO12));
1064 Insts.push_back(
1065 LoongArchAsmParser::Inst(LoongArch::LU32I_D, ELF::R_LARCH_PCALA64_LO20));
1066 Insts.push_back(
1067 LoongArchAsmParser::Inst(LoongArch::LU52I_D, ELF::R_LARCH_PCALA64_HI12));
1068 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::ADD_D));
1069
1070 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1071}
1072
1073void LoongArchAsmParser::emitLoadAddressGot(MCInst &Inst, SMLoc IDLoc,
1074 MCStreamer &Out) {
1075 // la.got $rd, sym
1076 MCRegister DestReg = Inst.getOperand(0).getReg();
1077 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1078 InstSeq Insts;
1079 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1080 unsigned LD = is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
1081 unsigned PCAIRel =
1082 has32S() ? ELF::R_LARCH_GOT_PC_HI20 : ELF::R_LARCH_GOT_PCADD_HI20;
1083 unsigned LDRel =
1084 has32S() ? ELF::R_LARCH_GOT_PC_LO12 : ELF::R_LARCH_GOT_PCADD_LO12;
1085
1086 if (getSTI().hasFeature(LoongArch::LaGlobalWithAbs)) {
1087 // with feature: +la-glabal-with-abs
1088 // for 32bit:
1089 // lu12i.w $rd, %got_hi20(sym)
1090 // ori $rd, $rd, %got_lo12(sym)
1091 // ld.w $rd, $rd, 0
1092 //
1093 // for 64bit:
1094 // lu12i.w $rd, %got_hi20(sym)
1095 // ori $rd, $rd, %got_lo12(sym)
1096 // lu32i.d $rd, %got64_lo20(sym)
1097 // lu52i.d $rd, $rd, %got64_hi12(sym)
1098 // ld.d $rd, $rd, 0
1099 Insts.push_back(
1100 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_GOT_HI20));
1101 Insts.push_back(
1102 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_GOT_LO12));
1103
1104 if (is64Bit()) {
1105 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1106 ELF::R_LARCH_GOT64_LO20));
1107 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1108 ELF::R_LARCH_GOT64_HI12));
1109 }
1110 Insts.push_back(LoongArchAsmParser::Inst(LD));
1111 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1112 return;
1113 }
1114 // for la32r expands to:
1115 // .Lpcadd_hi:
1116 // pcaddu12i $rd, %got_pcadd_hi20(sym)
1117 // ld.w $rd, $rd, %got_pcadd_lo12(.Lpcadd_hi)
1118 //
1119 // for la32s and la64 expands to:
1120 // pcalau12i $rd, %got_pc_hi20(sym)
1121 // ld.w/d $rd, $rd, %got_pc_lo12(sym)
1122 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1123 Insts.push_back(LoongArchAsmParser::Inst(LD, LDRel));
1124
1125 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1126 /*RelaxHint=*/true);
1127}
1128
1129void LoongArchAsmParser::emitLoadAddressGotLarge(MCInst &Inst, SMLoc IDLoc,
1130 MCStreamer &Out) {
1131 // la.got $rd, $rj, sym
1132 // expands to:
1133 // pcalau12i $rd, %got_pc_hi20(sym)
1134 // addi.d $rj, $r0, %got_pc_lo12(sym)
1135 // lu32i.d $rj, %got64_pc_lo20(sym)
1136 // lu52i.d $rj, $rj, %got64_pc_hi12(sym)
1137 // ldx.d $rd, $rd, $rj
1138 MCRegister DestReg = Inst.getOperand(0).getReg();
1139 MCRegister TmpReg = Inst.getOperand(1).getReg();
1140 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1141 InstSeq Insts;
1142
1143 Insts.push_back(
1144 LoongArchAsmParser::Inst(LoongArch::PCALAU12I, ELF::R_LARCH_GOT_PC_HI20));
1145 Insts.push_back(
1146 LoongArchAsmParser::Inst(LoongArch::ADDI_D, ELF::R_LARCH_GOT_PC_LO12));
1147 Insts.push_back(
1148 LoongArchAsmParser::Inst(LoongArch::LU32I_D, ELF::R_LARCH_GOT64_PC_LO20));
1149 Insts.push_back(
1150 LoongArchAsmParser::Inst(LoongArch::LU52I_D, ELF::R_LARCH_GOT64_PC_HI12));
1151 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LDX_D));
1152
1153 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1154}
1155
1156void LoongArchAsmParser::emitLoadAddressTLSLE(MCInst &Inst, SMLoc IDLoc,
1157 MCStreamer &Out) {
1158 // la.tls.le $rd, sym
1159 // expands to:
1160 // lu12i.w $rd, %le_hi20(sym)
1161 // ori $rd, $rd, %le_lo12(sym)
1162 MCRegister DestReg = Inst.getOperand(0).getReg();
1163 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1164 InstSeq Insts;
1165
1166 Insts.push_back(
1167 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_TLS_LE_HI20));
1168 Insts.push_back(
1169 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_TLS_LE_LO12));
1170
1171 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1172}
1173
1174void LoongArchAsmParser::emitLoadAddressTLSIE(MCInst &Inst, SMLoc IDLoc,
1175 MCStreamer &Out) {
1176 // la.tls.ie $rd, sym
1177 MCRegister DestReg = Inst.getOperand(0).getReg();
1178 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1179 InstSeq Insts;
1180 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1181 unsigned LD = is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
1182 unsigned PCAIRel =
1183 has32S() ? ELF::R_LARCH_TLS_IE_PC_HI20 : ELF::R_LARCH_TLS_IE_PCADD_HI20;
1184 unsigned LDRel =
1185 has32S() ? ELF::R_LARCH_TLS_IE_PC_LO12 : ELF::R_LARCH_TLS_IE_PCADD_LO12;
1186
1187 if (getSTI().hasFeature(LoongArch::LaGlobalWithAbs)) {
1188 // with feature: +la-glabal-with-abs
1189 // for 32bit:
1190 // lu12i.w $rd, %ie_hi20(sym)
1191 // ori $rd, $rd, %ie_lo12(sym)
1192 // ld.w $rd, $rd, 0
1193 //
1194 // for 64bit:
1195 // lu12i.w $rd, %ie_hi20(sym)
1196 // ori $rd, $rd, %ie_lo12(sym)
1197 // lu32i.d $rd, %ie64_lo20(sym)
1198 // lu52i.d $rd, $rd, %ie64_hi12(sym)
1199 // ld.d $rd, $rd, 0
1200 Insts.push_back(
1201 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_TLS_IE_HI20));
1202 Insts.push_back(
1203 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_TLS_IE_LO12));
1204
1205 if (is64Bit()) {
1206 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1207 ELF::R_LARCH_TLS_IE64_LO20));
1208 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1209 ELF::R_LARCH_TLS_IE64_HI12));
1210 }
1211 Insts.push_back(LoongArchAsmParser::Inst(LD));
1212 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1213 return;
1214 }
1215
1216 // for la32r expands to:
1217 // .Lpcadd_hi:
1218 // pcaddu12i $rd, %ie_pcadd_hi20(sym)
1219 // ld.w $rd, $rd, %ie_pcadd_lo12(.Lpcadd_hi)
1220 //
1221 // for la32s and la64 expands to:
1222 // pcalau12i $rd, %ie_pc_hi20(sym)
1223 // ld.w/d $rd, $rd, %ie_pc_lo12(sym)
1224 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1225 Insts.push_back(LoongArchAsmParser::Inst(LD, LDRel));
1226
1227 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1228 /*RelaxHint=*/true);
1229}
1230
1231void LoongArchAsmParser::emitLoadAddressTLSIELarge(MCInst &Inst, SMLoc IDLoc,
1232 MCStreamer &Out) {
1233 // la.tls.ie $rd, $rj, sym
1234 // expands to:
1235 // pcalau12i $rd, %ie_pc_hi20(sym)
1236 // addi.d $rj, $r0, %ie_pc_lo12(sym)
1237 // lu32i.d $rj, %ie64_pc_lo20(sym)
1238 // lu52i.d $rj, $rj, %ie64_pc_hi12(sym)
1239 // ldx.d $rd, $rd, $rj
1240 MCRegister DestReg = Inst.getOperand(0).getReg();
1241 MCRegister TmpReg = Inst.getOperand(1).getReg();
1242 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1243 InstSeq Insts;
1244
1245 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::PCALAU12I,
1246 ELF::R_LARCH_TLS_IE_PC_HI20));
1247 Insts.push_back(
1248 LoongArchAsmParser::Inst(LoongArch::ADDI_D, ELF::R_LARCH_TLS_IE_PC_LO12));
1249 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1250 ELF::R_LARCH_TLS_IE64_PC_LO20));
1251 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1252 ELF::R_LARCH_TLS_IE64_PC_HI12));
1253 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LDX_D));
1254
1255 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1256}
1257
1258void LoongArchAsmParser::emitLoadAddressTLSLD(MCInst &Inst, SMLoc IDLoc,
1259 MCStreamer &Out) {
1260 // la.tls.ld $rd, sym
1261 MCRegister DestReg = Inst.getOperand(0).getReg();
1262 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1263 InstSeq Insts;
1264 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1265 unsigned ADDI = is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
1266 unsigned PCAIRel =
1267 has32S() ? ELF::R_LARCH_TLS_LD_PC_HI20 : ELF::R_LARCH_TLS_LD_PCADD_HI20;
1268 unsigned ADDIRel =
1269 has32S() ? ELF::R_LARCH_GOT_PC_LO12 : ELF::R_LARCH_TLS_LD_PCADD_LO12;
1270
1271 if (getSTI().hasFeature(LoongArch::LaGlobalWithAbs)) {
1272 // with feature: +la-glabal-with-abs
1273 // for 32bit:
1274 // lu12i.w $rd, %ld_hi20(sym)
1275 // ori $rd, $rd, %got_lo12(sym)
1276 //
1277 // for 64bit:
1278 // lu12i.w $rd, %ld_hi20(sym)
1279 // ori $rd, $rd, %got_lo12(sym)
1280 // lu32i.d $rd, %got64_lo20(sym)
1281 // lu52i.d $rd, $rd, %got64_hi12(sym)
1282 Insts.push_back(
1283 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_TLS_LD_HI20));
1284 Insts.push_back(
1285 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_GOT_LO12));
1286
1287 if (is64Bit()) {
1288 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1289 ELF::R_LARCH_GOT64_LO20));
1290 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1291 ELF::R_LARCH_GOT64_HI12));
1292 }
1293 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1294 return;
1295 }
1296
1297 // for la32r expands to:
1298 // .Lpcadd_hi:
1299 // pcaddu12i $rd, %ld_pcadd_hi20(sym)
1300 // addi.w $rd, $rd, %ld_pcadd_lo12(.Lpcadd_hi)
1301 //
1302 // for la32s and la64 expands to:
1303 // pcalau12i $rd, %ld_pc_hi20(sym)
1304 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
1305 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1306 Insts.push_back(LoongArchAsmParser::Inst(ADDI, ADDIRel));
1307
1308 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1309 /*RelaxHint=*/true);
1310}
1311
1312void LoongArchAsmParser::emitLoadAddressTLSLDLarge(MCInst &Inst, SMLoc IDLoc,
1313 MCStreamer &Out) {
1314 // la.tls.ld $rd, $rj, sym
1315 // expands to:
1316 // pcalau12i $rd, %ld_pc_hi20(sym)
1317 // addi.d $rj, $r0, %got_pc_lo12(sym)
1318 // lu32i.d $rj, %got64_pc_lo20(sym)
1319 // lu52i.d $rj, $rj, %got64_pc_hi12(sym)
1320 // add.d $rd, $rd, $rj
1321 MCRegister DestReg = Inst.getOperand(0).getReg();
1322 MCRegister TmpReg = Inst.getOperand(1).getReg();
1323 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1324 InstSeq Insts;
1325
1326 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::PCALAU12I,
1327 ELF::R_LARCH_TLS_LD_PC_HI20));
1328 Insts.push_back(
1329 LoongArchAsmParser::Inst(LoongArch::ADDI_D, ELF::R_LARCH_GOT_PC_LO12));
1330 Insts.push_back(
1331 LoongArchAsmParser::Inst(LoongArch::LU32I_D, ELF::R_LARCH_GOT64_PC_LO20));
1332 Insts.push_back(
1333 LoongArchAsmParser::Inst(LoongArch::LU52I_D, ELF::R_LARCH_GOT64_PC_HI12));
1334 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::ADD_D));
1335
1336 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1337}
1338
1339void LoongArchAsmParser::emitLoadAddressTLSGD(MCInst &Inst, SMLoc IDLoc,
1340 MCStreamer &Out) {
1341 // la.tls.gd $rd, sym
1342 MCRegister DestReg = Inst.getOperand(0).getReg();
1343 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1344 InstSeq Insts;
1345 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1346 unsigned ADDI = is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
1347 unsigned PCAIRel =
1348 has32S() ? ELF::R_LARCH_TLS_GD_PC_HI20 : ELF::R_LARCH_TLS_GD_PCADD_HI20;
1349 unsigned ADDIRel =
1350 has32S() ? ELF::R_LARCH_GOT_PC_LO12 : ELF::R_LARCH_TLS_GD_PCADD_LO12;
1351
1352 if (getSTI().hasFeature(LoongArch::LaGlobalWithAbs)) {
1353 // with feature: +la-glabal-with-abs
1354 // for 32bit:
1355 // lu12i.w $rd, %gd_hi20(sym)
1356 // ori $rd, $rd, %got_lo12(sym)
1357 //
1358 // for 64bit:
1359 // lu12i.w $rd, %gd_hi20(sym)
1360 // ori $rd, $rd, %got_lo12(sym)
1361 // lu32i.d $rd, %got64_lo20(sym)
1362 // lu52i.d $rd, $rd, %got64_hi12(sym)
1363 Insts.push_back(
1364 LoongArchAsmParser::Inst(LoongArch::LU12I_W, ELF::R_LARCH_TLS_GD_HI20));
1365 Insts.push_back(
1366 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_GOT_LO12));
1367
1368 if (is64Bit()) {
1369 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1370 ELF::R_LARCH_GOT64_LO20));
1371 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1372 ELF::R_LARCH_GOT64_HI12));
1373 }
1374 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1375 return;
1376 }
1377
1378 // for la32r expands to:
1379 // .Lpcadd_hi:
1380 // pcaddu12i $rd, %gd_pcadd_hi20(sym)
1381 // addi.w $rd, $rd, %gd_pcadd_lo12(.Lpcadd_hi)
1382 //
1383 // for la32s and la64 expands to:
1384 // pcalau12i $rd, %gd_pc_hi20(sym)
1385 // addi.w/d $rd, $rd, %got_pc_lo12(sym)
1386 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1387 Insts.push_back(LoongArchAsmParser::Inst(ADDI, ADDIRel));
1388
1389 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1390 /*RelaxHint=*/true);
1391}
1392
1393void LoongArchAsmParser::emitLoadAddressTLSGDLarge(MCInst &Inst, SMLoc IDLoc,
1394 MCStreamer &Out) {
1395 // la.tls.gd $rd, $rj, sym
1396 // expands to:
1397 // pcalau12i $rd, %gd_pc_hi20(sym)
1398 // addi.d $rj, $r0, %got_pc_lo12(sym)
1399 // lu32i.d $rj, %got64_pc_lo20(sym)
1400 // lu52i.d $rj, $rj, %got64_pc_hi12(sym)
1401 // add.d $rd, $rd, $rj
1402 MCRegister DestReg = Inst.getOperand(0).getReg();
1403 MCRegister TmpReg = Inst.getOperand(1).getReg();
1404 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1405 InstSeq Insts;
1406
1407 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::PCALAU12I,
1408 ELF::R_LARCH_TLS_GD_PC_HI20));
1409 Insts.push_back(
1410 LoongArchAsmParser::Inst(LoongArch::ADDI_D, ELF::R_LARCH_GOT_PC_LO12));
1411 Insts.push_back(
1412 LoongArchAsmParser::Inst(LoongArch::LU32I_D, ELF::R_LARCH_GOT64_PC_LO20));
1413 Insts.push_back(
1414 LoongArchAsmParser::Inst(LoongArch::LU52I_D, ELF::R_LARCH_GOT64_PC_HI12));
1415 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::ADD_D));
1416
1417 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1418}
1419
1420void LoongArchAsmParser::emitLoadAddressTLSDesc(MCInst &Inst, SMLoc IDLoc,
1421 MCStreamer &Out) {
1422 // la.tls.desc $rd, sym
1423 MCRegister DestReg = Inst.getOperand(0).getReg();
1424 const MCExpr *Symbol = Inst.getOperand(1).getExpr();
1425 unsigned PCAI = has32S() ? LoongArch::PCALAU12I : LoongArch::PCADDU12I;
1426 unsigned ADDI = is64Bit() ? LoongArch::ADDI_D : LoongArch::ADDI_W;
1427 unsigned LD = is64Bit() ? LoongArch::LD_D : LoongArch::LD_W;
1428 unsigned PCAIRel = has32S() ? ELF::R_LARCH_TLS_DESC_PC_HI20
1429 : ELF::R_LARCH_TLS_DESC_PCADD_HI20;
1430 unsigned ADDIRel = has32S() ? ELF::R_LARCH_TLS_DESC_PC_LO12
1431 : ELF::R_LARCH_TLS_DESC_PCADD_LO12;
1432 InstSeq Insts;
1433
1434 if (getSTI().hasFeature(LoongArch::LaGlobalWithAbs)) {
1435 // with feature: +la-glabal-with-abs
1436 // for la32 expands to:
1437 // lu12i.w $rd, %desc_hi20(sym)
1438 // ori $rd, $rd, %desc_lo12(sym)
1439 // ld.w $ra, $rd, %desc_ld(sym)
1440 // jirl $ra, $ra, %desc_call(sym)
1441 //
1442 // for la64 expands to:
1443 // lu12i.w $rd, %desc_hi20(sym)
1444 // ori $rd, $rd, %desc_lo12(sym)
1445 // lu32i.d $rd, %desc64_lo20(sym)
1446 // lu52i.d $rd, $rd, %desc64_hi12(sym)
1447 // ld.d $ra, $rd, %desc_ld(sym)
1448 // jirl $ra, $ra, %desc_call(sym)
1449 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU12I_W,
1450 ELF::R_LARCH_TLS_DESC_HI20));
1451 Insts.push_back(
1452 LoongArchAsmParser::Inst(LoongArch::ORI, ELF::R_LARCH_TLS_DESC_LO12));
1453
1454 if (is64Bit()) {
1455 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1456 ELF::R_LARCH_TLS_DESC64_LO20));
1457 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1458 ELF::R_LARCH_TLS_DESC64_HI12));
1459 }
1460
1461 Insts.push_back(LoongArchAsmParser::Inst(LD, ELF::R_LARCH_TLS_DESC_LD));
1462 Insts.push_back(
1463 LoongArchAsmParser::Inst(LoongArch::JIRL, ELF::R_LARCH_TLS_DESC_CALL));
1464
1465 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out);
1466 return;
1467 }
1468
1469 // for la32r expands to:
1470 // .Lpcadd_hi:
1471 // pcaddu12i $rd, %desc_pcadd_hi20(sym)
1472 // addi.w $rd, $rd, %desc_pcadd_lo12(.Lpcadd_hi)
1473 // ld.w $ra, $rd, %desc_ld(sym)
1474 // jirl $ra, $ra, %desc_call(sym)
1475 //
1476 // for la32s and la64 expands to:
1477 // pcalau12i $rd, %desc_pc_hi20(sym)
1478 // addi.w/d $rd, $rd, %desc_pc_lo12(sym)
1479 // ld.w/d $ra, $rd, %desc_ld(sym)
1480 // jirl $ra, $ra, %desc_call(sym)
1481 Insts.push_back(LoongArchAsmParser::Inst(PCAI, PCAIRel));
1482 Insts.push_back(LoongArchAsmParser::Inst(ADDI, ADDIRel));
1483 Insts.push_back(LoongArchAsmParser::Inst(LD, ELF::R_LARCH_TLS_DESC_LD));
1484 Insts.push_back(
1485 LoongArchAsmParser::Inst(LoongArch::JIRL, ELF::R_LARCH_TLS_DESC_CALL));
1486
1487 emitLAInstSeq(DestReg, DestReg, Symbol, Insts, IDLoc, Out,
1488 /*RelaxHint=*/true);
1489}
1490
1491void LoongArchAsmParser::emitLoadAddressTLSDescLarge(MCInst &Inst, SMLoc IDLoc,
1492 MCStreamer &Out) {
1493 // la.tls.desc $rd, $rj, sym
1494 // expands to:
1495 // pcalau12i $rd, %desc_pc_hi20(sym)
1496 // addi.d $rj, $r0, %desc_pc_lo12(sym)
1497 // lu32i.d $rj, %desc64_pc_lo20(sym)
1498 // lu52i.d $rj, $rj, %desc64_pc_hi12(sym)
1499 // add.d $rd, $rd, $rj
1500 // ld.w/d $ra, $rd, %desc_ld(sym)
1501 // jirl $ra, $ra, %desc_call(sym)
1502 MCRegister DestReg = Inst.getOperand(0).getReg();
1503 MCRegister TmpReg = Inst.getOperand(1).getReg();
1504 const MCExpr *Symbol = Inst.getOperand(2).getExpr();
1505 InstSeq Insts;
1506
1507 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::PCALAU12I,
1508 ELF::R_LARCH_TLS_DESC_PC_HI20));
1509 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::ADDI_D,
1510 ELF::R_LARCH_TLS_DESC_PC_LO12));
1511 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU32I_D,
1512 ELF::R_LARCH_TLS_DESC64_PC_LO20));
1513 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::LU52I_D,
1514 ELF::R_LARCH_TLS_DESC64_PC_HI12));
1515 Insts.push_back(LoongArchAsmParser::Inst(LoongArch::ADD_D));
1516 Insts.push_back(
1517 LoongArchAsmParser::Inst(LoongArch::LD_D, ELF::R_LARCH_TLS_DESC_LD));
1518 Insts.push_back(
1519 LoongArchAsmParser::Inst(LoongArch::JIRL, ELF::R_LARCH_TLS_DESC_CALL));
1520
1521 emitLAInstSeq(DestReg, TmpReg, Symbol, Insts, IDLoc, Out);
1522}
1523
1524void LoongArchAsmParser::emitLoadImm(MCInst &Inst, SMLoc IDLoc,
1525 MCStreamer &Out) {
1526 MCRegister DestReg = Inst.getOperand(0).getReg();
1527 int64_t Imm = Inst.getOperand(1).getImm();
1528 MCRegister SrcReg = LoongArch::R0;
1529
1530 if (Inst.getOpcode() == LoongArch::PseudoLI_W)
1532
1533 for (LoongArchMatInt::Inst &Inst : LoongArchMatInt::generateInstSeq(Imm)) {
1534 switch (Inst.Opc) {
1535 case LoongArch::LU12I_W:
1536 Out.emitInstruction(
1537 MCInstBuilder(Inst.Opc).addReg(DestReg).addImm(Inst.Imm), getSTI());
1538 break;
1539 case LoongArch::ADDI_W:
1540 case LoongArch::ORI:
1541 case LoongArch::LU32I_D:
1542 case LoongArch::LU52I_D:
1543 case LoongArch::SLLI_D:
1544 Out.emitInstruction(
1545 MCInstBuilder(Inst.Opc).addReg(DestReg).addReg(SrcReg).addImm(
1546 Inst.Imm),
1547 getSTI());
1548 break;
1549 case LoongArch::BSTRINS_D:
1550 Out.emitInstruction(MCInstBuilder(Inst.Opc)
1551 .addReg(DestReg)
1552 .addReg(SrcReg)
1553 .addReg(SrcReg)
1554 .addImm(Inst.Imm >> 32)
1555 .addImm(Inst.Imm & 0xFF),
1556 getSTI());
1557 break;
1558 default:
1559 llvm_unreachable("unexpected opcode generated by LoongArchMatInt");
1560 }
1561 SrcReg = DestReg;
1562 }
1563}
1564
1565void LoongArchAsmParser::emitFuncCall(MCInst &Inst, SMLoc IDLoc,
1566 MCStreamer &Out, bool IsTailCall,
1567 bool IsCall36) {
1568 // call30 sym
1569 // expands to:
1570 // pcaddu12i $ra, %call30(sym)
1571 // jirl $ra, $ra, 0
1572 //
1573 // tail30 $rj, sym
1574 // expands to:
1575 // pcaddu12i $rj, %call30(sym)
1576 // jirl $r0, $rj, 0
1577 //
1578 // call36 sym
1579 // expands to:
1580 // pcaddu18i $ra, %call36(sym)
1581 // jirl $ra, $ra, 0
1582 //
1583 // tail36 $rj, sym
1584 // expands to:
1585 // pcaddu18i $rj, %call36(sym)
1586 // jirl $r0, $rj, 0
1587 MCRegister ScratchReg =
1588 IsTailCall ? Inst.getOperand(0).getReg() : MCRegister(LoongArch::R1);
1589 unsigned PCAI = IsCall36 ? LoongArch::PCADDU18I : LoongArch::PCADDU12I;
1590 unsigned Rel = IsCall36 ? ELF::R_LARCH_CALL36 : ELF::R_LARCH_CALL30;
1591 const MCExpr *Sym =
1592 IsTailCall ? Inst.getOperand(1).getExpr() : Inst.getOperand(0).getExpr();
1593 const LoongArchMCExpr *LE =
1594 LoongArchMCExpr::create(Sym, Rel, getContext(), /*RelaxHint=*/true);
1595
1596 Out.emitInstruction(MCInstBuilder(PCAI).addReg(ScratchReg).addExpr(LE),
1597 getSTI());
1598 Out.emitInstruction(
1599 MCInstBuilder(LoongArch::JIRL)
1600 .addReg(IsTailCall ? MCRegister(LoongArch::R0) : ScratchReg)
1601 .addReg(ScratchReg)
1602 .addImm(0),
1603 getSTI());
1604}
1605
1606bool LoongArchAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1608 MCStreamer &Out) {
1609 Inst.setLoc(IDLoc);
1610 switch (Inst.getOpcode()) {
1611 default:
1612 break;
1613 case LoongArch::PseudoLA_ABS:
1614 case LoongArch::PseudoLA_ABS_LARGE:
1615 emitLoadAddressAbs(Inst, IDLoc, Out);
1616 return false;
1617 case LoongArch::PseudoLA_PCREL:
1618 emitLoadAddressPcrel(Inst, IDLoc, Out);
1619 return false;
1620 case LoongArch::PseudoLA_PCREL_LARGE:
1621 emitLoadAddressPcrelLarge(Inst, IDLoc, Out);
1622 return false;
1623 case LoongArch::PseudoLA_GOT:
1624 emitLoadAddressGot(Inst, IDLoc, Out);
1625 return false;
1626 case LoongArch::PseudoLA_GOT_LARGE:
1627 emitLoadAddressGotLarge(Inst, IDLoc, Out);
1628 return false;
1629 case LoongArch::PseudoLA_TLS_LE:
1630 emitLoadAddressTLSLE(Inst, IDLoc, Out);
1631 return false;
1632 case LoongArch::PseudoLA_TLS_IE:
1633 emitLoadAddressTLSIE(Inst, IDLoc, Out);
1634 return false;
1635 case LoongArch::PseudoLA_TLS_IE_LARGE:
1636 emitLoadAddressTLSIELarge(Inst, IDLoc, Out);
1637 return false;
1638 case LoongArch::PseudoLA_TLS_LD:
1639 emitLoadAddressTLSLD(Inst, IDLoc, Out);
1640 return false;
1641 case LoongArch::PseudoLA_TLS_LD_LARGE:
1642 emitLoadAddressTLSLDLarge(Inst, IDLoc, Out);
1643 return false;
1644 case LoongArch::PseudoLA_TLS_GD:
1645 emitLoadAddressTLSGD(Inst, IDLoc, Out);
1646 return false;
1647 case LoongArch::PseudoLA_TLS_GD_LARGE:
1648 emitLoadAddressTLSGDLarge(Inst, IDLoc, Out);
1649 return false;
1650 case LoongArch::PseudoLA_TLS_DESC:
1651 emitLoadAddressTLSDesc(Inst, IDLoc, Out);
1652 return false;
1653 case LoongArch::PseudoLA_TLS_DESC_LARGE:
1654 emitLoadAddressTLSDescLarge(Inst, IDLoc, Out);
1655 return false;
1656 case LoongArch::PseudoLI_W:
1657 case LoongArch::PseudoLI_D:
1658 emitLoadImm(Inst, IDLoc, Out);
1659 return false;
1660 case LoongArch::PseudoCALL:
1661 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/false,
1662 /*IsCall36=*/is64Bit());
1663 return false;
1664 case LoongArch::PseudoCALL30:
1665 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/false, /*IsCall36=*/false);
1666 return false;
1667 case LoongArch::PseudoCALL36:
1668 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/false, /*IsCall36=*/true);
1669 return false;
1670 case LoongArch::PseudoTAIL:
1671 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/true, /*IsCall36=*/is64Bit());
1672 return false;
1673 case LoongArch::PseudoTAIL30:
1674 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/true, /*IsCall36=*/false);
1675 return false;
1676 case LoongArch::PseudoTAIL36:
1677 emitFuncCall(Inst, IDLoc, Out, /*IsTailCall=*/true, /*IsCall36=*/true);
1678 return false;
1679 }
1680 Out.emitInstruction(Inst, getSTI());
1681 return false;
1682}
1683
1684unsigned LoongArchAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
1685 unsigned Opc = Inst.getOpcode();
1686 const MCInstrDesc &MCID = MII.get(Opc);
1687 switch (Opc) {
1688 default:
1690 const bool IsAMCAS = LoongArchII::isAMCAS(MCID.TSFlags);
1691 MCRegister Rd = Inst.getOperand(0).getReg();
1692 MCRegister Rk = Inst.getOperand(IsAMCAS ? 2 : 1).getReg();
1693 MCRegister Rj = Inst.getOperand(IsAMCAS ? 3 : 2).getReg();
1694 if ((Rd == Rk || Rd == Rj) && Rd != LoongArch::R0)
1695 return Match_RequiresAMORdDifferRkRj;
1696 }
1697 break;
1698 case LoongArch::PseudoLA_TLS_DESC:
1699 case LoongArch::PseudoLA_TLS_DESC_LARGE: {
1700 MCRegister Rd = Inst.getOperand(0).getReg();
1701 if (Rd != LoongArch::R4)
1702 return Match_RequiresLAORdR4;
1703 break;
1704 }
1705 case LoongArch::PseudoLA_PCREL_LARGE:
1706 case LoongArch::PseudoLA_GOT_LARGE:
1707 case LoongArch::PseudoLA_TLS_IE_LARGE:
1708 case LoongArch::PseudoLA_TLS_LD_LARGE:
1709 case LoongArch::PseudoLA_TLS_GD_LARGE: {
1710 MCRegister Rd = Inst.getOperand(0).getReg();
1711 MCRegister Rj = Inst.getOperand(1).getReg();
1712 if (Rd == Rj)
1713 return Match_RequiresLAORdDifferRj;
1714 break;
1715 }
1716 case LoongArch::CSRXCHG:
1717 case LoongArch::GCSRXCHG: {
1718 MCRegister Rj = Inst.getOperand(2).getReg();
1719 if (Rj == LoongArch::R0 || Rj == LoongArch::R1)
1720 return Match_RequiresOpnd2NotR0R1;
1721 return Match_Success;
1722 }
1723 case LoongArch::BSTRINS_W:
1724 case LoongArch::BSTRINS_D:
1725 case LoongArch::BSTRPICK_W:
1726 case LoongArch::BSTRPICK_D: {
1727 unsigned Opc = Inst.getOpcode();
1728 const signed Msb =
1729 (Opc == LoongArch::BSTRINS_W || Opc == LoongArch::BSTRINS_D)
1730 ? Inst.getOperand(3).getImm()
1731 : Inst.getOperand(2).getImm();
1732 const signed Lsb =
1733 (Opc == LoongArch::BSTRINS_W || Opc == LoongArch::BSTRINS_D)
1734 ? Inst.getOperand(4).getImm()
1735 : Inst.getOperand(3).getImm();
1736 if (Msb < Lsb)
1737 return Match_RequiresMsbNotLessThanLsb;
1738 return Match_Success;
1739 }
1740 }
1741
1742 return Match_Success;
1743}
1744
1745unsigned
1746LoongArchAsmParser::validateTargetOperandClass(const MCParsedAsmOperand &AsmOp,
1747 unsigned Kind) {
1748 const LoongArchOperand &Op = static_cast<const LoongArchOperand &>(AsmOp);
1749 if (!Op.isReg())
1750 return Match_InvalidOperand;
1751
1752 MCRegister Reg = Op.getReg();
1753 if (Kind == MCK_GPRNoR0R1 && (Reg == LoongArch::R0 || Reg == LoongArch::R1))
1754 return Match_RequiresOpnd2NotR0R1;
1755
1756 return Match_InvalidOperand;
1757}
1758
1759bool LoongArchAsmParser::generateImmOutOfRangeError(
1760 OperandVector &Operands, uint64_t ErrorInfo, int64_t Lower, int64_t Upper,
1761 const Twine &Msg = "immediate must be an integer in the range") {
1762 SMLoc ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
1763 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
1764}
1765
1766bool LoongArchAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1768 MCStreamer &Out,
1769 uint64_t &ErrorInfo,
1770 bool MatchingInlineAsm) {
1771 MCInst Inst;
1772 FeatureBitset MissingFeatures;
1773
1774 auto Result = MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,
1775 MatchingInlineAsm);
1776 switch (Result) {
1777 default:
1778 break;
1779 case Match_Success:
1780 return processInstruction(Inst, IDLoc, Operands, Out);
1781 case Match_MissingFeature: {
1782 assert(MissingFeatures.any() && "Unknown missing features!");
1783 bool FirstFeature = true;
1784 std::string Msg = "instruction requires the following:";
1785 for (unsigned Feature : MissingFeatures) {
1786 Msg += FirstFeature ? " " : ", ";
1787 Msg += getSubtargetFeatureName(Feature);
1788 FirstFeature = false;
1789 }
1790 return Error(IDLoc, Msg);
1791 }
1792 case Match_MnemonicFail: {
1793 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
1794 std::string Suggestion = LoongArchMnemonicSpellCheck(
1795 ((LoongArchOperand &)*Operands[0]).getToken(), FBS, 0);
1796 return Error(IDLoc, "unrecognized instruction mnemonic" + Suggestion);
1797 }
1798 case Match_InvalidOperand: {
1799 SMLoc ErrorLoc = IDLoc;
1800 if (ErrorInfo != ~0ULL) {
1801 if (ErrorInfo >= Operands.size())
1802 return Error(ErrorLoc, "too few operands for instruction");
1803
1804 ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
1805 if (ErrorLoc == SMLoc())
1806 ErrorLoc = IDLoc;
1807 }
1808 return Error(ErrorLoc, "invalid operand for instruction");
1809 }
1810 }
1811
1812 // Handle the case when the error message is of specific type
1813 // other than the generic Match_InvalidOperand, and the
1814 // corresponding operand is missing.
1815 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
1816 SMLoc ErrorLoc = IDLoc;
1817 if (ErrorInfo != ~0ULL && ErrorInfo >= Operands.size())
1818 return Error(ErrorLoc, "too few operands for instruction");
1819 }
1820
1821 switch (Result) {
1822 default:
1823 break;
1824 case Match_RequiresMsbNotLessThanLsb: {
1825 SMLoc ErrorStart = Operands[3]->getStartLoc();
1826 return Error(ErrorStart, "msb is less than lsb",
1827 SMRange(ErrorStart, Operands[4]->getEndLoc()));
1828 }
1829 case Match_RequiresOpnd2NotR0R1:
1830 return Error(Operands[2]->getStartLoc(), "must not be $r0 or $r1");
1831 case Match_RequiresAMORdDifferRkRj:
1832 return Error(Operands[1]->getStartLoc(),
1833 "$rd must be different from both $rk and $rj");
1834 case Match_RequiresLAORdDifferRj:
1835 return Error(Operands[1]->getStartLoc(), "$rd must be different from $rj");
1836 case Match_RequiresLAORdR4:
1837 return Error(Operands[1]->getStartLoc(), "$rd must be $r4");
1838 case Match_InvalidUImm1:
1839 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1840 /*Upper=*/(1 << 1) - 1);
1841 case Match_InvalidUImm2:
1842 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1843 /*Upper=*/(1 << 2) - 1);
1844 case Match_InvalidUImm2plus1:
1845 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/1,
1846 /*Upper=*/(1 << 2));
1847 case Match_InvalidUImm3:
1848 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1849 /*Upper=*/(1 << 3) - 1);
1850 case Match_InvalidUImm4:
1851 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1852 /*Upper=*/(1 << 4) - 1);
1853 case Match_InvalidUImm5:
1854 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1855 /*Upper=*/(1 << 5) - 1);
1856 case Match_InvalidUImm6:
1857 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1858 /*Upper=*/(1 << 6) - 1);
1859 case Match_InvalidUImm7:
1860 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1861 /*Upper=*/(1 << 7) - 1);
1862 case Match_InvalidUImm8:
1863 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1864 /*Upper=*/(1 << 8) - 1);
1865 case Match_InvalidUImm12:
1866 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1867 /*Upper=*/(1 << 12) - 1);
1868 case Match_InvalidUImm12ori:
1869 return generateImmOutOfRangeError(
1870 Operands, ErrorInfo, /*Lower=*/0,
1871 /*Upper=*/(1 << 12) - 1,
1872 "operand must be a symbol with modifier (e.g. %abs_lo12) or an "
1873 "integer in the range");
1874 case Match_InvalidUImm14:
1875 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1876 /*Upper=*/(1 << 14) - 1);
1877 case Match_InvalidUImm15:
1878 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/0,
1879 /*Upper=*/(1 << 15) - 1);
1880 case Match_InvalidSImm5:
1881 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 4),
1882 /*Upper=*/(1 << 4) - 1);
1883 case Match_InvalidSImm8:
1884 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 7),
1885 /*Upper=*/(1 << 7) - 1);
1886 case Match_InvalidSImm8lsl1:
1887 return generateImmOutOfRangeError(
1888 Operands, ErrorInfo, /*Lower=*/-(1 << 8), /*Upper=*/(1 << 8) - 2,
1889 "immediate must be a multiple of 2 in the range");
1890 case Match_InvalidSImm8lsl2:
1891 return generateImmOutOfRangeError(
1892 Operands, ErrorInfo, /*Lower=*/-(1 << 9), /*Upper=*/(1 << 9) - 4,
1893 "immediate must be a multiple of 4 in the range");
1894 case Match_InvalidSImm10:
1895 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 9),
1896 /*Upper=*/(1 << 9) - 1);
1897 case Match_InvalidSImm8lsl3:
1898 return generateImmOutOfRangeError(
1899 Operands, ErrorInfo, /*Lower=*/-(1 << 10), /*Upper=*/(1 << 10) - 8,
1900 "immediate must be a multiple of 8 in the range");
1901 case Match_InvalidSImm9lsl3:
1902 return generateImmOutOfRangeError(
1903 Operands, ErrorInfo, /*Lower=*/-(1 << 11), /*Upper=*/(1 << 11) - 8,
1904 "immediate must be a multiple of 8 in the range");
1905 case Match_InvalidSImm10lsl2:
1906 return generateImmOutOfRangeError(
1907 Operands, ErrorInfo, /*Lower=*/-(1 << 11), /*Upper=*/(1 << 11) - 4,
1908 "immediate must be a multiple of 4 in the range");
1909 case Match_InvalidSImm11lsl1:
1910 return generateImmOutOfRangeError(
1911 Operands, ErrorInfo, /*Lower=*/-(1 << 11), /*Upper=*/(1 << 11) - 2,
1912 "immediate must be a multiple of 2 in the range");
1913 case Match_InvalidSImm12:
1914 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 11),
1915 /*Upper=*/(1 << 11) - 1);
1916 case Match_InvalidSImm12addlike:
1917 return generateImmOutOfRangeError(
1918 Operands, ErrorInfo, /*Lower=*/-(1 << 11),
1919 /*Upper=*/(1 << 11) - 1,
1920 "operand must be a symbol with modifier (e.g. %pc_lo12) or an integer "
1921 "in the range");
1922 case Match_InvalidSImm12lu52id:
1923 return generateImmOutOfRangeError(
1924 Operands, ErrorInfo, /*Lower=*/-(1 << 11),
1925 /*Upper=*/(1 << 11) - 1,
1926 "operand must be a symbol with modifier (e.g. %pc64_hi12) or an "
1927 "integer in the range");
1928 case Match_InvalidSImm13:
1929 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 12),
1930 /*Upper=*/(1 << 12) - 1);
1931 case Match_InvalidSImm14lsl2:
1932 return generateImmOutOfRangeError(
1933 Operands, ErrorInfo, /*Lower=*/-(1 << 15), /*Upper=*/(1 << 15) - 4,
1934 "immediate must be a multiple of 4 in the range");
1935 case Match_InvalidSImm16:
1936 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 15),
1937 /*Upper=*/(1 << 15) - 1);
1938 case Match_InvalidSImm16lsl2:
1939 return generateImmOutOfRangeError(
1940 Operands, ErrorInfo, /*Lower=*/-(1 << 17), /*Upper=*/(1 << 17) - 4,
1941 "operand must be a symbol with modifier (e.g. %b16) or an integer "
1942 "in the range");
1943 case Match_InvalidSImm20:
1944 return generateImmOutOfRangeError(Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1945 /*Upper=*/(1 << 19) - 1);
1946 case Match_InvalidSImm20lu12iw:
1947 return generateImmOutOfRangeError(
1948 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1949 /*Upper=*/(1 << 19) - 1,
1950 "operand must be a symbol with modifier (e.g. %abs_hi20) or an integer "
1951 "in the range");
1952 case Match_InvalidSImm20lu32id:
1953 return generateImmOutOfRangeError(
1954 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1955 /*Upper=*/(1 << 19) - 1,
1956 "operand must be a symbol with modifier (e.g. %abs64_lo20) or an "
1957 "integer in the range");
1958 case Match_InvalidSImm20pcalau12i:
1959 return generateImmOutOfRangeError(
1960 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1961 /*Upper=*/(1 << 19) - 1,
1962 "operand must be a symbol with modifier (e.g. %pc_hi20) or an integer "
1963 "in the range");
1964 case Match_InvalidSImm20pcaddu12i:
1965 return generateImmOutOfRangeError(
1966 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1967 /*Upper=*/(1 << 19) - 1,
1968 "operand must be a symbol with modifier (e.g. %call30 or %pcadd_hi20) "
1969 "or an integer in the range");
1970 case Match_InvalidSImm20pcaddu18i:
1971 return generateImmOutOfRangeError(
1972 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1973 /*Upper=*/(1 << 19) - 1,
1974 "operand must be a symbol with modifier (e.g. %call36) or an integer "
1975 "in the range");
1976 case Match_InvalidSImm20pcaddi:
1977 return generateImmOutOfRangeError(
1978 Operands, ErrorInfo, /*Lower=*/-(1 << 19),
1979 /*Upper=*/(1 << 19) - 1,
1980 "operand must be a symbol with modifier (e.g. %pcrel_20) or an integer "
1981 "in the range");
1982 case Match_InvalidSImm21lsl2:
1983 return generateImmOutOfRangeError(
1984 Operands, ErrorInfo, /*Lower=*/-(1 << 22), /*Upper=*/(1 << 22) - 4,
1985 "operand must be a symbol with modifier (e.g. %b21) or an integer "
1986 "in the range");
1987 case Match_InvalidSImm26Operand:
1988 return generateImmOutOfRangeError(
1989 Operands, ErrorInfo, /*Lower=*/-(1 << 27), /*Upper=*/(1 << 27) - 4,
1990 "operand must be a bare symbol name or an immediate must be a multiple "
1991 "of 4 in the range");
1992 case Match_InvalidImm32: {
1993 SMLoc ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
1994 return Error(ErrorLoc, "operand must be a 32 bit immediate");
1995 }
1996 case Match_InvalidImm64: {
1997 SMLoc ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
1998 return Error(ErrorLoc, "operand must be a 64 bit immediate");
1999 }
2000 case Match_InvalidBareSymbol: {
2001 SMLoc ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
2002 return Error(ErrorLoc, "operand must be a bare symbol name");
2003 }
2004 case Match_InvalidTPRelAddSymbol: {
2005 SMLoc ErrorLoc = ((LoongArchOperand &)*Operands[ErrorInfo]).getStartLoc();
2006 return Error(ErrorLoc, "operand must be a symbol with %le_add_r modifier");
2007 }
2008 }
2009 llvm_unreachable("Unknown match type detected!");
2010}
2011
2012bool LoongArchAsmParser::parseDirectiveOption() {
2013 MCAsmParser &Parser = getParser();
2014 // Get the option token.
2015 AsmToken Tok = Parser.getTok();
2016
2017 // At the moment only identifiers are supported.
2018 if (parseToken(AsmToken::Identifier, "expected identifier"))
2019 return true;
2020
2021 StringRef Option = Tok.getIdentifier();
2022
2023 if (Option == "push") {
2024 if (Parser.parseEOL())
2025 return true;
2026
2027 getTargetStreamer().emitDirectiveOptionPush();
2028 pushFeatureBits();
2029 return false;
2030 }
2031
2032 if (Option == "pop") {
2033 SMLoc StartLoc = Parser.getTok().getLoc();
2034 if (Parser.parseEOL())
2035 return true;
2036
2037 getTargetStreamer().emitDirectiveOptionPop();
2038 if (popFeatureBits())
2039 return Error(StartLoc, ".option pop with no .option push");
2040
2041 return false;
2042 }
2043
2044 if (Option == "relax") {
2045 if (Parser.parseEOL())
2046 return true;
2047
2048 getTargetStreamer().emitDirectiveOptionRelax();
2049 setFeatureBits(LoongArch::FeatureRelax, "relax");
2050 return false;
2051 }
2052
2053 if (Option == "norelax") {
2054 if (Parser.parseEOL())
2055 return true;
2056
2057 getTargetStreamer().emitDirectiveOptionNoRelax();
2058 clearFeatureBits(LoongArch::FeatureRelax, "relax");
2059 return false;
2060 }
2061
2062 // Unknown option.
2063 Warning(Parser.getTok().getLoc(),
2064 "unknown option, expected 'push', 'pop', 'relax' or 'norelax'");
2065 Parser.eatToEndOfStatement();
2066 return false;
2067}
2068
2069bool LoongArchAsmParser::parseDirectiveDtpRelWord() {
2070 const MCExpr *Value;
2071 if (getParser().parseExpression(Value))
2072 return true;
2073 getTargetStreamer().emitDTPRel32Value(Value);
2074 return parseEOL();
2075}
2076
2077bool LoongArchAsmParser::parseDirectiveDtpRelDWord() {
2078 const MCExpr *Value;
2079 if (getParser().parseExpression(Value))
2080 return true;
2081 getTargetStreamer().emitDTPRel64Value(Value);
2082 return parseEOL();
2083}
2084
2085ParseStatus LoongArchAsmParser::parseDirective(AsmToken DirectiveID) {
2086 if (DirectiveID.getString() == ".option")
2087 return parseDirectiveOption();
2088
2089 if (DirectiveID.getString() == ".dtprelword")
2090 return parseDirectiveDtpRelWord();
2091
2092 if (DirectiveID.getString() == ".dtpreldword")
2093 return parseDirectiveDtpRelDWord();
2094
2095 return ParseStatus::NoMatch;
2096}
2097
2098extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
static MCRegister MatchRegisterName(StringRef Name)
static const char * getSubtargetFeatureName(uint64_t Val)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
bpf unwind BPF Unwind Lower
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool matchRegisterNameHelper(const MCSubtargetInfo &STI, MCRegister &Reg, StringRef Name)
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition Compiler.h:433
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
static bool matchRegisterNameHelper(MCRegister &RegNo, StringRef Name)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchAsmParser()
unsigned Opc
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
static bool isGPR(MCRegister Reg, const MCRegisterInfo &MRI)
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
#define P(N)
static bool isUImm2(const MachineOperand &MO)
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
const char * Msg
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)
static bool is64Bit(const char *name)
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
LLVM_ABI SMLoc getEndLoc() const
Definition AsmLexer.cpp:33
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Definition MCAsmMacro.h:92
Base class for user error types.
Definition Error.h:354
static const char * getRegisterName(MCRegister Reg)
static const LoongArchMCExpr * create(const MCExpr *Expr, uint16_t S, MCContext &Ctx, bool Hint=false)
void printExpr(raw_ostream &, const MCExpr &) const
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual void addAliasForDirective(StringRef Directive, StringRef Alias)=0
LLVM_ABI MCSymbol * createNamedTempSymbol()
Create a temporary symbol with a unique name whose name cannot be omitted in the symbol table.
LLVM_ABI bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const
Try to evaluate the expression to a relocatable value, i.e.
Definition MCExpr.cpp:450
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
void setLoc(SMLoc loc)
Definition MCInst.h:206
unsigned getOpcode() const
Definition MCInst.h:201
void addOperand(const MCOperand Op)
Definition MCInst.h:214
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:209
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:89
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
int64_t getImm() const
Definition MCInst.h:83
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
const MCExpr * getExpr() const
Definition MCInst.h:117
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCTargetStreamer * getTargetStreamer()
Definition MCStreamer.h:335
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
const FeatureBitset & ToggleFeature(uint64_t FB)
Toggle a feature and return the re-computed feature bits.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Target specific streamer interface.
Definition MCStreamer.h:94
MCStreamer & getStreamer()
Definition MCStreamer.h:102
uint32_t getSpecifier() const
Definition MCValue.h:46
Ternary parse status returned by various parse* methods.
static constexpr StatusTy Failure
static constexpr StatusTy Success
static constexpr StatusTy NoMatch
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
constexpr const char * getPointer() const
Definition SMLoc.h:33
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool isAMCAS(uint64_t TSFlags)
static bool isSubjectToAMORdConstraint(uint64_t TSFlags)
InstSeq generateInstSeq(int64_t Val)
uint16_t parseSpecifier(StringRef name)
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
This is an optimization pass for GlobalISel generic memory operations.
Target & getTheLoongArch64Target()
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
static bool isMem(const MachineInstr &MI, unsigned Op)
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
Target & getTheLoongArch32Target()
DWARFExpression::Operation Op
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...