Added list of scratch & reserved registers to calling convention. The
[jitcs.git] / include / jitcs_memref.h
blobe5bb79918bd9e4c3b5bcffbb1d77c58b52fef522
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 const *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 const *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,
42 "wrong size of MemData");
44 struct MemoryReference : MemData {
45 void setVRegSpillSlot(Ref<const VirtualRegister> id) {
46 regs.base = id._ptr;
47 regs.index = nullptr;
48 offset = 0;
49 modeAndScale = MMODE_VRegSpillSlot;
51 void setBaseAndOffset(Ref<const VirtualRegister> id, int o) {
52 regs.base = id._ptr;
53 regs.index = nullptr;
54 offset = o;
55 modeAndScale = MMODE_Base;
57 void setBaseIndexAndOffset(Ref<const VirtualRegister> id,
58 Ref<const VirtualRegister> id2, int s, int o) {
59 regs.base = id._ptr;
60 regs.index = id2._ptr;
61 offset = o;
62 modeAndScale = MMODE_Base + (s << MMODE_Bits);
64 void setFPRelative(uint fp, int o) {
65 assert(fp >= 0 && fp < MMODE_FPCount);
66 offset = o;
67 modeAndScale = MMODE_SPRel + fp;
69 inline void setSPRelative(int o) {
70 setFPRelative(0, o);
72 void setGlobal(u8* p, int o) {
73 global.ptr = p;
74 offset = 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
93 #endif
94 // _JITCS_MEMREF_H_