1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
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"
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
40 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
42 MSP430AsmPrinter(raw_ostream &O, MSP430TargetMachine &TM,
43 const TargetAsmInfo *TAI, bool V)
44 : AsmPrinter(O, TM, TAI, V) {}
46 virtual const char *getPassName() const {
47 return "MSP430 Assembly Printer";
50 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);
54 void printCCOperand(const MachineInstr *MI, int OpNum);
55 bool printInstruction(const MachineInstr *MI); // autogenerated.
56 void printMachineInstruction(const MachineInstr * MI);
58 void emitFunctionHeader(const MachineFunction &MF);
59 bool runOnMachineFunction(MachineFunction &F);
60 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
63 void getAnalysisUsage(AnalysisUsage &AU) const {
64 AsmPrinter::getAnalysisUsage(AU);
68 } // end of anonymous namespace
70 #include "MSP430GenAsmWriter.inc"
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.
77 FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
78 MSP430TargetMachine &tm,
80 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
83 bool MSP430AsmPrinter::doInitialization(Module &M) {
84 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
85 return false; // success
89 bool MSP430AsmPrinter::doFinalization(Module &M) {
90 return AsmPrinter::doFinalization(M);
93 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
94 const Function *F = MF.getFunction();
96 SwitchToSection(TAI->SectionForGlobal(F));
98 unsigned FnAlign = MF.getAlignment();
99 EmitAlignment(FnAlign, F);
101 switch (F->getLinkage()) {
102 default: assert(0 && "Unknown linkage type!");
103 case Function::InternalLinkage: // Symbols default to internal.
104 case Function::PrivateLinkage:
106 case Function::ExternalLinkage:
107 O << "\t.globl\t" << CurrentFnName << '\n';
109 case Function::LinkOnceAnyLinkage:
110 case Function::LinkOnceODRLinkage:
111 case Function::WeakAnyLinkage:
112 case Function::WeakODRLinkage:
113 O << "\t.weak\t" << CurrentFnName << '\n';
117 printVisibility(CurrentFnName, F->getVisibility());
119 O << "\t.type\t" << CurrentFnName << ",@function\n"
120 << CurrentFnName << ":\n";
123 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
124 SetupMachineFunction(MF);
127 // Print the 'header' of function
128 emitFunctionHeader(MF);
130 // Print out code for the function.
131 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
133 // Print a label for the basic block.
134 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
135 // This is an entry block or a block that's only reachable via a
136 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
138 printBasicBlockLabel(I, true, true, VerboseAsm);
142 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
144 // Print the assembly for the instruction.
145 printMachineInstruction(II);
148 if (TAI->hasDotTypeDotSizeDirective())
149 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
153 // We didn't modify anything
157 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
160 // Call the autogenerated instruction printer routines.
161 if (printInstruction(MI))
164 assert(0 && "Should not happen");
167 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
168 const char* Modifier) {
169 const MachineOperand &MO = MI->getOperand(OpNum);
170 switch (MO.getType()) {
171 case MachineOperand::MO_Register:
172 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173 "Virtual registers should be already mapped!");
174 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
176 case MachineOperand::MO_Immediate:
177 if (!Modifier || strcmp(Modifier, "nohash"))
181 case MachineOperand::MO_MachineBasicBlock:
182 printBasicBlockLabel(MO.getMBB());
184 case MachineOperand::MO_GlobalAddress: {
185 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
186 bool isCallOp = Modifier && !strcmp(Modifier, "call");
187 std::string Name = Mang->getValueName(MO.getGlobal());
188 assert(MO.getOffset() == 0 && "No offsets allowed!");
199 case MachineOperand::MO_ExternalSymbol: {
200 bool isCallOp = Modifier && !strcmp(Modifier, "call");
201 std::string Name(TAI->getGlobalPrefix());
202 Name += MO.getSymbolName();
209 assert(0 && "Not implemented yet!");
213 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
214 const char* Modifier) {
215 const MachineOperand &Base = MI->getOperand(OpNum);
216 const MachineOperand &Disp = MI->getOperand(OpNum+1);
219 printOperand(MI, OpNum, "mem");
220 else if (Disp.isImm() && !Base.getReg())
221 printOperand(MI, OpNum);
222 else if (Base.getReg()) {
224 printOperand(MI, OpNum + 1, "nohash");
226 printOperand(MI, OpNum);
230 printOperand(MI, OpNum);
233 assert(0 && "Unsupported memory operand");
236 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
237 unsigned CC = MI->getOperand(OpNum).getImm();
241 assert(0 && "Unsupported CC code");
246 case MSP430::COND_NE:
249 case MSP430::COND_HS:
252 case MSP430::COND_LO:
255 case MSP430::COND_GE: