Skip to content

Commit 4bb60c2

Browse files
committed
[CSKY 6/n] Add support branch and symbol series instruction
This patch adds basic CSKY branch instructions and symbol address series instructions. Those two kinds of instruction have relationship between each other, and it involves much work about Fixups. For now, basic instructions are enabled except for disassembler support. We would support to generate basic codegen asm firstly and delay disassembler work later. Differential Revision: https://reviews.llvm.org/D95029
1 parent 4216389 commit 4bb60c2

13 files changed

Lines changed: 817 additions & 11 deletions

‎llvm/lib/Target/CSKY/AsmParser/CSKYAsmParser.cpp‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
#include "MCTargetDesc/CSKYMCExpr.h"
1011
#include "MCTargetDesc/CSKYMCTargetDesc.h"
1112
#include "TargetInfo/CSKYTargetInfo.h"
1213
#include "llvm/ADT/STLExtras.h"
@@ -58,6 +59,8 @@ class CSKYAsmParser : public MCTargetAsmParser {
5859
OperandMatchResultTy parseImmediate(OperandVector &Operands);
5960
OperandMatchResultTy parseRegister(OperandVector &Operands);
6061
OperandMatchResultTy parseBaseRegImm(OperandVector &Operands);
62+
OperandMatchResultTy parseCSKYSymbol(OperandVector &Operands);
63+
OperandMatchResultTy parseConstpoolSymbol(OperandVector &Operands);
6164

6265
bool parseOperand(OperandVector &Operands, StringRef Mnemonic);
6366

@@ -171,6 +174,20 @@ struct CSKYOperand : public MCParsedAsmOperand {
171174
bool isUImm12Shift1() { return isUImm<12, 1>(); }
172175
bool isUImm12Shift2() { return isUImm<12, 2>(); }
173176

177+
bool isSImm16Shift1() { return isSImm<16, 1>(); }
178+
179+
bool isCSKYSymbol() const {
180+
int64_t Imm;
181+
// Must be of 'immediate' type but not a constant.
182+
return isImm() && !evaluateConstantImm(getImm(), Imm);
183+
}
184+
185+
bool isConstpoolSymbol() const {
186+
int64_t Imm;
187+
// Must be of 'immediate' type but not a constant.
188+
return isImm() && !evaluateConstantImm(getImm(), Imm);
189+
}
190+
174191
/// Gets location of the first token of this operand.
175192
SMLoc getStartLoc() const override { return StartLoc; }
176193
/// Gets location of the last token of this operand.
@@ -350,6 +367,14 @@ bool CSKYAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
350367
"immediate must be a multiple of 4 bytes in the range");
351368
case Match_InvalidUImm16:
352369
return generateImmOutOfRangeError(Operands, ErrorInfo, 0, (1 << 16) - 1);
370+
case Match_InvalidCSKYSymbol: {
371+
SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
372+
return Error(ErrorLoc, "operand must be a symbol name");
373+
}
374+
case Match_InvalidConstpool: {
375+
SMLoc ErrorLoc = ((CSKYOperand &)*Operands[ErrorInfo]).getStartLoc();
376+
return Error(ErrorLoc, "operand must be a constpool symbol name");
377+
}
353378
}
354379

355380
llvm_unreachable("Unknown match type detected!");
@@ -482,6 +507,15 @@ OperandMatchResultTy CSKYAsmParser::parseImmediate(OperandVector &Operands) {
482507
/// information, adding to Operands. If operand was parsed, returns false, else
483508
/// true.
484509
bool CSKYAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
510+
// Check if the current operand has a custom associated parser, if so, try to
511+
// custom parse the operand, or fallback to the general approach.
512+
OperandMatchResultTy Result =
513+
MatchOperandParserImpl(Operands, Mnemonic, /*ParseForAllFeatures=*/true);
514+
if (Result == MatchOperand_Success)
515+
return false;
516+
if (Result == MatchOperand_ParseFail)
517+
return true;
518+
485519
// Attempt to parse token as register
486520
if (parseRegister(Operands) == MatchOperand_Success)
487521
return false;
@@ -500,6 +534,68 @@ bool CSKYAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
500534
return true;
501535
}
502536

