blob: e8004a7d1b42c592bbf10711911247a7b24a6431 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to the MSP430 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "MSP430.h"
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
35using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39namespace {
40 class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
41 public:
42 MSP430AsmPrinter(raw_ostream &O, MSP430TargetMachine &TM,
43 const TargetAsmInfo *TAI, bool Fast, bool Verbose)
44 : AsmPrinter(O, TM, TAI, Fast, Verbose) {}
45
46 virtual const char *getPassName() const {
47 return "MSP430 Assembly Printer";
48 }
49
Anton Korobeynikov1df221f2009-05-03 13:02:04 +000050 void printOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000051 bool printInstruction(const MachineInstr *MI); // autogenerated.
52 void printMachineInstruction(const MachineInstr * MI);
53 bool runOnMachineFunction(MachineFunction &F);
54 bool doInitialization(Module &M);
55 bool doFinalization(Module &M);
56
57 void getAnalysisUsage(AnalysisUsage &AU) const {
58 AsmPrinter::getAnalysisUsage(AU);
59 AU.setPreservesAll();
60 }
61 };
62} // end of anonymous namespace
63
64#include "MSP430GenAsmWriter.inc"
65
66/// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
67/// assembly code for a MachineFunction to the given output stream,
68/// using the given target machine description. This should work
69/// regardless of whether the function is in SSA form.
70///
71FunctionPass *llvm::createMSP430CodePrinterPass(raw_ostream &o,
72 MSP430TargetMachine &tm,
73 bool fast, bool verbose) {
74 return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), fast, verbose);
75}
76
77bool MSP430AsmPrinter::doInitialization(Module &M) {
78 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
79 return false; // success
80}
81
82
83bool MSP430AsmPrinter::doFinalization(Module &M) {
84 return AsmPrinter::doFinalization(M);
85}
86
Anton Korobeynikov09c42f52009-05-03 13:01:41 +000087bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88 // Print out code for the function.
89 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
90 I != E; ++I) {
91 // Print a label for the basic block.
92 if (I != MF.begin()) {
93 printBasicBlockLabel(I, true , true);
94 O << '\n';
95 }
96
97 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
98 II != E; ++II) {
99 // Print the assembly for the instruction.
100 O << "\t";
101 printMachineInstruction(II);
102 }
103
104 // Each Basic Block is separated by a newline
105 O << '\n';
106 }
107
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000108 // We didn't modify anything
109 return false;
110}
111
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000112void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000113 ++EmittedInsts;
114
115 // Call the autogenerated instruction printer routines.
116 if (printInstruction(MI))
117 return;
118
119 assert(0 && "Should not happen");
120}
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000121
122void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum) {
123 const MachineOperand &MO = MI->getOperand(OpNum);
124 switch (MO.getType()) {
125 case MachineOperand::MO_Register:
126 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
127 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
128 else
129 assert(0 && "not implemented");
130 break;
131 case MachineOperand::MO_Immediate:
132 O << "#" << MO.getImm();
133 break;
134 case MachineOperand::MO_MachineBasicBlock:
135 printBasicBlockLabel(MO.getMBB());
136 break;
137 default:
138 assert(0 && "Not implemented yet!");
139 }
140}