blob: 4cc8cc8c0c2af9b049c43f6f0425d53d73dab4cc [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;
71 case MachineOperand::MO_Immediate:
72 MCOp = MCOperand::createImm(MO.getImm());
73 break;
74 case MachineOperand::MO_GlobalAddress:
75 MCOp = lowerSymbolOperand(MO, AP.getSymbol(MO.getGlobal()), AP);
76 break;
77 }
78 return true;
79}
80
81void llvm::LowerRISCVMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI,
82 const AsmPrinter &AP) {
Alex Bradbury89718422017-10-19 21:37:38 +000083 OutMI.setOpcode(MI->getOpcode());
84
85 for (const MachineOperand &MO : MI->operands()) {
86 MCOperand MCOp;
Alex Bradburyec8aa912017-11-08 13:24:21 +000087 if (LowerRISCVMachineOperandToMCOperand(MO, MCOp, AP))
88 OutMI.addOperand(MCOp);
Alex Bradbury89718422017-10-19 21:37:38 +000089 }
90}