forked from alexfreud/winamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnseel-compiler.c
5700 lines (4988 loc) · 187 KB
/
nseel-compiler.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Expression Evaluator Library (NS-EEL) v2
Copyright (C) 2004-2013 Cockos Incorporated
Copyright (C) 1999-2003 Nullsoft, Inc.
nseel-compiler.c
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "ns-eel-int.h"
#include "denormal.h"
#include "wdlcstring.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#if !defined(EEL_TARGET_PORTABLE) && !defined(_WIN32)
#include <sys/mman.h>
#include <stdint.h>
#include <unistd.h>
#endif
#include "glue_x86.h"
#ifdef _WIN64
#include "glue_x86_64.h"
#endif // _WIN64
#define NSEEL_VARS_MALLOC_CHUNKSIZE 8
//#define LOG_OPT
//#define EEL_PRINT_FAILS
//#define EEL_VALIDATE_WORKTABLE_USE
//#define EEL_VALIDATE_FSTUBS
#ifdef EEL_PRINT_FAILS
#ifdef _WIN32
#define RET_MINUS1_FAIL(x) { OutputDebugString(x); return -1; }
#else
#define RET_MINUS1_FAIL(x) { printf("%s\n",x); return -1; }
#endif
#else
#define RET_MINUS1_FAIL(x) return -1;
#endif
#ifdef EEL_DUMP_OPS
FILE *g_eel_dump_fp, *g_eel_dump_fp2;
#endif
#ifdef EEL_VALIDATE_WORKTABLE_USE
#define MIN_COMPUTABLE_SIZE 0
#define COMPUTABLE_EXTRA_SPACE 64 // safety buffer, if EEL_VALIDATE_WORKTABLE_USE set, used for magic-value-checking
#else
#define MIN_COMPUTABLE_SIZE 32 // always use at least this big of a temp storage table (and reset the temp ptr when it goes past this boundary)
#define COMPUTABLE_EXTRA_SPACE 16 // safety buffer, if EEL_VALIDATE_WORKTABLE_USE set, used for magic-value-checking
#endif
/*
P1 is rightmost parameter
P2 is second rightmost, if any
P3 is third rightmost, if any
registers on x86 are (RAX etc on x86-64)
P1(ret) EAX
P2 EDI
P3 ECX
WTP RSI
x86_64: r12 is a pointer to ram_state.blocks
x86_64: r13 is a pointer to closenessfactor
registers on PPC are:
P1(ret) r3
P2 r14
P3 r15
WTP r16 (r17 has the original value)
r13 is a pointer to ram_state.blocks
ppc uses f31 and f30 and others for certain constants
*/
#ifdef EEL_TARGET_PORTABLE
#define EEL_DOESNT_NEED_EXEC_PERMS
#include "glue_port.h"
#elif defined(__ppc__)
#include "glue_ppc.h"
#elif defined(__aarch64__)
#include "glue_aarch64.h"
#elif defined(__arm__) || (defined (_M_ARM) && _M_ARM == 7)
#include "glue_arm.h"
#elif defined(_WIN64) || defined(__LP64__)
#include "glue_x86_64.h"
#else
#include "glue_x86.h"
#endif
#ifndef GLUE_INVSQRT_NEEDREPL
#define GLUE_INVSQRT_NEEDREPL 0
#endif
// used by //#eel-no-optimize:xxx, in ctx->optimizeDisableFlags
#define OPTFLAG_NO_OPTIMIZE 1
#define OPTFLAG_NO_FPSTACK 2
#define OPTFLAG_NO_INLINEFUNC 4
#define OPTFLAG_FULL_DENORMAL_CHECKS 8 // if set, denormals/NaN are always filtered on assign
#define OPTFLAG_NO_DENORMAL_CHECKS 16 // if set and FULL not set, denormals/NaN are never filtered on assign
#define DENORMAL_CLEARING_THRESHOLD 1.0e-50 // when adding/subtracting a constant, assume if it's greater than this, it will clear denormal (the actual value is probably 10^-290...)
#define MAX_SUB_NAMESPACES 32
typedef struct
{
const char *namespacePathToThis;
const char *subParmInfo[MAX_SUB_NAMESPACES];
} namespaceInformation;
static int nseel_evallib_stats[5]; // source bytes, static code bytes, call code bytes, data bytes, segments
int *NSEEL_getstats()
{
return nseel_evallib_stats;
}
static int findLineNumber(const char *exp, int byteoffs)
{
int lc=0;
while (byteoffs-->0 && *exp) if (*exp++ =='\n') lc++;
return lc;
}
static int nseel_vms_referencing_globallist_cnt;
nseel_globalVarItem *nseel_globalreg_list;
static EEL_F *get_global_var(compileContext *ctx, const char *gv, int addIfNotPresent);
static void *__newBlock(llBlock **start,int size, int wantMprotect);
#define OPCODE_IS_TRIVIAL(x) ((x)->opcodeType <= OPCODETYPE_VARPTRPTR)
enum {
OPCODETYPE_DIRECTVALUE=0,
OPCODETYPE_DIRECTVALUE_TEMPSTRING, // like directvalue, but will generate a new tempstring value on generate
OPCODETYPE_VALUE_FROM_NAMESPACENAME, // this.* or namespace.* are encoded this way
OPCODETYPE_VARPTR,
OPCODETYPE_VARPTRPTR,
OPCODETYPE_FUNC1,
OPCODETYPE_FUNC2,
OPCODETYPE_FUNC3,
OPCODETYPE_FUNCX,
OPCODETYPE_MOREPARAMS,
OPCODETYPE_INVALID,
};
struct opcodeRec
{
int opcodeType;
int fntype;
void *fn;
union {
struct opcodeRec *parms[3];
struct {
double directValue;
EEL_F *valuePtr; // if direct value, valuePtr can be cached
} dv;
} parms;
int namespaceidx;
// OPCODETYPE_VALUE_FROM_NAMESPACENAME (relname is either empty or blah)
// OPCODETYPE_VARPTR if it represents a global variable, will be nonempty
// OPCODETYPE_FUNC* with fntype=FUNCTYPE_EELFUNC
const char *relname;
};
static void *newTmpBlock(compileContext *ctx, int size)
{
const int align = 8;
const int a1=align-1;
char *p=(char*)__newBlock(&ctx->tmpblocks_head,size+a1, 0);
return p+((align-(((INT_PTR)p)&a1))&a1);
}
static void *__newBlock_align(compileContext *ctx, int size, int align, int isForCode)
{
const int a1=align-1;
char *p=(char*)__newBlock(
(
isForCode < 0 ? (isForCode == -2 ? &ctx->pblocks : &ctx->tmpblocks_head) :
isForCode > 0 ? &ctx->blocks_head :
&ctx->blocks_head_data) ,size+a1, isForCode>0);
return p+((align-(((INT_PTR)p)&a1))&a1);
}
static opcodeRec *newOpCode(compileContext *ctx, const char *str, int opType)
{
const size_t strszfull = str ? strlen(str) : 0;
const size_t str_sz = wdl_min(NSEEL_MAX_VARIABLE_NAMELEN, strszfull);
opcodeRec *rec = (opcodeRec*)__newBlock_align(ctx,
(int) (sizeof(opcodeRec) + (str_sz>0 ? str_sz+1 : 0)),
8, ctx->isSharedFunctions ? 0 : -1);
if (rec)
{
memset(rec,0,sizeof(*rec));
rec->opcodeType = opType;
if (str_sz > 0)
{
char *p = (char *)(rec+1);
memcpy(p,str,str_sz);
p[str_sz]=0;
rec->relname = p;
}
else
{
rec->relname = "";
}
}
return rec;
}
#define newCodeBlock(x,a) __newBlock_align(ctx,x,a,1)
#define newDataBlock(x,a) __newBlock_align(ctx,x,a,0)
#define newCtxDataBlock(x,a) __newBlock_align(ctx,x,a,-2)
static void freeBlocks(llBlock **start);
static int __growbuf_resize(eel_growbuf *buf, int newsize)
{
if (newsize<0)
{
free(buf->ptr);
buf->ptr=NULL;
buf->alloc=buf->size=0;
return 0;
}
if (newsize > buf->alloc)
{
const int newalloc = newsize + 4096 + newsize/2;
void *newptr = realloc(buf->ptr,newalloc);
if (!newptr)
{
newptr = malloc(newalloc);
if (!newptr) return 1;
if (buf->ptr && buf->size) memcpy(newptr,buf->ptr,buf->size);
free(buf->ptr);
buf->ptr=newptr;
}
else
buf->ptr = newptr;
buf->alloc=newalloc;
}
buf->size = newsize;
return 0;
}
#ifndef DECL_ASMFUNC
#define DECL_ASMFUNC(x) \
void nseel_asm_##x(void); \
void nseel_asm_##x##_end(void);
void _asm_megabuf(void);
void _asm_megabuf_end(void);
void _asm_gmegabuf(void);
void _asm_gmegabuf_end(void);
#endif
DECL_ASMFUNC(booltofp)
DECL_ASMFUNC(fptobool)
DECL_ASMFUNC(fptobool_rev)
DECL_ASMFUNC(sin)
DECL_ASMFUNC(cos)
DECL_ASMFUNC(tan)
DECL_ASMFUNC(1pdd)
DECL_ASMFUNC(2pdd)
DECL_ASMFUNC(2pdds)
DECL_ASMFUNC(1pp)
DECL_ASMFUNC(2pp)
DECL_ASMFUNC(sqr)
DECL_ASMFUNC(sqrt)
DECL_ASMFUNC(log)
DECL_ASMFUNC(log10)
DECL_ASMFUNC(abs)
DECL_ASMFUNC(min)
DECL_ASMFUNC(max)
DECL_ASMFUNC(min_fp)
DECL_ASMFUNC(max_fp)
DECL_ASMFUNC(sig)
DECL_ASMFUNC(sign)
DECL_ASMFUNC(band)
DECL_ASMFUNC(bor)
DECL_ASMFUNC(bnot)
DECL_ASMFUNC(bnotnot)
DECL_ASMFUNC(if)
DECL_ASMFUNC(fcall)
DECL_ASMFUNC(repeat)
DECL_ASMFUNC(repeatwhile)
DECL_ASMFUNC(equal)
DECL_ASMFUNC(equal_exact)
DECL_ASMFUNC(notequal_exact)
DECL_ASMFUNC(notequal)
DECL_ASMFUNC(below)
DECL_ASMFUNC(above)
DECL_ASMFUNC(beloweq)
DECL_ASMFUNC(aboveeq)
DECL_ASMFUNC(assign)
DECL_ASMFUNC(assign_fromfp)
DECL_ASMFUNC(assign_fast)
DECL_ASMFUNC(assign_fast_fromfp)
DECL_ASMFUNC(add)
DECL_ASMFUNC(sub)
DECL_ASMFUNC(add_op)
DECL_ASMFUNC(sub_op)
DECL_ASMFUNC(add_op_fast)
DECL_ASMFUNC(sub_op_fast)
DECL_ASMFUNC(mul)
DECL_ASMFUNC(div)
DECL_ASMFUNC(mul_op)
DECL_ASMFUNC(div_op)
DECL_ASMFUNC(mul_op_fast)
DECL_ASMFUNC(div_op_fast)
DECL_ASMFUNC(mod)
DECL_ASMFUNC(shl)
DECL_ASMFUNC(shr)
DECL_ASMFUNC(mod_op)
DECL_ASMFUNC(or)
DECL_ASMFUNC(or0)
DECL_ASMFUNC(xor)
DECL_ASMFUNC(xor_op)
DECL_ASMFUNC(and)
DECL_ASMFUNC(or_op)
DECL_ASMFUNC(and_op)
DECL_ASMFUNC(uplus)
DECL_ASMFUNC(uminus)
DECL_ASMFUNC(invsqrt)
DECL_ASMFUNC(dbg_getstackptr)
#ifdef NSEEL_EEL1_COMPAT_MODE
DECL_ASMFUNC(exec2)
#endif
DECL_ASMFUNC(stack_push)
DECL_ASMFUNC(stack_pop)
DECL_ASMFUNC(stack_pop_fast) // just returns value, doesn't mod param
DECL_ASMFUNC(stack_peek)
DECL_ASMFUNC(stack_peek_int)
DECL_ASMFUNC(stack_peek_top)
DECL_ASMFUNC(stack_exch)
static void *NSEEL_PProc_GRAM(void *data, int data_size, compileContext *ctx)
{
if (data_size>0) data=EEL_GLUE_set_immediate(data, (INT_PTR)ctx->gram_blocks);
return data;
}
static void *NSEEL_PProc_Stack(void *data, int data_size, compileContext *ctx)
{
codeHandleType *ch=ctx->tmpCodeHandle;
if (data_size>0)
{
UINT_PTR m1=(UINT_PTR)(NSEEL_STACK_SIZE * sizeof(EEL_F) - 1);
UINT_PTR stackptr = ((UINT_PTR) (&ch->stack));
ch->want_stack=1;
if (!ch->stack) ch->stack = newDataBlock(NSEEL_STACK_SIZE*sizeof(EEL_F),NSEEL_STACK_SIZE*sizeof(EEL_F));
data=EEL_GLUE_set_immediate(data, stackptr);
data=EEL_GLUE_set_immediate(data, m1); // and
data=EEL_GLUE_set_immediate(data, ((UINT_PTR)ch->stack&~m1)); //or
}
return data;
}
static void *NSEEL_PProc_Stack_PeekInt(void *data, int data_size, compileContext *ctx, INT_PTR offs)
{
codeHandleType *ch=ctx->tmpCodeHandle;
if (data_size>0)
{
UINT_PTR m1=(UINT_PTR)(NSEEL_STACK_SIZE * sizeof(EEL_F) - 1);
UINT_PTR stackptr = ((UINT_PTR) (&ch->stack));
ch->want_stack=1;
if (!ch->stack) ch->stack = newDataBlock(NSEEL_STACK_SIZE*sizeof(EEL_F),NSEEL_STACK_SIZE*sizeof(EEL_F));
data=EEL_GLUE_set_immediate(data, stackptr);
data=EEL_GLUE_set_immediate(data, offs);
data=EEL_GLUE_set_immediate(data, m1); // and
data=EEL_GLUE_set_immediate(data, ((UINT_PTR)ch->stack&~m1)); //or
}
return data;
}
static void *NSEEL_PProc_Stack_PeekTop(void *data, int data_size, compileContext *ctx)
{
codeHandleType *ch=ctx->tmpCodeHandle;
if (data_size>0)
{
UINT_PTR stackptr = ((UINT_PTR) (&ch->stack));
ch->want_stack=1;
if (!ch->stack) ch->stack = newDataBlock(NSEEL_STACK_SIZE*sizeof(EEL_F),NSEEL_STACK_SIZE*sizeof(EEL_F));
data=EEL_GLUE_set_immediate(data, stackptr);
}
return data;
}
#if defined(_MSC_VER) && _MSC_VER >= 1400
//static double __floor(double a) { return floor(a); }
//static double __ceil(double a) { return ceil(a); }
#define floor __floor
#define ceil __ceil
#endif
#ifdef NSEEL_EEL1_COMPAT_MODE
static double eel1band(double a, double b)
{
return (fabs(a)>NSEEL_CLOSEFACTOR && fabs(b) > NSEEL_CLOSEFACTOR) ? 1.0 : 0.0;
}
static double eel1bor(double a, double b)
{
return (fabs(a)>NSEEL_CLOSEFACTOR || fabs(b) > NSEEL_CLOSEFACTOR) ? 1.0 : 0.0;
}
static double eel1sigmoid(double x, double constraint)
{
double t = (1+exp(-x * (constraint)));
return fabs(t)>NSEEL_CLOSEFACTOR ? 1.0/t : 0;
}
#endif
#define FUNCTIONTYPE_PARAMETERCOUNTMASK 0xff
#define BIF_NPARAMS_MASK 0x7ffff00
#define BIF_RETURNSONSTACK 0x0000100
#define BIF_LASTPARMONSTACK 0x0000200
#define BIF_RETURNSBOOL 0x0000400
#define BIF_LASTPARM_ASBOOL 0x0000800
// 0x00?0000 -- taken by FP stack flags
#define BIF_TAKES_VARPARM 0x0400000
#define BIF_TAKES_VARPARM_EX 0x0C00000 // this is like varparm but check count exactly
#define BIF_WONTMAKEDENORMAL 0x0100000
#define BIF_CLEARDENORMAL 0x0200000
#if defined(GLUE_HAS_FXCH) && GLUE_MAX_FPSTACK_SIZE > 0
#define BIF_SECONDLASTPARMST 0x0001000 // use with BIF_LASTPARMONSTACK only (last two parameters get passed on fp stack)
#define BIF_LAZYPARMORDERING 0x0002000 // allow optimizer to avoid fxch when using BIF_TWOPARMSONFPSTACK_LAZY etc
#define BIF_REVERSEFPORDER 0x0004000 // force a fxch (reverse order of last two parameters on fp stack, used by comparison functions)
#ifndef BIF_FPSTACKUSE
#define BIF_FPSTACKUSE(x) (((x)>=0&&(x)<8) ? ((7-(x))<<16):0)
#endif
#ifndef BIF_GETFPSTACKUSE
#define BIF_GETFPSTACKUSE(x) (7 - (((x)>>16)&7))
#endif
#else
// do not support fp stack use unless GLUE_HAS_FXCH and GLUE_MAX_FPSTACK_SIZE>0
#define BIF_SECONDLASTPARMST 0
#define BIF_LAZYPARMORDERING 0
#define BIF_REVERSEFPORDER 0
#define BIF_FPSTACKUSE(x) 0
#define BIF_GETFPSTACKUSE(x) 0
#endif
#define BIF_TWOPARMSONFPSTACK (BIF_SECONDLASTPARMST|BIF_LASTPARMONSTACK)
#define BIF_TWOPARMSONFPSTACK_LAZY (BIF_LAZYPARMORDERING|BIF_SECONDLASTPARMST|BIF_LASTPARMONSTACK)
#ifndef GLUE_HAS_NATIVE_TRIGSQRTLOG
static double sqrt_fabs(double a) { return sqrt(fabs(a)); }
#endif
EEL_F NSEEL_CGEN_CALL nseel_int_rand(EEL_F f);
#define FNPTR_HAS_CONDITIONAL_EXEC(op) \
(op->fntype == FN_LOGICAL_AND || \
op->fntype == FN_LOGICAL_OR || \
op->fntype == FN_IF_ELSE || \
op->fntype == FN_WHILE || \
op->fntype == FN_LOOP)
static functionType fnTable1[] = {
#ifndef GLUE_HAS_NATIVE_TRIGSQRTLOG
{ "sin", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_WONTMAKEDENORMAL, {&sin} },
{ "cos", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_CLEARDENORMAL, {&cos} },
{ "tan", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&tan} },
{ "sqrt", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_WONTMAKEDENORMAL, {&sqrt_fabs}, },
{ "log", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&log} },
{ "log10", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&log10} },
#else
{ "sin", nseel_asm_sin,nseel_asm_sin_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_WONTMAKEDENORMAL|BIF_FPSTACKUSE(1) },
{ "cos", nseel_asm_cos,nseel_asm_cos_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_CLEARDENORMAL|BIF_FPSTACKUSE(1) },
{ "tan", nseel_asm_tan,nseel_asm_tan_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(1) },
{ "sqrt", nseel_asm_sqrt,nseel_asm_sqrt_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(1)|BIF_WONTMAKEDENORMAL },
{ "log", nseel_asm_log,nseel_asm_log_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(3), },
{ "log10", nseel_asm_log10,nseel_asm_log10_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(3), },
#endif
{ "asin", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&asin}, },
{ "acos", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&acos}, },
{ "atan", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&atan}, },
{ "atan2", nseel_asm_2pdd,nseel_asm_2pdd_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_TWOPARMSONFPSTACK, {&atan2}, },
{ "exp", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK, {&exp}, },
{ "abs", nseel_asm_abs,nseel_asm_abs_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(0)|BIF_WONTMAKEDENORMAL },
{ "sqr", nseel_asm_sqr,nseel_asm_sqr_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(1) },
{ "min", nseel_asm_min,nseel_asm_min_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_FPSTACKUSE(3)|BIF_WONTMAKEDENORMAL },
{ "max", nseel_asm_max,nseel_asm_max_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_FPSTACKUSE(3)|BIF_WONTMAKEDENORMAL },
{ "sign", nseel_asm_sign,nseel_asm_sign_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(2)|BIF_CLEARDENORMAL, },
{ "rand", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_CLEARDENORMAL, {&nseel_int_rand}, },
//{ "floor", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_CLEARDENORMAL, {&floor} },
//{ "ceil", nseel_asm_1pdd,nseel_asm_1pdd_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_CLEARDENORMAL, {&ceil} },
{ "invsqrt", nseel_asm_invsqrt,nseel_asm_invsqrt_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(3), {GLUE_INVSQRT_NEEDREPL} },
{ "__dbg_getstackptr", nseel_asm_dbg_getstackptr,nseel_asm_dbg_getstackptr_end, 1|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(1), },
#ifdef NSEEL_EEL1_COMPAT_MODE
{ "sigmoid", nseel_asm_2pdd,nseel_asm_2pdd_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_TWOPARMSONFPSTACK, {&eel1sigmoid}, },
// these differ from _and/_or, they always evaluate both...
{ "band", nseel_asm_2pdd,nseel_asm_2pdd_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_TWOPARMSONFPSTACK|BIF_CLEARDENORMAL , {&eel1band}, },
{ "bor", nseel_asm_2pdd,nseel_asm_2pdd_end, 2|NSEEL_NPARAMS_FLAG_CONST|BIF_RETURNSONSTACK|BIF_TWOPARMSONFPSTACK|BIF_CLEARDENORMAL , {&eel1bor}, },
{"exec2",nseel_asm_exec2,nseel_asm_exec2_end,2|NSEEL_NPARAMS_FLAG_CONST|BIF_WONTMAKEDENORMAL},
{"exec3",nseel_asm_exec2,nseel_asm_exec2_end,3|NSEEL_NPARAMS_FLAG_CONST|BIF_WONTMAKEDENORMAL},
#endif // end EEL1 compat
{"freembuf",_asm_generic1parm,_asm_generic1parm_end,1,{&__NSEEL_RAM_MemFree},NSEEL_PProc_RAM},
{"memcpy",_asm_generic3parm,_asm_generic3parm_end,3,{&__NSEEL_RAM_MemCpy},NSEEL_PProc_RAM},
{"memset",_asm_generic3parm,_asm_generic3parm_end,3,{&__NSEEL_RAM_MemSet},NSEEL_PProc_RAM},
{"__memtop",_asm_generic1parm,_asm_generic1parm_end,1,{&__NSEEL_RAM_MemTop},NSEEL_PProc_RAM},
{"mem_set_values",_asm_generic2parm_retd,_asm_generic2parm_retd_end,2|BIF_TAKES_VARPARM|BIF_RETURNSONSTACK,{&__NSEEL_RAM_Mem_SetValues},NSEEL_PProc_RAM},
{"mem_get_values",_asm_generic2parm_retd,_asm_generic2parm_retd_end,2|BIF_TAKES_VARPARM|BIF_RETURNSONSTACK,{&__NSEEL_RAM_Mem_GetValues},NSEEL_PProc_RAM},
{"stack_push",nseel_asm_stack_push,nseel_asm_stack_push_end,1|BIF_FPSTACKUSE(0),{0,},NSEEL_PProc_Stack},
{"stack_pop",nseel_asm_stack_pop,nseel_asm_stack_pop_end,1|BIF_FPSTACKUSE(1),{0,},NSEEL_PProc_Stack},
{"stack_peek",nseel_asm_stack_peek,nseel_asm_stack_peek_end,1|NSEEL_NPARAMS_FLAG_CONST|BIF_LASTPARMONSTACK|BIF_FPSTACKUSE(0),{0,},NSEEL_PProc_Stack},
{"stack_exch",nseel_asm_stack_exch,nseel_asm_stack_exch_end,1|BIF_FPSTACKUSE(1), {0,},NSEEL_PProc_Stack_PeekTop},
};
static eel_function_table default_user_funcs;
static int functable_lowerbound(functionType *list, int list_sz, const char *name, int *ismatch)
{
int a = 0, c = list_sz;
while (a != c)
{
const int b = (a+c)/2;
const int cmp = stricmp(name,list[b].name);
if (cmp > 0) a = b+1;
else if (cmp < 0) c = b;
else
{
*ismatch = 1;
return b;
}
}
*ismatch = 0;
return a;
}
static int funcTypeCmp(const void *a, const void *b) { return stricmp(((functionType*)a)->name,((functionType*)b)->name); }
functionType *nseel_getFunctionByName(compileContext *ctx, const char *name, int *mchk)
{
eel_function_table *tab = ctx && ctx->registered_func_tab ? ctx->registered_func_tab : &default_user_funcs;
static char sorted;
const int fn1size = (int) (sizeof(fnTable1)/sizeof(fnTable1[0]));
int idx,match;
if (!sorted)
{
NSEEL_HOSTSTUB_EnterMutex();
if (!sorted) qsort(fnTable1,fn1size,sizeof(fnTable1[0]),funcTypeCmp);
sorted=1;
NSEEL_HOSTSTUB_LeaveMutex();
}
idx=functable_lowerbound(fnTable1,fn1size,name,&match);
if (match) return fnTable1+idx;
if ((!ctx || !(ctx->current_compile_flags&NSEEL_CODE_COMPILE_FLAG_ONLY_BUILTIN_FUNCTIONS)) && tab->list)
{
idx=functable_lowerbound(tab->list,tab->list_size,name,&match);
if (match)
{
if (mchk)
{
while (idx>0 && !stricmp(tab->list[idx-1].name,name)) idx--;
*mchk = tab->list_size - 1 - idx;
}
return tab->list + idx;
}
}
return NULL;
}
int NSEEL_init() // returns 0 on success
{
#ifdef EEL_VALIDATE_FSTUBS
int a;
for (a=0;a < sizeof(fnTable1)/sizeof(fnTable1[0]);a++)
{
char *code_startaddr = (char*)fnTable1[a].afunc;
char *endp = (char *)fnTable1[a].func_e;
// validate
int sz=0;
char *f=(char *)GLUE_realAddress(code_startaddr,endp,&sz);
if (f+sz > endp)
{
#ifdef _WIN32
OutputDebugString("bad eel function stub\n");
#else
printf("bad eel function stub\n");
#endif
*(char *)NULL = 0;
}
}
#ifdef _WIN32
OutputDebugString("eel function stub (builtin) validation complete\n");
#else
printf("eel function stub (builtin) validation complete\n");
#endif
#endif
NSEEL_quit();
return 0;
}
void NSEEL_quit()
{
free(default_user_funcs.list);
default_user_funcs.list = NULL;
default_user_funcs.list_size = 0;
}
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)
{
const int sz = (int) ((char *)_asm_generic2parm_retd_end-(char *)_asm_generic2parm_retd);
NSEEL_addfunctionex2(name,min_np|(want_exact?BIF_TAKES_VARPARM_EX:BIF_TAKES_VARPARM),(char *)_asm_generic2parm_retd,sz,pproc,fptr,NULL,destination);
}
void 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
{
char *stub=NULL;
int stubsz=0;
#define DOSTUB(np) { \
stub = (ret_type == 1 ? (char*)_asm_generic##np##parm_retd : (char*)_asm_generic##np##parm); \
stubsz = (int) ((ret_type == 1 ? (char*)_asm_generic##np##parm_retd_end : (char *)_asm_generic##np##parm_end) - stub); \
}
if (np == 1) DOSTUB(1)
else if (np == 2) DOSTUB(2)
else if (np == 3) DOSTUB(3)
#undef DOSTUB
if (stub) NSEEL_addfunctionex2(name,np|(ret_type == -1 ? BIF_RETURNSBOOL:0), stub, stubsz, pproc,fptr,NULL,destination);
}
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)
{
const int list_size_chunk = 128;
functionType *r;
if (!destination) destination = &default_user_funcs;
if (!destination->list || !(destination->list_size & (list_size_chunk-1)))
{
void *nv = realloc(destination->list, (destination->list_size + list_size_chunk)*sizeof(functionType));
if (!nv) return;
destination->list = (functionType *)nv;
}
if (destination->list)
{
int match,idx;
idx=functable_lowerbound(destination->list,destination->list_size,name,&match);
#ifdef EEL_VALIDATE_FSTUBS
{
char *endp = code_startaddr+code_len;
// validate
int sz=0;
char *f=(char *)GLUE_realAddress(code_startaddr,endp,&sz);
if (f+sz > endp)
{
#ifdef _WIN32
OutputDebugString("bad eel function stub\n");
#else
printf("bad eel function stub\n");
#endif
*(char *)NULL = 0;
}
#ifdef _WIN32
OutputDebugString(name);
OutputDebugString(" - validated eel function stub\n");
#else
printf("eel function stub validation complete for %s\n",name);
#endif
}
#endif
r = destination->list + idx;
if (idx < destination->list_size)
memmove(r + 1, r, (destination->list_size - idx) * sizeof(functionType));
destination->list_size++;
memset(r, 0, sizeof(functionType));
if (!(nparms & BIF_RETURNSBOOL))
{
if (code_startaddr == (void *)&_asm_generic1parm_retd ||
code_startaddr == (void *)&_asm_generic2parm_retd ||
code_startaddr == (void *)&_asm_generic3parm_retd)
{
nparms |= BIF_RETURNSONSTACK;
}
}
r->nParams = nparms;
r->name = name;
r->afunc = code_startaddr;
r->func_e = code_startaddr + code_len;
r->pProc = pproc;
r->replptrs[0] = fptr;
r->replptrs[1] = fptr2;
}
}
//---------------------------------------------------------------------------------------------------------------
static void freeBlocks(llBlock **start)
{
llBlock *s=*start;
*start=0;
while (s)
{
llBlock *llB = s->next;
free(s);
s=llB;
}
}
//---------------------------------------------------------------------------------------------------------------
static void *__newBlock(llBlock **start, int size, int wantMprotect)
{
#if !defined(EEL_DOESNT_NEED_EXEC_PERMS) && defined(_WIN32)
DWORD ov;
UINT_PTR offs,eoffs;
#endif
llBlock *llb;
int alloc_size;
if (*start && (LLB_DSIZE - (*start)->sizeused) >= size)
{
void *t=(*start)->block+(*start)->sizeused;
(*start)->sizeused+=(size+7)&~7;
return t;
}
alloc_size=sizeof(llBlock);
if ((int)size > LLB_DSIZE) alloc_size += size - LLB_DSIZE;
llb = (llBlock *)malloc(alloc_size); // grab bigger block if absolutely necessary (heh)
if (!llb) return NULL;
#ifndef EEL_DOESNT_NEED_EXEC_PERMS
if (wantMprotect)
{
#ifdef _WIN32
offs=((UINT_PTR)llb)&~4095;
eoffs=((UINT_PTR)llb + alloc_size + 4095)&~4095;
VirtualProtect((LPVOID)offs,eoffs-offs,PAGE_EXECUTE_READWRITE,&ov);
// MessageBox(NULL,"vprotecting, yay\n","a",0);
#else
{
static int pagesize = 0;
if (!pagesize)
{
pagesize=sysconf(_SC_PAGESIZE);
if (!pagesize) pagesize=4096;
}
uintptr_t offs,eoffs;
offs=((uintptr_t)llb)&~(pagesize-1);
eoffs=((uintptr_t)llb + alloc_size + pagesize-1)&~(pagesize-1);
mprotect((void*)offs,eoffs-offs,PROT_WRITE|PROT_READ|PROT_EXEC);
}
#endif
}
#endif
llb->sizeused=(size+7)&~7;
llb->next = *start;
*start = llb;
return llb->block;
}
//---------------------------------------------------------------------------------------------------------------
opcodeRec *nseel_createCompiledValue(compileContext *ctx, EEL_F value)
{
opcodeRec *r=newOpCode(ctx,NULL,OPCODETYPE_DIRECTVALUE);
if (r)
{
r->parms.dv.directValue = value;
}
return r;
}
opcodeRec *nseel_createCompiledValuePtr(compileContext *ctx, EEL_F *addrValue, const char *namestr)
{
opcodeRec *r=newOpCode(ctx,namestr,OPCODETYPE_VARPTR);
if (!r) return 0;
r->parms.dv.valuePtr=addrValue;
return r;
}
static int validate_varname_for_function(compileContext *ctx, const char *name)
{
if (!ctx->function_curName || !ctx->function_globalFlag) return 1;
if (ctx->function_localTable_Size[2] > 0 && ctx->function_localTable_Names[2])
{
char * const * const namelist = ctx->function_localTable_Names[2];
const int namelist_sz = ctx->function_localTable_Size[2];
int i;
const size_t name_len = strlen(name);
for (i=0;i<namelist_sz;i++)
{
const char *nmchk=namelist[i];
const size_t l = strlen(nmchk);
if (l > 1 && nmchk[l-1] == '*')
{
if (name_len >= l && !strnicmp(nmchk,name,l-1) && name[l-1]=='.') return 1;
}
else
{
if (name_len == l && !stricmp(nmchk,name)) return 1;
}
}
}
return 0;
}
opcodeRec *nseel_resolve_named_symbol(compileContext *ctx, opcodeRec *rec, int parmcnt, int *errOut)
{
const int isFunctionMode = parmcnt >= 0;
int rel_prefix_len=0;
int rel_prefix_idx=-2;
int i;
char match_parmcnt[4]={-1,-1,-1,-1}; // [3] is guess
unsigned char match_parmcnt_pos=0;
char *sname = (char *)rec->relname;
int is_string_prefix = parmcnt < 0 && sname[0] == '#';
const char *prevent_function_calls = NULL;
if (errOut) *errOut = 0;
if (sname) sname += is_string_prefix;
if (rec->opcodeType != OPCODETYPE_VARPTR || !sname || !sname[0]) return NULL;
if (!isFunctionMode && !is_string_prefix && !strnicmp(sname,"reg",3) && isdigit(sname[3]) && isdigit(sname[4]) && !sname[5])
{
EEL_F *a=get_global_var(ctx,sname,1);
if (a)
{
rec->parms.dv.valuePtr = a;
sname[0]=0; // for dump_ops compat really, but this shouldn't be needed anyway
}
return rec;
}
if (ctx->function_curName)
{
if (!strnicmp(sname,"this.",5))
{
rel_prefix_len=5;
rel_prefix_idx=-1;
}
else if (!stricmp(sname,"this"))
{
rel_prefix_len=4;
rel_prefix_idx=-1;
}
// scan for parameters/local variables before user functions
if (rel_prefix_idx < -1 &&
ctx->function_localTable_Size[0] > 0 &&
ctx->function_localTable_Names[0] &&
ctx->function_localTable_ValuePtrs)
{
char * const * const namelist = ctx->function_localTable_Names[0];
const int namelist_sz = ctx->function_localTable_Size[0];
for (i=0; i < namelist_sz; i++)
{
const char *p = namelist[i];
if (p)
{
if (!isFunctionMode && !is_string_prefix && !strnicmp(p,sname,NSEEL_MAX_VARIABLE_NAMELEN))
{
rec->opcodeType = OPCODETYPE_VARPTRPTR;
rec->parms.dv.valuePtr=(EEL_F *)(ctx->function_localTable_ValuePtrs+i);
rec->parms.dv.directValue=0.0;
return rec;
}
else
{
const size_t plen = strlen(p);
if (plen > 1 && p[plen-1] == '*' && !strnicmp(p,sname,plen-1) && ((sname[plen-1] == '.'&&sname[plen]) || !sname[plen-1]))
{
rel_prefix_len=(int) (sname[plen-1] ? plen : plen-1);
rel_prefix_idx=i;
break;
}
}
}
}
}
// if instance name set, translate sname or sname.* into "this.sname.*"
if (rel_prefix_idx < -1 &&
ctx->function_localTable_Size[1] > 0 &&
ctx->function_localTable_Names[1])
{
char * const * const namelist = ctx->function_localTable_Names[1];
const int namelist_sz = ctx->function_localTable_Size[1];
const char *full_sname = rec->relname; // include # in checks
for (i=0; i < namelist_sz; i++)
{
const char *p = namelist[i];
if (p && *p)
{
const size_t tl = strlen(p);
if (!strnicmp(p,full_sname,tl) && (full_sname[tl] == 0 || full_sname[tl] == '.'))
{
rel_prefix_len=0; // treat as though this. prefixes is present
rel_prefix_idx=-1;
break;
}
}
}