code.c 14 KB

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