code.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  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".
  4. */
  5. /* $Header$ */
  6. /* C O D E - G E N E R A T I N G R O U T I N E S */
  7. #include "lint.h"
  8. #ifndef LINT
  9. #include <em.h>
  10. #else
  11. #include "em_lint.h"
  12. #include "l_lint.h"
  13. #endif LINT
  14. #include "botch_free.h"
  15. #include <alloc.h>
  16. #include "dataflow.h"
  17. #include "use_tmp.h"
  18. #include "arith.h"
  19. #include "type.h"
  20. #include "idf.h"
  21. #include "label.h"
  22. #include "code.h"
  23. #include "stmt.h"
  24. #include "def.h"
  25. #include "expr.h"
  26. #include "sizes.h"
  27. #include "stack.h"
  28. #include "level.h"
  29. #include "decspecs.h"
  30. #include "declar.h"
  31. #include "Lpars.h"
  32. #include "specials.h"
  33. #include "atw.h"
  34. #include "assert.h"
  35. #include "noRoption.h"
  36. #include "file_info.h"
  37. label lab_count = 1;
  38. label datlab_count = 1;
  39. #ifndef NOFLOAT
  40. int fp_used;
  41. #endif NOFLOAT
  42. /* global function info */
  43. char *func_name;
  44. struct type *func_type;
  45. int func_notypegiven;
  46. #ifdef USE_TMP
  47. static int tmp_id;
  48. static int pro_id;
  49. #endif USE_TMP
  50. extern char options[];
  51. extern char *symbol2str();
  52. #ifndef LINT
  53. init_code(dst_file)
  54. char *dst_file;
  55. {
  56. /* init_code() initialises the output file on which the
  57. compact EM code is written
  58. */
  59. C_init(word_size, pointer_size); /* initialise EM module */
  60. if (C_open(dst_file) == 0)
  61. fatal("cannot write to %s\n", dst_file);
  62. C_magic();
  63. C_ms_emx(word_size, pointer_size);
  64. #ifdef USE_TMP
  65. #ifdef PREPEND_SCOPES
  66. C_insertpart(tmp_id = C_getid());
  67. #endif USE_TMP
  68. #endif PREPEND_SCOPES
  69. }
  70. #endif LINT
  71. struct string_cst *str_list = 0;
  72. code_string(val, len, dlb)
  73. char *val;
  74. int len;
  75. label dlb;
  76. {
  77. register struct string_cst *sc = new_string_cst();
  78. C_ina_dlb(dlb);
  79. sc->next = str_list;
  80. str_list = sc;
  81. sc->sc_value = val;
  82. sc->sc_len = len;
  83. sc->sc_dlb = dlb;
  84. }
  85. def_strings(sc)
  86. register struct string_cst *sc;
  87. {
  88. while (sc) {
  89. struct string_cst *sc1 = sc;
  90. C_df_dlb(sc->sc_dlb);
  91. str_cst(sc->sc_value, sc->sc_len);
  92. sc = sc->next;
  93. free_string_cst(sc1);
  94. }
  95. }
  96. #ifndef LINT
  97. end_code()
  98. {
  99. /* end_code() performs the actions to be taken when closing
  100. the output stream.
  101. */
  102. #ifndef NOFLOAT
  103. if (fp_used) {
  104. /* floating point used */
  105. C_ms_flt();
  106. }
  107. #endif NOFLOAT
  108. def_strings(str_list);
  109. str_list = 0;
  110. C_ms_src((int)(LineNumber - 2), FileName);
  111. C_close();
  112. }
  113. #endif LINT
  114. #ifdef PREPEND_SCOPES
  115. prepend_scopes()
  116. {
  117. /* prepend_scopes() runs down the list of global idf's
  118. and generates those exa's, exp's, ina's and inp's
  119. that superior hindsight has provided.
  120. */
  121. register struct stack_entry *se = local_level->sl_entry;
  122. #ifdef USE_TMP
  123. C_beginpart(tmp_id);
  124. #endif USE_TMP
  125. while (se != 0) {
  126. register struct idf *id = se->se_idf;
  127. register struct def *df = id->id_def;
  128. if (df && (df->df_initialized || df->df_used || df->df_alloc))
  129. code_scope(id->id_text, df);
  130. se = se->next;
  131. }
  132. #ifdef USE_TMP
  133. C_endpart(tmp_id);
  134. #endif USE_TMP
  135. }
  136. #endif PREPEND_SCOPES
  137. code_scope(text, def)
  138. char *text;
  139. register struct def *def;
  140. {
  141. /* generates code for one name, text, of the storage class
  142. as given by def, if meaningful.
  143. */
  144. int fund = def->df_type->tp_fund;
  145. switch (def->df_sc) {
  146. case EXTERN:
  147. case GLOBAL:
  148. case IMPLICIT:
  149. if (fund == FUNCTION)
  150. C_exp(text);
  151. else
  152. C_exa_dnam(text);
  153. break;
  154. case STATIC:
  155. if (fund == FUNCTION)
  156. C_inp(text);
  157. else
  158. C_ina_dnam(text);
  159. break;
  160. }
  161. }
  162. static label return_label, return2_label;
  163. static char return_expr_occurred;
  164. static arith func_size;
  165. static label func_res_label;
  166. static char *last_fn_given = "";
  167. static label file_name_label;
  168. begin_proc(ds, idf) /* to be called when entering a procedure */
  169. struct decspecs *ds;
  170. struct idf *idf;
  171. {
  172. /* begin_proc() is called at the entrance of a new function
  173. and performs the necessary code generation:
  174. - a scope indicator (if needed) exp/inp
  175. - the procedure entry pro $name
  176. - reserves some space if the result of the function
  177. does not fit in the return area
  178. - a fil pseudo instruction
  179. */
  180. register char *name = idf->id_text;
  181. register struct def *def = idf->id_def;
  182. while (def->df_level > L_GLOBAL) def = def->next;
  183. /* idf->id_def does not indicate the right def structure
  184. when the function being defined has a parameter of the
  185. same name.
  186. */
  187. #ifndef PREPEND_SCOPES
  188. code_scope(name, def);
  189. #endif PREPEND_SCOPES
  190. #ifdef DATAFLOW
  191. if (options['d'])
  192. DfaStartFunction(name);
  193. #endif DATAFLOW
  194. /* set global function info */
  195. func_name = name;
  196. if (def->df_type->tp_fund != FUNCTION) {
  197. error("making function body for non-function");
  198. func_type = error_type;
  199. }
  200. else {
  201. func_type = def->df_type->tp_up;
  202. }
  203. func_notypegiven = ds->ds_notypegiven;
  204. func_size = ATW(func_type->tp_size);
  205. #ifndef USE_TMP
  206. C_pro_narg(name);
  207. #else
  208. C_insertpart(pro_id = C_getid());
  209. #endif
  210. if (is_struct_or_union(func_type->tp_fund)) {
  211. C_df_dlb(func_res_label = data_label());
  212. C_bss_cst(func_size, (arith)0, 1);
  213. }
  214. else
  215. func_res_label = 0;
  216. /* Special arrangements if the function result doesn't fit in
  217. the function return area of the EM machine. The size of
  218. the function return area is implementation dependent.
  219. */
  220. lab_count = (label) 1;
  221. return_label = text_label();
  222. return2_label = text_label();
  223. return_expr_occurred = 0;
  224. LocalInit();
  225. prc_entry(name);
  226. if (! options['L']) { /* profiling */
  227. if (strcmp(last_fn_given, FileName) != 0) {
  228. /* previous function came from other file */
  229. C_df_dlb(file_name_label = data_label());
  230. C_con_scon(last_fn_given = FileName,
  231. (arith)(strlen(FileName) + 1));
  232. }
  233. /* enable debug trace of EM source */
  234. C_fil_dlb(file_name_label, (arith)0);
  235. C_lin((arith)LineNumber);
  236. }
  237. }
  238. end_proc(fbytes)
  239. arith fbytes;
  240. {
  241. /* end_proc() deals with the code to be generated at the end of
  242. a function, as there is:
  243. - the EM ret instruction: "ret 0"
  244. - loading of the function result in the function
  245. result area if there has been a return <expr>
  246. in the function body (see do_return_expr())
  247. - indication of the use of floating points
  248. - indication of the number of bytes used for
  249. formal parameters
  250. - use of special identifiers such as "setjmp"
  251. - "end" + number of bytes used for local variables
  252. */
  253. arith nbytes;
  254. char optionsn = options['n'];
  255. #ifdef DATAFLOW
  256. if (options['d'])
  257. DfaEndFunction();
  258. #endif DATAFLOW
  259. C_df_ilb(return2_label);
  260. if (return_expr_occurred) C_asp(-func_size);
  261. C_df_ilb(return_label);
  262. prc_exit();
  263. #ifndef LINT
  264. if (return_expr_occurred) {
  265. if (func_res_label != 0) {
  266. C_lae_dlb(func_res_label, (arith)0);
  267. store_block(func_size, func_type->tp_align);
  268. C_lae_dlb(func_res_label, (arith)0);
  269. C_ret(pointer_size);
  270. }
  271. else
  272. C_ret(func_size);
  273. }
  274. else C_ret((arith) 0);
  275. #endif LINT
  276. /* getting the number of "local" bytes is posponed until here,
  277. because copying the function result in "func_res_label" may
  278. need temporaries! However, local_level is now L_FORMAL2, because
  279. L_LOCAL is already unstacked. Therefore, "unstack_level" must
  280. also pass "sl_max_block" to the level above L_LOCAL.
  281. */
  282. nbytes = ATW(- local_level->sl_max_block);
  283. #ifdef USE_TMP
  284. C_beginpart(pro_id);
  285. C_pro(func_name, nbytes);
  286. #endif
  287. if (fbytes > max_int) {
  288. error("%s has more than %ld parameter bytes",
  289. func_name, (long) max_int);
  290. }
  291. C_ms_par(fbytes); /* # bytes for formals */
  292. if (sp_occurred[SP_SETJMP]) { /* indicate use of "setjmp" */
  293. options['n'] = 1;
  294. C_ms_gto();
  295. sp_occurred[SP_SETJMP] = 0;
  296. }
  297. #ifdef USE_TMP
  298. C_endpart(pro_id);
  299. #endif
  300. LocalFinish();
  301. C_end(nbytes);
  302. if (nbytes > max_int) {
  303. error("%s has more than %ld bytes of local variables",
  304. func_name, (long) max_int);
  305. }
  306. options['n'] = optionsn;
  307. }
  308. do_return()
  309. {
  310. /* do_return handles the case of a return without expression.
  311. This version branches to the return label, which is
  312. probably smarter than generating a direct return.
  313. Return sequences may be expensive.
  314. */
  315. C_bra(return2_label);
  316. }
  317. do_return_expr(expr)
  318. struct expr *expr;
  319. {
  320. /* do_return_expr() generates the expression and the jump for
  321. a return statement with an expression.
  322. */
  323. ch7cast(&expr, RETURN, func_type);
  324. code_expr(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  325. C_bra(return_label);
  326. return_expr_occurred = 1;
  327. }
  328. code_declaration(idf, expr, lvl, sc)
  329. register struct idf *idf; /* idf to be declared */
  330. struct expr *expr; /* initialisation; NULL if absent */
  331. int lvl; /* declaration level */
  332. int sc; /* storage class, as in the declaration */
  333. {
  334. /* code_declaration() does the actual declaration of the
  335. variable indicated by "idf" on declaration level "lvl".
  336. If the variable is initialised, the expression is given
  337. in "expr", but for global and static initialisations it
  338. is just non-zero, as the expression is not parsed yet.
  339. There are some cases to be considered:
  340. - filter out typedefs, they don't correspond to code;
  341. - global variables, coded only if initialized;
  342. - local static variables;
  343. - local automatic variables;
  344. Since the expression may be modified in the process,
  345. code_declaration() frees it after use, as the caller can
  346. no longer do so.
  347. If there is a storage class indication (EXTERN/STATIC),
  348. code_declaration() will generate an exa or ina.
  349. The sc is the actual storage class, as given in the
  350. declaration. This is to allow:
  351. extern int a;
  352. int a = 5;
  353. while at the same time forbidding
  354. extern int a = 5;
  355. */
  356. register struct def *def = idf->id_def;
  357. register arith size = def->df_type->tp_size;
  358. int def_sc = def->df_sc;
  359. if (def_sc == TYPEDEF) /* no code for typedefs */
  360. return;
  361. if (sc == EXTERN && expr && !is_anon_idf(idf))
  362. error("%s is extern; cannot initialize", idf->id_text);
  363. if (lvl == L_GLOBAL) { /* global variable */
  364. /* is this an allocating declaration? */
  365. if ( (sc == 0 || sc == STATIC)
  366. && def->df_type->tp_fund != FUNCTION
  367. && size >= 0
  368. )
  369. def->df_alloc = ALLOC_SEEN;
  370. if (expr) { /* code only if initialized */
  371. #ifndef PREPEND_SCOPES
  372. code_scope(idf->id_text, def);
  373. #endif PREPEND_SCOPES
  374. def->df_alloc = ALLOC_DONE;
  375. C_df_dnam(idf->id_text);
  376. }
  377. }
  378. else
  379. if (lvl >= L_LOCAL) { /* local variable */
  380. /* STATIC, EXTERN, GLOBAL, IMPLICIT, AUTO or REGISTER */
  381. switch (def_sc) {
  382. case STATIC:
  383. if (def->df_type->tp_fund == FUNCTION) {
  384. /* should produce "inp $function" ??? */
  385. break;
  386. }
  387. /* they are handled on the spot and get an
  388. integer label in EM.
  389. */
  390. C_df_dlb((label)def->df_address);
  391. if (expr) { /* there is an initialisation */
  392. }
  393. else { /* produce blank space */
  394. if (size <= 0) {
  395. error("size of %s unknown", idf->id_text);
  396. size = (arith)0;
  397. }
  398. C_bss_cst(ATW(size), (arith)0, 1);
  399. }
  400. break;
  401. case EXTERN:
  402. case GLOBAL:
  403. case IMPLICIT:
  404. /* we are sure there is no expression */
  405. break;
  406. case AUTO:
  407. case REGISTER:
  408. if (expr)
  409. loc_init(expr, idf);
  410. break;
  411. default:
  412. crash("bad local storage class");
  413. /*NOTREACHED*/
  414. }
  415. }
  416. }
  417. loc_init(expr, id)
  418. struct expr *expr;
  419. register struct idf *id;
  420. {
  421. /* loc_init() generates code for the assignment of
  422. expression expr to the local variable described by id.
  423. It frees the expression afterwards.
  424. */
  425. register struct expr *e = expr;
  426. register struct type *tp = id->id_def->df_type;
  427. ASSERT(id->id_def->df_sc != STATIC);
  428. switch (tp->tp_fund) {
  429. case ARRAY:
  430. case STRUCT:
  431. case UNION:
  432. error("automatic %s cannot be initialized in declaration",
  433. symbol2str(tp->tp_fund));
  434. free_expression(e);
  435. return;
  436. }
  437. if (ISCOMMA(e)) { /* embraced: int i = {12}; */
  438. #ifndef NOROPTION
  439. if (options['R']) {
  440. if (ISCOMMA(e->OP_LEFT)) /* int i = {{1}} */
  441. expr_error(e, "extra braces not allowed");
  442. else
  443. if (e->OP_RIGHT != 0) /* int i = {1 , 2} */
  444. expr_error(e, "too many initializers");
  445. }
  446. #endif NOROPTION
  447. while (e) {
  448. loc_init(e->OP_LEFT, id);
  449. e = e->OP_RIGHT;
  450. }
  451. }
  452. else { /* not embraced */
  453. ch7cast(&expr, '=', tp); /* may modify expr */
  454. #ifndef LINT
  455. {
  456. struct value vl;
  457. EVAL(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  458. vl.vl_class = Name;
  459. vl.vl_data.vl_idf = id;
  460. vl.vl_value = (arith)0;
  461. store_val(&vl, tp);
  462. }
  463. #else LINT
  464. id->id_def->df_set = 1;
  465. #endif LINT
  466. free_expression(expr);
  467. }
  468. }
  469. bss(idf)
  470. register struct idf *idf;
  471. {
  472. /* bss() allocates bss space for the global idf.
  473. */
  474. arith size = idf->id_def->df_type->tp_size;
  475. #ifndef PREPEND_SCOPES
  476. code_scope(idf->id_text, idf->id_def);
  477. #endif PREPEND_SCOPES
  478. /* Since bss() is only called if df_alloc is non-zero, and
  479. since df_alloc is only non-zero if size >= 0, we have:
  480. */
  481. /* but we already gave a warning at the declaration of the
  482. array. Besides, the message given here does not apply to
  483. voids
  484. if (options['R'] && size == 0)
  485. warning("actual array of size 0");
  486. */
  487. C_df_dnam(idf->id_text);
  488. C_bss_cst(ATW(size), (arith)0, 1);
  489. }
  490. formal_cvt(df)
  491. register struct def *df;
  492. {
  493. /* formal_cvt() converts a formal parameter of type char or
  494. short from int to that type.
  495. */
  496. register struct type *tp = df->df_type;
  497. if (tp->tp_size != int_size &&
  498. (tp->tp_fund == CHAR || tp->tp_fund == SHORT)
  499. ) {
  500. LoadLocal(df->df_address, int_size);
  501. /* conversion(int_type, df->df_type); ???
  502. No, you can't do this on the stack! (CJ)
  503. */
  504. StoreLocal(df->df_address, tp->tp_size);
  505. }
  506. }
  507. #ifdef LINT
  508. /*ARGSUSED*/
  509. #endif LINT
  510. code_expr(expr, val, code, tlbl, flbl)
  511. struct expr *expr;
  512. label tlbl, flbl;
  513. {
  514. /* code_expr() is the parser's interface to the expression code
  515. generator. If line number trace is wanted, it generates a
  516. lin instruction. EVAL() is called directly.
  517. */
  518. #ifndef LINT
  519. if (! options['L']) /* profiling */
  520. C_lin((arith)(expr->ex_line));
  521. EVAL(expr, val, code, tlbl, flbl);
  522. #else LINT
  523. lint_expr(expr, code ? USED : IGNORED);
  524. #endif LINT
  525. }
  526. /* The FOR/WHILE/DO/SWITCH stacking mechanism:
  527. stack_stmt() has to be called at the entrance of a
  528. for, while, do or switch statement to indicate the
  529. EM labels where a subsequent break or continue causes
  530. the program to jump to.
  531. */
  532. static struct stmt_block *stmt_stack; /* top of statement stack */
  533. /* code_break() generates EM code needed at the occurrence of "break":
  534. it generates a branch instruction to the break label of the
  535. innermost statement in which break has a meaning.
  536. As "break" is legal in any of 'while', 'do', 'for' or 'switch',
  537. which are the only ones that are stacked, only the top of
  538. the stack is interesting.
  539. */
  540. code_break()
  541. {
  542. register struct stmt_block *stmt_block = stmt_stack;
  543. if (stmt_block)
  544. C_bra(stmt_block->st_break);
  545. else
  546. error("break not inside for, while, do or switch");
  547. }
  548. /* code_continue() generates EM code needed at the occurrence of
  549. "continue":
  550. it generates a branch instruction to the continue label of the
  551. innermost statement in which continue has a meaning.
  552. */
  553. code_continue()
  554. {
  555. register struct stmt_block *stmt_block = stmt_stack;
  556. while (stmt_block) {
  557. if (stmt_block->st_continue) {
  558. C_bra(stmt_block->st_continue);
  559. return;
  560. }
  561. stmt_block = stmt_block->next;
  562. }
  563. error("continue not inside for, while or do");
  564. }
  565. stack_stmt(break_label, cont_label)
  566. label break_label, cont_label;
  567. {
  568. register struct stmt_block *stmt_block = new_stmt_block();
  569. stmt_block->next = stmt_stack;
  570. stmt_block->st_break = break_label;
  571. stmt_block->st_continue = cont_label;
  572. stmt_stack = stmt_block;
  573. }
  574. unstack_stmt()
  575. {
  576. /* unstack_stmt() unstacks the data of a statement
  577. which may contain break or continue
  578. */
  579. register struct stmt_block *sbp = stmt_stack;
  580. stmt_stack = sbp->next;
  581. free_stmt_block(sbp);
  582. }
  583. static label prc_name;
  584. prc_entry(name)
  585. char *name;
  586. {
  587. if (options['p']) {
  588. C_df_dlb(prc_name = data_label());
  589. C_rom_scon(name, (arith) (strlen(name) + 1));
  590. C_lae_dlb(prc_name, (arith) 0);
  591. C_cal("procentry");
  592. C_asp(pointer_size);
  593. }
  594. }
  595. prc_exit()
  596. {
  597. if (options['p']) {
  598. C_lae_dlb(prc_name, (arith) 0);
  599. C_cal("procexit");
  600. C_asp(pointer_size);
  601. }
  602. }