blob: 0ad0a90396e1ec1a1d8411dc46ffe57cfd98494f [file] [log] [blame]
Anton Korobeynikov37171572009-05-03 12:57:15 +00001//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ------------------===//
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 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"
18#include "MSP430TargetMachine.h"
19#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000032#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000033#include "llvm/Support/Mangler.h"
Edwin Török675d5622009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000035
36using namespace llvm;
37
38STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
40namespace {
41 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
42 public:
Daniel Dunbar946686a2009-07-15 23:17:20 +000043 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000044 const TargetAsmInfo *TAI, bool V)
45 : AsmPrinter(O, TM, TAI, V) {}
Anton Korobeynikov37171572009-05-03 12:57:15 +000046
47 virtual const char *getPassName() const {
48 return "MSP430 Assembly Printer";
49 }
50
Anton Korobeynikov4c88f112009-05-03 13:06:03 +000051 void printOperand(const MachineInstr *MI, int OpNum,
52 const char* Modifier = 0);
53 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
Anton Korobeynikov46499082009-05-03 13:12:23 +000055 void printCCOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov37171572009-05-03 12:57:15 +000056 bool printInstruction(const MachineInstr *MI); // autogenerated.
57 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000058
59 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikov37171572009-05-03 12:57:15 +000060 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov37171572009-05-03 12:57:15 +000061
62 void getAnalysisUsage(AnalysisUsage &AU) const {
63 AsmPrinter::getAnalysisUsage(AU);
64 AU.setPreservesAll();
65 }
66 };
67} // end of anonymous namespace
68
69#include "MSP430GenAsmWriter.inc"
70
71/// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
72/// assembly code for a MachineFunction to the given output stream,
73/// using the given target machine description. This should work
74/// regardless of whether the function is in SSA form.
75///
David Greene302008d2009-07-14 20:18:05 +000076FunctionPass *llvm::createMSP430CodePrinterPass(formatted_raw_ostream &o,
Daniel Dunbar946686a2009-07-15 23:17:20 +000077 TargetMachine &tm,
Anton Korobeynikov0d370dc2009-05-03 13:19:42 +000078 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000079 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikov37171572009-05-03 12:57:15 +000080}
81
Anton Korobeynikov37171572009-05-03 12:57:15 +000082
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000083void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
84 const Function *F = MF.getFunction();
85
86 SwitchToSection(TAI->SectionForGlobal(F));
87
Bill Wendling25a8ae32009-06-30 22:38:32 +000088 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000089 EmitAlignment(FnAlign, F);
90
91 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000092 default: llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000093 case Function::InternalLinkage: // Symbols default to internal.
94 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000095 case Function::LinkerPrivateLinkage:
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000096 break;
97 case Function::ExternalLinkage:
98 O << "\t.globl\t" << CurrentFnName << '\n';
99 break;
100 case Function::LinkOnceAnyLinkage:
101 case Function::LinkOnceODRLinkage:
102 case Function::WeakAnyLinkage:
103 case Function::WeakODRLinkage:
104 O << "\t.weak\t" << CurrentFnName << '\n';
105 break;
106 }
107
108 printVisibility(CurrentFnName, F->getVisibility());
109
110 O << "\t.type\t" << CurrentFnName << ",@function\n"
111 << CurrentFnName << ":\n";
112}
113
Anton Korobeynikov83400902009-05-03 13:01:41 +0000114bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000115 SetupMachineFunction(MF);
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000116 O << "\n\n";
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000117
118 // Print the 'header' of function
119 emitFunctionHeader(MF);
120
Anton Korobeynikov83400902009-05-03 13:01:41 +0000121 // Print out code for the function.
122 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
123 I != E; ++I) {
124 // Print a label for the basic block.
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000125 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
126 // This is an entry block or a block that's only reachable via a
127 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
128 } else {
129 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000130 O << '\n';
131 }
132
133 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000134 II != E; ++II)
Anton Korobeynikov83400902009-05-03 13:01:41 +0000135 // Print the assembly for the instruction.
Anton Korobeynikov83400902009-05-03 13:01:41 +0000136 printMachineInstruction(II);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000137 }
138
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000139 if (TAI->hasDotTypeDotSizeDirective())
140 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
141
142 O.flush();
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
151 // Call the autogenerated instruction printer routines.
152 if (printInstruction(MI))
153 return;
154
Edwin Törökbd448e32009-07-14 16:55:14 +0000155 llvm_unreachable("Should not happen");
Anton Korobeynikov37171572009-05-03 12:57:15 +0000156}
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000157
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000158void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
159 const char* Modifier) {
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000160 const MachineOperand &MO = MI->getOperand(OpNum);
161 switch (MO.getType()) {
162 case MachineOperand::MO_Register:
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000163 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
164 "Virtual registers should be already mapped!");
165 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
166 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000167 case MachineOperand::MO_Immediate:
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000168 if (!Modifier || strcmp(Modifier, "nohash"))
169 O << '#';
170 O << MO.getImm();
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000171 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000172 case MachineOperand::MO_MachineBasicBlock:
173 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000174 return;
175 case MachineOperand::MO_GlobalAddress: {
176 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
177 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000178 std::string Name = Mang->getMangledName(MO.getGlobal());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000179 assert(MO.getOffset() == 0 && "No offsets allowed!");
180
181 if (isCallOp)
182 O << '#';
183 else if (isMemOp)
184 O << '&';
185
186 O << Name;
187
188 return;
189 }
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000190 case MachineOperand::MO_ExternalSymbol: {
191 bool isCallOp = Modifier && !strcmp(Modifier, "call");
192 std::string Name(TAI->getGlobalPrefix());
193 Name += MO.getSymbolName();
194 if (isCallOp)
195 O << '#';
196 O << Name;
197 return;
198 }
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000199 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000200 llvm_unreachable("Not implemented yet!");
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000201 }
202}
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000203
204void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
205 const char* Modifier) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000206 const MachineOperand &Base = MI->getOperand(OpNum);
207 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000208
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000209 if (Base.isGlobal())
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000210 printOperand(MI, OpNum, "mem");
211 else if (Disp.isImm() && !Base.getReg())
212 printOperand(MI, OpNum);
213 else if (Base.getReg()) {
214 if (Disp.getImm()) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000215 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000216 O << '(';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000217 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000218 O << ')';
219 } else {
220 O << '@';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000221 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000222 }
223 } else
Edwin Törökbd448e32009-07-14 16:55:14 +0000224 llvm_unreachable("Unsupported memory operand");
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000225}
226
Anton Korobeynikov46499082009-05-03 13:12:23 +0000227void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
228 unsigned CC = MI->getOperand(OpNum).getImm();
229
230 switch (CC) {
231 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000232 llvm_unreachable("Unsupported CC code");
Anton Korobeynikov46499082009-05-03 13:12:23 +0000233 break;
234 case MSP430::COND_E:
Anton Korobeynikov2e3b0442009-05-03 13:16:54 +0000235 O << "eq";
Anton Korobeynikov46499082009-05-03 13:12:23 +0000236 break;
237 case MSP430::COND_NE:
238 O << "ne";
239 break;
240 case MSP430::COND_HS:
241 O << "hs";
242 break;
243 case MSP430::COND_LO:
244 O << "lo";
245 break;
246 case MSP430::COND_GE:
247 O << "ge";
248 break;
249 case MSP430::COND_L:
250 O << 'l';
251 break;
252 }
253}