blob: 4342c10a1bf206b5b57d5748e53d0f75ac754c09 [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
Nick Lewycky0aee1612010-10-02 01:08:17 +000015#include "MSP430.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "InstPrinter/MSP430InstPrinter.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000017#include "MSP430InstrInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000018#include "MSP430MCInstLower.h"
19#include "MSP430TargetMachine.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000020#include "llvm/CodeGen/AsmPrinter.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000023#include "llvm/CodeGen/MachineInstr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/DerivedTypes.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000027#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
Evan Cheng1705ab02011-07-14 23:50:31 +000029#include "llvm/MC/MCAsmInfo.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000030#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCSymbol.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Nick Lewycky0aee1612010-10-02 01:08:17 +000034#include "llvm/Support/raw_ostream.h"
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "asm-printer"
38
Nick Lewycky0aee1612010-10-02 01:08:17 +000039namespace {
40 class MSP430AsmPrinter : public AsmPrinter {
41 public:
David Blaikie94598322015-01-18 20:29:04 +000042 MSP430AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
43 : AsmPrinter(TM, std::move(Streamer)) {}
Nick Lewycky0aee1612010-10-02 01:08:17 +000044
Craig Topper6f9e59e2014-04-29 07:58:09 +000045 const char *getPassName() const override {
Nick Lewycky0aee1612010-10-02 01:08:17 +000046 return "MSP430 Assembly Printer";
47 }
48
49 void printOperand(const MachineInstr *MI, int OpNum,
Craig Topper062a2ba2014-04-25 05:30:21 +000050 raw_ostream &O, const char* Modifier = nullptr);
Nick Lewycky0aee1612010-10-02 01:08:17 +000051 void printSrcMemOperand(const MachineInstr *MI, int OpNum,
52 raw_ostream &O);
53 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode,
Craig Topper6f9e59e2014-04-29 07:58:09 +000055 raw_ostream &O) override;
Nick Lewycky0aee1612010-10-02 01:08:17 +000056 bool PrintAsmMemoryOperand(const MachineInstr *MI,
57 unsigned OpNo, unsigned AsmVariant,
Craig Topper6f9e59e2014-04-29 07:58:09 +000058 const char *ExtraCode, raw_ostream &O) override;
59 void EmitInstruction(const MachineInstr *MI) override;
Nick Lewycky0aee1612010-10-02 01:08:17 +000060 };
61} // end of anonymous namespace
62
63
64void 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()) {
Craig Toppere55c5562012-02-07 02:50:20 +000068 default: llvm_unreachable("Not implemented yet!");
Nick Lewycky0aee1612010-10-02 01:08:17 +000069 case MachineOperand::MO_Register:
70 O << MSP430InstPrinter::getRegisterName(MO.getReg());
71 return;
72 case MachineOperand::MO_Immediate:
73 if (!Modifier || strcmp(Modifier, "nohash"))
74 O << '#';
75 O << MO.getImm();
76 return;
77 case MachineOperand::MO_MachineBasicBlock:
Matt Arsenault8b643552015-06-09 00:31:39 +000078 MO.getMBB()->getSymbol()->print(O, MAI);
Nick Lewycky0aee1612010-10-02 01:08:17 +000079 return;
80 case MachineOperand::MO_GlobalAddress: {
81 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
82 uint64_t Offset = MO.getOffset();
83
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.
86 // mov.w &foo, r1
87 // vs
88 // mov.w glb(r1), r2
89 // Otherwise (!) msp430-as will silently miscompile the output :(
90 if (!Modifier || strcmp(Modifier, "nohash"))
91 O << (isMemOp ? '&' : '#');
92 if (Offset)
93 O << '(' << Offset << '+';
94
Matt Arsenault8b643552015-06-09 00:31:39 +000095 getSymbol(MO.getGlobal())->print(O, MAI);
Nick Lewycky0aee1612010-10-02 01:08:17 +000096
97 if (Offset)
98 O << ')';
99
100 return;
101 }
Nick Lewycky0aee1612010-10-02 01:08:17 +0000102 }
103}
104
105void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
106 raw_ostream &O) {
107 const MachineOperand &Base = MI->getOperand(OpNum);
108 const MachineOperand &Disp = MI->getOperand(OpNum+1);
109
110 // Print displacement first
111
112 // Imm here is in fact global address - print extra modifier.
113 if (Disp.isImm() && !Base.getReg())
114 O << '&';
115 printOperand(MI, OpNum+1, O, "nohash");
116
117 // Print register base field
118 if (Base.getReg()) {
119 O << '(';
120 printOperand(MI, OpNum, O);
121 O << ')';
122 }
123}
124
125/// PrintAsmOperand - Print out an operand for an inline asm expression.
126///
127bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
128 unsigned AsmVariant,
129 const char *ExtraCode, raw_ostream &O) {
130 // Does this asm operand have a single letter operand modifier?
131 if (ExtraCode && ExtraCode[0])
132 return true; // Unknown modifier.
133
134 printOperand(MI, OpNo, O);
135 return false;
136}
137
138bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
139 unsigned OpNo, unsigned AsmVariant,
140 const char *ExtraCode,
141 raw_ostream &O) {
142 if (ExtraCode && ExtraCode[0]) {
143 return true; // Unknown modifier.
144 }
145 printSrcMemOperand(MI, OpNo, O);
146 return false;
147}
148
149//===----------------------------------------------------------------------===//
150void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
Benjamin Kramer628a39f2012-06-06 18:25:08 +0000151 MSP430MCInstLower MCInstLowering(OutContext, *this);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000152
153 MCInst TmpInst;
154 MCInstLowering.Lower(MI, TmpInst);
Lang Hames9ff69c82015-04-24 19:11:51 +0000155 EmitToStreamer(*OutStreamer, TmpInst);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000156}
157
Nick Lewycky0aee1612010-10-02 01:08:17 +0000158// Force static initialization.
159extern "C" void LLVMInitializeMSP430AsmPrinter() {
160 RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
Nick Lewycky0aee1612010-10-02 01:08:17 +0000161}