1 //===-- jitcs_memref.h - memory parameters in instructions -----*- C++ -*-===//
3 // A MemoryReference contains all data necessary to describe the memory
4 // parameter of a single hardware instruction. If a MemoryReference contains
5 // uses of a non-fixed virtual register, and is used in more than one
6 // instruction, copies will be created at register allocation time.
8 // TODO: The source looks rather ugly right now.
10 //===----------------------------------------------------------------------===//
12 #ifndef _JITCS_MEMREF_H_
13 #define _JITCS_MEMREF_H_
15 #include "jitcs_base.h"
16 #include "jitcs_ids.h"
19 struct VirtualRegister
;
21 template <int N
> struct MemDataImpl
{};
22 template <> struct MemDataImpl
<4> {
24 struct { VirtualRegister
const *base
, *index
; } regs
;
25 struct { u8
* ptr
; } global
;
30 template <> struct MemDataImpl
<8> {
32 struct { VirtualRegister
const *base
, *index
; } regs
;
33 struct { u8
* ptr
; } global
;
40 typedef MemDataImpl
<sizeof(void*)> MemData
;
41 static_assert((sizeof(MemData
)&~15) == 0);
43 struct MemoryReference
: MemData
{
44 void setVRegSpillSlot(Ref
<VirtualRegister
> id
) {
48 modeAndScale
= MMODE_VRegSpillSlot
;
50 void setBaseAndOffset(Ref
<VirtualRegister
> id
, int o
) {
54 modeAndScale
= MMODE_Base
;
56 void setBaseIndexAndOffset(Ref
<VirtualRegister
> id
, Ref
<VirtualRegister
> id2
, int s
, int o
) {
60 modeAndScale
= MMODE_Base
+ (s
<< MMODE_Bits
);
62 void setFPRelative(uint fp
, int o
) {
63 assert(fp
>= 0 && fp
< MMODE_FPCount
);
65 modeAndScale
= MMODE_SPRel
+ fp
;
67 inline void setSPRelative(int o
) {
70 void setGlobal(u8
* p
, int o
) {
73 modeAndScale
= MMODE_Global
;
76 MMode
getMode() const { return static_cast<MMode
>(modeAndScale
& MMODE_Mask
); }
77 int getScale() const { return modeAndScale
>> MMODE_Mask
; }
78 int getOffset() const { return offset
; }
79 RefOrNull
<VirtualRegister
> getBaseReg() const { return regs
.base
; }
80 RefOrNull
<VirtualRegister
> getIndexReg() const { return regs
.index
; }
81 const u8
* getGlobal() const { return global
.ptr
; }
83 void setBaseReg(RefOrNull
<VirtualRegister
> r
) { regs
.base
= r
; }
84 void setIndexReg(RefOrNull
<VirtualRegister
> r
) { regs
.index
= r
; }
87 } // end of namespace jitcs