LMMS
Loading...
Searching...
No Matches
ns-eel.h
Go to the documentation of this file.
1/*
2 Nullsoft Expression Evaluator Library (NS-EEL)
3 Copyright (C) 1999-2003 Nullsoft, Inc.
4
5 ns-eel.h: main application interface header
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24
25#ifndef __NS_EEL_H__
26#define __NS_EEL_H__
27
28// put standard includes here
29#include <stdlib.h>
30#include <stdio.h>
31
32#ifndef EEL_F_SIZE
33#define EEL_F_SIZE 8
34#endif
35
36#include "../wdltypes.h"
37
38typedef double EEL_F WDL_FIXALIGN;
39typedef double *EEL_F_PTR;
40
41#ifdef _MSC_VER
42#define NSEEL_CGEN_CALL __cdecl
43#else
44#define NSEEL_CGEN_CALL
45#endif
46
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52// host should implement these (can be empty stub functions if no VM will execute code in multiple threads at once)
53
54 // implement if you will be running the code in same VM from multiple threads,
55 // or VMs that have the same GRAM pointer from different threads, or multiple
56 // VMs that have a NULL GRAM pointer from multiple threads.
57 // if you give each VM it's own unique GRAM and only run each VM in one thread, then you can leave it blank.
58
59 // or if you're daring....
60
63
64
65int NSEEL_init(); // returns nonzero on failure (only if EEL_VALIDATE_FSTUBS defined), otherwise the same as NSEEL_quit(), and completely optional
66void NSEEL_quit(); // clears any added functions
67
68
69// adds a function that returns a value (EEL_F)
70#define NSEEL_addfunc_retval(name,np,pproc,fptr) \
71 NSEEL_addfunc_ret_type(name,np,1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
72
73// adds a function that returns a pointer (EEL_F*)
74#define NSEEL_addfunc_retptr(name,np,pproc,fptr) \
75 NSEEL_addfunc_ret_type(name,np,0,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
76
77// adds a void or bool function
78#define NSEEL_addfunc_retbool(name,np,pproc,fptr) \
79 NSEEL_addfunc_ret_type(name,np,-1,pproc,(void *)(fptr),NSEEL_ADDFUNC_DESTINATION)
80
81// adds a function that takes min_np or more parameters (func sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
82#define NSEEL_addfunc_varparm(name, min_np, pproc, fptr) \
83 NSEEL_addfunc_varparm_ex(name,min_np,0,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
84
85// adds a function that takes np parameters via func: sig needs to be EEL_F func(void *ctx, INT_PTR np, EEL_F **parms)
86#define NSEEL_addfunc_exparms(name, np, pproc, fptr) \
87 NSEEL_addfunc_varparm_ex(name,np,1,pproc,fptr,NSEEL_ADDFUNC_DESTINATION)
88
89
90// deprecated
91#define NSEEL_addfunction(name,nparms,code,len) NSEEL_addfunctionex((name),(nparms),(code),(len),0,0)
92#define NSEEL_addfunctionex(name,nparms,code,len,pproc,fptr) NSEEL_addfunctionex2((name),(nparms),(code),(len),(pproc),(fptr),0, NSEEL_ADDFUNC_DESTINATION)
93
94#ifndef NSEEL_ADDFUNC_DESTINATION
95#define NSEEL_ADDFUNC_DESTINATION (NULL)
96#endif
97
98struct functionType;
99
100typedef struct
101{
105
106struct _compileContext;
107typedef void *(*NSEEL_PPPROC)(void *data, int data_size, struct _compileContext *userfunc_data);
108void NSEEL_addfunctionex2(const char *name, int nparms, char *code_startaddr, int code_len, NSEEL_PPPROC pproc, void *fptr, void *fptr2, eel_function_table *destination);
109
110void NSEEL_addfunc_ret_type(const char *name, int np, int ret_type, NSEEL_PPPROC pproc, void *fptr, eel_function_table *destination); // ret_type=-1 for bool, 1 for value, 0 for ptr
111void NSEEL_addfunc_varparm_ex(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination);
112void NSEEL_addfunc_varparm_ctxptr(const char *name, int min_np, int want_exact, void *ctxptr, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination);
113void NSEEL_addfunc_varparm_ctxptr2(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, void *ctx, EEL_F (NSEEL_CGEN_CALL *fptr)(void *, void *,INT_PTR, EEL_F **), eel_function_table *destination);
114
115int *NSEEL_getstats(); // returns a pointer to 5 ints... source bytes, static code bytes, call code bytes, data bytes, number of code handles
116
117typedef void *NSEEL_VMCTX;
118typedef void *NSEEL_CODEHANDLE;
119
120NSEEL_VMCTX NSEEL_VM_alloc(); // return a handle
121void NSEEL_VM_free(NSEEL_VMCTX ctx); // free when done with a VM and ALL of its code have been freed, as well
122
123void NSEEL_VM_SetFunctionTable(NSEEL_VMCTX, eel_function_table *tab); // use NULL to use default (global) table
124
125// validateFunc can return error message if not permitted
126void NSEEL_VM_SetFunctionValidator(NSEEL_VMCTX, const char * (*validateFunc)(const char *fn_name, void *user), void *user);
127
131void NSEEL_VM_enumallvars(NSEEL_VMCTX ctx, int (*func)(const char *name, EEL_F *val, void *ctx), void *userctx); // return false from func to stop
132
133EEL_F *NSEEL_VM_regvar(NSEEL_VMCTX ctx, const char *name); // register a variable (before compilation)
134EEL_F *NSEEL_VM_getvar(NSEEL_VMCTX ctx, const char *name); // get a variable (if registered or created by code)
135int NSEEL_VM_get_var_refcnt(NSEEL_VMCTX _ctx, const char *name); // returns -1 if not registered, or >=0
136void NSEEL_VM_set_var_resolver(NSEEL_VMCTX ctx, EEL_F *(*res)(void *userctx, const char *name), void *userctx);
137
138void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx); // clears and frees all (VM) RAM used
139void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX); // call after code to free the script-requested memory
140int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx); // want NSEEL_VM_freeRAMIfCodeRequested?
141
142// if you set this, it uses a local GMEM context.
143// Must be set before compilation.
144// void *p=NULL;
145// NSEEL_VM_SetGRAM(ctx,&p);
146// .. do stuff
147// NSEEL_VM_FreeGRAM(&p);
148void NSEEL_VM_SetGRAM(NSEEL_VMCTX ctx, void **gram);
149void NSEEL_VM_FreeGRAM(void **ufd); // frees a gmem context.
150void NSEEL_VM_SetCustomFuncThis(NSEEL_VMCTX ctx, void *thisptr);
151
152EEL_F *NSEEL_VM_getramptr(NSEEL_VMCTX ctx, unsigned int offs, int *validCount);
153EEL_F *NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount);
154
155
156// set 0 to query. returns actual value used (limits, granularity apply -- see NSEEL_RAM_BLOCKS)
157int NSEEL_VM_setramsize(NSEEL_VMCTX ctx, int maxent);
158
159
160struct eelStringSegmentRec {
162 const char *str_start; // escaped characters, including opening/trailing characters
163 int str_len;
164};
166 EEL_F (*onString)(void *caller_this, struct eelStringSegmentRec *list),
167 EEL_F (*onNamedString)(void *caller_this, const char *name));
168
169// call with NULL to calculate size, or non-null to generate to buffer (returning size used -- will not null terminate, caller responsibility)
170int nseel_stringsegments_tobuf(char *bufOut, int bufout_sz, struct eelStringSegmentRec *list);
171
172
173NSEEL_CODEHANDLE NSEEL_code_compile(NSEEL_VMCTX ctx, const char *code, int lineoffs);
174#define NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS 1 // allows that code's functions to be used in other code (note you shouldn't destroy that codehandle without destroying others first if used)
175#define NSEEL_CODE_COMPILE_FLAG_COMMONFUNCS_RESET 2 // resets common code functions
176#define NSEEL_CODE_COMPILE_FLAG_NOFPSTATE 4 // hint that the FPU/SSE state should be good-to-go
177#define NSEEL_CODE_COMPILE_FLAG_ONLY_BUILTIN_FUNCTIONS 8 // very restrictive mode (only math functions really)
178
179NSEEL_CODEHANDLE NSEEL_code_compile_ex(NSEEL_VMCTX ctx, const char *code, int lineoffs, int flags);
180
185int *NSEEL_code_getstats(NSEEL_CODEHANDLE code); // 4 ints...source bytes, static code bytes, call code bytes, data bytes
186
187
188// global memory control/view
189extern unsigned int NSEEL_RAM_limitmem; // if nonzero, memory limit for user data, in bytes
190extern unsigned int NSEEL_RAM_memused;
191extern int NSEEL_RAM_memused_errors;
192
193
194
195// configuration:
196
197// use the handwritten lexer -- the flex (eel2.l generated) lexer mostly works, but doesn't support string parsing at the moment
198// this mode is faster and uses less ram than eel2.l anyway, so leave it on
199#define NSEEL_SUPER_MINIMAL_LEXER
200
201 // #define NSEEL_EEL1_COMPAT_MODE // supports old behaviors (continue after failed compile), old functions _bnot etc. disables string support (strings were used as comments in eel1 etc)
202
203#define NSEEL_MAX_VARIABLE_NAMELEN 128 // define this to override the max variable length
204#define NSEEL_MAX_EELFUNC_PARAMETERS 40
205#define NSEEL_MAX_FUNCSIG_NAME 2048 // longer than variable maxlen, due to multiple namespaces
206
207// maximum loop length (0 for unlimited)
208#ifndef NSEEL_LOOPFUNC_SUPPORT_MAXLEN
209#define NSEEL_LOOPFUNC_SUPPORT_MAXLEN 1048576
210#endif
211
212#define NSEEL_MAX_FUNCTION_SIZE_FOR_INLINE 2048
213
214// when a VM ctx doesn't have a GRAM context set, make the global one this big
215#define NSEEL_SHARED_GRAM_SIZE (1<<20)
216
217//#define EEL_DUMP_OPS // used for testing frontend parser/logic changes
218
219// note: if you wish to change NSEEL_RAM_*, and your target is x86-64, you will
220// need to edit asm-nseel-x64-sse.asm to match
221
222// 512 * 65536 = 32 million entries maximum (256MB RAM)
223// default is limited to 128 * 65536 = 8 million entries (64MB RAM)
224
225// default to 8 million entries, use NSEEL_VM_setramsize() to change at runtime
226#define NSEEL_RAM_BLOCKS_DEFAULTMAX 128
227
228// 512 entry block table maximum (2k/4k per VM)
229#define NSEEL_RAM_BLOCKS_LOG2 9
230
231 // 65536 items per block (512KB)
232#define NSEEL_RAM_ITEMSPERBLOCK_LOG2 16
233
234#define NSEEL_RAM_BLOCKS (1 << NSEEL_RAM_BLOCKS_LOG2)
235#define NSEEL_RAM_ITEMSPERBLOCK (1<<NSEEL_RAM_ITEMSPERBLOCK_LOG2)
236
237#define NSEEL_STACK_SIZE 4096 // about 64k overhead if the stack functions are used in a given code handle
238
239// arch neutral mode, runs about 1/8th speed or so
240//#define EEL_TARGET_PORTABLE
241
242#ifdef EEL_TARGET_PORTABLE
243#ifdef EEL_PORTABLE_TAILCALL
244typedef void (*EEL_BC_TYPE)(void *next_inst, void *state);
245#else
246#define EEL_BC_TYPE int
247#endif
248#endif
249
250#ifdef NSEEL_EEL1_COMPAT_MODE
251double *NSEEL_getglobalregs();
252#endif
253
254void eel_enterfp(int s[2]);
255void eel_leavefp(int s[2]);
256
257extern void *(*nseel_gmem_calloc)(size_t,size_t); // set this to the calloc() implementation used by the context that will call NSEEL_VM_FreeGRAM()
258
259#ifdef __cplusplus
260}
261#endif
262
263#endif//__NS_EEL_H__
unsigned s
Definition inflate.c:1555
EEL_F *(* NSEEL_VM_regvar)(NSEEL_VMCTX ctx, const char *name)
Definition eel_import.h:13
EEL_F *(* NSEEL_VM_getramptr)(NSEEL_VMCTX ctx, unsigned int offs, int *validAmt)
Definition eel_import.h:23
void(* NSEEL_VM_free)(NSEEL_VMCTX ctx)
Definition eel_import.h:11
void(* eel_leavefp)(int s[2])
Definition eel_import.h:43
void(* NSEEL_VM_SetGRAM)(NSEEL_VMCTX, void **)
Definition eel_import.h:10
void(* NSEEL_VM_set_var_resolver)(NSEEL_VMCTX ctx, EEL_F *(*res)(void *userctx, const char *name), void *userctx)
Definition eel_import.h:17
NSEEL_CODEHANDLE(* NSEEL_code_compile_ex)(NSEEL_VMCTX ctx, const char *code, int lineoffs, int flags)
Definition eel_import.h:16
void(* NSEEL_VM_SetStringFunc)(NSEEL_VMCTX ctx, EEL_F(*onString)(void *caller_this, struct eelStringSegmentRec *list), EEL_F(*onNamedString)(void *caller_this, const char *name))
Definition eel_import.h:32
void(* NSEEL_code_free)(NSEEL_CODEHANDLE code)
Definition eel_import.h:20
void(* NSEEL_code_execute)(NSEEL_CODEHANDLE code)
Definition eel_import.h:19
int(* nseel_stringsegments_tobuf)(char *bufOut, int bufout_sz, struct eelStringSegmentRec *list)
Definition eel_import.h:37
void(* eel_enterfp)(int s[2])
Definition eel_import.h:42
void(* NSEEL_VM_enumallvars)(NSEEL_VMCTX ctx, int(*func)(const char *name, EEL_F *val, void *ctx), void *userctx)
Definition eel_import.h:22
char *(* NSEEL_code_getcodeerror)(NSEEL_VMCTX ctx)
Definition eel_import.h:18
void(* NSEEL_addfunc_ret_type)(const char *name, int np, int ret_type, NSEEL_PPPROC pproc, void *fptr, eel_function_table *destination)
Definition eel_import.h:5
void(* NSEEL_addfunc_varparm_ex)(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, EEL_F(NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination)
Definition eel_import.h:6
void(* NSEEL_VM_SetFunctionTable)(NSEEL_VMCTX, eel_function_table *tab)
Definition eel_import.h:12
void(* NSEEL_VM_SetCustomFuncThis)(NSEEL_VMCTX ctx, void *thisptr)
Definition eel_import.h:15
static const char * name
Definition pugl.h:1582
int val
Definition jpeglib.h:956
JSAMPIMAGE data
Definition jpeglib.h:945
unsigned int NSEEL_RAM_memused
Definition nseel-ram.c:40
void NSEEL_VM_remove_all_nonreg_vars(NSEEL_VMCTX _ctx)
Definition nseel-compiler.c:5480
int NSEEL_init()
Definition nseel-compiler.c:711
void NSEEL_addfunc_varparm_ctxptr(const char *name, int min_np, int want_exact, void *ctxptr, EEL_F(NSEEL_CGEN_CALL *fptr)(void *, INT_PTR, EEL_F **), eel_function_table *destination)
Definition nseel-compiler.c:729
EEL_F * NSEEL_VM_getvar(NSEEL_VMCTX ctx, const char *name)
Definition nseel-compiler.c:5650
EEL_F * NSEEL_VM_getramptr_noalloc(NSEEL_VMCTX ctx, unsigned int offs, int *validCount)
Definition nseel-ram.c:446
int NSEEL_RAM_memused_errors
Definition nseel-ram.c:41
void NSEEL_VM_freeRAM(NSEEL_VMCTX ctx)
Definition nseel-ram.c:393
void *(* NSEEL_PPPROC)(void *data, int data_size, struct _compileContext *userfunc_data)
Definition ns-eel.h:107
#define NSEEL_CGEN_CALL
Definition ns-eel.h:44
void NSEEL_VM_remove_unused_vars(NSEEL_VMCTX _ctx)
Definition nseel-compiler.c:5474
void NSEEL_addfunc_varparm_ctxptr2(const char *name, int min_np, int want_exact, NSEEL_PPPROC pproc, void *ctx, EEL_F(NSEEL_CGEN_CALL *fptr)(void *, void *, INT_PTR, EEL_F **), eel_function_table *destination)
Definition nseel-compiler.c:734
void NSEEL_VM_SetFunctionValidator(NSEEL_VMCTX, const char *(*validateFunc)(const char *fn_name, void *user), void *user)
Definition nseel-compiler.c:5318
int NSEEL_code_geterror_flag(NSEEL_VMCTX ctx)
Definition nseel-compiler.c:5218
void NSEEL_quit()
Definition nseel-compiler.c:717
void NSEEL_VM_freeRAMIfCodeRequested(NSEEL_VMCTX)
Definition nseel-ram.c:56
int * NSEEL_getstats()
Definition nseel-compiler.c:165
void * NSEEL_VMCTX
Definition ns-eel.h:117
void NSEEL_VM_FreeGRAM(void **ufd)
Definition nseel-ram.c:415
void * NSEEL_CODEHANDLE
Definition ns-eel.h:118
int NSEEL_VM_wantfreeRAM(NSEEL_VMCTX ctx)
Definition nseel-ram.c:45
NSEEL_VMCTX NSEEL_VM_alloc()
Definition eel_import.h:9
int * NSEEL_code_getstats(NSEEL_CODEHANDLE code)
Definition nseel-compiler.c:5385
unsigned int NSEEL_RAM_limitmem
Definition nseel-ram.c:39
void NSEEL_HOSTSTUB_LeaveMutex()
Definition ysfx_api_eel.cpp:142
NSEEL_CODEHANDLE NSEEL_code_compile(NSEEL_VMCTX ctx, const char *code, int lineoffs)
Definition nseel-compiler.c:4514
void NSEEL_addfunctionex2(const char *name, int nparms, char *code_startaddr, int code_len, NSEEL_PPPROC pproc, void *fptr, void *fptr2, eel_function_table *destination)
Definition nseel-compiler.c:755
double * EEL_F_PTR
Definition ns-eel.h:39
void NSEEL_VM_clear_var_refcnts(NSEEL_VMCTX _ctx)
Definition nseel-compiler.c:5486
void NSEEL_HOSTSTUB_EnterMutex()
Definition ysfx_api_eel.cpp:138
int NSEEL_VM_get_var_refcnt(NSEEL_VMCTX _ctx, const char *name)
Definition nseel-compiler.c:5664
int NSEEL_VM_setramsize(NSEEL_VMCTX ctx, int maxent)
Definition nseel-compiler.c:5304
Definition ns-eel-int.h:182
Definition inftrees.h:27
Definition ns-eel.h:101
struct functionType * list
Definition ns-eel.h:102
int list_size
Definition ns-eel.h:103
Definition eel_import.h:27
const char * str_start
Definition eel_import.h:29
struct eelStringSegmentRec * _next
Definition eel_import.h:28
int str_len
Definition eel_import.h:30
Definition ns-eel-int.h:252
intptr_t INT_PTR
Definition swell-types.h:42
#define void
Definition unzip.h:396
#define WDL_FIXALIGN
Definition wdltypes.h:85