blob: b6eb6fef566e2836497589308dda08f15bc042b7 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-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"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovf2c3e172009-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:
43 MSP430AsmPrinter(raw_ostream &O, MSP430TargetMachine &TM,
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000044 const TargetAsmInfo *TAI, bool V)
45 : AsmPrinter(O, TM, TAI, V) {}
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000046
47 virtual const char *getPassName() const {
48 return "MSP430 Assembly Printer";
49 }
50
Anton Korobeynikov36b6e532009-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 Korobeynikov8b528e52009-05-03 13:12:23 +000055 void printCCOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000056 bool printInstruction(const MachineInstr *MI); // autogenerated.
57 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov6130fc82009-05-03 13:17:11 +000058
59 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000060 bool runOnMachineFunction(MachineFunction &F);
61 bool doInitialization(Module &M);
62 bool doFinalization(Module &M);
63
64 void getAnalysisUsage(AnalysisUsage &AU) const {
65 AsmPrinter::getAnalysisUsage(AU);
66 AU.setPreservesAll();
67 }
68 };
69} // end of anonymous namespace
70
71#include "MSP430GenAsmWriter.inc"
72
73/// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
74/// assembly code for a MachineFunction to the given output stream,
75/// using the given target machine description. This should work
76/// regardless of whether the function is in SSA form.
77///
78FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
79 MSP430TargetMachine &tm,
Anton Korobeynikov60871cb2009-05-03 13:19:42 +000080 bool verbose) {
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000081 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000082}
83
84bool MSP430AsmPrinter::doInitialization(Module &M) {
85 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
86 return false; // success
87}
88
89
90bool MSP430AsmPrinter::doFinalization(Module &M) {
91 return AsmPrinter::doFinalization(M);
92}
93
Anton Korobeynikov6130fc82009-05-03 13:17:11 +000094void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
95 const Function *F = MF.getFunction();
96
97 SwitchToSection(TAI->SectionForGlobal(F));
98
Bill Wendling20c568f2009-06-30 22:38:32 +000099 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000100 EmitAlignment(FnAlign, F);
101
102 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000103 default: llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000104 case Function::InternalLinkage: // Symbols default to internal.
105 case Function::PrivateLinkage:
106 break;
107 case Function::ExternalLinkage:
108 O << "\t.globl\t" << CurrentFnName << '\n';
109 break;
110 case Function::LinkOnceAnyLinkage:
111 case Function::LinkOnceODRLinkage:
112 case Function::WeakAnyLinkage:
113 case Function::WeakODRLinkage:
114 O << "\t.weak\t" << CurrentFnName << '\n';
115 break;
116 }
117
118 printVisibility(CurrentFnName, F->getVisibility());
119
120 O << "\t.type\t" << CurrentFnName << ",@function\n"
121 << CurrentFnName << ":\n";
122}
123
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000124bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000125 SetupMachineFunction(MF);
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000126 O << "\n\n";
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000127
128 // Print the 'header' of function
129 emitFunctionHeader(MF);
130
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000131 // Print out code for the function.
132 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
133 I != E; ++I) {
134 // Print a label for the basic block.
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000135 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
136 // This is an entry block or a block that's only reachable via a
137 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
138 } else {
139 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000140 O << '\n';
141 }
142
143 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000144 II != E; ++II)
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000145 // Print the assembly for the instruction.
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000146 printMachineInstruction(II);
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000147 }
148
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000149 if (TAI->hasDotTypeDotSizeDirective())
150 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
151
152 O.flush();
153
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000154 // We didn't modify anything
155 return false;
156}
157
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000158void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000159 ++EmittedInsts;
160
161 // Call the autogenerated instruction printer routines.
162 if (printInstruction(MI))
163 return;
164
Torok Edwinc23197a2009-07-14 16:55:14 +0000165 llvm_unreachable("Should not happen");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000166}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000167
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000168void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
169 const char* Modifier) {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000170 const MachineOperand &MO = MI->getOperand(OpNum);
171 switch (MO.getType()) {
172 case MachineOperand::MO_Register:
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000173 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
174 "Virtual registers should be already mapped!");
175 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
176 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000177 case MachineOperand::MO_Immediate:
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000178 if (!Modifier || strcmp(Modifier, "nohash"))
179 O << '#';
180 O << MO.getImm();
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000181 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000182 case MachineOperand::MO_MachineBasicBlock:
183 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000184 return;
185 case MachineOperand::MO_GlobalAddress: {
186 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
187 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Daniel Dunbar192957d2009-07-14 16:12:13 +0000188 std::string Name = Mang->getValueName(MO.getGlobal());
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000189 assert(MO.getOffset() == 0 && "No offsets allowed!");
190
191 if (isCallOp)
192 O << '#';
193 else if (isMemOp)
194 O << '&';
195
196 O << Name;
197
198 return;
199 }
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000200 case MachineOperand::MO_ExternalSymbol: {
201 bool isCallOp = Modifier && !strcmp(Modifier, "call");
202 std::string Name(TAI->getGlobalPrefix());
203 Name += MO.getSymbolName();
204 if (isCallOp)
205 O << '#';
206 O << Name;
207 return;
208 }
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000209 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000210 llvm_unreachable("Not implemented yet!");
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000211 }
212}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000213
214void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
215 const char* Modifier) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000216 const MachineOperand &Base = MI->getOperand(OpNum);
217 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000218
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000219 if (Base.isGlobal())
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000220 printOperand(MI, OpNum, "mem");
221 else if (Disp.isImm() && !Base.getReg())
222 printOperand(MI, OpNum);
223 else if (Base.getReg()) {
224 if (Disp.getImm()) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000225 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000226 O << '(';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000227 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000228 O << ')';
229 } else {
230 O << '@';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000231 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000232 }
233 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000234 llvm_unreachable("Unsupported memory operand");
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000235}
236
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000237void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
238 unsigned CC = MI->getOperand(OpNum).getImm();
239
240 switch (CC) {
241 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000242 llvm_unreachable("Unsupported CC code");
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000243 break;
244 case MSP430::COND_E:
Anton Korobeynikovd9e89f62009-05-03 13:16:54 +0000245 O << "eq";
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000246 break;
247 case MSP430::COND_NE:
248 O << "ne";
249 break;
250 case MSP430::COND_HS:
251 O << "hs";
252 break;
253 case MSP430::COND_LO:
254 O << "lo";
255 break;
256 case MSP430::COND_GE:
257 O << "ge";
258 break;
259 case MSP430::COND_L:
260 O << 'l';
261 break;
262 }
263}