LLVM 24.0.0git
CSKYAsmParser.cpp
Go to the documentation of this file.
1//===---- CSKYAsmParser.cpp - Parse CSKY 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
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/Statistic.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
30#include "llvm/MC/MCStreamer.h"
35#include "llvm/Support/Debug.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "csky-asm-parser"
41
42// Include the auto-generated portion of the compress emitter.
43#define GEN_COMPRESS_INSTR
44#include "CSKYGenCompressInstEmitter.inc"
45
46STATISTIC(CSKYNumInstrsCompressed,
47 "Number of C-SKY Compressed instructions emitted");
48
49namespace {
50struct CSKYOperand;
51
52class CSKYAsmParser : public MCTargetAsmParser {
53 const CSKYMCOptions &CLOpts;
54
55 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
56 unsigned Kind) override;
57
58 bool generateImmOutOfRangeError(OperandVector &Operands, uint64_t ErrorInfo,
59 int64_t Lower, int64_t Upper,
60 const Twine &Msg);
61
62 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
63
64 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
65 OperandVector &Operands, MCStreamer &Out,
66 uint64_t &ErrorInfo,
67 bool MatchingInlineAsm) override;
68
69 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
70
71 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
72 SMLoc NameLoc, OperandVector &Operands) override;
73
74 ParseStatus parseDirective(AsmToken DirectiveID) override;
75
76 // Helper to actually emit an instruction to the MCStreamer. Also, when
77 // possible, compression of the instruction is performed.
78 void emitToStreamer(MCStreamer &S, const MCInst &Inst);
79
80 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
81 SMLoc &EndLoc) override;
82
83 bool processInstruction(MCInst &Inst, SMLoc IDLoc, OperandVector &Operands,
84 MCStreamer &Out);
85 bool processLRW(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
86 bool processJSRI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
87 bool processJMPI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out);
88
89 CSKYTargetStreamer &getTargetStreamer() {
90 assert(getParser().getStreamer().getTargetStreamer() &&
91 "do not have a target streamer");
92 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
93 return static_cast<CSKYTargetStreamer &>(TS);
94 }
95
96// Auto-generated instruction matching functions
97#define GET_ASSEMBLER_HEADER
98#include "CSKYGenAsmMatcher.inc"
99
101 ParseStatus parseRegister(OperandVector &Operands);
102 ParseStatus parseBaseRegImm(OperandVector &Operands);
103 ParseStatus parseCSKYSymbol(OperandVector &Operands);
104 ParseStatus parseConstpoolSymbol(OperandVector &Operands);
105 ParseStatus parseDataSymbol(OperandVector &Operands);
106 ParseStatus parsePSRFlag(OperandVector &Operands);
107 ParseStatus parseRegSeq(OperandVector &Operands);
108 ParseStatus parseRegList(OperandVector &Operands);
109
110 bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
111
112 bool parseDirectiveAttribute();
113
114public:
115 enum CSKYMatchResultTy {
116 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
117 Match_RequiresSameSrcAndDst,
118 Match_InvalidRegOutOfRange,
119#define GET_OPERAND_DIAGNOSTIC_TYPES
120#include "CSKYGenAsmMatcher.inc"
121#undef GET_OPERAND_DIAGNOSTIC_TYPES
122 };
123
124 CSKYAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
125 const MCInstrInfo &MII)
126 : MCTargetAsmParser(STI, MII),
127 CLOpts(CSKY_MC::getCLOpts(Parser.getContext().getTargetOptions())) {
128
130
131 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
132 if (CLOpts.add_build_attributes)
133 getTargetStreamer().emitTargetAttributes(STI, /*HardFloatABI=*/false);
134 }
135};
136
137/// Instances of this class represent a parsed machine instruction.
138struct CSKYOperand : public MCParsedAsmOperand {
139
140 enum KindTy {
141 Token,
142 Register,
143 Immediate,
144 RegisterSeq,
145 CPOP,
146 RegisterList
147 } Kind;
148
149 struct RegOp {
150 MCRegister RegNum;
151 };
152
153 struct ImmOp {
154 const MCExpr *Val;
155 };
156
157 struct ConstpoolOp {
158 const MCExpr *Val;
159 };
160
161 struct RegSeqOp {
162 MCRegister RegNumFrom;
163 MCRegister RegNumTo;
164 };
165
166 struct RegListOp {
167 MCRegister List1From;
168 MCRegister List1To;
169 MCRegister List2From;
170 MCRegister List2To;
171 MCRegister List3From;
172 MCRegister List3To;
173 MCRegister List4From;
174 MCRegister List4To;
175 };
176
177 SMLoc StartLoc, EndLoc;
178 union {
179 StringRef Tok;
180 RegOp Reg;
181 ImmOp Imm;
182 ConstpoolOp CPool;
183 RegSeqOp RegSeq;
184 RegListOp RegList;
185 };
186
187 CSKYOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
188
189public:
190 CSKYOperand(const CSKYOperand &o) : MCParsedAsmOperand() {
191 Kind = o.Kind;
192 StartLoc = o.StartLoc;
193 EndLoc = o.EndLoc;
194 switch (Kind) {
195 case Register:
196 Reg = o.Reg;
197 break;
198 case RegisterSeq:
199 RegSeq = o.RegSeq;
200 break;
201 case CPOP:
202 CPool = o.CPool;
203 break;
204 case Immediate:
205 Imm = o.Imm;
206 break;
207 case Token:
208 Tok = o.Tok;
209 break;
210 case RegisterList:
211 RegList = o.RegList;
212 break;
213 }
214 }
215
216 bool isToken() const override { return Kind == Token; }
217 bool isReg() const override { return Kind == Register; }
218 bool isImm() const override { return Kind == Immediate; }
219 bool isRegisterSeq() const { return Kind == RegisterSeq; }
220 bool isRegisterList() const { return Kind == RegisterList; }
221 bool isConstPoolOp() const { return Kind == CPOP; }
222
223 bool isMem() const override { return false; }
224
225 static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm) {
226 if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
227 Imm = CE->getValue();
228 return true;
229 }
230
231 return false;
232 }
233
234 template <unsigned num, unsigned shift = 0> bool isUImm() const {
235 if (!isImm())
236 return false;
237
238 int64_t Imm;
239 bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
240 return IsConstantImm && isShiftedUInt<num, shift>(Imm);
241 }
242
243 template <unsigned num> bool isOImm() const {
244 if (!isImm())
245 return false;
246
247 int64_t Imm;
248 bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
249 return IsConstantImm && isUInt<num>(Imm - 1);
250 }
251
252 template <unsigned num, unsigned shift = 0> bool isSImm() const {
253 if (!isImm())
254 return false;
255
256 int64_t Imm;
257 bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
258 return IsConstantImm && isShiftedInt<num, shift>(Imm);
259 }
260
261 bool isUImm1() const { return isUImm<1>(); }
262 bool isUImm2() const { return isUImm<2>(); }
263 bool isUImm3() const { return isUImm<3>(); }
264 bool isUImm4() const { return isUImm<4>(); }
265 bool isUImm5() const { return isUImm<5>(); }
266 bool isUImm6() const { return isUImm<6>(); }
267 bool isUImm7() const { return isUImm<7>(); }
268 bool isUImm8() const { return isUImm<8>(); }
269 bool isUImm12() const { return isUImm<12>(); }
270 bool isUImm16() const { return isUImm<16>(); }
271 bool isUImm20() const { return isUImm<20>(); }
272 bool isUImm24() const { return isUImm<24>(); }
273
274 bool isOImm3() const { return isOImm<3>(); }
275 bool isOImm4() const { return isOImm<4>(); }
276 bool isOImm5() const { return isOImm<5>(); }
277 bool isOImm6() const { return isOImm<6>(); }
278 bool isOImm8() const { return isOImm<8>(); }
279 bool isOImm12() const { return isOImm<12>(); }
280 bool isOImm16() const { return isOImm<16>(); }
281
282 bool isSImm8() const { return isSImm<8>(); }
283
284 bool isUImm5Shift1() { return isUImm<5, 1>(); }
285 bool isUImm5Shift2() { return isUImm<5, 2>(); }
286 bool isUImm7Shift1() { return isUImm<7, 1>(); }
287 bool isUImm7Shift2() { return isUImm<7, 2>(); }
288 bool isUImm7Shift3() { return isUImm<7, 3>(); }
289 bool isUImm8Shift2() { return isUImm<8, 2>(); }
290 bool isUImm8Shift3() { return isUImm<8, 3>(); }
291 bool isUImm8Shift8() { return isUImm<8, 8>(); }
292 bool isUImm8Shift16() { return isUImm<8, 16>(); }
293 bool isUImm8Shift24() { return isUImm<8, 24>(); }
294 bool isUImm12Shift1() { return isUImm<12, 1>(); }
295 bool isUImm12Shift2() { return isUImm<12, 2>(); }
296 bool isUImm16Shift8() { return isUImm<16, 8>(); }
297 bool isUImm16Shift16() { return isUImm<16, 16>(); }
298 bool isUImm24Shift8() { return isUImm<24, 8>(); }
299
300 bool isSImm16Shift1() { return isSImm<16, 1>(); }
301
302 bool isCSKYSymbol() const { return isImm(); }
303
304 bool isConstpool() const { return isConstPoolOp(); }
305 bool isDataSymbol() const { return isConstPoolOp(); }
306
307 bool isPSRFlag() const {
308 int64_t Imm;
309 // Must be of 'immediate' type and a constant.
310 if (!isImm() || !evaluateConstantImm(getImm(), Imm))
311 return false;
312
313 return isUInt<5>(Imm);
314 }
315
316 template <unsigned MIN, unsigned MAX> bool isRegSeqTemplate() const {
317 if (!isRegisterSeq())
318 return false;
319
320 std::pair<unsigned, unsigned> regSeq = getRegSeq();
321
322 return MIN <= regSeq.first && regSeq.first <= regSeq.second &&
323 regSeq.second <= MAX;
324 }
325
326 bool isRegSeq() const { return isRegSeqTemplate<CSKY::R0, CSKY::R31>(); }
327
328 bool isRegSeqV1() const {
329 return isRegSeqTemplate<CSKY::F0_32, CSKY::F15_32>();
330 }
331
332 bool isRegSeqV2() const {
333 return isRegSeqTemplate<CSKY::F0_32, CSKY::F31_32>();
334 }
335
336 static bool isLegalRegList(unsigned from, unsigned to) {
337 if (from == 0 && to == 0)
338 return true;
339
340 if (from == to) {
341 if (from != CSKY::R4 && from != CSKY::R15 && from != CSKY::R16 &&
342 from != CSKY::R28)
343 return false;
344
345 return true;
346 } else {
347 if (from != CSKY::R4 && from != CSKY::R16)
348 return false;
349
350 if (from == CSKY::R4 && to > CSKY::R4 && to < CSKY::R12)
351 return true;
352 else if (from == CSKY::R16 && to > CSKY::R16 && to < CSKY::R18)
353 return true;
354 else
355 return false;
356 }
357 }
358
359 bool isRegList() const {
360 if (!isRegisterList())
361 return false;
362
363 auto regList = getRegList();
364
365 if (!isLegalRegList(regList.List1From, regList.List1To))
366 return false;
367 if (!isLegalRegList(regList.List2From, regList.List2To))
368 return false;
369 if (!isLegalRegList(regList.List3From, regList.List3To))
370 return false;
371 if (!isLegalRegList(regList.List4From, regList.List4To))
372 return false;
373
374 return true;
375 }
376
377 bool isExtImm6() {
378 if (!isImm())
379 return false;
380
381 int64_t Imm;
382 bool IsConstantImm = evaluateConstantImm(getImm(), Imm);
383 if (!IsConstantImm)
384 return false;
385
386 int uimm4 = Imm & 0xf;
387
388 return isShiftedUInt<6, 0>(Imm) && uimm4 >= 0 && uimm4 <= 14;
389 }
390
391 /// Gets location of the first token of this operand.
392 SMLoc getStartLoc() const override { return StartLoc; }
393 /// Gets location of the last token of this operand.
394 SMLoc getEndLoc() const override { return EndLoc; }
395
396 MCRegister getReg() const override {
397 assert(Kind == Register && "Invalid type access!");
398 return Reg.RegNum;
399 }
400
401 std::pair<MCRegister, MCRegister> getRegSeq() const {
402 assert(Kind == RegisterSeq && "Invalid type access!");
403 return {RegSeq.RegNumFrom, RegSeq.RegNumTo};
404 }
405
406 RegListOp getRegList() const {
407 assert(Kind == RegisterList && "Invalid type access!");
408 return RegList;
409 }
410
411 const MCExpr *getImm() const {
412 assert(Kind == Immediate && "Invalid type access!");
413 return Imm.Val;
414 }
415
416 const MCExpr *getConstpoolOp() const {
417 assert(Kind == CPOP && "Invalid type access!");
418 return CPool.Val;
419 }
420
421 StringRef getToken() const {
422 assert(Kind == Token && "Invalid type access!");
423 return Tok;
424 }
425
426 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
427 const CSKYMCOptions &CLOpts = CSKY_MC::getCLOpts(MAI.getTargetOptions());
428 auto RegName = [&](MCRegister Reg) {
429 if (Reg)
431 else
432 return "noreg";
433 };
434
435 switch (Kind) {
436 case CPOP:
437 MAI.printExpr(OS, *getConstpoolOp());
438 break;
439 case Immediate:
440 MAI.printExpr(OS, *getImm());
441 break;
442 case KindTy::Register:
443 OS << "<register " << RegName(getReg()) << ">";
444 break;
445 case RegisterSeq:
446 OS << "<register-seq ";
447 OS << RegName(getRegSeq().first) << "-" << RegName(getRegSeq().second)
448 << ">";
449 break;
450 case RegisterList:
451 OS << "<register-list ";
452 OS << RegName(getRegList().List1From) << "-"
453 << RegName(getRegList().List1To) << ",";
454 OS << RegName(getRegList().List2From) << "-"
455 << RegName(getRegList().List2To) << ",";
456 OS << RegName(getRegList().List3From) << "-"
457 << RegName(getRegList().List3To) << ",";
458 OS << RegName(getRegList().List4From) << "-"
459 << RegName(getRegList().List4To);
460 break;
461 case Token:
462 OS << "'" << getToken() << "'";
463 break;
464 }
465 }
466
467 static std::unique_ptr<CSKYOperand> createToken(StringRef Str, SMLoc S) {
468 auto Op = std::make_unique<CSKYOperand>(Token);
469 Op->Tok = Str;
470 Op->StartLoc = S;
471 Op->EndLoc = S;
472 return Op;
473 }
474
475 static std::unique_ptr<CSKYOperand> createReg(MCRegister RegNo, SMLoc S,
476 SMLoc E) {
477 auto Op = std::make_unique<CSKYOperand>(Register);
478 Op->Reg.RegNum = RegNo;
479 Op->StartLoc = S;
480 Op->EndLoc = E;
481 return Op;
482 }
483
484 static std::unique_ptr<CSKYOperand>
485 createRegSeq(MCRegister RegNoFrom, MCRegister RegNoTo, SMLoc S) {
486 auto Op = std::make_unique<CSKYOperand>(RegisterSeq);
487 Op->RegSeq.RegNumFrom = RegNoFrom;
488 Op->RegSeq.RegNumTo = RegNoTo;
489 Op->StartLoc = S;
490 Op->EndLoc = S;
491 return Op;
492 }
493
494 static std::unique_ptr<CSKYOperand>
495 createRegList(const SmallVector<MCRegister, 4> &reglist, SMLoc S) {
496 auto Op = std::make_unique<CSKYOperand>(RegisterList);
497 Op->RegList.List1From = 0;
498 Op->RegList.List1To = 0;
499 Op->RegList.List2From = 0;
500 Op->RegList.List2To = 0;
501 Op->RegList.List3From = 0;
502 Op->RegList.List3To = 0;
503 Op->RegList.List4From = 0;
504 Op->RegList.List4To = 0;
505
506 for (unsigned i = 0; i < reglist.size(); i += 2) {
507 if (Op->RegList.List1From == 0) {
508 Op->RegList.List1From = reglist[i];
509 Op->RegList.List1To = reglist[i + 1];
510 } else if (Op->RegList.List2From == 0) {
511 Op->RegList.List2From = reglist[i];
512 Op->RegList.List2To = reglist[i + 1];
513 } else if (Op->RegList.List3From == 0) {
514 Op->RegList.List3From = reglist[i];
515 Op->RegList.List3To = reglist[i + 1];
516 } else if (Op->RegList.List4From == 0) {
517 Op->RegList.List4From = reglist[i];
518 Op->RegList.List4To = reglist[i + 1];
519 } else {
520 assert(0);
521 }
522 }
523
524 Op->StartLoc = S;
525 Op->EndLoc = S;
526 return Op;
527 }
528
529 static std::unique_ptr<CSKYOperand> createImm(const MCExpr *Val, SMLoc S,
530 SMLoc E) {
531 auto Op = std::make_unique<CSKYOperand>(Immediate);
532 Op->Imm.Val = Val;
533 Op->StartLoc = S;
534 Op->EndLoc = E;
535 return Op;
536 }
537
538 static std::unique_ptr<CSKYOperand> createConstpoolOp(const MCExpr *Val,
539 SMLoc S, SMLoc E) {
540 auto Op = std::make_unique<CSKYOperand>(CPOP);
541 Op->CPool.Val = Val;
542 Op->StartLoc = S;
543 Op->EndLoc = E;
544 return Op;
545 }
546
547 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
548 assert(Expr && "Expr shouldn't be null!");
549 if (auto *CE = dyn_cast<MCConstantExpr>(Expr))
550 Inst.addOperand(MCOperand::createImm(CE->getValue()));
551 else
553 }
554
555 // Used by the TableGen Code.
556 void addRegOperands(MCInst &Inst, unsigned N) const {
557 assert(N == 1 && "Invalid number of operands!");
559 }
560
561 void addImmOperands(MCInst &Inst, unsigned N) const {
562 assert(N == 1 && "Invalid number of operands!");
563 addExpr(Inst, getImm());
564 }
565
566 void addConstpoolOperands(MCInst &Inst, unsigned N) const {
567 assert(N == 1 && "Invalid number of operands!");
568 Inst.addOperand(MCOperand::createExpr(getConstpoolOp()));
569 }
570
571 void addRegSeqOperands(MCInst &Inst, unsigned N) const {
572 assert(N == 2 && "Invalid number of operands!");
573 auto regSeq = getRegSeq();
574
575 Inst.addOperand(MCOperand::createReg(regSeq.first));
576 Inst.addOperand(MCOperand::createReg(regSeq.second));
577 }
578
579 static unsigned getListValue(unsigned ListFrom, unsigned ListTo) {
580 if (ListFrom == ListTo && ListFrom == CSKY::R15)
581 return (1 << 4);
582 else if (ListFrom == ListTo && ListFrom == CSKY::R28)
583 return (1 << 8);
584 else if (ListFrom == CSKY::R4)
585 return ListTo - ListFrom + 1;
586 else if (ListFrom == CSKY::R16)
587 return ((ListTo - ListFrom + 1) << 5);
588 else
589 return 0;
590 }
591
592 void addRegListOperands(MCInst &Inst, unsigned N) const {
593 assert(N == 1 && "Invalid number of operands!");
594 auto regList = getRegList();
595
596 unsigned V = 0;
597
598 unsigned T = getListValue(regList.List1From, regList.List1To);
599 if (T != 0)
600 V = V | T;
601
602 T = getListValue(regList.List2From, regList.List2To);
603 if (T != 0)
604 V = V | T;
605
606 T = getListValue(regList.List3From, regList.List3To);
607 if (T != 0)
608 V = V | T;
609
610 T = getListValue(regList.List4From, regList.List4To);
611 if (T != 0)
612 V = V | T;
613
615 }
616
617 bool isValidForTie(const CSKYOperand &Other) const {
618 if (Kind != Other.Kind)
619 return false;
620
621 switch (Kind) {
622 default:
623 llvm_unreachable("Unexpected kind");
624 return false;
625 case Register:
626 return Reg.RegNum == Other.Reg.RegNum;
627 }
628 }
629};
630} // end anonymous namespace.
631
632#define GET_REGISTER_MATCHER
633#define GET_SUBTARGET_FEATURE_NAME
634#define GET_MATCHER_IMPLEMENTATION
635#define GET_MNEMONIC_SPELL_CHECKER
636#include "CSKYGenAsmMatcher.inc"
637
638static std::string CSKYMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,
639 unsigned VariantID = 0);
640
641bool CSKYAsmParser::generateImmOutOfRangeError(
643 const Twine &Msg = "immediate must be an integer in the range") {
644 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
645 return Error(ErrorLoc, Msg + " [" + Twine(Lower) + ", " + Twine(Upper) + "]");
646}
647
648bool CSKYAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
650 MCStreamer &Out,
651 uint64_t &ErrorInfo,
652 bool MatchingInlineAsm) {
653 MCInst Inst;
654 FeatureBitset MissingFeatures;
655
656 auto Result = MatchInstructionImpl(Operands, Inst, ErrorInfo, MissingFeatures,
657 MatchingInlineAsm);
658 switch (Result) {
659 default:
660 break;
661 case Match_Success:
662 return processInstruction(Inst, IDLoc, Operands, Out);
663 case Match_MissingFeature: {
664 assert(MissingFeatures.any() && "Unknown missing features!");
665 ListSeparator LS;
666 std::string Msg = "instruction requires the following: ";
667 for (unsigned Feature : MissingFeatures) {
668 Msg += LS;
669 Msg += getSubtargetFeatureName(Feature);
670 }
671 return Error(IDLoc, Msg);
672 }
673 case Match_MnemonicFail: {
674 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
675 std::string Suggestion =
676 CSKYMnemonicSpellCheck(((CSKYOperand &)*Operands[0]).getToken(), FBS);
677 return Error(IDLoc, "unrecognized instruction mnemonic" + Suggestion);
678 }
679 case Match_InvalidTiedOperand:
680 case Match_InvalidOperand: {
681 SMLoc ErrorLoc = IDLoc;
682 if (ErrorInfo != ~0U) {
683 if (ErrorInfo >= Operands.size())
684 return Error(ErrorLoc, "too few operands for instruction");
685
686 ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
687 if (ErrorLoc == SMLoc())
688 ErrorLoc = IDLoc;
689 }
690 return Error(ErrorLoc, "invalid operand for instruction");
691 }
692 }
693
694 // Handle the case when the error message is of specific type
695 // other than the generic Match_InvalidOperand, and the
696 // corresponding operand is missing.
697 if (Result > FIRST_TARGET_MATCH_RESULT_TY) {
698 SMLoc ErrorLoc = IDLoc;
699 if (ErrorInfo != ~0U && ErrorInfo >= Operands.size())
700 return Error(ErrorLoc, "too few operands for instruction");
701 }
702
703 switch (Result) {
704 default:
705 break;
706 case Match_InvalidSImm8:
707 return generateImmOutOfRangeError(Operands, ErrorInfo, -(1 << 7),
708 (1 << 7) - 1);
709 case Match_InvalidOImm3:
710 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 3));
711 case Match_InvalidOImm4:
712 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 4));
713 case Match_InvalidOImm5:
714 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 5));
715 case Match_InvalidOImm6:
716 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 6));
717 case Match_InvalidOImm8:
718 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 8));
719 case Match_InvalidOImm12:
720 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 12));
721 case Match_InvalidOImm16:
722 return generateImmOutOfRangeError(Operands, ErrorInfo, 1, (1 << 16));
723 case Match_InvalidUImm1:
724 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 1) - 1);
725 case Match_InvalidUImm2:
726 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 2) - 1);
727 case Match_InvalidUImm3:
728 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 3) - 1);
729 case Match_InvalidUImm4:
730 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 4) - 1);
731 case Match_InvalidUImm5:
732 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 5) - 1);
733 case Match_InvalidUImm6:
734 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 6) - 1);
735 case Match_InvalidUImm7:
736 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 7) - 1);
737 case Match_InvalidUImm8:
738 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 8) - 1);
739 case Match_InvalidUImm12:
740 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 12) - 1);
741 case Match_InvalidUImm16:
742 return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 16) - 1);
743 case Match_InvalidUImm5Shift1:
744 return generateImmOutOfRangeError(
745 Operands, ErrorInfo, 0, (1 << 5) - 2,
746 "immediate must be a multiple of 2 bytes in the range");
747 case Match_InvalidUImm12Shift1:
748 return generateImmOutOfRangeError(
749 Operands, ErrorInfo, 0, (1 << 12) - 2,
750 "immediate must be a multiple of 2 bytes in the range");
751 case Match_InvalidUImm5Shift2:
752 return generateImmOutOfRangeError(
753 Operands, ErrorInfo, 0, (1 << 5) - 4,
754 "immediate must be a multiple of 4 bytes in the range");
755 case Match_InvalidUImm7Shift1:
756 return generateImmOutOfRangeError(
757 Operands, ErrorInfo, 0, (1 << 7) - 2,
758 "immediate must be a multiple of 2 bytes in the range");
759 case Match_InvalidUImm7Shift2:
760 return generateImmOutOfRangeError(
761 Operands, ErrorInfo, 0, (1 << 7) - 4,
762 "immediate must be a multiple of 4 bytes in the range");
763 case Match_InvalidUImm8Shift2:
764 return generateImmOutOfRangeError(
765 Operands, ErrorInfo, 0, (1 << 8) - 4,
766 "immediate must be a multiple of 4 bytes in the range");
767 case Match_InvalidUImm8Shift3:
768 return generateImmOutOfRangeError(
769 Operands, ErrorInfo, 0, (1 << 8) - 8,
770 "immediate must be a multiple of 8 bytes in the range");
771 case Match_InvalidUImm8Shift8:
772 return generateImmOutOfRangeError(
773 Operands, ErrorInfo, 0, (1 << 8) - 256,
774 "immediate must be a multiple of 256 bytes in the range");
775 case Match_InvalidUImm12Shift2:
776 return generateImmOutOfRangeError(
777 Operands, ErrorInfo, 0, (1 << 12) - 4,
778 "immediate must be a multiple of 4 bytes in the range");
779 case Match_InvalidCSKYSymbol: {
780 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
781 return Error(ErrorLoc, "operand must be a symbol name");
782 }
783 case Match_InvalidConstpool: {
784 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
785 return Error(ErrorLoc, "operand must be a constpool symbol name");
786 }
787 case Match_InvalidPSRFlag: {
788 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
789 return Error(ErrorLoc, "psrset operand is not valid");
790 }
791 case Match_InvalidRegSeq: {
792 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
793 return Error(ErrorLoc, "Register sequence is not valid");
794 }
795 case Match_InvalidRegOutOfRange: {
796 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
797 return Error(ErrorLoc, "register is out of range");
798 }
799 case Match_RequiresSameSrcAndDst: {
800 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
801 return Error(ErrorLoc, "src and dst operand must be same");
802 }
803 case Match_InvalidRegList: {
804 SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
805 return Error(ErrorLoc, "invalid register list");
806 }
807 }
808 LLVM_DEBUG(dbgs() << "Result = " << Result);
809 llvm_unreachable("Unknown match type detected!");
810}
811
812bool CSKYAsmParser::processLRW(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out) {
813 Inst.setLoc(IDLoc);
814
815 unsigned Opcode;
816 MCOperand Op;
817 if (Inst.getOpcode() == CSKY::PseudoLRW16)
818 Opcode = CSKY::LRW16;
819 else
820 Opcode = CSKY::LRW32;
821
822 if (Inst.getOperand(1).isImm()) {
823 if (isUInt<8>(Inst.getOperand(1).getImm()) &&
824 Inst.getOperand(0).getReg() <= CSKY::R7) {
825 Opcode = CSKY::MOVI16;
826 } else if (getSTI().hasFeature(CSKY::HasE2) &&
827 isUInt<16>(Inst.getOperand(1).getImm())) {
828 Opcode = CSKY::MOVI32;
829 } else {
830 auto *Expr = getTargetStreamer().addConstantPoolEntry(
832 Inst.getLoc());
833 Inst.erase(std::prev(Inst.end()));
835 }
836 } else {
837 const MCExpr *AdjustExpr = nullptr;
838 if (const auto *CSKYExpr =
840 if (CSKYExpr->getSpecifier() == CSKY::S_TLSGD ||
841 CSKYExpr->getSpecifier() == CSKY::S_TLSIE ||
842 CSKYExpr->getSpecifier() == CSKY::S_TLSLDM) {
843 MCSymbol *Dot = getContext().createNamedTempSymbol();
844 Out.emitLabel(Dot);
845 AdjustExpr = MCSymbolRefExpr::create(Dot, getContext());
846 }
847 }
848 auto *Expr = getTargetStreamer().addConstantPoolEntry(
849 Inst.getOperand(1).getExpr(), Inst.getLoc(), AdjustExpr);
850 Inst.erase(std::prev(Inst.end()));
852 }
853
854 Inst.setOpcode(Opcode);
855
856 Out.emitInstruction(Inst, getSTI());
857 return false;
858}
859
860bool CSKYAsmParser::processJSRI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out) {
861 Inst.setLoc(IDLoc);
862
863 if (Inst.getOperand(0).isImm()) {
864 const MCExpr *Expr = getTargetStreamer().addConstantPoolEntry(
866 Inst.getLoc());
867 Inst.setOpcode(CSKY::JSRI32);
868 Inst.erase(std::prev(Inst.end()));
870 } else {
871 const MCExpr *Expr = getTargetStreamer().addConstantPoolEntry(
872 Inst.getOperand(0).getExpr(), Inst.getLoc());
873 Inst.setOpcode(CSKY::JBSR32);
875 }
876
877 Out.emitInstruction(Inst, getSTI());
878 return false;
879}
880
881bool CSKYAsmParser::processJMPI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out) {
882 Inst.setLoc(IDLoc);
883
884 if (Inst.getOperand(0).isImm()) {
885 const MCExpr *Expr = getTargetStreamer().addConstantPoolEntry(
887 Inst.getLoc());
888 Inst.setOpcode(CSKY::JMPI32);
889 Inst.erase(std::prev(Inst.end()));
891 } else {
892 const MCExpr *Expr = getTargetStreamer().addConstantPoolEntry(
893 Inst.getOperand(0).getExpr(), Inst.getLoc());
894 Inst.setOpcode(CSKY::JBR32);
896 }
897
898 Out.emitInstruction(Inst, getSTI());
899 return false;
900}
901
902bool CSKYAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
904 MCStreamer &Out) {
905
906 switch (Inst.getOpcode()) {
907 default:
908 break;
909 case CSKY::LDQ32:
910 case CSKY::STQ32:
911 if (Inst.getOperand(1).getReg() != CSKY::R4 ||
912 Inst.getOperand(2).getReg() != CSKY::R7) {
913 return Error(IDLoc, "Register sequence is not valid. 'r4-r7' expected");
914 }
915 Inst.setOpcode(Inst.getOpcode() == CSKY::LDQ32 ? CSKY::LDM32 : CSKY::STM32);
916 break;
917 case CSKY::SEXT32:
918 case CSKY::ZEXT32:
919 if (Inst.getOperand(2).getImm() < Inst.getOperand(3).getImm())
920 return Error(IDLoc, "msb must be greater or equal to lsb");
921 break;
922 case CSKY::INS32:
923 if (Inst.getOperand(3).getImm() < Inst.getOperand(4).getImm())
924 return Error(IDLoc, "msb must be greater or equal to lsb");
925 break;
926 case CSKY::IDLY32:
927 if (Inst.getOperand(0).getImm() > 32 || Inst.getOperand(0).getImm() < 0)
928 return Error(IDLoc, "n must be in range [0,32]");
929 break;
930 case CSKY::ADDC32:
931 case CSKY::SUBC32:
932 case CSKY::ADDC16:
933 case CSKY::SUBC16:
934 Inst.erase(std::next(Inst.begin()));
935 Inst.erase(std::prev(Inst.end()));
936 Inst.insert(std::next(Inst.begin()), MCOperand::createReg(CSKY::C));
937 Inst.insert(Inst.end(), MCOperand::createReg(CSKY::C));
938 break;
939 case CSKY::CMPNEI32:
940 case CSKY::CMPNEI16:
941 case CSKY::CMPNE32:
942 case CSKY::CMPNE16:
943 case CSKY::CMPHSI32:
944 case CSKY::CMPHSI16:
945 case CSKY::CMPHS32:
946 case CSKY::CMPHS16:
947 case CSKY::CMPLTI32:
948 case CSKY::CMPLTI16:
949 case CSKY::CMPLT32:
950 case CSKY::CMPLT16:
951 case CSKY::BTSTI32:
952 Inst.erase(Inst.begin());
953 Inst.insert(Inst.begin(), MCOperand::createReg(CSKY::C));
954 break;
955 case CSKY::MVCV32:
956 Inst.erase(std::next(Inst.begin()));
957 Inst.insert(Inst.end(), MCOperand::createReg(CSKY::C));
958 break;
959 case CSKY::PseudoLRW16:
960 case CSKY::PseudoLRW32:
961 return processLRW(Inst, IDLoc, Out);
962 case CSKY::PseudoJSRI32:
963 return processJSRI(Inst, IDLoc, Out);
964 case CSKY::PseudoJMPI32:
965 return processJMPI(Inst, IDLoc, Out);
966 case CSKY::JBSR32:
967 case CSKY::JBR16:
968 case CSKY::JBT16:
969 case CSKY::JBF16:
970 case CSKY::JBR32:
971 case CSKY::JBT32:
972 case CSKY::JBF32:
973 unsigned Num = Inst.getNumOperands() - 1;
974 assert(Inst.getOperand(Num).isExpr());
975
976 const MCExpr *Expr = getTargetStreamer().addConstantPoolEntry(
977 Inst.getOperand(Num).getExpr(), Inst.getLoc());
978
980 break;
981 }
982
983 emitToStreamer(Out, Inst);
984 return false;
985}
986
987// Attempts to match Name as a register (either using the default name or
988// alternative ABI names), setting RegNo to the matching register. Upon
989// failure, returns true and sets RegNo to 0.
991 StringRef Name) {
992 Reg = MatchRegisterName(Name);
993
994 if (Reg == CSKY::NoRegister)
996
997 return Reg == CSKY::NoRegister;
998}
999
1000bool CSKYAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
1001 SMLoc &EndLoc) {
1002 const AsmToken &Tok = getParser().getTok();
1003 StartLoc = Tok.getLoc();
1004 EndLoc = Tok.getEndLoc();
1005 StringRef Name = getLexer().getTok().getIdentifier();
1006
1007 if (!matchRegisterNameHelper(getSTI(), Reg, Name)) {
1008 getParser().Lex(); // Eat identifier token.
1009 return false;
1010 }
1011
1012 return true;
1013}
1014
1015ParseStatus CSKYAsmParser::parseRegister(OperandVector &Operands) {
1016 SMLoc S = getLoc();
1017 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1018
1019 switch (getLexer().getKind()) {
1020 default:
1021 return ParseStatus::NoMatch;
1022 case AsmToken::Identifier: {
1023 StringRef Name = getLexer().getTok().getIdentifier();
1024 MCRegister Reg;
1025
1026 if (matchRegisterNameHelper(getSTI(), Reg, Name))
1027 return ParseStatus::NoMatch;
1028
1029 getLexer().Lex();
1030 Operands.push_back(CSKYOperand::createReg(Reg, S, E));
1031
1032 return ParseStatus::Success;
1033 }
1034 }
1035}
1036
1037ParseStatus CSKYAsmParser::parseBaseRegImm(OperandVector &Operands) {
1038 assert(getLexer().is(AsmToken::LParen));
1039
1040 Operands.push_back(CSKYOperand::createToken("(", getLoc()));
1041
1042 auto Tok = getParser().Lex(); // Eat '('
1043
1044 if (!parseRegister(Operands).isSuccess()) {
1045 getLexer().UnLex(Tok);
1046 Operands.pop_back();
1047 return ParseStatus::NoMatch;
1048 }
1049
1050 if (getLexer().is(AsmToken::RParen)) {
1051 Operands.push_back(CSKYOperand::createToken(")", getLoc()));
1052 getParser().Lex(); // Eat ')'
1053 return ParseStatus::Success;
1054 }
1055
1056 if (getLexer().isNot(AsmToken::Comma))
1057 return Error(getLoc(), "expected ','");
1058
1059 getParser().Lex(); // Eat ','
1060
1061 if (parseRegister(Operands).isSuccess()) {
1062 if (getLexer().isNot(AsmToken::LessLess))
1063 return Error(getLoc(), "expected '<<'");
1064
1065 Operands.push_back(CSKYOperand::createToken("<<", getLoc()));
1066
1067 getParser().Lex(); // Eat '<<'
1068
1069 if (!parseImmediate(Operands).isSuccess())
1070 return Error(getLoc(), "expected imm");
1071
1072 } else if (!parseImmediate(Operands).isSuccess()) {
1073 return Error(getLoc(), "expected imm");
1074 }
1075
1076 if (getLexer().isNot(AsmToken::RParen))
1077 return Error(getLoc(), "expected ')'");
1078
1079 Operands.push_back(CSKYOperand::createToken(")", getLoc()));
1080
1081 getParser().Lex(); // Eat ')'
1082
1083 return ParseStatus::Success;
1084}
1085
1086ParseStatus CSKYAsmParser::parseImmediate(OperandVector &Operands) {
1087 switch (getLexer().getKind()) {
1088 default:
1089 return ParseStatus::NoMatch;
1090 case AsmToken::LParen:
1091 case AsmToken::Minus:
1092 case AsmToken::Plus:
1093 case AsmToken::Integer:
1094 case AsmToken::String:
1095 break;
1096 }
1097
1098 const MCExpr *IdVal;
1099 SMLoc S = getLoc();
1100 if (getParser().parseExpression(IdVal))
1101 return Error(getLoc(), "unknown expression");
1102
1103 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1104 Operands.push_back(CSKYOperand::createImm(IdVal, S, E));
1105 return ParseStatus::Success;
1106}
1107
1108/// Looks at a token type and creates the relevant operand from this
1109/// information, adding to Operands. If operand was parsed, returns false, else
1110/// true.
1111bool CSKYAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1112 // Check if the current operand has a custom associated parser, if so, try to
1113 // custom parse the operand, or fallback to the general approach.
1114 ParseStatus Result =
1115 MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
1116 if (Result.isSuccess())
1117 return false;
1118 if (Result.isFailure())
1119 return true;
1120
1121 // Attempt to parse token as register
1122 auto Res = parseRegister(Operands);
1123 if (Res.isSuccess())
1124 return false;
1125 if (Res.isFailure())
1126 return true;
1127
1128 // Attempt to parse token as (register, imm)
1129 if (getLexer().is(AsmToken::LParen)) {
1130 Res = parseBaseRegImm(Operands);
1131 if (Res.isSuccess())
1132 return false;
1133 if (Res.isFailure())
1134 return true;
1135 }
1136
1137 Res = parseImmediate(Operands);
1138 if (Res.isSuccess())
1139 return false;
1140 if (Res.isFailure())
1141 return true;
1142
1143 // Finally we have exhausted all options and must declare defeat.
1144 Error(getLoc(), "unknown operand");
1145 return true;
1146}
1147
1148ParseStatus CSKYAsmParser::parseCSKYSymbol(OperandVector &Operands) {
1149 SMLoc S = getLoc();
1150 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1151 const MCExpr *Res;
1152
1153 if (getLexer().getKind() != AsmToken::Identifier)
1154 return ParseStatus::NoMatch;
1155
1156 StringRef Identifier;
1157 AsmToken Tok = getLexer().getTok();
1158
1159 if (getParser().parseIdentifier(Identifier))
1160 return Error(getLoc(), "unknown identifier");
1161
1163 if (Identifier.consume_back("@GOT"))
1164 Kind = CSKY::S_GOT;
1165 else if (Identifier.consume_back("@GOTOFF"))
1167 else if (Identifier.consume_back("@PLT"))
1168 Kind = CSKY::S_PLT;
1169 else if (Identifier.consume_back("@GOTPC"))
1171 else if (Identifier.consume_back("@TLSGD32"))
1173 else if (Identifier.consume_back("@GOTTPOFF"))
1175 else if (Identifier.consume_back("@TPOFF"))
1177 else if (Identifier.consume_back("@TLSLDM32"))
1179 else if (Identifier.consume_back("@TLSLDO32"))
1181
1182 MCSymbol *Sym = getContext().getInlineAsmLabel(Identifier);
1183
1184 if (!Sym)
1185 Sym = getContext().getOrCreateSymbol(Identifier);
1186
1187 if (Sym->isVariable()) {
1188 const MCExpr *V = Sym->getVariableValue();
1189 if (!isa<MCSymbolRefExpr>(V)) {
1190 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1191 return Error(getLoc(), "unknown symbol");
1192 }
1193 Res = V;
1194 } else
1195 Res = MCSymbolRefExpr::create(Sym, getContext());
1196
1197 MCBinaryExpr::Opcode Opcode;
1198 switch (getLexer().getKind()) {
1199 default:
1200 if (Kind != CSKY::S_None)
1201 Res = MCSpecifierExpr::create(Res, Kind, getContext());
1202
1203 Operands.push_back(CSKYOperand::createImm(Res, S, E));
1204 return ParseStatus::Success;
1205 case AsmToken::Plus:
1206 Opcode = MCBinaryExpr::Add;
1207 break;
1208 case AsmToken::Minus:
1209 Opcode = MCBinaryExpr::Sub;
1210 break;
1211 }
1212
1213 getLexer().Lex(); // eat + or -
1214
1215 const MCExpr *Expr;
1216 if (getParser().parseExpression(Expr))
1217 return Error(getLoc(), "unknown expression");
1218 Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
1219 Operands.push_back(CSKYOperand::createImm(Res, S, E));
1220 return ParseStatus::Success;
1221}
1222
1223ParseStatus CSKYAsmParser::parseDataSymbol(OperandVector &Operands) {
1224 SMLoc S = getLoc();
1225 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1226 const MCExpr *Res;
1227
1228 if (!parseOptionalToken(AsmToken::LBrac))
1229 return ParseStatus::NoMatch;
1230 if (getLexer().getKind() != AsmToken::Identifier) {
1231 const MCExpr *Expr;
1232 if (getParser().parseExpression(Expr))
1233 return Error(getLoc(), "unknown expression");
1234
1235 if (parseToken(AsmToken::RBrac, "expected ']'"))
1236 return ParseStatus::Failure;
1237
1238 Operands.push_back(CSKYOperand::createConstpoolOp(Expr, S, E));
1239 return ParseStatus::Success;
1240 }
1241
1242 AsmToken Tok = getLexer().getTok();
1243 StringRef Identifier;
1244
1245 if (getParser().parseIdentifier(Identifier))
1246 return Error(getLoc(), "unknown identifier " + Identifier);
1247
1249 if (Identifier.consume_back("@GOT"))
1251 else if (Identifier.consume_back("@PLT"))
1253
1254 MCSymbol *Sym = getContext().getInlineAsmLabel(Identifier);
1255
1256 if (!Sym)
1257 Sym = getContext().getOrCreateSymbol(Identifier);
1258
1259 if (Sym->isVariable()) {
1260 const MCExpr *V = Sym->getVariableValue();
1261 if (!isa<MCSymbolRefExpr>(V)) {
1262 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1263 return Error(getLoc(), "unknown symbol");
1264 }
1265 Res = V;
1266 } else {
1267 Res = MCSymbolRefExpr::create(Sym, getContext());
1268 }
1269
1270 MCBinaryExpr::Opcode Opcode;
1271 switch (getLexer().getKind()) {
1272 default:
1273 return Error(getLoc(), "unknown symbol");
1274 case AsmToken::RBrac:
1275
1276 getLexer().Lex(); // Eat ']'.
1277
1278 if (Kind != CSKY::S_None)
1279 Res = MCSpecifierExpr::create(Res, Kind, getContext());
1280
1281 Operands.push_back(CSKYOperand::createConstpoolOp(Res, S, E));
1282 return ParseStatus::Success;
1283 case AsmToken::Plus:
1284 Opcode = MCBinaryExpr::Add;
1285 break;
1286 case AsmToken::Minus:
1287 Opcode = MCBinaryExpr::Sub;
1288 break;
1289 }
1290
1291 getLexer().Lex(); // eat + or -
1292
1293 const MCExpr *Expr;
1294 if (getParser().parseExpression(Expr))
1295 return Error(getLoc(), "unknown expression");
1296 if (parseToken(AsmToken::RBrac, "expected ']'"))
1297 return ParseStatus::Failure;
1298
1299 Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
1300 Operands.push_back(CSKYOperand::createConstpoolOp(Res, S, E));
1301 return ParseStatus::Success;
1302}
1303
1304ParseStatus CSKYAsmParser::parseConstpoolSymbol(OperandVector &Operands) {
1305 SMLoc S = getLoc();
1306 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1307 const MCExpr *Res;
1308
1309 if (!parseOptionalToken(AsmToken::LBrac))
1310 return ParseStatus::NoMatch;
1311
1312 if (getLexer().getKind() != AsmToken::Identifier) {
1313 const MCExpr *Expr;
1314 if (getParser().parseExpression(Expr))
1315 return Error(getLoc(), "unknown expression");
1316 if (parseToken(AsmToken::RBrac))
1317 return ParseStatus::Failure;
1318
1319 Operands.push_back(CSKYOperand::createConstpoolOp(Expr, S, E));
1320 return ParseStatus::Success;
1321 }
1322
1323 AsmToken Tok = getLexer().getTok();
1324 StringRef Identifier;
1325
1326 if (getParser().parseIdentifier(Identifier))
1327 return Error(getLoc(), "unknown identifier");
1328
1329 MCSymbol *Sym = getContext().getInlineAsmLabel(Identifier);
1330
1331 if (!Sym)
1332 Sym = getContext().getOrCreateSymbol(Identifier);
1333
1334 if (Sym->isVariable()) {
1335 const MCExpr *V = Sym->getVariableValue();
1336 if (!isa<MCSymbolRefExpr>(V)) {
1337 getLexer().UnLex(Tok); // Put back if it's not a bare symbol.
1338 return Error(getLoc(), "unknown symbol");
1339 }
1340 Res = V;
1341 } else {
1342 Res = MCSymbolRefExpr::create(Sym, getContext());
1343 }
1344
1345 MCBinaryExpr::Opcode Opcode;
1346 switch (getLexer().getKind()) {
1347 default:
1348 return Error(getLoc(), "unknown symbol");
1349 case AsmToken::RBrac:
1350
1351 getLexer().Lex(); // Eat ']'.
1352
1353 Operands.push_back(CSKYOperand::createConstpoolOp(Res, S, E));
1354 return ParseStatus::Success;
1355 case AsmToken::Plus:
1356 Opcode = MCBinaryExpr::Add;
1357 break;
1358 case AsmToken::Minus:
1359 Opcode = MCBinaryExpr::Sub;
1360 break;
1361 }
1362
1363 getLexer().Lex(); // eat + or -
1364
1365 const MCExpr *Expr;
1366 if (getParser().parseExpression(Expr))
1367 return Error(getLoc(), "unknown expression");
1368 if (parseToken(AsmToken::RBrac, "expected ']'"))
1369 return ParseStatus::Failure;
1370
1371 Res = MCBinaryExpr::create(Opcode, Res, Expr, getContext());
1372 Operands.push_back(CSKYOperand::createConstpoolOp(Res, S, E));
1373 return ParseStatus::Success;
1374}
1375
1376ParseStatus CSKYAsmParser::parsePSRFlag(OperandVector &Operands) {
1377 SMLoc S = getLoc();
1378 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1379
1380 unsigned Flag = 0;
1381
1382 while (getLexer().isNot(AsmToken::EndOfStatement)) {
1383 StringRef Identifier;
1384 if (getParser().parseIdentifier(Identifier))
1385 return Error(getLoc(), "unknown identifier " + Identifier);
1386
1387 if (Identifier == "sie")
1388 Flag = (1 << 4) | Flag;
1389 else if (Identifier == "ee")
1390 Flag = (1 << 3) | Flag;
1391 else if (Identifier == "ie")
1392 Flag = (1 << 2) | Flag;
1393 else if (Identifier == "fe")
1394 Flag = (1 << 1) | Flag;
1395 else if (Identifier == "af")
1396 Flag = (1 << 0) | Flag;
1397 else
1398 return Error(getLoc(), "expected " + Identifier);
1399
1400 if (getLexer().is(AsmToken::EndOfStatement))
1401 break;
1402
1403 if (parseToken(AsmToken::Comma, "expected ','"))
1404 return ParseStatus::Failure;
1405 }
1406
1407 Operands.push_back(
1408 CSKYOperand::createImm(MCConstantExpr::create(Flag, getContext()), S, E));
1409 return ParseStatus::Success;
1410}
1411
1412ParseStatus CSKYAsmParser::parseRegSeq(OperandVector &Operands) {
1413 SMLoc S = getLoc();
1414
1415 if (!parseRegister(Operands).isSuccess())
1416 return ParseStatus::NoMatch;
1417
1418 auto Ry = Operands.back()->getReg();
1419 Operands.pop_back();
1420
1421 if (parseToken(AsmToken::Minus, "expected '-'"))
1422 return ParseStatus::Failure;
1423 if (!parseRegister(Operands).isSuccess())
1424 return Error(getLoc(), "invalid register");
1425
1426 auto Rz = Operands.back()->getReg();
1427 Operands.pop_back();
1428
1429 Operands.push_back(CSKYOperand::createRegSeq(Ry, Rz, S));
1430 return ParseStatus::Success;
1431}
1432
1433ParseStatus CSKYAsmParser::parseRegList(OperandVector &Operands) {
1434 SMLoc S = getLoc();
1436 while (true) {
1437
1438 if (!parseRegister(Operands).isSuccess())
1439 return Error(getLoc(), "invalid register");
1440
1441 auto Ry = Operands.back()->getReg();
1442 Operands.pop_back();
1443
1444 if (parseOptionalToken(AsmToken::Minus)) {
1445 if (!parseRegister(Operands).isSuccess())
1446 return Error(getLoc(), "invalid register");
1447
1448 auto Rz = Operands.back()->getReg();
1449 Operands.pop_back();
1450
1451 reglist.push_back(Ry);
1452 reglist.push_back(Rz);
1453
1454 if (getLexer().is(AsmToken::EndOfStatement))
1455 break;
1456 (void)parseOptionalToken(AsmToken::Comma);
1457 } else if (parseOptionalToken(AsmToken::Comma)) {
1458 reglist.push_back(Ry);
1459 reglist.push_back(Ry);
1460 } else if (getLexer().is(AsmToken::EndOfStatement)) {
1461 reglist.push_back(Ry);
1462 reglist.push_back(Ry);
1463 break;
1464 } else {
1465 return Error(getLoc(), "invalid register list");
1466 }
1467 }
1468
1469 Operands.push_back(CSKYOperand::createRegList(reglist, S));
1470 return ParseStatus::Success;
1471}
1472
1473bool CSKYAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
1474 SMLoc NameLoc, OperandVector &Operands) {
1475 // First operand is token for instruction.
1476 Operands.push_back(CSKYOperand::createToken(Name, NameLoc));
1477
1478 // If there are no more operands, then finish.
1479 if (getLexer().is(AsmToken::EndOfStatement))
1480 return false;
1481
1482 // Parse first operand.
1483 if (parseOperand(Operands, Name))
1484 return true;
1485
1486 // Parse until end of statement, consuming commas between operands.
1487 while (parseOptionalToken(AsmToken::Comma))
1488 if (parseOperand(Operands, Name))
1489 return true;
1490
1491 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1492 SMLoc Loc = getLexer().getLoc();
1493 getParser().eatToEndOfStatement();
1494 return Error(Loc, "unexpected token");
1495 }
1496
1497 getParser().Lex(); // Consume the EndOfStatement.
1498 return false;
1499}
1500
1501ParseStatus CSKYAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
1502 SMLoc &EndLoc) {
1503 const AsmToken &Tok = getParser().getTok();
1504 StartLoc = Tok.getLoc();
1505 EndLoc = Tok.getEndLoc();
1506
1507 StringRef Name = getLexer().getTok().getIdentifier();
1508
1509 if (matchRegisterNameHelper(getSTI(), Reg, Name))
1510 return ParseStatus::NoMatch;
1511
1512 getParser().Lex(); // Eat identifier token.
1513 return ParseStatus::Success;
1514}
1515
1516ParseStatus CSKYAsmParser::parseDirective(AsmToken DirectiveID) {
1517 StringRef IDVal = DirectiveID.getString();
1518
1519 if (IDVal == ".csky_attribute")
1520 return parseDirectiveAttribute();
1521
1522 return ParseStatus::NoMatch;
1523}
1524
1525/// parseDirectiveAttribute
1526/// ::= .attribute expression ',' ( expression | "string" )
1527bool CSKYAsmParser::parseDirectiveAttribute() {
1528 MCAsmParser &Parser = getParser();
1529 int64_t Tag;
1530 SMLoc TagLoc;
1531 TagLoc = Parser.getTok().getLoc();
1532 if (Parser.getTok().is(AsmToken::Identifier)) {
1533 StringRef Name = Parser.getTok().getIdentifier();
1534 std::optional<unsigned> Ret =
1536 if (!Ret)
1537 return Error(TagLoc, "attribute name not recognised: " + Name);
1538 Tag = *Ret;
1539 Parser.Lex();
1540 } else {
1541 const MCExpr *AttrExpr;
1542
1543 TagLoc = Parser.getTok().getLoc();
1544 if (Parser.parseExpression(AttrExpr))
1545 return true;
1546
1547 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(AttrExpr);
1548 if (!CE)
1549 return Error(TagLoc, "expected numeric constant");
1550
1551 Tag = CE->getValue();
1552 }
1553
1554 if (Parser.parseComma())
1555 return true;
1556
1557 StringRef StringValue;
1558 int64_t IntegerValue = 0;
1559 bool IsIntegerValue = ((Tag != CSKYAttrs::CSKY_ARCH_NAME) &&
1562
1563 SMLoc ValueExprLoc = Parser.getTok().getLoc();
1564 if (IsIntegerValue) {
1565 const MCExpr *ValueExpr;
1566 if (Parser.parseExpression(ValueExpr))
1567 return true;
1568
1569 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ValueExpr);
1570 if (!CE)
1571 return Error(ValueExprLoc, "expected numeric constant");
1572 IntegerValue = CE->getValue();
1573 } else {
1574 if (Parser.getTok().isNot(AsmToken::String))
1575 return Error(Parser.getTok().getLoc(), "expected string constant");
1576
1577 StringValue = Parser.getTok().getStringContents();
1578 Parser.Lex();
1579 }
1580
1581 if (Parser.parseEOL())
1582 return true;
1583
1584 if (IsIntegerValue)
1585 getTargetStreamer().emitAttribute(Tag, IntegerValue);
1587 getTargetStreamer().emitTextAttribute(Tag, StringValue);
1588 else {
1590 ? CSKY::parseArch(StringValue)
1591 : CSKY::parseCPUArch(StringValue);
1592 if (ID == CSKY::ArchKind::INVALID)
1593 return Error(ValueExprLoc, (Tag == CSKYAttrs::CSKY_ARCH_NAME)
1594 ? "unknown arch name"
1595 : "unknown cpu name");
1596
1597 getTargetStreamer().emitTextAttribute(Tag, StringValue);
1598 }
1599
1600 return false;
1601}
1602
1603unsigned CSKYAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1604 unsigned Kind) {
1605 CSKYOperand &Op = static_cast<CSKYOperand &>(AsmOp);
1606
1607 if (!Op.isReg())
1608 return Match_InvalidOperand;
1609
1610 MCRegister Reg = Op.getReg();
1611
1612 // FPR32 registers are coerced to FPR64/sFPR64 by the generated matcher, so
1613 // this only sees fr16-fr31 used as an sFPR64 operand.
1614 if (Kind == MCK_sFPR64 &&
1615 getCSKYMCRegisterClass(CSKY::FPR32RegClassID).contains(Reg))
1616 return Match_InvalidRegOutOfRange;
1617
1618 return Match_InvalidOperand;
1619}
1620
1621void CSKYAsmParser::emitToStreamer(MCStreamer &S, const MCInst &Inst) {
1622 MCInst CInst;
1623 bool Res = false;
1624 if (CLOpts.asm_compressed_inst)
1625 Res = compressInst(CInst, Inst, getSTI());
1626 if (Res)
1627 ++CSKYNumInstrsCompressed;
1628 S.emitInstruction((Res ? CInst : Inst), getSTI());
1629}
1630
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 bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
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)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmParser()
static std::string CSKYMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS, unsigned VariantID=0)
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
Register Reg
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
static bool isUImm2(const MachineOperand &MO)
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
bool isNot(TokenKind K) const
Definition MCAsmMacro.h:76
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
StringRef getStringContents() const
Get the contents of a string token (without quotes).
Definition MCAsmMacro.h:83
bool is(TokenKind K) const
Definition MCAsmMacro.h:75
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
static const char * getRegisterName(const CSKYMCOptions &CLOpts, MCRegister Reg)
Base class for user error types.
Definition Error.h:354
Container class for subtarget features.
void printExpr(raw_ostream &, const MCExpr &) const
const MCTargetOptions & getTargetOptions() const
Definition MCAsmInfo.h:452
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:201
@ Sub
Subtraction.
Definition MCExpr.h:323
@ Add
Addition.
Definition MCExpr.h:301
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
void erase(iterator I)
Definition MCInst.h:224
unsigned getNumOperands() const
Definition MCInst.h:212
SMLoc getLoc() const
Definition MCInst.h:208
void setLoc(SMLoc loc)
Definition MCInst.h:207
unsigned getOpcode() const
Definition MCInst.h:202
iterator insert(iterator I, const MCOperand &Op)
Definition MCInst.h:232
void addOperand(const MCOperand Op)
Definition MCInst.h:215
iterator begin()
Definition MCInst.h:227
iterator end()
Definition MCInst.h:229
void setOpcode(unsigned Op)
Definition MCInst.h:201
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
bool isImm() const
Definition MCInst.h:66
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
MCTargetAsmParser - Generic interface to target specific assembly parsers.
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)
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.
const AArch64MCOptions & getCLOpts(const MCTargetOptions &Options)
LLVM_ABI const TagNameMap & getCSKYAttributeTags()
const CSKYMCOptions & getCLOpts(const MCTargetOptions &Options)
uint8_t Specifier
LLVM_ABI ArchKind parseCPUArch(StringRef CPU)
LLVM_ABI ArchKind parseArch(StringRef Arch)
LLVM_ABI std::optional< unsigned > attrTypeFromString(StringRef tag, TagNameMap tagNameMap)
Flag
These should be considered private to the implementation of the MCInstrDesc class.
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
LLVM_ABI TargetOptions getTargetOptions(const cl::LibraryOptionsContext &LibraryOptions, const llvm::Triple &TheTriple)
Common utility function tightly tied to the options listed here.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
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
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
Target & getTheCSKYTarget()
@ Other
Any other memory.
Definition ModRef.h:68
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 bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:199
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...