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 "InstPrinter/MSP430InstPrinter.h"
18 #include "MSP430InstrInfo.h"
19 #include "MSP430MCInstLower.h"
20 #include "MSP430TargetMachine.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCInst.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/Mangler.h"
40 class MSP430AsmPrinter : public AsmPrinter {
42 MSP430AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
43 : AsmPrinter(TM, Streamer) {}
45 virtual const char *getPassName() const {
46 return "MSP430 Assembly Printer";
49 void printOperand(const MachineInstr *MI, int OpNum,
50 raw_ostream &O, const char* Modifier = 0);
51 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
53 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode,
56 bool PrintAsmMemoryOperand(const MachineInstr *MI,
57 unsigned OpNo, unsigned AsmVariant,
58 const char *ExtraCode, raw_ostream &O);
59 void EmitInstruction(const MachineInstr *MI);
61 } // end of anonymous namespace
64 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
65 raw_ostream &O, const char *Modifier) {
66 const MachineOperand &MO = MI->getOperand(OpNum);
67 switch (MO.getType()) {
68 default: llvm_unreachable("Not implemented yet!");
69 case MachineOperand::MO_Register:
70 O << MSP430InstPrinter::getRegisterName(MO.getReg());
72 case MachineOperand::MO_Immediate:
73 if (!Modifier || strcmp(Modifier, "nohash"))
77 case MachineOperand::MO_MachineBasicBlock:
78 O << *MO.getMBB()->getSymbol();
80 case MachineOperand::MO_GlobalAddress: {
81 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
82 uint64_t Offset = MO.getOffset();
84 // If the global address expression is a part of displacement field with a
85 // register base, we should not emit any prefix symbol here, e.g.
89 // Otherwise (!) msp430-as will silently miscompile the output :(
90 if (!Modifier || strcmp(Modifier, "nohash"))
91 O << (isMemOp ? '&' : '#');
93 O << '(' << Offset << '+';
95 O << *getSymbol(MO.getGlobal());
102 case MachineOperand::MO_ExternalSymbol: {
103 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
104 O << (isMemOp ? '&' : '#');
105 SmallString<128> Name;
106 Mang->getNameWithPrefix(Name, MO.getSymbolName());
113 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
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())
123 printOperand(MI, OpNum+1, O, "nohash");
125 // Print register base field
128 printOperand(MI, OpNum, O);
133 /// PrintAsmOperand - Print out an operand for an inline asm expression.
135 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
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);
146 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
147 unsigned OpNo, unsigned AsmVariant,
148 const char *ExtraCode,
150 if (ExtraCode && ExtraCode[0]) {
151 return true; // Unknown modifier.
153 printSrcMemOperand(MI, OpNo, O);
157 //===----------------------------------------------------------------------===//
158 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
159 MSP430MCInstLower MCInstLowering(OutContext, *this);
162 MCInstLowering.Lower(MI, TmpInst);
163 OutStreamer.EmitInstruction(TmpInst);
166 // Force static initialization.
167 extern "C" void LLVMInitializeMSP430AsmPrinter() {
168 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);