1// RUN: llvm-tblgen -gen-emitter -I %p/../../include %s | FileCheck %s
2
3// Check if VarLenCodeEmitterGen works correctly.
4
5include "llvm/Target/Target.td"
6
7def ArchInstrInfo : InstrInfo { }
8
9def Arch : Target {
10  let InstructionSet = ArchInstrInfo;
11}
12
13def Reg : Register<"reg">;
14
15def RegClass : RegisterClass<"foo", [i64], 0, (add Reg)>;
16
17def GR64 : RegisterOperand<RegClass>;
18
19class MyMemOperand<dag sub_ops> : Operand<iPTR> {
20  let MIOperandInfo = sub_ops;
21  dag Base;
22  dag Extension;
23}
24
25class MyVarInst<MyMemOperand memory_op> : Instruction {
26  dag Inst;
27
28  let OutOperandList = (outs GR64:$dst);
29  let InOperandList  = (ins memory_op:$src);
30
31  // Testing `ascend` and `descend`
32  let Inst = (ascend
33    (descend 0b10110111, memory_op.Base),
34    memory_op.Extension,
35    // Testing operand referencing.
36    (operand "$dst", 4),
37    // Testing operand referencing with a certain bit range.
38    (slice "$dst", 3, 1)
39  );
40}
41
42class MemOp16<string op_name> : MyMemOperand<(ops GR64:$reg, i16imm:$offset)> {
43  // Testing sub-operand referencing.
44  let Base = (operand "$"#op_name#".reg", 8);
45  let Extension = (operand "$"#op_name#".offset", 16);
46}
47
48class MemOp32<string op_name> : MyMemOperand<(ops GR64:$reg, i32imm:$offset)> {
49  let Base = (operand "$"#op_name#".reg", 8);
50  // Testing variable-length instruction encoding.
51  let Extension = (operand "$"#op_name#".offset", 32);
52}
53
54def FOO16 : MyVarInst<MemOp16<"src">>;
55def FOO32 : MyVarInst<MemOp32<"src">>;
56
57// The fixed bits part
58// CHECK: {/*NumBits*/39,
59// CHECK-SAME: // FOO16
60// CHECK: {/*NumBits*/55,
61// CHECK-SAME: // FOO32
62// CHECK: UINT64_C(46848), // FOO16
63// CHECK: UINT64_C(46848), // FOO32
64
65// CHECK-LABEL: case ::FOO16: {
66// CHECK: Scratch = Scratch.zextOrSelf(39);
67// src.reg
68// CHECK: getMachineOpValue(MI, MI.getOperand(1), Scratch, Fixups, STI);
69// CHECK: Inst.insertBits(Scratch.extractBits(8, 0), 0);
70// src.offset
71// CHECK: getMachineOpValue(MI, MI.getOperand(2), Scratch, Fixups, STI);
72// CHECK: Inst.insertBits(Scratch.extractBits(16, 0), 16);
73// 1st dst
74// CHECK: getMachineOpValue(MI, MI.getOperand(0), Scratch, Fixups, STI);
75// CHECK: Inst.insertBits(Scratch.extractBits(4, 0), 32);
76// 2nd dst
77// CHECK: getMachineOpValue(MI, MI.getOperand(0), Scratch, Fixups, STI);
78// CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 36);
79
80// CHECK-LABEL: case ::FOO32: {
81// CHECK: Scratch = Scratch.zextOrSelf(55);
82// src.reg
83// CHECK: getMachineOpValue(MI, MI.getOperand(1), Scratch, Fixups, STI);
84// CHECK: Inst.insertBits(Scratch.extractBits(8, 0), 0);
85// src.offset
86// CHECK: getMachineOpValue(MI, MI.getOperand(2), Scratch, Fixups, STI);
87// CHECK: Inst.insertBits(Scratch.extractBits(32, 0), 16);
88// 1st dst
89// CHECK: getMachineOpValue(MI, MI.getOperand(0), Scratch, Fixups, STI);
90// CHECK: Inst.insertBits(Scratch.extractBits(4, 0), 48);
91// 2nd dst
92// CHECK: getMachineOpValue(MI, MI.getOperand(0), Scratch, Fixups, STI);
93// CHECK: Inst.insertBits(Scratch.extractBits(3, 1), 52);
94