blob: 0d5481b1c2748bbc2cf8f63e852ebb0a13c8e3ae [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);
57 bool runOnMachineFunction(MachineFunction &F);
58 bool doInitialization(Module &M);
59 bool doFinalization(Module &M);
60
61 void getAnalysisUsage(AnalysisUsage &AU) const {
62 AsmPrinter::getAnalysisUsage(AU);
63 AU.setPreservesAll();
64 }
65 };
66} // end of anonymous namespace
67
68#include "MSP430GenAsmWriter.inc"
69
70/// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
71/// assembly code for a MachineFunction to the given output stream,
72/// using the given target machine description. This should work
73/// regardless of whether the function is in SSA form.
74///
75FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
76 MSP430TargetMachine &tm,
77 bool fast, bool verbose) {
78 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
79}
80
81bool MSP430AsmPrinter::doInitialization(Module &M) {
82 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
83 return false; // success
84}
85
86
87bool MSP430AsmPrinter::doFinalization(Module &M) {
88 return AsmPrinter::doFinalization(M);
89}
90
Anton Korobeynikov09c42f52009-05-03 13:01:41 +000091bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 // Print out code for the function.
93 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
94 I != E; ++I) {
95 // Print a label for the basic block.
96 if (I != MF.begin()) {
97 printBasicBlockLabel(I, true , true);
98 O << '\n';
99 }
100
101 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
102 II != E; ++II) {
103 // Print the assembly for the instruction.
104 O << "\t";
105 printMachineInstruction(II);
106 }
107
108 // Each Basic Block is separated by a newline
109 O << '\n';
110 }
111
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000112 // We didn't modify anything
113 return false;
114}
115
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000116void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000117 ++EmittedInsts;
118
119 // Call the autogenerated instruction printer routines.
120 if (printInstruction(MI))
121 return;
122
123 assert(0 && "Should not happen");
124}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000125
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000126void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
127 const char* Modifier) {
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000128 const MachineOperand &MO = MI->getOperand(OpNum);
129 switch (MO.getType()) {
130 case MachineOperand::MO_Register:
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000131 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
132 "Virtual registers should be already mapped!");
133 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
134 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000135 case MachineOperand::MO_Immediate:
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000136 if (!Modifier || strcmp(Modifier, "nohash"))
137 O << '#';
138 O << MO.getImm();
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000139 return;
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000140 case MachineOperand::MO_MachineBasicBlock:
141 printBasicBlockLabel(MO.getMBB());
Anton Korobeynikov3c2684d2009-05-03 13:08:13 +0000142 return;
143 case MachineOperand::MO_GlobalAddress: {
144 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
145 bool isCallOp = Modifier && !strcmp(Modifier, "call");
146 std::string Name = Mang->getValueName(MO.getGlobal());
147 assert(MO.getOffset() == 0 && "No offsets allowed!");
148
149 if (isCallOp)
150 O << '#';
151 else if (isMemOp)
152 O << '&';
153
154 O << Name;
155
156 return;
157 }
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000158 case MachineOperand::MO_ExternalSymbol: {
159 bool isCallOp = Modifier && !strcmp(Modifier, "call");
160 std::string Name(TAI->getGlobalPrefix());
161 Name += MO.getSymbolName();
162 if (isCallOp)
163 O << '#';
164 O << Name;
165 return;
166 }
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000167 default:
168 assert(0 && "Not implemented yet!");
169 }
170}
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000171
172void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
173 const char* Modifier) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000174 const MachineOperand &Base = MI->getOperand(OpNum);
175 const MachineOperand &Disp = MI->getOperand(OpNum+1);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000176
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000177 if (Base.isGlobal())
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000178 printOperand(MI, OpNum, "mem");
179 else if (Disp.isImm() && !Base.getReg())
180 printOperand(MI, OpNum);
181 else if (Base.getReg()) {
182 if (Disp.getImm()) {
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000183 printOperand(MI, OpNum + 1, "nohash");
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000184 O << '(';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000185 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000186 O << ')';
187 } else {
188 O << '@';
Anton Korobeynikovcf14ae52009-05-03 13:09:40 +0000189 printOperand(MI, OpNum);
Anton Korobeynikov1deea5f2009-05-03 13:09:10 +0000190 }
191 } else
192 assert(0 && "Unsupported memory operand");
Anton Korobeynikov36b6e532009-05-03 13:06:03 +0000193}
194
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000195void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
196 unsigned CC = MI->getOperand(OpNum).getImm();
197
198 switch (CC) {
199 default:
200 assert(0 && "Unsupported CC code");
201 break;
202 case MSP430::COND_E:
203 O << 'e';
204 break;
205 case MSP430::COND_NE:
206 O << "ne";
207 break;
208 case MSP430::COND_HS:
209 O << "hs";
210 break;
211 case MSP430::COND_LO:
212 O << "lo";
213 break;
214 case MSP430::COND_GE:
215 O << "ge";
216 break;
217 case MSP430::COND_L:
218 O << 'l';
219 break;
220 }
221}