blob: 3e50be867ced95b63a139e404d6a41d49973d2af [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- RISCVMCInstLower.cpp - Convert RISCV MachineInstr to an MCInst ------=//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alex Bradbury89718422017-10-19 21:37:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains code to lower RISCV MachineInstrs to their corresponding
10// MCInst records.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RISCV.h"
Alex Bradburyec8aa912017-11-08 13:24:21 +000015#include "MCTargetDesc/RISCVMCExpr.h"
16#include "llvm/CodeGen/AsmPrinter.h"
Alex Bradbury89718422017-10-19 21:37:38 +000017#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
Alex Bradburyec8aa912017-11-08 13:24:21 +000028static MCOperand lowerSymbolOperand(const MachineOperand &MO, MCSymbol *Sym,
29 const AsmPrinter &AP) {
30 MCContext &Ctx = AP.OutContext;
31 RISCVMCExpr::VariantKind Kind;
32
33 switch (MO.getTargetFlags()) {
34 default:
35 llvm_unreachable("Unknown target flag on GV operand");
36 case RISCVII::MO_None:
37 Kind = RISCVMCExpr::VK_RISCV_None;
38 break;
39 case RISCVII::MO_LO:
40 Kind = RISCVMCExpr::VK_RISCV_LO;
41 break;
42 case RISCVII::MO_HI:
43 Kind = RISCVMCExpr::VK_RISCV_HI;
44 break;
45 }
46
47 const MCExpr *ME =
48 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx);
49
Alex Bradbury315cd3a2018-01-10 21:05:07 +000050 if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
Alex Bradburyec8aa912017-11-08 13:24:21 +000051 ME = MCBinaryExpr::createAdd(
52 ME, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
53
Alex Bradbury315cd3a2018-01-10 21:05:07 +000054 if (Kind != RISCVMCExpr::VK_RISCV_None)
55 ME = RISCVMCExpr::create(ME, Kind, Ctx);
Alex Bradburyec8aa912017-11-08 13:24:21 +000056 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:
Alex Bradbury315cd3a2018-01-10 21:05:07 +000078 MCOp = lowerSymbolOperand(MO, MO.getMBB()->getSymbol(), AP);
Alex Bradbury74913e12017-11-08 13:31:40 +000079 break;
Alex Bradburyec8aa912017-11-08 13:24:21 +000080 case MachineOperand::MO_GlobalAddress:
81 MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
82 break;
Alex Bradburyffc435e2017-11-21 08:11:03 +000083 case MachineOperand::MO_BlockAddress:
84 MCOp = lowerSymbolOperand(
85 MO, AP.GetBlockAddressSymbol(MO.getBlockAddress()), AP);
86 break;
87 case MachineOperand::MO_ExternalSymbol:
88 MCOp = lowerSymbolOperand(
89 MO, AP.GetExternalSymbolSymbol(MO.getSymbolName()), AP);
90 break;
Alex Bradbury80c8eb72018-03-20 13:26:12 +000091 case MachineOperand::MO_ConstantPoolIndex:
92 MCOp = lowerSymbolOperand(MO, AP.GetCPISymbol(MO.getIndex()), AP);
93 break;
Alex Bradburyec8aa912017-11-08 13:24:21 +000094 }
95 return true;
96}
97
98void llvm::LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
99 const AsmPrinter &AP) {
Alex Bradbury89718422017-10-19 21:37:38 +0000100 OutMI.setOpcode(MI->getOpcode());
101
102 for (const MachineOperand &MO : MI->operands()) {
103 MCOperand MCOp;
Alex Bradburyec8aa912017-11-08 13:24:21 +0000104 if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
105 OutMI.addOperand(MCOp);
Alex Bradbury89718422017-10-19 21:37:38 +0000106 }
107}