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 EmitConstantPool(MF.getConstantPool());
114 EmitJumpTableInfo(MF);
116 // We didn't modify anything
120 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
123 processDebugLoc(MI, true);
125 printInstructionThroughMCStreamer(MI);
131 processDebugLoc(MI, false);
134 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
135 const char* Modifier) {
136 const MachineOperand &MO = MI->getOperand(OpNum);
137 switch (MO.getType()) {
138 case MachineOperand::MO_Register:
139 O << MSP430InstPrinter::getRegisterName(MO.getReg());
141 case MachineOperand::MO_Immediate:
142 if (!Modifier || strcmp(Modifier, "nohash"))
146 case MachineOperand::MO_MachineBasicBlock:
147 O << *MO.getMBB()->getSymbol(OutContext);
149 case MachineOperand::MO_GlobalAddress: {
150 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
151 uint64_t Offset = MO.getOffset();
153 O << (isMemOp ? '&' : '#');
155 O << '(' << Offset << '+';
157 O << *GetGlobalValueSymbol(MO.getGlobal());
164 case MachineOperand::MO_ExternalSymbol: {
165 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
166 O << (isMemOp ? '&' : '#');
167 O << MAI->getGlobalPrefix() << MO.getSymbolName();
171 llvm_unreachable("Not implemented yet!");
175 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
176 const char* Modifier) {
177 const MachineOperand &Base = MI->getOperand(OpNum);
178 const MachineOperand &Disp = MI->getOperand(OpNum+1);
180 // Print displacement first
182 printOperand(MI, OpNum+1, "mem");
187 printOperand(MI, OpNum+1, "nohash");
191 // Print register base field
194 printOperand(MI, OpNum);
199 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
200 unsigned CC = MI->getOperand(OpNum).getImm();
204 llvm_unreachable("Unsupported CC code");
206 case MSP430CC::COND_E:
209 case MSP430CC::COND_NE:
212 case MSP430CC::COND_HS:
215 case MSP430CC::COND_LO:
218 case MSP430CC::COND_GE:
221 case MSP430CC::COND_L:
227 /// PrintAsmOperand - Print out an operand for an inline asm expression.
229 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
231 const char *ExtraCode) {
232 // Does this asm operand have a single letter operand modifier?
233 if (ExtraCode && ExtraCode[0])
234 return true; // Unknown modifier.
236 printOperand(MI, OpNo);
240 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
241 unsigned OpNo, unsigned AsmVariant,
242 const char *ExtraCode) {
243 if (ExtraCode && ExtraCode[0]) {
244 return true; // Unknown modifier.
246 printSrcMemOperand(MI, OpNo);
250 //===----------------------------------------------------------------------===//
251 void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
253 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
255 switch (MI->getOpcode()) {
256 case TargetInstrInfo::DBG_LABEL:
257 case TargetInstrInfo::EH_LABEL:
258 case TargetInstrInfo::GC_LABEL:
261 case TargetInstrInfo::KILL:
264 case TargetInstrInfo::INLINEASM:
267 case TargetInstrInfo::IMPLICIT_DEF:
268 printImplicitDef(MI);
274 MCInstLowering.Lower(MI, TmpInst);
276 printMCInst(&TmpInst);
279 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
280 unsigned SyntaxVariant,
281 const MCAsmInfo &MAI,
283 if (SyntaxVariant == 0)
284 return new MSP430InstPrinter(O, MAI);
288 // Force static initialization.
289 extern "C" void LLVMInitializeMSP430AsmPrinter() {
290 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
291 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
292 createMSP430MCInstPrinter);