code.c 13 KB

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