c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* Copyright (c) 1982 Regents of the University of California */
  2. static char sccsid[] = "@(#)c.c 1.7 8/16/83";
  3. /*
  4. * C-dependent symbol routines.
  5. */
  6. #include "defs.h"
  7. #include "symbols.h"
  8. #include "printsym.h"
  9. #include "languages.h"
  10. #include "c.h"
  11. #include "tree.h"
  12. #include "eval.h"
  13. #include "operators.h"
  14. #include "mappings.h"
  15. #include "process.h"
  16. #include "runtime.h"
  17. #include "machine.h"
  18. #ifndef public
  19. # include "tree.h"
  20. #endif
  21. #define isdouble(range) ( \
  22. range->symvalue.rangev.upper == 0 and range->symvalue.rangev.lower > 0 \
  23. )
  24. #define isrange(t, name) (t->class == RANGE and istypename(t->type, name))
  25. /*
  26. * Initialize C language information.
  27. */
  28. public c_init()
  29. {
  30. Language lang;
  31. lang = language_define("c", ".c");
  32. language_setop(lang, L_PRINTDECL, c_printdecl);
  33. language_setop(lang, L_PRINTVAL, c_printval);
  34. language_setop(lang, L_TYPEMATCH, c_typematch);
  35. language_setop(lang, L_BUILDAREF, c_buildaref);
  36. language_setop(lang, L_EVALAREF, c_evalaref);
  37. }
  38. /*
  39. * Test if two types are compatible.
  40. */
  41. public Boolean c_typematch(type1, type2)
  42. Symbol type1, type2;
  43. {
  44. Boolean b;
  45. register Symbol t1, t2, tmp;
  46. t1 = type1;
  47. t2 = type2;
  48. if (t1 == t2) {
  49. b = true;
  50. } else {
  51. t1 = rtype(t1);
  52. t2 = rtype(t2);
  53. if (t1->type == t_char or t1->type == t_int or t1->type == t_real) {
  54. tmp = t1;
  55. t1 = t2;
  56. t2 = tmp;
  57. }
  58. b = (Boolean) (
  59. (
  60. isrange(t1, "int") and
  61. (t2->type == t_int or t2->type == t_char)
  62. ) or (
  63. isrange(t1, "char") and
  64. (t2->type == t_char or t2->type == t_int)
  65. ) or (
  66. t1->class == RANGE and isdouble(t1) and t2->type == t_real
  67. ) or (
  68. t1->type == t2->type and (
  69. (t1->class == t2->class) or
  70. (t1->class == SCAL and t2->class == CONST) or
  71. (t1->class == CONST and t2->class == SCAL)
  72. )
  73. )
  74. );
  75. }
  76. return b;
  77. }
  78. /*
  79. * Decide if a field is a bit field.
  80. */
  81. private Boolean isbitfield(s)
  82. register Symbol s;
  83. {
  84. Boolean b;
  85. register Integer off, len;
  86. register Symbol t;
  87. off = s->symvalue.field.offset;
  88. len = s->symvalue.field.length;
  89. if ((off mod BITSPERBYTE) != 0 or (len mod BITSPERBYTE) != 0) {
  90. b = true;
  91. } else {
  92. t = rtype(s->type);
  93. b = (Boolean)
  94. (t->class == SCAL and len != (sizeof(int)*BITSPERBYTE) or
  95. len != (size(t)*BITSPERBYTE)
  96. );
  97. }
  98. return b;
  99. }
  100. /*
  101. * Print out the declaration of a C variable.
  102. */
  103. public c_printdecl(s)
  104. Symbol s;
  105. {
  106. printdecl(s, 0);
  107. }
  108. private printdecl(s, indent)
  109. register Symbol s;
  110. Integer indent;
  111. {
  112. register Symbol t;
  113. Boolean semicolon, newline;
  114. semicolon = true;
  115. newline = true;
  116. if (indent > 0) {
  117. printf("%*c", indent, ' ');
  118. }
  119. if (s->class == TYPE) {
  120. printf("typedef ");
  121. }
  122. switch (s->class) {
  123. case CONST:
  124. if (s->type->class == SCAL) {
  125. printf("(enumeration constant, ord %ld)",
  126. s->symvalue.iconval);
  127. } else {
  128. printf("const %s = ", symname(s));
  129. printval(s);
  130. }
  131. break;
  132. case TYPE:
  133. case VAR:
  134. if (s->class != TYPE) {
  135. if (s->level == 2) {
  136. printf("static ");
  137. } else if (s->level < 0) {
  138. printf("register ");
  139. }
  140. }
  141. if (s->type->class == ARRAY) {
  142. printtype(s->type, s->type->type, indent);
  143. t = rtype(s->type->chain);
  144. assert(t->class == RANGE);
  145. printf(" %s[%d]", symname(s), t->symvalue.rangev.upper + 1);
  146. } else {
  147. printtype(s, s->type, indent);
  148. if (s->type->class != PTR) {
  149. printf(" ");
  150. }
  151. printf("%s", symname(s));
  152. }
  153. break;
  154. case FIELD:
  155. if (s->type->class == ARRAY) {
  156. printtype(s->type, s->type->type, indent);
  157. t = rtype(s->type->chain);
  158. assert(t->class == RANGE);
  159. printf(" %s[%d]", symname(s), t->symvalue.rangev.upper + 1);
  160. } else {
  161. printtype(s, s->type, indent);
  162. if (s->type->class != PTR) {
  163. printf(" ");
  164. }
  165. printf("%s", symname(s));
  166. }
  167. if (isbitfield(s)) {
  168. printf(" : %d", s->symvalue.field.length);
  169. }
  170. break;
  171. case TAG:
  172. if (s->type == nil) {
  173. findtype(s);
  174. if (s->type == nil) {
  175. error("unexpected missing type information");
  176. }
  177. }
  178. printtype(s, s->type, indent);
  179. break;
  180. case RANGE:
  181. case ARRAY:
  182. case RECORD:
  183. case VARNT:
  184. case PTR:
  185. semicolon = false;
  186. printtype(s, s, indent);
  187. break;
  188. case PROC:
  189. semicolon = false;
  190. printf("%s", symname(s));
  191. c_listparams(s);
  192. newline = false;
  193. break;
  194. case FUNC:
  195. semicolon = false;
  196. if (not istypename(s->type, "void")) {
  197. printtype(s, s->type, indent);
  198. printf(" ");
  199. }
  200. printf("%s", symname(s));
  201. c_listparams(s);
  202. newline = false;
  203. break;
  204. case MODULE:
  205. semicolon = false;
  206. printf("source file \"%s.c\"", symname(s));
  207. break;
  208. case PROG:
  209. semicolon = false;
  210. printf("executable file \"%s\"", symname(s));
  211. break;
  212. default:
  213. error("class %s in c_printdecl", classname(s));
  214. }
  215. if (semicolon) {
  216. putchar(';');
  217. }
  218. if (newline) {
  219. putchar('\n');
  220. }
  221. }
  222. /*
  223. * Recursive whiz-bang procedure to print the type portion
  224. * of a declaration.
  225. *
  226. * The symbol associated with the type is passed to allow
  227. * searching for type names without getting "type blah = blah".
  228. */
  229. private printtype(s, t, indent)
  230. Symbol s;
  231. Symbol t;
  232. Integer indent;
  233. {
  234. register Symbol i;
  235. long r0, r1;
  236. register String p;
  237. checkref(s);
  238. checkref(t);
  239. switch (t->class) {
  240. case VAR:
  241. case CONST:
  242. case PROC:
  243. panic("printtype: class %s", classname(t));
  244. break;
  245. case ARRAY:
  246. printf("array[");
  247. i = t->chain;
  248. if (i != nil) {
  249. for (;;) {
  250. printtype(i, i, indent);
  251. i = i->chain;
  252. if (i == nil) {
  253. break;
  254. }
  255. printf(", ");
  256. }
  257. }
  258. printf("] of ");
  259. printtype(t, t->type, indent);
  260. break;
  261. case RECORD:
  262. case VARNT:
  263. printf("%s ", c_classname(t));
  264. if (s->name != nil and s->class == TAG) {
  265. p = symname(s);
  266. if (p[0] == '$' and p[1] == '$') {
  267. printf("%s ", &p[2]);
  268. } else {
  269. printf("%s ", p);
  270. }
  271. }
  272. printf("{\n", t->class == RECORD ? "struct" : "union");
  273. for (i = t->chain; i != nil; i = i->chain) {
  274. assert(i->class == FIELD);
  275. printdecl(i, indent+4);
  276. }
  277. if (indent > 0) {
  278. printf("%*c", indent, ' ');
  279. }
  280. printf("}");
  281. break;
  282. case RANGE:
  283. r0 = t->symvalue.rangev.lower;
  284. r1 = t->symvalue.rangev.upper;
  285. if (istypename(t->type, "char")) {
  286. if (r0 < 0x20 or r0 > 0x7e) {
  287. printf("%ld..", r0);
  288. } else {
  289. printf("'%c'..", (char) r0);
  290. }
  291. if (r1 < 0x20 or r1 > 0x7e) {
  292. printf("\\%lo", r1);
  293. } else {
  294. printf("'%c'", (char) r1);
  295. }
  296. } else if (r0 > 0 and r1 == 0) {
  297. printf("%ld byte real", r0);
  298. } else if (r0 >= 0) {
  299. printf("%lu..%lu", r0, r1);
  300. } else {
  301. printf("%ld..%ld", r0, r1);
  302. }
  303. break;
  304. case PTR:
  305. printtype(t, t->type, indent);
  306. if (t->type->class != PTR) {
  307. printf(" ");
  308. }
  309. printf("*");
  310. break;
  311. case FUNC:
  312. printtype(t, t->type, indent);
  313. printf("()");
  314. break;
  315. case TYPE:
  316. if (t->name != nil) {
  317. printf("%s", symname(t));
  318. } else {
  319. printtype(t, t->type, indent);
  320. }
  321. break;
  322. case TYPEREF:
  323. printf("@%s", symname(t));
  324. break;
  325. case SCAL:
  326. printf("enum ");
  327. if (s->name != nil and s->class == TAG) {
  328. printf("%s ", symname(s));
  329. }
  330. printf("{ ");
  331. i = t->chain;
  332. if (i != nil) {
  333. for (;;) {
  334. printf("%s", symname(i));
  335. i = i->chain;
  336. if (i == nil) break;
  337. printf(", ");
  338. }
  339. }
  340. printf(" }");
  341. break;
  342. case TAG:
  343. if (t->type == nil) {
  344. printf("unresolved tag %s", symname(t));
  345. } else {
  346. i = rtype(t->type);
  347. printf("%s %s", c_classname(i), symname(t));
  348. }
  349. break;
  350. default:
  351. printf("(class %d)", t->class);
  352. break;
  353. }
  354. }
  355. /*
  356. * List the parameters of a procedure or function.
  357. * No attempt is made to combine like types.
  358. */
  359. public c_listparams(s)
  360. Symbol s;
  361. {
  362. register Symbol t;
  363. putchar('(');
  364. for (t = s->chain; t != nil; t = t->chain) {
  365. printf("%s", symname(t));
  366. if (t->chain != nil) {
  367. printf(", ");
  368. }
  369. }
  370. putchar(')');
  371. if (s->chain != nil) {
  372. printf("\n");
  373. for (t = s->chain; t != nil; t = t->chain) {
  374. if (t->class != VAR) {
  375. panic("unexpected class %d for parameter", t->class);
  376. }
  377. printdecl(t, 0);
  378. }
  379. } else {
  380. putchar('\n');
  381. }
  382. }
  383. /*
  384. * Print out the value on the top of the expression stack
  385. * in the format for the type of the given symbol.
  386. */
  387. public c_printval(s)
  388. Symbol s;
  389. {
  390. register Symbol t;
  391. register Address a;
  392. register int i, len;
  393. switch (s->class) {
  394. case CONST:
  395. case TYPE:
  396. case VAR:
  397. case REF:
  398. case FVAR:
  399. case TAG:
  400. c_printval(s->type);
  401. break;
  402. case FIELD:
  403. if (isbitfield(s)) {
  404. len = s->symvalue.field.length;
  405. if (len <= BITSPERBYTE) {
  406. i = pop(char);
  407. } else if (len <= sizeof(short)*BITSPERBYTE) {
  408. i = pop(short);
  409. } else {
  410. i = pop(long);
  411. }
  412. i >>= (s->symvalue.field.offset mod BITSPERBYTE);
  413. i &= ((1 << len) - 1);
  414. t = rtype(s->type);
  415. if (t->class == SCAL) {
  416. printenum(i, t);
  417. } else {
  418. printrange(i, t);
  419. }
  420. } else {
  421. c_printval(s->type);
  422. }
  423. break;
  424. case ARRAY:
  425. t = rtype(s->type);
  426. if (t->class == RANGE and istypename(t->type, "char")) {
  427. len = size(s);
  428. sp -= len;
  429. printf("\"%.*s\"", len, sp);
  430. } else {
  431. printarray(s);
  432. }
  433. break;
  434. case RECORD:
  435. c_printstruct(s);
  436. break;
  437. case RANGE:
  438. if (istypename(s->type, "boolean")) {
  439. printrange(popsmall(s), s);
  440. } else if (istypename(s->type, "char")) {
  441. printrange(pop(char), s);
  442. } else if (isdouble(s)) {
  443. switch (s->symvalue.rangev.lower) {
  444. case sizeof(float):
  445. prtreal(pop(float));
  446. break;
  447. case sizeof(double):
  448. prtreal(pop(double));
  449. break;
  450. default:
  451. panic("bad real size %d", t->symvalue.rangev.lower);
  452. break;
  453. }
  454. } else {
  455. printrange(popsmall(s), s);
  456. }
  457. break;
  458. case PTR:
  459. t = rtype(s->type);
  460. a = pop(Address);
  461. if (a == 0) {
  462. printf("(nil)");
  463. } else if (t->class == RANGE and istypename(t->type, "char")) {
  464. printstring(a);
  465. } else {
  466. printf("0x%x", a);
  467. }
  468. break;
  469. case SCAL:
  470. i = pop(Integer);
  471. printenum(i, s);
  472. break;
  473. default:
  474. if (ord(s->class) > ord(TYPEREF)) {
  475. panic("printval: bad class %d", ord(s->class));
  476. }
  477. sp -= size(s);
  478. printf("<%s>", c_classname(s));
  479. break;
  480. }
  481. }
  482. /*
  483. * Print out a C structure.
  484. */
  485. private c_printstruct(s)
  486. Symbol s;
  487. {
  488. register Symbol f;
  489. register Stack *savesp;
  490. register Integer n, off, len;
  491. sp -= size(s);
  492. savesp = sp;
  493. printf("(");
  494. f = s->chain;
  495. for (;;) {
  496. off = f->symvalue.field.offset;
  497. len = f->symvalue.field.length;
  498. n = (off + len + 7) div BITSPERBYTE;
  499. sp += n;
  500. printf("%s = ", symname(f));
  501. c_printval(f);
  502. sp = savesp;
  503. f = f->chain;
  504. if (f == nil) break;
  505. printf(", ");
  506. }
  507. printf(")");
  508. }
  509. /*
  510. * Print out a range type (integer, char, or boolean).
  511. */
  512. private printrange(i, t)
  513. Integer i;
  514. register Symbol t;
  515. {
  516. if (istypename(t->type, "boolean")) {
  517. printf(((Boolean) i) == true ? "true" : "false");
  518. } else if (istypename(t->type, "char")) {
  519. putchar('\'');
  520. printchar(i);
  521. putchar('\'');
  522. } else if (t->symvalue.rangev.lower >= 0) {
  523. printf("%lu", i);
  524. } else {
  525. printf("%ld", i);
  526. }
  527. }
  528. /*
  529. * Print out a null-terminated string (pointer to char)
  530. * starting at the given address.
  531. */
  532. private printstring(addr)
  533. Address addr;
  534. {
  535. register Address a;
  536. register Integer i, len;
  537. register Boolean endofstring;
  538. union {
  539. char ch[sizeof(Word)];
  540. int word;
  541. } u;
  542. putchar('"');
  543. a = addr;
  544. endofstring = false;
  545. while (not endofstring) {
  546. dread(&u, a, sizeof(u));
  547. i = 0;
  548. do {
  549. if (u.ch[i] == '\0') {
  550. endofstring = true;
  551. } else {
  552. printchar(u.ch[i]);
  553. }
  554. ++i;
  555. } while (i < sizeof(Word) and not endofstring);
  556. a += sizeof(Word);
  557. }
  558. putchar('"');
  559. }
  560. /*
  561. * Print out an enumerated value by finding the corresponding
  562. * name in the enumeration list.
  563. */
  564. private printenum(i, t)
  565. Integer i;
  566. Symbol t;
  567. {
  568. register Symbol e;
  569. e = t->chain;
  570. while (e != nil and e->symvalue.iconval != i) {
  571. e = e->chain;
  572. }
  573. if (e != nil) {
  574. printf("%s", symname(e));
  575. } else {
  576. printf("%d", i);
  577. }
  578. }
  579. /*
  580. * Return the C name for the particular class of a symbol.
  581. */
  582. public String c_classname(s)
  583. Symbol s;
  584. {
  585. String str;
  586. switch (s->class) {
  587. case RECORD:
  588. str = "struct";
  589. break;
  590. case VARNT:
  591. str = "union";
  592. break;
  593. case SCAL:
  594. str = "enum";
  595. break;
  596. default:
  597. str = classname(s);
  598. }
  599. return str;
  600. }
  601. public Node c_buildaref(a, slist)
  602. Node a, slist;
  603. {
  604. register Symbol t;
  605. register Node p;
  606. Symbol etype, atype, eltype;
  607. Node esub, r;
  608. r = a;
  609. t = rtype(a->nodetype);
  610. eltype = t->type;
  611. if (t->class == PTR) {
  612. p = slist->value.arg[0];
  613. if (not compatible(p->nodetype, t_int)) {
  614. beginerrmsg();
  615. fprintf(stderr, "bad type for subscript of ");
  616. prtree(stderr, a);
  617. enderrmsg();
  618. }
  619. r = build(O_MUL, p, build(O_LCON, (long) size(eltype)));
  620. r = build(O_ADD, build(O_RVAL, a), r);
  621. r->nodetype = eltype;
  622. } else if (t->class != ARRAY) {
  623. beginerrmsg();
  624. prtree(stderr, a);
  625. fprintf(stderr, " is not an array");
  626. enderrmsg();
  627. } else {
  628. p = slist;
  629. t = t->chain;
  630. for (; p != nil and t != nil; p = p->value.arg[1], t = t->chain) {
  631. esub = p->value.arg[0];
  632. etype = rtype(esub->nodetype);
  633. atype = rtype(t);
  634. if (not compatible(atype, etype)) {
  635. beginerrmsg();
  636. fprintf(stderr, "subscript ");
  637. prtree(stderr, esub);
  638. fprintf(stderr, " is the wrong type");
  639. enderrmsg();
  640. }
  641. r = build(O_INDEX, r, esub);
  642. r->nodetype = eltype;
  643. }
  644. if (p != nil or t != nil) {
  645. beginerrmsg();
  646. if (p != nil) {
  647. fprintf(stderr, "too many subscripts for ");
  648. } else {
  649. fprintf(stderr, "not enough subscripts for ");
  650. }
  651. prtree(stderr, a);
  652. enderrmsg();
  653. }
  654. }
  655. return r;
  656. }
  657. /*
  658. * Evaluate a subscript index.
  659. */
  660. public int c_evalaref(s, i)
  661. Symbol s;
  662. long i;
  663. {
  664. long lb, ub;
  665. s = rtype(s)->chain;
  666. lb = s->symvalue.rangev.lower;
  667. ub = s->symvalue.rangev.upper;
  668. if (i < lb or i > ub) {
  669. error("subscript out of range");
  670. }
  671. return (i - lb);
  672. }