1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430InstPrinter.h"
19 #include "MSP430MCAsmInfo.h"
20 #include "MSP430MCInstLower.h"
21 #include "MSP430TargetMachine.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/DwarfWriter.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/TargetData.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegistry.h"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/ErrorHandling.h"
44 STATISTIC(EmittedInsts, "Number of machine instrs printed");
47 EnableMCInst("enable-msp430-mcinst-printer", cl::Hidden,
48 cl::desc("enable experimental mcinst gunk in the msp430 backend"));
51 class MSP430AsmPrinter : public AsmPrinter {
53 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
54 const MCAsmInfo *MAI, bool V)
55 : AsmPrinter(O, TM, MAI, V) {}
57 virtual const char *getPassName() const {
58 return "MSP430 Assembly Printer";
61 void printMCInst(const MCInst *MI) {
62 MSP430InstPrinter(O, *MAI).printInstruction(MI);
64 void printOperand(const MachineInstr *MI, int OpNum,
65 const char* Modifier = 0);
66 void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
67 printOperand(MI, OpNum);
69 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
70 const char* Modifier = 0);
71 void printCCOperand(const MachineInstr *MI, int OpNum);
72 void printMachineInstruction(const MachineInstr * MI);
73 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
75 const char *ExtraCode);
76 bool PrintAsmMemoryOperand(const MachineInstr *MI,
77 unsigned OpNo, unsigned AsmVariant,
78 const char *ExtraCode);
79 void printInstructionThroughMCStreamer(const MachineInstr *MI);
81 bool runOnMachineFunction(MachineFunction &F);
83 void getAnalysisUsage(AnalysisUsage &AU) const {
84 AsmPrinter::getAnalysisUsage(AU);
88 } // end of anonymous namespace
91 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 SetupMachineFunction(MF);
97 // Print out code for the function.
98 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
100 // Print a label for the basic block.
101 EmitBasicBlockStart(I);
103 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
105 // Print the assembly for the instruction.
106 printMachineInstruction(II);
109 if (MAI->hasDotTypeDotSizeDirective())
110 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
112 // Print out constants referenced by the function
113 EmitJumpTableInfo(MF);
115 // We didn't modify anything
119 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
122 processDebugLoc(MI, true);
124 printInstructionThroughMCStreamer(MI);
130 processDebugLoc(MI, false);
133 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
134 const char* Modifier) {
135 const MachineOperand &MO = MI->getOperand(OpNum);
136 switch (MO.getType()) {
137 case MachineOperand::MO_Register:
138 O << MSP430InstPrinter::getRegisterName(MO.getReg());
140 case MachineOperand::MO_Immediate:
141 if (!Modifier || strcmp(Modifier, "nohash"))
145 case MachineOperand::MO_MachineBasicBlock:
146 O << *MO.getMBB()->getSymbol(OutContext);
148 case MachineOperand::MO_GlobalAddress: {
149 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
150 uint64_t Offset = MO.getOffset();
152 O << (isMemOp ? '&' : '#');
154 O << '(' << Offset << '+';
156 O << *GetGlobalValueSymbol(MO.getGlobal());
163 case MachineOperand::MO_ExternalSymbol: {
164 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
165 O << (isMemOp ? '&' : '#');
166 O << MAI->getGlobalPrefix() << MO.getSymbolName();
170 llvm_unreachable("Not implemented yet!");
174 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
175 const char* Modifier) {
176 const MachineOperand &Base = MI->getOperand(OpNum);
177 const MachineOperand &Disp = MI->getOperand(OpNum+1);
179 // Print displacement first
181 printOperand(MI, OpNum+1, "mem");
186 printOperand(MI, OpNum+1, "nohash");
190 // Print register base field
193 printOperand(MI, OpNum);
198 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
199 unsigned CC = MI->getOperand(OpNum).getImm();
203 llvm_unreachable("Unsupported CC code");
205 case MSP430CC::COND_E:
208 case MSP430CC::COND_NE:
211 case MSP430CC::COND_HS:
214 case MSP430CC::COND_LO:
217 case MSP430CC::COND_GE:
220 case MSP430CC::COND_L:
226 /// PrintAsmOperand - Print out an operand for an inline asm expression.
228 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
230 const char *ExtraCode) {
231 // Does this asm operand have a single letter operand modifier?
232 if (ExtraCode && ExtraCode[0])
233 return true; // Unknown modifier.
235 printOperand(MI, OpNo);
239 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
240 unsigned OpNo, unsigned AsmVariant,
241 const char *ExtraCode) {
242 if (ExtraCode && ExtraCode[0]) {
243 return true; // Unknown modifier.
245 printSrcMemOperand(MI, OpNo);
249 //===----------------------------------------------------------------------===//
250 void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
252 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
254 switch (MI->getOpcode()) {
255 case TargetInstrInfo::DBG_LABEL:
256 case TargetInstrInfo::EH_LABEL:
257 case TargetInstrInfo::GC_LABEL:
260 case TargetInstrInfo::KILL:
263 case TargetInstrInfo::INLINEASM:
266 case TargetInstrInfo::IMPLICIT_DEF:
267 printImplicitDef(MI);
273 MCInstLowering.Lower(MI, TmpInst);
275 printMCInst(&TmpInst);
278 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
279 unsigned SyntaxVariant,
280 const MCAsmInfo &MAI,
282 if (SyntaxVariant == 0)
283 return new MSP430InstPrinter(O, MAI);
287 // Force static initialization.
288 extern "C" void LLVMInitializeMSP430AsmPrinter() {
289 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
290 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
291 createMSP430MCInstPrinter);