Path: blob/main/contrib/llvm-project/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
35294 views
//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This class prints an MSP430 MCInst to a .s file.9//10//===----------------------------------------------------------------------===//1112#include "MSP430InstPrinter.h"13#include "MSP430.h"14#include "llvm/MC/MCAsmInfo.h"15#include "llvm/MC/MCExpr.h"16#include "llvm/MC/MCInst.h"17#include "llvm/MC/MCInstrInfo.h"18#include "llvm/Support/ErrorHandling.h"19#include "llvm/Support/FormattedStream.h"20using namespace llvm;2122#define DEBUG_TYPE "asm-printer"2324// Include the auto-generated portion of the assembly writer.25#define PRINT_ALIAS_INSTR26#include "MSP430GenAsmWriter.inc"2728void MSP430InstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const {29O << getRegisterName(Reg);30}3132void MSP430InstPrinter::printInst(const MCInst *MI, uint64_t Address,33StringRef Annot, const MCSubtargetInfo &STI,34raw_ostream &O) {35if (!printAliasInstr(MI, Address, O))36printInstruction(MI, Address, O);37printAnnotation(O, Annot);38}3940void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,41raw_ostream &O) {42const MCOperand &Op = MI->getOperand(OpNo);43if (Op.isImm()) {44int64_t Imm = Op.getImm() * 2 + 2;45O << "$";46if (Imm >= 0)47O << '+';48O << Imm;49} else {50assert(Op.isExpr() && "unknown pcrel immediate operand");51Op.getExpr()->print(O, &MAI);52}53}5455void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,56raw_ostream &O, const char *Modifier) {57assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");58const MCOperand &Op = MI->getOperand(OpNo);59if (Op.isReg()) {60O << getRegisterName(Op.getReg());61} else if (Op.isImm()) {62O << '#' << Op.getImm();63} else {64assert(Op.isExpr() && "unknown operand kind in printOperand");65O << '#';66Op.getExpr()->print(O, &MAI);67}68}6970void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,71raw_ostream &O,72const char *Modifier) {73const MCOperand &Base = MI->getOperand(OpNo);74const MCOperand &Disp = MI->getOperand(OpNo+1);7576// Print displacement first7778// If the global address expression is a part of displacement field with a79// register base, we should not emit any prefix symbol here, e.g.80// mov.w &foo, r181// vs82// mov.w glb(r1), r283// Otherwise (!) msp430-as will silently miscompile the output :(84if (Base.getReg() == MSP430::SR)85O << '&';8687if (Disp.isExpr())88Disp.getExpr()->print(O, &MAI);89else {90assert(Disp.isImm() && "Expected immediate in displacement field");91O << Disp.getImm();92}9394// Print register base field95if ((Base.getReg() != MSP430::SR) &&96(Base.getReg() != MSP430::PC))97O << '(' << getRegisterName(Base.getReg()) << ')';98}99100void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,101raw_ostream &O) {102const MCOperand &Base = MI->getOperand(OpNo);103O << "@" << getRegisterName(Base.getReg());104}105106void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,107raw_ostream &O) {108const MCOperand &Base = MI->getOperand(OpNo);109O << "@" << getRegisterName(Base.getReg()) << "+";110}111112void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,113raw_ostream &O) {114unsigned CC = MI->getOperand(OpNo).getImm();115116switch (CC) {117default:118llvm_unreachable("Unsupported CC code");119case MSP430CC::COND_E:120O << "eq";121break;122case MSP430CC::COND_NE:123O << "ne";124break;125case MSP430CC::COND_HS:126O << "hs";127break;128case MSP430CC::COND_LO:129O << "lo";130break;131case MSP430CC::COND_GE:132O << "ge";133break;134case MSP430CC::COND_L:135O << 'l';136break;137case MSP430CC::COND_N:138O << 'n';139break;140}141}142143144