blob: bfdb058c94d33b58e9c08d64891580ba1df25e74 [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"
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,
Anton Korobeynikov60871cb2009-05-03 13:19:42 +000043 const TargetAsmInfo *TAI,
44 CodeGenOpt::Level OL, bool V)
45 : AsmPrinter(O, TM, TAI, OL, 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 CodeGenOpt::Level OptLevel,
81 bool verbose) {
82 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000083}
84
85bool MSP430AsmPrinter::doInitialization(Module &M) {
86 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
87 return false; // success
88}
89
90
91bool MSP430AsmPrinter::doFinalization(Module &M) {
92 return AsmPrinter::doFinalization(M);
93}
94
Anton Korobeynikov6130fc82009-05-03 13:17:11 +000095void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
96 const Function *F = MF.getFunction();
97
98 SwitchToSection(TAI->SectionForGlobal(F));
99
Bill Wendling20c568f2009-06-30 22:38:32 +0000100 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000101 EmitAlignment(FnAlign, F);
102
103 switch (F->getLinkage()) {
104 default: assert(0 && "Unknown linkage type!");
105 case Function::InternalLinkage: // Symbols default to internal.
106 case Function::PrivateLinkage:
107 break;
108 case Function::ExternalLinkage:
109 O << "\t.globl\t" << CurrentFnName << '\n';
110 break;
111 case Function::LinkOnceAnyLinkage:
112 case Function::LinkOnceODRLinkage:
113 case Function::WeakAnyLinkage:
114 case Function::WeakODRLinkage:
115 O << "\t.weak\t" << CurrentFnName << '\n';
116 break;
117 }
118
119 printVisibility(CurrentFnName, F->getVisibility());
120
121 O << "\t.type\t" << CurrentFnName << ",@function\n"
122 << CurrentFnName << ":\n";
123}
124
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000125bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000126 SetupMachineFunction(MF);
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000127 O << "\n\n";
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000128
129 // Print the 'header' of function
130 emitFunctionHeader(MF);
131
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000132 // Print out code for the function.
133 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
134 I != E; ++I) {
135 // Print a label for the basic block.
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000136 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
137 // This is an entry block or a block that's only reachable via a
138 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
139 } else {
140 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000141 O << '\n';
142 }
143
144 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikov1394db02009-05-03 13:17:31 +0000145 II != E; ++II)
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000146 // Print the assembly for the instruction.
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000147 printMachineInstruction(II);
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000148 }
149
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000150 if (TAI->hasDotTypeDotSizeDirective())
151 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
152
153 O.flush();
154
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000155 // We didn't modify anything
156 return false;
157}
158
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000159void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000160 ++EmittedInsts;
161
162 // Call the autogenerated instruction printer routines.
163 if (printInstruction(MI))
164 return;
165
166 assert(0 && "Should not happen");
167}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000168
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000169void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
170 const char* Modifier) {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000171 const MachineOperand &MO = MI->getOperand(OpNum);
172 switch (MO.getType()) {
173 case MachineOperand::MO_Register:
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000174 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
175 "Virtual registers should be already mapped!");
176 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
177 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000178 case MachineOperand::MO_Immediate:
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000179 if (!Modifier || strcmp(Modifier, "nohash"))
180 O << '#';
181 O << MO.getImm();
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000182 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000183 case MachineOperand::MO_MachineBasicBlock:
184 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000185 return;
186 case MachineOperand::MO_GlobalAddress: {
187 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
188 bool isCallOp = Modifier && !strcmp(Modifier, "call");
189 std::string Name = Mang->getValueName(MO.getGlobal());
190 assert(MO.getOffset() == 0 && "No offsets allowed!");
191
192 if (isCallOp)
193 O << '#';
194 else if (isMemOp)
195 O << '&';
196
197 O << Name;
198
199 return;
200 }
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000201 case MachineOperand::MO_ExternalSymbol: {
202 bool isCallOp = Modifier && !strcmp(Modifier, "call");
203 std::string Name(TAI->getGlobalPrefix());
204 Name += MO.getSymbolName();
205 if (isCallOp)
206 O << '#';
207 O << Name;
208 return;
209 }
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000210 default:
211 assert(0 && "Not implemented yet!");
212 }
213}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000214
215void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
216 const char* Modifier) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000217 const MachineOperand &Base = MI->getOperand(OpNum);
218 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000219
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000220 if (Base.isGlobal())
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000221 printOperand(MI, OpNum, "mem");
222 else if (Disp.isImm() && !Base.getReg())
223 printOperand(MI, OpNum);
224 else if (Base.getReg()) {
225 if (Disp.getImm()) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000226 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000227 O << '(';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000228 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000229 O << ')';
230 } else {
231 O << '@';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000232 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000233 }
234 } else
235 assert(0 && "Unsupported memory operand");
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000236}
237
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000238void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
239 unsigned CC = MI->getOperand(OpNum).getImm();
240
241 switch (CC) {
242 default:
243 assert(0 && "Unsupported CC code");
244 break;
245 case MSP430::COND_E:
Anton Korobeynikovd9e89f62009-05-03 13:16:54 +0000246 O << "eq";
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000247 break;
248 case MSP430::COND_NE:
249 O << "ne";
250 break;
251 case MSP430::COND_HS:
252 O << "hs";
253 break;
254 case MSP430::COND_LO:
255 O << "lo";
256 break;
257 case MSP430::COND_GE:
258 O << "ge";
259 break;
260 case MSP430::COND_L:
261 O << 'l';
262 break;
263 }
264}