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/TargetRegistry.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Support/Mangler.h"
35 #include "llvm/Support/ErrorHandling.h"
39 STATISTIC(EmittedInsts, "Number of machine instrs printed");
42 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
44 MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
45 const TargetAsmInfo *TAI, bool V)
46 : AsmPrinter(O, TM, TAI, V) {}
48 virtual const char *getPassName() const {
49 return "MSP430 Assembly Printer";
52 void printOperand(const MachineInstr *MI, int OpNum,
53 const char* Modifier = 0);
54 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
56 void printCCOperand(const MachineInstr *MI, int OpNum);
57 bool printInstruction(const MachineInstr *MI); // autogenerated.
58 void printMachineInstruction(const MachineInstr * MI);
60 void emitFunctionHeader(const MachineFunction &MF);
61 bool runOnMachineFunction(MachineFunction &F);
63 virtual void PrintGlobalVariable(const GlobalVariable *GV) {
64 // FIXME: No support for global variables?
67 void getAnalysisUsage(AnalysisUsage &AU) const {
68 AsmPrinter::getAnalysisUsage(AU);
72 } // end of anonymous namespace
74 #include "MSP430GenAsmWriter.inc"
77 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
78 const Function *F = MF.getFunction();
80 SwitchToSection(TAI->SectionForGlobal(F));
82 unsigned FnAlign = MF.getAlignment();
83 EmitAlignment(FnAlign, F);
85 switch (F->getLinkage()) {
86 default: llvm_unreachable("Unknown linkage type!");
87 case Function::InternalLinkage: // Symbols default to internal.
88 case Function::PrivateLinkage:
89 case Function::LinkerPrivateLinkage:
91 case Function::ExternalLinkage:
92 O << "\t.globl\t" << CurrentFnName << '\n';
94 case Function::LinkOnceAnyLinkage:
95 case Function::LinkOnceODRLinkage:
96 case Function::WeakAnyLinkage:
97 case Function::WeakODRLinkage:
98 O << "\t.weak\t" << CurrentFnName << '\n';
102 printVisibility(CurrentFnName, F->getVisibility());
104 O << "\t.type\t" << CurrentFnName << ",@function\n"
105 << CurrentFnName << ":\n";
108 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
109 SetupMachineFunction(MF);
112 // Print the 'header' of function
113 emitFunctionHeader(MF);
115 // Print out code for the function.
116 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 // Print a label for the basic block.
119 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
120 // This is an entry block or a block that's only reachable via a
121 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
123 printBasicBlockLabel(I, true, true, VerboseAsm);
127 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
129 // Print the assembly for the instruction.
130 printMachineInstruction(II);
133 if (TAI->hasDotTypeDotSizeDirective())
134 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 if (printInstruction(MI))
149 llvm_unreachable("Should not happen");
152 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
153 const char* Modifier) {
154 const MachineOperand &MO = MI->getOperand(OpNum);
155 switch (MO.getType()) {
156 case MachineOperand::MO_Register:
157 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
158 "Virtual registers should be already mapped!");
159 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
161 case MachineOperand::MO_Immediate:
162 if (!Modifier || strcmp(Modifier, "nohash"))
166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMBB());
169 case MachineOperand::MO_GlobalAddress: {
170 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
171 bool isCallOp = Modifier && !strcmp(Modifier, "call");
172 std::string Name = Mang->getMangledName(MO.getGlobal());
173 assert(MO.getOffset() == 0 && "No offsets allowed!");
184 case MachineOperand::MO_ExternalSymbol: {
185 bool isCallOp = Modifier && !strcmp(Modifier, "call");
186 std::string Name(TAI->getGlobalPrefix());
187 Name += MO.getSymbolName();
194 llvm_unreachable("Not implemented yet!");
198 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
199 const char* Modifier) {
200 const MachineOperand &Base = MI->getOperand(OpNum);
201 const MachineOperand &Disp = MI->getOperand(OpNum+1);
204 printOperand(MI, OpNum, "mem");
205 else if (Disp.isImm() && !Base.getReg())
206 printOperand(MI, OpNum);
207 else if (Base.getReg()) {
209 printOperand(MI, OpNum + 1, "nohash");
211 printOperand(MI, OpNum);
215 printOperand(MI, OpNum);
218 llvm_unreachable("Unsupported memory operand");
221 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
222 unsigned CC = MI->getOperand(OpNum).getImm();
226 llvm_unreachable("Unsupported CC code");
231 case MSP430::COND_NE:
234 case MSP430::COND_HS:
237 case MSP430::COND_LO:
240 case MSP430::COND_GE:
249 extern "C" void LLVMInitializeMSP430Target() {
250 // Register the target.
251 RegisterTargetMachine<MSP430TargetMachine> X(TheMSP430Target);
252 RegisterAsmPrinter<MSP430AsmPrinter> Y(TheMSP430Target);