Fixed some bugs.
[llvm/zpu.git] / lib / Target / MSP430 / MSP430AsmPrinter.cpp
bloba1a7f44c19c484cab303e35dd08b2890cb75952a
1 //===-- 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.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430MCAsmInfo.h"
19 #include "MSP430MCInstLower.h"
20 #include "MSP430TargetMachine.h"
21 #include "InstPrinter/MSP430InstPrinter.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineConstantPool.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/MC/MCInst.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Target/Mangler.h"
35 #include "llvm/Target/TargetData.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegistry.h"
38 #include "llvm/Support/raw_ostream.h"
39 using namespace llvm;
41 namespace {
42 class MSP430AsmPrinter : public AsmPrinter {
43 public:
44 MSP430AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
45 : AsmPrinter(TM, Streamer) {}
47 virtual const char *getPassName() const {
48 return "MSP430 Assembly Printer";
51 void printOperand(const MachineInstr *MI, int OpNum,
52 raw_ostream &O, const char* Modifier = 0);
53 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54 raw_ostream &O);
55 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode,
57 raw_ostream &O);
58 bool PrintAsmMemoryOperand(const MachineInstr *MI,
59 unsigned OpNo, unsigned AsmVariant,
60 const char *ExtraCode, raw_ostream &O);
61 void EmitInstruction(const MachineInstr *MI);
63 } // end of anonymous namespace
66 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
67 raw_ostream &O, const char *Modifier) {
68 const MachineOperand &MO = MI->getOperand(OpNum);
69 switch (MO.getType()) {
70 default: assert(0 && "Not implemented yet!");
71 case MachineOperand::MO_Register:
72 O << MSP430InstPrinter::getRegisterName(MO.getReg());
73 return;
74 case MachineOperand::MO_Immediate:
75 if (!Modifier || strcmp(Modifier, "nohash"))
76 O << '#';
77 O << MO.getImm();
78 return;
79 case MachineOperand::MO_MachineBasicBlock:
80 O << *MO.getMBB()->getSymbol();
81 return;
82 case MachineOperand::MO_GlobalAddress: {
83 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
84 uint64_t Offset = MO.getOffset();
86 // If the global address expression is a part of displacement field with a
87 // register base, we should not emit any prefix symbol here, e.g.
88 // mov.w &foo, r1
89 // vs
90 // mov.w glb(r1), r2
91 // Otherwise (!) msp430-as will silently miscompile the output :(
92 if (!Modifier || strcmp(Modifier, "nohash"))
93 O << (isMemOp ? '&' : '#');
94 if (Offset)
95 O << '(' << Offset << '+';
97 O << *Mang->getSymbol(MO.getGlobal());
99 if (Offset)
100 O << ')';
102 return;
104 case MachineOperand::MO_ExternalSymbol: {
105 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
106 O << (isMemOp ? '&' : '#');
107 O << MAI->getGlobalPrefix() << MO.getSymbolName();
108 return;
113 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
114 raw_ostream &O) {
115 const MachineOperand &Base = MI->getOperand(OpNum);
116 const MachineOperand &Disp = MI->getOperand(OpNum+1);
118 // Print displacement first
120 // Imm here is in fact global address - print extra modifier.
121 if (Disp.isImm() && !Base.getReg())
122 O << '&';
123 printOperand(MI, OpNum+1, O, "nohash");
125 // Print register base field
126 if (Base.getReg()) {
127 O << '(';
128 printOperand(MI, OpNum, O);
129 O << ')';
133 /// PrintAsmOperand - Print out an operand for an inline asm expression.
135 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
136 unsigned AsmVariant,
137 const char *ExtraCode, raw_ostream &O) {
138 // Does this asm operand have a single letter operand modifier?
139 if (ExtraCode && ExtraCode[0])
140 return true; // Unknown modifier.
142 printOperand(MI, OpNo, O);
143 return false;
146 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
147 unsigned OpNo, unsigned AsmVariant,
148 const char *ExtraCode,
149 raw_ostream &O) {
150 if (ExtraCode && ExtraCode[0]) {
151 return true; // Unknown modifier.
153 printSrcMemOperand(MI, OpNo, O);
154 return false;
157 //===----------------------------------------------------------------------===//
158 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
159 MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
161 MCInst TmpInst;
162 MCInstLowering.Lower(MI, TmpInst);
163 OutStreamer.EmitInstruction(TmpInst);
166 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
167 unsigned SyntaxVariant,
168 const MCAsmInfo &MAI) {
169 if (SyntaxVariant == 0)
170 return new MSP430InstPrinter(MAI);
171 return 0;
174 // Force static initialization.
175 extern "C" void LLVMInitializeMSP430AsmPrinter() {
176 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
177 TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
178 createMSP430MCInstPrinter);