2010-10-02 09:16:59 +08:00
|
|
|
//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-10-02 09:16:59 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class prints an MSP430 MCInst to a .s file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MSP430InstPrinter.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "MSP430.h"
|
2010-10-02 09:16:59 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2018-11-15 20:29:43 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2010-10-02 09:16:59 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2010-10-02 09:16:59 +08:00
|
|
|
// Include the auto-generated portion of the assembly writer.
|
2018-11-15 20:29:43 +08:00
|
|
|
#define PRINT_ALIAS_INSTR
|
2010-10-02 09:16:59 +08:00
|
|
|
#include "MSP430GenAsmWriter.inc"
|
|
|
|
|
2011-09-16 07:38:46 +08:00
|
|
|
void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
|
2015-03-28 04:36:02 +08:00
|
|
|
StringRef Annot, const MCSubtargetInfo &STI) {
|
2018-11-15 20:29:43 +08:00
|
|
|
if (!printAliasInstr(MI, O))
|
|
|
|
printInstruction(MI, O);
|
2011-09-22 01:58:45 +08:00
|
|
|
printAnnotation(O, Annot);
|
2010-10-02 09:16:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
2018-11-15 20:29:43 +08:00
|
|
|
if (Op.isImm()) {
|
|
|
|
int64_t Imm = Op.getImm() * 2 + 2;
|
|
|
|
O << "$";
|
|
|
|
if (Imm >= 0)
|
|
|
|
O << '+';
|
|
|
|
O << Imm;
|
|
|
|
} else {
|
2010-10-02 09:16:59 +08:00
|
|
|
assert(Op.isExpr() && "unknown pcrel immediate operand");
|
2015-06-09 08:31:39 +08:00
|
|
|
Op.getExpr()->print(O, &MAI);
|
2010-10-02 09:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O, const char *Modifier) {
|
2014-04-28 12:05:08 +08:00
|
|
|
assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
|
2010-10-02 09:16:59 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isReg()) {
|
|
|
|
O << getRegisterName(Op.getReg());
|
|
|
|
} else if (Op.isImm()) {
|
|
|
|
O << '#' << Op.getImm();
|
|
|
|
} else {
|
|
|
|
assert(Op.isExpr() && "unknown operand kind in printOperand");
|
2015-06-09 08:31:39 +08:00
|
|
|
O << '#';
|
|
|
|
Op.getExpr()->print(O, &MAI);
|
2010-10-02 09:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O,
|
|
|
|
const char *Modifier) {
|
|
|
|
const MCOperand &Base = MI->getOperand(OpNo);
|
|
|
|
const MCOperand &Disp = MI->getOperand(OpNo+1);
|
|
|
|
|
|
|
|
// Print displacement first
|
|
|
|
|
|
|
|
// If the global address expression is a part of displacement field with a
|
|
|
|
// register base, we should not emit any prefix symbol here, e.g.
|
|
|
|
// mov.w &foo, r1
|
|
|
|
// vs
|
|
|
|
// mov.w glb(r1), r2
|
|
|
|
// Otherwise (!) msp430-as will silently miscompile the output :(
|
2018-11-15 20:29:43 +08:00
|
|
|
if (Base.getReg() == MSP430::SR)
|
2010-10-02 09:16:59 +08:00
|
|
|
O << '&';
|
|
|
|
|
|
|
|
if (Disp.isExpr())
|
2015-06-09 08:31:39 +08:00
|
|
|
Disp.getExpr()->print(O, &MAI);
|
2010-10-02 09:16:59 +08:00
|
|
|
else {
|
|
|
|
assert(Disp.isImm() && "Expected immediate in displacement field");
|
|
|
|
O << Disp.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print register base field
|
2018-11-15 20:29:43 +08:00
|
|
|
if ((Base.getReg() != MSP430::SR) &&
|
|
|
|
(Base.getReg() != MSP430::PC))
|
2010-10-02 09:16:59 +08:00
|
|
|
O << '(' << getRegisterName(Base.getReg()) << ')';
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:29:43 +08:00
|
|
|
void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &Base = MI->getOperand(OpNo);
|
|
|
|
O << "@" << getRegisterName(Base.getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &Base = MI->getOperand(OpNo);
|
|
|
|
O << "@" << getRegisterName(Base.getReg()) << "+";
|
|
|
|
}
|
|
|
|
|
2010-10-02 09:16:59 +08:00
|
|
|
void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
unsigned CC = MI->getOperand(OpNo).getImm();
|
|
|
|
|
|
|
|
switch (CC) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported CC code");
|
|
|
|
case MSP430CC::COND_E:
|
|
|
|
O << "eq";
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_NE:
|
|
|
|
O << "ne";
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_HS:
|
|
|
|
O << "hs";
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_LO:
|
|
|
|
O << "lo";
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_GE:
|
|
|
|
O << "ge";
|
|
|
|
break;
|
|
|
|
case MSP430CC::COND_L:
|
|
|
|
O << 'l';
|
|
|
|
break;
|
2018-11-15 20:29:43 +08:00
|
|
|
case MSP430CC::COND_N:
|
|
|
|
O << 'n';
|
|
|
|
break;
|
2010-10-02 09:16:59 +08:00
|
|
|
}
|
|
|
|
}
|