code.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. /* $Id$ */
  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 <stdlib.h>
  8. #include <string.h>
  9. #include "lint.h"
  10. #include "debug.h"
  11. #include "dbsymtab.h"
  12. #ifndef LINT
  13. #include <em.h>
  14. #else
  15. #include "l_em.h"
  16. #include "l_lint.h"
  17. #endif /* LINT */
  18. #include "botch_free.h"
  19. #include <alloc.h>
  20. #include "dataflow.h"
  21. #include "use_tmp.h"
  22. #include <flt_arith.h>
  23. #include "arith.h"
  24. #include "type.h"
  25. #include "idf.h"
  26. #include "label.h"
  27. #include "code.h"
  28. #include "stmt.h"
  29. #include "def.h"
  30. #include "expr.h"
  31. #include "sizes.h"
  32. #include "stack.h"
  33. #include "level.h"
  34. #include "decspecs.h"
  35. #include "declar.h"
  36. #include "Lpars.h"
  37. #include "specials.h"
  38. #include "atw.h"
  39. #include "assert.h"
  40. #include "LLlex.h"
  41. #include "align.h"
  42. #include "blocks.h"
  43. #include "code_c.h"
  44. #include "conversion.h"
  45. #include "util_loc.h"
  46. #include "stab.h"
  47. #include "error.h"
  48. #include "struct_loc.h"
  49. #include "ch3.h"
  50. #include "idf_loc.h"
  51. #include "expr_loc.h"
  52. #include "ival.h"
  53. #include "eval.h"
  54. #include <symbol2str.h>
  55. #ifdef LINT
  56. #include "l_lint.h"
  57. #endif /* LINT */
  58. #ifdef DBSYMTAB
  59. #include <stb.h>
  60. #endif /* DBSYMTAB */
  61. label lab_count = 1;
  62. label datlab_count = 1;
  63. int fp_used;
  64. /* global function info */
  65. char *func_name;
  66. struct type *func_type;
  67. #ifdef LINT
  68. int func_notypegiven;
  69. #endif
  70. #ifdef USE_TMP
  71. static int tmp_id;
  72. static int pro_id;
  73. #endif /* USE_TMP */
  74. extern char options[];
  75. extern char *source;
  76. static void def_strings(struct string_cst *sc);
  77. static void prc_entry(char *name);
  78. static void prc_exit();
  79. #ifndef LINT
  80. void init_code(char *dst_file)
  81. {
  82. /* init_code() initialises the output file on which the
  83. compact EM code is written
  84. */
  85. C_init(word_size, pointer_size); /* initialise EM module */
  86. if (C_open(dst_file) == 0)
  87. fatal("cannot write to %s\n", dst_file);
  88. C_magic();
  89. C_ms_emx(word_size, pointer_size);
  90. #ifdef DBSYMTAB
  91. if (options['g']) {
  92. C_ms_std(source, N_SO, 0);
  93. stb_typedef(int_type, "int");
  94. stb_typedef(schar_type, "char");
  95. stb_typedef(long_type, "long");
  96. stb_typedef(short_type, "short");
  97. stb_typedef(uchar_type, "unsigned char");
  98. stb_typedef(ushort_type, "unsigned short");
  99. stb_typedef(ulong_type, "unsigned long");
  100. stb_typedef(uint_type, "unsigned int");
  101. stb_typedef(float_type, "float");
  102. stb_typedef(double_type, "double");
  103. stb_typedef(lngdbl_type, "long double");
  104. stb_typedef(void_type, "void");
  105. }
  106. #endif /* DBSYMTAB */
  107. #ifdef USE_TMP
  108. #ifdef PREPEND_SCOPES
  109. C_insertpart(tmp_id = C_getid());
  110. #endif /* PREPEND_SCOPES */
  111. #endif /* USE_TMP */
  112. }
  113. #endif /* LINT */
  114. struct string_cst *str_list = 0;
  115. label code_string(char *val, int len)
  116. {
  117. register struct string_cst *sc = new_string_cst();
  118. label dlb = data_label();
  119. C_ina_dlb(dlb);
  120. sc->next = str_list;
  121. str_list = sc;
  122. sc->sc_value = val;
  123. sc->sc_len = len;
  124. sc->sc_dlb = dlb;
  125. return dlb;
  126. }
  127. static void def_strings(struct string_cst *sc)
  128. {
  129. while (sc) {
  130. struct string_cst *sc1 = sc;
  131. C_df_dlb(sc->sc_dlb);
  132. str_cst(sc->sc_value, sc->sc_len, 1); /* string in rom */
  133. sc = sc->next;
  134. free(sc1->sc_value);
  135. free_string_cst(sc1);
  136. }
  137. }
  138. /* flush_strings() is called from program.g after each external definition */
  139. void flush_strings()
  140. {
  141. if (str_list) {
  142. def_strings(str_list);
  143. str_list = 0;
  144. }
  145. }
  146. #ifndef LINT
  147. void end_code()
  148. {
  149. /* end_code() performs the actions to be taken when closing
  150. the output stream.
  151. */
  152. if (fp_used) {
  153. /* floating point used */
  154. C_ms_flt();
  155. }
  156. C_ms_src((int)(LineNumber - 2), source);
  157. C_close();
  158. }
  159. #endif /* LINT */
  160. #ifdef PREPEND_SCOPES
  161. void prepend_scopes()
  162. {
  163. /* prepend_scopes() runs down the list of global idf's
  164. and generates those exa's, exp's, ina's and inp's
  165. that superior hindsight has provided.
  166. */
  167. struct stack_entry *se = local_level->sl_entry;
  168. #ifdef USE_TMP
  169. C_beginpart(tmp_id);
  170. #endif /* USE_TMP */
  171. while (se != 0) {
  172. struct def *df = se->se_idf->id_def;
  173. if (df && (df->df_initialized || df->df_used || df->df_alloc)) {
  174. code_scope(se->se_idf->id_text, df);
  175. }
  176. se = se->next;
  177. }
  178. #ifdef USE_TMP
  179. C_endpart(tmp_id);
  180. #endif /* USE_TMP */
  181. }
  182. #endif /* PREPEND_SCOPES */
  183. void code_scope(char *text, struct def *def)
  184. {
  185. /* generates code for one name, text, of the storage class
  186. as given by def, if meaningful.
  187. */
  188. int fund = def->df_type->tp_fund;
  189. switch (def->df_sc) {
  190. case EXTERN:
  191. case GLOBAL:
  192. if (fund == FUNCTION)
  193. C_exp(text);
  194. else
  195. C_exa_dnam(text);
  196. break;
  197. case STATIC:
  198. if (fund == FUNCTION)
  199. C_inp(text);
  200. else
  201. C_ina_dnam(text);
  202. break;
  203. }
  204. }
  205. static label return_label, return2_label;
  206. static char return_expr_occurred;
  207. static arith func_size;
  208. static int struct_return;
  209. static char *last_fn_given = (char *)0;
  210. static label file_name_label;
  211. /* to be called when entering a procedure */
  212. void begin_proc(struct decspecs *ds, struct idf *idf)
  213. {
  214. /* begin_proc() is called at the entrance of a new function
  215. and performs the necessary code generation:
  216. - a scope indicator (if needed) exp/inp
  217. - the procedure entry pro $name
  218. - reserves some space if the result of the function
  219. does not fit in the return area
  220. - a fil pseudo instruction
  221. */
  222. char *name = idf->id_text;
  223. struct def *def = idf->id_def;
  224. /* idf->id_def does not indicate the right def structure
  225. * when the function being defined has a parameter of the
  226. * same name in an old-style function definition.
  227. */
  228. while (def->df_level != L_GLOBAL) def = def->next;
  229. /* When we have a new-style function definition, the parameters
  230. * are defined first, which means that it may look as if they have
  231. * the greatest scope. Correct this.
  232. */
  233. if (idf->id_def == def && def->next && def->next->df_level == L_PROTO) {
  234. struct def *tmpdef = def->next;
  235. def->next = tmpdef->next;
  236. tmpdef->next = def;
  237. idf->id_def = tmpdef;
  238. }
  239. #ifndef PREPEND_SCOPES
  240. code_scope(name, def);
  241. #endif /* PREPEND_SCOPES */
  242. #ifdef DATAFLOW
  243. if (options['d'])
  244. DfaStartFunction(name);
  245. #endif /* DATAFLOW */
  246. /* set global function info */
  247. func_name = name;
  248. if (def->df_type->tp_fund != FUNCTION) {
  249. error("making function body for non-function");
  250. def->df_type = error_type;
  251. }
  252. func_type = def->df_type->tp_up;
  253. #ifdef LINT
  254. func_notypegiven = ds->ds_notypegiven;
  255. #endif
  256. func_size = ATW(func_type->tp_size);
  257. sp_occurred[SP_SETJMP] = 0;
  258. #ifndef USE_TMP
  259. C_pro_narg(name);
  260. #else
  261. C_insertpart(pro_id = C_getid());
  262. #endif
  263. if (is_struct_or_union(func_type->tp_fund)) {
  264. if (func_size <= 0) {
  265. error("unknown return type for function %s", name);
  266. } else {
  267. struct_return = 1;
  268. }
  269. }
  270. else
  271. struct_return = 0;
  272. /* Special arrangements if the function result doesn't fit in
  273. the function return area of the EM machine. The size of
  274. the function return area is implementation dependent.
  275. */
  276. lab_count = (label) 1;
  277. return_label = text_label();
  278. return2_label = text_label();
  279. return_expr_occurred = 0;
  280. LocalInit();
  281. prc_entry(name);
  282. if (! options['L']) { /* profiling */
  283. if (!last_fn_given || strcmp(last_fn_given, FileName) != 0) {
  284. /* previous function came from other file */
  285. C_df_dlb(file_name_label = data_label());
  286. C_con_scon(last_fn_given = FileName,
  287. (arith)(strlen(FileName) + 1));
  288. }
  289. /* enable debug trace of EM source */
  290. C_fil_dlb(file_name_label, (arith)0);
  291. C_lin((arith)LineNumber);
  292. }
  293. #ifdef DBSYMTAB
  294. if (options['g']) {
  295. stb_string(def, FUNCTION, name);
  296. if (! strcmp(name, "main")) {
  297. C_ms_stb_cst(name, N_MAIN, 0, (arith) 0);
  298. }
  299. }
  300. #endif
  301. }
  302. void end_proc(arith fbytes)
  303. {
  304. /* end_proc() deals with the code to be generated at the end of
  305. a function, as there is:
  306. - the EM ret instruction: "ret 0"
  307. - loading of the function result in the function
  308. result area if there has been a return <expr>
  309. in the function body (see do_return_expr())
  310. - indication of the use of floating points
  311. - indication of the number of bytes used for
  312. formal parameters
  313. - use of special identifiers such as "__setjmp"
  314. - "end" + number of bytes used for local variables
  315. */
  316. arith nbytes;
  317. char optionsn = options['n'];
  318. #ifdef DATAFLOW
  319. if (options['d'])
  320. DfaEndFunction();
  321. #endif /* DATAFLOW */
  322. C_df_ilb(return2_label);
  323. if (return_expr_occurred && struct_return == 0) {
  324. C_asp(-func_size);
  325. }
  326. C_df_ilb(return_label);
  327. prc_exit();
  328. if (return_expr_occurred) {
  329. if (struct_return != 0) {
  330. LoadLocal((arith) 0, pointer_size);
  331. C_ret(pointer_size);
  332. }
  333. else
  334. C_ret(func_size);
  335. }
  336. else C_ret((arith) 0);
  337. /* getting the number of "local" bytes is posponed until here,
  338. because copying the function result may need temporaries!
  339. However, local_level is now L_FORMAL2, because
  340. L_LOCAL is already unstacked. Therefore, "unstack_level" must
  341. also pass "sl_max_block" to the level above L_LOCAL.
  342. */
  343. nbytes = ATW(- local_level->sl_max_block);
  344. #ifdef USE_TMP
  345. C_beginpart(pro_id);
  346. C_pro(func_name, nbytes);
  347. #endif
  348. if (fbytes > max_int) {
  349. error("%s has more than %ld parameter bytes",
  350. func_name, (long) max_int);
  351. }
  352. C_ms_par(fbytes); /* # bytes for formals */
  353. if (sp_occurred[SP_SETJMP]) { /* indicate use of "__setjmp" */
  354. options['n'] = 1;
  355. C_ms_gto();
  356. sp_occurred[SP_SETJMP] = 0;
  357. }
  358. #ifdef USE_TMP
  359. C_endpart(pro_id);
  360. #endif
  361. LocalFinish();
  362. C_end(nbytes);
  363. if (nbytes > max_int) {
  364. error("%s has more than %ld bytes of local variables",
  365. func_name, (long) max_int);
  366. }
  367. options['n'] = optionsn;
  368. }
  369. void do_return()
  370. {
  371. /* do_return handles the case of a return without expression.
  372. This version branches to the return label, which is
  373. probably smarter than generating a direct return.
  374. Return sequences may be expensive.
  375. */
  376. #ifdef DBSYMTAB
  377. if (options['g']) db_line(dot.tk_file, dot.tk_line);
  378. #endif /* DBSYMTAB */
  379. C_bra(return2_label);
  380. }
  381. void do_return_expr(struct expr *expr)
  382. {
  383. /* do_return_expr() generates the expression and the jump for
  384. a return statement with an expression.
  385. */
  386. ch3cast(&expr, RETURN, func_type);
  387. code_expr(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  388. if (struct_return != 0) {
  389. LoadLocal((arith) 0, pointer_size);
  390. store_block(func_type->tp_size, func_type->tp_align);
  391. }
  392. C_bra(return_label);
  393. return_expr_occurred = 1;
  394. }
  395. /* struct idf *idf idf to be declared
  396. * struct expr *expr initialisation; NULL if absent
  397. * int lvl declaration level
  398. * int sc storage class, as in the declaration
  399. */
  400. void code_declaration(struct idf *idf, struct expr *expr, int lvl, int sc)
  401. {
  402. /* code_declaration() does the actual declaration of the
  403. variable indicated by "idf" on declaration level "lvl".
  404. If the variable is initialised, the expression is given
  405. in "expr", but for global and static initialisations it
  406. is just non-zero, as the expression is not parsed yet.
  407. There are some cases to be considered:
  408. - filter out typedefs, they don't correspond to code;
  409. - global variables, coded only if initialized;
  410. - local static variables;
  411. - local automatic variables;
  412. Since the expression may be modified in the process,
  413. code_declaration() frees it after use, as the caller can
  414. no longer do so.
  415. If there is a storage class indication (EXTERN/STATIC),
  416. code_declaration() will generate an exa or ina.
  417. The sc is the actual storage class, as given in the
  418. declaration.
  419. */
  420. struct def *def = idf->id_def;
  421. arith size = def->df_type->tp_size;
  422. int fund = def->df_type->tp_fund;
  423. int def_sc = def->df_sc;
  424. if (def_sc == TYPEDEF) { /* no code for typedefs */
  425. #ifdef DBSYMTAB
  426. if (options['g']) {
  427. stb_typedef(def->df_type, idf->id_text);
  428. }
  429. #endif /* DBSYMTAB */
  430. return;
  431. }
  432. if (lvl == L_GLOBAL) { /* global variable */
  433. /* is this an allocating declaration? */
  434. if ( (sc == 0 || sc == STATIC)
  435. && fund != FUNCTION
  436. )
  437. def->df_alloc = ALLOC_SEEN;
  438. if (expr && def_sc == STATIC && sc == EXTERN) {
  439. warning("%s has internal linkage", idf->id_text);
  440. }
  441. if (expr) { /* code only if initialized */
  442. #ifndef PREPEND_SCOPES
  443. code_scope(idf->id_text, def);
  444. #endif /* PREPEND_SCOPES */
  445. def->df_alloc = ALLOC_DONE;
  446. C_df_dnam(idf->id_text);
  447. }
  448. }
  449. else
  450. if (lvl >= L_LOCAL) { /* local variable */
  451. /* STATIC, EXTERN, GLOBAL, AUTO or REGISTER */
  452. switch (def_sc) {
  453. case STATIC:
  454. if (fund == FUNCTION) {
  455. /* should produce "inp $function" ??? */
  456. break;
  457. }
  458. /* they are handled on the spot and get an
  459. integer label in EM.
  460. */
  461. #ifdef DBSYMTAB
  462. if (options['g'] && ! expr) {
  463. stb_string(def, sc, idf->id_text);
  464. }
  465. #endif /* DBSYMTAB */
  466. C_df_dlb((label)def->df_address);
  467. if (expr) { /* there is an initialisation */
  468. }
  469. else { /* produce blank space */
  470. if (size <= 0) {
  471. error("size of %s unknown", idf->id_text);
  472. size = (arith)0;
  473. }
  474. C_bss_cst(ATW(size), (arith)0, 1);
  475. }
  476. break;
  477. case EXTERN:
  478. if (expr && !is_anon_idf(idf) && level != L_GLOBAL)
  479. error("cannot initialize extern %s in block"
  480. , idf->id_text);
  481. case GLOBAL:
  482. /* we are sure there is no expression */
  483. break;
  484. case AUTO:
  485. case REGISTER:
  486. #ifdef DBSYMTAB
  487. if (options['g']) {
  488. stb_string(def, sc, idf->id_text);
  489. }
  490. #endif /* DBSYMTAB */
  491. if (expr)
  492. loc_init(expr, idf);
  493. else if ((fund == ARRAY)
  494. && (def->df_type->tp_size == (arith)-1)) {
  495. error("size for local %s unknown"
  496. , idf->id_text);
  497. }
  498. break;
  499. default:
  500. crash("bad local storage class");
  501. /*NOTREACHED*/
  502. }
  503. }
  504. }
  505. void loc_init(struct expr *expr, struct idf *id)
  506. {
  507. /* loc_init() generates code for the assignment of
  508. expression expr to the local variable described by id.
  509. It frees the expression afterwards.
  510. */
  511. struct expr *e = expr;
  512. struct def *df = id->id_def;
  513. struct type *tp = df->df_type;
  514. static arith tmpoffset = 0;
  515. static arith unknownsize = 0;
  516. ASSERT(df->df_sc != STATIC);
  517. switch (tp->tp_fund) {
  518. case ARRAY:
  519. if (tp->tp_size == (arith) -1)
  520. unknownsize = 1;
  521. case STRUCT:
  522. case UNION:
  523. if (e != (struct expr *) 0) {
  524. break; /* switch */
  525. } else if (!tmpoffset) {/* first time for this variable */
  526. tmpoffset = df->df_address;
  527. if (unknownsize) tmpoffset = -1;
  528. df->df_address = data_label();
  529. C_df_dlb((label)df->df_address);
  530. } else {
  531. C_lae_dlb((label)df->df_address, (arith)0);
  532. load_block(tp->tp_size, word_align);
  533. if (unknownsize) {
  534. /* tmpoffset += tp->tp_size; */
  535. unknownsize = 0;
  536. tmpoffset = NewLocal(tp->tp_size
  537. , tp->tp_align
  538. , regtype(tp)
  539. , df->df_sc);
  540. }
  541. C_lal(tmpoffset);
  542. store_block(tp->tp_size, tmpoffset % word_align ? 1 : word_align);
  543. df->df_address = tmpoffset;
  544. tmpoffset = 0;
  545. }
  546. #ifdef DBSYMTAB
  547. if (options['g']) {
  548. stb_string(df, AUTO, id->id_text);
  549. }
  550. #endif /* DBSYMTAB */
  551. return;
  552. }
  553. if (ISCOMMA(e)) { /* embraced: int i = {12}; */
  554. while (e) {
  555. loc_init(e->OP_LEFT, id);
  556. e = e->OP_RIGHT;
  557. }
  558. }
  559. else { /* not embraced */
  560. ch3cast(&expr, '=', tp); /* may modify expr */
  561. #ifndef LINT
  562. {
  563. struct value vl;
  564. EVAL(expr, RVAL, TRUE, NO_LABEL, NO_LABEL);
  565. vl.vl_class = Name;
  566. vl.vl_data.vl_idf = id;
  567. vl.vl_value = (arith)0;
  568. store_val(&vl, tp);
  569. }
  570. #else /* LINT */
  571. id->id_def->df_set = 1;
  572. #endif /* LINT */
  573. free_expression(expr);
  574. }
  575. }
  576. void bss(struct idf *idf)
  577. {
  578. /* bss() allocates bss space for the global idf.
  579. */
  580. struct def *df = idf->id_def;
  581. #ifndef PREPEND_SCOPES
  582. code_scope(idf->id_text, df);
  583. #endif /* PREPEND_SCOPES */
  584. #ifdef DBSYMTAB
  585. if (options['g']) {
  586. stb_string(df, df->df_sc, idf->id_text);
  587. }
  588. #endif /* DBSYMTAB */
  589. if (df->df_type->tp_size <= 0) {
  590. if (df->df_sc != STATIC &&
  591. df->df_type->tp_fund == ARRAY &&
  592. df->df_type->tp_up &&
  593. df->df_type->tp_up->tp_size >= 0) {
  594. C_df_dnam(idf->id_text);
  595. C_bss_cst(ATW(df->df_type->tp_up->tp_size), (arith)0, 1);
  596. }
  597. else error("size of %s unknown (\"%s\", line %d)"
  598. , idf->id_text, df->df_file, df->df_line);
  599. } else {
  600. C_df_dnam(idf->id_text);
  601. C_bss_cst(ATW(df->df_type->tp_size), (arith)0, 1);
  602. }
  603. }
  604. void formal_cvt(int hasproto, struct def *df)
  605. {
  606. /* formal_cvt() converts a formal parameter of type char or
  607. short from int to that type. It also converts a formal
  608. parameter of type float from a double to a float.
  609. */
  610. struct type *tp = df->df_type;
  611. if (tp->tp_size != int_size &&
  612. (tp->tp_fund == CHAR || tp->tp_fund == SHORT)
  613. ) {
  614. LoadLocal(df->df_address, int_size);
  615. /* conversion(int_type, df->df_type); ???
  616. No, you can't do this on the stack! (CJ)
  617. */
  618. StoreLocal(df->df_address, tp->tp_size);
  619. } else if (tp->tp_size != double_size
  620. && tp->tp_fund == FLOAT
  621. && !hasproto) {
  622. LoadLocal(df->df_address, double_size);
  623. #ifndef LINT
  624. conversion(double_type, float_type);
  625. #endif /* LINT */
  626. StoreLocal(df->df_address, tp->tp_size);
  627. }
  628. }
  629. #ifdef LINT
  630. /*ARGSUSED*/
  631. #endif /* LINT */
  632. void code_expr(struct expr *expr, int val, int code, label tlbl, label flbl)
  633. {
  634. /* code_expr() is the parser's interface to the expression code
  635. generator. If line number trace is wanted, it generates a
  636. lin instruction. EVAL() is called directly.
  637. */
  638. #ifndef LINT
  639. if (! options['L']) /* profiling */
  640. C_lin((arith)(expr->ex_line));
  641. #ifdef DBSYMTAB
  642. if (options['g']) db_line(expr->ex_file, (unsigned int)expr->ex_line);
  643. #endif
  644. EVAL(expr, val, code, tlbl, flbl);
  645. #else /* LINT */
  646. lint_expr(expr, code ? USED : IGNORED);
  647. #endif /* LINT */
  648. }
  649. /* The FOR/WHILE/DO/SWITCH stacking mechanism:
  650. stack_stmt() has to be called at the entrance of a
  651. for, while, do or switch statement to indicate the
  652. EM labels where a subsequent break or continue causes
  653. the program to jump to.
  654. */
  655. static struct stmt_block *stmt_stack; /* top of statement stack */
  656. /* code_break() generates EM code needed at the occurrence of "break":
  657. it generates a branch instruction to the break label of the
  658. innermost statement in which break has a meaning.
  659. As "break" is legal in any of 'while', 'do', 'for' or 'switch',
  660. which are the only ones that are stacked, only the top of
  661. the stack is interesting.
  662. */
  663. void code_break()
  664. {
  665. struct stmt_block *stmt_block = stmt_stack;
  666. #ifdef DBSYMTAB
  667. if (options['g']) db_line(dot.tk_file, dot.tk_line);
  668. #endif /* DBSYMTAB */
  669. if (stmt_block)
  670. C_bra(stmt_block->st_break);
  671. else
  672. error("break not inside for, while, do or switch");
  673. }
  674. /* code_continue() generates EM code needed at the occurrence of
  675. "continue":
  676. it generates a branch instruction to the continue label of the
  677. innermost statement in which continue has a meaning.
  678. */
  679. void code_continue()
  680. {
  681. struct stmt_block *stmt_block = stmt_stack;
  682. while (stmt_block) {
  683. if (stmt_block->st_continue) {
  684. #ifdef DBSYMTAB
  685. if (options['g']) db_line(dot.tk_file, dot.tk_line);
  686. #endif /* DBSYMTAB */
  687. C_bra(stmt_block->st_continue);
  688. return;
  689. }
  690. stmt_block = stmt_block->next;
  691. }
  692. error("continue not inside for, while or do");
  693. }
  694. void stack_stmt(label break_label, label cont_label)
  695. {
  696. struct stmt_block *stmt_block = new_stmt_block();
  697. stmt_block->next = stmt_stack;
  698. stmt_block->st_break = break_label;
  699. stmt_block->st_continue = cont_label;
  700. stmt_stack = stmt_block;
  701. }
  702. void unstack_stmt()
  703. {
  704. /* unstack_stmt() unstacks the data of a statement
  705. which may contain break or continue
  706. */
  707. register struct stmt_block *sbp = stmt_stack;
  708. stmt_stack = sbp->next;
  709. free_stmt_block(sbp);
  710. }
  711. static label prc_name;
  712. static void prc_entry(char *name)
  713. {
  714. if (options['p']) {
  715. C_df_dlb(prc_name = data_label());
  716. C_rom_scon(name, (arith) (strlen(name) + 1));
  717. C_lae_dlb(prc_name, (arith) 0);
  718. C_cal("procentry");
  719. C_asp(pointer_size);
  720. }
  721. }
  722. static void prc_exit()
  723. {
  724. if (options['p']) {
  725. C_lae_dlb(prc_name, (arith) 0);
  726. C_cal("procexit");
  727. C_asp(pointer_size);
  728. }
  729. }
  730. #ifdef DBSYMTAB
  731. void db_line(char *file, unsigned int line)
  732. {
  733. static unsigned oldline;
  734. static char *oldfile;
  735. if (file != oldfile || line != oldline) {
  736. C_ms_std((char *) 0, N_SLINE, (int) line);
  737. oldline = line;
  738. oldfile = file;
  739. }
  740. }
  741. #endif /* DBSYMTAB */