15 changes: 5 additions & 10 deletions llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
#ifndef LLVM_LIB_TARGET_M68K_INSTPRINTER_M68KINSTPRINTER_H
#define LLVM_LIB_TARGET_M68K_INSTPRINTER_M68KINSTPRINTER_H

#include "M68kMemOperandPrinter.h"
#include "llvm/MC/MCInstPrinter.h"

namespace llvm {

class TargetMachine;

class M68kInstPrinter : public MCInstPrinter {
class M68kInstPrinter : public MCInstPrinter,
public M68kMemOperandPrinter<M68kInstPrinter, MCInst> {
friend class M68kMemOperandPrinter;

public:
M68kInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
const MCRegisterInfo &MRI)
Expand Down Expand Up @@ -48,16 +52,7 @@ class M68kInstPrinter : public MCInstPrinter {
/// Print register mask for MOVEM instruction in order A7-A0,D7-D0
void printMoveMaskR(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printDisp(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printARIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printARIPIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printARIPDMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printARIDMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printARIIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printAbsMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
void printPCDMem(const MCInst *MI, uint64_t Address, unsigned opNum,
raw_ostream &O);
void printPCIMem(const MCInst *MI, uint64_t Address, unsigned opNum,
raw_ostream &O);

//===----------------------------------------------------------------------===//
// Specializations
Expand Down
80 changes: 80 additions & 0 deletions llvm/lib/Target/M68k/MCTargetDesc/M68kMemOperandPrinter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//===-- M68kMemOperandPrinter.h - Memory operands printing ------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains memory operand printing logics shared between AsmPrinter
// and MCInstPrinter.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_M68K_MEMOPERANDPRINTER_M68KINSTPRINTER_H
#define LLVM_LIB_TARGET_M68K_MEMOPERANDPRINTER_M68KINSTPRINTER_H

#include "M68kBaseInfo.h"

#include "llvm/Support/raw_ostream.h"

namespace llvm {
template <class Derived, typename InstTy> class M68kMemOperandPrinter {
Derived &impl() { return *static_cast<Derived *>(this); }

protected:
void printARIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
O << '(';
impl().printOperand(MI, OpNum, O);
O << ')';
}

void printARIPIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
O << "(";
impl().printOperand(MI, OpNum, O);
O << ")+";
}

void printARIPDMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
O << "-(";
impl().printOperand(MI, OpNum, O);
O << ")";
}

void printARIDMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
O << '(';
impl().printDisp(MI, OpNum + M68k::MemDisp, O);
O << ',';
impl().printOperand(MI, OpNum + M68k::MemBase, O);
O << ')';
}

void printARIIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
O << '(';
impl().printDisp(MI, OpNum + M68k::MemDisp, O);
O << ',';
impl().printOperand(MI, OpNum + M68k::MemBase, O);
O << ',';
impl().printOperand(MI, OpNum + M68k::MemIndex, O);
O << ')';
}

void printPCDMem(const InstTy *MI, uint64_t Address, unsigned OpNum,
raw_ostream &O) {
O << '(';
impl().printDisp(MI, OpNum + M68k::PCRelDisp, O);
O << ",%pc)";
}

void printPCIMem(const InstTy *MI, uint64_t Address, unsigned OpNum,
raw_ostream &O) {
O << '(';
impl().printDisp(MI, OpNum + M68k::PCRelDisp, O);
O << ",%pc,";
impl().printOperand(MI, OpNum + M68k::PCRelIndex, O);
O << ')';
}
};
} // end namespace llvm
#endif
32 changes: 32 additions & 0 deletions llvm/test/CodeGen/M68k/inline-asm.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -mtriple=m68k < %s -o - | FileCheck %s

@g = internal global i32 10, align 4

; This function is primarily testing constant constraints that can NOT
; be easily checked by Clang. For example, 'K' and 'M' are both
; constraints for values that are outside certain numerical range.
Expand Down Expand Up @@ -120,3 +122,33 @@ entry:
ret void
}

define void @memory_constraints() {
; CHECK-LABEL: memory_constraints:
; CHECK: .cfi_startproc
; CHECK-NEXT: ; %bb.0: ; %entry
; CHECK-NEXT: suba.l #4, %sp
; CHECK-NEXT: .cfi_def_cfa_offset -8
; CHECK-NEXT: ;APP
; CHECK-NEXT: move.l (0,%sp), %d1
; CHECK-NEXT: ;NO_APP
; CHECK-NEXT: ;APP
; CHECK-NEXT: move.l (g,%pc), %d2
; CHECK-NEXT: ;NO_APP
; CHECK-NEXT: lea (0,%sp), %a0
; CHECK-NEXT: ;APP
; CHECK-NEXT: move.l (%a0), %d3
; CHECK-NEXT: ;NO_APP
; CHECK-NEXT: ;APP
; CHECK-NEXT: move.l (0,%sp), %d4
; CHECK-NEXT: ;NO_APP
; CHECK-NEXT: adda.l #4, %sp
; CHECK-NEXT: rts
entry:
%x = alloca i32, align 4
call void asm sideeffect "move.l $0, %d1", "*m"(ptr elementtype(i32) %x)
call void asm sideeffect "move.l $0, %d2", "*m"(ptr elementtype(i32) @g)
call void asm sideeffect "move.l $0, %d3", "*Q"(ptr elementtype(i32) %x)
call void asm sideeffect "move.l $0, %d4", "*U"(ptr elementtype(i32) %x)
ret void
}