//===-- W65816MCInstLower.cpp - Convert W65816 MachineInstr to MCInst -----===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains code to lower W65816 MachineInstrs to their
// corresponding MCInst records, following the MSP430 pattern.  It handles
// register, immediate, global/external-symbol, block-address, jump-table,
// and constant-pool operands (see LowerSymbolOperand).
//
//===----------------------------------------------------------------------===//

#include "W65816MCInstLower.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"

using namespace llvm;

MCSymbol *
W65816MCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
  assert(MO.getTargetFlags() == 0 && "unknown target flag on GV operand");
  return Printer.getSymbol(MO.getGlobal());
}

MCSymbol *
W65816MCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {
  assert(MO.getTargetFlags() == 0 && "unknown target flag on ES operand");
  return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
}

MCSymbol *W65816MCInstLower::GetJumpTableSymbol(const MachineOperand &MO) const {
  const DataLayout &DL = Printer.getDataLayout();
  SmallString<256> Name;
  raw_svector_ostream(Name)
      << DL.getInternalSymbolPrefix() << "JTI" << Printer.getFunctionNumber()
      << '_' << MO.getIndex();
  assert(MO.getTargetFlags() == 0 && "unknown target flag on JT operand");
  return Ctx.getOrCreateSymbol(Name);
}

MCSymbol *
W65816MCInstLower::GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
  const DataLayout &DL = Printer.getDataLayout();
  SmallString<256> Name;
  raw_svector_ostream(Name)
      << DL.getInternalSymbolPrefix() << "CPI" << Printer.getFunctionNumber()
      << '_' << MO.getIndex();
  assert(MO.getTargetFlags() == 0 && "unknown target flag on CPI operand");
  return Ctx.getOrCreateSymbol(Name);
}

MCSymbol *
W65816MCInstLower::GetBlockAddressSymbol(const MachineOperand &MO) const {
  assert(MO.getTargetFlags() == 0 && "unknown target flag on BA operand");
  return Printer.GetBlockAddressSymbol(MO.getBlockAddress());
}

MCOperand W65816MCInstLower::LowerSymbolOperand(const MachineOperand &MO,
                                                MCSymbol *Sym) const {
  const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
  if (!MO.isJTI() && MO.getOffset())
    Expr = MCBinaryExpr::createAdd(
        Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
  return MCOperand::createExpr(Expr);
}

void W65816MCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
  OutMI.setOpcode(MI->getOpcode());

  for (const MachineOperand &MO : MI->operands()) {
    MCOperand MCOp;
    switch (MO.getType()) {
    default:
      MI->print(errs());
      llvm_unreachable("W65816: unknown operand type in MCInstLower");
    case MachineOperand::MO_Register:
      if (MO.isImplicit())
        continue;
      MCOp = MCOperand::createReg(MO.getReg());
      break;
    case MachineOperand::MO_Immediate:
      MCOp = MCOperand::createImm(MO.getImm());
      break;
    case MachineOperand::MO_MachineBasicBlock:
      MCOp = MCOperand::createExpr(
          MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
      break;
    case MachineOperand::MO_GlobalAddress:
      MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
      break;
    case MachineOperand::MO_ExternalSymbol:
      MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
      break;
    case MachineOperand::MO_JumpTableIndex:
      MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
      break;
    case MachineOperand::MO_ConstantPoolIndex:
      MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
      break;
    case MachineOperand::MO_BlockAddress:
      MCOp = LowerSymbolOperand(MO, GetBlockAddressSymbol(MO));
      break;
    case MachineOperand::MO_RegisterMask:
      continue;
    }

    OutMI.addOperand(MCOp);
  }
}
