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 "MSP430MCAsmInfo.h"
19 #include "MSP430TargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetRegistry.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Mangler.h"
38 #include "llvm/Support/ErrorHandling.h"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
47 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
48 const MCAsmInfo *MAI, bool V)
49 : AsmPrinter(O, TM, MAI, V) {}
51 virtual const char *getPassName() const {
52 return "MSP430 Assembly Printer";
55 void printOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
58 const char* Modifier = 0);
59 void printCCOperand(const MachineInstr *MI, int OpNum);
60 void printInstruction(const MachineInstr *MI); // autogenerated.
61 static const char *getRegisterName(unsigned RegNo);
63 void printMachineInstruction(const MachineInstr * MI);
64 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66 const char *ExtraCode);
67 bool PrintAsmMemoryOperand(const MachineInstr *MI,
68 unsigned OpNo, unsigned AsmVariant,
69 const char *ExtraCode);
71 void emitFunctionHeader(const MachineFunction &MF);
72 bool runOnMachineFunction(MachineFunction &F);
74 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
75 // FIXME: No support for global variables?
78 void getAnalysisUsage(AnalysisUsage &AU) const {
79 AsmPrinter::getAnalysisUsage(AU);
83 } // end of anonymous namespace
85 #include "MSP430GenAsmWriter.inc"
88 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
89 const Function *F = MF.getFunction();
91 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
93 unsigned FnAlign = MF.getAlignment();
94 EmitAlignment(FnAlign, F);
96 switch (F->getLinkage()) {
97 default: llvm_unreachable("Unknown linkage type!");
98 case Function::InternalLinkage: // Symbols default to internal.
99 case Function::PrivateLinkage:
100 case Function::LinkerPrivateLinkage:
102 case Function::ExternalLinkage:
103 O << "\t.globl\t" << CurrentFnName << '\n';
105 case Function::LinkOnceAnyLinkage:
106 case Function::LinkOnceODRLinkage:
107 case Function::WeakAnyLinkage:
108 case Function::WeakODRLinkage:
109 O << "\t.weak\t" << CurrentFnName << '\n';
113 printVisibility(CurrentFnName, F->getVisibility());
115 O << "\t.type\t" << CurrentFnName << ",@function\n"
116 << CurrentFnName << ":\n";
119 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
120 SetupMachineFunction(MF);
123 // Print the 'header' of function
124 emitFunctionHeader(MF);
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
129 // Print a label for the basic block.
130 EmitBasicBlockStart(I);
132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
134 // Print the assembly for the instruction.
135 printMachineInstruction(II);
138 if (MAI->hasDotTypeDotSizeDirective())
139 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
141 // We didn't modify anything
145 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
148 processDebugLoc(MI, true);
150 // Call the autogenerated instruction printer routines.
151 printInstruction(MI);
153 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
157 processDebugLoc(MI, false);
160 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
161 const char* Modifier) {
162 const MachineOperand &MO = MI->getOperand(OpNum);
163 switch (MO.getType()) {
164 case MachineOperand::MO_Register:
165 O << getRegisterName(MO.getReg());
167 case MachineOperand::MO_Immediate:
168 if (!Modifier || strcmp(Modifier, "nohash"))
172 case MachineOperand::MO_MachineBasicBlock:
173 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
175 case MachineOperand::MO_GlobalAddress: {
176 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
177 std::string Name = Mang->getMangledName(MO.getGlobal());
178 uint64_t Offset = MO.getOffset();
180 O << (isMemOp ? '&' : '#');
182 O << '(' << Offset << '+';
190 case MachineOperand::MO_ExternalSymbol: {
191 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
192 std::string Name(MAI->getGlobalPrefix());
193 Name += MO.getSymbolName();
195 O << (isMemOp ? '&' : '#') << Name;
200 llvm_unreachable("Not implemented yet!");
204 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
205 const char* Modifier) {
206 const MachineOperand &Base = MI->getOperand(OpNum);
207 const MachineOperand &Disp = MI->getOperand(OpNum+1);
210 printOperand(MI, OpNum, "mem");
211 else if (Disp.isImm() && !Base.getReg())
212 printOperand(MI, OpNum);
213 else if (Base.getReg()) {
215 printOperand(MI, OpNum + 1, "nohash");
217 printOperand(MI, OpNum);
221 printOperand(MI, OpNum);
224 llvm_unreachable("Unsupported memory operand");
227 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
228 unsigned CC = MI->getOperand(OpNum).getImm();
232 llvm_unreachable("Unsupported CC code");
237 case MSP430::COND_NE:
240 case MSP430::COND_HS:
243 case MSP430::COND_LO:
246 case MSP430::COND_GE:
255 /// PrintAsmOperand - Print out an operand for an inline asm expression.
257 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
259 const char *ExtraCode) {
260 // Does this asm operand have a single letter operand modifier?
261 if (ExtraCode && ExtraCode[0])
262 return true; // Unknown modifier.
264 printOperand(MI, OpNo);
268 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
269 unsigned OpNo, unsigned AsmVariant,
270 const char *ExtraCode) {
271 if (ExtraCode && ExtraCode[0]) {
272 return true; // Unknown modifier.
274 printSrcMemOperand(MI, OpNo);
278 // Force static initialization.
279 extern "C" void LLVMInitializeMSP430AsmPrinter() {
280 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);