1 //===-- AVRMCInstLower.cpp - Convert AVR 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 AVR MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AVRMCInstLower.h"
16
17 #include "AVRInstrInfo.h"
18 #include "MCTargetDesc/AVRMCExpr.h"
19
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/Support/ErrorHandling.h"
24
25 namespace llvm {
26
lowerSymbolOperand(const MachineOperand & MO,MCSymbol * Sym) const27 MCOperand AVRMCInstLower::lowerSymbolOperand(const MachineOperand &MO,
28 MCSymbol *Sym) const {
29 unsigned char TF = MO.getTargetFlags();
30 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
31
32 bool IsNegated = false;
33 if (TF & AVRII::MO_NEG) { IsNegated = true; }
34
35 if (!MO.isJTI() && MO.getOffset()) {
36 Expr = MCBinaryExpr::createAdd(
37 Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
38 }
39
40 bool IsFunction = MO.isGlobal() && isa<Function>(MO.getGlobal());
41
42 if (TF & AVRII::MO_LO) {
43 if (IsFunction) {
44 // N.B. Should we use _GS fixups here to cope with >128k progmem?
45 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_LO8, Expr, IsNegated, Ctx);
46 } else {
47 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_LO8, Expr, IsNegated, Ctx);
48 }
49 } else if (TF & AVRII::MO_HI) {
50 if (IsFunction) {
51 // N.B. Should we use _GS fixups here to cope with >128k progmem?
52 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_PM_HI8, Expr, IsNegated, Ctx);
53 } else {
54 Expr = AVRMCExpr::create(AVRMCExpr::VK_AVR_HI8, Expr, IsNegated, Ctx);
55 }
56 } else if (TF != 0) {
57 llvm_unreachable("Unknown target flag on symbol operand");
58 }
59
60 return MCOperand::createExpr(Expr);
61 }
62
lowerInstruction(const MachineInstr & MI,MCInst & OutMI) const63 void AVRMCInstLower::lowerInstruction(const MachineInstr &MI, MCInst &OutMI) const {
64 OutMI.setOpcode(MI.getOpcode());
65
66 for (MachineOperand const &MO : MI.operands()) {
67 MCOperand MCOp;
68
69 switch (MO.getType()) {
70 default:
71 MI.print(errs());
72 llvm_unreachable("unknown operand type");
73 case MachineOperand::MO_Register:
74 // Ignore all implicit register operands.
75 if (MO.isImplicit())
76 continue;
77 MCOp = MCOperand::createReg(MO.getReg());
78 break;
79 case MachineOperand::MO_Immediate:
80 MCOp = MCOperand::createImm(MO.getImm());
81 break;
82 case MachineOperand::MO_GlobalAddress:
83 MCOp = lowerSymbolOperand(MO, Printer.getSymbol(MO.getGlobal()));
84 break;
85 case MachineOperand::MO_ExternalSymbol:
86 MCOp = lowerSymbolOperand(
87 MO, Printer.GetExternalSymbolSymbol(MO.getSymbolName()));
88 break;
89 case MachineOperand::MO_MachineBasicBlock:
90 MCOp = MCOperand::createExpr(
91 MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
92 break;
93 case MachineOperand::MO_RegisterMask:
94 continue;
95 case MachineOperand::MO_BlockAddress:
96 MCOp = lowerSymbolOperand(
97 MO, Printer.GetBlockAddressSymbol(MO.getBlockAddress()));
98 break;
99 case MachineOperand::MO_JumpTableIndex:
100 MCOp = lowerSymbolOperand(MO, Printer.GetJTISymbol(MO.getIndex()));
101 break;
102 case MachineOperand::MO_ConstantPoolIndex:
103 MCOp = lowerSymbolOperand(MO, Printer.GetCPISymbol(MO.getIndex()));
104 break;
105 }
106
107 OutMI.addOperand(MCOp);
108 }
109 }
110
111 } // end of namespace llvm
112
113