code.c 15 KB

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