/
script_parser.y
6516 lines (5363 loc) · 169 KB
/
script_parser.y
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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2011 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
%{
/*
* script.y
*
* The yacc grammar for the scipt files.
*/
#include "lib/framework/frame.h"
#include "lib/framework/frameresource.h"
#include "lib/framework/string_ext.h"
#include "lib/script/interpreter.h"
#include "lib/script/parse.h"
#include "lib/script/script.h"
#include <physfs.h>
/* this will give us a more detailed error output */
#define YYERROR_VERBOSE true
/* Script defines stack */
extern int scr_lex(void);
extern int scr_lex_destroy(void);
/* Error return codes for code generation functions */
typedef enum _code_error
{
CE_OK, // No error
CE_MEMORY, // Out of memory
CE_PARSE // A parse error occured
} CODE_ERROR;
/* Turn off a couple of warnings that the yacc generated code gives */
/* Pointer to the compiled code */
static SCRIPT_CODE *psFinalProg=NULL;
/* Pointer to current block of compiled code */
static CODE_BLOCK *psCurrBlock=NULL;
/* Pointer to current block of conditional code */
static COND_BLOCK *psCondBlock=NULL;
/* Pointer to current block of object variable code */
static OBJVAR_BLOCK *psObjVarBlock=NULL;
/* Pointer to current block of compiled parameter code */
static PARAM_BLOCK *psCurrPBlock=NULL;
/* Any errors occured? */
static BOOL bError=false;
//String support
//-----------------------------
char msg[MAXSTRLEN];
extern char STRSTACK[MAXSTACKLEN][MAXSTRLEN]; // just a simple string "stack"
extern UDWORD CURSTACKSTR; //Current string index
/* Pointer into the current code block */
static INTERP_VAL *ip;
/* Pointer to current parameter declaration block */
//static PARAM_DECL *psCurrParamDecl=NULL;
/* Pointer to current trigger subdeclaration */
static TRIGGER_DECL *psCurrTDecl=NULL;
/* Pointer to current variable subdeclaration */
static VAR_DECL *psCurrVDecl=NULL;
/* Pointer to the current variable identifier declaration */
static VAR_IDENT_DECL *psCurrVIdentDecl=NULL;
/* Pointer to the current array access block */
static ARRAY_BLOCK *psCurrArrayBlock=NULL;
/* Return code from code generation functions */
static CODE_ERROR codeRet;
/* The list of global variables */
static VAR_SYMBOL *psGlobalVars=NULL;
/* The list of global arrays */
static VAR_SYMBOL *psGlobalArrays=NULL;
#define maxEventsLocalVars 1200
static VAR_SYMBOL *psLocalVarsB[maxEventsLocalVars]; /* local var storage */
static UDWORD numEventLocalVars[maxEventsLocalVars]; /* number of declard local vars for each event */
EVENT_SYMBOL *psCurEvent = NULL; /* stores current event: for local var declaration */
/* The current object variable context */
static INTERP_TYPE objVarContext = (INTERP_TYPE)0;
/* Control whether debug info is generated */
static BOOL genDebugInfo = true;
/* Currently defined triggers */
static TRIGGER_SYMBOL *psTriggers;
static UDWORD numTriggers;
/* Currently defined events */
static EVENT_SYMBOL *psEvents;
static UDWORD numEvents;
/* This is true when local variables are being defined.
* (So local variables can have the same name as global ones)
*/
static BOOL localVariableDef=false;
/* The identifier for the current script function being defined */
//static char *pCurrFuncIdent=NULL;
/* A temporary store for a line number - used when
* generating debugging info for functions, conditionals and loops.
*/
static UDWORD debugLine;
/* The table of user types */
TYPE_SYMBOL *asScrTypeTab;
/* The table of instinct function type definitions */
FUNC_SYMBOL *asScrInstinctTab;
/* The table of external variables and their access functions */
VAR_SYMBOL *asScrExternalTab;
/* The table of object variables and their access functions. */
VAR_SYMBOL *asScrObjectVarTab;
/* The table of constant variables */
CONST_SYMBOL *asScrConstantTab;
/* The table of callback triggers */
CALLBACK_SYMBOL *asScrCallbackTab;
/* Used for additional debug output */
void script_debug(const char *pFormat, ...);
/****************************************************************************************
*
* Code Block Macros
*
* These macros are used to allocate and free the different types of code
* block used within the compiler
*/
/* What the macro should do if it has an allocation error.
* This is different depending on whether the macro is used
* in a function, or in a rule body.
*
* This definition is used within the code generation functions
* and is then changed for use within a rule body.
*/
#define ALLOC_ERROR_ACTION return CE_MEMORY
/* Macro to allocate a program structure, size is in _bytes_ */
#define ALLOC_PROG(psProg, codeSize, pAICode, numGlobs, numArys, numTrigs, numEvnts) \
(psProg) = (SCRIPT_CODE *)malloc(sizeof(SCRIPT_CODE)); \
if ((psProg) == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psProg)->pCode = (INTERP_VAL *)malloc((codeSize) * sizeof(INTERP_VAL)); \
if ((psProg)->pCode == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
if (numGlobs > 0) \
{ \
(psProg)->pGlobals = (INTERP_TYPE *)malloc(sizeof(INTERP_TYPE) * (numGlobs)); \
if ((psProg)->pGlobals == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
} \
else \
{ \
(psProg)->pGlobals = NULL; \
} \
if (numArys > 0) \
{ \
(psProg)->psArrayInfo = (ARRAY_DATA *)malloc(sizeof(ARRAY_DATA) * (numArys)); \
if ((psProg)->psArrayInfo == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
} \
else \
{ \
(psProg)->psArrayInfo = NULL; \
} \
(psProg)->numArrays = (UWORD)(numArys); \
if ((numTrigs) > 0) \
{ \
(psProg)->pTriggerTab = malloc(sizeof(UWORD) * ((numTrigs) + 1)); \
if ((psProg)->pTriggerTab == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psProg)->psTriggerData = malloc(sizeof(TRIGGER_DATA) * (numTrigs)); \
if ((psProg)->psTriggerData == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
} \
else \
{ \
(psProg)->pTriggerTab = NULL; \
(psProg)->psTriggerData = NULL; \
} \
(psProg)->pEventTab = malloc(sizeof(UWORD) * ((numEvnts) + 1)); \
if ((psProg)->pEventTab == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psProg)->pEventLinks = malloc(sizeof(SWORD) * (numEvnts)); \
if ((psProg)->pEventLinks == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psProg)->numGlobals = (UWORD)(numGlobs); \
(psProg)->numTriggers = (UWORD)(numTriggers); \
(psProg)->numEvents = (UWORD)(numEvnts); \
(psProg)->size = (codeSize) * sizeof(INTERP_VAL);
/* Macro to allocate a code block, blockSize - number of INTERP_VALs we need*/
#define ALLOC_BLOCK(psBlock, num) \
(psBlock) = (CODE_BLOCK *)malloc(sizeof(CODE_BLOCK)); \
if ((psBlock) == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psBlock)->pCode = (INTERP_VAL *)malloc((num) * sizeof(INTERP_VAL)); \
if ((psBlock)->pCode == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
free((psBlock)); \
ALLOC_ERROR_ACTION; \
} \
(psBlock)->size = (num); \
(psBlock)->debugEntries = 0
/* Macro to free a code block */
#define FREE_BLOCK(psBlock) \
free((psBlock)->pCode); \
free((psBlock))
/* Macro to allocate a parameter block */
#define ALLOC_PBLOCK(psBlock, num, paramSize) \
(psBlock) = (PARAM_BLOCK *)malloc(sizeof(PARAM_BLOCK)); \
if ((psBlock) == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psBlock)->pCode = (INTERP_VAL *)malloc((num) * sizeof(INTERP_VAL)); \
if ((psBlock)->pCode == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
free((psBlock)); \
ALLOC_ERROR_ACTION; \
} \
(psBlock)->aParams = (INTERP_TYPE *)malloc(sizeof(INTERP_TYPE) * (paramSize)); \
if ((psBlock)->aParams == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
free((psBlock)->pCode); \
free((psBlock)); \
ALLOC_ERROR_ACTION; \
} \
(psBlock)->size = (num); \
(psBlock)->numParams = (paramSize)
/* Macro to free a parameter block */
#define FREE_PBLOCK(psBlock) \
free((psBlock)->pCode); \
free((psBlock)->aParams); \
free((psBlock))
/* Macro to allocate a parameter declaration block */
#define ALLOC_PARAMDECL(psPDecl, num) \
(psPDecl) = (PARAM_DECL *)malloc(sizeof(PARAM_DECL)); \
if ((psPDecl) == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psPDecl)->aParams = (INTERP_TYPE *)malloc(sizeof(INTERP_TYPE) * (num)); \
if ((psPDecl)->aParams == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psPDecl)->numParams = (num)
/* Macro to free a parameter declaration block */
#define FREE_PARAMDECL(psPDecl) \
free((psPDecl)->aParams); \
free((psPDecl))
/* Macro to allocate a conditional block */
#define ALLOC_CONDBLOCK(psCB, num, numBlocks) \
(psCB) = (COND_BLOCK *)malloc(sizeof(COND_BLOCK)); \
if ((psCB) == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psCB)->aOffsets = (UDWORD *)malloc(sizeof(SDWORD) * (num)); \
if ((psCB)->aOffsets == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psCB)->pCode = (INTERP_VAL *)malloc((numBlocks) * sizeof(INTERP_VAL)); \
if ((psCB)->pCode == NULL) \
{ \
debug(LOG_ERROR, "Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psCB)->size = (numBlocks); \
(psCB)->numOffsets = (num)
/* Macro to free a conditional block */
#define FREE_CONDBLOCK(psCB) \
free((psCB)->aOffsets); \
free((psCB)->pCode); \
free(psCB)
/* Macro to free a code block */
#define FREE_USERBLOCK(psBlock) \
free((psBlock)->pCode); \
free((psBlock))
/* Macro to allocate an object variable block */
#define ALLOC_OBJVARBLOCK(psOV, blockSize, psVar) \
(psOV) = (OBJVAR_BLOCK *)malloc(sizeof(OBJVAR_BLOCK)); \
if ((psOV) == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psOV)->pCode = (INTERP_VAL *)malloc((blockSize) * sizeof(INTERP_VAL)); \
if ((psOV)->pCode == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psOV)->size = (blockSize); \
(psOV)->psObjVar = (psVar)
/* Macro to free an object variable block */
#define FREE_OBJVARBLOCK(psOV) \
free((psOV)->pCode); \
free(psOV)
/* Macro to allocate an array variable block */
#define ALLOC_ARRAYBLOCK(psAV, blockSize, psVar) \
(psAV) = (ARRAY_BLOCK *)malloc(sizeof(ARRAY_BLOCK)); \
if ((psAV) == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psAV)->pCode = (INTERP_VAL *)malloc((blockSize) * sizeof(INTERP_VAL)); \
if ((psAV)->pCode == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psAV)->size = (blockSize); \
(psAV)->dimensions = 1; \
(psAV)->psArrayVar = (psVar)
/* Macro to free an object variable block */
#define FREE_ARRAYBLOCK(psAV) \
free((psAV)->pCode); \
free((psAV))
/* Allocate a trigger subdecl */
#define ALLOC_TSUBDECL(psTSub, blockType, blockSize, blockTime) \
(psTSub) = malloc(sizeof(TRIGGER_DECL)); \
if ((psTSub) == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psTSub)->type = (blockType); \
(psTSub)->time = (blockTime); \
if ((blockSize) > 0) \
{ \
(psTSub)->pCode = (INTERP_VAL *)malloc((blockSize) * sizeof(INTERP_VAL)); \
if ((psTSub)->pCode == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
(psTSub)->size = (blockSize); \
} \
else \
{ \
(psTSub)->pCode = NULL; \
(psTSub)->size = 0; \
}
/* Free a trigger subdecl */
#define FREE_TSUBDECL(psTSub) \
if ((psTSub)->pCode) \
{ \
free((psTSub)->pCode); \
} \
free(psTSub)
/* Allocate a variable declaration block */
#define ALLOC_VARDECL(psDcl) \
(psDcl)=malloc(sizeof(VAR_DECL)); \
if ((psDcl) == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
}
/* Free a variable declaration block */
#define FREE_VARDECL(psDcl) \
free(psDcl)
/* Allocate a variable declaration block */
static inline CODE_ERROR do_ALLOC_VARIDENTDECL(VAR_IDENT_DECL** psDcl, const char* ident, unsigned int dim)
{
// Allocate memory
*psDcl = malloc(sizeof(VAR_IDENT_DECL));
if (*psDcl == NULL)
{
scr_error("Out of memory");
ALLOC_ERROR_ACTION;
}
// Copy over the "ident" string (if it's there)
if (ident != NULL)
{
(*psDcl)->pIdent = strdup(ident);
if ((*psDcl)->pIdent == NULL)
{
scr_error("Out of memory");
ALLOC_ERROR_ACTION;
}
}
else
{
(*psDcl)->pIdent = NULL;
}
(*psDcl)->dimensions = dim;
return CE_OK;
}
#define ALLOC_VARIDENTDECL(psDcl, ident, dim) \
{ \
CODE_ERROR err = do_ALLOC_VARIDENTDECL(&psDcl, ident, dim); \
if (err != CE_OK) \
return err; \
}
/* Free a variable declaration block */
static void freeVARIDENTDECL(VAR_IDENT_DECL* psDcl)
{
free(psDcl->pIdent);
free(psDcl);
}
/****************************************************************************************
*
* Code block manipulation macros.
*
* These are used to copy chunks of code from one block to another
* or to insert opcodes or other values into a code block.
* All the macros use the ip parameter. This is a pointer into the code
* block that is incremented by the macro. This ensures that it always points
* to the next free space in the code block.
*/
/* Macro to store an opcode in a code block, opcode type is '-1' */
#define PUT_OPCODE(ip, opcode) \
(ip)->type = VAL_OPCODE; \
(ip)->v.ival = (opcode) << OPCODE_SHIFT; \
(ip)++
/* Macro to put a packed opcode in a code block */
#define PUT_PKOPCODE(ip, opcode, data) \
(ip)->type = VAL_PKOPCODE; \
(ip)->v.ival = ((SDWORD)(data)) & OPCODE_DATAMASK; \
(ip)->v.ival = (((SDWORD)(opcode)) << OPCODE_SHIFT) | ((ip)->v.ival); \
(ip)++
/* Special macro for floats, could be a bit tricky */
#define PUT_DATA_FLOAT(ip, data) \
ip->type = VAL_FLOAT; \
ip->v.fval = (float)(data); \
ip++
/* Macros to store a value in a code block */
#define PUT_DATA_BOOL(ip, value) \
(ip)->type = VAL_BOOL; \
(ip)->v.bval = (BOOL)(value); \
(ip)++
#define PUT_DATA_INT(ip, value) \
(ip)->type = VAL_INT; \
(ip)->v.ival = (SDWORD)(value); \
(ip)++
#define PUT_DATA_STRING(ip, value) \
(ip)->type = VAL_STRING; \
(ip)->v.sval = (char *)(value); \
(ip)++
#define PUT_OBJECT_CONST(ip, value, constType) \
(ip)->type = (constType); \
(ip)->v.oval = (value); \
(ip)++
#define PUT_STRING(ip, value) \
((INTERP_VAL *)(ip))->type = VAL_STRING; \
((INTERP_VAL *)(ip))->v.sval = (value); \
((INTERP_VAL *)(ip)) += 1
/* Macro to store an external function index in a code block */
#define PUT_FUNC_EXTERN(ip, func) \
(ip)->type = VAL_FUNC_EXTERN; \
(ip)->v.pFuncExtern = (SCRIPT_FUNC)func; \
(ip)++
/* Macro to store an internal (in-script, actually an event) function index in a code block */
#define PUT_EVENT(ip, func) \
(ip)->type = VAL_EVENT; \
(ip)->v.ival = (SDWORD)func; \
(ip)++
/* Macro to store trigger */
#define PUT_TRIGGER(ip, func) \
(ip)->type = VAL_TRIGGER; \
(ip)->v.ival = (SDWORD)func; \
(ip)++
/* Macro to store a function pointer in a code block */
#define PUT_VARFUNC(ip, func) \
(ip)->type = VAL_OBJ_GETSET; \
(ip)->v.pObjGetSet = (SCRIPT_VARFUNC)func; \
(ip)++
/* Macro to store a variable index number in a code block - NOT USED */
#define PUT_INDEX(ip, index) \
(ip)->type = -1; \
(ip)->v.ival = (SDWORD)(index); \
(ip)++
/* Macro to copy a code block into another code block */
#define PUT_BLOCK(ip, psOldBlock) \
memcpy(ip, (psOldBlock)->pCode, (psOldBlock)->size * sizeof(INTERP_VAL)); \
(ip) = (INTERP_VAL *)( (ip) + (psOldBlock)->size)
/***********************************************************************************
*
* Debugging information macros
*
* These macros are only used to generate debugging information for scripts.
*/
/* Macro to allocate debugging info for a CODE_BLOCK or a COND_BLOCK */
#define ALLOC_DEBUG(psBlock, num) \
if (genDebugInfo) \
{ \
(psBlock)->psDebug = (SCRIPT_DEBUG *)malloc(sizeof(SCRIPT_DEBUG) * (num)); \
if ((psBlock)->psDebug == NULL) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
memset((psBlock)->psDebug, 0, sizeof(SCRIPT_DEBUG) * (num));\
(psBlock)->debugEntries = (UWORD)(num); \
} \
else \
{ \
(psBlock)->psDebug = NULL; \
(psBlock)->debugEntries = 0; \
}
/* Macro to free debugging info */
#define FREE_DEBUG(psBlock) \
if (genDebugInfo) \
free((psBlock)->psDebug)
/* Macro to copy the debugging information from one block to another */
#define PUT_DEBUG(psFinal, psBlock) \
if (genDebugInfo) \
{ \
memcpy((psFinal)->psDebug, (psBlock)->psDebug, \
sizeof(SCRIPT_DEBUG) * (psBlock)->debugEntries); \
(psFinal)->debugEntries = (psBlock)->debugEntries; \
}
/* Macro to combine the debugging information in two blocks into a third block */
static UDWORD _dbEntry;
static SCRIPT_DEBUG *_psCurr;
#define COMBINE_DEBUG(psFinal, psBlock1, psBlock2) \
if (genDebugInfo) \
{ \
memcpy((psFinal)->psDebug, (psBlock1)->psDebug, \
sizeof(SCRIPT_DEBUG) * (psBlock1)->debugEntries); \
_baseOffset = (psBlock1)->size / sizeof(UDWORD); \
for(_dbEntry = 0; _dbEntry < (psBlock2)->debugEntries; _dbEntry++) \
{ \
_psCurr = (psFinal)->psDebug + (psBlock1)->debugEntries + _dbEntry; \
_psCurr->line = (psBlock2)->psDebug[_dbEntry].line; \
_psCurr->offset = (psBlock2)->psDebug[_dbEntry].offset + _baseOffset; \
} \
(psFinal)->debugEntries = (psBlock1)->debugEntries + (psBlock2)->debugEntries; \
}
/* Macro to append some debugging information onto a block, given the instruction
offset of the debugging information already in the destination block */
#define APPEND_DEBUG(psFinal, baseOffset, psBlock) \
if (genDebugInfo) \
{ \
for(_dbEntry = 0; _dbEntry < (psBlock)->debugEntries; _dbEntry++) \
{ \
_psCurr = (psFinal)->psDebug + (psFinal)->debugEntries + _dbEntry; \
_psCurr->line = (psBlock)->psDebug[_dbEntry].line; \
_psCurr->offset = (psBlock)->psDebug[_dbEntry].offset + (baseOffset); \
} \
(psFinal)->debugEntries = (UWORD)((psFinal)->debugEntries + (psBlock)->debugEntries); \
}
/* Macro to store a label in the debug info */
#define DEBUG_LABEL(psBlock, offset, pString) \
if (genDebugInfo) \
{ \
(psBlock)->psDebug[offset].pLabel = malloc(strlen(pString)+1); \
if (!(psBlock)->psDebug[offset].pLabel) \
{ \
scr_error("Out of memory"); \
ALLOC_ERROR_ACTION; \
} \
strcpy((psBlock)->psDebug[offset].pLabel, pString); \
}
/***************************************************************************************
*
* Code generation functions
*
* These functions are used within rule bodies to generate code.
*/
/* Macro to deal with the errors returned by code generation functions.
* Used within the rule body.
*/
#define CHECK_CODE_ERROR(error) \
if ((error) == CE_MEMORY) \
{ \
YYABORT; \
} \
else if ((error) == CE_PARSE) \
{ \
YYERROR; \
}
void script_debug(const char *pFormat, ...)
{
char buffer[500];
va_list pArgs;
va_start(pArgs, pFormat);
vsnprintf(buffer, sizeof(buffer), pFormat, pArgs);
va_end(pArgs);
debug(LOG_SCRIPT, "%s", buffer);
}
/* Generate the code for a function call, checking the parameter
* types match.
*/
static CODE_ERROR scriptCodeFunction(FUNC_SYMBOL *psFSymbol, // The function being called
PARAM_BLOCK *psPBlock, // The functions parameters
BOOL expContext, // Whether the function is being
// called in an expression context
CODE_BLOCK **ppsCBlock) // The generated code block
{
UDWORD size, i;
INTERP_VAL *ip;
BOOL typeError = false;
char aErrorString[255];
INTERP_TYPE type1,type2;
ASSERT( psFSymbol != NULL, "ais_CodeFunction: Invalid function symbol pointer" );
ASSERT( psPBlock != NULL,
"scriptCodeFunction: Invalid param block pointer" );
ASSERT( (psPBlock->size == 0) || psPBlock->pCode != NULL,
"scriptCodeFunction: Invalid parameter code pointer" );
ASSERT( ppsCBlock != NULL,
"scriptCodeFunction: Invalid generated code block pointer" );
/* Check the parameter types match what the function needs */
for(i=0; (i<psFSymbol->numParams) && (i<psPBlock->numParams); i++)
{
/* if (psFSymbol->aParams[i] != VAL_VOID &&
psFSymbol->aParams[i] != psPBlock->aParams[i])*/
//TODO: string support
if(psFSymbol->aParams[i] != VAL_STRING) // string - allow mixed types if string is parameter type //TODO: tweak this
{
type1 = psFSymbol->aParams[i];
type2 = psPBlock->aParams[i];
if (!interpCheckEquiv(type1, type2))
{
debug(LOG_ERROR, "scriptCodeFunction: Type mismatch for parameter %d (%d/%d)", i, psFSymbol->aParams[i], psPBlock->aParams[i]);
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for parameter %d", i);
scr_error("%s", aErrorString);
typeError = true;
}
}
//else
//{
// debug(LOG_SCRIPT, "scriptCodeFunction: %s takes string as parameter %d (provided: %d)", psFSymbol->pIdent, i, psPBlock->aParams[i]);
//}
}
/* Check the number of parameters matches that expected */
if (psFSymbol->numParams != psPBlock->numParams)
{
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psFSymbol->numParams);
scr_error("%s", aErrorString);
*ppsCBlock = NULL;
return CE_PARSE;
}
if (typeError)
{
/* Report the error here so all the */
/* type mismatches are reported */
*ppsCBlock = NULL;
return CE_PARSE;
}
//size = psPBlock->size + sizeof(OPCODE) + sizeof(SCRIPT_FUNC);
size = psPBlock->size + 1 + 1; //size + opcode + sizeof(SCRIPT_FUNC)
if (!expContext && (psFSymbol->type != VAL_VOID))
{
//size += sizeof(OPCODE);
size += 1; //+ 1 additional opcode to pop value if needed
}
ALLOC_BLOCK(*ppsCBlock, size);
ip = (*ppsCBlock)->pCode;
(*ppsCBlock)->type = psFSymbol->type;
/* Copy in the code for the parameters */
PUT_BLOCK(ip, psPBlock);
FREE_PBLOCK(psPBlock);
/* Make the function call */
if (psFSymbol->script)
{
/* function defined in this script */
// PUT_OPCODE(ip, OP_FUNC);
// PUT_SCRIPTFUNC(ip, psFSymbol);
ASSERT(false, "wrong function type call");
}
else
{
/* call an instinct function */
PUT_OPCODE(ip, OP_CALL);
PUT_FUNC_EXTERN(ip, psFSymbol->pFunc);
}
if (!expContext && (psFSymbol->type != VAL_VOID))
{
/* Clear the return value from the stack */
PUT_OPCODE(ip, OP_POP);
}
return CE_OK;
}
/* Function call: Check the parameter types match, assumes param count matched */
static UDWORD checkFuncParamTypes(EVENT_SYMBOL *psFSymbol, // The function being called
PARAM_BLOCK *psPBlock) // The generated code block
{
UDWORD i;
//debug(LOG_SCRIPT,"checkFuncParamTypes");
/* Check the parameter types match what the function needs */
for(i=0; (i<psFSymbol->numParams) && (i<psPBlock->numParams); i++)
{
//TODO: string support
//if(psFSymbol->aParams[i] != VAL_STRING) // string - allow mixed types if string is parameter type
//{
if (!interpCheckEquiv(psFSymbol->aParams[i], psPBlock->aParams[i]))
{
debug(LOG_ERROR, "checkFuncParamTypes: Type mismatch for parameter %d ('1' based) in Function '%s' (provided type: %d, expected: %d)", (i+1), psFSymbol->pIdent, psPBlock->aParams[i], psFSymbol->aParams[i]);
scr_error("Parameter type mismatch");
return i+1;
}
//}
}
//debug(LOG_SCRIPT,"END checkFuncParamTypes");
return 0; //all ok
}
#ifdef UNUSED
/*
* function call
*/
static CODE_ERROR scriptCodeCallFunction(FUNC_SYMBOL *psFSymbol, // The function being called
PARAM_BLOCK *psPBlock, // The functions parameters
CODE_BLOCK **ppsCBlock) // The generated code block
{
UDWORD size;
INTERP_VAL *ip;
//debug(LOG_SCRIPT, "scriptCodeCallFunction");
ASSERT( psFSymbol != NULL, "scriptCodeCallFunction: Invalid function symbol pointer" );
ASSERT( psPBlock != NULL,
"scriptCodeCallFunction: Invalid param block pointer" );
ASSERT( (psPBlock->size == 0) || psPBlock->pCode != NULL,
"scriptCodeCallFunction: Invalid parameter code pointer" );
ASSERT( ppsCBlock != NULL,
"scriptCodeCallFunction: Invalid generated code block pointer" );
//size = psPBlock->size + sizeof(OPCODE) + sizeof(SCRIPT_FUNC);
size = psPBlock->size + 1 + 1; //size + opcode + SCRIPT_FUNC
ALLOC_BLOCK(*ppsCBlock, size);
ip = (*ppsCBlock)->pCode;
(*ppsCBlock)->type = psFSymbol->type;
/* Copy in the code for the parameters */
PUT_BLOCK(ip, psPBlock);
FREE_PBLOCK(psPBlock);
/* Make the function call */
PUT_OPCODE(ip, OP_CALL);
PUT_FUNC_EXTERN(ip, psFSymbol->pFunc);
//debug(LOG_SCRIPT, "END scriptCodeCallFunction");
return CE_OK;
}
#endif
/* Generate the code for a parameter callback, checking the parameter
* types match.
*/
static CODE_ERROR scriptCodeCallbackParams(
CALLBACK_SYMBOL *psCBSymbol, // The callback being called
PARAM_BLOCK *psPBlock, // The callbacks parameters
TRIGGER_DECL **ppsTDecl) // The generated code block
{
UDWORD size, i;
INTERP_VAL *ip;
BOOL typeError = false;
char aErrorString[255];
ASSERT( psPBlock != NULL,
"scriptCodeCallbackParams: Invalid param block pointer" );
ASSERT( (psPBlock->size == 0) || psPBlock->pCode != NULL,
"scriptCodeCallbackParams: Invalid parameter code pointer" );
ASSERT( ppsTDecl != NULL,
"scriptCodeCallbackParams: Invalid generated code block pointer" );
ASSERT( psCBSymbol->pFunc != NULL,
"scriptCodeCallbackParams: Expected function pointer for callback symbol" );
/* Check the parameter types match what the function needs */
for(i=0; (i<psCBSymbol->numParams) && (i<psPBlock->numParams); i++)
{
if (!interpCheckEquiv(psCBSymbol->aParams[i], psPBlock->aParams[i]))
{
snprintf(aErrorString, sizeof(aErrorString), "Type mismatch for parameter %d", i);
scr_error("%s", aErrorString);
typeError = true;
}
}
/* Check the number of parameters matches that expected */
if (psPBlock->numParams == 0)
{
scr_error("Expected parameters to callback");
*ppsTDecl = NULL;
return CE_PARSE;
}
else if (psCBSymbol->numParams != psPBlock->numParams)
{
snprintf(aErrorString, sizeof(aErrorString), "Expected %d parameters", psCBSymbol->numParams);
scr_error("%s", aErrorString);
*ppsTDecl = NULL;
return CE_PARSE;
}
if (typeError)
{
/* Return the error here so all the */
/* type mismatches are reported */
*ppsTDecl = NULL;
return CE_PARSE;
}
//size = psPBlock->size + sizeof(OPCODE) + sizeof(SCRIPT_FUNC);
size = psPBlock->size + 1 + 1; //size + opcode + SCRIPT_FUNC
ALLOC_TSUBDECL(*ppsTDecl, psCBSymbol->type, size, 0);
ip = (*ppsTDecl)->pCode;
/* Copy in the code for the parameters */
PUT_BLOCK(ip, psPBlock);
FREE_PBLOCK(psPBlock);
/* call the instinct function */
PUT_OPCODE(ip, OP_CALL);
PUT_FUNC_EXTERN(ip, psCBSymbol->pFunc);
return CE_OK;
}
/* Generate code for assigning a value to a variable */
static CODE_ERROR scriptCodeAssignment(VAR_SYMBOL *psVariable, // The variable to assign to
CODE_BLOCK *psValue, // The code for the value to
// assign
CODE_BLOCK **ppsBlock) // Generated code
{
SDWORD size;
ASSERT( psVariable != NULL,
"scriptCodeAssignment: Invalid variable symbol pointer" );
ASSERT( psValue != NULL,
"scriptCodeAssignment: Invalid value code block pointer" );
ASSERT( psValue->pCode != NULL,
"scriptCodeAssignment: Invalid value code pointer" );
ASSERT( ppsBlock != NULL,
"scriptCodeAssignment: Invalid generated code block pointer" );
//size = psValue->size + sizeof(OPCODE);
size = psValue->size + 1; //1 - for assignment opcode
if (psVariable->storage == ST_EXTERN)
{
// Check there is a set function
if (psVariable->set == NULL)
{
scr_error("No set function for external variable");
return CE_PARSE;
}
//size += sizeof(SCRIPT_VARFUNC);
size += 1; //1 - for set func pointer
}
ALLOC_BLOCK(*ppsBlock, size);
ip = (*ppsBlock)->pCode;
/* Copy in the code for the expression */
PUT_BLOCK(ip, psValue);
FREE_BLOCK(psValue);