1//===-- BPFAsmParser.cpp - Parse BPF 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 "MCTargetDesc/BPFMCTargetDesc.h"
10#include "TargetInfo/BPFTargetInfo.h"
11#include "llvm/ADT/StringSwitch.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCInst.h"
15#include "llvm/MC/MCInstrInfo.h"
16#include "llvm/MC/MCParser/AsmLexer.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
18#include "llvm/MC/MCParser/MCTargetAsmParser.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCSubtargetInfo.h"
21#include "llvm/MC/TargetRegistry.h"
22#include "llvm/Support/Casting.h"
23#include "llvm/Support/Compiler.h"
24
25using namespace llvm;
26
27namespace {
28struct BPFOperand;
29
30class BPFAsmParser : public MCTargetAsmParser {
31
32 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
33
34 bool PreMatchCheck(OperandVector &Operands);
35
36 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
37 OperandVector &Operands, MCStreamer &Out,
38 uint64_t &ErrorInfo,
39 bool MatchingInlineAsm) override;
40
41 bool parseRegister(MCRegister &Reo, SMLoc &StartLoc, SMLoc &EndLoc) override;
42 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
43 SMLoc &EndLoc) override;
44
45 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
46 SMLoc NameLoc, OperandVector &Operands) override;
47
48 // "=" is used as assignment operator for assembly statment, so can't be used
49 // for symbol assignment.
50 bool equalIsAsmAssignment() override { return false; }
51 // "*" is used for dereferencing memory that it will be the start of
52 // statement.
53 bool tokenIsStartOfStatement(AsmToken::TokenKind Token) override {
54 return Token == AsmToken::Star;
55 }
56
57#define GET_ASSEMBLER_HEADER
58#include "BPFGenAsmMatcher.inc"
59
60 ParseStatus parseImmediate(OperandVector &Operands);
61 ParseStatus parseRegister(OperandVector &Operands);
62 ParseStatus parseOperandAsOperator(OperandVector &Operands);
63
64public:
65 enum BPFMatchResultTy {
66 Match_Dummy = FIRST_TARGET_MATCH_RESULT_TY,
67#define GET_OPERAND_DIAGNOSTIC_TYPES
68#include "BPFGenAsmMatcher.inc"
69#undef GET_OPERAND_DIAGNOSTIC_TYPES
70 };
71
72 BPFAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
73 const MCInstrInfo &MII)
74 : MCTargetAsmParser(STI, MII) {
75 setAvailableFeatures(ComputeAvailableFeatures(FB: STI.getFeatureBits()));
76 }
77};
78
79/// BPFOperand - Instances of this class represent a parsed machine
80/// instruction
81struct BPFOperand : public MCParsedAsmOperand {
82
83 enum KindTy {
84 Token,
85 Register,
86 Immediate,
87 } Kind;
88
89 struct RegOp {
90 MCRegister RegNum;
91 };
92
93 struct ImmOp {
94 const MCExpr *Val;
95 };
96
97 SMLoc StartLoc, EndLoc;
98 union {
99 StringRef Tok;
100 RegOp Reg;
101 ImmOp Imm;
102 };
103
104 BPFOperand(KindTy K) : Kind(K) {}
105
106public:
107 BPFOperand(const BPFOperand &o) : MCParsedAsmOperand() {
108 Kind = o.Kind;
109 StartLoc = o.StartLoc;
110 EndLoc = o.EndLoc;
111
112 switch (Kind) {
113 case Register:
114 Reg = o.Reg;
115 break;
116 case Immediate:
117 Imm = o.Imm;
118 break;
119 case Token:
120 Tok = o.Tok;
121 break;
122 }
123 }
124
125 bool isToken() const override { return Kind == Token; }
126 bool isReg() const override { return Kind == Register; }
127 bool isImm() const override { return Kind == Immediate; }
128 bool isMem() const override { return false; }
129
130 bool isConstantImm() const {
131 return isImm() && isa<MCConstantExpr>(Val: getImm());
132 }
133
134 int64_t getConstantImm() const {
135 const MCExpr *Val = getImm();
136 return static_cast<const MCConstantExpr *>(Val)->getValue();
137 }
138
139 bool isSImm16() const {
140 return (isConstantImm() && isInt<16>(x: getConstantImm()));
141 }
142
143 bool isSymbolRef() const { return isImm() && isa<MCSymbolRefExpr>(Val: getImm()); }
144
145 bool isBrTarget() const { return isSymbolRef() || isSImm16(); }
146
147 /// getStartLoc - Gets location of the first token of this operand
148 SMLoc getStartLoc() const override { return StartLoc; }
149 /// getEndLoc - Gets location of the last token of this operand
150 SMLoc getEndLoc() const override { return EndLoc; }
151
152 MCRegister getReg() const override {
153 assert(Kind == Register && "Invalid type access!");
154 return Reg.RegNum;
155 }
156
157 const MCExpr *getImm() const {
158 assert(Kind == Immediate && "Invalid type access!");
159 return Imm.Val;
160 }
161
162 StringRef getToken() const {
163 assert(Kind == Token && "Invalid type access!");
164 return Tok;
165 }
166
167 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
168 switch (Kind) {
169 case Immediate:
170 MAI.printExpr(OS, *getImm());
171 break;
172 case Register:
173 OS << "<register x";
174 OS << getReg().id() << ">";
175 break;
176 case Token:
177 OS << "'" << getToken() << "'";
178 break;
179 }
180 }
181
182 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
183 assert(Expr && "Expr shouldn't be null!");
184
185 if (auto *CE = dyn_cast<MCConstantExpr>(Val: Expr))
186 Inst.addOperand(Op: MCOperand::createImm(Val: CE->getValue()));
187 else
188 Inst.addOperand(Op: MCOperand::createExpr(Val: Expr));
189 }
190
191 // Used by the TableGen Code
192 void addRegOperands(MCInst &Inst, unsigned N) const {
193 assert(N == 1 && "Invalid number of operands!");
194 Inst.addOperand(Op: MCOperand::createReg(Reg: getReg()));
195 }
196
197 void addImmOperands(MCInst &Inst, unsigned N) const {
198 assert(N == 1 && "Invalid number of operands!");
199 addExpr(Inst, Expr: getImm());
200 }
201
202 static std::unique_ptr<BPFOperand> createToken(StringRef Str, SMLoc S) {
203 auto Op = std::make_unique<BPFOperand>(args: Token);
204 Op->Tok = Str;
205 Op->StartLoc = S;
206 Op->EndLoc = S;
207 return Op;
208 }
209
210 static std::unique_ptr<BPFOperand> createReg(MCRegister Reg, SMLoc S,
211 SMLoc E) {
212 auto Op = std::make_unique<BPFOperand>(args: Register);
213 Op->Reg.RegNum = Reg;
214 Op->StartLoc = S;
215 Op->EndLoc = E;
216 return Op;
217 }
218
219 static std::unique_ptr<BPFOperand> createImm(const MCExpr *Val, SMLoc S,
220 SMLoc E) {
221 auto Op = std::make_unique<BPFOperand>(args: Immediate);
222 Op->Imm.Val = Val;
223 Op->StartLoc = S;
224 Op->EndLoc = E;
225 return Op;
226 }
227
228 // Identifiers that can be used at the start of a statment.
229 static bool isValidIdAtStart(StringRef Name) {
230 return StringSwitch<bool>(Name.lower())
231 .Case(S: "if", Value: true)
232 .Case(S: "call", Value: true)
233 .Case(S: "callx", Value: true)
234 .Case(S: "goto", Value: true)
235 .Case(S: "gotol", Value: true)
236 .Case(S: "gotox", Value: true)
237 .Case(S: "may_goto", Value: true)
238 .Case(S: "unwind_goto", Value: true)
239 .Case(S: "*", Value: true)
240 .Case(S: "exit", Value: true)
241 .Case(S: "unwind_exit", Value: true)
242 .Case(S: "lock", Value: true)
243 .Case(S: "ld_pseudo", Value: true)
244 .Case(S: "store_release", Value: true)
245 .Default(Value: false);
246 }
247
248 // Identifiers that can be used in the middle of a statment.
249 static bool isValidIdInMiddle(StringRef Name) {
250 return StringSwitch<bool>(Name.lower())
251 .Case(S: "u64", Value: true)
252 .Case(S: "u32", Value: true)
253 .Case(S: "u16", Value: true)
254 .Case(S: "u8", Value: true)
255 .Case(S: "s32", Value: true)
256 .Case(S: "s16", Value: true)
257 .Case(S: "s8", Value: true)
258 .Case(S: "be64", Value: true)
259 .Case(S: "be32", Value: true)
260 .Case(S: "be16", Value: true)
261 .Case(S: "le64", Value: true)
262 .Case(S: "le32", Value: true)
263 .Case(S: "le16", Value: true)
264 .Case(S: "bswap16", Value: true)
265 .Case(S: "bswap32", Value: true)
266 .Case(S: "bswap64", Value: true)
267 .Case(S: "goto", Value: true)
268 .Case(S: "ll", Value: true)
269 .Case(S: "skb", Value: true)
270 .Case(S: "s", Value: true)
271 .Case(S: "uh", Value: true)
272 .Case(S: "sh", Value: true)
273 .Case(S: "atomic_fetch_add", Value: true)
274 .Case(S: "atomic_fetch_and", Value: true)
275 .Case(S: "atomic_fetch_or", Value: true)
276 .Case(S: "atomic_fetch_xor", Value: true)
277 .Case(S: "xchg_64", Value: true)
278 .Case(S: "xchg32_32", Value: true)
279 .Case(S: "cmpxchg_64", Value: true)
280 .Case(S: "cmpxchg32_32", Value: true)
281 .Case(S: "addr_space_cast", Value: true)
282 .Case(S: "load_acquire", Value: true)
283 .Default(Value: false);
284 }
285};
286} // end anonymous namespace.
287
288#define GET_REGISTER_MATCHER
289#define GET_MATCHER_IMPLEMENTATION
290#include "BPFGenAsmMatcher.inc"
291
292bool BPFAsmParser::PreMatchCheck(OperandVector &Operands) {
293
294 if (Operands.size() == 4) {
295 // check "reg1 = -reg2" and "reg1 = be16/be32/be64/le16/le32/le64 reg2",
296 // reg1 must be the same as reg2
297 BPFOperand &Op0 = (BPFOperand &)*Operands[0];
298 BPFOperand &Op1 = (BPFOperand &)*Operands[1];
299 BPFOperand &Op2 = (BPFOperand &)*Operands[2];
300 BPFOperand &Op3 = (BPFOperand &)*Operands[3];
301 if (Op0.isReg() && Op1.isToken() && Op2.isToken() && Op3.isReg()
302 && Op1.getToken() == "="
303 && (Op2.getToken() == "-" || Op2.getToken() == "be16"
304 || Op2.getToken() == "be32" || Op2.getToken() == "be64"
305 || Op2.getToken() == "le16" || Op2.getToken() == "le32"
306 || Op2.getToken() == "le64")
307 && Op0.getReg() != Op3.getReg())
308 return true;
309 }
310
311 return false;
312}
313
314bool BPFAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
315 OperandVector &Operands,
316 MCStreamer &Out, uint64_t &ErrorInfo,
317 bool MatchingInlineAsm) {
318 MCInst Inst;
319 SMLoc ErrorLoc;
320
321 if (PreMatchCheck(Operands))
322 return Error(L: IDLoc, Msg: "additional inst constraint not met");
323
324 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, matchingInlineAsm: MatchingInlineAsm)) {
325 default:
326 break;
327 case Match_Success:
328 Inst.setLoc(IDLoc);
329 Out.emitInstruction(Inst, STI: getSTI());
330 return false;
331 case Match_MissingFeature:
332 return Error(L: IDLoc, Msg: "instruction use requires an option to be enabled");
333 case Match_MnemonicFail:
334 return Error(L: IDLoc, Msg: "unrecognized instruction mnemonic");
335 case Match_InvalidOperand:
336 ErrorLoc = IDLoc;
337
338 if (ErrorInfo != ~0U) {
339 if (ErrorInfo >= Operands.size())
340 return Error(L: ErrorLoc, Msg: "too few operands for instruction");
341
342 ErrorLoc = ((BPFOperand &)*Operands[ErrorInfo]).getStartLoc();
343
344 if (ErrorLoc == SMLoc())
345 ErrorLoc = IDLoc;
346 }
347
348 return Error(L: ErrorLoc, Msg: "invalid operand for instruction");
349 case Match_InvalidBrTarget:
350 return Error(L: Operands[ErrorInfo]->getStartLoc(),
351 Msg: "operand is not an identifier or 16-bit signed integer");
352 case Match_InvalidSImm16:
353 return Error(L: Operands[ErrorInfo]->getStartLoc(),
354 Msg: "operand is not a 16-bit signed integer");
355 case Match_InvalidTiedOperand:
356 return Error(L: Operands[ErrorInfo]->getStartLoc(),
357 Msg: "operand is not the same as the dst register");
358 }
359
360 llvm_unreachable("Unknown match type detected!");
361}
362
363bool BPFAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
364 SMLoc &EndLoc) {
365 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
366 return Error(L: StartLoc, Msg: "invalid register name");
367 return false;
368}
369
370ParseStatus BPFAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
371 SMLoc &EndLoc) {
372 const AsmToken &Tok = getParser().getTok();
373 StartLoc = Tok.getLoc();
374 EndLoc = Tok.getEndLoc();
375 Reg = BPF::NoRegister;
376 StringRef Name = getLexer().getTok().getIdentifier();
377
378 if (!MatchRegisterName(Name)) {
379 getParser().Lex(); // Eat identifier token.
380 return ParseStatus::Success;
381 }
382
383 return ParseStatus::NoMatch;
384}
385
386ParseStatus BPFAsmParser::parseOperandAsOperator(OperandVector &Operands) {
387 SMLoc S = getLoc();
388
389 if (getLexer().getKind() == AsmToken::Identifier) {
390 StringRef Name = getLexer().getTok().getIdentifier();
391
392 if (BPFOperand::isValidIdInMiddle(Name)) {
393 getLexer().Lex();
394 Operands.push_back(Elt: BPFOperand::createToken(Str: Name, S));
395 return ParseStatus::Success;
396 }
397
398 return ParseStatus::NoMatch;
399 }
400
401 switch (getLexer().getKind()) {
402 case AsmToken::Minus:
403 case AsmToken::Plus: {
404 if (getLexer().peekTok().is(K: AsmToken::Integer))
405 return ParseStatus::NoMatch;
406 [[fallthrough]];
407 }
408
409 case AsmToken::Equal:
410 case AsmToken::Greater:
411 case AsmToken::Less:
412 case AsmToken::Pipe:
413 case AsmToken::Star:
414 case AsmToken::LParen:
415 case AsmToken::RParen:
416 case AsmToken::LBrac:
417 case AsmToken::RBrac:
418 case AsmToken::Slash:
419 case AsmToken::Amp:
420 case AsmToken::Percent:
421 case AsmToken::Caret: {
422 StringRef Name = getLexer().getTok().getString();
423 getLexer().Lex();
424 Operands.push_back(Elt: BPFOperand::createToken(Str: Name, S));
425
426 return ParseStatus::Success;
427 }
428
429 case AsmToken::EqualEqual:
430 case AsmToken::ExclaimEqual:
431 case AsmToken::GreaterEqual:
432 case AsmToken::GreaterGreater:
433 case AsmToken::LessEqual:
434 case AsmToken::LessLess: {
435 Operands.push_back(Elt: BPFOperand::createToken(
436 Str: getLexer().getTok().getString().substr(Start: 0, N: 1), S));
437 Operands.push_back(Elt: BPFOperand::createToken(
438 Str: getLexer().getTok().getString().substr(Start: 1, N: 1), S));
439 getLexer().Lex();
440
441 return ParseStatus::Success;
442 }
443
444 default:
445 break;
446 }
447
448 return ParseStatus::NoMatch;
449}
450
451ParseStatus BPFAsmParser::parseRegister(OperandVector &Operands) {
452 SMLoc S = getLoc();
453 SMLoc E = SMLoc::getFromPointer(Ptr: S.getPointer() - 1);
454
455 switch (getLexer().getKind()) {
456 default:
457 return ParseStatus::NoMatch;
458 case AsmToken::Identifier:
459 StringRef Name = getLexer().getTok().getIdentifier();
460 MCRegister Reg = MatchRegisterName(Name);
461
462 if (!Reg)
463 return ParseStatus::NoMatch;
464
465 getLexer().Lex();
466 Operands.push_back(Elt: BPFOperand::createReg(Reg, S, E));
467 }
468 return ParseStatus::Success;
469}
470
471ParseStatus BPFAsmParser::parseImmediate(OperandVector &Operands) {
472 switch (getLexer().getKind()) {
473 default:
474 return ParseStatus::NoMatch;
475 case AsmToken::LParen:
476 case AsmToken::Minus:
477 case AsmToken::Plus:
478 case AsmToken::Integer:
479 case AsmToken::String:
480 case AsmToken::Identifier:
481 break;
482 }
483
484 const MCExpr *IdVal;
485 SMLoc S = getLoc();
486
487 if (getParser().parseExpression(Res&: IdVal))
488 return ParseStatus::Failure;
489
490 SMLoc E = SMLoc::getFromPointer(Ptr: S.getPointer() - 1);
491 Operands.push_back(Elt: BPFOperand::createImm(Val: IdVal, S, E));
492
493 return ParseStatus::Success;
494}
495
496/// Parse an BPF instruction which is in BPF verifier format.
497bool BPFAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
498 SMLoc NameLoc, OperandVector &Operands) {
499 // The first operand could be either register or actually an operator.
500 MCRegister Reg = MatchRegisterName(Name);
501
502 if (Reg) {
503 SMLoc E = SMLoc::getFromPointer(Ptr: NameLoc.getPointer() - 1);
504 Operands.push_back(Elt: BPFOperand::createReg(Reg, S: NameLoc, E));
505 } else if (BPFOperand::isValidIdAtStart(Name))
506 Operands.push_back(Elt: BPFOperand::createToken(Str: Name, S: NameLoc));
507 else
508 return Error(L: NameLoc, Msg: "invalid register/token name");
509
510 while (!getLexer().is(K: AsmToken::EndOfStatement)) {
511 // Attempt to parse token as operator
512 if (parseOperandAsOperator(Operands).isSuccess())
513 continue;
514
515 // Attempt to parse token as register
516 if (parseRegister(Operands).isSuccess())
517 continue;
518
519 if (getLexer().is(K: AsmToken::Comma)) {
520 getLexer().Lex();
521 continue;
522 }
523
524 // Attempt to parse token as an immediate
525 if (!parseImmediate(Operands).isSuccess()) {
526 SMLoc Loc = getLexer().getLoc();
527 return Error(L: Loc, Msg: "unexpected token");
528 }
529 }
530
531 if (getLexer().isNot(K: AsmToken::EndOfStatement)) {
532 SMLoc Loc = getLexer().getLoc();
533
534 getParser().eatToEndOfStatement();
535
536 return Error(L: Loc, Msg: "unexpected token");
537 }
538
539 // Consume the EndOfStatement.
540 getParser().Lex();
541 return false;
542}
543
544extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFAsmParser() {
545 RegisterMCAsmParser<BPFAsmParser> X(getTheBPFTarget());
546 RegisterMCAsmParser<BPFAsmParser> Y(getTheBPFleTarget());
547 RegisterMCAsmParser<BPFAsmParser> Z(getTheBPFbeTarget());
548}
549