blob: bb112bc25502fb3a5c430c1b4f85cdd63c70fb42 [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"
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"
Daniel Dunbarc680b012009-07-25 06:49:55 +000030#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000033#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000034#include "llvm/Support/Mangler.h"
Edwin Török675d5622009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000036
37using namespace llvm;
38
39STATISTIC(EmittedInsts, "Number of machine instrs printed");
40
41namespace {
42 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
43 public:
Daniel Dunbar946686a2009-07-15 23:17:20 +000044 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000045 const TargetAsmInfo *TAI, bool V)
46 : AsmPrinter(O, TM, TAI, V) {}
Anton Korobeynikov37171572009-05-03 12:57:15 +000047
48 virtual const char *getPassName() const {
49 return "MSP430 Assembly Printer";
50 }
51
Anton Korobeynikov4c88f112009-05-03 13:06:03 +000052 void printOperand(const MachineInstr *MI, int OpNum,
53 const char* Modifier = 0);
54 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
Anton Korobeynikov46499082009-05-03 13:12:23 +000056 void printCCOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov37171572009-05-03 12:57:15 +000057 bool printInstruction(const MachineInstr *MI); // autogenerated.
58 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000059
60 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikov37171572009-05-03 12:57:15 +000061 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov37171572009-05-03 12:57:15 +000062
Chris Lattnerae982212009-07-21 18:38:57 +000063 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
64 // FIXME: No support for global variables?
65 }
66
Anton Korobeynikov37171572009-05-03 12:57:15 +000067 void getAnalysisUsage(AnalysisUsage &AU) const {
68 AsmPrinter::getAnalysisUsage(AU);
69 AU.setPreservesAll();
70 }
71 };
72} // end of anonymous namespace
73
74#include "MSP430GenAsmWriter.inc"
75
Anton Korobeynikov37171572009-05-03 12:57:15 +000076
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000077void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
78 const Function *F = MF.getFunction();
79
80 SwitchToSection(TAI->SectionForGlobal(F));
81
Bill Wendling25a8ae32009-06-30 22:38:32 +000082 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000083 EmitAlignment(FnAlign, F);
84
85 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000086 default: llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000087 case Function::InternalLinkage: // Symbols default to internal.
88 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000089 case Function::LinkerPrivateLinkage:
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000090 break;
91 case Function::ExternalLinkage:
92 O << "\t.globl\t" << CurrentFnName << '\n';
93 break;
94 case Function::LinkOnceAnyLinkage:
95 case Function::LinkOnceODRLinkage:
96 case Function::WeakAnyLinkage:
97 case Function::WeakODRLinkage:
98 O << "\t.weak\t" << CurrentFnName << '\n';
99 break;
100 }
101
102 printVisibility(CurrentFnName, F->getVisibility());
103
104 O << "\t.type\t" << CurrentFnName << ",@function\n"
105 << CurrentFnName << ":\n";
106}
107
Anton Korobeynikov83400902009-05-03 13:01:41 +0000108bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000109 SetupMachineFunction(MF);
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000110 O << "\n\n";
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000111
112 // Print the 'header' of function
113 emitFunctionHeader(MF);
114
Anton Korobeynikov83400902009-05-03 13:01:41 +0000115 // Print out code for the function.
116 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
117 I != E; ++I) {
118 // Print a label for the basic block.
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000119 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
120 // This is an entry block or a block that's only reachable via a
121 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
122 } else {
123 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000124 O << '\n';
125 }
126
127 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000128 II != E; ++II)
Anton Korobeynikov83400902009-05-03 13:01:41 +0000129 // Print the assembly for the instruction.
Anton Korobeynikov83400902009-05-03 13:01:41 +0000130 printMachineInstruction(II);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000131 }
132
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000133 if (TAI->hasDotTypeDotSizeDirective())
134 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
135
136 O.flush();
137
Anton Korobeynikov37171572009-05-03 12:57:15 +0000138 // We didn't modify anything
139 return false;
140}
141
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000142void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikov37171572009-05-03 12:57:15 +0000143 ++EmittedInsts;
144
145 // Call the autogenerated instruction printer routines.
146 if (printInstruction(MI))
147 return;
148
Edwin Törökbd448e32009-07-14 16:55:14 +0000149 llvm_unreachable("Should not happen");
Anton Korobeynikov37171572009-05-03 12:57:15 +0000150}
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000151
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000152void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
153 const char* Modifier) {
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000154 const MachineOperand &MO = MI->getOperand(OpNum);
155 switch (MO.getType()) {
156 case MachineOperand::MO_Register:
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000157 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
158 "Virtual registers should be already mapped!");
159 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
160 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000161 case MachineOperand::MO_Immediate:
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000162 if (!Modifier || strcmp(Modifier, "nohash"))
163 O << '#';
164 O << MO.getImm();
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000165 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000168 return;
169 case MachineOperand::MO_GlobalAddress: {
170 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
171 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000172 std::string Name = Mang->getMangledName(MO.getGlobal());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000173 assert(MO.getOffset() == 0 && "No offsets allowed!");
174
175 if (isCallOp)
176 O << '#';
177 else if (isMemOp)
178 O << '&';
179
180 O << Name;
181
182 return;
183 }
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000184 case MachineOperand::MO_ExternalSymbol: {
185 bool isCallOp = Modifier && !strcmp(Modifier, "call");
186 std::string Name(TAI->getGlobalPrefix());
187 Name += MO.getSymbolName();
188 if (isCallOp)
189 O << '#';
190 O << Name;
191 return;
192 }
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000193 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000194 llvm_unreachable("Not implemented yet!");
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000195 }
196}
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000197
198void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
199 const char* Modifier) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000200 const MachineOperand &Base = MI->getOperand(OpNum);
201 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000202
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000203 if (Base.isGlobal())
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000204 printOperand(MI, OpNum, "mem");
205 else if (Disp.isImm() && !Base.getReg())
206 printOperand(MI, OpNum);
207 else if (Base.getReg()) {
208 if (Disp.getImm()) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000209 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000210 O << '(';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000211 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000212 O << ')';
213 } else {
214 O << '@';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000215 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000216 }
217 } else
Edwin Törökbd448e32009-07-14 16:55:14 +0000218 llvm_unreachable("Unsupported memory operand");
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000219}
220
Anton Korobeynikov46499082009-05-03 13:12:23 +0000221void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
222 unsigned CC = MI->getOperand(OpNum).getImm();
223
224 switch (CC) {
225 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000226 llvm_unreachable("Unsupported CC code");
Anton Korobeynikov46499082009-05-03 13:12:23 +0000227 break;
228 case MSP430::COND_E:
Anton Korobeynikov2e3b0442009-05-03 13:16:54 +0000229 O << "eq";
Anton Korobeynikov46499082009-05-03 13:12:23 +0000230 break;
231 case MSP430::COND_NE:
232 O << "ne";
233 break;
234 case MSP430::COND_HS:
235 O << "hs";
236 break;
237 case MSP430::COND_LO:
238 O << "lo";
239 break;
240 case MSP430::COND_GE:
241 O << "ge";
242 break;
243 case MSP430::COND_L:
244 O << 'l';
245 break;
246 }
247}
Daniel Dunbarc680b012009-07-25 06:49:55 +0000248
249extern "C" void LLVMInitializeMSP430Target() {
250 // Register the target.
251 RegisterTargetMachine<MSP430TargetMachine> X(TheMSP430Target);
252 RegisterAsmPrinter<MSP430AsmPrinter> Y(TheMSP430Target);
253}