blob: ff9a6c0a221f8670bdac3a99aec1b0328a43bc92 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZMCInstLower.cpp - Lower MachineInstr to MCInst -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
15
16using namespace llvm;
17
Ulrich Weigand5f613df2013-05-06 16:15:19 +000018// Return the VK_* enumeration for MachineOperand target flags Flags.
19static MCSymbolRefExpr::VariantKind getVariantKind(unsigned Flags) {
20 switch (Flags & SystemZII::MO_SYMBOL_MODIFIER) {
21 case 0:
22 return MCSymbolRefExpr::VK_None;
23 case SystemZII::MO_GOT:
24 return MCSymbolRefExpr::VK_GOT;
25 }
26 llvm_unreachable("Unrecognised MO_ACCESS_MODEL");
27}
28
Rafael Espindola69c1d632013-10-29 16:18:15 +000029SystemZMCInstLower::SystemZMCInstLower(MCContext &ctx,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000030 SystemZAsmPrinter &asmprinter)
Rafael Espindola69c1d632013-10-29 16:18:15 +000031 : Ctx(ctx), AsmPrinter(asmprinter) {}
Ulrich Weigand5f613df2013-05-06 16:15:19 +000032
Richard Sandifordf348f832013-09-25 10:37:17 +000033const MCExpr *
34SystemZMCInstLower::getExpr(const MachineOperand &MO,
35 MCSymbolRefExpr::VariantKind Kind) const {
36 const MCSymbol *Symbol;
37 bool HasOffset = true;
38 switch (MO.getType()) {
39 case MachineOperand::MO_MachineBasicBlock:
40 Symbol = MO.getMBB()->getSymbol();
41 HasOffset = false;
42 break;
43
44 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +000045 Symbol = AsmPrinter.getSymbol(MO.getGlobal());
Richard Sandifordf348f832013-09-25 10:37:17 +000046 break;
47
48 case MachineOperand::MO_ExternalSymbol:
49 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
50 break;
51
52 case MachineOperand::MO_JumpTableIndex:
53 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
54 HasOffset = false;
55 break;
56
57 case MachineOperand::MO_ConstantPoolIndex:
58 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
59 break;
60
61 case MachineOperand::MO_BlockAddress:
62 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
63 break;
64
65 default:
66 llvm_unreachable("unknown operand type");
Ulrich Weigand5f613df2013-05-06 16:15:19 +000067 }
Richard Sandifordf348f832013-09-25 10:37:17 +000068 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
69 if (HasOffset)
70 if (int64_t Offset = MO.getOffset()) {
71 const MCExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
72 Expr = MCBinaryExpr::CreateAdd(Expr, OffsetExpr, Ctx);
73 }
74 return Expr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000075}
76
77MCOperand SystemZMCInstLower::lowerOperand(const MachineOperand &MO) const {
78 switch (MO.getType()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000079 case MachineOperand::MO_Register:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000080 return MCOperand::CreateReg(MO.getReg());
81
82 case MachineOperand::MO_Immediate:
83 return MCOperand::CreateImm(MO.getImm());
84
Richard Sandifordf348f832013-09-25 10:37:17 +000085 default: {
86 MCSymbolRefExpr::VariantKind Kind = getVariantKind(MO.getTargetFlags());
87 return MCOperand::CreateExpr(getExpr(MO, Kind));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000088 }
89 }
90}
91
92void SystemZMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
Richard Sandifordf348f832013-09-25 10:37:17 +000093 OutMI.setOpcode(MI->getOpcode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000094 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
95 const MachineOperand &MO = MI->getOperand(I);
Richard Sandifordb8204052013-07-12 09:08:12 +000096 // Ignore all implicit register operands.
97 if (!MO.isReg() || !MO.isImplicit())
98 OutMI.addOperand(lowerOperand(MO));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000099 }
100}