10 #define LLVMJIT_EMIT_H
18 #include <llvm-c/Core.h>
19 #include <llvm-c/Target.h>
27 static inline LLVMValueRef
28 l_ptr_const(
void *ptr, LLVMTypeRef
type)
30 LLVMValueRef
c = LLVMConstInt(
TypeSizeT, (uintptr_t) ptr,
false);
32 return LLVMConstIntToPtr(
c,
type);
38 static inline LLVMTypeRef
41 return LLVMPointerType(t, 0);
47 static inline LLVMValueRef
48 l_int8_const(LLVMContextRef lc,
int8 i)
50 return LLVMConstInt(LLVMInt8TypeInContext(lc),
i,
false);
56 static inline LLVMValueRef
57 l_int16_const(LLVMContextRef lc,
int16 i)
59 return LLVMConstInt(LLVMInt16TypeInContext(lc),
i,
false);
65 static inline LLVMValueRef
66 l_int32_const(LLVMContextRef lc,
int32 i)
68 return LLVMConstInt(LLVMInt32TypeInContext(lc),
i,
false);
74 static inline LLVMValueRef
75 l_int64_const(LLVMContextRef lc, int64
i)
77 return LLVMConstInt(LLVMInt64TypeInContext(lc),
i,
false);
83 static inline LLVMValueRef
84 l_sizet_const(
size_t i)
92 static inline LLVMValueRef
101 static inline LLVMValueRef
102 l_pbool_const(
bool i)
107 static inline LLVMValueRef
108 l_struct_gep(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef v,
int32 idx,
const char *
name)
110 return LLVMBuildStructGEP2(
b, t, v,
idx,
"");
113 static inline LLVMValueRef
114 l_gep(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef v, LLVMValueRef *indices,
int32 nindices,
const char *
name)
116 return LLVMBuildGEP2(
b, t, v, indices, nindices,
name);
119 static inline LLVMValueRef
120 l_load(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef v,
const char *
name)
122 return LLVMBuildLoad2(
b, t, v,
name);
125 static inline LLVMValueRef
126 l_call(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef
fn, LLVMValueRef *
args,
int32 nargs,
const char *
name)
128 return LLVMBuildCall2(
b, t,
fn,
args, nargs,
name);
134 static inline LLVMValueRef
135 l_load_struct_gep(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef v,
int32 idx,
const char *
name)
138 LLVMStructGetTypeAtIndex(t,
idx),
139 l_struct_gep(
b, t, v,
idx,
""),
146 static inline LLVMValueRef
147 l_load_gep1(LLVMBuilderRef
b, LLVMTypeRef t, LLVMValueRef v, LLVMValueRef
idx,
const char *
name)
149 return l_load(
b, t, l_gep(
b, t, v, &
idx, 1,
""),
name);
153 static inline LLVMBasicBlockRef l_bb_before_v(LLVMBasicBlockRef r,
const char *
fmt,...)
pg_attribute_printf(2, 3);
159 static inline LLVMBasicBlockRef
160 l_bb_before_v(LLVMBasicBlockRef r, const
char *
fmt,...)
170 lc = LLVMGetTypeContext(LLVMTypeOf(LLVMGetBasicBlockParent(r)));
172 return LLVMInsertBasicBlockInContext(lc, r,
buf);
176 static inline LLVMBasicBlockRef l_bb_append_v(LLVMValueRef f,
const char *
fmt,...)
pg_attribute_printf(2, 3);
182 static inline LLVMBasicBlockRef
183 l_bb_append_v(LLVMValueRef f, const
char *
fmt,...)
193 lc = LLVMGetTypeContext(LLVMTypeOf(f));
195 return LLVMAppendBasicBlockInContext(lc, f,
buf);
202 l_callsite_ro(LLVMValueRef f)
204 const char argname[] =
"readonly";
205 LLVMAttributeRef ref;
207 ref = LLVMCreateStringAttribute(LLVMGetTypeContext(LLVMTypeOf(f)),
212 LLVMAddCallSiteAttribute(f, LLVMAttributeFunctionIndex, ref);
219 l_callsite_alwaysinline(LLVMValueRef f)
221 const char argname[] =
"alwaysinline";
223 LLVMAttributeRef attr;
225 id = LLVMGetEnumAttributeKindForName(argname,
226 sizeof(argname) - 1);
227 attr = LLVMCreateEnumAttribute(LLVMGetTypeContext(LLVMTypeOf(f)),
id, 0);
228 LLVMAddCallSiteAttribute(f, LLVMAttributeFunctionIndex, attr);
234 static inline LLVMValueRef
235 l_mcxt_switch(LLVMModuleRef mod, LLVMBuilderRef
b, LLVMValueRef nc)
237 const char *cmc =
"CurrentMemoryContext";
241 if (!(
cur = LLVMGetNamedGlobal(mod, cmc)))
244 LLVMBuildStore(
b, nc,
cur);
252 static inline LLVMValueRef
253 l_funcnullp(LLVMBuilderRef
b, LLVMValueRef v_fcinfo,
size_t argno)
258 v_args = l_struct_gep(
b,
263 v_argn = l_struct_gep(
b,
268 return l_struct_gep(
b,
278 static inline LLVMValueRef
279 l_funcvaluep(LLVMBuilderRef
b, LLVMValueRef v_fcinfo,
size_t argno)
284 v_args = l_struct_gep(
b,
289 v_argn = l_struct_gep(
b,
294 return l_struct_gep(
b,
304 static inline LLVMValueRef
305 l_funcnull(LLVMBuilderRef
b, LLVMValueRef v_fcinfo,
size_t argno)
313 static inline LLVMValueRef
314 l_funcvalue(LLVMBuilderRef
b, LLVMValueRef v_fcinfo,
size_t argno)
316 return l_load(
b,
TypeSizeT, l_funcvaluep(
b, v_fcinfo, argno),
"");
Datum idx(PG_FUNCTION_ARGS)
#define pg_attribute_printf(f, a)
#define FIELDNO_FUNCTIONCALLINFODATA_ARGS
static void const char * fmt
LLVMTypeRef StructFunctionCallInfoData
LLVMTypeRef TypeParamBool
LLVMTypeRef StructMemoryContextData
LLVMTypeRef TypeStorageBool
LLVMTypeRef StructNullableDatum
#define FIELDNO_NULLABLE_DATUM_ISNULL
#define FIELDNO_NULLABLE_DATUM_DATUM
static void * fn(void *arg)