blob: 18151f4c6dee45a035f0017358c5e2855e4cdcd9 [file] [log] [blame]
Nick Lewycky306084e2010-10-02 01:16:59 +00001//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
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 class prints an MSP430 MCInst to a .s file.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "MSP430.h"
Nick Lewycky306084e2010-10-02 01:16:59 +000016#include "MSP430InstPrinter.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FormattedStream.h"
22using namespace llvm;
23
24
25// Include the auto-generated portion of the assembly writer.
26#include "MSP430GenAsmWriter.inc"
27
Owen Andersona0c3b972011-09-15 23:38:46 +000028void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
29 StringRef Annot) {
Nick Lewycky306084e2010-10-02 01:16:59 +000030 printInstruction(MI, O);
Owen Andersona0c3b972011-09-15 23:38:46 +000031 if (CommentStream) printAnnotation(*CommentStream, Annot);
Nick Lewycky306084e2010-10-02 01:16:59 +000032}
33
34void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
35 raw_ostream &O) {
36 const MCOperand &Op = MI->getOperand(OpNo);
37 if (Op.isImm())
38 O << Op.getImm();
39 else {
40 assert(Op.isExpr() && "unknown pcrel immediate operand");
41 O << *Op.getExpr();
42 }
43}
44
45void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
46 raw_ostream &O, const char *Modifier) {
47 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
48 const MCOperand &Op = MI->getOperand(OpNo);
49 if (Op.isReg()) {
50 O << getRegisterName(Op.getReg());
51 } else if (Op.isImm()) {
52 O << '#' << Op.getImm();
53 } else {
54 assert(Op.isExpr() && "unknown operand kind in printOperand");
55 O << '#' << *Op.getExpr();
56 }
57}
58
59void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
60 raw_ostream &O,
61 const char *Modifier) {
62 const MCOperand &Base = MI->getOperand(OpNo);
63 const MCOperand &Disp = MI->getOperand(OpNo+1);
64
65 // Print displacement first
66
67 // If the global address expression is a part of displacement field with a
68 // register base, we should not emit any prefix symbol here, e.g.
69 // mov.w &foo, r1
70 // vs
71 // mov.w glb(r1), r2
72 // Otherwise (!) msp430-as will silently miscompile the output :(
73 if (!Base.getReg())
74 O << '&';
75
76 if (Disp.isExpr())
77 O << *Disp.getExpr();
78 else {
79 assert(Disp.isImm() && "Expected immediate in displacement field");
80 O << Disp.getImm();
81 }
82
83 // Print register base field
84 if (Base.getReg())
85 O << '(' << getRegisterName(Base.getReg()) << ')';
86}
87
88void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
89 raw_ostream &O) {
90 unsigned CC = MI->getOperand(OpNo).getImm();
91
92 switch (CC) {
93 default:
94 llvm_unreachable("Unsupported CC code");
95 break;
96 case MSP430CC::COND_E:
97 O << "eq";
98 break;
99 case MSP430CC::COND_NE:
100 O << "ne";
101 break;
102 case MSP430CC::COND_HS:
103 O << "hs";
104 break;
105 case MSP430CC::COND_LO:
106 O << "lo";
107 break;
108 case MSP430CC::COND_GE:
109 O << "ge";
110 break;
111 case MSP430CC::COND_L:
112 O << 'l';
113 break;
114 }
115}