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: gencode.c,v 1.3 1994/06/24 11:30:46 ceriel Exp $" ;
13 /* Here we find all routines dealing with pure EM code generation */
44 C_df_ilb((label)currline->emlabel);
46 if ( tronoff || traceflag) {
49 C_asp((arith)BEMINTSIZE);
57 /* Handle data statements */
62 extern long sys_filesize();
64 /* NOSTRICT */ l= (List *) salloc(sizeof(List));
65 l->linenr= currline->linenr;
66 l->emlabel = sys_filesize(datfname);
72 while (l1->nextlist) l1= l1->nextlist;
85 /* called at end to generate the data seek table */
86 C_exa_dnam("_seektab");
87 C_df_dnam("_seektab"); /* VRAAGTEKEN */
91 C_rom_cst((arith)(l->linenr));
92 C_rom_cst((arith)(line++));
101 /* ERROR and exception handling */
105 /* exceptions to subroutines are supported only */
109 C_loc((arith)gosubcnt);
110 l= (List *) gosublabel();
111 l->emlabel= gotolabel(lab);
113 C_asp((arith)BEMINTSIZE);
121 /* convert expression to a valid error number */
122 /* obtain the message and print it */
124 C_asp((arith)typesize(exprtype));
133 C_loc((arith)recsize);
135 C_asp((arith)(2*BEMPTRSIZE+BEMINTSIZE));
147 C_asp((arith)typestring(INTTYPE));
152 C_asp((arith)typestring(DOUBLETYPE));
156 C_asp((arith)BEMPTRSIZE);
158 case 0: /* result of tab function etc */
161 error("printstmt:unexpected");
170 if ( i) C_cal("_zone");
175 writestmt(exprtype,comma)
178 if ( comma) C_cal("_wrcomma");
193 error("printstmt:unexpected");
195 C_asp((arith)BEMPTRSIZE);
203 /* save this information too */
207 C_asp((arith)BEMINTSIZE);
210 C_asp((arith)BEMINTSIZE);
220 C_asp((arith)BEMPTRSIZE);
221 if (qst) C_cal("_qstmark");
229 if ( type!= STRINGTYPE)
230 error("String variable expected");
232 C_asp((arith)BEMPTRSIZE);
253 error("readelm:unexpected type");
255 C_asp((arith)BEMPTRSIZE);
260 /* Swap exchanges the variable values */
261 swapstmt(ltype,rtype)
265 error("Type mismatch");
280 error("swap:unexpected");
283 C_asp((arith)(2*BEMPTRSIZE));
288 /* input/output handling */
291 { /* obtain file descroption */
294 C_asp((arith)BEMINTSIZE);
299 /* The if-then-else statements */
303 /* This BASIC follows the True= -1 rule */
307 if ( type == INTTYPE)
310 if ( type == FLOATTYPE || type == DOUBLETYPE )
312 C_lae_dnam("fltnull",(arith)0);
313 C_loi((arith)BEMFLTSIZE);
314 C_cmf((arith)BEMFLTSIZE);
317 else error("Integer or Float expected");
331 C_df_ilb((label)elselab);
337 elsepart(lab)int lab;
339 C_df_ilb((label)lab);
344 /* generate code for the for-statement */
345 #define MAXFORDEPTH 20
348 Symbol *loopvar; /* loop variable */
352 int fortst; /* variable limit test */
353 int forinc; /* variable increment code */
354 int forout; /* end of loop */
355 } fortable[MAXFORDEPTH];
370 if ( (type!=INTTYPE && type!=FLOATTYPE && type!=DOUBLETYPE) ||
372 error("Illegal loop variable");
373 if ( forcnt >=MAXFORDEPTH)
374 error("too many for statements");
378 f->fortst=genlabel();
379 f->forinc=genlabel();
380 f->forout=genlabel();
381 /* generate space for temporary objects */
382 f->initaddress= dclspace(type);
383 f->limitaddress= dclspace(type);
384 f->stepaddress= dclspace(type);
393 /* save start value of loop variable in a save place*/
394 /* to avoid clashing with final value and step expression */
397 result= fortable[forcnt].loopvar->symtype;
398 conversion(type,result);
399 storevar(fortable[forcnt].initaddress, result);
407 /* save the limit value too*/
410 result= fortable[forcnt].loopvar->symtype;
411 conversion(type,result);
412 storevar(fortable[forcnt].limitaddress, result);
422 type= f->loopvar->symtype;
423 /* evaluate lower bound times sign of step */
424 C_lae_dlb((label)f->initaddress,(arith)0);
426 conversion(type,DOUBLETYPE);
427 C_lae_dlb((label)f->stepaddress,(arith)0);
429 conversion(type,DOUBLETYPE);
431 C_asp((arith)BEMFLTSIZE);
432 C_lfr((arith)BEMINTSIZE);
433 conversion(INTTYPE,DOUBLETYPE);
434 C_mlf((arith)BEMFLTSIZE);
435 /* evaluate higher bound times sign of step */
436 C_lae_dlb((label)f->limitaddress,(arith)0);
438 conversion(type,DOUBLETYPE);
439 C_lae_dlb((label)f->stepaddress,(arith)0);
441 conversion(type,DOUBLETYPE);
443 C_asp((arith)BEMFLTSIZE);
444 C_lfr((arith)BEMINTSIZE);
445 conversion(INTTYPE,DOUBLETYPE);
446 C_mlf((arith)BEMFLTSIZE);
448 C_cmf((arith)BEMFLTSIZE);
449 C_zgt((label)f->forout);
462 result= f->loopvar->symtype;
463 varaddress= f->loopvar->symalias;
464 conversion(type,result);
465 storevar(f->stepaddress, result);
466 /* all information available, generate for-loop head */
467 /* test for ingoring loop */
469 /* set initial value */
470 C_lae_dlb((label)f->initaddress,(arith)0);
472 C_lae_dlb((label)varaddress,(arith)0);
473 C_sti((arith)typestring(result));
474 C_bra((label)f->fortst);
475 /* increment loop variable */
476 C_df_ilb((label)f->forinc);
477 C_lae_dlb((label)varaddress,(arith)0);
479 C_lae_dlb((label)f->stepaddress,(arith)0);
481 if (result == INTTYPE)
482 C_adi((arith)BEMINTSIZE);
483 else C_adf((arith)BEMFLTSIZE);
484 C_lae_dlb((label)varaddress,(arith)0);
485 C_sti((arith)typestring(result));
487 C_df_ilb((label)f->fortst);
488 C_lae_dlb((label)varaddress,(arith)0);
490 /* Start of NEW code */
491 C_lae_dlb((label)f->stepaddress,(arith)0);
493 conversion(result,DOUBLETYPE);
495 C_asp((arith)BEMFLTSIZE);
496 C_lfr((arith)BEMINTSIZE);
497 conversion(INTTYPE,result);
498 if ( result == INTTYPE )
499 C_mli((arith)BEMINTSIZE);
500 else C_mlf((arith)BEMFLTSIZE);
501 /* End of NEW code */
502 C_lae_dlb((label)f->limitaddress,(arith)0);
505 C_lae_dlb((label)f->stepaddress,(arith)0);
507 conversion(result,DOUBLETYPE);
509 C_asp((arith)BEMFLTSIZE);
510 C_lfr((arith)BEMINTSIZE);
511 conversion(INTTYPE,result);
512 if ( result == INTTYPE )
513 C_mli((arith)BEMINTSIZE);
514 else C_mlf((arith)BEMFLTSIZE);
516 if (result == INTTYPE)
517 C_cmi((arith)BEMINTSIZE);
518 else C_cmf((arith)BEMFLTSIZE);
519 C_zgt((label)f->forout);
527 if (forcnt>MAXFORDEPTH || forcnt<0 ||
528 (s && s!= fortable[forcnt].loopvar))
529 error("NEXT without FOR");
531 /* address of variable is on top of stack ! */
532 C_bra((label)fortable[forcnt].forinc);
533 C_df_ilb((label)fortable[forcnt].forout);
540 pokestmt(type1,type2)
543 conversion(type1,INTTYPE);
544 conversion(type2,INTTYPE);
545 C_asp((arith)(2*BEMINTSIZE));
550 /* generate code for the while statement */
553 int whilecnt, whilelabels[MAXDEPTH][2]; /*0=head,1=out */
558 if ( whilecnt==MAXDEPTH)
559 fatal("too many nestings");
560 /* gendummy label in graph */
562 whilelabels[whilecnt][0]= currline->emlabel;
563 whilelabels[whilecnt][1]= genlabel();
564 C_df_ilb((label)whilelabels[whilecnt][0]);
572 /* test expression type */
573 conversion(exprtype,INTTYPE);
574 C_zeq((label)whilelabels[whilecnt][1]);
582 error("not part of while statement");
584 C_bra((label)whilelabels[whilecnt][0]);
585 C_df_ilb((label)whilelabels[whilecnt][1]);
592 /* generate code for the final version */
595 /* generate the EM prolog code */
596 C_df_dnam("fltnull");
606 /* NEW variable we make */
608 C_bss_dnam((arith)BEMPTRSIZE,"dummy3",(arith)0,0);
609 C_df_dnam("tronoff");
616 C_exa_dnam("_iomode");
617 C_df_dnam("_iomode");
618 C_rom_scon("O",(arith)2);
619 C_exa_dnam("_errsym");
620 C_df_dnam("_errsym");
621 C_bss_cst((arith)BEMINTSIZE,(arith)0,1);
622 C_exa_dnam("_erlsym");
623 C_df_dnam("_erlsym");
624 C_bss_cst((arith)BEMINTSIZE,(arith)0,1);
632 label l = genlabel(), l2;
634 err_goto_label = genlabel();
636 C_pro("main",(arith)0);
643 C_lae_dlb(l2, (arith) 0);
644 C_loi((arith) BEMPTRSIZE);
645 C_exa_dnam("trpbuf");
646 C_lae_dnam("trpbuf",(arith)0);
649 C_asp((arith)(BEMPTRSIZE+BEMPTRSIZE));
650 C_lfr((arith)BEMINTSIZE);
651 C_dup((arith)BEMINTSIZE);
653 C_lae_dnam("returns",(arith)0);
654 C_csa((arith)BEMINTSIZE);
656 C_asp((arith)BEMINTSIZE);
657 result= sys_open(datfname, OP_WRITE, &datfile);
658 if ( result==0 ) fatal("improper file creation permission");
669 C_asp((arith)BEMINTSIZE);
670 C_df_dnam("datfname");
671 C_rom_scon(datfname,(arith)strlen(datfname) + 1); /* EHB */
673 C_rom_scon("i\\0",(arith)4);
674 C_df_dnam("datfdes");
675 C_rom_dnam("datfname",(arith)0);
677 C_rom_cst((arith)(itoa(strlen(datfname))));
678 C_df_dnam("dattdes");
679 C_rom_dnam("dattyp",(arith)0);
682 C_lae_dnam("dattdes",(arith)0);
683 C_lae_dnam("datfdes",(arith)0);
686 C_asp((arith)(2*BEMPTRSIZE+BEMINTSIZE));
693 /* finalization code */
699 datatable(); /* NEW */
702 C_df_ilb(err_goto_label);