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/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegistry.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Support/Mangler.h"
36 #include "llvm/Support/ErrorHandling.h"
40 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
45 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
46 const TargetAsmInfo *TAI, bool V)
47 : AsmPrinter(O, TM, TAI, V) {}
49 virtual const char *getPassName() const {
50 return "MSP430 Assembly Printer";
53 void printOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
55 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printCCOperand(const MachineInstr *MI, int OpNum);
58 void printInstruction(const MachineInstr *MI); // autogenerated.
59 void printMachineInstruction(const MachineInstr * MI);
61 void emitFunctionHeader(const MachineFunction &MF);
62 bool runOnMachineFunction(MachineFunction &F);
64 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
65 // FIXME: No support for global variables?
68 void getAnalysisUsage(AnalysisUsage &AU) const {
69 AsmPrinter::getAnalysisUsage(AU);
73 } // end of anonymous namespace
75 #include "MSP430GenAsmWriter.inc"
78 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
79 const Function *F = MF.getFunction();
81 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
83 unsigned FnAlign = MF.getAlignment();
84 EmitAlignment(FnAlign, F);
86 switch (F->getLinkage()) {
87 default: llvm_unreachable("Unknown linkage type!");
88 case Function::InternalLinkage: // Symbols default to internal.
89 case Function::PrivateLinkage:
90 case Function::LinkerPrivateLinkage:
92 case Function::ExternalLinkage:
93 O << "\t.globl\t" << CurrentFnName << '\n';
95 case Function::LinkOnceAnyLinkage:
96 case Function::LinkOnceODRLinkage:
97 case Function::WeakAnyLinkage:
98 case Function::WeakODRLinkage:
99 O << "\t.weak\t" << CurrentFnName << '\n';
103 printVisibility(CurrentFnName, F->getVisibility());
105 O << "\t.type\t" << CurrentFnName << ",@function\n"
106 << CurrentFnName << ":\n";
109 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
110 SetupMachineFunction(MF);
113 // Print the 'header' of function
114 emitFunctionHeader(MF);
116 // Print out code for the function.
117 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
119 // Print a label for the basic block.
120 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
121 // This is an entry block or a block that's only reachable via a
122 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
124 printBasicBlockLabel(I, true, true, VerboseAsm);
128 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
130 // Print the assembly for the instruction.
131 printMachineInstruction(II);
134 if (TAI->hasDotTypeDotSizeDirective())
135 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
137 // We didn't modify anything
141 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
144 // Call the autogenerated instruction printer routines.
145 printInstruction(MI);
148 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
149 const char* Modifier) {
150 const MachineOperand &MO = MI->getOperand(OpNum);
151 switch (MO.getType()) {
152 case MachineOperand::MO_Register:
153 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
154 "Virtual registers should be already mapped!");
155 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
157 case MachineOperand::MO_Immediate:
158 if (!Modifier || strcmp(Modifier, "nohash"))
162 case MachineOperand::MO_MachineBasicBlock:
163 printBasicBlockLabel(MO.getMBB());
165 case MachineOperand::MO_GlobalAddress: {
166 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
167 bool isCallOp = Modifier && !strcmp(Modifier, "call");
168 std::string Name = Mang->getMangledName(MO.getGlobal());
169 assert(MO.getOffset() == 0 && "No offsets allowed!");
180 case MachineOperand::MO_ExternalSymbol: {
181 bool isCallOp = Modifier && !strcmp(Modifier, "call");
182 std::string Name(TAI->getGlobalPrefix());
183 Name += MO.getSymbolName();
190 llvm_unreachable("Not implemented yet!");
194 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
195 const char* Modifier) {
196 const MachineOperand &Base = MI->getOperand(OpNum);
197 const MachineOperand &Disp = MI->getOperand(OpNum+1);
200 printOperand(MI, OpNum, "mem");
201 else if (Disp.isImm() && !Base.getReg())
202 printOperand(MI, OpNum);
203 else if (Base.getReg()) {
205 printOperand(MI, OpNum + 1, "nohash");
207 printOperand(MI, OpNum);
211 printOperand(MI, OpNum);
214 llvm_unreachable("Unsupported memory operand");
217 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
218 unsigned CC = MI->getOperand(OpNum).getImm();
222 llvm_unreachable("Unsupported CC code");
227 case MSP430::COND_NE:
230 case MSP430::COND_HS:
233 case MSP430::COND_LO:
236 case MSP430::COND_GE:
245 extern "C" void LLVMInitializeMSP430Target() {
246 // Register the target.
247 RegisterTargetMachine<MSP430TargetMachine> X(TheMSP430Target);
248 RegisterAsmPrinter<MSP430AsmPrinter> Y(TheMSP430Target);