-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy path0008-RISCV-Add-RISCVInstPrinter-and-basic-MC-assembler-te.patch
431 lines (420 loc) · 15.3 KB
/
0008-RISCV-Add-RISCVInstPrinter-and-basic-MC-assembler-te.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alex Bradbury <asb@lowrisc.org>
Subject: [RISCV] Add RISCVInstPrinter and basic MC assembler tests
Differential Revision: https://reviews.llvm.org/D23564
---
lib/Target/RISCV/CMakeLists.txt | 4 +-
lib/Target/RISCV/InstPrinter/CMakeLists.txt | 3 ++
lib/Target/RISCV/InstPrinter/LLVMBuild.txt | 23 ++++++++
lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp | 55 +++++++++++++++++++
lib/Target/RISCV/InstPrinter/RISCVInstPrinter.h | 43 +++++++++++++++
lib/Target/RISCV/LLVMBuild.txt | 5 +-
lib/Target/RISCV/MCTargetDesc/LLVMBuild.txt | 2 +-
.../RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp | 10 ++++
test/MC/RISCV/elf-header.s | 42 +++++++++++++++
test/MC/RISCV/lit.local.cfg | 3 ++
test/MC/RISCV/rv32i-invalid.s | 30 +++++++++++
test/MC/RISCV/rv32i-valid.s | 63 ++++++++++++++++++++++
12 files changed, 279 insertions(+), 4 deletions(-)
create mode 100644 lib/Target/RISCV/InstPrinter/CMakeLists.txt
create mode 100644 lib/Target/RISCV/InstPrinter/LLVMBuild.txt
create mode 100644 lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp
create mode 100644 lib/Target/RISCV/InstPrinter/RISCVInstPrinter.h
create mode 100644 test/MC/RISCV/elf-header.s
create mode 100644 test/MC/RISCV/lit.local.cfg
create mode 100644 test/MC/RISCV/rv32i-invalid.s
create mode 100644 test/MC/RISCV/rv32i-valid.s
diff --git a/lib/Target/RISCV/CMakeLists.txt b/lib/Target/RISCV/CMakeLists.txt
index 47e53a36722..6cc55fd3a17 100644
--- a/lib/Target/RISCV/CMakeLists.txt
+++ b/lib/Target/RISCV/CMakeLists.txt
@@ -4,6 +4,7 @@ tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM RISCVGenAsmMatcher.inc -gen-asm-matcher)
+tablegen(LLVM RISCVGenAsmWriter.inc -gen-asm-writer)
add_public_tablegen_target(RISCVCommonTableGen)
@@ -12,5 +13,6 @@ add_llvm_target(RISCVCodeGen
)
add_subdirectory(AsmParser)
-add_subdirectory(TargetInfo)
+add_subdirectory(InstPrinter)
add_subdirectory(MCTargetDesc)
+add_subdirectory(TargetInfo)
diff --git a/lib/Target/RISCV/InstPrinter/CMakeLists.txt b/lib/Target/RISCV/InstPrinter/CMakeLists.txt
new file mode 100644
index 00000000000..7f91743fb93
--- /dev/null
+++ b/lib/Target/RISCV/InstPrinter/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_llvm_library(LLVMRISCVAsmPrinter
+ RISCVInstPrinter.cpp
+ )
diff --git a/lib/Target/RISCV/InstPrinter/LLVMBuild.txt b/lib/Target/RISCV/InstPrinter/LLVMBuild.txt
new file mode 100644
index 00000000000..5f4545e3d67
--- /dev/null
+++ b/lib/Target/RISCV/InstPrinter/LLVMBuild.txt
@@ -0,0 +1,23 @@
+;===- ./lib/Target/RISCV/InstPrinter/LLVMBuild.txt -------------*- Conf -*--===;
+;
+; The LLVM Compiler Infrastructure
+;
+; This file is distributed under the University of Illinois Open Source
+; License. See LICENSE.TXT for details.
+;
+;===------------------------------------------------------------------------===;
+;
+; This is an LLVMBuild description file for the components in this subdirectory.
+;
+; For more information on the LLVMBuild system, please see:
+;
+; http://llvm.org/docs/LLVMBuild.html
+;
+;===------------------------------------------------------------------------===;
+
+[component_0]
+type = Library
+name = RISCVAsmPrinter
+parent = RISCV
+required_libraries = MC Support
+add_to_library_groups = RISCV
diff --git a/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp b/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp
new file mode 100644
index 00000000000..e55658e3968
--- /dev/null
+++ b/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.cpp
@@ -0,0 +1,55 @@
+//===-- RISCVInstPrinter.cpp - Convert RISCV MCInst to asm syntax ---------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This class prints an RISCV MCInst to a .s file.
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCVInstPrinter.h"
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCSymbol.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FormattedStream.h"
+using namespace llvm;
+
+#define DEBUG_TYPE "asm-printer"
+
+// Include the auto-generated portion of the assembly writer.
+#include "RISCVGenAsmWriter.inc"
+
+void RISCVInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
+ StringRef Annot, const MCSubtargetInfo &STI) {
+ printInstruction(MI, O);
+ printAnnotation(O, Annot);
+}
+
+void RISCVInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
+ O << getRegisterName(RegNo);
+}
+
+void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
+ raw_ostream &O, const char *Modifier) {
+ assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+ const MCOperand &MO = MI->getOperand(OpNo);
+
+ if (MO.isReg()) {
+ printRegName(O, MO.getReg());
+ return;
+ }
+
+ if (MO.isImm()) {
+ O << MO.getImm();
+ return;
+ }
+
+ assert(MO.isExpr() && "Unknown operand kind in printOperand");
+ MO.getExpr()->print(O, &MAI);
+}
diff --git a/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.h b/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.h
new file mode 100644
index 00000000000..f378c6f18da
--- /dev/null
+++ b/lib/Target/RISCV/InstPrinter/RISCVInstPrinter.h
@@ -0,0 +1,43 @@
+//===-- RISCVInstPrinter.h - Convert RISCV MCInst to asm syntax ---*- C++ -*--//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This class prints a RISCV MCInst to a .s file.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_RISCV_INSTPRINTER_RISCVINSTPRINTER_H
+#define LLVM_LIB_TARGET_RISCV_INSTPRINTER_RISCVINSTPRINTER_H
+
+#include "MCTargetDesc/RISCVMCTargetDesc.h"
+#include "llvm/MC/MCInstPrinter.h"
+
+namespace llvm {
+class MCOperand;
+
+class RISCVInstPrinter : public MCInstPrinter {
+public:
+ RISCVInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
+ const MCRegisterInfo &MRI)
+ : MCInstPrinter(MAI, MII, MRI) {}
+
+ void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot,
+ const MCSubtargetInfo &STI) override;
+ void printRegName(raw_ostream &O, unsigned RegNo) const override;
+
+ void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
+ const char *Modifier = nullptr);
+
+ // Autogenerated by tblgen.
+ void printInstruction(const MCInst *MI, raw_ostream &O);
+ static const char *getRegisterName(unsigned RegNo,
+ unsigned AltIdx = RISCV::ABIRegAltName);
+};
+}
+
+#endif
diff --git a/lib/Target/RISCV/LLVMBuild.txt b/lib/Target/RISCV/LLVMBuild.txt
index 3e540048921..933fed1856b 100644
--- a/lib/Target/RISCV/LLVMBuild.txt
+++ b/lib/Target/RISCV/LLVMBuild.txt
@@ -16,17 +16,18 @@
;===------------------------------------------------------------------------===;
[common]
-subdirectories = AsmParser TargetInfo MCTargetDesc
+subdirectories = AsmParser InstPrinter TargetInfo MCTargetDesc
[component_0]
type = TargetGroup
name = RISCV
parent = Target
has_asmparser = 1
+has_asmprinter = 1
[component_1]
type = Library
name = RISCVCodeGen
parent = RISCV
-required_libraries = Core CodeGen RISCVInfo Support Target
+required_libraries = AsmPrinter Core CodeGen MC RISCVAsmPrinter RISCVDesc RISCVInfo Support Target
add_to_library_groups = RISCV
diff --git a/lib/Target/RISCV/MCTargetDesc/LLVMBuild.txt b/lib/Target/RISCV/MCTargetDesc/LLVMBuild.txt
index e48f04963ff..92daae30e3f 100644
--- a/lib/Target/RISCV/MCTargetDesc/LLVMBuild.txt
+++ b/lib/Target/RISCV/MCTargetDesc/LLVMBuild.txt
@@ -19,5 +19,5 @@
type = Library
name = RISCVDesc
parent = RISCV
-required_libraries = MC RISCVInfo Support
+required_libraries = MC RISCVAsmPrinter RISCVInfo Support
add_to_library_groups = RISCV
diff --git a/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
index bad5f440061..72485c5cd08 100644
--- a/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
+++ b/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "RISCVMCTargetDesc.h"
+#include "InstPrinter/RISCVInstPrinter.h"
#include "RISCVMCAsmInfo.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/MC/MCAsmInfo.h"
@@ -47,6 +48,14 @@ static MCAsmInfo *createRISCVMCAsmInfo(const MCRegisterInfo &MRI,
return new RISCVMCAsmInfo(TT);
}
+static MCInstPrinter *createRISCVMCInstPrinter(const Triple &T,
+ unsigned SyntaxVariant,
+ const MCAsmInfo &MAI,
+ const MCInstrInfo &MII,
+ const MCRegisterInfo &MRI) {
+ return new RISCVInstPrinter(MAI, MII, MRI);
+}
+
extern "C" void LLVMInitializeRISCVTargetMC() {
for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) {
TargetRegistry::RegisterMCAsmInfo(*T, createRISCVMCAsmInfo);
@@ -54,5 +63,6 @@ extern "C" void LLVMInitializeRISCVTargetMC() {
TargetRegistry::RegisterMCRegInfo(*T, createRISCVMCRegisterInfo);
TargetRegistry::RegisterMCAsmBackend(*T, createRISCVAsmBackend);
TargetRegistry::RegisterMCCodeEmitter(*T, createRISCVMCCodeEmitter);
+ TargetRegistry::RegisterMCInstPrinter(*T, createRISCVMCInstPrinter);
}
}
diff --git a/test/MC/RISCV/elf-header.s b/test/MC/RISCV/elf-header.s
new file mode 100644
index 00000000000..29a7036f2fc
--- /dev/null
+++ b/test/MC/RISCV/elf-header.s
@@ -0,0 +1,42 @@
+# RUN: llvm-mc %s -filetype=obj -triple=riscv32 | llvm-readobj -h \
+# RUN: | FileCheck -check-prefix=RV32 %s
+# RUN: llvm-mc %s -filetype=obj -triple=riscv64 | llvm-readobj -h \
+# RUN: | FileCheck -check-prefix=RV64 %s
+
+# RV32: Format: ELF32-riscv
+# RV32: Arch: riscv32
+# RV32: AddressSize: 32bit
+# RV32: ElfHeader {
+# RV32: Ident {
+# RV32: Magic: (7F 45 4C 46)
+# RV32: Class: 32-bit (0x1)
+# RV32: DataEncoding: LittleEndian (0x1)
+# RV32: FileVersion: 1
+# RV32: OS/ABI: SystemV (0x0)
+# RV32: ABIVersion: 0
+# RV32: }
+# RV32: Type: Relocatable (0x1)
+# RV32: Machine: EM_RISCV (0xF3)
+# RV32: Version: 1
+# RV32: Flags [ (0x0)
+# RV32: ]
+# RV32: }
+
+# RV64: Format: ELF64-riscv
+# RV64: Arch: riscv64
+# RV64: AddressSize: 64bit
+# RV64: ElfHeader {
+# RV64: Ident {
+# RV64: Magic: (7F 45 4C 46)
+# RV64: Class: 64-bit (0x2)
+# RV64: DataEncoding: LittleEndian (0x1)
+# RV64: FileVersion: 1
+# RV64: OS/ABI: SystemV (0x0)
+# RV64: ABIVersion: 0
+# RV64: }
+# RV64: Type: Relocatable (0x1)
+# RV64: Machine: EM_RISCV (0xF3)
+# RV64: Version: 1
+# RV64: Flags [ (0x0)
+# RV64: ]
+# RV64: }
diff --git a/test/MC/RISCV/lit.local.cfg b/test/MC/RISCV/lit.local.cfg
new file mode 100644
index 00000000000..d0b081e3e8b
--- /dev/null
+++ b/test/MC/RISCV/lit.local.cfg
@@ -0,0 +1,3 @@
+if not 'RISCV' in config.root.targets:
+ config.unsupported = True
+
diff --git a/test/MC/RISCV/rv32i-invalid.s b/test/MC/RISCV/rv32i-invalid.s
new file mode 100644
index 00000000000..d6a6de7a278
--- /dev/null
+++ b/test/MC/RISCV/rv32i-invalid.s
@@ -0,0 +1,30 @@
+# RUN: not llvm-mc -triple riscv32 < %s 2>&1 | FileCheck %s
+
+# Out of range immediates
+ori a0, a1, -2049 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [-2048, 2047]
+andi ra, sp, 2048 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [-2048, 2047]
+
+# Invalid mnemonics
+subs t0, t2, t1 # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
+nandi t0, zero, 0 # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
+
+# Invalid register names
+addi foo, sp, 10 # CHECK: :[[@LINE]]:6: error: unknown operand
+slti a10, a2, 0x20 # CHECK: :[[@LINE]]:6: error: unknown operand
+slt x32, s0, s0 # CHECK: :[[@LINE]]:5: error: unknown operand
+
+# RV64I mnemonics
+addiw a0, sp, 100 # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
+sraw t0, s2, zero # CHECK: :[[@LINE]]:1: error: unrecognized instruction mnemonic
+
+# Invalid operand types
+xori sp, 22, 220 # CHECK: :[[@LINE]]:10: error: invalid operand for instruction
+sub t0, t2, 1 # CHECK: :[[@LINE]]:13: error: invalid operand for instruction
+
+# Too many operands
+add ra, zero, zero, zero # CHECK: :[[@LINE]]:21: error: invalid operand for instruction
+sltiu s2, s3, 0x50, 0x60 # CHECK: :[[@LINE]]:21: error: invalid operand for instruction
+
+# Too few operands
+ori a0, a1 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
+xor s2, s2 # CHECK: :[[@LINE]]:1: error: too few operands for instruction
diff --git a/test/MC/RISCV/rv32i-valid.s b/test/MC/RISCV/rv32i-valid.s
new file mode 100644
index 00000000000..fc89cd42016
--- /dev/null
+++ b/test/MC/RISCV/rv32i-valid.s
@@ -0,0 +1,63 @@
+# RUN: llvm-mc %s -triple=riscv32 -show-encoding \
+# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
+# RUN: llvm-mc %s -triple=riscv64 -show-encoding \
+# RUN: | FileCheck -check-prefixes=CHECK,CHECK-INST %s
+
+# CHECK-INST: addi ra, sp, 2
+# CHECK: encoding: [0x93,0x00,0x21,0x00]
+addi ra, sp, 2
+# CHECK-INST: slti a0, a2, -20
+# CHECK: encoding: [0x13,0x25,0xc6,0xfe]
+slti a0, a2, -20
+# CHECK-INST: sltiu s2, s3, 80
+# CHECK: encoding: [0x13,0xb9,0x09,0x05]
+sltiu s2, s3, 0x50
+# CHECK-INST: xori tp, t1, -99
+# CHECK: encoding: [0x13,0x42,0xd3,0xf9]
+xori tp, t1, -99
+# CHECK-INST: ori a0, a1, -2048
+# CHECK: encoding: [0x13,0xe5,0x05,0x80]
+ori a0, a1, -2048
+# CHECK-INST: andi ra, sp, 2047
+# CHECK: encoding: [0x93,0x70,0xf1,0x7f]
+andi ra, sp, 2047
+# CHECK-INST: andi ra, sp, 2047
+# CHECK: encoding: [0x93,0x70,0xf1,0x7f]
+andi x1, x2, 2047
+
+# CHECK-INST: add ra, zero, zero
+# CHECK: encoding: [0xb3,0x00,0x00,0x00]
+add ra, zero, zero
+# CHECK-INST: add ra, zero, zero
+# CHECK: encoding: [0xb3,0x00,0x00,0x00]
+add x1, x0, x0
+# CHECK-INST: sub t0, t2, t1
+# CHECK: encoding: [0xb3,0x82,0x63,0x40]
+sub t0, t2, t1
+# CHECK-INST: sll a5, a4, a3
+# CHECK: encoding: [0xb3,0x17,0xd7,0x00]
+sll a5, a4, a3
+# CHECK-INST: slt s0, s0, s0
+# CHECK: encoding: [0x33,0x24,0x84,0x00]
+slt s0, s0, s0
+# CHECK-INST: sltu gp, a0, a1
+# CHECK: encoding: [0xb3,0x31,0xb5,0x00]
+sltu gp, a0, a1
+# CHECK-INST: xor s2, s2, s8
+# CHECK: encoding: [0x33,0x49,0x89,0x01]
+xor s2, s2, s8
+# CHECK-INST: xor s2, s2, s8
+# CHECK: encoding: [0x33,0x49,0x89,0x01]
+xor x18, x18, x24
+# CHECK-INST: srl a0, s0, t0
+# CHECK: encoding: [0x33,0x55,0x54,0x00]
+srl a0, s0, t0
+# CHECK-INST: sra t0, s2, zero
+# CHECK: encoding: [0xb3,0x52,0x09,0x40]
+sra t0, s2, zero
+# CHECK-INST: or s10, t1, ra
+# CHECK: encoding: [0x33,0x6d,0x13,0x00]
+or s10, t1, ra
+# CHECK-INST: and a0, s2, s3
+# CHECK: encoding: [0x33,0x75,0x39,0x01]
+and a0, s2, s3
--
2.16.2