1 //===-- SystemZMCInstLower.cpp - Lower MachineInstr to MCInst -------------===//
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 #include "SystemZMCInstLower.h"
11 #include "SystemZAsmPrinter.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/Target/Mangler.h"
18 // Where relaxable pairs of reloc-generating instructions exist,
19 // we tend to use the longest form by default, since that produces
20 // correct assembly in cases where no relaxation is performed.
21 // If Opcode is one such instruction, return the opcode for the
22 // shortest possible form instead, otherwise return Opcode itself.
23 static unsigned getShortenedInstr(unsigned Opcode) {
25 case SystemZ::BRCL: return SystemZ::BRC;
26 case SystemZ::JG: return SystemZ::J;
27 case SystemZ::BRASL: return SystemZ::BRAS;
32 // Return the VK_* enumeration for MachineOperand target flags Flags.
33 static MCSymbolRefExpr::VariantKind getVariantKind(unsigned Flags) {
34 switch (Flags & SystemZII::MO_SYMBOL_MODIFIER) {
36 return MCSymbolRefExpr::VK_None;
37 case SystemZII::MO_GOT:
38 return MCSymbolRefExpr::VK_GOT;
40 llvm_unreachable("Unrecognised MO_ACCESS_MODEL");
43 SystemZMCInstLower::SystemZMCInstLower(Mangler *mang, MCContext &ctx,
44 SystemZAsmPrinter &asmprinter)
45 : Mang(mang), Ctx(ctx), AsmPrinter(asmprinter) {}
47 MCOperand SystemZMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
48 const MCSymbol *Symbol,
49 int64_t Offset) const {
50 MCSymbolRefExpr::VariantKind Kind = getVariantKind(MO.getTargetFlags());
51 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
53 const MCExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
54 Expr = MCBinaryExpr::CreateAdd(Expr, OffsetExpr, Ctx);
56 return MCOperand::CreateExpr(Expr);
59 MCOperand SystemZMCInstLower::lowerOperand(const MachineOperand &MO) const {
60 switch (MO.getType()) {
62 llvm_unreachable("unknown operand type");
64 case MachineOperand::MO_Register:
65 // Ignore all implicit register operands.
68 return MCOperand::CreateReg(MO.getReg());
70 case MachineOperand::MO_Immediate:
71 return MCOperand::CreateImm(MO.getImm());
73 case MachineOperand::MO_MachineBasicBlock:
74 return lowerSymbolOperand(MO, MO.getMBB()->getSymbol(),
75 /* MO has no offset field */0);
77 case MachineOperand::MO_GlobalAddress:
78 return lowerSymbolOperand(MO, Mang->getSymbol(MO.getGlobal()),
81 case MachineOperand::MO_ExternalSymbol: {
82 StringRef Name = MO.getSymbolName();
83 return lowerSymbolOperand(MO, AsmPrinter.GetExternalSymbolSymbol(Name),
87 case MachineOperand::MO_JumpTableIndex:
88 return lowerSymbolOperand(MO, AsmPrinter.GetJTISymbol(MO.getIndex()),
89 /* MO has no offset field */0);
91 case MachineOperand::MO_ConstantPoolIndex:
92 return lowerSymbolOperand(MO, AsmPrinter.GetCPISymbol(MO.getIndex()),
95 case MachineOperand::MO_BlockAddress: {
96 const BlockAddress *BA = MO.getBlockAddress();
97 return lowerSymbolOperand(MO, AsmPrinter.GetBlockAddressSymbol(BA),
103 void SystemZMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
104 unsigned Opcode = MI->getOpcode();
105 // When emitting binary code, start with the shortest form of an instruction
106 // and then relax it where necessary.
107 if (!AsmPrinter.OutStreamer.hasRawTextSupport())
108 Opcode = getShortenedInstr(Opcode);
109 OutMI.setOpcode(Opcode);
110 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
111 const MachineOperand &MO = MI->getOperand(I);
112 MCOperand MCOp = lowerOperand(MO);
114 OutMI.addOperand(MCOp);