1 //===-- SparcMCInstLower.cpp - Convert Sparc 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 // This file contains code to lower Sparc MachineInstrs to their corresponding
11 // MCInst records.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "Sparc.h"
16 #include "MCTargetDesc/SparcMCExpr.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineOperand.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 
28 using namespace llvm;
29 
30 
LowerSymbolOperand(const MachineInstr * MI,const MachineOperand & MO,AsmPrinter & AP)31 static MCOperand LowerSymbolOperand(const MachineInstr *MI,
32                                     const MachineOperand &MO,
33                                     AsmPrinter &AP) {
34 
35   SparcMCExpr::VariantKind Kind =
36     (SparcMCExpr::VariantKind)MO.getTargetFlags();
37   const MCSymbol *Symbol = nullptr;
38 
39   switch(MO.getType()) {
40   default: llvm_unreachable("Unknown type in LowerSymbolOperand");
41   case MachineOperand::MO_MachineBasicBlock:
42     Symbol = MO.getMBB()->getSymbol();
43     break;
44 
45   case MachineOperand::MO_GlobalAddress:
46     Symbol = AP.getSymbol(MO.getGlobal());
47     break;
48 
49   case MachineOperand::MO_BlockAddress:
50     Symbol = AP.GetBlockAddressSymbol(MO.getBlockAddress());
51     break;
52 
53   case MachineOperand::MO_ExternalSymbol:
54     Symbol = AP.GetExternalSymbolSymbol(MO.getSymbolName());
55     break;
56 
57   case MachineOperand::MO_ConstantPoolIndex:
58     Symbol = AP.GetCPISymbol(MO.getIndex());
59     break;
60   }
61 
62   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Symbol,
63                                                          AP.OutContext);
64   const SparcMCExpr *expr = SparcMCExpr::Create(Kind, MCSym,
65                                                 AP.OutContext);
66   return MCOperand::CreateExpr(expr);
67 }
68 
LowerOperand(const MachineInstr * MI,const MachineOperand & MO,AsmPrinter & AP)69 static MCOperand LowerOperand(const MachineInstr *MI,
70                               const MachineOperand &MO,
71                               AsmPrinter &AP) {
72   switch(MO.getType()) {
73   default: llvm_unreachable("unknown operand type"); break;
74   case MachineOperand::MO_Register:
75     if (MO.isImplicit())
76       break;
77     return MCOperand::CreateReg(MO.getReg());
78 
79   case MachineOperand::MO_Immediate:
80     return MCOperand::CreateImm(MO.getImm());
81 
82   case MachineOperand::MO_MachineBasicBlock:
83   case MachineOperand::MO_GlobalAddress:
84   case MachineOperand::MO_BlockAddress:
85   case MachineOperand::MO_ExternalSymbol:
86   case MachineOperand::MO_ConstantPoolIndex:
87     return LowerSymbolOperand(MI, MO, AP);
88 
89   case MachineOperand::MO_RegisterMask:   break;
90 
91   }
92   return MCOperand();
93 }
94 
LowerSparcMachineInstrToMCInst(const MachineInstr * MI,MCInst & OutMI,AsmPrinter & AP)95 void llvm::LowerSparcMachineInstrToMCInst(const MachineInstr *MI,
96                                           MCInst &OutMI,
97                                           AsmPrinter &AP)
98 {
99 
100   OutMI.setOpcode(MI->getOpcode());
101 
102   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
103     const MachineOperand &MO = MI->getOperand(i);
104     MCOperand MCOp = LowerOperand(MI, MO, AP);
105 
106     if (MCOp.isValid())
107       OutMI.addOperand(MCOp);
108   }
109 }
110