blob: 91065d86bc173684fa9d387db7dcb08f41afcea9 [file] [log] [blame]
Nick Lewycky0aee1612010-10-02 01:08:17 +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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "InstPrinter/MSP430InstPrinter.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000018#include "MSP430InstrInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000019#include "MSP430MCInstLower.h"
20#include "MSP430TargetMachine.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000021#include "llvm/CodeGen/AsmPrinter.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000024#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
27#include "llvm/IR/DerivedTypes.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000028#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/Module.h"
Evan Cheng1705ab02011-07-14 23:50:31 +000030#include "llvm/MC/MCAsmInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000031#include "llvm/MC/MCInst.h"
32#include "llvm/MC/MCStreamer.h"
33#include "llvm/MC/MCSymbol.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000035#include "llvm/Support/raw_ostream.h"
36using namespace llvm;
37
38namespace {
39 class MSP430AsmPrinter : public AsmPrinter {
40 public:
41 MSP430AsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
42 : AsmPrinter(TM, Streamer) {}
43
44 virtual const char *getPassName() const {
45 return "MSP430 Assembly Printer";
46 }
47
48 void printOperand(const MachineInstr *MI, int OpNum,
49 raw_ostream &O, const char* Modifier = 0);
50 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
51 raw_ostream &O);
52 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53 unsigned AsmVariant, const char *ExtraCode,
54 raw_ostream &O);
55 bool PrintAsmMemoryOperand(const MachineInstr *MI,
56 unsigned OpNo, unsigned AsmVariant,
57 const char *ExtraCode, raw_ostream &O);
58 void EmitInstruction(const MachineInstr *MI);
59 };
60} // end of anonymous namespace
61
62
63void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
64 raw_ostream &O, const char *Modifier) {
65 const MachineOperand &MO = MI->getOperand(OpNum);
66 switch (MO.getType()) {
Craig Toppere55c5562012-02-07 02:50:20 +000067 default: llvm_unreachable("Not implemented yet!");
Nick Lewycky0aee1612010-10-02 01:08:17 +000068 case MachineOperand::MO_Register:
69 O << MSP430InstPrinter::getRegisterName(MO.getReg());
70 return;
71 case MachineOperand::MO_Immediate:
72 if (!Modifier || strcmp(Modifier, "nohash"))
73 O << '#';
74 O << MO.getImm();
75 return;
76 case MachineOperand::MO_MachineBasicBlock:
77 O << *MO.getMBB()->getSymbol();
78 return;
79 case MachineOperand::MO_GlobalAddress: {
80 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
81 uint64_t Offset = MO.getOffset();
82
83 // If the global address expression is a part of displacement field with a
84 // register base, we should not emit any prefix symbol here, e.g.
85 // mov.w &foo, r1
86 // vs
87 // mov.w glb(r1), r2
88 // Otherwise (!) msp430-as will silently miscompile the output :(
89 if (!Modifier || strcmp(Modifier, "nohash"))
90 O << (isMemOp ? '&' : '#');
91 if (Offset)
92 O << '(' << Offset << '+';
93
Rafael Espindola79858aa2013-10-29 17:07:16 +000094 O << *getSymbol(MO.getGlobal());
Nick Lewycky0aee1612010-10-02 01:08:17 +000095
96 if (Offset)
97 O << ')';
98
99 return;
100 }
Nick Lewycky0aee1612010-10-02 01:08:17 +0000101 }
102}
103
104void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
105 raw_ostream &O) {
106 const MachineOperand &Base = MI->getOperand(OpNum);
107 const MachineOperand &Disp = MI->getOperand(OpNum+1);
108
109 // Print displacement first
110
111 // Imm here is in fact global address - print extra modifier.
112 if (Disp.isImm() && !Base.getReg())
113 O << '&';
114 printOperand(MI, OpNum+1, O, "nohash");
115
116 // Print register base field
117 if (Base.getReg()) {
118 O << '(';
119 printOperand(MI, OpNum, O);
120 O << ')';
121 }
122}
123
124/// PrintAsmOperand - Print out an operand for an inline asm expression.
125///
126bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
127 unsigned AsmVariant,
128 const char *ExtraCode, raw_ostream &O) {
129 // Does this asm operand have a single letter operand modifier?
130 if (ExtraCode && ExtraCode[0])
131 return true; // Unknown modifier.
132
133 printOperand(MI, OpNo, O);
134 return false;
135}
136
137bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
138 unsigned OpNo, unsigned AsmVariant,
139 const char *ExtraCode,
140 raw_ostream &O) {
141 if (ExtraCode && ExtraCode[0]) {
142 return true; // Unknown modifier.
143 }
144 printSrcMemOperand(MI, OpNo, O);
145 return false;
146}
147
148//===----------------------------------------------------------------------===//
149void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
Benjamin Kramer628a39f2012-06-06 18:25:08 +0000150 MSP430MCInstLower MCInstLowering(OutContext, *this);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000151
152 MCInst TmpInst;
153 MCInstLowering.Lower(MI, TmpInst);
David Woodhousee6c13e42014-01-28 23:12:42 +0000154 EmitToStreamer(OutStreamer, TmpInst);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000155}
156
Nick Lewycky0aee1612010-10-02 01:08:17 +0000157// Force static initialization.
158extern "C" void LLVMInitializeMSP430AsmPrinter() {
159 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000160}