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"
34 #include "llvm/Support/ErrorHandling.h"
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
41 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
43 MSP430AsmPrinter(raw_ostream &O, MSP430TargetMachine &TM,
44 const TargetAsmInfo *TAI, bool V)
45 : AsmPrinter(O, TM, TAI, V) {}
47 virtual const char *getPassName() const {
48 return "MSP430 Assembly Printer";
51 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);
55 void printCCOperand(const MachineInstr *MI, int OpNum);
56 bool printInstruction(const MachineInstr *MI); // autogenerated.
57 void printMachineInstruction(const MachineInstr * MI);
59 void emitFunctionHeader(const MachineFunction &MF);
60 bool runOnMachineFunction(MachineFunction &F);
61 bool doInitialization(Module &M);
62 bool doFinalization(Module &M);
64 void getAnalysisUsage(AnalysisUsage &AU) const {
65 AsmPrinter::getAnalysisUsage(AU);
69 } // end of anonymous namespace
71 #include "MSP430GenAsmWriter.inc"
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.
78 FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
79 MSP430TargetMachine &tm,
81 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
84 bool MSP430AsmPrinter::doInitialization(Module &M) {
85 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
86 return false; // success
90 bool MSP430AsmPrinter::doFinalization(Module &M) {
91 return AsmPrinter::doFinalization(M);
94 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
95 const Function *F = MF.getFunction();
97 SwitchToSection(TAI->SectionForGlobal(F));
99 unsigned FnAlign = MF.getAlignment();
100 EmitAlignment(FnAlign, F);
102 switch (F->getLinkage()) {
103 default: llvm_unreachable("Unknown linkage type!");
104 case Function::InternalLinkage: // Symbols default to internal.
105 case Function::PrivateLinkage:
107 case Function::ExternalLinkage:
108 O << "\t.globl\t" << CurrentFnName << '\n';
110 case Function::LinkOnceAnyLinkage:
111 case Function::LinkOnceODRLinkage:
112 case Function::WeakAnyLinkage:
113 case Function::WeakODRLinkage:
114 O << "\t.weak\t" << CurrentFnName << '\n';
118 printVisibility(CurrentFnName, F->getVisibility());
120 O << "\t.type\t" << CurrentFnName << ",@function\n"
121 << CurrentFnName << ":\n";
124 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
125 SetupMachineFunction(MF);
128 // Print the 'header' of function
129 emitFunctionHeader(MF);
131 // Print out code for the function.
132 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
134 // Print a label for the basic block.
135 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
136 // This is an entry block or a block that's only reachable via a
137 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
139 printBasicBlockLabel(I, true, true, VerboseAsm);
143 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
145 // Print the assembly for the instruction.
146 printMachineInstruction(II);
149 if (TAI->hasDotTypeDotSizeDirective())
150 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
154 // We didn't modify anything
158 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
161 // Call the autogenerated instruction printer routines.
162 if (printInstruction(MI))
165 llvm_unreachable("Should not happen");
168 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
169 const char* Modifier) {
170 const MachineOperand &MO = MI->getOperand(OpNum);
171 switch (MO.getType()) {
172 case MachineOperand::MO_Register:
173 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
174 "Virtual registers should be already mapped!");
175 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
177 case MachineOperand::MO_Immediate:
178 if (!Modifier || strcmp(Modifier, "nohash"))
182 case MachineOperand::MO_MachineBasicBlock:
183 printBasicBlockLabel(MO.getMBB());
185 case MachineOperand::MO_GlobalAddress: {
186 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
187 bool isCallOp = Modifier && !strcmp(Modifier, "call");
188 std::string Name = Mang->getValueName(MO.getGlobal());
189 assert(MO.getOffset() == 0 && "No offsets allowed!");
200 case MachineOperand::MO_ExternalSymbol: {
201 bool isCallOp = Modifier && !strcmp(Modifier, "call");
202 std::string Name(TAI->getGlobalPrefix());
203 Name += MO.getSymbolName();
210 llvm_unreachable("Not implemented yet!");
214 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
215 const char* Modifier) {
216 const MachineOperand &Base = MI->getOperand(OpNum);
217 const MachineOperand &Disp = MI->getOperand(OpNum+1);
220 printOperand(MI, OpNum, "mem");
221 else if (Disp.isImm() && !Base.getReg())
222 printOperand(MI, OpNum);
223 else if (Base.getReg()) {
225 printOperand(MI, OpNum + 1, "nohash");
227 printOperand(MI, OpNum);
231 printOperand(MI, OpNum);
234 llvm_unreachable("Unsupported memory operand");
237 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
238 unsigned CC = MI->getOperand(OpNum).getImm();
242 llvm_unreachable("Unsupported CC code");
247 case MSP430::COND_NE:
250 case MSP430::COND_HS:
253 case MSP430::COND_LO:
256 case MSP430::COND_GE: