blob: d8ae11f2bd900dce4f4ff5e7bc8385f4b6d2f939 [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an 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// This file contains code to lower RISCV MachineInstrs to their corresponding
11// MCInst records.
12//
13//===----------------------------------------------------------------------===//
14
15#include "RISCV.h"
Alex Bradburyec8aa912017-11-08 13:24:21 +000016#include "MCTargetDesc/RISCVMCExpr.h"
17#include "llvm/CodeGen/AsmPrinter.h"
Alex Bradbury89718422017-10-19 21:37:38 +000018#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineInstr.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
Alex Bradburyec8aa912017-11-08 13:24:21 +000029static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
30 const AsmPrinter &AP) {
31 MCContext &Ctx = AP.OutContext;
32 RISCVMCExpr::VariantKind Kind;
33
34 switch (MO.getTargetFlags()) {
35 default:
36 llvm_unreachable("Unknown target flag on GV operand");
37 case RISCVII::MO_None:
38 Kind = RISCVMCExpr::VK_RISCV_None;
39 break;
40 case RISCVII::MO_LO:
41 Kind = RISCVMCExpr::VK_RISCV_LO;
42 break;
43 case RISCVII::MO_HI:
44 Kind = RISCVMCExpr::VK_RISCV_HI;
45 break;
46 }
47
48 const MCExpr *ME =
49 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
50
51 if (!MO.isJTI() && MO.getOffset())
52 ME = MCBinaryExpr::createAdd(
53 ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
54
55 ME = RISCVMCExpr::create(ME, Kind, Ctx);
56 return MCOperand::createExpr(ME);
57}
58
59bool llvm::LowerRISCVMachineOperandToMCOperand(const MachineOperand &MO,
60 MCOperand &MCOp,
61 const AsmPrinter &AP) {
62 switch (MO.getType()) {
63 default:
64 report_fatal_error("LowerRISCVMachineInstrToMCInst: unknown operand type");
65 case MachineOperand::MO_Register:
66 // Ignore all implicit register operands.
67 if (MO.isImplicit())
68 return false;
69 MCOp = MCOperand::createReg(MO.getReg());
70 break;
Alex Bradburya3376752017-11-08 13:41:21 +000071 case MachineOperand::MO_RegisterMask:
72 // Regmasks are like implicit defs.
73 return false;
Alex Bradburyec8aa912017-11-08 13:24:21 +000074 case MachineOperand::MO_Immediate:
75 MCOp = MCOperand::createImm(MO.getImm());
76 break;
Alex Bradbury74913e12017-11-08 13:31:40 +000077 case MachineOperand::MO_MachineBasicBlock:
78 MCOp = MCOperand::createExpr(
79 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), AP.OutContext));
80 break;
Alex Bradburyec8aa912017-11-08 13:24:21 +000081 case MachineOperand::MO_GlobalAddress:
82 MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
83 break;
Alex Bradburyffc435e2017-11-21 08:11:03 +000084 case MachineOperand::MO_BlockAddress:
85 MCOp = lowerSymbolOperand(
86 MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
87 break;
88 case MachineOperand::MO_ExternalSymbol:
89 MCOp = lowerSymbolOperand(
90 MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
91 break;
Alex Bradburyec8aa912017-11-08 13:24:21 +000092 }
93 return true;
94}
95
96void llvm::LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
97 const AsmPrinter &AP) {
Alex Bradbury89718422017-10-19 21:37:38 +000098 OutMI.setOpcode(MI->getOpcode());
99
100 for (const MachineOperand &MO : MI->operands()) {
101 MCOperand MCOp;
Alex Bradburyec8aa912017-11-08 13:24:21 +0000102 if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
103 OutMI.addOperand(MCOp);
Alex Bradbury89718422017-10-19 21:37:38 +0000104 }
105}