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
) == 16 || sizeof(MemData
) == 32,
42 "wrong size of MemData");
44 struct MemoryReference
: MemData
{
45 void setVRegSpillSlot(Ref
<const VirtualRegister
> id
) {
49 modeAndScale
= MMODE_VRegSpillSlot
;
51 void setBaseAndOffset(Ref
<const VirtualRegister
> id
, int o
) {
55 modeAndScale
= MMODE_Base
;
57 void setBaseIndexAndOffset(Ref
<const VirtualRegister
> id
,
58 Ref
<const VirtualRegister
> id2
, int s
, int o
) {
60 regs
.index
= id2
._ptr
;
62 modeAndScale
= MMODE_Base
+ (s
<< MMODE_Bits
);
64 void setFPRelative(uint fp
, int o
) {
65 assert(fp
>= 0 && fp
< MMODE_FPCount
);
67 modeAndScale
= MMODE_SPRel
+ fp
;
69 inline void setSPRelative(int o
) {
72 void setGlobal(u8
* p
, int o
) {
75 modeAndScale
= MMODE_Global
;
78 MMode
getMode() const { return static_cast<MMode
>(modeAndScale
& MMODE_Mask
); }
79 int getScale() const { return modeAndScale
>> MMODE_Bits
; }
80 int getOffset() const { return offset
; }
81 RefOrNull
<const VirtualRegister
> getBaseReg() const { return regs
.base
; }
82 RefOrNull
<const VirtualRegister
> getIndexReg() const { return regs
.index
; }
83 RefOrNull
<const VirtualRegister
> getConstBaseReg() const { return regs
.base
; }
84 RefOrNull
<const VirtualRegister
> getConstIndexReg() const { return regs
.index
; }
85 const u8
* getGlobal() const { return global
.ptr
; }
87 void setBaseReg(RefOrNull
<const VirtualRegister
> r
) { regs
.base
= r
._ptr
; }
88 void setIndexReg(RefOrNull
<const VirtualRegister
> r
) { regs
.index
= r
._ptr
; }
91 } // end of namespace jitcs