[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / Target / XCore / XCoreAsmPrinter.cpp
blob6b3dc27cb88695763fe072cf2eb0b4c39c43a826
1 //===-- XCoreAsmPrinter.cpp - XCore 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 XAS-format XCore assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/XCoreInstPrinter.h"
15 #include "TargetInfo/XCoreTargetInfo.h"
16 #include "XCore.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreMCInstLower.h"
19 #include "XCoreSubtarget.h"
20 #include "XCoreTargetMachine.h"
21 #include "XCoreTargetStreamer.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineJumpTableInfo.h"
29 #include "llvm/CodeGen/MachineModuleInfo.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCExpr.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbolELF.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Target/TargetLoweringObjectFile.h"
45 #include <algorithm>
46 #include <cctype>
47 using namespace llvm;
49 #define DEBUG_TYPE "asm-printer"
51 namespace {
52 class XCoreAsmPrinter : public AsmPrinter {
53 XCoreMCInstLower MCInstLowering;
54 XCoreTargetStreamer &getTargetStreamer();
56 public:
57 explicit XCoreAsmPrinter(TargetMachine &TM,
58 std::unique_ptr<MCStreamer> Streamer)
59 : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
61 StringRef getPassName() const override { return "XCore Assembly Printer"; }
63 void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
64 const std::string &directive = ".jmptable");
65 void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
66 printInlineJT(MI, opNum, O, ".jmptable32");
68 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
69 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 const char *ExtraCode, raw_ostream &O) override;
71 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
72 const char *ExtraCode, raw_ostream &O) override;
74 void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
75 void EmitGlobalVariable(const GlobalVariable *GV) override;
77 void EmitFunctionEntryLabel() override;
78 void EmitInstruction(const MachineInstr *MI) override;
79 void EmitFunctionBodyStart() override;
80 void EmitFunctionBodyEnd() override;
82 } // end of anonymous namespace
84 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
85 return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
88 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
89 assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
90 GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
91 "Unexpected linkage");
92 if (ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType())) {
94 MCSymbol *SymGlob = OutContext.getOrCreateSymbol(
95 Twine(Sym->getName() + StringRef(".globound")));
96 OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Global);
97 OutStreamer->EmitAssignment(SymGlob,
98 MCConstantExpr::create(ATy->getNumElements(),
99 OutContext));
100 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
101 GV->hasCommonLinkage()) {
102 OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Weak);
107 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
108 // Check to see if this is a special global used by LLVM, if so, emit it.
109 if (!GV->hasInitializer() ||
110 EmitSpecialLLVMGlobal(GV))
111 return;
113 const DataLayout &DL = getDataLayout();
114 OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(GV, TM));
116 MCSymbol *GVSym = getSymbol(GV);
117 const Constant *C = GV->getInitializer();
118 const Align Alignment(DL.getPrefTypeAlignment(C->getType()));
120 // Mark the start of the global
121 getTargetStreamer().emitCCTopData(GVSym->getName());
123 switch (GV->getLinkage()) {
124 case GlobalValue::AppendingLinkage:
125 report_fatal_error("AppendingLinkage is not supported by this target!");
126 case GlobalValue::LinkOnceAnyLinkage:
127 case GlobalValue::LinkOnceODRLinkage:
128 case GlobalValue::WeakAnyLinkage:
129 case GlobalValue::WeakODRLinkage:
130 case GlobalValue::ExternalLinkage:
131 case GlobalValue::CommonLinkage:
132 emitArrayBound(GVSym, GV);
133 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
135 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
136 GV->hasCommonLinkage())
137 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
138 LLVM_FALLTHROUGH;
139 case GlobalValue::InternalLinkage:
140 case GlobalValue::PrivateLinkage:
141 break;
142 default:
143 llvm_unreachable("Unknown linkage type!");
146 EmitAlignment(std::max(Alignment, Align(4)), GV);
148 if (GV->isThreadLocal()) {
149 report_fatal_error("TLS is not supported by this target!");
151 unsigned Size = DL.getTypeAllocSize(C->getType());
152 if (MAI->hasDotTypeDotSizeDirective()) {
153 OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
154 OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
156 OutStreamer->EmitLabel(GVSym);
158 EmitGlobalConstant(DL, C);
159 // The ABI requires that unsigned scalar types smaller than 32 bits
160 // are padded to 32 bits.
161 if (Size < 4)
162 OutStreamer->EmitZeros(4 - Size);
164 // Mark the end of the global
165 getTargetStreamer().emitCCBottomData(GVSym->getName());
168 void XCoreAsmPrinter::EmitFunctionBodyStart() {
169 MCInstLowering.Initialize(&MF->getContext());
172 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
173 /// the last basic block in the function.
174 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
175 // Emit function end directives
176 getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
179 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
180 // Mark the start of the function
181 getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
182 OutStreamer->EmitLabel(CurrentFnSym);
185 void XCoreAsmPrinter::
186 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
187 const std::string &directive) {
188 unsigned JTI = MI->getOperand(opNum).getIndex();
189 const MachineFunction *MF = MI->getParent()->getParent();
190 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
191 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
192 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
193 O << "\t" << directive << " ";
194 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
195 MachineBasicBlock *MBB = JTBBs[i];
196 if (i > 0)
197 O << ",";
198 MBB->getSymbol()->print(O, MAI);
202 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
203 raw_ostream &O) {
204 const DataLayout &DL = getDataLayout();
205 const MachineOperand &MO = MI->getOperand(opNum);
206 switch (MO.getType()) {
207 case MachineOperand::MO_Register:
208 O << XCoreInstPrinter::getRegisterName(MO.getReg());
209 break;
210 case MachineOperand::MO_Immediate:
211 O << MO.getImm();
212 break;
213 case MachineOperand::MO_MachineBasicBlock:
214 MO.getMBB()->getSymbol()->print(O, MAI);
215 break;
216 case MachineOperand::MO_GlobalAddress:
217 PrintSymbolOperand(MO, O);
218 break;
219 case MachineOperand::MO_ConstantPoolIndex:
220 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
221 << MO.getIndex();
222 break;
223 case MachineOperand::MO_BlockAddress:
224 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
225 break;
226 default:
227 llvm_unreachable("not implemented");
231 /// PrintAsmOperand - Print out an operand for an inline asm expression.
233 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
234 const char *ExtraCode, raw_ostream &O) {
235 // Print the operand if there is no operand modifier.
236 if (!ExtraCode || !ExtraCode[0]) {
237 printOperand(MI, OpNo, O);
238 return false;
241 // Otherwise fallback on the default implementation.
242 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
245 bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
246 unsigned OpNum,
247 const char *ExtraCode,
248 raw_ostream &O) {
249 if (ExtraCode && ExtraCode[0]) {
250 return true; // Unknown modifier.
252 printOperand(MI, OpNum, O);
253 O << '[';
254 printOperand(MI, OpNum + 1, O);
255 O << ']';
256 return false;
259 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
260 SmallString<128> Str;
261 raw_svector_ostream O(Str);
263 switch (MI->getOpcode()) {
264 case XCore::DBG_VALUE:
265 llvm_unreachable("Should be handled target independently");
266 case XCore::ADD_2rus:
267 if (MI->getOperand(2).getImm() == 0) {
268 O << "\tmov "
269 << XCoreInstPrinter::getRegisterName(MI->getOperand(0).getReg()) << ", "
270 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg());
271 OutStreamer->EmitRawText(O.str());
272 return;
274 break;
275 case XCore::BR_JT:
276 case XCore::BR_JT32:
277 O << "\tbru "
278 << XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg()) << '\n';
279 if (MI->getOpcode() == XCore::BR_JT)
280 printInlineJT(MI, 0, O);
281 else
282 printInlineJT32(MI, 0, O);
283 O << '\n';
284 OutStreamer->EmitRawText(O.str());
285 return;
288 MCInst TmpInst;
289 MCInstLowering.Lower(MI, TmpInst);
291 EmitToStreamer(*OutStreamer, TmpInst);
294 // Force static initialization.
295 extern "C" void LLVMInitializeXCoreAsmPrinter() {
296 RegisterAsmPrinter<XCoreAsmPrinter> X(getTheXCoreTarget());