xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/CSKY/CSKYAsmPrinter.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1 //===-- CSKYAsmPrinter.cpp - CSKY LLVM assembly writer --------------------===//
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 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to the CSKY assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "CSKYAsmPrinter.h"
14 #include "CSKY.h"
15 #include "CSKYTargetMachine.h"
16 #include "MCTargetDesc/CSKYInstPrinter.h"
17 #include "MCTargetDesc/CSKYMCExpr.h"
18 #include "TargetInfo/CSKYTargetInfo.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstBuilder.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/TargetRegistry.h"
28 
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "csky-asm-printer"
32 
33 STATISTIC(CSKYNumInstrsCompressed,
34           "Number of C-SKY Compressed instructions emitted");
35 
36 CSKYAsmPrinter::CSKYAsmPrinter(llvm::TargetMachine &TM,
37                                std::unique_ptr<llvm::MCStreamer> Streamer)
38     : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(OutContext, *this) {}
39 
40 bool CSKYAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
41   Subtarget = &MF.getSubtarget<CSKYSubtarget>();
42   return AsmPrinter::runOnMachineFunction(MF);
43 }
44 
45 #define GEN_COMPRESS_INSTR
46 #include "CSKYGenCompressInstEmitter.inc"
47 void CSKYAsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
48   MCInst CInst;
49   bool Res = compressInst(CInst, Inst, *Subtarget, OutStreamer->getContext());
50   if (Res)
51     ++CSKYNumInstrsCompressed;
52   AsmPrinter::EmitToStreamer(*OutStreamer, Res ? CInst : Inst);
53 }
54 
55 // Simple pseudo-instructions have their lowering (with expansion to real
56 // instructions) auto-generated.
57 #include "CSKYGenMCPseudoLowering.inc"
58 
59 void CSKYAsmPrinter::emitInstruction(const MachineInstr *MI) {
60   // Do any auto-generated pseudo lowerings.
61   if (emitPseudoExpansionLowering(*OutStreamer, MI))
62     return;
63 
64   MCInst TmpInst;
65   MCInstLowering.Lower(MI, TmpInst);
66   EmitToStreamer(*OutStreamer, TmpInst);
67 }
68 
69 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmPrinter() {
70   RegisterAsmPrinter<CSKYAsmPrinter> X(getTheCSKYTarget());
71 }
72