code.c 12 KB

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