blob: 0755e369814e7fa1bb1a38a28fb6b10dfe50446d [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,
43 const TargetAsmInfo *TAI, bool Fast, bool Verbose)
44 : AsmPrinter(O, TM, TAI, Fast, Verbose) {}
45
46 virtual const char *getPassName() const {
47 return "MSP430 Assembly Printer";
48 }
49
Anton Korobeynikov36b6e532009-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 Korobeynikov8b528e52009-05-03 13:12:23 +000054 void printCCOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000055 bool printInstruction(const MachineInstr *MI); // autogenerated.
56 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov6130fc82009-05-03 13:17:11 +000057
58 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikovf2c3e172009-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,
79 bool fast, bool verbose) {
80 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
81}
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 Korobeynikov6130fc82009-05-03 13:17:11 +000093void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
94 const Function *F = MF.getFunction();
95
96 SwitchToSection(TAI->SectionForGlobal(F));
97
98 unsigned FnAlign = 4;
99 if (F->hasFnAttr(Attribute::OptimizeForSize))
100 FnAlign = 1;
101
102 EmitAlignment(FnAlign, F);
103
104 switch (F->getLinkage()) {
105 default: assert(0 && "Unknown linkage type!");
106 case Function::InternalLinkage: // Symbols default to internal.
107 case Function::PrivateLinkage:
108 break;
109 case Function::ExternalLinkage:
110 O << "\t.globl\t" << CurrentFnName << '\n';
111 break;
112 case Function::LinkOnceAnyLinkage:
113 case Function::LinkOnceODRLinkage:
114 case Function::WeakAnyLinkage:
115 case Function::WeakODRLinkage:
116 O << "\t.weak\t" << CurrentFnName << '\n';
117 break;
118 }
119
120 printVisibility(CurrentFnName, F->getVisibility());
121
122 O << "\t.type\t" << CurrentFnName << ",@function\n"
123 << CurrentFnName << ":\n";
124}
125
Anton Korobeynikov09c42f52009-05-03 13:01:41 +0000126bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000127 SetupMachineFunction(MF);
128
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.
136 if (I != MF.begin()) {
137 printBasicBlockLabel(I, true , true);
138 O << '\n';
139 }
140
141 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
142 II != E; ++II) {
143 // Print the assembly for the instruction.
144 O << "\t";
145 printMachineInstruction(II);
146 }
147
148 // Each Basic Block is separated by a newline
149 O << '\n';
150 }
151
Anton Korobeynikov6130fc82009-05-03 13:17:11 +0000152 if (TAI->hasDotTypeDotSizeDirective())
153 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
154
155 O.flush();
156
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000157 // We didn't modify anything
158 return false;
159}
160
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000161void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000162 ++EmittedInsts;
163
164 // Call the autogenerated instruction printer routines.
165 if (printInstruction(MI))
166 return;
167
168 assert(0 && "Should not happen");
169}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000170
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000171void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
172 const char* Modifier) {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000173 const MachineOperand &MO = MI->getOperand(OpNum);
174 switch (MO.getType()) {
175 case MachineOperand::MO_Register:
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000176 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
177 "Virtual registers should be already mapped!");
178 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
179 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000180 case MachineOperand::MO_Immediate:
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000181 if (!Modifier || strcmp(Modifier, "nohash"))
182 O << '#';
183 O << MO.getImm();
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000184 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000185 case MachineOperand::MO_MachineBasicBlock:
186 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000187 return;
188 case MachineOperand::MO_GlobalAddress: {
189 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
190 bool isCallOp = Modifier && !strcmp(Modifier, "call");
191 std::string Name = Mang->getValueName(MO.getGlobal());
192 assert(MO.getOffset() == 0 && "No offsets allowed!");
193
194 if (isCallOp)
195 O << '#';
196 else if (isMemOp)
197 O << '&';
198
199 O << Name;
200
201 return;
202 }
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000203 case MachineOperand::MO_ExternalSymbol: {
204 bool isCallOp = Modifier && !strcmp(Modifier, "call");
205 std::string Name(TAI->getGlobalPrefix());
206 Name += MO.getSymbolName();
207 if (isCallOp)
208 O << '#';
209 O << Name;
210 return;
211 }
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000212 default:
213 assert(0 && "Not implemented yet!");
214 }
215}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000216
217void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
218 const char* Modifier) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000219 const MachineOperand &Base = MI->getOperand(OpNum);
220 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000221
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000222 if (Base.isGlobal())
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000223 printOperand(MI, OpNum, "mem");
224 else if (Disp.isImm() && !Base.getReg())
225 printOperand(MI, OpNum);
226 else if (Base.getReg()) {
227 if (Disp.getImm()) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000228 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000229 O << '(';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000230 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000231 O << ')';
232 } else {
233 O << '@';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000234 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000235 }
236 } else
237 assert(0 && "Unsupported memory operand");
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000238}
239
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000240void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
241 unsigned CC = MI->getOperand(OpNum).getImm();
242
243 switch (CC) {
244 default:
245 assert(0 && "Unsupported CC code");
246 break;
247 case MSP430::COND_E:
Anton Korobeynikovd9e89f62009-05-03 13:16:54 +0000248 O << "eq";
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000249 break;
250 case MSP430::COND_NE:
251 O << "ne";
252 break;
253 case MSP430::COND_HS:
254 O << "hs";
255 break;
256 case MSP430::COND_LO:
257 O << "lo";
258 break;
259 case MSP430::COND_GE:
260 O << "ge";
261 break;
262 case MSP430::COND_L:
263 O << 'l';
264 break;
265 }
266}