1 //===- MSP430AsmParser.cpp - Parse MSP430 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 
9 #include "MSP430.h"
10 #include "MSP430RegisterInfo.h"
11 #include "MCTargetDesc/MSP430MCTargetDesc.h"
12 #include "TargetInfo/MSP430TargetInfo.h"
13 
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstBuilder.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/MC/TargetRegistry.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/MathExtras.h"
30 
31 #define DEBUG_TYPE "msp430-asm-parser"
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 /// Parses MSP430 assembly from a stream.
38 class MSP430AsmParser : public MCTargetAsmParser {
39   const MCSubtargetInfo &STI;
40   MCAsmParser &Parser;
41   const MCRegisterInfo *MRI;
42 
43   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
44                                OperandVector &Operands, MCStreamer &Out,
45                                uint64_t &ErrorInfo,
46                                bool MatchingInlineAsm) override;
47 
48   bool parseRegister(MCRegister &RegNo, SMLoc &StartLoc,
49                      SMLoc &EndLoc) override;
50   OperandMatchResultTy tryParseRegister(MCRegister &RegNo, SMLoc &StartLoc,
51                                         SMLoc &EndLoc) override;
52 
53   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
54                         SMLoc NameLoc, OperandVector &Operands) override;
55 
56   ParseStatus parseDirective(AsmToken DirectiveID) override;
57   bool ParseDirectiveRefSym(AsmToken DirectiveID);
58 
59   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
60                                       unsigned Kind) override;
61 
62   bool parseJccInstruction(ParseInstructionInfo &Info, StringRef Name,
63                            SMLoc NameLoc, OperandVector &Operands);
64 
65   bool ParseOperand(OperandVector &Operands);
66 
67   bool ParseLiteralValues(unsigned Size, SMLoc L);
68 
69   MCAsmParser &getParser() const { return Parser; }
70   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
71 
72   /// @name Auto-generated Matcher Functions
73   /// {
74 
75 #define GET_ASSEMBLER_HEADER
76 #include "MSP430GenAsmMatcher.inc"
77 
78   /// }
79 
80 public:
81   MSP430AsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
82                   const MCInstrInfo &MII, const MCTargetOptions &Options)
83       : MCTargetAsmParser(Options, STI, MII), STI(STI), Parser(Parser) {
84     MCAsmParserExtension::Initialize(Parser);
85     MRI = getContext().getRegisterInfo();
86 
87     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
88   }
89 };
90 
91 /// A parsed MSP430 assembly operand.
92 class MSP430Operand : public MCParsedAsmOperand {
93   typedef MCParsedAsmOperand Base;
94 
95   enum KindTy {
96     k_Imm,
97     k_Reg,
98     k_Tok,
99     k_Mem,
100     k_IndReg,
101     k_PostIndReg
102   } Kind;
103 
104   struct Memory {
105     unsigned Reg;
106     const MCExpr *Offset;
107   };
108   union {
109     const MCExpr *Imm;
110     unsigned      Reg;
111     StringRef     Tok;
112     Memory        Mem;
113   };
114 
115   SMLoc Start, End;
116 
117 public:
118   MSP430Operand(StringRef Tok, SMLoc const &S)
119       : Kind(k_Tok), Tok(Tok), Start(S), End(S) {}
120   MSP430Operand(KindTy Kind, unsigned Reg, SMLoc const &S, SMLoc const &E)
121       : Kind(Kind), Reg(Reg), Start(S), End(E) {}
122   MSP430Operand(MCExpr const *Imm, SMLoc const &S, SMLoc const &E)
123       : Kind(k_Imm), Imm(Imm), Start(S), End(E) {}
124   MSP430Operand(unsigned Reg, MCExpr const *Expr, SMLoc const &S,
125                 SMLoc const &E)
126       : Kind(k_Mem), Mem({Reg, Expr}), Start(S), End(E) {}
127 
128   void addRegOperands(MCInst &Inst, unsigned N) const {
129     assert((Kind == k_Reg || Kind == k_IndReg || Kind == k_PostIndReg) &&
130         "Unexpected operand kind");
131     assert(N == 1 && "Invalid number of operands!");
132 
133     Inst.addOperand(MCOperand::createReg(Reg));
134   }
135 
136   void addExprOperand(MCInst &Inst, const MCExpr *Expr) const {
137     // Add as immediate when possible
138     if (!Expr)
139       Inst.addOperand(MCOperand::createImm(0));
140     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
141       Inst.addOperand(MCOperand::createImm(CE->getValue()));
142     else
143       Inst.addOperand(MCOperand::createExpr(Expr));
144   }
145 
146   void addImmOperands(MCInst &Inst, unsigned N) const {
147     assert(Kind == k_Imm && "Unexpected operand kind");
148     assert(N == 1 && "Invalid number of operands!");
149 
150     addExprOperand(Inst, Imm);
151   }
152 
153   void addMemOperands(MCInst &Inst, unsigned N) const {
154     assert(Kind == k_Mem && "Unexpected operand kind");
155     assert(N == 2 && "Invalid number of operands");
156 
157     Inst.addOperand(MCOperand::createReg(Mem.Reg));
158     addExprOperand(Inst, Mem.Offset);
159   }
160 
161   bool isReg()   const override { return Kind == k_Reg; }
162   bool isImm()   const override { return Kind == k_Imm; }
163   bool isToken() const override { return Kind == k_Tok; }
164   bool isMem()   const override { return Kind == k_Mem; }
165   bool isIndReg()         const { return Kind == k_IndReg; }
166   bool isPostIndReg()     const { return Kind == k_PostIndReg; }
167 
168   bool isCGImm() const {
169     if (Kind != k_Imm)
170       return false;
171 
172     int64_t Val;
173     if (!Imm->evaluateAsAbsolute(Val))
174       return false;
175 
176     if (Val == 0 || Val == 1 || Val == 2 || Val == 4 || Val == 8 || Val == -1)
177       return true;
178 
179     return false;
180   }
181 
182   StringRef getToken() const {
183     assert(Kind == k_Tok && "Invalid access!");
184     return Tok;
185   }
186 
187   unsigned getReg() const override {
188     assert(Kind == k_Reg && "Invalid access!");
189     return Reg;
190   }
191 
192   void setReg(unsigned RegNo) {
193     assert(Kind == k_Reg && "Invalid access!");
194     Reg = RegNo;
195   }
196 
197   static std::unique_ptr<MSP430Operand> CreateToken(StringRef Str, SMLoc S) {
198     return std::make_unique<MSP430Operand>(Str, S);
199   }
200 
201   static std::unique_ptr<MSP430Operand> CreateReg(unsigned RegNum, SMLoc S,
202                                                   SMLoc E) {
203     return std::make_unique<MSP430Operand>(k_Reg, RegNum, S, E);
204   }
205 
206   static std::unique_ptr<MSP430Operand> CreateImm(const MCExpr *Val, SMLoc S,
207                                                   SMLoc E) {
208     return std::make_unique<MSP430Operand>(Val, S, E);
209   }
210 
211   static std::unique_ptr<MSP430Operand> CreateMem(unsigned RegNum,
212                                                   const MCExpr *Val,
213                                                   SMLoc S, SMLoc E) {
214     return std::make_unique<MSP430Operand>(RegNum, Val, S, E);
215   }
216 
217   static std::unique_ptr<MSP430Operand> CreateIndReg(unsigned RegNum, SMLoc S,
218                                                   SMLoc E) {
219     return std::make_unique<MSP430Operand>(k_IndReg, RegNum, S, E);
220   }
221 
222   static std::unique_ptr<MSP430Operand> CreatePostIndReg(unsigned RegNum, SMLoc S,
223                                                   SMLoc E) {
224     return std::make_unique<MSP430Operand>(k_PostIndReg, RegNum, S, E);
225   }
226 
227   SMLoc getStartLoc() const override { return Start; }
228   SMLoc getEndLoc() const override { return End; }
229 
230   void print(raw_ostream &O) const override {
231     switch (Kind) {
232     case k_Tok:
233       O << "Token " << Tok;
234       break;
235     case k_Reg:
236       O << "Register " << Reg;
237       break;
238     case k_Imm:
239       O << "Immediate " << *Imm;
240       break;
241     case k_Mem:
242       O << "Memory ";
243       O << *Mem.Offset << "(" << Reg << ")";
244       break;
245     case k_IndReg:
246       O << "RegInd " << Reg;
247       break;
248     case k_PostIndReg:
249       O << "PostInc " << Reg;
250       break;
251     }
252   }
253 };
254 } // end anonymous namespace
255 
256 bool MSP430AsmParser::MatchAndEmitInstruction(SMLoc Loc, unsigned &Opcode,
257                                               OperandVector &Operands,
258                                               MCStreamer &Out,
259                                               uint64_t &ErrorInfo,
260                                               bool MatchingInlineAsm) {
261   MCInst Inst;
262   unsigned MatchResult =
263       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
264 
265   switch (MatchResult) {
266   case Match_Success:
267     Inst.setLoc(Loc);
268     Out.emitInstruction(Inst, STI);
269     return false;
270   case Match_MnemonicFail:
271     return Error(Loc, "invalid instruction mnemonic");
272   case Match_InvalidOperand: {
273     SMLoc ErrorLoc = Loc;
274     if (ErrorInfo != ~0U) {
275       if (ErrorInfo >= Operands.size())
276         return Error(ErrorLoc, "too few operands for instruction");
277 
278       ErrorLoc = ((MSP430Operand &)*Operands[ErrorInfo]).getStartLoc();
279       if (ErrorLoc == SMLoc())
280         ErrorLoc = Loc;
281     }
282     return Error(ErrorLoc, "invalid operand for instruction");
283   }
284   default:
285     return true;
286   }
287 }
288 
289 // Auto-generated by TableGen
290 static unsigned MatchRegisterName(StringRef Name);
291 static unsigned MatchRegisterAltName(StringRef Name);
292 
293 bool MSP430AsmParser::parseRegister(MCRegister &RegNo, SMLoc &StartLoc,
294                                     SMLoc &EndLoc) {
295   switch (tryParseRegister(RegNo, StartLoc, EndLoc)) {
296   case MatchOperand_ParseFail:
297     return Error(StartLoc, "invalid register name");
298   case MatchOperand_Success:
299     return false;
300   case MatchOperand_NoMatch:
301     return true;
302   }
303 
304   llvm_unreachable("unknown match result type");
305 }
306 
307 OperandMatchResultTy MSP430AsmParser::tryParseRegister(MCRegister &RegNo,
308                                                        SMLoc &StartLoc,
309                                                        SMLoc &EndLoc) {
310   if (getLexer().getKind() == AsmToken::Identifier) {
311     auto Name = getLexer().getTok().getIdentifier().lower();
312     RegNo = MatchRegisterName(Name);
313     if (RegNo == MSP430::NoRegister) {
314       RegNo = MatchRegisterAltName(Name);
315       if (RegNo == MSP430::NoRegister)
316         return MatchOperand_NoMatch;
317     }
318 
319     AsmToken const &T = getParser().getTok();
320     StartLoc = T.getLoc();
321     EndLoc = T.getEndLoc();
322     getLexer().Lex(); // eat register token
323 
324     return MatchOperand_Success;
325   }
326 
327   return MatchOperand_ParseFail;
328 }
329 
330 bool MSP430AsmParser::parseJccInstruction(ParseInstructionInfo &Info,
331                                           StringRef Name, SMLoc NameLoc,
332                                           OperandVector &Operands) {
333   if (!Name.starts_with_insensitive("j"))
334     return true;
335 
336   auto CC = Name.drop_front().lower();
337   unsigned CondCode;
338   if (CC == "ne" || CC == "nz")
339     CondCode = MSP430CC::COND_NE;
340   else if (CC == "eq" || CC == "z")
341     CondCode = MSP430CC::COND_E;
342   else if (CC == "lo" || CC == "nc")
343     CondCode = MSP430CC::COND_LO;
344   else if (CC == "hs" || CC == "c")
345     CondCode = MSP430CC::COND_HS;
346   else if (CC == "n")
347     CondCode = MSP430CC::COND_N;
348   else if (CC == "ge")
349     CondCode = MSP430CC::COND_GE;
350   else if (CC == "l")
351     CondCode = MSP430CC::COND_L;
352   else if (CC == "mp")
353     CondCode = MSP430CC::COND_NONE;
354   else
355     return Error(NameLoc, "unknown instruction");
356 
357   if (CondCode == (unsigned)MSP430CC::COND_NONE)
358     Operands.push_back(MSP430Operand::CreateToken("jmp", NameLoc));
359   else {
360     Operands.push_back(MSP430Operand::CreateToken("j", NameLoc));
361     const MCExpr *CCode = MCConstantExpr::create(CondCode, getContext());
362     Operands.push_back(MSP430Operand::CreateImm(CCode, SMLoc(), SMLoc()));
363   }
364 
365   // Skip optional '$' sign.
366   (void)parseOptionalToken(AsmToken::Dollar);
367 
368   const MCExpr *Val;
369   SMLoc ExprLoc = getLexer().getLoc();
370   if (getParser().parseExpression(Val))
371     return Error(ExprLoc, "expected expression operand");
372 
373   int64_t Res;
374   if (Val->evaluateAsAbsolute(Res))
375     if (Res < -512 || Res > 511)
376       return Error(ExprLoc, "invalid jump offset");
377 
378   Operands.push_back(MSP430Operand::CreateImm(Val, ExprLoc,
379     getLexer().getLoc()));
380 
381   if (getLexer().isNot(AsmToken::EndOfStatement)) {
382     SMLoc Loc = getLexer().getLoc();
383     getParser().eatToEndOfStatement();
384     return Error(Loc, "unexpected token");
385   }
386 
387   getParser().Lex(); // Consume the EndOfStatement.
388   return false;
389 }
390 
391 bool MSP430AsmParser::ParseInstruction(ParseInstructionInfo &Info,
392                                        StringRef Name, SMLoc NameLoc,
393                                        OperandVector &Operands) {
394   // Drop .w suffix
395   if (Name.ends_with_insensitive(".w"))
396     Name = Name.drop_back(2);
397 
398   if (!parseJccInstruction(Info, Name, NameLoc, Operands))
399     return false;
400 
401   // First operand is instruction mnemonic
402   Operands.push_back(MSP430Operand::CreateToken(Name, NameLoc));
403 
404   // If there are no more operands, then finish
405   if (getLexer().is(AsmToken::EndOfStatement))
406     return false;
407 
408   // Parse first operand
409   if (ParseOperand(Operands))
410     return true;
411 
412   // Parse second operand if any
413   if (parseOptionalToken(AsmToken::Comma) && ParseOperand(Operands))
414     return true;
415 
416   if (getLexer().isNot(AsmToken::EndOfStatement)) {
417     SMLoc Loc = getLexer().getLoc();
418     getParser().eatToEndOfStatement();
419     return Error(Loc, "unexpected token");
420   }
421 
422   getParser().Lex(); // Consume the EndOfStatement.
423   return false;
424 }
425 
426 bool MSP430AsmParser::ParseDirectiveRefSym(AsmToken DirectiveID) {
427   StringRef Name;
428   if (getParser().parseIdentifier(Name))
429     return TokError("expected identifier in directive");
430 
431   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
432   getStreamer().emitSymbolAttribute(Sym, MCSA_Global);
433   return parseEOL();
434 }
435 
436 ParseStatus MSP430AsmParser::parseDirective(AsmToken DirectiveID) {
437   StringRef IDVal = DirectiveID.getIdentifier();
438   if (IDVal.lower() == ".long")
439     return ParseLiteralValues(4, DirectiveID.getLoc());
440   if (IDVal.lower() == ".word" || IDVal.lower() == ".short")
441     return ParseLiteralValues(2, DirectiveID.getLoc());
442   if (IDVal.lower() == ".byte")
443     return ParseLiteralValues(1, DirectiveID.getLoc());
444   if (IDVal.lower() == ".refsym")
445     return ParseDirectiveRefSym(DirectiveID);
446   return ParseStatus::NoMatch;
447 }
448 
449 bool MSP430AsmParser::ParseOperand(OperandVector &Operands) {
450   switch (getLexer().getKind()) {
451     default: return true;
452     case AsmToken::Identifier: {
453       // try rN
454       MCRegister RegNo;
455       SMLoc StartLoc, EndLoc;
456       if (!parseRegister(RegNo, StartLoc, EndLoc)) {
457         Operands.push_back(MSP430Operand::CreateReg(RegNo, StartLoc, EndLoc));
458         return false;
459       }
460       [[fallthrough]];
461     }
462     case AsmToken::Integer:
463     case AsmToken::Plus:
464     case AsmToken::Minus: {
465       SMLoc StartLoc = getParser().getTok().getLoc();
466       const MCExpr *Val;
467       // Try constexpr[(rN)]
468       if (!getParser().parseExpression(Val)) {
469         MCRegister RegNo = MSP430::PC;
470         SMLoc EndLoc = getParser().getTok().getLoc();
471         // Try (rN)
472         if (parseOptionalToken(AsmToken::LParen)) {
473           SMLoc RegStartLoc;
474           if (parseRegister(RegNo, RegStartLoc, EndLoc))
475             return true;
476           EndLoc = getParser().getTok().getEndLoc();
477           if (!parseOptionalToken(AsmToken::RParen))
478             return true;
479         }
480         Operands.push_back(MSP430Operand::CreateMem(RegNo, Val, StartLoc,
481           EndLoc));
482         return false;
483       }
484       return true;
485     }
486     case AsmToken::Amp: {
487       // Try &constexpr
488       SMLoc StartLoc = getParser().getTok().getLoc();
489       getLexer().Lex(); // Eat '&'
490       const MCExpr *Val;
491       if (!getParser().parseExpression(Val)) {
492         SMLoc EndLoc = getParser().getTok().getLoc();
493         Operands.push_back(MSP430Operand::CreateMem(MSP430::SR, Val, StartLoc,
494           EndLoc));
495         return false;
496       }
497       return true;
498     }
499     case AsmToken::At: {
500       // Try @rN[+]
501       SMLoc StartLoc = getParser().getTok().getLoc();
502       getLexer().Lex(); // Eat '@'
503       MCRegister RegNo;
504       SMLoc RegStartLoc, EndLoc;
505       if (parseRegister(RegNo, RegStartLoc, EndLoc))
506         return true;
507       if (parseOptionalToken(AsmToken::Plus)) {
508         Operands.push_back(MSP430Operand::CreatePostIndReg(RegNo, StartLoc, EndLoc));
509         return false;
510       }
511       if (Operands.size() > 1) // Emulate @rd in destination position as 0(rd)
512         Operands.push_back(MSP430Operand::CreateMem(RegNo,
513             MCConstantExpr::create(0, getContext()), StartLoc, EndLoc));
514       else
515         Operands.push_back(MSP430Operand::CreateIndReg(RegNo, StartLoc, EndLoc));
516       return false;
517     }
518     case AsmToken::Hash:
519       // Try #constexpr
520       SMLoc StartLoc = getParser().getTok().getLoc();
521       getLexer().Lex(); // Eat '#'
522       const MCExpr *Val;
523       if (!getParser().parseExpression(Val)) {
524         SMLoc EndLoc = getParser().getTok().getLoc();
525         Operands.push_back(MSP430Operand::CreateImm(Val, StartLoc, EndLoc));
526         return false;
527       }
528       return true;
529   }
530 }
531 
532 bool MSP430AsmParser::ParseLiteralValues(unsigned Size, SMLoc L) {
533   auto parseOne = [&]() -> bool {
534     const MCExpr *Value;
535     if (getParser().parseExpression(Value))
536       return true;
537     getParser().getStreamer().emitValue(Value, Size, L);
538     return false;
539   };
540   return (parseMany(parseOne));
541 }
542 
543 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMSP430AsmParser() {
544   RegisterMCAsmParser<MSP430AsmParser> X(getTheMSP430Target());
545 }
546 
547 #define GET_REGISTER_MATCHER
548 #define GET_MATCHER_IMPLEMENTATION
549 #include "MSP430GenAsmMatcher.inc"
550 
551 static unsigned convertGR16ToGR8(unsigned Reg) {
552   switch (Reg) {
553   default:
554     llvm_unreachable("Unknown GR16 register");
555   case MSP430::PC:  return MSP430::PCB;
556   case MSP430::SP:  return MSP430::SPB;
557   case MSP430::SR:  return MSP430::SRB;
558   case MSP430::CG:  return MSP430::CGB;
559   case MSP430::R4:  return MSP430::R4B;
560   case MSP430::R5:  return MSP430::R5B;
561   case MSP430::R6:  return MSP430::R6B;
562   case MSP430::R7:  return MSP430::R7B;
563   case MSP430::R8:  return MSP430::R8B;
564   case MSP430::R9:  return MSP430::R9B;
565   case MSP430::R10: return MSP430::R10B;
566   case MSP430::R11: return MSP430::R11B;
567   case MSP430::R12: return MSP430::R12B;
568   case MSP430::R13: return MSP430::R13B;
569   case MSP430::R14: return MSP430::R14B;
570   case MSP430::R15: return MSP430::R15B;
571   }
572 }
573 
574 unsigned MSP430AsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
575                                                      unsigned Kind) {
576   MSP430Operand &Op = static_cast<MSP430Operand &>(AsmOp);
577 
578   if (!Op.isReg())
579     return Match_InvalidOperand;
580 
581   unsigned Reg = Op.getReg();
582   bool isGR16 =
583       MSP430MCRegisterClasses[MSP430::GR16RegClassID].contains(Reg);
584 
585   if (isGR16 && (Kind == MCK_GR8)) {
586     Op.setReg(convertGR16ToGR8(Reg));
587     return Match_Success;
588   }
589 
590   return Match_InvalidOperand;
591 }
592