blob: b1fa3f0e7dace56fdbf554c8571071fdfc3832bd [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"
32#include "llvm/Support/Mangler.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39namespace {
40 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
41 public:
42 MSP430AsmPrinter(raw_ostream &O, MSP430TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000043 const TargetAsmInfo *TAI, bool V)
44 : AsmPrinter(O, TM, TAI, V) {}
Anton Korobeynikov37171572009-05-03 12:57:15 +000045
46 virtual const char *getPassName() const {
47 return "MSP430 Assembly Printer";
48 }
49
Anton Korobeynikov4c88f112009-05-03 13:06:03 +000050 void printOperand(const MachineInstr *MI, int OpNum,
51 const char* Modifier = 0);
52 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
53 const char* Modifier = 0);
Anton Korobeynikov46499082009-05-03 13:12:23 +000054 void printCCOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov37171572009-05-03 12:57:15 +000055 bool printInstruction(const MachineInstr *MI); // autogenerated.
56 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000057
58 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikov37171572009-05-03 12:57:15 +000059 bool runOnMachineFunction(MachineFunction &F);
60 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
62
63 void getAnalysisUsage(AnalysisUsage &AU) const {
64 AsmPrinter::getAnalysisUsage(AU);
65 AU.setPreservesAll();
66 }
67 };
68} // end of anonymous namespace
69
70#include "MSP430GenAsmWriter.inc"
71
72/// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
73/// assembly code for a MachineFunction to the given output stream,
74/// using the given target machine description. This should work
75/// regardless of whether the function is in SSA form.
76///
77FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
78 MSP430TargetMachine &tm,
Anton Korobeynikov0d370dc2009-05-03 13:19:42 +000079 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000080 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikov37171572009-05-03 12:57:15 +000081}
82
83bool MSP430AsmPrinter::doInitialization(Module &M) {
84 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
85 return false; // success
86}
87
88
89bool MSP430AsmPrinter::doFinalization(Module &M) {
90 return AsmPrinter::doFinalization(M);
91}
92
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000093void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
94 const Function *F = MF.getFunction();
95
96 SwitchToSection(TAI->SectionForGlobal(F));
97
Bill Wendling25a8ae32009-06-30 22:38:32 +000098 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000099 EmitAlignment(FnAlign, F);
100
101 switch (F->getLinkage()) {
102 default: assert(0 && "Unknown linkage type!");
103 case Function::InternalLinkage: // Symbols default to internal.
104 case Function::PrivateLinkage:
105 break;
106 case Function::ExternalLinkage:
107 O << "\t.globl\t" << CurrentFnName << '\n';
108 break;
109 case Function::LinkOnceAnyLinkage:
110 case Function::LinkOnceODRLinkage:
111 case Function::WeakAnyLinkage:
112 case Function::WeakODRLinkage:
113 O << "\t.weak\t" << CurrentFnName << '\n';
114 break;
115 }
116
117 printVisibility(CurrentFnName, F->getVisibility());
118
119 O << "\t.type\t" << CurrentFnName << ",@function\n"
120 << CurrentFnName << ":\n";
121}
122
Anton Korobeynikov83400902009-05-03 13:01:41 +0000123bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000124 SetupMachineFunction(MF);
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000125 O << "\n\n";
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000126
127 // Print the 'header' of function
128 emitFunctionHeader(MF);
129
Anton Korobeynikov83400902009-05-03 13:01:41 +0000130 // Print out code for the function.
131 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
132 I != E; ++I) {
133 // Print a label for the basic block.
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000134 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
135 // This is an entry block or a block that's only reachable via a
136 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
137 } else {
138 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000139 O << '\n';
140 }
141
142 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000143 II != E; ++II)
Anton Korobeynikov83400902009-05-03 13:01:41 +0000144 // Print the assembly for the instruction.
Anton Korobeynikov83400902009-05-03 13:01:41 +0000145 printMachineInstruction(II);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000146 }
147
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000148 if (TAI->hasDotTypeDotSizeDirective())
149 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
150
151 O.flush();
152
Anton Korobeynikov37171572009-05-03 12:57:15 +0000153 // We didn't modify anything
154 return false;
155}
156
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000157void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikov37171572009-05-03 12:57:15 +0000158 ++EmittedInsts;
159
160 // Call the autogenerated instruction printer routines.
161 if (printInstruction(MI))
162 return;
163
164 assert(0 && "Should not happen");
165}
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000166
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000167void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
168 const char* Modifier) {
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000169 const MachineOperand &MO = MI->getOperand(OpNum);
170 switch (MO.getType()) {
171 case MachineOperand::MO_Register:
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000172 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173 "Virtual registers should be already mapped!");
174 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
175 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000176 case MachineOperand::MO_Immediate:
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000177 if (!Modifier || strcmp(Modifier, "nohash"))
178 O << '#';
179 O << MO.getImm();
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000180 return;
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000181 case MachineOperand::MO_MachineBasicBlock:
182 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikove60685a2009-05-03 13:08:13 +0000183 return;
184 case MachineOperand::MO_GlobalAddress: {
185 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
186 bool isCallOp = Modifier && !strcmp(Modifier, "call");
187 std::string Name = Mang->getValueName(MO.getGlobal());
188 assert(MO.getOffset() == 0 && "No offsets allowed!");
189
190 if (isCallOp)
191 O << '#';
192 else if (isMemOp)
193 O << '&';
194
195 O << Name;
196
197 return;
198 }
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000199 case MachineOperand::MO_ExternalSymbol: {
200 bool isCallOp = Modifier && !strcmp(Modifier, "call");
201 std::string Name(TAI->getGlobalPrefix());
202 Name += MO.getSymbolName();
203 if (isCallOp)
204 O << '#';
205 O << Name;
206 return;
207 }
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000208 default:
209 assert(0 && "Not implemented yet!");
210 }
211}
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000212
213void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
214 const char* Modifier) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000215 const MachineOperand &Base = MI->getOperand(OpNum);
216 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000217
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000218 if (Base.isGlobal())
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000219 printOperand(MI, OpNum, "mem");
220 else if (Disp.isImm() && !Base.getReg())
221 printOperand(MI, OpNum);
222 else if (Base.getReg()) {
223 if (Disp.getImm()) {
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000224 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000225 O << '(';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000226 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000227 O << ')';
228 } else {
229 O << '@';
Anton Korobeynikova6e36692009-05-03 13:09:40 +0000230 printOperand(MI, OpNum);
Anton Korobeynikov0a4985b2009-05-03 13:09:10 +0000231 }
232 } else
233 assert(0 && "Unsupported memory operand");
Anton Korobeynikov4c88f112009-05-03 13:06:03 +0000234}
235
Anton Korobeynikov46499082009-05-03 13:12:23 +0000236void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
237 unsigned CC = MI->getOperand(OpNum).getImm();
238
239 switch (CC) {
240 default:
241 assert(0 && "Unsupported CC code");
242 break;
243 case MSP430::COND_E:
Anton Korobeynikov2e3b0442009-05-03 13:16:54 +0000244 O << "eq";
Anton Korobeynikov46499082009-05-03 13:12:23 +0000245 break;
246 case MSP430::COND_NE:
247 O << "ne";
248 break;
249 case MSP430::COND_HS:
250 O << "hs";
251 break;
252 case MSP430::COND_LO:
253 O << "lo";
254 break;
255 case MSP430::COND_GE:
256 O << "ge";
257 break;
258 case MSP430::COND_L:
259 O << 'l';
260 break;
261 }
262}