1 //===-- M68kInstrBuilder.h - Functions to build M68k insts ------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file exposes functions that may be used with BuildMI from the
11 /// MachineInstrBuilder.h file to handle M68k'isms in a clean way.
12 ///
13 /// TODO The BuildMem function may be used with the BuildMI function to add
14 /// entire memory references in a single, typed, function call.  M68k memory
15 /// references can be very complex expressions (described in the README), so
16 /// wrapping them up behind an easier to use interface makes sense.
17 /// Descriptions of the functions are included below.
18 ///
19 /// For reference, the order of operands for memory references is:
20 /// (Operand), Base, Scale, Index, Displacement.
21 ///
22 //===----------------------------------------------------------------------===//
23 //
24 #ifndef LLVM_LIB_TARGET_M68K_M68KINSTRBUILDER_H
25 #define LLVM_LIB_TARGET_M68K_M68KINSTRBUILDER_H
26 
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/MachineOperand.h"
34 #include "llvm/MC/MCInstrDesc.h"
35 
36 #include <cassert>
37 
38 namespace llvm {
39 namespace M68k {
40 static inline const MachineInstrBuilder &
41 addOffset(const MachineInstrBuilder &MIB, int Offset) {
42   return MIB.addImm(Offset);
43 }
44 
45 /// addRegIndirectWithDisp - This function is used to add a memory reference
46 /// of the form (Offset, Base), i.e., one with no scale or index, but with a
47 /// displacement. An example is: (4,D0).
48 static inline const MachineInstrBuilder &
49 addRegIndirectWithDisp(const MachineInstrBuilder &MIB, Register Reg,
50                        bool IsKill, int Offset) {
51   return MIB.addImm(Offset).addReg(Reg, getKillRegState(IsKill));
52 }
53 
54 /// addFrameReference - This function is used to add a reference to the base of
55 /// an abstract object on the stack frame of the current function.  This
56 /// reference has base register as the FrameIndex offset until it is resolved.
57 /// This allows a constant offset to be specified as well...
58 static inline const MachineInstrBuilder &
59 addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
60   MachineInstr *MI = MIB;
61   MachineFunction &MF = *MI->getParent()->getParent();
62   MachineFrameInfo &MFI = MF.getFrameInfo();
63   const MCInstrDesc &MCID = MI->getDesc();
64   auto Flags = MachineMemOperand::MONone;
65   if (MCID.mayLoad())
66     Flags |= MachineMemOperand::MOLoad;
67   if (MCID.mayStore())
68     Flags |= MachineMemOperand::MOStore;
69   MachineMemOperand *MMO = MF.getMachineMemOperand(
70       MachinePointerInfo::getFixedStack(MF, FI, Offset), Flags,
71       MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
72   return MIB.addImm(Offset).addFrameIndex(FI).addMemOperand(MMO);
73 }
74 
75 static inline const MachineInstrBuilder &
76 addMemOperand(const MachineInstrBuilder &MIB, int FI, int Offset = 0) {
77   MachineInstr *MI = MIB;
78   MachineFunction &MF = *MI->getParent()->getParent();
79   MachineFrameInfo &MFI = MF.getFrameInfo();
80   const MCInstrDesc &MCID = MI->getDesc();
81   auto Flags = MachineMemOperand::MONone;
82   if (MCID.mayLoad())
83     Flags |= MachineMemOperand::MOLoad;
84   if (MCID.mayStore())
85     Flags |= MachineMemOperand::MOStore;
86   MachineMemOperand *MMO = MF.getMachineMemOperand(
87       MachinePointerInfo::getFixedStack(MF, FI, Offset), Flags,
88       MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
89   return MIB.addMemOperand(MMO);
90 }
91 } // end namespace M68k
92 } // end namespace llvm
93 
94 #endif // LLVM_LIB_TARGET_M68K_M68KINSTRBUILDER_H
95