1 /* Pawn compiler - Recursive descend expresion parser
3 * Copyright (c) ITB CompuPhase, 1997-2005
5 * This software is provided "as-is", without any express or implied warranty.
6 * In no event will the authors be held liable for any damages arising from
7 * the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software in
15 * a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
21 * Version: $Id: sc3.c 3598 2006-07-04 13:44:04Z thiadmer $
25 #include <stdlib.h> /* for _MAX_PATH */
28 #include <alloc/fortify.h>
32 static int skim(int *opstr,void (*testfunc)(int),int dropval,int endval,
33 int (*hier)(value*),value *lval);
34 static void dropout(int lvalue,void (*testfunc)(int val),int exit1,value *lval);
35 static int plnge(int *opstr,int opoff,int (*hier)(value *lval),value *lval,
36 char *forcetag,int chkbitwise);
37 static int plnge1(int (*hier)(value *lval),value *lval);
38 static void plnge2(void (*oper)(void),
39 int (*hier)(value *lval),
40 value *lval1,value *lval2);
41 static cell calc(cell left,void (*oper)(),cell right,char *boolresult);
42 static int hier14(value *lval);
43 static int hier13(value *lval);
44 static int hier12(value *lval);
45 static int hier11(value *lval);
46 static int hier10(value *lval);
47 static int hier9(value *lval);
48 static int hier8(value *lval);
49 static int hier7(value *lval);
50 static int hier6(value *lval);
51 static int hier5(value *lval);
52 static int hier4(value *lval);
53 static int hier3(value *lval);
54 static int hier2(value *lval);
55 static int hier1(value *lval1);
56 static int primary(value *lval);
57 static void clear_value(value *lval);
58 static void callfunction(symbol *sym,value *lval_result,int matchparanthesis);
59 static int dbltest(void (*oper)(),value *lval1,value *lval2);
60 static int commutative(void (*oper)());
61 static int constant(value *lval);
63 static char lastsymbol[sNAMEMAX+1]; /* name of last function/variable */
64 static int bitwise_opercount; /* count of bitwise operators in an expression */
65 static int decl_heap=0;
67 /* Function addresses of binary operators for signed operations */
68 static void (*op1[17])(void) = {
69 os_mult,os_div,os_mod, /* hier3, index 0 */
70 ob_add,ob_sub, /* hier4, index 3 */
71 ob_sal,os_sar,ou_sar, /* hier5, index 5 */
72 ob_and, /* hier6, index 8 */
73 ob_xor, /* hier7, index 9 */
74 ob_or, /* hier8, index 10 */
75 os_le,os_ge,os_lt,os_gt, /* hier9, index 11 */
76 ob_eq,ob_ne, /* hier10, index 15 */
78 /* These two functions are defined because the functions inc() and dec() in
79 * SC4.C have a different prototype than the other code generation functions.
80 * The arrays for user-defined functions use the function pointers for
81 * identifying what kind of operation is requested; these functions must all
82 * have the same prototype. As inc() and dec() are special cases already, it
83 * is simplest to add two "do-nothing" functions.
85 static void user_inc(void) {}
86 static void user_dec(void) {}
89 * Searches for a binary operator a list of operators. The list is stored in
90 * the array "list". The last entry in the list should be set to 0.
92 * The index of an operator in "list" (if found) is returned in "opidx". If
93 * no operator is found, nextop() returns 0.
95 * If an operator is found in the expression, it cannot be used in a function
96 * call with omitted parantheses. Mark this...
98 * Global references: sc_allowproccall (modified)
100 static int nextop(int *opidx,int *list)
104 if (matchtoken(*list)){
105 sc_allowproccall=FALSE;
106 return TRUE; /* found! */
112 return FALSE; /* entire list scanned, nothing found */
115 SC_FUNC int check_userop(void (*oper)(void),int tag1,int tag2,int numparam,
116 value *lval,int *resulttag)
118 static char *binoperstr[] = { "*", "/", "%", "+", "-", "", "", "",
119 "", "", "", "<=", ">=", "<", ">", "==", "!=" };
120 static int binoper_savepri[] = { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
121 FALSE, FALSE, FALSE, FALSE, FALSE,
122 TRUE, TRUE, TRUE, TRUE, FALSE, FALSE };
123 static char *unoperstr[] = { "!", "-", "++", "--" };
124 static void (*unopers[])(void) = { lneg, neg, user_inc, user_dec };
125 char opername[4] = "", symbolname[sNAMEMAX+1];
126 int i,swapparams,savepri,savealt;
130 /* since user-defined operators on untagged operands are forbidden, we have
133 assert(numparam==1 || numparam==2);
134 if (tag1==0 && (numparam==1 || tag2==0))
137 savepri=savealt=FALSE;
138 /* find the name with the operator */
141 /* assignment operator: a special case */
142 strcpy(opername,"=");
143 if (lval!=NULL && (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR))
146 assert( (sizeof binoperstr / sizeof binoperstr[0]) == (sizeof op1 / sizeof op1[0]) );
147 for (i=0; i<sizeof op1 / sizeof op1[0]; i++) {
149 strcpy(opername,binoperstr[i]);
150 savepri=binoper_savepri[i];
158 /* try a select group of unary operators */
159 assert( (sizeof unoperstr / sizeof unoperstr[0]) == (sizeof unopers / sizeof unopers[0]) );
160 if (opername[0]=='\0') {
161 for (i=0; i<sizeof unopers / sizeof unopers[0]; i++) {
162 if (oper==unopers[i]) {
163 strcpy(opername,unoperstr[i]);
169 /* if not found, quit */
170 if (opername[0]=='\0')
173 /* create a symbol name from the tags and the operator name */
174 assert(numparam==1 || numparam==2);
175 operator_symname(symbolname,opername,tag1,tag2,numparam,tag2);
177 sym=findglb(symbolname,sGLOBAL);
178 if (sym==NULL /*|| (sym->usage & uDEFINE)==0*/) { /* ??? should not check uDEFINE; first pass clears these bits */
179 /* check for commutative operators */
180 if (tag1==tag2 || oper==NULL || !commutative(oper))
181 return FALSE; /* not commutative, cannot swap operands */
182 /* if arrived here, the operator is commutative and the tags are different,
183 * swap tags and try again
185 assert(numparam==2); /* commutative operator must be a binary operator */
186 operator_symname(symbolname,opername,tag2,tag1,numparam,tag1);
188 sym=findglb(symbolname,sGLOBAL);
189 if (sym==NULL /*|| (sym->usage & uDEFINE)==0*/)
193 /* check existance and the proper declaration of this function */
194 if ((sym->usage & uMISSING)!=0 || (sym->usage & uPROTOTYPED)==0) {
195 char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */
196 funcdisplayname(symname,sym->name);
197 if ((sym->usage & uMISSING)!=0)
198 error(4,symname); /* function not defined */
199 if ((sym->usage & uPROTOTYPED)==0)
200 error(71,symname); /* operator must be declared before use */
203 /* we don't want to use the redefined operator in the function that
204 * redefines the operator itself, otherwise the snippet below gives
205 * an unexpected recursion:
206 * fixed:operator+(fixed:a, fixed:b)
212 /* for increment and decrement operators, the symbol must first be loaded
213 * (and stored back afterwards)
215 if (oper==user_inc || oper==user_dec) {
218 if (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR)
219 pushreg(sPRI); /* save current address in PRI */
220 rvalue(lval); /* get the symbol's value in PRI */
223 assert(!savepri || !savealt); /* either one MAY be set, but not both */
225 /* the chained comparison operators require that the ALT register is
226 * unmodified, so we save it here; actually, we save PRI because the normal
227 * instruction sequence (without user operator) swaps PRI and ALT
229 pushreg(sPRI); /* right-hand operand is in PRI */
230 } else if (savealt) {
231 /* for the assignment operator, ALT may contain an address at which the
232 * result must be stored; this address must be preserved accross the
235 assert(lval!=NULL); /* this was checked earlier */
236 assert(lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR); /* checked earlier */
240 /* push parameters, call the function */
241 paramspassed= (oper==NULL) ? 1 : numparam;
242 switch (paramspassed) {
247 /* note that 1) a function expects that the parameters are pushed
248 * in reversed order, and 2) the left operand is in the secondary register
249 * and the right operand is in the primary register */
261 markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */
262 pushval((cell)paramspassed*sizeof(cell));
263 assert(sym->ident==iFUNCTN);
264 ffcall(sym,NULL,paramspassed);
265 if (sc_status!=statSKIP)
266 markusage(sym,uREAD); /* do not mark as "used" when this call itself is skipped */
267 if ((sym->usage & uNATIVE)!=0 && sym->x.lib!=NULL)
268 sym->x.lib->value += 1; /* increment "usage count" of the library */
269 sideeffect=TRUE; /* assume functions carry out a side-effect */
270 assert(resulttag!=NULL);
271 *resulttag=sym->tag; /* save tag of the called function */
273 if (savepri || savealt)
274 popreg(sALT); /* restore the saved PRI/ALT that into ALT */
275 if (oper==user_inc || oper==user_dec) {
277 if (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR)
278 popreg(sALT); /* restore address (in ALT) */
279 store(lval); /* store PRI in the symbol */
280 moveto1(); /* make sure PRI is restored on exit */
285 SC_FUNC int matchtag(int formaltag,int actualtag,int allowcoerce)
287 if (formaltag!=actualtag) {
288 /* if the formal tag is zero and the actual tag is not "fixed", the actual
289 * tag is "coerced" to zero
291 if (!allowcoerce || formaltag!=0 || (actualtag & FIXEDTAG)!=0)
298 * The AMX pseudo-processor has no direct support for logical (boolean)
299 * operations. These have to be done via comparing and jumping. Since we are
300 * already jumping through the code, we might as well implement an "early
301 * drop-out" evaluation (also called "short-circuit"). This conforms to
304 * expr1 || expr2 expr2 will only be evaluated if expr1 is false.
305 * expr1 && expr2 expr2 will only be evaluated if expr1 is true.
307 * expr1 || expr2 && expr3 expr2 will only be evaluated if expr1 is false
308 * and expr3 will only be evaluated if expr1 is
309 * false and expr2 is true.
311 * Code generation for the last example proceeds thus:
315 * jump to "l1" if result of expr1 not equal to 0
317 * -> operator && found; skip to higher level in hierarchy diagram
318 * jump to "l2" if result of expr2 equal to 0
320 * jump to "l2" if result of expr3 equal to 0
321 * set expression result to 1 (true)
323 * l2: set expression result to 0 (false)
325 * <- drop back to previous hierarchy level
326 * jump to "l1" if result of expr2 && expr3 not equal to 0
327 * set expression result to 0 (false)
329 * l1: set expression result to 1 (true)
334 /* Skim over terms adjoining || and && operators
335 * dropval The value of the expression after "dropping out". An "or" drops
336 * out when the left hand is TRUE, so dropval must be 1 on "or"
338 * endval The value of the expression when no expression drops out. In an
339 * "or" expression, this happens when both the left hand and the
340 * right hand are FALSE, so endval must be 0 for "or" expressions.
342 static int skim(int *opstr,void (*testfunc)(int),int dropval,int endval,
343 int (*hier)(value*),value *lval)
345 int lvalue,hits,droplab,endlab,opidx;
346 int allconst,foundop;
351 stgget(&index,&cidx); /* mark position in code generator */
352 hits=FALSE; /* no logical operators "hit" yet */
353 allconst=TRUE; /* assume all values "const" */
355 droplab=0; /* to avoid a compiler warning */
357 lvalue=plnge1(hier,lval); /* evaluate left expression */
359 allconst= allconst && (lval->ident==iCONSTEXPR);
362 /* one operator was already found */
363 if (testfunc==jmp_ne0)
364 lval->constval= lval->constval || constval;
366 lval->constval= lval->constval && constval;
368 constval=lval->constval; /* save result accumulated so far */
371 foundop=nextop(&opidx,opstr);
372 if ((foundop || hits) && (lval->ident==iARRAY || lval->ident==iREFARRAY))
373 error(33, lval->sym ? (lval->sym->name ? lval->sym->name : "-unknown") : "-unknown-"); /* array was not indexed in an expression */
376 /* this is the first operator in the list */
380 dropout(lvalue,testfunc,droplab,lval);
381 } else if (hits) { /* no (more) identical operators */
382 dropout(lvalue,testfunc,droplab,lval); /* found at least one operator! */
383 ldconst(endval,sPRI);
384 jumplabel(endlab=getlabel());
386 ldconst(dropval,sPRI);
389 lval->tag=pc_addtag("bool"); /* force tag to be "bool" */
391 lval->ident=iCONSTEXPR;
392 lval->constval=constval;
393 stgdel(index,cidx); /* scratch generated code and calculate */
395 lval->ident=iEXPRESSION;
400 return lvalue; /* none of the operators in "opstr" were found */
407 * Reads into the primary register the variable pointed to by lval if
408 * plunging through the hierarchy levels detected an lvalue. Otherwise
409 * if a constant was detected, it is loaded. If there is no constant and
410 * no lvalue, the primary register must already contain the expression
413 * After that, the compare routines "jmp_ne0" or "jmp_eq0" are called, which
414 * compare the primary register against 0, and jump to the "early drop-out"
415 * label "exit1" if the condition is true.
417 static void dropout(int lvalue,void (*testfunc)(int val),int exit1,value *lval)
421 else if (lval->ident==iCONSTEXPR)
422 ldconst(lval->constval,sPRI);
426 static void checkfunction(value *lval)
428 symbol *sym=lval->sym;
430 if (sym==NULL || (sym->ident!=iFUNCTN && sym->ident!=iREFFUNC))
431 return; /* no known symbol, or not a function result */
433 if ((sym->usage & uDEFINE)!=0) {
434 /* function is defined, can now check the return value (but make an
435 * exception for directly recursive functions)
437 if (sym!=curfunc && (sym->usage & uRETVALUE)==0) {
438 char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */
439 funcdisplayname(symname,sym->name);
440 error(209,symname); /* function should return a value */
443 /* function not yet defined, set */
444 sym->usage|=uRETVALUE; /* make sure that a future implementation of
445 * the function uses "return <value>" */
450 * Plunge to a lower level
452 static int plnge(int *opstr,int opoff,int (*hier)(value *lval),value *lval,
453 char *forcetag,int chkbitwise)
459 lvalue=plnge1(hier,lval);
460 if (nextop(&opidx,opstr)==0)
461 return lvalue; /* no operator in "opstr" found */
466 if (chkbitwise && count++>0 && bitwise_opercount!=0)
468 opidx+=opoff; /* add offset to index returned by nextop() */
469 plnge2(op1[opidx],hier,lval,&lval2);
470 if (op1[opidx]==ob_and || op1[opidx]==ob_or)
473 lval->tag=pc_addtag(forcetag);
474 } while (nextop(&opidx,opstr)); /* do */
475 return FALSE; /* result of expression is not an lvalue */
480 * Binary plunge to lower level; this is very simular to plnge, but
481 * it has special code generation sequences for chained operations.
483 static int plnge_rel(int *opstr,int opoff,int (*hier)(value *lval),value *lval)
489 /* this function should only be called for relational operators */
490 assert(op1[opoff]==os_le);
491 lvalue=plnge1(hier,lval);
492 if (nextop(&opidx,opstr)==0)
493 return lvalue; /* no operator in "opstr" found */
497 lval->boolresult=TRUE;
499 /* same check as in plnge(), but "chkbitwise" is always TRUE */
500 if (count>0 && bitwise_opercount!=0)
504 *lval=lval2; /* copy right hand expression of the previous iteration */
507 plnge2(op1[opidx],hier,lval,&lval2);
510 } while (nextop(&opidx,opstr)); /* enddo */
511 lval->constval=lval->boolresult;
512 lval->tag=pc_addtag("bool"); /* force tag to be "bool" */
513 return FALSE; /* result of expression is not an lvalue */
518 * Unary plunge to lower level
519 * Called by: skim(), plnge(), plnge2(), plnge_rel(), hier14() and hier13()
521 static int plnge1(int (*hier)(value *lval),value *lval)
526 stgget(&index,&cidx); /* mark position in code generator */
527 lvalue=(*hier)(lval);
528 if (lval->ident==iCONSTEXPR)
529 stgdel(index,cidx); /* load constant later */
535 * Binary plunge to lower level
536 * Called by: plnge(), plnge_rel(), hier14() and hier1()
538 static void plnge2(void (*oper)(void),
539 int (*hier)(value *lval),
540 value *lval1,value *lval2)
545 stgget(&index,&cidx); /* mark position in code generator */
546 if (lval1->ident==iCONSTEXPR) { /* constant on left side; it is not yet loaded */
547 if (plnge1(hier,lval2))
548 rvalue(lval2); /* load lvalue now */
549 else if (lval2->ident==iCONSTEXPR)
550 ldconst(lval2->constval<<dbltest(oper,lval2,lval1),sPRI);
551 ldconst(lval1->constval<<dbltest(oper,lval2,lval1),sALT);
552 /* ^ doubling of constants operating on integer addresses */
553 /* is restricted to "add" and "subtract" operators */
554 } else { /* non-constant on left side */
556 if (plnge1(hier,lval2))
558 if (lval2->ident==iCONSTEXPR) { /* constant on right side */
559 if (commutative(oper)) { /* test for commutative operators */
561 stgdel(index,cidx); /* scratch pushreg() and constant fetch (then
562 * fetch the constant again */
563 ldconst(lval2->constval<<dbltest(oper,lval1,lval2),sALT);
564 /* now, the primary register has the left operand and the secondary
565 * register the right operand; swap the "lval" variables so that lval1
566 * is associated with the secondary register and lval2 with the
567 * primary register, as is the "normal" case.
573 ldconst(lval2->constval<<dbltest(oper,lval1,lval2),sPRI);
574 popreg(sALT); /* pop result of left operand into secondary register */
576 } else { /* non-constants on both sides */
578 if (dbltest(oper,lval1,lval2))
579 cell2addr(); /* double primary register */
580 if (dbltest(oper,lval2,lval1))
581 cell2addr_alt(); /* double secondary register */
585 /* If used in an expression, a function should return a value.
586 * If the function has been defined, we can check this. If the
587 * function was not defined, we can set this requirement (so that
588 * a future function definition can check this bit.
590 checkfunction(lval1);
591 checkfunction(lval2);
592 if (lval1->ident==iARRAY || lval1->ident==iREFARRAY) {
593 char *ptr=(lval1->sym!=NULL) ? lval1->sym->name : "-unknown-";
594 error(33,ptr); /* array must be indexed */
595 } else if (lval2->ident==iARRAY || lval2->ident==iREFARRAY) {
596 char *ptr=(lval2->sym!=NULL) ? lval2->sym->name : "-unknown-";
597 error(33,ptr); /* array must be indexed */
599 /* ??? ^^^ should do same kind of error checking with functions */
601 /* check whether an "operator" function is defined for the tag names
602 * (a constant expression cannot be optimized in that case)
604 if (check_userop(oper,lval1->tag,lval2->tag,2,NULL,&lval1->tag)) {
605 lval1->ident=iEXPRESSION;
607 } else if (lval1->ident==iCONSTEXPR && lval2->ident==iCONSTEXPR) {
608 /* only constant expression if both constant */
609 stgdel(index,cidx); /* scratch generated code and calculate */
610 if (!matchtag(lval1->tag,lval2->tag,FALSE))
611 error(213); /* tagname mismatch */
612 lval1->constval=calc(lval1->constval,oper,lval2->constval,&lval1->boolresult);
614 if (!matchtag(lval1->tag,lval2->tag,FALSE))
615 error(213); /* tagname mismatch */
616 (*oper)(); /* do the (signed) operation */
617 lval1->ident=iEXPRESSION;
622 static cell truemodulus(cell a,cell b)
624 return (a % b + b) % b;
627 static cell calc(cell left,void (*oper)(),cell right,char *boolresult)
630 return (left | right);
631 else if (oper==ob_xor)
632 return (left ^ right);
633 else if (oper==ob_and)
634 return (left & right);
635 else if (oper==ob_eq)
636 return (left == right);
637 else if (oper==ob_ne)
638 return (left != right);
639 else if (oper==os_le)
640 return *boolresult &= (char)(left <= right), right;
641 else if (oper==os_ge)
642 return *boolresult &= (char)(left >= right), right;
643 else if (oper==os_lt)
644 return *boolresult &= (char)(left < right), right;
645 else if (oper==os_gt)
646 return *boolresult &= (char)(left > right), right;
647 else if (oper==os_sar)
648 return (left >> (int)right);
649 else if (oper==ou_sar)
650 return ((ucell)left >> (ucell)right);
651 else if (oper==ob_sal)
652 return ((ucell)left << (int)right);
653 else if (oper==ob_add)
654 return (left + right);
655 else if (oper==ob_sub)
656 return (left - right);
657 else if (oper==os_mult)
658 return (left * right);
659 else if (oper==os_div)
660 return (left - truemodulus(left,right)) / right;
661 else if (oper==os_mod)
662 return truemodulus(left,right);
664 error(29); /* invalid expression, assumed 0 (this should never occur) */
668 SC_FUNC int expression(cell *val,int *tag,symbol **symptr,int chkfuncresult)
670 int locheap=decl_heap;
675 /* scrap any arrays left on the heap */
676 assert(decl_heap>=locheap);
677 modheap((locheap-decl_heap)*sizeof(cell)); /* remove heap space, so negative delta */
680 if (lval.ident==iCONSTEXPR && val!=NULL) /* constant expression */
687 checkfunction(&lval);
691 SC_FUNC int sc_getstateid(constvalue **automaton,constvalue **state)
693 char name[sNAMEMAX+1];
698 assert(automaton!=NULL);
700 if (!(islabel=matchtoken(tLABEL)) && !needtoken(tSYMBOL))
703 tokeninfo(&val,&str);
704 assert(strlen(str)<sizeof name);
706 if (islabel || matchtoken(':')) {
707 /* token is an automaton name, add the name and get a new token */
708 *automaton=automaton_find(name);
709 /* read in the state name before checking the automaton, to keep the parser
710 * going (an "unknown automaton" error may occur when the "state" instruction
711 * precedes any state definition)
713 if (!needtoken(tSYMBOL))
715 tokeninfo(&val,&str); /* do not copy the name yet, must check automaton first */
716 if (*automaton==NULL) {
717 error(86,name); /* unknown automaton */
720 assert((*automaton)->index>0);
721 assert(strlen(str)<sizeof name);
724 *automaton=automaton_find("");
725 assert(*automaton!=NULL);
726 assert((*automaton)->index==0);
728 assert(*automaton!=NULL);
729 fsa=(*automaton)->index;
731 assert(*automaton!=NULL);
732 *state=state_find(name,fsa);
734 char *fsaname=(*automaton)->name;
737 error(87,name,fsaname); /* unknown state for automaton */
744 SC_FUNC cell array_totalsize(symbol *sym)
749 assert(sym->ident==iARRAY || sym->ident==iREFARRAY);
750 length=sym->dim.array.length;
751 if (sym->dim.array.level > 0) {
752 cell sublength=array_totalsize(finddepend(sym));
754 length=length+length*sublength;
761 static cell array_levelsize(symbol *sym,int level)
764 assert(sym->ident==iARRAY || sym->ident==iREFARRAY);
765 assert(level <= sym->dim.array.level);
766 while (level-- > 0) {
770 return sym->dim.array.length;
775 * Lowest hierarchy level (except for the , operator).
777 * Global references: sc_intest (reffered to only)
778 * sc_allowproccall (modified)
780 static int hier14(value *lval1)
783 value lval2={0},lval3={0};
788 int bwcount,leftarray;
789 cell arrayidx1[sDIMEN_MAX],arrayidx2[sDIMEN_MAX]; /* last used array indices */
792 bwcount=bitwise_opercount;
794 /* initialize the index arrays with unlikely constant indices; note that
795 * these indices will only be changed when the array is indexed with a
796 * constant, and that negative array indices are invalid (so actually, any
797 * negative value would do).
799 for (i=0; i<sDIMEN_MAX; i++)
800 arrayidx1[i]=arrayidx2[i]=(cell)(-1L << (sizeof(cell)*8-1));
801 org_arrayidx=lval1->arrayidx; /* save current pointer, to reset later */
802 if (lval1->arrayidx==NULL)
803 lval1->arrayidx=arrayidx1;
804 lvalue=plnge1(hier13,lval1);
805 if (lval1->ident!=iARRAYCELL && lval1->ident!=iARRAYCHAR)
806 lval1->arrayidx=NULL;
807 if (lval1->ident==iCONSTEXPR) /* load constant here */
808 ldconst(lval1->constval,sPRI);
844 case '=': /* simple assignment */
847 error(211); /* possibly unintended assignment */
851 bitwise_opercount=bwcount;
852 lval1->arrayidx=org_arrayidx; /* restore array index pointer */
856 /* if we get here, it was an assignment; first check a few special cases
857 * and then the general */
858 if (lval1->ident==iARRAYCHAR) {
859 /* special case, assignment to packed character in a cell is permitted */
861 } else if (lval1->ident==iARRAY || lval1->ident==iREFARRAY) {
862 /* array assignment is permitted too (with restrictions) */
864 return error(23); /* array assignment must be simple assigment */
865 assert(lval1->sym!=NULL);
866 if (array_totalsize(lval1->sym)==0)
867 return error(46,lval1->sym->name); /* unknown array size */
871 /* operand on left side of assignment must be lvalue */
873 return error(22); /* must be lvalue */
874 /* may not change "constant" parameters */
875 assert(lval1->sym!=NULL);
876 if ((lval1->sym->usage & uCONST)!=0)
877 return error(22); /* assignment to const argument */
878 sc_allowproccall=FALSE; /* may no longer use "procedure call" syntax */
880 lval3=*lval1; /* save symbol to enable storage of expresion result */
881 lval1->arrayidx=org_arrayidx; /* restore array index pointer */
882 if (lval1->ident==iARRAYCELL || lval1->ident==iARRAYCHAR
883 || lval1->ident==iARRAY || lval1->ident==iREFARRAY)
885 /* if indirect fetch: save PRI (cell address) */
890 lval2.arrayidx=arrayidx2;
891 plnge2(oper,hier14,lval1,&lval2);
892 if (lval2.ident!=iARRAYCELL && lval2.ident!=iARRAYCHAR)
896 if (!oper && lval3.arrayidx!=NULL && lval2.arrayidx!=NULL
897 && lval3.ident==lval2.ident && lval3.sym==lval2.sym)
900 assert(lval2.arrayidx==arrayidx2);
901 for (i=0; i<sDIMEN_MAX; i++)
902 same=same && (lval3.arrayidx[i]==lval2.arrayidx[i]);
904 error(226,lval3.sym->name); /* self-assignment */
909 plnge2(oper,hier14,lval1,&lval2);
911 /* if direct fetch and simple assignment: no "push"
912 * and "pop" needed -> call hier14() directly, */
914 rvalue(&lval2); /* instead of plnge2(). */
915 else if (lval2.ident==iVARIABLE)
916 lval2.ident=iEXPRESSION;/* mark as "rvalue" if it is not an "lvalue" */
917 checkfunction(&lval2);
918 /* check whether lval2 and lval3 (old lval1) refer to the same variable */
919 if (lval2.ident==iVARIABLE && lval3.ident==lval2.ident && lval3.sym==lval2.sym) {
920 assert(lval3.sym!=NULL);
921 error(226,lval3.sym->name); /* self-assignment */
925 /* Array elements are sometimes considered as sub-arrays --when the
926 * array index is an enumeration field and the enumeration size is greater
927 * than 1. If the expression on the right side of the assignment is a cell,
928 * or if an operation is in effect, this does not apply.
930 leftarray= lval3.ident==iARRAY || lval3.ident==iREFARRAY
931 || ((lval3.ident==iARRAYCELL || lval3.ident==iARRAYCHAR)
932 && lval3.constval>1 && lval3.sym->dim.array.level==0
933 && !oper && (lval2.ident==iARRAY || lval2.ident==iREFARRAY));
935 /* Left operand is an array, right operand should be an array variable
936 * of the same size and the same dimension, an array literal (of the
937 * same size) or a literal string. For single-dimensional arrays without
938 * tag for the index, it is permitted to assign a smaller array into a
939 * larger one (without warning). This is to make it easier to work with
944 int ltlength=(int)lval3.sym->dim.array.length;
945 if ((lval3.ident==iARRAYCELL || lval3.ident==iARRAYCHAR)
946 && lval3.constval>0 && lval3.sym->dim.array.level==0)
948 ltlength=(int)lval3.constval;
950 if (lval2.ident!=iARRAY && lval2.ident!=iREFARRAY
951 && (lval2.sym==NULL || lval2.constval<=0))
952 error(33,lval3.sym->name); /* array must be indexed */
953 if (lval2.sym!=NULL) {
954 if (lval2.constval==0) {
955 val=lval2.sym->dim.array.length;/* array variable */
958 if (lval2.sym->dim.array.level!=0)
959 error(28,lval2.sym->name);
961 level=lval2.sym->dim.array.level;
962 idxtag=lval2.sym->x.tags.index;
963 if (level==0 && idxtag==0 && lval3.sym->x.tags.index==0)
966 val=lval2.constval; /* literal array */
968 /* If val is negative, it means that lval2 is a literal string.
969 * The string array size may be smaller than the destination
970 * array, provided that the destination array does not have an
975 if (lval3.sym->x.tags.index==0)
979 if (lval3.sym->dim.array.level!=level)
980 return error(48); /* array dimensions must match */
981 else if (ltlength<val || exactmatch && ltlength>val || val==0)
982 return error(47); /* array sizes must match */
983 else if (lval3.ident!=iARRAYCELL && !matchtag(lval3.sym->x.tags.index,idxtag,TRUE))
984 error(229,(lval2.sym!=NULL) ? lval2.sym->name : lval3.sym->name); /* index tag mismatch */
986 /* check the sizes of all sublevels too */
987 symbol *sym1 = lval3.sym;
988 symbol *sym2 = lval2.sym;
990 assert(sym1!=NULL && sym2!=NULL);
991 /* ^^^ sym2 must be valid, because only variables can be
992 * multi-dimensional (there are no multi-dimensional literals),
993 * sym1 must be valid because it must be an lvalue
996 for (i=0; i<level; i++) {
997 sym1=finddepend(sym1);
998 sym2=finddepend(sym2);
999 assert(sym1!=NULL && sym2!=NULL);
1000 /* ^^^ both arrays have the same dimensions (this was checked
1001 * earlier) so the dependend should always be found
1003 if (sym1->dim.array.length!=sym2->dim.array.length)
1004 error(47); /* array sizes must match */
1005 else if (!matchtag(sym1->x.tags.index,sym2->x.tags.index,TRUE))
1006 error(229,sym2->name); /* index tag mismatch */
1008 /* get the total size in cells of the multi-dimensional array */
1009 val=array_totalsize(lval3.sym);
1010 assert(val>0); /* already checked */
1013 /* left operand is not an array, right operand should then not be either */
1014 if (lval2.ident==iARRAY || lval2.ident==iREFARRAY)
1015 error(6); /* must be assigned to an array */
1018 memcopy(val*sizeof(cell));
1020 check_userop(NULL,lval2.tag,lval3.tag,2,&lval3,&lval2.tag);
1021 store(&lval3); /* now, store the expression result */
1023 if (!oper && !matchtag(lval3.tag,lval2.tag,TRUE))
1024 error(213); /* tagname mismatch (if "oper", warning already given in plunge2()) */
1026 markusage(lval3.sym,uWRITTEN);
1028 bitwise_opercount=bwcount;
1029 lval1->ident=iEXPRESSION;
1030 return FALSE; /* expression result is never an lvalue */
1033 static int hier13(value *lval)
1035 int lvalue=plnge1(hier12,lval);
1036 if (matchtoken('?')) {
1037 int flab1=getlabel();
1038 int flab2=getlabel();
1044 } else if (lval->ident==iCONSTEXPR) {
1045 ldconst(lval->constval,sPRI);
1046 error(lval->constval ? 206 : 205); /* redundant test */
1048 jmp_eq0(flab1); /* go to second expression if primary register==0 */
1049 PUSHSTK_I(sc_allowtags);
1050 sc_allowtags=FALSE; /* do not allow tagnames here (colon is a special token) */
1053 if (lval->ident==iCONSTEXPR) /* load constant here */
1054 ldconst(lval->constval,sPRI);
1055 sc_allowtags=(short)POPSTK_I(); /* restore */
1061 if (lval2.ident==iCONSTEXPR) /* load constant here */
1062 ldconst(lval2.constval,sPRI);
1063 array1= (lval->ident==iARRAY || lval->ident==iREFARRAY);
1064 array2= (lval2.ident==iARRAY || lval2.ident==iREFARRAY);
1065 if (array1 && !array2) {
1066 char *ptr=(lval->sym->name!=NULL) ? lval->sym->name : "-unknown-";
1067 error(33,ptr); /* array must be indexed */
1068 } else if (!array1 && array2) {
1069 char *ptr=(lval2.sym->name!=NULL) ? lval2.sym->name : "-unknown-";
1070 error(33,ptr); /* array must be indexed */
1072 /* ??? if both are arrays, should check dimensions */
1073 if (!matchtag(lval->tag,lval2.tag,FALSE))
1074 error(213); /* tagname mismatch ('true' and 'false' expressions) */
1076 if (lval->ident==iARRAY)
1077 lval->ident=iREFARRAY; /* iARRAY becomes iREFARRAY */
1078 else if (lval->ident!=iREFARRAY)
1079 lval->ident=iEXPRESSION; /* iREFARRAY stays iREFARRAY, rest becomes iEXPRESSION */
1080 return FALSE; /* conditional expression is no lvalue */
1086 /* the order of the operators in these lists is important and must be
1087 * the same as the order of the operators in the array "op1"
1089 static int list3[] = {'*','/','%',0};
1090 static int list4[] = {'+','-',0};
1091 static int list5[] = {tSHL,tSHR,tSHRU,0};
1092 static int list6[] = {'&',0};
1093 static int list7[] = {'^',0};
1094 static int list8[] = {'|',0};
1095 static int list9[] = {tlLE,tlGE,'<','>',0};
1096 static int list10[] = {tlEQ,tlNE,0};
1097 static int list11[] = {tlAND,0};
1098 static int list12[] = {tlOR,0};
1100 static int hier12(value *lval)
1102 return skim(list12,jmp_ne0,1,0,hier11,lval);
1105 static int hier11(value *lval)
1107 return skim(list11,jmp_eq0,0,1,hier10,lval);
1110 static int hier10(value *lval)
1112 return plnge(list10,15,hier9,lval,"bool",TRUE);
1113 } /* ^ this variable is the starting index in the op1[]
1114 * array of the operators of this hierarchy level */
1116 static int hier9(value *lval)
1117 { /* <=, >=, <, > */
1118 return plnge_rel(list9,11,hier8,lval);
1121 static int hier8(value *lval)
1123 return plnge(list8,10,hier7,lval,NULL,FALSE);
1126 static int hier7(value *lval)
1128 return plnge(list7,9,hier6,lval,NULL,FALSE);
1131 static int hier6(value *lval)
1133 return plnge(list6,8,hier5,lval,NULL,FALSE);
1136 static int hier5(value *lval)
1138 return plnge(list5,5,hier4,lval,NULL,FALSE);
1141 static int hier4(value *lval)
1143 return plnge(list4,3,hier3,lval,NULL,FALSE);
1146 static int hier3(value *lval)
1148 return plnge(list3,0,hier2,lval,NULL,FALSE);
1151 static int hier2(value *lval)
1162 case tINC: /* ++lval */
1164 return error(22); /* must be lvalue */
1165 assert(lval->sym!=NULL);
1166 if ((lval->sym->usage & uCONST)!=0)
1167 return error(22); /* assignment to const argument */
1168 if (!check_userop(user_inc,lval->tag,0,1,lval,&lval->tag))
1169 inc(lval); /* increase variable first */
1170 rvalue(lval); /* and read the result into PRI */
1172 return FALSE; /* result is no longer lvalue */
1173 case tDEC: /* --lval */
1175 return error(22); /* must be lvalue */
1176 assert(lval->sym!=NULL);
1177 if ((lval->sym->usage & uCONST)!=0)
1178 return error(22); /* assignment to const argument */
1179 if (!check_userop(user_dec,lval->tag,0,1,lval,&lval->tag))
1180 dec(lval); /* decrease variable first */
1181 rvalue(lval); /* and read the result into PRI */
1183 return FALSE; /* result is no longer lvalue */
1184 case '~': /* ~ (one's complement) */
1187 invert(); /* bitwise NOT */
1188 lval->constval=~lval->constval;
1190 case '!': /* ! (logical negate) */
1193 if (check_userop(lneg,lval->tag,0,1,NULL,&lval->tag)) {
1194 lval->ident=iEXPRESSION;
1197 lneg(); /* 0 -> 1, !0 -> 0 */
1198 lval->constval=!lval->constval;
1199 lval->tag=pc_addtag("bool");
1202 case '-': /* unary - (two's complement) */
1205 /* make a special check for a constant expression with the tag of a
1206 * rational number, so that we can simple swap the sign of that constant.
1208 if (lval->ident==iCONSTEXPR && lval->tag==sc_rationaltag && sc_rationaltag!=0) {
1209 if (rational_digits==0) {
1210 #if PAWN_CELL_SIZE==32
1211 float *f = (float *)&lval->constval;
1212 #elif PAWN_CELL_SIZE==64
1213 double *f = (double *)&lval->constval;
1215 #error Unsupported cell size
1217 *f= - *f; /* this modifies lval->constval */
1219 /* the negation of a fixed point number is just an integer negation */
1220 lval->constval=-lval->constval;
1222 } else if (check_userop(neg,lval->tag,0,1,NULL,&lval->tag)) {
1223 lval->ident=iEXPRESSION;
1226 neg(); /* arithmic negation */
1227 lval->constval=-lval->constval;
1230 case tLABEL: /* tagname override */
1238 while (matchtoken('('))
1242 return error(20,st); /* illegal symbol name */
1245 sym=findglb(st,sSTATEVAR);
1246 if (sym!=NULL && sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0)
1247 sym=NULL; /* symbol is not a function, it is in the table, but not "defined" */
1249 if (!val && find_subst(st,strlen(st))!=NULL)
1252 lval->ident=iCONSTEXPR;
1253 lval->constval= val;
1254 lval->tag=pc_addtag("bool");
1255 ldconst(lval->constval,sPRI);
1256 while (paranthese--)
1261 while (matchtoken('('))
1265 return error(20,st); /* illegal symbol name */
1268 sym=findglb(st,sSTATEVAR);
1270 return error(17,st); /* undefined symbol */
1271 if (sym->ident==iCONSTEXPR)
1272 error(39); /* constant symbol has no size */
1273 else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC)
1274 error(72); /* "function" symbol has no size */
1275 else if ((sym->usage & uDEFINE)==0)
1276 return error(17,st); /* undefined symbol (symbol is in the table, but it is "used" only) */
1278 lval->ident=iCONSTEXPR;
1279 lval->constval=1; /* preset */
1280 if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1282 symbol *idxsym=NULL;
1284 for (level=0; matchtoken('['); level++) {
1286 if (subsym!=NULL && level==subsym->dim.array.level && matchtoken(tSYMBOL)) {
1288 int cmptag=subsym->x.tags.index;
1289 tokeninfo(&val,&idxname);
1290 if ((idxsym=findconst(idxname,&cmptag))==NULL)
1291 error(80,idxname); /* unknown symbol, or non-constant */
1293 error(91,idxname); /* ambiguous constant */
1297 subsym=finddepend(subsym);
1299 if (level>sym->dim.array.level+1)
1300 error(28,sym->name); /* invalid subscript */
1301 else if (level==sym->dim.array.level+1)
1302 lval->constval= (idxsym!=NULL && idxsym->dim.array.length>0) ? idxsym->dim.array.length : 1;
1304 lval->constval=array_levelsize(sym,level);
1305 if (lval->constval==0 && strchr((char *)lptr,PREPROC_TERM)==NULL)
1306 error(224,st); /* indeterminate array size in "sizeof" expression */
1308 ldconst(lval->constval,sPRI);
1309 while (paranthese--)
1314 while (matchtoken('('))
1317 if (tok!=tSYMBOL && tok!=tLABEL)
1318 return error(20,st); /* illegal symbol name */
1320 constvalue *tagsym=find_constval(&tagname_tab,st,0);
1321 tag=(int)((tagsym!=NULL) ? tagsym->value : 0);
1325 sym=findglb(st,sSTATEVAR);
1327 return error(17,st); /* undefined symbol */
1328 if ((sym->usage & uDEFINE)==0)
1329 return error(17,st); /* undefined symbol (symbol is in the table, but it is "used" only) */
1332 if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1334 symbol *idxsym=NULL;
1336 for (level=0; matchtoken('['); level++) {
1338 if (subsym!=NULL && level==subsym->dim.array.level && matchtoken(tSYMBOL)) {
1340 int cmptag=subsym->x.tags.index;
1341 tokeninfo(&val,&idxname);
1342 if ((idxsym=findconst(idxname,&cmptag))==NULL)
1343 error(80,idxname); /* unknown symbol, or non-constant */
1345 error(91,idxname); /* ambiguous constant */
1349 subsym=finddepend(subsym);
1351 if (level>sym->dim.array.level+1)
1352 error(28,sym->name); /* invalid subscript */
1353 else if (level==sym->dim.array.level+1 && idxsym!=NULL)
1354 tag= idxsym->x.tags.index;
1358 lval->ident=iCONSTEXPR;
1359 lval->constval=tag | PUBLICTAG;
1360 ldconst(lval->constval,sPRI);
1361 while (paranthese--)
1365 constvalue *automaton;
1367 if (sc_getstateid(&automaton,&state)) {
1368 assert(automaton!=NULL);
1369 assert(automaton->index==0 && automaton->name[0]=='\0' || automaton->index>0);
1370 loadreg(automaton->value,sALT);
1371 assert(state!=NULL);
1372 ldconst(state->value,sPRI);
1375 lval->ident=iEXPRESSION;
1376 lval->tag=pc_addtag("bool");
1383 /* check for postfix operators */
1384 if (matchtoken(';')) {
1385 /* Found a ';', do not look further for postfix operators */
1386 lexpush(); /* push ';' back after successful match */
1388 } else if (matchtoken(tTERM)) {
1389 /* Found a newline that ends a statement (this is the case when
1390 * semicolons are optional). Note that an explicit semicolon was
1391 * handled above. This case is similar, except that the token must
1392 * not be pushed back.
1398 case tINC: /* lval++ */
1400 return error(22); /* must be lvalue */
1401 assert(lval->sym!=NULL);
1402 if ((lval->sym->usage & uCONST)!=0)
1403 return error(22); /* assignment to const argument */
1404 /* on incrementing array cells, the address in PRI must be saved for
1405 * incremening the value, whereas the current value must be in PRI
1408 saveresult= (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR);
1410 pushreg(sPRI); /* save address in PRI */
1411 rvalue(lval); /* read current value into PRI */
1413 swap1(); /* save PRI on the stack, restore address in PRI */
1414 if (!check_userop(user_inc,lval->tag,0,1,lval,&lval->tag))
1415 inc(lval); /* increase variable afterwards */
1417 popreg(sPRI); /* restore PRI (result of rvalue()) */
1419 return FALSE; /* result is no longer lvalue */
1420 case tDEC: /* lval-- */
1422 return error(22); /* must be lvalue */
1423 assert(lval->sym!=NULL);
1424 if ((lval->sym->usage & uCONST)!=0)
1425 return error(22); /* assignment to const argument */
1426 saveresult= (lval->ident==iARRAYCELL || lval->ident==iARRAYCHAR);
1428 pushreg(sPRI); /* save address in PRI */
1429 rvalue(lval); /* read current value into PRI */
1431 swap1(); /* save PRI on the stack, restore address in PRI */
1432 if (!check_userop(user_dec,lval->tag,0,1,lval,&lval->tag))
1433 dec(lval); /* decrease variable afterwards */
1435 popreg(sPRI); /* restore PRI (result of rvalue()) */
1438 case tCHAR: /* char (compute required # of cells */
1439 if (lval->ident==iCONSTEXPR) {
1440 lval->constval *= sCHARBITS/8; /* from char to bytes */
1441 lval->constval = (lval->constval + sizeof(cell)-1) / sizeof(cell);
1444 rvalue(lval); /* fetch value if not already in PRI */
1445 char2addr(); /* from characters to bytes */
1446 addconst(sizeof(cell)-1); /* make sure the value is rounded up */
1447 addr2cell(); /* truncate to number of cells */
1460 * The highest hierarchy level: it looks for pointer and array indices
1461 * and function calls.
1462 * Generates code to fetch a pointer value if it is indexed and code to
1463 * add to the pointer value or the array address (the address is already
1464 * read at primary()). It also generates code to fetch a function address
1465 * if that hasn't already been done at primary() (check lval[4]) and calls
1466 * callfunction() to call the function.
1468 static int hier1(value *lval1)
1470 int lvalue,index,tok,symtok;
1476 symbol dummysymbol,*cursym; /* for changing the index tags in case of enumerated pseudo-arrays */
1478 lvalue=primary(lval1);
1479 symtok=tokeninfo(&val,&st); /* get token read by primary() */
1483 if (matchtoken('[') || matchtoken('{') || matchtoken('(')) {
1484 tok=tokeninfo(&val,&st); /* get token read by matchtoken() */
1485 if (sym==NULL && symtok!=tSYMBOL) {
1486 /* we do not have a valid symbol and we appear not to have read a valid
1487 * symbol name (so it is unlikely that we would have read a name of an
1488 * undefined symbol) */
1489 error(29); /* expression error, assumed 0 */
1490 lexpush(); /* analyse '(', '{' or '[' again later */
1493 if (tok=='[' || tok=='{') { /* subscript */
1494 close = (char)((tok=='[') ? ']' : '}');
1495 if (sym==NULL) { /* sym==NULL if lval is a constant or a literal */
1496 error(28,"<no variable>"); /* cannot subscript */
1499 } else if (sym->ident!=iARRAY && sym->ident!=iREFARRAY){
1500 error(28,sym->name); /* cannot subscript, variable is not an array */
1503 } else if (sym->dim.array.level>0 && close!=']') {
1504 error(51); /* invalid subscript, must use [ ] */
1508 /* set the tag to match (enumeration fields as indices) */
1509 lval2.cmptag=sym->x.tags.index;
1510 stgget(&index,&cidx); /* mark position in code generator */
1511 pushreg(sPRI); /* save base address of the array */
1512 if (hier14(&lval2)) /* create expression for the array index */
1514 if (lval2.ident==iARRAY || lval2.ident==iREFARRAY)
1515 error(33,lval2.sym->name); /* array must be indexed */
1517 if (!matchtag(sym->x.tags.index,lval2.tag,TRUE))
1519 if (lval2.ident==iCONSTEXPR) { /* constant expression */
1520 stgdel(index,cidx); /* scratch generated code */
1521 if (lval1->arrayidx!=NULL) { /* keep constant index, for checking */
1522 assert(sym->dim.array.level>=0 && sym->dim.array.level<sDIMEN_MAX);
1523 lval1->arrayidx[sym->dim.array.level]=lval2.constval;
1526 /* normal array index */
1527 if (lval2.constval<0 || sym->dim.array.length!=0 && sym->dim.array.length<=lval2.constval)
1528 error(32,sym->name); /* array index out of bounds */
1529 if (lval2.constval!=0) {
1530 /* don't add offsets for zero subscripts */
1531 #if PAWN_CELL_SIZE==16
1532 ldconst(lval2.constval<<1,sALT);
1533 #elif PAWN_CELL_SIZE==32
1534 ldconst(lval2.constval<<2,sALT);
1535 #elif PAWN_CELL_SIZE==64
1536 ldconst(lval2.constval<<3,sALT);
1538 #error Unsupported cell size
1543 /* character index */
1544 if (lval2.constval<0 || sym->dim.array.length!=0
1545 && sym->dim.array.length*((8*sizeof(cell))/sCHARBITS)<=(ucell)lval2.constval)
1546 error(32,sym->name); /* array index out of bounds */
1547 if (lval2.constval!=0) {
1548 /* don't add offsets for zero subscripts */
1550 ldconst(lval2.constval<<1,sALT);/* 16-bit character */
1552 ldconst(lval2.constval,sALT); /* 8-bit character */
1556 charalign(); /* align character index into array */
1558 /* if the array index is a field from an enumeration, get the tag name
1559 * from the field and save the size of the field too.
1561 assert(lval2.sym==NULL || lval2.sym->dim.array.level==0);
1562 if (lval2.sym!=NULL && lval2.sym->dim.array.length>0 && sym->dim.array.level==0) {
1563 lval1->tag=lval2.sym->x.tags.index;
1564 lval1->constval=lval2.sym->dim.array.length;
1567 /* array index is not constant */
1568 lval1->arrayidx=NULL; /* reset, so won't be checked */
1570 if (sym->dim.array.length!=0)
1571 ffbounds(sym->dim.array.length-1); /* run time check for array bounds */
1572 cell2addr(); /* normal array index */
1574 if (sym->dim.array.length!=0)
1575 ffbounds(sym->dim.array.length*(32/sCHARBITS)-1);
1576 char2addr(); /* character array index */
1579 ob_add(); /* base address was popped into secondary register */
1581 charalign(); /* align character index into array */
1583 /* the indexed item may be another array (multi-dimensional arrays) */
1584 assert(cursym==sym && sym!=NULL); /* should still be set */
1585 if (sym->dim.array.level>0) {
1586 assert(close==']'); /* checked earlier */
1587 assert(cursym==lval1->sym);
1588 /* read the offset to the subarray and add it to the current address */
1589 lval1->ident=iARRAYCELL;
1590 pushreg(sPRI); /* the optimizer makes this to a MOVE.alt */
1594 /* adjust the "value" structure and find the referenced array */
1595 lval1->ident=iREFARRAY;
1596 lval1->sym=finddepend(sym);
1597 assert(lval1->sym!=NULL);
1598 assert(lval1->sym->dim.array.level==sym->dim.array.level-1);
1600 /* try to parse subsequent array indices */
1601 lvalue=FALSE; /* for now, a iREFARRAY is no lvalue */
1604 assert(sym->dim.array.level==0);
1605 /* set type to fetch... INDIRECTLY */
1606 lval1->ident= (char)((close==']') ? iARRAYCELL : iARRAYCHAR);
1607 /* if the array index is a field from an enumeration, get the tag name
1608 * from the field and save the size of the field too. Otherwise, the
1609 * tag is the one from the array symbol.
1611 if (lval2.ident==iCONSTEXPR && lval2.sym!=NULL
1612 && lval2.sym->dim.array.length>0 && sym->dim.array.level==0)
1614 lval1->tag=lval2.sym->x.tags.index;
1615 lval1->constval=lval2.sym->dim.array.length;
1616 if (lval2.tag==sym->x.tags.index && lval1->constval>1 && matchtoken('[')) {
1617 /* an array indexed with an enumeration field may be considered a sub-array */
1619 lvalue=FALSE; /* for now, a iREFARRAY is no lvalue */
1620 lval1->ident=iREFARRAY;
1621 /* initialize a dummy symbol, which is a copy of the current symbol,
1622 * but with an adjusted index tag
1626 /* get the tag of the root of the enumeration */
1627 assert(lval2.sym!=NULL);
1628 dummysymbol.x.tags.index=lval2.sym->x.tags.field;
1629 dummysymbol.dim.array.length=lval2.sym->dim.array.length;
1630 cursym=&dummysymbol;
1636 if (cursym!=&dummysymbol)
1637 lval1->tag=sym->tag;
1640 /* a cell in an array is an lvalue, a character in an array is not
1641 * always a *valid* lvalue */
1643 } else { /* tok=='(' -> function(...) */
1646 || (sym->ident!=iFUNCTN && sym->ident!=iREFFUNC))
1648 if (sym==NULL && sc_status==statFIRST) {
1649 /* could be a "use before declaration"; in that case, create a stub
1650 * function so that the usage can be marked.
1652 sym=fetchfunc(lastsymbol,0);
1654 error(103); /* insufficient memory */
1655 markusage(sym,uREAD);
1657 return error(12); /* invalid function call */
1659 } else if ((sym->usage & uMISSING)!=0) {
1660 char symname[2*sNAMEMAX+16]; /* allow space for user defined operators */
1661 funcdisplayname(symname,sym->name);
1662 error(4,symname); /* function not defined */
1664 callfunction(sym,lval1,TRUE);
1665 return FALSE; /* result of function call is no lvalue */
1668 if (sym!=NULL && lval1->ident==iFUNCTN) {
1669 assert(sym->ident==iFUNCTN);
1670 if (sc_allowproccall) {
1671 callfunction(sym,lval1,FALSE);
1674 for (sym=glbtab.next; sym!=NULL; sym=sym->next) {
1675 if (sym->ident==iFUNCTN
1676 && (sym->usage & uPUBLIC)!=0 && (sym->usage & uDEFINE)!=0)
1678 assert(sym->vclass==sGLOBAL);
1679 if (strcmp(sym->name, lval1->sym->name)==0) {
1688 lval1->ident=iCONSTEXPR;
1690 lval1->tag=pc_addtag("Function");
1692 error(76); /* invalid function call, or syntax error */
1702 * Returns 1 if the operand is an lvalue (everything except arrays, functions
1703 * constants and -of course- errors).
1704 * Generates code to fetch the address of arrays. Code for constants is
1705 * already generated by constant().
1706 * This routine first clears the entire lval array (all fields are set to 0).
1708 * Global references: sc_intest (may be altered, but restored upon termination)
1710 static int primary(value *lval)
1717 if (matchtoken('(')){ /* sub-expression - (expression,...) */
1718 PUSHSTK_I(sc_intest);
1719 PUSHSTK_I(sc_allowtags);
1721 sc_intest=FALSE; /* no longer in "test" expression */
1722 sc_allowtags=TRUE; /* allow tagnames to be used in parenthesized expressions */
1723 sc_allowproccall=FALSE;
1725 lvalue=hier14(lval);
1726 while (matchtoken(','));
1728 lexclr(FALSE); /* clear lex() push-back, it should have been
1729 * cleared already by needtoken() */
1730 sc_allowtags=(short)POPSTK_I();
1731 sc_intest=(short)POPSTK_I();
1735 clear_value(lval); /* clear lval */
1738 /* lastsymbol is char[sNAMEMAX+1], lex() should have truncated any symbol
1739 * to sNAMEMAX significant characters */
1740 assert(strlen(st)<sizeof lastsymbol);
1741 strcpy(lastsymbol,st);
1743 if (tok==tSYMBOL && !findconst(st,NULL)) {
1744 /* first look for a local variable */
1745 if ((sym=findloc(st))!=0) {
1746 if (sym->ident==iLABEL) {
1747 error(29); /* expression error, assumed 0 */
1748 ldconst(0,sPRI); /* load 0 */
1749 return FALSE; /* return 0 for labels (expression error) */
1752 lval->ident=sym->ident;
1754 if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1755 address(sym,sPRI); /* get starting address in primary register */
1756 return FALSE; /* return 0 for array (not lvalue) */
1758 return TRUE; /* return 1 if lvalue (not label or array) */
1761 /* now try a global variable */
1762 if ((sym=findglb(st,sSTATEVAR))!=0) {
1763 if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
1764 /* if the function is only in the table because it was inserted as a
1765 * stub in the first pass (i.e. it was "used" but never declared or
1766 * implemented, issue an error
1768 if ((sym->usage & uPROTOTYPED)==0)
1771 if ((sym->usage & uDEFINE)==0)
1774 lval->ident=sym->ident;
1776 if (sym->ident==iARRAY || sym->ident==iREFARRAY) {
1777 address(sym,sPRI); /* get starting address in primary register */
1778 return FALSE; /* return 0 for array (not lvalue) */
1780 return TRUE; /* return 1 if lvalue (not function or array) */
1784 if (!sc_allowproccall)
1785 return error(17,st); /* undefined symbol */
1786 /* an unknown symbol, but used in a way compatible with the "procedure
1787 * call" syntax. So assume that the symbol refers to a function.
1789 assert(sc_status==statFIRST);
1790 sym=fetchfunc(st,0);
1792 error(103); /* insufficient memory */
1795 assert(sym->ident==iFUNCTN || sym->ident==iREFFUNC);
1797 lval->ident=sym->ident;
1799 return FALSE; /* return 0 for function (not an lvalue) */
1801 lexpush(); /* push the token, it is analyzed by constant() */
1802 if (constant(lval)==0) {
1803 error(29); /* expression error, assumed 0 */
1804 ldconst(0,sPRI); /* load 0 */
1806 return FALSE; /* return 0 for constants (or errors) */
1809 static void clear_value(value *lval)
1815 lval->boolresult=FALSE;
1816 /* do not clear lval->arrayidx, it is preset in hier14() */
1817 /* do not clear lval->cmptag */
1820 static void setdefarray(cell *string,cell size,cell array_sz,cell *dataaddr,int fconst)
1822 /* The routine must copy the default array data onto the heap, as to avoid
1823 * that a function can change the default value. An optimization is that
1824 * the default array data is "dumped" into the data segment only once (on the
1827 assert(string!=NULL);
1829 /* check whether to dump the default array */
1830 assert(dataaddr!=NULL);
1831 if (sc_status==statWRITE && *dataaddr<0) {
1833 *dataaddr=(litidx+glb_declared)*sizeof(cell);
1834 for (i=0; i<size; i++)
1838 /* if the function is known not to modify the array (meaning that it also
1839 * does not modify the default value), directly pass the address of the
1840 * array in the data segment.
1843 ldconst(*dataaddr,sPRI);
1845 /* Generate the code:
1846 * CONST.pri dataaddr ;address of the default array data
1847 * HEAP array_sz*sizeof(cell) ;heap address in ALT
1848 * MOVS size*sizeof(cell) ;copy data from PRI to ALT
1849 * MOVE.PRI ;PRI = address on the heap
1851 ldconst(*dataaddr,sPRI);
1852 /* "array_sz" is the size of the argument (the value between the brackets
1853 * in the declaration), "size" is the size of the default array data.
1855 assert(array_sz>=size);
1856 modheap((int)array_sz*sizeof(cell));
1857 /* ??? should perhaps fill with zeros first */
1858 memcopy(size*sizeof(cell));
1863 static int findnamedarg(arginfo *arg,char *name)
1867 for (i=0; arg[i].ident!=0 && arg[i].ident!=iVARARGS; i++)
1868 if (strcmp(arg[i].name,name)==0)
1873 static int checktag(int tags[],int numtags,int exprtag)
1879 for (i=0; i<numtags; i++)
1880 if (matchtag(tags[i],exprtag,TRUE))
1881 return TRUE; /* matching tag */
1882 return FALSE; /* no tag matched */
1893 * Generates code to call a function. This routine handles default arguments
1894 * and positional as well as named parameters.
1896 static void callfunction(symbol *sym,value *lval_result,int matchparanthesis)
1898 static long nest_stkusage=0L;
1899 static int nesting=0;
1902 int argpos; /* index in the output stream (argpos==nargs if positional parameters) */
1903 int argidx=0; /* index in "arginfo" list */
1904 int nargs=0; /* number of arguments */
1906 int namedparams=FALSE;
1909 char arglist[sMAXARGS];
1910 constvalue arrayszlst = { NULL, "", 0, 0};/* array size list starts empty */
1911 constvalue taglst = { NULL, "", 0, 0}; /* tag list starts empty */
1917 lval_result->ident=iEXPRESSION; /* preset, may be changed later */
1918 lval_result->constval=0;
1919 lval_result->tag=sym->tag;
1920 /* check whether this is a function that returns an array */
1921 symret=finddepend(sym);
1922 assert(symret==NULL || symret->ident==iREFARRAY);
1925 /* allocate space on the heap for the array, and pass the pointer to the
1926 * reserved memory block as a hidden parameter
1928 retsize=(int)array_totalsize(symret);
1930 modheap(retsize*sizeof(cell));/* address is in ALT */
1931 pushreg(sALT); /* pass ALT as the last (hidden) parameter */
1933 /* also mark the ident of the result as "array" */
1934 lval_result->ident=iREFARRAY;
1935 lval_result->sym=symret;
1940 assert(nest_stkusage>=0);
1943 assert(nest_stkusage==0);
1945 sc_allowproccall=FALSE; /* parameters may not use procedure call syntax */
1947 if ((sym->flags & flgDEPRECATED)!=0) {
1948 char *ptr= (sym->documentation!=NULL) ? sym->documentation : "";
1949 error(234,sym->name,ptr); /* deprecated (probably a native function) */
1952 /* run through the arguments */
1953 arg=sym->dim.arglist;
1955 stgmark(sSTARTREORDER);
1956 memset(arglist,ARG_UNHANDLED,sizeof arglist);
1957 if (matchparanthesis) {
1958 /* Opening brace was already parsed, if closing brace follows, this
1959 * call passes no parameters.
1961 close=matchtoken(')');
1963 /* When we find an end of line here, it may be a function call passing
1964 * no parameters, or it may be that the first parameter is on a line
1965 * below. But as a parameter can be anything, this is difficult to check.
1966 * The only simple check that we have is the use of "named parameters".
1968 close=matchtoken(tTERM);
1970 close=!matchtoken('.');
1972 lexpush(); /* reset the '.' */
1977 if (matchtoken('.')) {
1979 if (needtoken(tSYMBOL))
1980 tokeninfo(&lexval,&lexstr);
1983 argpos=findnamedarg(arg,lexstr);
1985 error(17,lexstr); /* undefined symbol */
1986 break; /* exit loop, argpos is invalid */
1992 error(44); /* positional parameters must precede named parameters */
1995 /* the number of arguments this was already checked at the declaration
1996 * of the function; check it again for functions with a variable
1999 if (argpos>=sMAXARGS)
2000 error(45); /* too many function arguments */
2001 stgmark((char)(sEXPRSTART+argpos));/* mark beginning of new expression in stage */
2002 if (arglist[argpos]!=ARG_UNHANDLED)
2003 error(58); /* argument already set */
2004 if (matchtoken('_')) {
2005 arglist[argpos]=ARG_IGNORED; /* flag argument as "present, but ignored" */
2006 if (arg[argidx].ident==0 || arg[argidx].ident==iVARARGS) {
2007 error(92); /* argument count mismatch */
2008 } else if (!arg[argidx].hasdefault) {
2009 error(34,nargs+1); /* argument has no default value */
2011 if (arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS)
2013 /* The rest of the code to handle default values is at the bottom
2014 * of this routine where default values for unspecified parameters
2015 * are (also) handled. Note that above, the argument is flagged as
2019 arglist[argpos]=ARG_DONE; /* flag argument as "present" */
2020 if (arg[argidx].numtags==1) /* set the expected tag, if any */
2021 lval.cmptag=arg[argidx].tags[0];
2022 lvalue=hier14(&lval);
2023 assert(sc_status==statFIRST || arg[argidx].tags!=NULL);
2024 switch (arg[argidx].ident) {
2026 error(92); /* argument count mismatch */
2029 /* always pass by reference */
2030 if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) {
2031 assert(lval.sym!=NULL);
2032 if ((lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0) {
2033 /* treat a "const" variable passed to a function with a non-const
2034 * "variable argument list" as a constant here */
2036 error(22); /* need lvalue */
2038 rvalue(&lval); /* get value in PRI */
2039 setheap_pri(); /* address of the value on the heap in PRI */
2043 } else if (lvalue) {
2044 address(lval.sym,sPRI);
2046 setheap_pri(); /* address of the value on the heap in PRI */
2050 } else if (lval.ident==iCONSTEXPR || lval.ident==iEXPRESSION
2051 || lval.ident==iARRAYCHAR)
2053 /* fetch value if needed */
2054 if (lval.ident==iARRAYCHAR)
2056 /* allocate a cell on the heap and store the
2057 * value (already in PRI) there */
2058 setheap_pri(); /* address of the value on the heap in PRI */
2062 /* ??? handle const array passed by reference */
2063 /* otherwise, the address is already in PRI */
2065 markusage(lval.sym,uWRITTEN);
2066 if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag))
2069 append_constval(&taglst,arg[argidx].name,lval.tag,0);
2072 if (lval.ident==iLABEL || lval.ident==iFUNCTN || lval.ident==iREFFUNC
2073 || lval.ident==iARRAY || lval.ident==iREFARRAY)
2074 error(35,argidx+1); /* argument type mismatch */
2076 rvalue(&lval); /* get value (direct or indirect) */
2077 /* otherwise, the expression result is already in PRI */
2078 assert(arg[argidx].numtags>0);
2079 check_userop(NULL,lval.tag,arg[argidx].tags[0],2,NULL,&lval.tag);
2080 if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag))
2083 append_constval(&taglst,arg[argidx].name,lval.tag,0);
2084 argidx++; /* argument done */
2087 if (!lvalue || lval.ident==iARRAYCHAR)
2088 error(35,argidx+1); /* argument type mismatch */
2089 if (lval.sym!=NULL && (lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0)
2090 error(35,argidx+1); /* argument type mismatch */
2091 if (lval.ident==iVARIABLE || lval.ident==iREFERENCE) {
2093 assert(lval.sym!=NULL);
2094 address(lval.sym,sPRI);
2096 setheap_pri(); /* address of the value on the heap in PRI */
2101 /* otherwise, the address is already in PRI */
2102 if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag))
2105 append_constval(&taglst,arg[argidx].name,lval.tag,0);
2106 argidx++; /* argument done */
2108 markusage(lval.sym,uWRITTEN);
2111 if (lval.ident!=iARRAY && lval.ident!=iREFARRAY
2112 && lval.ident!=iARRAYCELL)
2114 error(35,argidx+1); /* argument type mismatch */
2117 if (lval.sym!=NULL && (lval.sym->usage & uCONST)!=0 && (arg[argidx].usage & uCONST)==0)
2118 error(35,argidx+1); /* argument type mismatch */
2119 /* Verify that the dimensions match with those in arg[argidx].
2120 * A literal array always has a single dimension.
2121 * An iARRAYCELL parameter is also assumed to have a single dimension.
2123 if (lval.sym==NULL || lval.ident==iARRAYCELL) {
2124 if (arg[argidx].numdim!=1) {
2125 error(48); /* array dimensions must match */
2126 } else if (arg[argidx].dim[0]!=0) {
2127 assert(arg[argidx].dim[0]>0);
2128 if (lval.ident==iARRAYCELL) {
2129 error(47); /* array sizes must match */
2131 assert(lval.constval!=0); /* literal array must have a size */
2132 /* A literal array must have exactly the same size as the
2133 * function argument; a literal string may be smaller than
2134 * the function argument.
2136 if (lval.constval>0 && arg[argidx].dim[0]!=lval.constval
2137 || lval.constval<0 && arg[argidx].dim[0] < -lval.constval)
2138 error(47); /* array sizes must match */
2141 if (lval.ident!=iARRAYCELL) {
2142 /* save array size, for default values with uSIZEOF flag */
2143 cell array_sz=lval.constval;
2144 assert(array_sz!=0);/* literal array must have a size */
2146 array_sz= -array_sz;
2147 append_constval(&arrayszlst,arg[argidx].name,array_sz,0);
2150 symbol *sym=lval.sym;
2153 if (sym->dim.array.level+1!=arg[argidx].numdim)
2154 error(48); /* array dimensions must match */
2155 /* the lengths for all dimensions must match, unless the dimension
2156 * length was defined at zero (which means "undefined")
2158 while (sym->dim.array.level>0) {
2159 assert(level<sDIMEN_MAX);
2160 if (arg[argidx].dim[level]!=0 && sym->dim.array.length!=arg[argidx].dim[level])
2161 error(47); /* array sizes must match */
2162 else if (!matchtag(arg[argidx].idxtag[level],sym->x.tags.index,TRUE))
2163 error(229,sym->name); /* index tag mismatch */
2164 append_constval(&arrayszlst,arg[argidx].name,sym->dim.array.length,level);
2165 sym=finddepend(sym);
2169 /* the last dimension is checked too, again, unless it is zero */
2170 assert(level<sDIMEN_MAX);
2172 if (arg[argidx].dim[level]!=0 && sym->dim.array.length!=arg[argidx].dim[level])
2173 error(47); /* array sizes must match */
2174 else if (!matchtag(arg[argidx].idxtag[level],sym->x.tags.index,TRUE))
2175 error(229,sym->name); /* index tag mismatch */
2176 append_constval(&arrayszlst,arg[argidx].name,sym->dim.array.length,level);
2178 /* address already in PRI */
2179 if (!checktag(arg[argidx].tags,arg[argidx].numtags,lval.tag))
2182 append_constval(&taglst,arg[argidx].name,lval.tag,0);
2183 // ??? set uWRITTEN?
2184 argidx++; /* argument done */
2187 pushreg(sPRI); /* store the function argument on the stack */
2188 markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */
2191 assert(arglist[argpos]!=ARG_UNHANDLED);
2193 if (matchparanthesis) {
2194 close=matchtoken(')');
2195 if (!close) /* if not paranthese... */
2196 if (!needtoken(',')) /* ...should be comma... */
2197 break; /* ...but abort loop if neither */
2199 close=!matchtoken(',');
2200 if (close) { /* if not comma... */
2201 if (needtoken(tTERM)==1)/* ...must be end of statement */
2202 lexpush(); /* push again, because end of statement is analised later */
2205 } while (!close && freading && !matchtoken(tENDEXPR)); /* do */
2207 /* check remaining function arguments (they may have default values) */
2208 for (argidx=0; arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS; argidx++) {
2209 if (arglist[argidx]==ARG_DONE)
2210 continue; /* already seen and handled this argument */
2211 /* in this first stage, we also skip the arguments with uSIZEOF and uTAGOF;
2212 * these are handled last
2214 if ((arg[argidx].hasdefault & uSIZEOF)!=0 || (arg[argidx].hasdefault & uTAGOF)!=0) {
2215 assert(arg[argidx].ident==iVARIABLE);
2218 stgmark((char)(sEXPRSTART+argidx));/* mark beginning of new expression in stage */
2219 if (arg[argidx].hasdefault) {
2220 if (arg[argidx].ident==iREFARRAY) {
2222 setdefarray(arg[argidx].defvalue.array.data,
2223 arg[argidx].defvalue.array.size,
2224 arg[argidx].defvalue.array.arraysize,
2225 &arg[argidx].defvalue.array.addr,
2226 (arg[argidx].usage & uCONST)!=0);
2227 if ((arg[argidx].usage & uCONST)==0) {
2228 heapalloc+=arg[argidx].defvalue.array.arraysize;
2229 nest_stkusage+=arg[argidx].defvalue.array.arraysize;
2231 /* keep the lengths of all dimensions of a multi-dimensional default array */
2232 assert(arg[argidx].numdim>0);
2233 if (arg[argidx].numdim==1) {
2234 append_constval(&arrayszlst,arg[argidx].name,arg[argidx].defvalue.array.arraysize,0);
2236 for (level=0; level<arg[argidx].numdim; level++) {
2237 assert(level<sDIMEN_MAX);
2238 append_constval(&arrayszlst,arg[argidx].name,arg[argidx].dim[level],level);
2241 } else if (arg[argidx].ident==iREFERENCE) {
2242 setheap(arg[argidx].defvalue.val);
2243 /* address of the value on the heap in PRI */
2247 int dummytag=arg[argidx].tags[0];
2248 ldconst(arg[argidx].defvalue.val,sPRI);
2249 assert(arg[argidx].numtags>0);
2250 check_userop(NULL,arg[argidx].defvalue_tag,arg[argidx].tags[0],2,NULL,&dummytag);
2251 assert(dummytag==arg[argidx].tags[0]);
2253 pushreg(sPRI); /* store the function argument on the stack */
2254 markexpr(sPARM,NULL,0); /* mark the end of a sub-expression */
2257 error(202,argidx); /* argument count mismatch */
2259 if (arglist[argidx]==ARG_UNHANDLED)
2261 arglist[argidx]=ARG_DONE;
2263 /* now a second loop to catch the arguments with default values that are
2264 * the "sizeof" or "tagof" of other arguments
2266 for (argidx=0; arg[argidx].ident!=0 && arg[argidx].ident!=iVARARGS; argidx++) {
2269 if (arglist[argidx]==ARG_DONE)
2270 continue; /* already seen and handled this argument */
2271 stgmark((char)(sEXPRSTART+argidx));/* mark beginning of new expression in stage */
2272 assert(arg[argidx].ident==iVARIABLE); /* if "sizeof", must be single cell */
2273 /* if unseen, must be "sizeof" or "tagof" */
2274 assert((arg[argidx].hasdefault & uSIZEOF)!=0 || (arg[argidx].hasdefault & uTAGOF)!=0);
2275 if ((arg[argidx].hasdefault & uSIZEOF)!=0) {
2276 /* find the argument; if it isn't found, the argument's default value
2277 * was a "sizeof" of a non-array (a warning for this was already given
2278 * when declaring the function)
2280 asz=find_constval(&arrayszlst,arg[argidx].defvalue.size.symname,
2281 arg[argidx].defvalue.size.level);
2283 array_sz=asz->value;
2285 error(224,arg[argidx].name); /* indeterminate array size in "sizeof" expression */
2290 asz=find_constval(&taglst,arg[argidx].defvalue.size.symname,
2291 arg[argidx].defvalue.size.level);
2293 exporttag(asz->value);
2294 array_sz=asz->value | PUBLICTAG; /* must be set, because it just was exported */
2299 ldconst(array_sz,sPRI);
2300 pushreg(sPRI); /* store the function argument on the stack */
2301 markexpr(sPARM,NULL,0);
2303 if (arglist[argidx]==ARG_UNHANDLED)
2305 arglist[argidx]=ARG_DONE;
2307 stgmark(sENDREORDER); /* mark end of reversed evaluation */
2308 pushval((cell)nargs*sizeof(cell));
2310 ffcall(sym,NULL,nargs);
2311 if (sc_status!=statSKIP)
2312 markusage(sym,uREAD); /* do not mark as "used" when this call itself is skipped */
2313 if ((sym->usage & uNATIVE)!=0 &&sym->x.lib!=NULL)
2314 sym->x.lib->value += 1; /* increment "usage count" of the library */
2315 modheap(-heapalloc*sizeof(cell));
2317 popreg(sPRI); /* pop hidden parameter as function result */
2318 sideeffect=TRUE; /* assume functions carry out a side-effect */
2319 delete_consttable(&arrayszlst); /* clear list of array sizes */
2320 delete_consttable(&taglst); /* clear list of parameter tags */
2322 /* maintain max. amount of memory used */
2325 totalsize=declared+decl_heap+1; /* local variables & return value size,
2326 * +1 for PROC opcode */
2327 if (lval_result->ident==iREFARRAY)
2328 totalsize++; /* add hidden parameter (on the stack) */
2329 if ((sym->usage & uNATIVE)==0)
2330 totalsize++; /* add "call" opcode */
2331 totalsize+=nest_stkusage;
2332 assert(curfunc!=NULL);
2333 if (curfunc->x.stacksize<totalsize)
2334 curfunc->x.stacksize=totalsize;
2335 nest_stkusage-=nargs+heapalloc+1; /* stack/heap space, +1 for argcount param */
2336 /* if there is a syntax error in the script, the stack calculation is
2337 * probably incorrect; but we may not allow it to drop below zero
2339 if (nest_stkusage<0)
2343 /* scrap any arrays left on the heap, with the exception of the array that
2344 * this function has as a result (in other words, scrap all arrays on the
2345 * heap that caused by expressions in the function arguments)
2347 assert(decl_heap>=locheap);
2348 modheap((locheap-decl_heap)*sizeof(cell)); /* remove heap space, so negative delta */
2355 * Returns a non-zero value if lval1 an array and lval2 is not an array and
2356 * the operation is addition or subtraction.
2358 * Returns the "shift" count (1 for 16-bit, 2 for 32-bit) to align a cell
2359 * to an array offset.
2361 static int dbltest(void (*oper)(),value *lval1,value *lval2)
2363 if ((oper!=ob_add) && (oper!=ob_sub))
2365 if (lval1->ident!=iARRAY)
2367 if (lval2->ident==iARRAY)
2369 return sizeof(cell)/2; /* 1 for 16-bit, 2 for 32-bit */
2374 * Test whether an operator is commutative, i.e. x oper y == y oper x.
2375 * Commutative operators are: + (addition)
2376 * * (multiplication)
2383 * If in an expression, code for the left operand has been generated and
2384 * the right operand is a constant and the operator is commutative, the
2385 * precautionary "push" of the primary register is scrapped and the constant
2386 * is read into the secondary register immediately.
2388 static int commutative(void (*oper)())
2390 return oper==ob_add || oper==os_mult
2391 || oper==ob_eq || oper==ob_ne
2392 || oper==ob_and || oper==ob_xor || oper==ob_or;
2397 * Generates code to fetch a number, a literal character (which is returned
2398 * by lex() as a number as well) or a literal string (lex() stores the
2399 * strings in the literal queue). If the operand was a number, it is stored
2400 * in lval->constval.
2402 * The function returns 1 if the token was a constant or a string, 0
2405 static int constant(value *lval)
2407 int tok,index,ident;
2411 int cmptag=lval->cmptag;
2414 if (tok==tSYMBOL && (sym=findconst(st,&cmptag))!=0) {
2416 error(91,sym->name); /* ambiguity: multiple matching constants (different tags) */
2417 lval->constval=sym->addr;
2418 ldconst(lval->constval,sPRI);
2419 lval->ident=iCONSTEXPR;
2422 markusage(sym,uREAD);
2423 } else if (tok==tNUMBER) {
2425 ldconst(lval->constval,sPRI);
2426 lval->ident=iCONSTEXPR;
2427 } else if (tok==tRATIONAL) {
2429 ldconst(lval->constval,sPRI);
2430 lval->ident=iCONSTEXPR;
2431 lval->tag=sc_rationaltag;
2432 } else if (tok==tSTRING) {
2433 /* lex() stores starting index of string in the literal table in 'val' */
2434 ldconst((val+glb_declared)*sizeof(cell),sPRI);
2435 lval->ident=iARRAY; /* pretend this is a global array */
2436 lval->constval=val-litidx; /* constval == the negative value of the
2437 * size of the literal array; using a negative
2438 * value distinguishes between literal arrays
2439 * and literal strings (this was done for
2440 * array assignment). */
2441 } else if (tok=='{') {
2445 /* cannot call constexpr() here, because "staging" is already turned
2446 * on at this point */
2448 stgget(&index,&cidx); /* mark position in code generator */
2449 ident=expression(&item,&tag,NULL,FALSE);
2450 stgdel(index,cidx); /* scratch generated code */
2451 if (ident!=iCONSTEXPR)
2452 error(8); /* must be constant expression */
2455 else if (!matchtag(lasttag,tag,FALSE))
2456 error(213); /* tagname mismatch */
2457 litadd(item); /* store expression result in literal table */
2458 } while (matchtoken(','));
2459 if (!needtoken('}'))
2461 ldconst((val+glb_declared)*sizeof(cell),sPRI);
2462 lval->ident=iARRAY; /* pretend this is a global array */
2463 lval->constval=litidx-val; /* constval == the size of the literal array */
2465 return FALSE; /* no, it cannot be interpreted as a constant */
2467 return TRUE; /* yes, it was a constant value */