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 "MSP430TargetAsmInfo.h"
19 #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"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetRegistry.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/ErrorHandling.h"
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
46 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
47 const TargetAsmInfo *TAI, bool V)
48 : AsmPrinter(O, TM, TAI, V) {}
50 virtual const char *getPassName() const {
51 return "MSP430 Assembly Printer";
54 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);
58 void printCCOperand(const MachineInstr *MI, int OpNum);
59 void printInstruction(const MachineInstr *MI); // autogenerated.
60 void printMachineInstruction(const MachineInstr * MI);
62 void emitFunctionHeader(const MachineFunction &MF);
63 bool runOnMachineFunction(MachineFunction &F);
65 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
66 // FIXME: No support for global variables?
69 void getAnalysisUsage(AnalysisUsage &AU) const {
70 AsmPrinter::getAnalysisUsage(AU);
74 } // end of anonymous namespace
76 #include "MSP430GenAsmWriter.inc"
79 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
80 const Function *F = MF.getFunction();
82 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
84 unsigned FnAlign = MF.getAlignment();
85 EmitAlignment(FnAlign, F);
87 switch (F->getLinkage()) {
88 default: llvm_unreachable("Unknown linkage type!");
89 case Function::InternalLinkage: // Symbols default to internal.
90 case Function::PrivateLinkage:
91 case Function::LinkerPrivateLinkage:
93 case Function::ExternalLinkage:
94 O << "\t.globl\t" << CurrentFnName << '\n';
96 case Function::LinkOnceAnyLinkage:
97 case Function::LinkOnceODRLinkage:
98 case Function::WeakAnyLinkage:
99 case Function::WeakODRLinkage:
100 O << "\t.weak\t" << CurrentFnName << '\n';
104 printVisibility(CurrentFnName, F->getVisibility());
106 O << "\t.type\t" << CurrentFnName << ",@function\n"
107 << CurrentFnName << ":\n";
110 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
111 SetupMachineFunction(MF);
114 // Print the 'header' of function
115 emitFunctionHeader(MF);
117 // Print out code for the function.
118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
120 // Print a label for the basic block.
121 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
122 // This is an entry block or a block that's only reachable via a
123 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
125 printBasicBlockLabel(I, true, true, VerboseAsm);
129 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
131 // Print the assembly for the instruction.
132 printMachineInstruction(II);
135 if (TAI->hasDotTypeDotSizeDirective())
136 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
138 // We didn't modify anything
142 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
145 // Call the autogenerated instruction printer routines.
146 printInstruction(MI);
149 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
150 const char* Modifier) {
151 const MachineOperand &MO = MI->getOperand(OpNum);
152 switch (MO.getType()) {
153 case MachineOperand::MO_Register:
154 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
155 "Virtual registers should be already mapped!");
156 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
158 case MachineOperand::MO_Immediate:
159 if (!Modifier || strcmp(Modifier, "nohash"))
163 case MachineOperand::MO_MachineBasicBlock:
164 printBasicBlockLabel(MO.getMBB());
166 case MachineOperand::MO_GlobalAddress: {
167 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
168 bool isCallOp = Modifier && !strcmp(Modifier, "call");
169 std::string Name = Mang->getMangledName(MO.getGlobal());
170 assert(MO.getOffset() == 0 && "No offsets allowed!");
181 case MachineOperand::MO_ExternalSymbol: {
182 bool isCallOp = Modifier && !strcmp(Modifier, "call");
183 std::string Name(TAI->getGlobalPrefix());
184 Name += MO.getSymbolName();
191 llvm_unreachable("Not implemented yet!");
195 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
196 const char* Modifier) {
197 const MachineOperand &Base = MI->getOperand(OpNum);
198 const MachineOperand &Disp = MI->getOperand(OpNum+1);
201 printOperand(MI, OpNum, "mem");
202 else if (Disp.isImm() && !Base.getReg())
203 printOperand(MI, OpNum);
204 else if (Base.getReg()) {
206 printOperand(MI, OpNum + 1, "nohash");
208 printOperand(MI, OpNum);
212 printOperand(MI, OpNum);
215 llvm_unreachable("Unsupported memory operand");
218 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
219 unsigned CC = MI->getOperand(OpNum).getImm();
223 llvm_unreachable("Unsupported CC code");
228 case MSP430::COND_NE:
231 case MSP430::COND_HS:
234 case MSP430::COND_LO:
237 case MSP430::COND_GE:
246 // Force static initialization.
247 extern "C" void LLVMInitializeMSP430AsmPrinter() {
248 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);