537+
OperandMatchResultTy CSKYAsmParser::parseCSKYSymbol(OperandVector &Operands) {
538+
SMLoc S = getLoc();
539+
SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
540+
541+
if (getLexer().getKind() != AsmToken::Identifier)
542+
return MatchOperand_NoMatch;
543+
544+
StringRef Identifier;
545+
if (getParser().parseIdentifier(Identifier))
546+
return MatchOperand_ParseFail;
547+
548+
CSKYMCExpr::VariantKind Kind = CSKYMCExpr::VK_CSKY_None;
549+
550+
if (Identifier.consume_back("@GOT"))
551+
Kind = CSKYMCExpr::VK_CSKY_GOT;
552+
else if (Identifier.consume_back("@GOTOFF"))
553+
Kind = CSKYMCExpr::VK_CSKY_GOTOFF;
554+
else if (Identifier.consume_back("@PLT"))
555+
Kind = CSKYMCExpr::VK_CSKY_PLT;
556+
else if (Identifier.consume_back("@GOTPC"))
557+
Kind = CSKYMCExpr::VK_CSKY_GOTPC;
558+
559+
MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
560+
const MCExpr *Res =
561+
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
562+
563+
if (Kind != CSKYMCExpr::VK_CSKY_None)
564+
Res = CSKYMCExpr::create(Res, Kind, getContext());
565+
566+
Operands.push_back(CSKYOperand::createImm(Res, S, E));
567+
return MatchOperand_Success;
568+
}
569+
570+
OperandMatchResultTy
571+
CSKYAsmParser::parseConstpoolSymbol(OperandVector &Operands) {
572+
SMLoc S = getLoc();
573+
SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
574+
575+
if (getLexer().getKind() != AsmToken::LBrac)
576+
return MatchOperand_NoMatch;
577+
578+
getLexer().Lex(); // Eat '['.
579+
580+
if (getLexer().getKind() != AsmToken::Identifier)
581+
return MatchOperand_NoMatch;
582+
583+
StringRef Identifier;
584+
if (getParser().parseIdentifier(Identifier))
585+
return MatchOperand_ParseFail;
586+
587+
if (getLexer().getKind() != AsmToken::RBrac)
588+
return MatchOperand_NoMatch;
589+
590+
getLexer().Lex(); // Eat ']'.
591+
592+
MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
593+
const MCExpr *Res =
594+
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
595+
Operands.push_back(CSKYOperand::createImm(Res, S, E));
596+
return MatchOperand_Success;
597+
}
598+
503599
bool CSKYAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
504600
SMLoc NameLoc, OperandVector &Operands) {
505601
// First operand is token for instruction.

‎llvm/lib/Target/CSKY/CSKYInstrFormats.td‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ class J<bits<6> opcode, dag outs, dag ins, string op, list<dag> pattern>
5454
pattern> {
5555
bits<26> offset;
5656
let Inst{25 - 0} = offset;
57+
let isCall = 1;
58+
let Defs = [ R15 ];
5759
}
5860

5961
// Format< OP[6] | RZ[5] | SOP[3] | OFFSET[18] >
6062
// Instructions(7): grs, lrs32.b, lrs32.h, lrs32.w, srs32.b, srs32.h, srs32.w
61-
class I_18_Z_L<bits<3> sop, string op, dag outs, dag ins, list<dag> pattern>
62-
: CSKY32Inst<AddrModeNone, 0x33, outs, ins,
63-
!strconcat(op, "\t$rz, $offset"), pattern> {
63+
class I_18_Z_L<bits<3> sop, string asm, dag outs, dag ins, list<dag> pattern>
64+
: CSKY32Inst<AddrModeNone, 0x33, outs, ins, asm, pattern> {
6465
bits<5> rz;
6566
bits<18> offset;
6667
let Inst{25 - 21} = rz;
@@ -102,7 +103,7 @@ class I_16_MOV<bits<5> sop, string op, ImmLeaf ImmType>
102103
// Instructions(1): lrw32
103104
class I_16_Z_L<bits<5> sop, string op, dag ins, list<dag> pattern>
104105
: CSKY32Inst<AddrModeNone, 0x3a, (outs GPR:$rz), ins,
105-
!strconcat(op, "\t$rz, [$imm16]"), pattern> {
106+
!strconcat(op, "\t$rz, $imm16"), pattern> {
106107
bits<5> rz;
107108
bits<16> imm16;
108109
let Inst{25 - 21} = sop;
@@ -112,9 +113,8 @@ class I_16_Z_L<bits<5> sop, string op, dag ins, list<dag> pattern>
112113

113114
// Format< OP[6] | SOP[5] | 00000[5] | OFFSET[16] >
114115
// Instructions(5): bt32, bf32, br32, jmpi32, jsri32
115-
class I_16_L<bits<5> sop, dag outs, dag ins, string op, list<dag> pattern>
116-
: CSKY32Inst<AddrModeNone, 0x3a, outs, ins, !strconcat(op, "\t$imm16"),
117-
pattern> {
116+
class I_16_L<bits<5> sop, dag outs, dag ins, string asm, list<dag> pattern>
117+
: CSKY32Inst<AddrModeNone, 0x3a, outs, ins, asm, pattern> {
118118
bits<16> imm16;
119119
let Inst{25 - 21} = sop;
120120
let Inst{20 - 16} = 0;
@@ -159,6 +159,19 @@ class I_16_RET<bits<5> sop, bits<5> pcode, string op, list<dag> pattern>
159159
let isBarrier = 1;
160160
}
161161

162+
// Instructions(1): rte32
163+
class I_16_RET_I<bits<5> sop, bits<5> pcode, string op, list<dag> pattern>
164+
: CSKY32Inst<AddrModeNone, 0x30, (outs), (ins), op, pattern> {
165+
let Inst{25 - 21} = sop;
166+
let Inst{20 - 16} = pcode;
167+
let Inst{15 - 10} = 0x10;
168+
let Inst{9 - 5} = 1;
169+
let Inst{4 - 0} = 0;
170+
let isTerminator = 1;
171+
let isReturn = 1;
172+
let isBarrier = 1;
173+
}
174+
162175
// Format< OP[6] | SOP[5] | RX[5] | IMM16[16] >
163176
// Instructions(3): cmpnei32, cmphsi32, cmplti32
164177
class I_16_X<bits<5> sop, string op, Operand operand>

‎llvm/lib/Target/CSKY/CSKYInstrInfo.td‎

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
// CSKY specific DAG Nodes.
1616
//===----------------------------------------------------------------------===//
1717

18-
// TODO: Add CSKY specific DAG Nodes.
18+
// Target-dependent nodes.
19+
def CSKY_RET : SDNode<"CSKYISD::RET", SDTNone,
20+
[SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
1921

2022
//===----------------------------------------------------------------------===//
2123
// Operand and SDNode transformation definitions.
@@ -81,6 +83,41 @@ def uimm_shift : Operand<i32>, ImmLeaf<i32, "return isUInt<2>(Imm);"> {
8183
let ParserMatchClass = UImmAsmOperand<2>;
8284
}
8385

86+
def CSKYSymbol : AsmOperandClass {
87+
let Name = "CSKYSymbol";
88+
let RenderMethod = "addImmOperands";
89+
let DiagnosticType = "InvalidCSKYSymbol";
90+
let ParserMethod = "parseCSKYSymbol";
91+
}
92+
93+
def br_symbol : Operand<iPTR> {
94+
let EncoderMethod =
95+
"getBranchSymbolOpValue<CSKY::fixup_csky_pcrel_imm16_scale2>";
96+
let ParserMatchClass = CSKYSymbol;
97+
}
98+
99+
def call_symbol : Operand<iPTR> {
100+
let ParserMatchClass = CSKYSymbol;
101+
let EncoderMethod = "getCallSymbolOpValue";
102+
}
103+
104+
def Constpool : AsmOperandClass {
105+
let Name = "ConstpoolSymbol";
106+
let RenderMethod = "addImmOperands";
107+
let DiagnosticType = "InvalidConstpool";
108+
let ParserMethod = "parseConstpoolSymbol";
109+
}
110+
111+
def constpool_symbol : Operand<iPTR> {
112+
let ParserMatchClass = Constpool;
113+
let EncoderMethod =
114+
"getConstpoolSymbolOpValue<CSKY::fixup_csky_pcrel_uimm16_scale4>";
115+
}
116+
117+
def bare_symbol : Operand<iPTR> {
118+
let ParserMatchClass = CSKYSymbol;
119+
let EncoderMethod = "getBareSymbolOpValue";
120+
}
84121

85122
def oimm12 : oimm<12>;
86123
def oimm16 : oimm<16>;
@@ -229,3 +266,69 @@ def MOVIH32 : I_16_MOV<0x11, "movih32", uimm16_16_xform>;
229266

230267
def MVC32 : R_Z_1<0x1, 0x8, "mvc32">;
231268
def MVCV32 : R_Z_1<0x1, 0x10, "mvcv32">;
269+
270+
//===----------------------------------------------------------------------===//
271+
// Branch and call instructions.
272+
//===----------------------------------------------------------------------===//
273+
274+
let isBranch = 1, isTerminator = 1 in {
275+
let isBarrier = 1, isPredicable = 1 in
276+
def BR32 : I_16_L<0x0, (outs), (ins br_symbol:$imm16), "br32\t$imm16",
277+
[(br bb:$imm16)]>;
278+
279+
def BT32 : I_16_L<0x3, (outs), (ins CARRY:$ca, br_symbol:$imm16),
280+
"bt32\t$imm16", [(brcond CARRY:$ca, bb:$imm16)]>;
281+
def BF32 : I_16_L<0x2, (outs), (ins CARRY:$ca, br_symbol:$imm16),
282+
"bf32\t$imm16", []>;
283+
}
284+
285+
286+
def BEZ32 : I_16_X_L<0x8, "bez32", br_symbol>;
287+
def BNEZ32 : I_16_X_L<0x9, "bnez32", br_symbol>;
288+
def BHZ32 : I_16_X_L<0xA, "bhz32", br_symbol>;
289+
def BLSZ32 : I_16_X_L<0xB, "blsz32", br_symbol>;
290+
def BLZ32 : I_16_X_L<0xC, "blz32", br_symbol>;
291+
def BHSZ32 : I_16_X_L<0xD, "bhsz32", br_symbol>;
292+
293+
let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
294+
def JMP32 : I_16_JX<0x6, "jmp32", [(brind GPR:$rx)]>; // jmp to register
295+
def JMPI32 : I_16_L<0x16, (outs), (ins constpool_symbol:$imm16),
296+
"jmpi32\t$imm16", []>;
297+
}
298+
299+
let isCall = 1, Defs = [ R15 ] in
300+
def JSR32 : I_16_JX<0x7, "jsr32", []>;
301+
302+
let isCall = 1, Defs = [ R15 ] , mayLoad = 1 in
303+
def JSRI32: I_16_L<0x17, (outs),
304+
(ins constpool_symbol:$imm16), "jsri32\t$imm16", []>;
305+
306+
307+
def BSR32 : J<0x38, (outs), (ins call_symbol:$offset), "bsr32", []>;
308+
309+
def BSR32_BR : J<0x38, (outs), (ins call_symbol:$offset), "bsr32", []>{
310+
let isCodeGenOnly = 1;
311+
let isBranch = 1;
312+
let isTerminator = 1;
313+
let isBarrier = 1;
314+
let isPredicable = 1;
315+
let Defs = [ R15 ];
316+
}
317+
318+
def RTS32 : I_16_RET<0x6, 0xF, "rts32", [(CSKY_RET)]>;
319+
320+
321+
def RTE32 : I_16_RET_I<0, 0, "rte32", []>;
322+
323+
//===----------------------------------------------------------------------===//
324+
// Symbol address instructions.
325+
//===----------------------------------------------------------------------===//
326+
327+
def GRS32 : I_18_Z_L<0x3, "grs32\t$rz, $offset",
328+
(outs GPR:$rz), (ins bare_symbol:$offset), []>;
329+
330+
let mayLoad = 1, mayStore = 0 in {
331+
def LRW32 : I_16_Z_L<0x14, "lrw32", (ins constpool_symbol:$imm16), []>;
332+
let isCodeGenOnly = 1 in
333+
def LRW32_Gen : I_16_Z_L<0x14, "lrw32", (ins bare_symbol:$src1, constpool_symbol:$imm16), []>;
334+
}

‎llvm/lib/Target/CSKY/MCTargetDesc/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_llvm_component_library(LLVMCSKYDesc
33
CSKYELFObjectWriter.cpp
44
CSKYInstPrinter.cpp
55
CSKYMCAsmInfo.cpp
6+
CSKYMCExpr.cpp
67
CSKYMCTargetDesc.cpp
78
CSKYMCCodeEmitter.cpp
89

0 commit comments

Comments
 (0)