blob: d849a837502086154cb31d8f9839f928bad88b72 [file] [log] [blame]
Daniel Dunbarc680b012009-07-25 06:49:55 +00001//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
Anton Korobeynikov37171572009-05-03 12:57:15 +00002//
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 a printer that converts from our internal representation
11// of machine-dependent LLVM code to the MSP430 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "MSP430.h"
17#include "MSP430InstrInfo.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000018#include "MSP430MCAsmInfo.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000019#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"
Chris Lattner73266f92009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000030#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000031#include "llvm/Target/TargetData.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbarc680b012009-07-25 06:49:55 +000033#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000036#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000037#include "llvm/Support/Mangler.h"
Edwin Török675d5622009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000039
40using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
44namespace {
45 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
46 public:
Daniel Dunbar946686a2009-07-15 23:17:20 +000047 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnera5ef4d32009-08-22 21:43:10 +000048 const MCAsmInfo *MAI, bool V)
49 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov37171572009-05-03 12:57:15 +000050
51 virtual const char *getPassName() const {
52 return "MSP430 Assembly Printer";
53 }
54
Anton Korobeynikov4c88f112009-05-03 13:06:03 +000055 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);
Anton Korobeynikov46499082009-05-03 13:12:23 +000059 void printCCOperand(const MachineInstr *MI, int OpNum);
Chris Lattnerddb259a2009-08-08 01:32:19 +000060 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner213703c2009-09-13 20:19:22 +000061 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000062
Anton Korobeynikov37171572009-05-03 12:57:15 +000063 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov7c672df2009-08-26 13:44:29 +000064 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
65 unsigned AsmVariant,
66 const char *ExtraCode);
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000067
68 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikov37171572009-05-03 12:57:15 +000069 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov37171572009-05-03 12:57:15 +000070
Chris Lattnerae982212009-07-21 18:38:57 +000071 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
72 // FIXME: No support for global variables?
73 }
74
Anton Korobeynikov37171572009-05-03 12:57:15 +000075 void getAnalysisUsage(AnalysisUsage &AU) const {
76 AsmPrinter::getAnalysisUsage(AU);
77 AU.setPreservesAll();
78 }
79 };
80} // end of anonymous namespace
81
82#include "MSP430GenAsmWriter.inc"
83
Anton Korobeynikov37171572009-05-03 12:57:15 +000084
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000085void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
86 const Function *F = MF.getFunction();
87
Chris Lattner73266f92009-08-19 05:49:37 +000088 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000089
Bill Wendling25a8ae32009-06-30 22:38:32 +000090 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000091 EmitAlignment(FnAlign, F);
92
93 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000094 default: llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000095 case Function::InternalLinkage: // Symbols default to internal.
96 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000097 case Function::LinkerPrivateLinkage:
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000098 break;
99 case Function::ExternalLinkage:
100 O << "\t.globl\t" << CurrentFnName << '\n';
101 break;
102 case Function::LinkOnceAnyLinkage:
103 case Function::LinkOnceODRLinkage:
104 case Function::WeakAnyLinkage:
105 case Function::WeakODRLinkage:
106 O << "\t.weak\t" << CurrentFnName << '\n';
107 break;
108 }
109
110 printVisibility(CurrentFnName, F->getVisibility());
111
112 O << "\t.type\t" << CurrentFnName << ",@function\n"
113 << CurrentFnName << ":\n";
114}
115
Anton Korobeynikov83400902009-05-03 13:01:41 +0000116bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000117 SetupMachineFunction(MF);
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000118 O << "\n\n";
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000119
120 // Print the 'header' of function
121 emitFunctionHeader(MF);
122
Anton Korobeynikov83400902009-05-03 13:01:41 +0000123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 // Print a label for the basic block.
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000127 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
128 // This is an entry block or a block that's only reachable via a
129 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
130 } else {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000131 EmitBasicBlockStart(I);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000132 O << '\n';
133 }
134
135 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000136 II != E; ++II)
Anton Korobeynikov83400902009-05-03 13:01:41 +0000137 // Print the assembly for the instruction.
Anton Korobeynikov83400902009-05-03 13:01:41 +0000138 printMachineInstruction(II);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000139 }
140
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000141 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000142 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
143
Anton Korobeynikov37171572009-05-03 12:57:15 +0000144 // We didn't modify anything
145 return false;
146}
147
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000148void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikov37171572009-05-03 12:57:15 +0000149 ++EmittedInsts;
150
Chris Lattner32d4cc72009-09-09 23:14:36 +0000151 processDebugLoc(MI->getDebugLoc());
152
Anton Korobeynikov37171572009-05-03 12:57:15 +0000153 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000154 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000155
156 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
157 EmitComments(*MI);
158 O << '\n';
Anton Korobeynikov37171572009-05-03 12:57:15 +0000159}
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000160
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000161void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
162 const char* Modifier) {
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000163 const MachineOperand &MO = MI->getOperand(OpNum);
164 switch (MO.getType()) {
165 case MachineOperand::MO_Register:
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000166 O << getRegisterName(MO.getReg());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000167 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000168 case MachineOperand::MO_Immediate:
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000169 if (!Modifier || strcmp(Modifier, "nohash"))
170 O << '#';
171 O << MO.getImm();
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000172 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000173 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000174 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000175 return;
176 case MachineOperand::MO_GlobalAddress: {
177 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
178 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000179 std::string Name = Mang->getMangledName(MO.getGlobal());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000180 assert(MO.getOffset() == 0 && "No offsets allowed!");
181
182 if (isCallOp)
183 O << '#';
184 else if (isMemOp)
185 O << '&';
186
187 O << Name;
188
189 return;
190 }
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000191 case MachineOperand::MO_ExternalSymbol: {
192 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000193 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000194 Name += MO.getSymbolName();
195 if (isCallOp)
196 O << '#';
197 O << Name;
198 return;
199 }
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000200 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000201 llvm_unreachable("Not implemented yet!");
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000202 }
203}
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000204
205void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
206 const char* Modifier) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000207 const MachineOperand &Base = MI->getOperand(OpNum);
208 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000209
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000210 if (Base.isGlobal())
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000211 printOperand(MI, OpNum, "mem");
212 else if (Disp.isImm() && !Base.getReg())
213 printOperand(MI, OpNum);
214 else if (Base.getReg()) {
215 if (Disp.getImm()) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000216 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000217 O << '(';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000218 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000219 O << ')';
220 } else {
221 O << '@';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000222 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000223 }
224 } else
Edwin Törökbd448e32009-07-14 16:55:14 +0000225 llvm_unreachable("Unsupported memory operand");
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000226}
227
Anton Korobeynikov46499082009-05-03 13:12:23 +0000228void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
229 unsigned CC = MI->getOperand(OpNum).getImm();
230
231 switch (CC) {
232 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000233 llvm_unreachable("Unsupported CC code");
Anton Korobeynikov46499082009-05-03 13:12:23 +0000234 break;
235 case MSP430::COND_E:
Anton Korobeynikov2e3b0442009-05-03 13:16:54 +0000236 O << "eq";
Anton Korobeynikov46499082009-05-03 13:12:23 +0000237 break;
238 case MSP430::COND_NE:
239 O << "ne";
240 break;
241 case MSP430::COND_HS:
242 O << "hs";
243 break;
244 case MSP430::COND_LO:
245 O << "lo";
246 break;
247 case MSP430::COND_GE:
248 O << "ge";
249 break;
250 case MSP430::COND_L:
251 O << 'l';
252 break;
253 }
254}
Daniel Dunbarc680b012009-07-25 06:49:55 +0000255
Anton Korobeynikov7c672df2009-08-26 13:44:29 +0000256/// PrintAsmOperand - Print out an operand for an inline asm expression.
257///
258bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
259 unsigned AsmVariant,
260 const char *ExtraCode) {
261 // Does this asm operand have a single letter operand modifier?
262 if (ExtraCode && ExtraCode[0])
263 return true; // Unknown modifier.
264
265 printOperand(MI, OpNo);
266 return false;
267}
268
Anton Korobeynikovf1a7a482009-08-14 19:06:50 +0000269// Force static initialization.
270extern "C" void LLVMInitializeMSP430AsmPrinter() {
271 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
Daniel Dunbarc680b012009-07-25 06:49:55 +0000272}