code.c 14 KB

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