potion  0.2
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
internal.h
Go to the documentation of this file.
1 
5 #ifndef POTION_INTERNAL_H
6 #define POTION_INTERNAL_H
7 
8 struct Potion_State;
9 
10 typedef unsigned char u8;
11 
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)
18 
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))
24 
25 #ifndef min
26 #define min(a, b) ((a) <= (b) ? (a) : (b))
27 #endif
28 
29 #ifndef max
30 #define max(a, b) ((a) >= (b) ? (a) : (b))
31 #endif
32 
33 #define TYPE_BATCH_SIZE 4096
34 
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; \
38  (N)->len = 0
39 
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) \
44  capa += S; \
45  capa = sizeof(*(N)->ptr) * capa; \
46  PN_REALLOC(N, V, T, capa); \
47  (N)->siz = capa; \
48  } \
49 })
50 
51 #define PN_ATOI(X,N,B) ({ \
52  char *Ap = X; \
53  long Ai = 0; int Am = 1; \
54  size_t Al = N; \
55  if (*Ap == '-') { Am = -1; Ap++; Al--; } \
56  while (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); \
63  else break; \
64  Ap++; \
65  } \
66  Ai * Am; \
67 })
68 
70 struct PNBHeader {
71  u8 sig[4];
75  u8 pn;
76  u8 proto[];
77 };
78 
79 size_t potion_cp_strlen_utf8(const char *);
80 void *potion_mmap(size_t, const char);
81 int potion_munmap(void *, size_t);
82 // i686-w64-mingw32 /include/stdio.h has asprintf defined
83 // i386-mingw32 not
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, ...);
87 #endif
88 #define PN_ALLOC_FUNC(size) potion_mmap(size, 1)
89 
90 //
91 // stack manipulation routines
92 //
93 #if POTION_X86 == POTION_JIT_TARGET
94 #if PN_SIZE_T == 8
95 // preserve: rbx r12 r13 r14 r15. scratch: rax rcx rdx r8 r9 r10 r11.
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
99 #else
100 #if defined(__SANITIZE_ADDRESS__) && defined(__linux__)
101 #define POTION_ESP(p) __asm__("mov %%rsp, %0" : "=r" (*p)); *p += 0xd0
102 #else
103 #define POTION_ESP(p) __asm__("mov %%rsp, %0" : "=r" (*p))
104 #endif
105 #endif
106 #define POTION_EBP(p) __asm__("mov %%rbp, %0" : "=r" (*p))
107 #else
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
111 #else
112 #if defined(__SANITIZE_ADDRESS__) && defined(__linux__)
113 #define POTION_ESP(p) __asm__("mov %%esp, %0" : "=r" (*p)); *p += 0xd0
114 #else
115 #define POTION_ESP(p) __asm__("mov %%esp, %0" : "=r" (*p))
116 #endif
117 #endif
118 #define POTION_EBP(p) __asm__("mov %%ebp, %0" : "=r" (*p))
119 #endif
120 #else
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)
125 #endif
126 
127 #ifndef O_BINARY
128 #define O_BINARY 0
129 #endif
130 
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
135 #endif
136 
137 #define GC_PROTECT(P) P->mem->protect = (void *)P->mem->birth_cur
138 
139 /* for the jit and bytecode, too large to be inlined into the jit */
140 PN potion_vm_eq(Potion *, PN, PN);
142 
143 #endif
int potion_munmap(void *, size_t)
Definition: contrib.c:101
u8 proto[]
Definition: internal.h:76
u8 sig[4]
Definition: internal.h:71
u8 vmid
Definition: internal.h:74
size_t potion_cp_strlen_utf8(const char *)
wonderful utf-8 counting trickery by colin percival
Definition: contrib.c:17
void * potion_mmap(size_t, const char)
Definition: contrib.c:89
u8 major
Definition: internal.h:72
unsigned char u8
Definition: internal.h:8
.pnb binary dump header
Definition: internal.h:70
void potion_esp(void **esp)
Definition: internal.c:311
the global interpreter state P. currently singleton (not threads yet)
Definition: potion.h:644
PN potion_vm_neq(Potion *, PN, PN)
Definition: vm.c:427
volatile _PN PN
Definition: potion.h:81
u8 minor
Definition: internal.h:73
PN potion_vm_eq(Potion *, PN, PN)
Definition: vm.c:416