5 #ifndef POTION_INTERNAL_H
6 #define POTION_INTERNAL_H
10 typedef unsigned char u8;
12 #define PN_ALLOC(V,T) (T *)potion_gc_alloc(P, V, sizeof(T))
13 #define PN_ALLOC_N(V,T,C) (T *)potion_gc_alloc(P, V, sizeof(T)+C)
14 #define PN_CALLOC_N(V,T,C) (T *)potion_gc_calloc(P, V, sizeof(T)+C)
15 #define PN_REALLOC(X,V,T,N) (X)=(T *)potion_gc_realloc(P, V, (struct PNObject *)(X), sizeof(T) + N)
16 #define PN_DALLOC_N(T,N) potion_data_alloc(P, sizeof(T)*N)
17 #define PN_STRDUP(S) PN_STR(S)
19 #define PN_MEMZERO(X,T) memset((X), 0, sizeof(T))
20 #define PN_MEMZERO_N(X,T,N) memset((X), 0, sizeof(T)*(N))
21 #define PN_MEMCPY(X,Y,T) memcpy((void *)(X), (void *)(Y), sizeof(T))
22 #define PN_MEMCPY_N(X,Y,T,N) memcpy((void *)(X), (void *)(Y), sizeof(T)*(N))
23 #define PN_MEMMOVE_N(DST,SRC,T,N) memmove((void *)(DST), (void *)(SRC), sizeof(T)*(N))
26 #define min(a, b) ((a) <= (b) ? (a) : (b))
30 #define max(a, b) ((a) >= (b) ? (a) : (b))
33 #define TYPE_BATCH_SIZE 4096
35 #define PN_FLEX_NEW(N, V, T, S) \
36 (N) = PN_ALLOC_N(V, T, (sizeof(*(N)->ptr) * S)); \
37 (N)->siz = sizeof(*(N)->ptr) * S; \
40 #define PN_FLEX_NEEDS(X, N, V, T, S) ({ \
41 PN_SIZE capa = (N)->siz / sizeof(*(N)->ptr); \
42 if (capa < (N)->len + X) { \
43 while (capa < (N)->len + X) \
45 capa = sizeof(*(N)->ptr) * capa; \
46 PN_REALLOC(N, V, T, capa); \
51 #define PN_ATOI(X,N,B) ({ \
53 long Ai = 0; int Am = 1; \
55 if (*Ap == '-') { Am = -1; Ap++; Al--; } \
57 if ((*Ap >= '0') && (*Ap <= '9')) \
58 Ai = (Ai * B) + (*Ap - '0'); \
59 else if ((*Ap >= 'A') && (*Ap <= 'F')) \
60 Ai = (Ai * B) + ((*Ap - 'A') + 10); \
61 else if ((*Ap >= 'a') && (*Ap <= 'f')) \
62 Ai = (Ai * B) + ((*Ap - 'a') + 10); \
84 #if POTION_WIN32 && !defined(__MINGW_SCANF_FORMAT)
85 int vasprintf (
char **strp,
const char *fmt, __VALIST ap);
86 int asprintf (
char **string_ptr,
const char *format, ...);
88 #define PN_ALLOC_FUNC(size) potion_mmap(size, 1)
93 #if POTION_X86 == POTION_JIT_TARGET
96 #define PN_SAVED_REGS 5
97 #if defined(__SANITIZE_ADDRESS__) && defined(__APPLE__)
98 #define POTION_ESP(p) __asm__("mov %%rsp, %0" : "=r" (*p)); *p += 0x178
100 #if defined(__SANITIZE_ADDRESS__) && defined(__linux__)
101 #define POTION_ESP(p) __asm__("mov %%rsp, %0" : "=r" (*p)); *p += 0xd0
103 #define POTION_ESP(p) __asm__("mov %%rsp, %0" : "=r" (*p))
106 #define POTION_EBP(p) __asm__("mov %%rbp, %0" : "=r" (*p))
108 #define PN_SAVED_REGS 3
109 #if defined(__SANITIZE_ADDRESS__) && defined(__APPLE__)
110 #define POTION_ESP(p) __asm__("mov %%esp, %0" : "=r" (*p)); *p += 0x178
112 #if defined(__SANITIZE_ADDRESS__) && defined(__linux__)
113 #define POTION_ESP(p) __asm__("mov %%esp, %0" : "=r" (*p)); *p += 0xd0
115 #define POTION_ESP(p) __asm__("mov %%esp, %0" : "=r" (*p))
118 #define POTION_EBP(p) __asm__("mov %%ebp, %0" : "=r" (*p))
121 #define PN_SAVED_REGS 0
122 __attribute__ ((noinline)) void
potion_esp(
void **);
123 #define POTION_ESP(p) potion_esp((void **)p)
124 #define POTION_EBP(p) potion_esp((void **)p)
131 #if POTION_STACK_DIR > 0
132 #define STACK_UPPER(a, b) a
133 #elif POTION_STACK_DIR < 0
134 #define STACK_UPPER(a, b) b
137 #define GC_PROTECT(P) P->mem->protect = (void *)P->mem->birth_cur
int potion_munmap(void *, size_t)
size_t potion_cp_strlen_utf8(const char *)
wonderful utf-8 counting trickery by colin percival
void * potion_mmap(size_t, const char)
void potion_esp(void **esp)
the global interpreter state P. currently singleton (not threads yet)
PN potion_vm_neq(Potion *, PN, PN)
PN potion_vm_eq(Potion *, PN, PN)