2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
9 static char rcs_id[] = "$Id: eval.c,v 1.2 1994/06/24 11:30:39 ceriel Exp $" ;
13 /* Here you find all routines to evaluate expressions and
14 generate code for assignment statements
20 /* determine the result type of an expression */
21 if ( ltype==STRINGTYPE || rtype==STRINGTYPE)
24 error("type conflict, string expected");
28 if ( ltype<rtype) return(rtype);
34 conversion(oldtype,newtype)
37 /* the value on top of the stack should be converted */
38 if ( oldtype==newtype) return;
43 if ( newtype==FLOATTYPE || newtype==DOUBLETYPE)
45 C_loc((arith)BEMINTSIZE);
46 C_loc((arith)BEMFLTSIZE);
50 print("type n=%d o=%d\n",newtype,oldtype);
51 error("conversion error");
56 if ( newtype==INTTYPE)
60 C_asp((arith)BEMFLTSIZE);
61 C_lfr((arith)BEMINTSIZE);
63 } else if ( newtype==FLOATTYPE || newtype==DOUBLETYPE)
67 print("type n=%d o=%d\n",newtype,oldtype);
68 error("conversion error");
74 extraconvert(oldtype,newtype,topstack)
75 int oldtype,newtype,topstack;
77 /* the value below the top of the stack should be converted */
78 if ( oldtype==newtype ) return;
79 if ( debug) print("extra convert %d %d %d\n",oldtype,newtype,topstack);
80 /* save top in dummy */
85 C_ste_dnam("dummy1",(arith)0);
90 C_lae_dnam("dummy1",(arith)0);
91 C_sti((arith)BEMFLTSIZE);
94 error("conversion error");
97 /* now its on top of the stack */
99 conversion(oldtype,newtype);
105 C_loe_dnam("dummy1",(arith)0);
110 C_lae_dnam("dummy1",(arith)0);
111 C_loi((arith)BEMFLTSIZE);
117 boolop(ltype,rtype,operator)
118 int ltype,rtype,operator;
120 if ( operator != NOTSYM)
122 extraconvert(ltype,INTTYPE,rtype);
123 conversion(rtype,INTTYPE);
124 } else conversion(ltype,INTTYPE);
129 C_com((arith)BEMINTSIZE);
132 C_and((arith)BEMINTSIZE);
135 C_ior((arith)BEMINTSIZE);
138 C_xor((arith)BEMINTSIZE);
141 C_xor((arith)BEMINTSIZE);
142 C_com((arith)BEMINTSIZE);
146 C_com((arith)BEMINTSIZE);
147 C_and((arith)BEMINTSIZE);
148 C_com((arith)BEMINTSIZE);
151 error("boolop:unexpected");
169 case '<': C_zlt((label)l1); break;
170 case '>': C_zgt((label)l1); break;
171 case '=': C_zeq((label)l1); break;
172 case NESYM: C_zne((label)l1); break;
173 case LESYM: C_zle((label)l1); break;
174 case GESYM: C_zge((label)l1); break;
175 default: error("relop:unexpected operator");
187 relop( ltype,rtype,operator)
188 int ltype,rtype,operator;
192 if (debug) print("relop %d %d op=%d\n",ltype,rtype,operator);
193 result= exprtype(ltype,rtype);
194 extraconvert(ltype,result,rtype);
195 conversion(rtype,result);
196 /* compare the objects */
197 if ( result==INTTYPE)
198 C_cmi((arith)BEMINTSIZE);
199 else if ( result==FLOATTYPE || result==DOUBLETYPE)
200 C_cmf((arith)BEMFLTSIZE);
201 else if ( result==STRINGTYPE)
204 C_asp((arith)(2*BEMPTRSIZE));
205 C_lfr((arith)BEMINTSIZE);
206 } else error("relop:unexpected");
207 /* handle the relational operators */
214 plusmin(ltype,rtype,operator)
215 int ltype,rtype,operator;
219 result= exprtype(ltype,rtype);
220 if ( result== STRINGTYPE)
225 C_asp((arith)(2*BEMPTRSIZE));
226 C_lfr((arith)BEMPTRSIZE);
227 } else error("illegal operator");
229 extraconvert(ltype,result,rtype);
230 conversion(rtype,result);
231 if ( result== INTTYPE)
234 C_adi((arith)BEMINTSIZE);
235 else C_sbi((arith)BEMINTSIZE);
238 C_adf((arith)BEMFLTSIZE);
239 else C_sbf((arith)BEMFLTSIZE);
247 muldiv(ltype,rtype,operator)
248 int ltype,rtype,operator;
252 result=exprtype(ltype,rtype);
253 if (operator==MODSYM || operator== '\\') result=INTTYPE;
254 extraconvert(ltype,result,rtype);
255 conversion(rtype,result);
256 if ( result== INTTYPE)
261 extraconvert(ltype,result,rtype);
262 conversion(rtype,result);
263 C_dvf((arith)BEMFLTSIZE);
266 C_dvi((arith)BEMINTSIZE);
269 C_mli((arith)BEMINTSIZE);
271 if ( operator==MODSYM)
272 C_rmi((arith)BEMINTSIZE);
273 else error("illegal operator");
276 C_dvf((arith)BEMFLTSIZE);
279 C_mlf((arith)BEMFLTSIZE);
280 else error("illegal operator");
293 C_ngi((arith)BEMINTSIZE);
297 C_ngf((arith)BEMFLTSIZE);
300 error("Illegal operator");
311 int resulttype = exprtype(ltype, rtype);
313 extraconvert(ltype,resulttype,rtype);
314 conversion(rtype,resulttype);
324 error("Illegal operator");
326 C_asp((arith)(2*typestring(resulttype)));
327 C_lfr((arith)typestring(resulttype));
334 extraconvert(ltype,DOUBLETYPE,rtype);
335 conversion(rtype,DOUBLETYPE);
337 C_asp((arith)(2*BEMFLTSIZE));
338 C_lfr((arith)BEMFLTSIZE);
357 error("typesize:unexpected");
358 if (debug) print("type received %d\n",ltype);
378 error("typestring: unexpected type");
388 /* load a simple variable its address is on the stack*/
389 C_loi((arith)typestring(type));
409 C_df_dlb((label)index);
410 C_bss_fcon((arith)BEMFLTSIZE,value,(arith)BEMFLTSIZE,1);
411 C_lae_dlb((label)index,(arith)0);
412 C_loi((arith)BEMFLTSIZE);
421 C_lae_dlb((label)value,(arith)0);
433 if (debug) print("load %s %d\n",s->symname,s->symtype);
435 C_lae_dlb((label)s->symalias,(arith)0);
438 if (debug) print("load parm %d\n",j);
439 /* first count the sizes. */
441 for(i=fcn->dimensions;i>j;i--)
442 sum += typesize(fcn->dimlimit[i-1]);
450 /* This is a new routine */
453 C_lae_dnam("dummy3",(arith)0);
454 C_sti((arith)BEMPTRSIZE);
465 C_lae_dnam("dummy3",(arith)0); /* Statement added by us */
466 C_loi((arith)BEMPTRSIZE);
467 /* address is on stack already */
468 C_sti((arith)typestring(type));
476 /*store value back */
477 C_lae_dlb((label)lab,(arith)0);
478 C_sti((arith)typestring(type));
483 /* maintain a stack of array references */
484 int dimstk[MAXDIMENSIONS], dimtop= -1;
485 Symbol *arraystk[MAXDIMENSIONS];
492 if ( dimtop<MAXDIMENSIONS) dimtop++;
493 if ( s->dimensions==0)
500 C_lae_dlb((label)s->symalias,(arith)0);
507 return(arraystk[dimtop--]->symtype);
518 if ( dimtop<0 || dimtop>=MAXDIMENSIONS)
519 fatal("too many nested array references");
520 /* index expression is on top of stack */
523 if ( dim>=s->dimensions)
525 error("too many indices");
529 conversion(type,INTTYPE);
530 C_lae_dlb((label)s->dimalias[dim],(arith)0);
531 C_aar((arith)BEMINTSIZE);