blob: 6bb96f1b40f286e6a87ef1e942febbaca337dd04 [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"
Rafael Espindola894843c2014-01-07 21:19:40 +000012#include "llvm/IR/Mangler.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000013#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCStreamer.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000015
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;
Ulrich Weigand7db69182015-02-18 09:13:27 +000025 case SystemZII::MO_INDNTPOFF:
26 return MCSymbolRefExpr::VK_INDNTPOFF;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000027 }
28 llvm_unreachable("Unrecognised MO_ACCESS_MODEL");
29}
30
Rafael Espindola69c1d632013-10-29 16:18:15 +000031SystemZMCInstLower::SystemZMCInstLower(MCContext &ctx,
Ulrich Weigand5f613df2013-05-06 16:15:19 +000032 SystemZAsmPrinter &asmprinter)
Rafael Espindola69c1d632013-10-29 16:18:15 +000033 : Ctx(ctx), AsmPrinter(asmprinter) {}
Ulrich Weigand5f613df2013-05-06 16:15:19 +000034
Richard Sandifordf348f832013-09-25 10:37:17 +000035const MCExpr *
36SystemZMCInstLower::getExpr(const MachineOperand &MO,
37 MCSymbolRefExpr::VariantKind Kind) const {
38 const MCSymbol *Symbol;
39 bool HasOffset = true;
40 switch (MO.getType()) {
41 case MachineOperand::MO_MachineBasicBlock:
42 Symbol = MO.getMBB()->getSymbol();
43 HasOffset = false;
44 break;
45
46 case MachineOperand::MO_GlobalAddress:
Rafael Espindola79858aa2013-10-29 17:07:16 +000047 Symbol = AsmPrinter.getSymbol(MO.getGlobal());
Richard Sandifordf348f832013-09-25 10:37:17 +000048 break;
49
50 case MachineOperand::MO_ExternalSymbol:
51 Symbol = AsmPrinter.GetExternalSymbolSymbol(MO.getSymbolName());
52 break;
53
54 case MachineOperand::MO_JumpTableIndex:
55 Symbol = AsmPrinter.GetJTISymbol(MO.getIndex());
56 HasOffset = false;
57 break;
58
59 case MachineOperand::MO_ConstantPoolIndex:
60 Symbol = AsmPrinter.GetCPISymbol(MO.getIndex());
61 break;
62
63 case MachineOperand::MO_BlockAddress:
64 Symbol = AsmPrinter.GetBlockAddressSymbol(MO.getBlockAddress());
65 break;
66
67 default:
68 llvm_unreachable("unknown operand type");
Ulrich Weigand5f613df2013-05-06 16:15:19 +000069 }
Richard Sandifordf348f832013-09-25 10:37:17 +000070 const MCExpr *Expr = MCSymbolRefExpr::Create(Symbol, Kind, Ctx);
71 if (HasOffset)
72 if (int64_t Offset = MO.getOffset()) {
73 const MCExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
74 Expr = MCBinaryExpr::CreateAdd(Expr, OffsetExpr, Ctx);
75 }
76 return Expr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000077}
78
79MCOperand SystemZMCInstLower::lowerOperand(const MachineOperand &MO) const {
80 switch (MO.getType()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000081 case MachineOperand::MO_Register:
Ulrich Weigand5f613df2013-05-06 16:15:19 +000082 return MCOperand::CreateReg(MO.getReg());
83
84 case MachineOperand::MO_Immediate:
85 return MCOperand::CreateImm(MO.getImm());
86
Richard Sandifordf348f832013-09-25 10:37:17 +000087 default: {
88 MCSymbolRefExpr::VariantKind Kind = getVariantKind(MO.getTargetFlags());
89 return MCOperand::CreateExpr(getExpr(MO, Kind));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090 }
91 }
92}
93
94void SystemZMCInstLower::lower(const MachineInstr *MI, MCInst &OutMI) const {
Richard Sandifordf348f832013-09-25 10:37:17 +000095 OutMI.setOpcode(MI->getOpcode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000096 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
97 const MachineOperand &MO = MI->getOperand(I);
Richard Sandifordb8204052013-07-12 09:08:12 +000098 // Ignore all implicit register operands.
99 if (!MO.isReg() || !MO.isImplicit())
100 OutMI.addOperand(lowerOperand(MO));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000101 }
102}