code.c 12 KB

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