1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 static char sccsid[] = "@(#)fortran.c 5.1 (Berkeley) 05/31/85"; 9 #endif not lint 10 11 static char rcsid[] = "$Header: fortran.c,v 1.5 84/12/26 10:39:37 linton Exp $"; 12 13 /* 14 * FORTRAN dependent symbol routines. 15 */ 16 17 #include "defs.h" 18 #include "symbols.h" 19 #include "printsym.h" 20 #include "languages.h" 21 #include "fortran.h" 22 #include "tree.h" 23 #include "eval.h" 24 #include "operators.h" 25 #include "mappings.h" 26 #include "process.h" 27 #include "runtime.h" 28 #include "machine.h" 29 30 #define isfloat(range) ( \ 31 range->symvalue.rangev.upper == 0 and range->symvalue.rangev.lower > 0 \ 32 ) 33 34 #define isrange(t, name) (t->class == RANGE and istypename(t->type, name)) 35 36 #define MAXDIM 20 37 38 private Language fort; 39 40 /* 41 * Initialize FORTRAN language information. 42 */ 43 44 public fortran_init() 45 { 46 fort = language_define("fortran", ".f"); 47 language_setop(fort, L_PRINTDECL, fortran_printdecl); 48 language_setop(fort, L_PRINTVAL, fortran_printval); 49 language_setop(fort, L_TYPEMATCH, fortran_typematch); 50 language_setop(fort, L_BUILDAREF, fortran_buildaref); 51 language_setop(fort, L_EVALAREF, fortran_evalaref); 52 language_setop(fort, L_MODINIT, fortran_modinit); 53 language_setop(fort, L_HASMODULES, fortran_hasmodules); 54 language_setop(fort, L_PASSADDR, fortran_passaddr); 55 } 56 57 /* 58 * Test if two types are compatible. 59 * 60 * Integers and reals are not compatible since they cannot always be mixed. 61 */ 62 63 public Boolean fortran_typematch(type1, type2) 64 Symbol type1, type2; 65 { 66 67 /* only does integer for now; may need to add others 68 */ 69 70 Boolean b; 71 register Symbol t1, t2, tmp; 72 73 t1 = rtype(type1); 74 t2 = rtype(type2); 75 if(t1 == nil or t1->type == nil or t2 == nil or t2->type == nil ) b = false; 76 else { b = (Boolean) ( 77 (t1 == t2) or 78 (t1->type == t_int and (istypename(t2->type, "integer") or 79 istypename(t2->type, "integer*2")) ) or 80 (t2->type == t_int and (istypename(t1->type, "integer") or 81 istypename(t1->type, "integer*2")) ) 82 ); 83 } 84 /*OUT fprintf(stderr," %d compat %s %s \n", b, 85 (t1 == nil or t1->type == nil ) ? "nil" : symname(t1->type), 86 (t2 == nil or t2->type == nil ) ? "nil" : symname(t2->type) );*/ 87 return b; 88 } 89 90 private String typename(s) 91 Symbol s; 92 { 93 int ub; 94 static char buf[20]; 95 char *pbuf; 96 Symbol st,sc; 97 98 if(s->type->class == TYPE) return(symname(s->type)); 99 100 for(st = s->type; st->type->class != TYPE; st = st->type); 101 102 pbuf=buf; 103 104 if(istypename(st->type,"char")) { 105 sprintf(pbuf,"character*"); 106 pbuf += strlen(pbuf); 107 sc = st->chain; 108 if(sc->symvalue.rangev.uppertype == R_ARG or 109 sc->symvalue.rangev.uppertype == R_TEMP) { 110 if( ! getbound(s,sc->symvalue.rangev.upper, 111 sc->symvalue.rangev.uppertype, &ub) ) 112 sprintf(pbuf,"(*)"); 113 else 114 sprintf(pbuf,"%d",ub); 115 } 116 else sprintf(pbuf,"%d",sc->symvalue.rangev.upper); 117 } 118 else { 119 sprintf(pbuf,"%s ",symname(st->type)); 120 } 121 return(buf); 122 } 123 124 private Symbol mksubs(pbuf,st) 125 Symbol st; 126 char **pbuf; 127 { 128 int lb, ub; 129 Symbol r, eltype; 130 131 if(st->class != ARRAY or (istypename(st->type, "char")) ) return; 132 else { 133 mksubs(pbuf,st->type); 134 assert( (r = st->chain)->class == RANGE); 135 136 if(r->symvalue.rangev.lowertype == R_ARG or 137 r->symvalue.rangev.lowertype == R_TEMP) { 138 if( ! getbound(st,r->symvalue.rangev.lower, 139 r->symvalue.rangev.lowertype, &lb) ) 140 sprintf(*pbuf,"?:"); 141 else 142 sprintf(*pbuf,"%d:",lb); 143 } 144 else { 145 lb = r->symvalue.rangev.lower; 146 sprintf(*pbuf,"%d:",lb); 147 } 148 *pbuf += strlen(*pbuf); 149 150 if(r->symvalue.rangev.uppertype == R_ARG or 151 r->symvalue.rangev.uppertype == R_TEMP) { 152 if( ! getbound(st,r->symvalue.rangev.upper, 153 r->symvalue.rangev.uppertype, &ub) ) 154 sprintf(*pbuf,"?,"); 155 else 156 sprintf(*pbuf,"%d,",ub); 157 } 158 else { 159 ub = r->symvalue.rangev.upper; 160 sprintf(*pbuf,"%d,",ub); 161 } 162 *pbuf += strlen(*pbuf); 163 164 } 165 } 166 167 /* 168 * Print out the declaration of a FORTRAN variable. 169 */ 170 171 public fortran_printdecl(s) 172 Symbol s; 173 { 174 175 176 Symbol eltype; 177 178 switch (s->class) { 179 180 case CONST: 181 182 printf("parameter %s = ", symname(s)); 183 printval(s); 184 break; 185 186 case REF: 187 printf(" (dummy argument) "); 188 189 case VAR: 190 if (s->type->class == ARRAY && 191 (not istypename(s->type->type,"char")) ) { 192 char bounds[130], *p1, **p; 193 p1 = bounds; 194 p = &p1; 195 mksubs(p,s->type); 196 *p -= 1; 197 **p = '\0'; /* get rid of trailing ',' */ 198 printf(" %s %s[%s] ",typename(s), symname(s), bounds); 199 } else { 200 printf("%s %s", typename(s), symname(s)); 201 } 202 break; 203 204 case FUNC: 205 if (not istypename(s->type, "void")) { 206 printf(" %s function ", typename(s) ); 207 } 208 else printf(" subroutine"); 209 printf(" %s ", symname(s)); 210 fortran_listparams(s); 211 break; 212 213 case MODULE: 214 printf("source file \"%s.c\"", symname(s)); 215 break; 216 217 case PROG: 218 printf("executable file \"%s\"", symname(s)); 219 break; 220 221 default: 222 error("class %s in fortran_printdecl", classname(s)); 223 } 224 putchar('\n'); 225 } 226 227 /* 228 * List the parameters of a procedure or function. 229 * No attempt is made to combine like types. 230 */ 231 232 public fortran_listparams(s) 233 Symbol s; 234 { 235 register Symbol t; 236 237 putchar('('); 238 for (t = s->chain; t != nil; t = t->chain) { 239 printf("%s", symname(t)); 240 if (t->chain != nil) { 241 printf(", "); 242 } 243 } 244 putchar(')'); 245 if (s->chain != nil) { 246 printf("\n"); 247 for (t = s->chain; t != nil; t = t->chain) { 248 if (t->class != REF) { 249 panic("unexpected class %d for parameter", t->class); 250 } 251 printdecl(t, 0); 252 } 253 } else { 254 putchar('\n'); 255 } 256 } 257 258 /* 259 * Print out the value on the top of the expression stack 260 * in the format for the type of the given symbol. 261 */ 262 263 public fortran_printval(s) 264 Symbol s; 265 { 266 register Symbol t; 267 register Address a; 268 register int i, len; 269 double d1, d2; 270 271 switch (s->class) { 272 case CONST: 273 case TYPE: 274 case VAR: 275 case REF: 276 case FVAR: 277 case TAG: 278 fortran_printval(s->type); 279 break; 280 281 case ARRAY: 282 t = rtype(s->type); 283 if (t->class == RANGE and istypename(t->type, "char")) { 284 len = size(s); 285 sp -= len; 286 printf("\"%.*s\"", len, sp); 287 } else { 288 fortran_printarray(s); 289 } 290 break; 291 292 case RANGE: 293 if (isfloat(s)) { 294 switch (s->symvalue.rangev.lower) { 295 case sizeof(float): 296 prtreal(pop(float)); 297 break; 298 299 case sizeof(double): 300 if (istypename(s->type,"complex")) { 301 d2 = pop(float); 302 d1 = pop(float); 303 printf("("); 304 prtreal(d1); 305 printf(","); 306 prtreal(d2); 307 printf(")"); 308 } else { 309 prtreal(pop(double)); 310 } 311 break; 312 313 default: 314 panic("bad size \"%d\" for real", 315 t->symvalue.rangev.lower); 316 break; 317 } 318 } else { 319 printint(popsmall(s), s); 320 } 321 break; 322 323 default: 324 if (ord(s->class) > ord(TYPEREF)) { 325 panic("printval: bad class %d", ord(s->class)); 326 } 327 error("don't know how to print a %s", fortran_classname(s)); 328 /* NOTREACHED */ 329 } 330 } 331 332 /* 333 * Print out an int 334 */ 335 336 private printint(i, t) 337 Integer i; 338 register Symbol t; 339 { 340 if (istypename(t->type, "logical")) { 341 printf(((Boolean) i) == true ? "true" : "false"); 342 } 343 else if ( (t->type == t_int) or istypename(t->type, "integer") or 344 istypename(t->type,"integer*2") ) { 345 printf("%ld", i); 346 } else { 347 error("unkown type in fortran printint"); 348 } 349 } 350 351 /* 352 * Print out a null-terminated string (pointer to char) 353 * starting at the given address. 354 */ 355 356 private printstring(addr) 357 Address addr; 358 { 359 register Address a; 360 register Integer i, len; 361 register Boolean endofstring; 362 union { 363 char ch[sizeof(Word)]; 364 int word; 365 } u; 366 367 putchar('"'); 368 a = addr; 369 endofstring = false; 370 while (not endofstring) { 371 dread(&u, a, sizeof(u)); 372 i = 0; 373 do { 374 if (u.ch[i] == '\0') { 375 endofstring = true; 376 } else { 377 printchar(u.ch[i]); 378 } 379 ++i; 380 } while (i < sizeof(Word) and not endofstring); 381 a += sizeof(Word); 382 } 383 putchar('"'); 384 } 385 /* 386 * Return the FORTRAN name for the particular class of a symbol. 387 */ 388 389 public String fortran_classname(s) 390 Symbol s; 391 { 392 String str; 393 394 switch (s->class) { 395 case REF: 396 str = "dummy argument"; 397 break; 398 399 case CONST: 400 str = "parameter"; 401 break; 402 403 default: 404 str = classname(s); 405 } 406 return str; 407 } 408 409 /* reverses the indices from the expr_list; should be folded into buildaref 410 * and done as one recursive routine 411 */ 412 Node private rev_index(here,n) 413 register Node here,n; 414 { 415 416 register Node i; 417 418 if( here == nil or here == n) i=nil; 419 else if( here->value.arg[1] == n) i = here; 420 else i=rev_index(here->value.arg[1],n); 421 return i; 422 } 423 424 public Node fortran_buildaref(a, slist) 425 Node a, slist; 426 { 427 register Symbol as; /* array of array of .. cursor */ 428 register Node en; /* Expr list cursor */ 429 Symbol etype; /* Type of subscript expr */ 430 Node esub, tree; /* Subscript expression ptr and tree to be built*/ 431 432 tree=a; 433 434 as = rtype(tree->nodetype); /* node->sym.type->array*/ 435 if ( not ( 436 (tree->nodetype->class == VAR or tree->nodetype->class == REF) 437 and as->class == ARRAY 438 ) ) { 439 beginerrmsg(); 440 prtree(stderr, a); 441 fprintf(stderr, " is not an array"); 442 /*fprintf(stderr, " a-> %x as %x ", tree->nodetype, as ); OUT*/ 443 enderrmsg(); 444 } else { 445 for (en = rev_index(slist,nil); en != nil and as->class == ARRAY; 446 en = rev_index(slist,en), as = as->type) { 447 esub = en->value.arg[0]; 448 etype = rtype(esub->nodetype); 449 assert(as->chain->class == RANGE); 450 if ( not compatible( t_int, etype) ) { 451 beginerrmsg(); 452 fprintf(stderr, "subscript "); 453 prtree(stderr, esub); 454 fprintf(stderr, " is type %s ",symname(etype->type) ); 455 enderrmsg(); 456 } 457 tree = build(O_INDEX, tree, esub); 458 tree->nodetype = as->type; 459 } 460 if (en != nil or 461 (as->class == ARRAY && (not istypename(as->type,"char"))) ) { 462 beginerrmsg(); 463 if (en != nil) { 464 fprintf(stderr, "too many subscripts for "); 465 } else { 466 fprintf(stderr, "not enough subscripts for "); 467 } 468 prtree(stderr, tree); 469 enderrmsg(); 470 } 471 } 472 return tree; 473 } 474 475 /* 476 * Evaluate a subscript index. 477 */ 478 479 public fortran_evalaref(s, base, i) 480 Symbol s; 481 Address base; 482 long i; 483 { 484 Symbol r, t; 485 long lb, ub; 486 487 t = rtype(s); 488 r = t->chain; 489 if ( 490 r->symvalue.rangev.lowertype == R_ARG or 491 r->symvalue.rangev.lowertype == R_TEMP 492 ) { 493 if (not getbound( 494 s, r->symvalue.rangev.lower, r->symvalue.rangev.lowertype, &lb 495 )) { 496 error("dynamic bounds not currently available"); 497 } 498 } else { 499 lb = r->symvalue.rangev.lower; 500 } 501 if ( 502 r->symvalue.rangev.uppertype == R_ARG or 503 r->symvalue.rangev.uppertype == R_TEMP 504 ) { 505 if (not getbound( 506 s, r->symvalue.rangev.upper, r->symvalue.rangev.uppertype, &ub 507 )) { 508 error("dynamic bounds not currently available"); 509 } 510 } else { 511 ub = r->symvalue.rangev.upper; 512 } 513 514 if (i < lb or i > ub) { 515 error("subscript out of range"); 516 } 517 push(long, base + (i - lb) * size(t->type)); 518 } 519 520 private fortran_printarray(a) 521 Symbol a; 522 { 523 struct Bounds { int lb, val, ub} dim[MAXDIM]; 524 525 Symbol sc,st,eltype; 526 char buf[50]; 527 char *subscr; 528 int i,ndim,elsize; 529 Stack *savesp; 530 Boolean done; 531 532 st = a; 533 534 savesp = sp; 535 sp -= size(a); 536 ndim=0; 537 538 for(;;){ 539 sc = st->chain; 540 if(sc->symvalue.rangev.lowertype == R_ARG or 541 sc->symvalue.rangev.lowertype == R_TEMP) { 542 if( ! getbound(a,sc->symvalue.rangev.lower, 543 sc->symvalue.rangev.lowertype, &dim[ndim].lb) ) 544 error(" dynamic bounds not currently available"); 545 } 546 else dim[ndim].lb = sc->symvalue.rangev.lower; 547 548 if(sc->symvalue.rangev.uppertype == R_ARG or 549 sc->symvalue.rangev.uppertype == R_TEMP) { 550 if( ! getbound(a,sc->symvalue.rangev.upper, 551 sc->symvalue.rangev.uppertype, &dim[ndim].ub) ) 552 error(" dynamic bounds not currently available"); 553 } 554 else dim[ndim].ub = sc->symvalue.rangev.upper; 555 556 ndim ++; 557 if (st->type->class == ARRAY) st=st->type; 558 else break; 559 } 560 561 if(istypename(st->type,"char")) { 562 eltype = st; 563 ndim--; 564 } 565 else eltype=st->type; 566 elsize=size(eltype); 567 sp += elsize; 568 /*printf("ndim %d elsize %lx in fortran_printarray\n",ndim,elsize);OUT*/ 569 570 ndim--; 571 for (i=0;i<=ndim;i++){ 572 dim[i].val=dim[i].lb; 573 /*OUT printf(" %d %d %d \n",i,dim[i].lb,dim[i].ub); 574 fflush(stdout); OUT*/ 575 } 576 577 578 for(;;) { 579 buf[0]=','; 580 subscr = buf+1; 581 582 for (i=ndim-1;i>=0;i--) { 583 584 sprintf(subscr,"%d,",dim[i].val); 585 subscr += strlen(subscr); 586 } 587 *--subscr = '\0'; 588 589 for(i=dim[ndim].lb;i<=dim[ndim].ub;i++) { 590 printf("[%d%s]\t",i,buf); 591 printval(eltype); 592 printf("\n"); 593 sp += 2*elsize; 594 } 595 dim[ndim].val=dim[ndim].ub; 596 597 i=ndim-1; 598 if (i<0) break; 599 600 done=false; 601 do { 602 dim[i].val++; 603 if(dim[i].val > dim[i].ub) { 604 dim[i].val = dim[i].lb; 605 if(--i<0) done=true; 606 } 607 else done=true; 608 } 609 while (not done); 610 if (i<0) break; 611 } 612 } 613 614 /* 615 * Initialize typetable at beginning of a module. 616 */ 617 618 public fortran_modinit (typetable) 619 Symbol typetable[]; 620 { 621 /* nothing for now */ 622 } 623 624 public boolean fortran_hasmodules () 625 { 626 return false; 627 } 628 629 public boolean fortran_passaddr (param, exprtype) 630 Symbol param, exprtype; 631 { 632 return false; 633 } 634