-
Notifications
You must be signed in to change notification settings - Fork 0
/
asmjit_common.h
168 lines (148 loc) · 6.91 KB
/
asmjit_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef _PG_ASMJIT_H_
#define _PG_ASMJIT_H_
#include <asmjit/asmjit.h>
extern "C" {
#if __cplusplus > 199711L
#define register
#endif
#include "postgres.h"
#include "access/htup_details.h"
#include "access/tupdesc_details.h"
#include "catalog/pg_attribute.h"
#include "executor/execExpr.h"
#include "executor/tuptable.h"
#include "fmgr.h"
#include "jit/jit.h"
#include "nodes/execnodes.h"
#include "nodes/pg_list.h"
#include "portability/instr_time.h"
#include "storage/ipc.h"
#include "utils/expandeddatum.h"
#include "utils/memutils.h"
#include "utils/palloc.h"
#include "utils/resowner.h"
namespace jit = asmjit;
typedef struct AsmJitContext {
JitContext base;
ResourceOwner resowner;
List *funcs;
} AsmJitContext;
extern bool AsmJitCompileExpr(ExprState *State);
extern void AsmJitReleaseContext(JitContext *Ctx);
extern void AsmJitResetAfterError(void);
extern void *EmitJittedFunction(AsmJitContext *Context, jit::CodeHolder &Code);
typedef void (*TupleDeformingFunc)(TupleTableSlot *);
TupleDeformingFunc CompileTupleDeformingFunc(AsmJitContext *Context,
jit::JitRuntime &Runtime,
TupleDesc Desc,
const TupleTableSlotOps *TtsOps,
int NAttrs);
}
#define TYPES_INFO(struct_type, member_type, member_name, reg_type) \
static inline jit::x86::Gp emit_load_##member_name##_from_##struct_type( \
jit::x86::Compiler &cc, jit::x86::Gp &object_addr) { \
jit::x86::Mem member_ptr = jit::x86::ptr( \
object_addr, offsetof(struct_type, member_name), sizeof(member_type)); \
jit::x86::Gp member = cc.new##reg_type(#struct_type "_" #member_name); \
cc.mov(member, member_ptr); \
return member; \
} \
template <typename Op> \
static inline void emit_store_##member_name##_to_##struct_type( \
jit::x86::Compiler &cc, jit::x86::Gp &object_addr, Op &&val) { \
jit::x86::Mem member_ptr = jit::x86::ptr( \
object_addr, offsetof(struct_type, member_name), sizeof(member_type)); \
cc.mov(member_ptr, val); \
}
#include "jit_types_info.inc"
#undef TYPES_INFO
#define LOAD_STORE_CONST(CType, JitType) \
static inline jit::x86::Gp EmitLoadConst##JitType( \
jit::x86::Compiler &cc, const char *fmt, CType c) { \
jit::x86::Gp JitReg = cc.new##JitType(fmt); \
cc.mov(JitReg, jit::imm(c)); \
return JitReg; \
}
LOAD_STORE_CONST(uint8, UInt8)
LOAD_STORE_CONST(int8, Int8)
LOAD_STORE_CONST(uint32, UInt32)
LOAD_STORE_CONST(int32, Int32)
LOAD_STORE_CONST(void *, UIntPtr)
LOAD_STORE_CONST(intptr_t, IntPtr)
LOAD_STORE_CONST(uint64, UInt64)
LOAD_STORE_CONST(int64, Int64)
#undef LOAD_STORE_CONST
static inline void EmitLoadFromArray(jit::x86::Compiler &cc,
jit::x86::Gp &Array, size_t Index,
jit::x86::Gp &Elem, size_t ElemSize) {
jit::x86::Mem ElemPtr = jit::x86::ptr(Array, Index * ElemSize, ElemSize);
cc.mov(Elem, ElemPtr);
}
template <typename T>
static inline void EmitStoreToArray(jit::x86::Compiler &cc, jit::x86::Gp &Array,
size_t Index, const T &Elem,
size_t ElemSize) {
jit::x86::Mem ElemPtr = jit::x86::ptr(Array, Index * ElemSize, ElemSize);
cc.mov(ElemPtr, Elem);
}
/* TODO: Combine with EmitLoadFromArray. */
static inline void EmitLoadFromFlexibleArray(jit::x86::Compiler &cc,
jit::x86::Gp &ObjectAddr,
size_t ArrayOff, size_t Index,
jit::x86::Gp &Elem,
size_t ElemSize) {
jit::x86::Mem ElemPtr =
jit::x86::ptr(ObjectAddr, ArrayOff + Index * ElemSize, ElemSize);
cc.mov(Elem, ElemPtr);
}
template <typename T>
static inline void EmitStoreToFlexibleArray(jit::x86::Compiler &cc,
jit::x86::Gp &ObjectAddr,
size_t ArrayOff, size_t Index,
const T &Elem, size_t ElemSize) {
jit::x86::Mem ElemPtr =
jit::x86::ptr(ObjectAddr, ArrayOff + Index * ElemSize, ElemSize);
cc.mov(ElemPtr, Elem);
}
static inline jit::x86::Gp
LoadFuncArgNull(jit::x86::Compiler &cc, jit::x86::Gp &v_fcinfo, size_t argno) {
jit::x86::Gp v_argnull = cc.newInt8("v_argnull.i8");
EmitLoadFromFlexibleArray(cc, v_fcinfo,
offsetof(FunctionCallInfoBaseData, args) +
argno * sizeof(NullableDatum) +
offsetof(NullableDatum, isnull),
0, v_argnull, sizeof(bool));
return v_argnull;
}
static inline jit::x86::Gp LoadFuncArgValue(jit::x86::Compiler &cc,
jit::x86::Gp &v_funcinfo,
size_t argno) {
jit::x86::Gp v_argvalue = cc.newUIntPtr("v_argvalue.uintptr");
EmitLoadFromFlexibleArray(cc, v_funcinfo,
offsetof(FunctionCallInfoBaseData, args) +
argno * sizeof(NullableDatum) +
offsetof(NullableDatum, value),
0, v_argvalue, sizeof(Datum));
return v_argvalue;
}
template <typename T>
static inline void StoreFuncArgNull(jit::x86::Compiler &cc,
jit::x86::Gp &v_fcinfo, size_t argno,
const T &v_val) {
EmitStoreToFlexibleArray(cc, v_fcinfo,
offsetof(FunctionCallInfoBaseData, args) +
argno * sizeof(NullableDatum) +
offsetof(NullableDatum, isnull),
0, v_val, sizeof(bool));
}
template <typename T>
static inline void StoreFuncArgValue(jit::x86::Compiler &cc,
jit::x86::Gp &v_fcinfo, size_t argno,
const T &v_val) {
EmitStoreToFlexibleArray(cc, v_fcinfo,
offsetof(FunctionCallInfoBaseData, args) +
argno * sizeof(NullableDatum) +
offsetof(NullableDatum, value),
0, v_val, sizeof(Datum));
}
#endif