1 //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly printer -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Streams SystemZ assembly language and associated data, in the form of
11 // MCInsts and MCExprs respectively.
13 //===----------------------------------------------------------------------===//
15 #include "SystemZAsmPrinter.h"
16 #include "InstPrinter/SystemZInstPrinter.h"
17 #include "SystemZConstantPoolValue.h"
18 #include "SystemZMCInstLower.h"
19 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
20 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInstBuilder.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Target/Mangler.h"
29 // Return an RI instruction like MI with opcode Opcode, but with the
30 // GR64 register operands turned into GR32s.
31 static MCInst lowerRILow(const MachineInstr *MI, unsigned Opcode) {
32 return MCInstBuilder(Opcode)
33 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(0).getReg()))
34 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(1).getReg()))
35 .addImm(MI->getOperand(2).getImm());
38 // Return an RI instruction like MI with opcode Opcode, but with the
39 // GR64 register operands turned into GRH32s.
40 static MCInst lowerRIHigh(const MachineInstr *MI, unsigned Opcode) {
41 return MCInstBuilder(Opcode)
42 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(0).getReg()))
43 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(1).getReg()))
44 .addImm(MI->getOperand(2).getImm());
47 // Return an RI instruction like MI with opcode Opcode, but with the
48 // R2 register turned into a GR64.
49 static MCInst lowerRIEfLow(const MachineInstr *MI, unsigned Opcode) {
50 return MCInstBuilder(Opcode)
51 .addReg(MI->getOperand(0).getReg())
52 .addReg(MI->getOperand(1).getReg())
53 .addReg(SystemZMC::getRegAsGR64(MI->getOperand(2).getReg()))
54 .addImm(MI->getOperand(3).getImm())
55 .addImm(MI->getOperand(4).getImm())
56 .addImm(MI->getOperand(5).getImm());
59 void SystemZAsmPrinter::EmitInstruction(const MachineInstr *MI) {
60 SystemZMCInstLower Lower(MF->getContext(), *this);
62 switch (MI->getOpcode()) {
64 LoweredMI = MCInstBuilder(SystemZ::BR).addReg(SystemZ::R14D);
67 case SystemZ::CallBRASL:
68 LoweredMI = MCInstBuilder(SystemZ::BRASL)
69 .addReg(SystemZ::R14D)
70 .addExpr(Lower.getExpr(MI->getOperand(0), MCSymbolRefExpr::VK_PLT));
73 case SystemZ::CallBASR:
74 LoweredMI = MCInstBuilder(SystemZ::BASR)
75 .addReg(SystemZ::R14D)
76 .addReg(MI->getOperand(0).getReg());
80 LoweredMI = MCInstBuilder(SystemZ::JG)
81 .addExpr(Lower.getExpr(MI->getOperand(0), MCSymbolRefExpr::VK_PLT));
85 LoweredMI = MCInstBuilder(SystemZ::BR).addReg(SystemZ::R1D);
89 LoweredMI = MCInstBuilder(SystemZ::IILF)
90 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(0).getReg()))
91 .addImm(MI->getOperand(2).getImm());
95 LoweredMI = MCInstBuilder(SystemZ::IIHF)
96 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(0).getReg()))
97 .addImm(MI->getOperand(2).getImm());
100 case SystemZ::RISBHH:
101 case SystemZ::RISBHL:
102 LoweredMI = lowerRIEfLow(MI, SystemZ::RISBHG);
105 case SystemZ::RISBLH:
106 case SystemZ::RISBLL:
107 LoweredMI = lowerRIEfLow(MI, SystemZ::RISBLG);
110 #define LOWER_LOW(NAME) \
111 case SystemZ::NAME##64: LoweredMI = lowerRILow(MI, SystemZ::NAME); break
125 #define LOWER_HIGH(NAME) \
126 case SystemZ::NAME##64: LoweredMI = lowerRIHigh(MI, SystemZ::NAME); break
141 Lower.lower(MI, LoweredMI);
144 OutStreamer.EmitInstruction(LoweredMI);
147 // Convert a SystemZ-specific constant pool modifier into the associated
148 // MCSymbolRefExpr variant kind.
149 static MCSymbolRefExpr::VariantKind
150 getModifierVariantKind(SystemZCP::SystemZCPModifier Modifier) {
152 case SystemZCP::NTPOFF: return MCSymbolRefExpr::VK_NTPOFF;
154 llvm_unreachable("Invalid SystemCPModifier!");
157 void SystemZAsmPrinter::
158 EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
159 SystemZConstantPoolValue *ZCPV =
160 static_cast<SystemZConstantPoolValue*>(MCPV);
163 MCSymbolRefExpr::Create(getSymbol(ZCPV->getGlobalValue()),
164 getModifierVariantKind(ZCPV->getModifier()),
166 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(ZCPV->getType());
168 OutStreamer.EmitValue(Expr, Size);
171 bool SystemZAsmPrinter::PrintAsmOperand(const MachineInstr *MI,
174 const char *ExtraCode,
176 if (ExtraCode && *ExtraCode == 'n') {
177 if (!MI->getOperand(OpNo).isImm())
179 OS << -int64_t(MI->getOperand(OpNo).getImm());
181 SystemZMCInstLower Lower(MF->getContext(), *this);
182 MCOperand MO(Lower.lowerOperand(MI->getOperand(OpNo)));
183 SystemZInstPrinter::printOperand(MO, OS);
188 bool SystemZAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
191 const char *ExtraCode,
193 SystemZInstPrinter::printAddress(MI->getOperand(OpNo).getReg(),
194 MI->getOperand(OpNo + 1).getImm(),
195 MI->getOperand(OpNo + 2).getReg(), OS);
199 void SystemZAsmPrinter::EmitEndOfAsmFile(Module &M) {
200 if (Subtarget->isTargetELF()) {
201 const TargetLoweringObjectFileELF &TLOFELF =
202 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
204 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
206 // Output stubs for external and common global variables.
207 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
208 if (!Stubs.empty()) {
209 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
210 const DataLayout *TD = TM.getDataLayout();
212 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
213 OutStreamer.EmitLabel(Stubs[i].first);
214 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
215 TD->getPointerSize(0));
222 // Force static initialization.
223 extern "C" void LLVMInitializeSystemZAsmPrinter() {
224 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);