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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
|
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2010 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
*/
/*
* Stack.c
*
* A stack for the script interpreter.
*/
#include <stdarg.h>
#include <string.h>
#include "lib/framework/frame.h"
#include "lib/framework/string_ext.h"
#include "interpreter.h"
#include "stack.h"
#include "codeprint.h"
#include "script.h"
/* number of values in each stack chunk */
#define INIT_SIZE 30 //15
#define EXT_SIZE 10 //2
char STRSTACK[MAXSTACKLEN][MAXSTRLEN]; //simple string 'stack'
UDWORD CURSTACKSTR = 0; //Points to the top of the string stack
/* store for a 'chunk' of the stack */
typedef struct _stack_chunk
{
INTERP_VAL *aVals;
UDWORD size;
struct _stack_chunk *psNext, *psPrev;
} STACK_CHUNK;
/* The first chunk of the stack */
static STACK_CHUNK *psStackBase=NULL;
/* The current stack chunk */
static STACK_CHUNK *psCurrChunk=NULL;
/* The current free entry on the current stack chunk */
static UDWORD currEntry=0;
/* Get rid of the top value without returning it */
static inline BOOL stackRemoveTop(void);
/* Check if the stack is empty */
BOOL stackEmpty(void)
{
return (psCurrChunk->psPrev == NULL && currEntry == 0);
}
/* Allocate a new chunk for the stack */
static BOOL stackNewChunk(UDWORD size)
{
/* see if a chunk has already been allocated */
if (psCurrChunk->psNext != NULL)
{
psCurrChunk = psCurrChunk->psNext;
currEntry = 0;
}
else
{
/* Allocate a new chunk */
psCurrChunk->psNext = calloc(1, sizeof(*psCurrChunk->psNext));
if (!psCurrChunk->psNext)
{
return false;
}
psCurrChunk->psNext->aVals = calloc(size, sizeof(*psCurrChunk->psNext->aVals));
if (!psCurrChunk->psNext->aVals)
{
free(psCurrChunk->psNext);
psCurrChunk->psNext = NULL;
return false;
}
psCurrChunk->psNext->size = size;
psCurrChunk->psNext->psPrev = psCurrChunk;
psCurrChunk->psNext->psNext = NULL;
psCurrChunk = psCurrChunk->psNext;
currEntry = 0;
}
return true;
}
/* Push a value onto the stack */
BOOL stackPush(INTERP_VAL *psVal)
{
/* Store the value in the stack - psCurrChunk/currEntry always point to
valid space */
/* String support: Copy the string, otherwise the stack will operate directly on the
original string (like & opcode will actually store the result in the first
variable which would point to the original string) */
if(psVal->type == VAL_STRING) //pushing string
{
/* strings should already have memory allocated */
if(psCurrChunk->aVals[currEntry].type != VAL_STRING) //needs to have memory allocated for string
psCurrChunk->aVals[currEntry].v.sval = (char*)malloc(MAXSTRLEN);
strcpy(psCurrChunk->aVals[currEntry].v.sval, psVal->v.sval); //copy string to stack
psCurrChunk->aVals[currEntry].type = VAL_STRING;
}
else /* pushing non-string */
{
/* free stack var allocated string memory, if stack var used to be of type VAL_STRING */
if(psCurrChunk->aVals[currEntry].type == VAL_STRING)
{
free(psCurrChunk->aVals[currEntry].v.sval); //don't need it anymore
psCurrChunk->aVals[currEntry].v.sval = NULL;
}
/* copy type/data as union */
memcpy(&(psCurrChunk->aVals[currEntry]), psVal, sizeof(INTERP_VAL));
}
/* Now update psCurrChunk and currEntry */
currEntry++;
if (currEntry == psCurrChunk->size)
{
/* At the end of this chunk, need a new one */
if (!stackNewChunk(EXT_SIZE))
{
/* Out of memory */
debug(LOG_ERROR, "stackPush: Out of memory");
return false;
}
}
return true;
}
/* Pop a value off the stack */
BOOL stackPop(INTERP_VAL *psVal)
{
if (stackEmpty())
{
debug(LOG_ERROR, "stackPop: stack empty");
ASSERT( false, "stackPop: stack empty" );
return false;
}
/* move the stack pointer down one */
if (currEntry == 0)
{
/* have to move onto the previous chunk. */
psCurrChunk = psCurrChunk->psPrev;
currEntry = psCurrChunk->size -1;
}
else
{
currEntry--;
}
/* copy the entire value off the stack */
memcpy(psVal, &(psCurrChunk->aVals[currEntry]), sizeof(INTERP_VAL));
return true;
}
/* Return pointer to the top value without poping it */
BOOL stackPeekTop(INTERP_VAL **ppsVal)
{
if ((psCurrChunk->psPrev == NULL) && (currEntry == 0))
{
debug(LOG_ERROR, "stackPeekTop: stack empty");
ASSERT( false, "stackPeekTop: stack empty" );
return false;
}
if (currEntry == 0)
{
STACK_CHUNK *pPrevChunck;
/* have to move onto the previous chunk */
pPrevChunck = psCurrChunk->psPrev;
/* Return top value */
*ppsVal = &(pPrevChunck->aVals[pPrevChunck->size - 1]);
}
else
{
/* Return top value of the current chunk */
*ppsVal = &(psCurrChunk->aVals[currEntry - 1]);
}
return true;
}
/* Pop a value off the stack, checking that the type matches what is passed in */
BOOL stackPopType(INTERP_VAL *psVal)
{
INTERP_VAL *psTop;
if ((psCurrChunk->psPrev == NULL) && (currEntry == 0))
{
debug(LOG_ERROR, "stackPopType: stack empty");
ASSERT( false, "stackPopType: stack empty" );
return false;
}
/* move the stack pointer down one */
if (currEntry == 0)
{
/* have to move onto the previous chunk. */
psCurrChunk = psCurrChunk->psPrev;
currEntry = psCurrChunk->size -1;
}
else
{
currEntry--;
}
psTop = psCurrChunk->aVals + currEntry;
//String support
if(psVal->type == VAL_STRING) //If we are about to assign something to a string variable (psVal)
{
if(psTop->type != VAL_STRING) //if assigning a non-string value to a string variable, then convert the value
{
/* Check for compatible types */
switch (psTop->type)
{
case VAL_INT: /* int->string */
sprintf(psVal->v.sval, "%d", psTop->v.ival);
break;
case VAL_BOOL: /* bool->string */
sprintf(psVal->v.sval, "%d", psTop->v.bval);
break;
case VAL_FLOAT: /* float->string */
sprintf(psVal->v.sval, "%f", psTop->v.fval);
break;
default:
debug(LOG_ERROR, "stackPopType: trying to assign an incompatible data type to a string variable (type: %d)", psTop->type);
return false;
break;
}
}
else //assigning string value to a string variable - COPY string
{
strcpy(psVal->v.sval, psTop->v.sval);
}
}
else // we are about to assign something to a non-string variable (psVal)
{
if (!interpCheckEquiv(psVal->type,psTop->type))
{
debug(LOG_ERROR, "stackPopType: type mismatch: %d/%d", psVal->type, psTop->type);
ASSERT( !"type mismatch", "stackPopType: type mismatch: %d/%d", psVal->type, psTop->type);
return false;
}
/* copy the entire union off the stack */
memcpy(&(psVal->v), &(psTop->v), sizeof(psTop->v));
}
return true;
}
/* Pop a number of values off the stack checking their types
* This is used by instinct functions to get their parameters
*/
BOOL stackPopParams(unsigned int numParams, ...)
{
va_list args;
unsigned int i, index;
STACK_CHUNK *psCurr;
// Find the position of the first parameter, and set
// the stack top to it
if (numParams <= currEntry)
{
// parameters are all on current chunk
currEntry = currEntry - numParams;
psCurr = psCurrChunk;
}
else
{
// Have to work down the previous chunks to find the first param
unsigned int params = numParams - currEntry;
for(psCurr = psCurrChunk->psPrev; psCurr != NULL; psCurr = psCurr->psPrev)
{
if (params <= psCurr->size)
{
// found the first param
currEntry = psCurr->size - params;
psCurrChunk = psCurr;
break;
}
else
{
params -= psCurr->size;
}
}
}
if (!psCurr)
{
debug( LOG_ERROR, "stackPopParams: not enough parameters on stack" );
ASSERT( false, "stackPopParams: not enough parameters on stack" );
return false;
}
va_start(args, numParams);
// Get the values, checking their types
index = currEntry;
for (i = 0; i < numParams; i++)
{
INTERP_VAL *psVal = psCurr->aVals + index;
INTERP_TYPE type = va_arg(args, INTERP_TYPE);
void * pData = va_arg(args, void *);
switch (type)
{
case VAL_BOOL:
*(BOOL*)pData = psVal->v.bval;
break;
case VAL_INT:
*(int*)pData = psVal->v.ival;
break;
case VAL_FLOAT:
*(float*)pData = psVal->v.fval;
break;
case VAL_STRING:
switch (psVal->type)
{
case VAL_BOOL:
sprintf((char*)pData, "%d", psVal->v.bval);
break;
case VAL_INT:
sprintf((char*)pData, "%d", psVal->v.ival);
break;
case VAL_FLOAT:
sprintf((char*)pData, "%f", psVal->v.fval);
break;
case VAL_STRING:
strcpy((char*)pData, psVal->v.sval);
break;
default:
ASSERT(false,
"StackPopParam - wrong data type being converted to string (type: %s)",
scriptTypeToString(psVal->type));
break;
}
break;
default: // anything (including references)
if (scriptTypeIsPointer(psVal->type))
{
if (psVal->type >= VAL_REF) // if it's a reference
{
INTERP_VAL *refVal = psVal->v.oval; // oval is a pointer to INTERP_VAL in this case
/* doublecheck types */
ASSERT(interpCheckEquiv(type & ~VAL_REF, refVal->type),
"stackPopParams: type mismatch for a reference (expected %s, got %s)",
scriptTypeToString(type & ~VAL_REF), scriptTypeToString(refVal->type));
// type of provided container and type of the INTERP_VAL pointed by psVal->v.oval
*(void**)pData = &(refVal->v); /* get pointer to the union */
}
else // some pointer type
{
*(void**)pData = psVal->v.oval;
}
}
else
{
*(int*)pData = psVal->v.ival;
}
}
index++;
if (index >= psCurr->size)
{
psCurr = psCurr->psNext;
index = 0;
}
}
va_end(args);
return true;
}
/* Push a value onto the stack without using a value structure
NOTE: result->type is _not_ set yet - use 'type' instead
*/
BOOL stackPushResult(INTERP_TYPE type, INTERP_VAL *result)
{
/* assign type, wasn't done before */
result->type = type;
/* String support: Copy the string, otherwise the stack will operate directly on the
original string (like & opcode will actually store the result in the first
variable which would point to the original string) */
if(result->type == VAL_STRING) //pushing string
{
/* strings should already have memory allocated */
if(psCurrChunk->aVals[currEntry].type != VAL_STRING) //needs to have memory allocated for string
psCurrChunk->aVals[currEntry].v.sval = (char*)malloc(MAXSTRLEN);
strcpy(psCurrChunk->aVals[currEntry].v.sval, result->v.sval); //copy string to stack
psCurrChunk->aVals[currEntry].type = VAL_STRING;
}
else /* pushing non-string */
{
/* free stack var allocated string memory, if stack var used to be of type VAL_STRING */
if(psCurrChunk->aVals[currEntry].type == VAL_STRING)
{
free(psCurrChunk->aVals[currEntry].v.sval); //don't need it anymore
psCurrChunk->aVals[currEntry].v.sval = NULL;
}
/* copy type/data as union */
memcpy(&(psCurrChunk->aVals[currEntry]), result, sizeof(INTERP_VAL));
}
// Now update psCurrChunk and currEntry
currEntry++;
if (currEntry == psCurrChunk->size)
{
// At the end of this chunk, need a new one
if (!stackNewChunk(EXT_SIZE))
{
// Out of memory
return false;
}
}
return true;
}
/* Look at a value on the stack without removing it.
* index is how far down the stack to look.
* Index 0 is the top entry on the stack.
*/
BOOL stackPeek(INTERP_VAL *psVal, UDWORD index)
{
STACK_CHUNK *psCurr;
if (index < currEntry)
{
/* Looking at entry on current chunk */
memcpy(psVal, &(psCurrChunk->aVals[currEntry - index - 1]), sizeof(INTERP_VAL));
return true;
}
else
{
/* Have to work down the previous chunks to find the entry */
index -= currEntry;
for(psCurr = psCurrChunk->psPrev; psCurr != NULL; psCurr = psCurr->psPrev)
{
if (index < psCurr->size)
{
/* found the entry */
memcpy(psVal, &(psCurr->aVals[psCurr->size - 1 - index]), sizeof(INTERP_VAL));
return true;
}
else
{
index -= psCurr->size;
}
}
}
/* If we got here the index is off the bottom of the stack */
ASSERT( false, "stackPeek: index too large" );
return false;
}
/* Print the top value on the stack */
void stackPrintTop(void)
{
INTERP_VAL sVal;
if (stackPeek(&sVal, 0))
{
cpPrintVal(sVal);
}
else
{
debug( LOG_NEVER, "STACK EMPTY" );
}
}
/* Do binary operations on the top of the stack
* This effectively pops two values and pushes the result
*/
BOOL stackBinaryOp(OPCODE opcode)
{
STACK_CHUNK *psChunk;
INTERP_VAL *psV1, *psV2;
// Get the parameters
if (psCurrChunk->psPrev == NULL && currEntry < 2)
{
debug(LOG_ERROR, "stackBinaryOp: not enough entries on stack");
ASSERT( false, "stackBinaryOp: not enough entries on stack" );
return false;
}
if (currEntry > 1)
{
psV1 = psCurrChunk->aVals + currEntry - 2;
psV2 = psCurrChunk->aVals + currEntry - 1;
currEntry -= 1;
}
else if (currEntry == 1)
{
// One value is on the previous chunk
psChunk = psCurrChunk->psPrev;
psV1 = psChunk->aVals + psChunk->size - 1;
psV2 = psCurrChunk->aVals + currEntry - 1;
currEntry -= 1;
}
else
{
// both on the previous chunk, pop to the previous chunk
psCurrChunk = psCurrChunk->psPrev;
currEntry = psCurrChunk->size - 1;
psV1 = psCurrChunk->aVals + psCurrChunk->size - 2;
psV2 = psCurrChunk->aVals + psCurrChunk->size - 1;
}
if(opcode != OP_CONC) //string - Don't check if OP_CONC, since types can be mixed here
{
if (!interpCheckEquiv(psV1->type, psV2->type))
{
debug(LOG_ERROR, "stackBinaryOp: type mismatch");
ASSERT( false, "stackBinaryOp: type mismatch" );
return false;
}
/* find out if the result will be a float. Both or neither arguments are floats - should be taken care of by bison*/
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT)
{
ASSERT(psV1->type == VAL_FLOAT && psV2->type == VAL_FLOAT, "Can't implicitly convert float->int (type1: %d, type2: %d)", psV1->type , psV2->type );
}
}
// do the operation
switch (opcode)
{
case OP_ADD:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT)
{
psV1->type = VAL_FLOAT;
psV1->v.fval = psV1->v.fval + psV2->v.fval;
}
else
{
psV1->v.ival = psV1->v.ival + psV2->v.ival;
}
break;
case OP_SUB:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT)
{
psV1->type = VAL_FLOAT;
psV1->v.fval = psV1->v.fval - psV2->v.fval;
}
else
{
psV1->v.ival = psV1->v.ival - psV2->v.ival;
}
break;
case OP_MUL:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT)
{
psV1->type = VAL_FLOAT;
psV1->v.fval = psV1->v.fval * psV2->v.fval;
}
else
{
psV1->v.ival = psV1->v.ival * psV2->v.ival;
}
break;
case OP_DIV:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT)
{
if(psV2->v.fval != 0.0f)
{
psV1->type = VAL_FLOAT;
psV1->v.fval = psV1->v.fval / psV2->v.fval;
}
else
{
debug(LOG_ERROR, "stackBinaryOp: division by zero (float)");
return false;
}
}
else
{
if(psV2->v.ival != 0)
{
psV1->v.ival = psV1->v.ival / psV2->v.ival;
}
else
{
debug(LOG_ERROR, "stackBinaryOp: division by zero (integer)");
return false;
}
}
break;
case OP_AND:
psV1->v.bval = psV1->v.bval && psV2->v.bval;
break;
case OP_OR:
psV1->v.bval = psV1->v.bval || psV2->v.bval;
break;
case OP_EQUAL:
if(psV1->type == VAL_FLOAT && psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval == psV2->v.fval;
}else if(psV1->type == VAL_STRING && psV2->type == VAL_STRING){
psV1->v.bval = (strcasecmp(psV1->v.sval,psV2->v.sval) == 0); /* case-insensitive */
}else{
psV1->v.bval = psV1->v.ival == psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_NOTEQUAL:
if(psV1->type == VAL_FLOAT && psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval != psV2->v.fval;
}else if(psV1->type == VAL_STRING && psV2->type == VAL_STRING){
psV1->v.bval = (strcasecmp(psV1->v.sval,psV2->v.sval) != 0); /* case-insensitive */
}else{
psV1->v.bval = psV1->v.ival != psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_GREATEREQUAL:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval >= psV2->v.fval;
}else{
psV1->v.bval = psV1->v.ival >= psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_LESSEQUAL:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval <= psV2->v.fval;
}else{
psV1->v.bval = psV1->v.ival <= psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_GREATER:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval > psV2->v.fval;
}else{
psV1->v.bval = psV1->v.ival > psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_LESS:
if(psV1->type == VAL_FLOAT || psV2->type == VAL_FLOAT){
psV1->v.bval = psV1->v.fval < psV2->v.fval;
}else{
psV1->v.bval = psV1->v.ival < psV2->v.ival;
}
psV1->type = VAL_BOOL;
break;
case OP_CONC: //String concatenation
{
char tempstr1[MAXSTRLEN];
char tempstr2[MAXSTRLEN];
/* Check first value if it's compatible with Strings */
switch (psV1->type)
{
case VAL_INT: //first value isn't string, but can be converted to string
snprintf(tempstr1, sizeof(tempstr1), "%d", psV1->v.ival); //int->string
psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
case VAL_BOOL:
snprintf(tempstr1, sizeof(tempstr1), "%d",psV1->v.bval); //bool->string
psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
case VAL_FLOAT:
snprintf(tempstr1, sizeof(tempstr1), "%f", psV1->v.fval); //float->string
psV1->type = VAL_STRING; //Mark as string
psV1->v.sval = (char*)malloc(MAXSTRLEN); //allocate space for the string, since the result (string) of concatenation will be saved here
break;
case VAL_STRING:
sstrcpy(tempstr1, psV1->v.sval);
break;
default:
debug(LOG_ERROR, "stackBinaryOp: OP_CONC: first parameter is not compatible with Strings");
return false;
break;
}
/* Check second value if it's compatible with Strings */
switch (psV2->type)
{
case VAL_INT:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.ival); //int->string
break;
case VAL_BOOL:
snprintf(tempstr2, sizeof(tempstr2), "%d", psV2->v.bval); //bool->string
break;
case VAL_FLOAT:
snprintf(tempstr2, sizeof(tempstr2), "%f", psV2->v.fval); //float->string
break;
case VAL_STRING:
sstrcpy(tempstr2, psV2->v.sval);
break;
default:
debug(LOG_ERROR, "stackBinaryOp: OP_CONC: first parameter is not compatible with Strings");
return false;
break;
}
sstrcat(tempstr1, tempstr2);
strcpy(psV1->v.sval,tempstr1); //Assign
}
break;
default:
debug(LOG_ERROR, "stackBinaryOp: unknown opcode");
ASSERT( false, "stackBinaryOp: unknown opcode" );
return false;
break;
}
return true;
}
/* Perform a unary operation on the top of the stack
* This effectively pops a value and pushes the result
*/
BOOL stackUnaryOp(OPCODE opcode)
{
STACK_CHUNK *psChunk;
INTERP_VAL *psVal;
// Get the value
if (psCurrChunk->psPrev == NULL && currEntry == 0)
{
ASSERT( false, "stackUnaryOp: not enough entries on stack" );
return false;
}
if (currEntry > 0)
{
psVal = psCurrChunk->aVals + currEntry - 1;
}
else
{
// Value is on the previous chunk
psChunk = psCurrChunk->psPrev;
psVal = psChunk->aVals + psChunk->size - 1;
}
// Do the operation
switch (opcode)
{
case OP_INC:
switch (psVal->type)
{
case (VAL_REF | VAL_INT):
psVal = (INTERP_VAL*)psVal->v.oval; //get variable
ASSERT(psVal->type == VAL_INT, "Invalid type for increment opcode: %d", psVal->type);
psVal->v.ival++;
/* Just get rid of the variable pointer, since already increased it */
if (!stackRemoveTop())
{
debug( LOG_ERROR, "stackUnaryOpcode: OP_INC: could not pop" );
return false;
}
break;
default:
ASSERT( false, "stackUnaryOp: invalid type for OP_INC (type: %d)", psVal->type );
return false;
break;
}
break;
case OP_DEC:
switch (psVal->type)
{
case (VAL_REF | VAL_INT):
psVal = (INTERP_VAL*)psVal->v.oval; //get variable
ASSERT(psVal->type == VAL_INT, "Invalid type for decrement opcode: %d", psVal->type);
psVal->v.ival--;
/* Just get rid of the variable pointer, since already decreased it */
if (!stackRemoveTop())
{
debug( LOG_ERROR, "stackUnaryOpcode: OP_DEC: could not pop" );
return false;
}
break;
default:
ASSERT( false, "stackUnaryOp: invalid type for OP_DEC (type: %d)", psVal->type );
return false;
break;
}
break;
case OP_NEG:
switch (psVal->type)
{
case VAL_INT:
psVal->v.ival = - psVal->v.ival;
break;
case VAL_FLOAT:
psVal->v.fval = - psVal->v.fval;
break;
default:
ASSERT( false, "stackUnaryOp: invalid type for negation (type: %d)", psVal->type );
return false;
break;
}
break;
case OP_NOT:
switch (psVal->type)
{
case VAL_BOOL:
psVal->v.bval = !psVal->v.bval;
break;
default:
ASSERT( false, "stackUnaryOp: invalid type for NOT (type: %d)", psVal->type );
return false;
break;
}
break;
default:
ASSERT( false, "stackUnaryOp: unknown opcode (opcode: %d)", opcode );
return false;
break;
}
return true;
}
BOOL stackCastTop(INTERP_TYPE neededType)
{
INTERP_VAL *pTop;
ASSERT(neededType == VAL_INT || neededType == VAL_FLOAT,
"stackCast: can't cast to %d", neededType);
if (!stackPeekTop(&pTop) || pTop==NULL)
{
ASSERT(false, "castTop: failed to peek stack");
return false;
}
/* see if we can cast this data type */
switch (pTop->type)
{
case VAL_BOOL:
switch (neededType)
{
case VAL_FLOAT: /* casting from bool to float */
pTop->v.fval = (float)pTop->v.bval;
pTop->type = VAL_FLOAT;
return true;
default:
break;
}
break;
case VAL_INT:
switch (neededType)
{
case VAL_FLOAT: /* casting from int to float */
pTop->v.fval = (float)pTop->v.ival;
pTop->type = VAL_FLOAT;
return true;
default:
break;
}
break;
case VAL_FLOAT:
switch (neededType)
{
case VAL_INT: /* casting from float to int */
pTop->v.ival = (int)pTop->v.fval;
pTop->type = VAL_INT;
return true;
default:
break;
}
break;
default:
break;
}
debug(LOG_ERROR, "can't cast from %s to %s",
scriptTypeToString(pTop->type), scriptTypeToString(neededType));
return false;
}
/* Initialise the stack */
BOOL stackInitialise(void)
{
psStackBase = calloc(1, sizeof(*psStackBase));
if (psStackBase == NULL)
{
debug( LOG_FATAL, "Out of memory" );
abort();
return false;
}
psStackBase->aVals = calloc(INIT_SIZE, sizeof(*psStackBase->aVals));
if (!psStackBase->aVals)
{
debug( LOG_FATAL, "Out of memory" );
abort();
return false;
}
psStackBase->size = INIT_SIZE;
psStackBase->psPrev = NULL;
psStackBase->psNext = NULL;
psCurrChunk = psStackBase;
//string support
CURSTACKSTR = 0; //initialize string 'stack'
return true;
}
/* Shutdown the stack */
void stackShutDown(void)
{
STACK_CHUNK *psCurr, *psNext;
UDWORD i;
if ((psCurrChunk != psStackBase) && (currEntry != 0))
{
debug( LOG_NEVER, "stackShutDown: stack is not empty on shutdown" );
}
for(psCurr = psStackBase; psCurr != NULL; psCurr = psNext)
{
psNext = psCurr->psNext;
/* Free strings */
for(i=0; i< psCurr->size; i++) //go through all values on this chunk
{
if(psCurr->aVals[i].type == VAL_STRING)
{
if(psCurr->aVals[i].v.sval != NULL) //FIXME: seems to be causing problems sometimes
{
debug(LOG_WZ, "freeing '%s' ", psCurr->aVals[i].v.sval);
free(psCurr->aVals[i].v.sval);
psCurr->aVals[i].v.sval = NULL;
}
else
{
debug(LOG_SCRIPT, "stackShutDown: VAL_STRING with null pointer");
}
}
}
free(psCurr->aVals);
free(psCurr);
}
psStackBase = NULL;
}
/* Get rid of the top value without returning it */
static inline BOOL stackRemoveTop(void)
{
if ((psCurrChunk->psPrev == NULL) && (currEntry == 0))
{
debug(LOG_ERROR, "stackRemoveTop: stack empty");
ASSERT( false, "stackRemoveTop: stack empty" );
return false;
}
/* move the stack pointer down one */
if (currEntry == 0)
{
/* have to move onto the previous chunk. */
psCurrChunk = psCurrChunk->psPrev;
currEntry = psCurrChunk->size -1;
}
else
{
currEntry--;
}
return true;
}
/* Reset the stack to an empty state */
void stackReset(void)
{
ASSERT( ((psCurrChunk == psStackBase) && (currEntry == 0)),
"stackReset: stack is not empty" );
psCurrChunk = psStackBase;
currEntry = 0;
}
|