First effort to revive BasicBlock and Function classes and
[jitcs.git] / include / jitcs_memref.h
blob43f6ea9acff473c3639b7dc8d25df9dff739b8a9
1 //===-- jitcs_memref.h - memory parameters in instructions -----*- C++ -*-===//
2 //
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.
7 //
8 // TODO: The source looks rather ugly right now.
9 //
10 //===----------------------------------------------------------------------===//
12 #ifndef _JITCS_MEMREF_H_
13 #define _JITCS_MEMREF_H_
15 #include "jitcs_base.h"
16 #include "jitcs_ids.h"
18 namespace jitcs {
19 struct VirtualRegister;
21 template <int N> struct MemDataImpl {};
22 template <> struct MemDataImpl<4> {
23 union {
24 struct { VirtualRegister *base, *index; } regs;
25 struct { u8* ptr; } global;
26 };
27 int offset;
28 int modeAndScale;
30 template <> struct MemDataImpl<8> {
31 union {
32 struct { VirtualRegister *base, *index; } regs;
33 struct { u8* ptr; } global;
34 };
35 int offset;
36 int modeAndScale;
37 iptr _padding;
40 typedef MemDataImpl<sizeof(void*)> MemData;
41 static_assert(sizeof(MemData) == 16 || sizeof(MemData) == 32, "wrong size of MemData");
43 struct MemoryReference : MemData {
44 void setVRegSpillSlot(Ref<VirtualRegister> id) {
45 regs.base = id._ptr;
46 regs.index = nullptr;
47 offset = 0;
48 modeAndScale = MMODE_VRegSpillSlot;
50 void setBaseAndOffset(Ref<VirtualRegister> id, int o) {
51 regs.base = id._ptr;
52 regs.index = nullptr;
53 offset = o;
54 modeAndScale = MMODE_Base;
56 void setBaseIndexAndOffset(Ref<VirtualRegister> id, Ref<VirtualRegister> id2, int s, int o) {
57 regs.base = id._ptr;
58 regs.index = id2._ptr;
59 offset = o;
60 modeAndScale = MMODE_Base + (s << MMODE_Bits);
62 void setFPRelative(uint fp, int o) {
63 assert(fp >= 0 && fp < MMODE_FPCount);
64 offset = o;
65 modeAndScale = MMODE_SPRel + fp;
67 inline void setSPRelative(int o) {
68 setFPRelative(0, o);
70 void setGlobal(u8* p, int o) {
71 global.ptr = p;
72 offset = 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._ptr; }
84 void setIndexReg(RefOrNull<VirtualRegister> r) { regs.index = r._ptr; }
87 } // end of namespace jitcs
89 #endif
90 // _JITCS_MEMREF_H_