blob: 6e2e9599dfea921ebf10968b2e10406a7c53ca6d [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"
Chris Lattner621c44d2009-08-22 20:48:53 +000018#include "MSP430MCAsmInfo.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000019#include "MSP430TargetMachine.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Module.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner73266f92009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000030#include "llvm/Target/TargetData.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbarc680b012009-07-25 06:49:55 +000032#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000033#include "llvm/ADT/Statistic.h"
34#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000036#include "llvm/Support/Mangler.h"
Edwin Török675d5622009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov37171572009-05-03 12:57:15 +000038
39using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
43namespace {
44 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
45 public:
Daniel Dunbar946686a2009-07-15 23:17:20 +000046 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnera5ef4d32009-08-22 21:43:10 +000047 const MCAsmInfo *MAI, bool V)
48 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov37171572009-05-03 12:57:15 +000049
50 virtual const char *getPassName() const {
51 return "MSP430 Assembly Printer";
52 }
53
Anton Korobeynikov4c88f112009-05-03 13:06:03 +000054 void printOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
56 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
Anton Korobeynikov46499082009-05-03 13:12:23 +000058 void printCCOperand(const MachineInstr *MI, int OpNum);
Chris Lattnerddb259a2009-08-08 01:32:19 +000059 void printInstruction(const MachineInstr *MI); // autogenerated.
Anton Korobeynikov37171572009-05-03 12:57:15 +000060 void printMachineInstruction(const MachineInstr * MI);
Anton Korobeynikov7c672df2009-08-26 13:44:29 +000061 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
62 unsigned AsmVariant,
63 const char *ExtraCode);
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000064
65 void emitFunctionHeader(const MachineFunction &MF);
Anton Korobeynikov37171572009-05-03 12:57:15 +000066 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov37171572009-05-03 12:57:15 +000067
Chris Lattnerae982212009-07-21 18:38:57 +000068 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
69 // FIXME: No support for global variables?
70 }
71
Anton Korobeynikov37171572009-05-03 12:57:15 +000072 void getAnalysisUsage(AnalysisUsage &AU) const {
73 AsmPrinter::getAnalysisUsage(AU);
74 AU.setPreservesAll();
75 }
76 };
77} // end of anonymous namespace
78
79#include "MSP430GenAsmWriter.inc"
80
Anton Korobeynikov37171572009-05-03 12:57:15 +000081
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000082void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
83 const Function *F = MF.getFunction();
84
Chris Lattner73266f92009-08-19 05:49:37 +000085 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000086
Bill Wendling25a8ae32009-06-30 22:38:32 +000087 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000088 EmitAlignment(FnAlign, F);
89
90 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000091 default: llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000092 case Function::InternalLinkage: // Symbols default to internal.
93 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000094 case Function::LinkerPrivateLinkage:
Anton Korobeynikov3f95a852009-05-03 13:17:11 +000095 break;
96 case Function::ExternalLinkage:
97 O << "\t.globl\t" << CurrentFnName << '\n';
98 break;
99 case Function::LinkOnceAnyLinkage:
100 case Function::LinkOnceODRLinkage:
101 case Function::WeakAnyLinkage:
102 case Function::WeakODRLinkage:
103 O << "\t.weak\t" << CurrentFnName << '\n';
104 break;
105 }
106
107 printVisibility(CurrentFnName, F->getVisibility());
108
109 O << "\t.type\t" << CurrentFnName << ",@function\n"
110 << CurrentFnName << ":\n";
111}
112
Anton Korobeynikov83400902009-05-03 13:01:41 +0000113bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000114 SetupMachineFunction(MF);
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000115 O << "\n\n";
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000116
117 // Print the 'header' of function
118 emitFunctionHeader(MF);
119
Anton Korobeynikov83400902009-05-03 13:01:41 +0000120 // Print out code for the function.
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 I != E; ++I) {
123 // Print a label for the basic block.
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000124 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
125 // This is an entry block or a block that's only reachable via a
126 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
127 } else {
128 printBasicBlockLabel(I, true, true, VerboseAsm);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000129 O << '\n';
130 }
131
132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Anton Korobeynikova5a157b2009-05-03 13:17:31 +0000133 II != E; ++II)
Anton Korobeynikov83400902009-05-03 13:01:41 +0000134 // Print the assembly for the instruction.
Anton Korobeynikov83400902009-05-03 13:01:41 +0000135 printMachineInstruction(II);
Anton Korobeynikov83400902009-05-03 13:01:41 +0000136 }
137
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000138 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov3f95a852009-05-03 13:17:11 +0000139 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
140
Anton Korobeynikov37171572009-05-03 12:57:15 +0000141 // We didn't modify anything
142 return false;
143}
144
Anton Korobeynikovf1fb8c72009-05-03 13:02:04 +0000145void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikov37171572009-05-03 12:57:15 +0000146 ++EmittedInsts;
147
148 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000149 printInstruction(MI);
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");
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000186 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov165bbe32009-05-03 13:14:46 +0000187 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
Anton Korobeynikov7c672df2009-08-26 13:44:29 +0000249/// PrintAsmOperand - Print out an operand for an inline asm expression.
250///
251bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
252 unsigned AsmVariant,
253 const char *ExtraCode) {
254 // Does this asm operand have a single letter operand modifier?
255 if (ExtraCode && ExtraCode[0])
256 return true; // Unknown modifier.
257
258 printOperand(MI, OpNo);
259 return false;
260}
261
Anton Korobeynikovf1a7a482009-08-14 19:06:50 +0000262// Force static initialization.
263extern "C" void LLVMInitializeMSP430AsmPrinter() {
264 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
Daniel Dunbarc680b012009-07-25 06:49:55 +0000265}