il3_change.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* I N L I N E S U B S T I T U T I O N
  7. *
  8. * I L 3 _ C H A N G E . C
  9. */
  10. #include <stdio.h>
  11. #include <em_mnem.h>
  12. #include <em_pseu.h>
  13. #include <em_spec.h>
  14. #include <em_mes.h>
  15. #include "../share/types.h"
  16. #include "il.h"
  17. #include "../share/debug.h"
  18. #include "../share/alloc.h"
  19. #include "../share/global.h"
  20. #include "../share/def.h"
  21. #include "../share/lset.h"
  22. #include "../share/aux.h"
  23. #include "../share/get.h"
  24. #include "../share/put.h"
  25. #include "il_aux.h"
  26. #include "il3_change.h"
  27. #include "il3_aux.h"
  28. /* chg_callseq */
  29. STATIC line_p par_expr(l,expr)
  30. line_p l, expr;
  31. {
  32. /* Find the first line of the expression of which
  33. * l is the last line; expr contains a pointer
  34. * to a copy of that expression; effectively we
  35. * just have to tally lines.
  36. */
  37. line_p lnp;
  38. for (lnp = expr->l_next; lnp != (line_p) 0; lnp = lnp->l_next) {
  39. assert(l != (line_p) 0);
  40. l = PREV(l);
  41. }
  42. return l;
  43. }
  44. STATIC rem_text(l1,l2)
  45. line_p l1,l2;
  46. {
  47. /* Remove the lines from l1 to l2 (inclusive) */
  48. line_p l, lstop;
  49. l = PREV(l1);
  50. lstop = l2->l_next;
  51. while (l->l_next != lstop) {
  52. rem_line(l->l_next);
  53. }
  54. }
  55. STATIC store_tmp(p,l,size)
  56. proc_p p;
  57. line_p l;
  58. offset size;
  59. {
  60. /* Emit code to store a 'size'-byte value in a new
  61. * temporary local variable in the stack frame of p.
  62. * Put this code after line l.
  63. */
  64. line_p lnp;
  65. lnp = int_line(tmplocal(p,size)); /* line with operand temp. */
  66. if (size == ws) {
  67. lnp->l_instr = op_stl; /* STL temp. */
  68. } else {
  69. if (size == 2*ws) {
  70. lnp->l_instr = op_sdl; /* SDL temp. */
  71. } else {
  72. /* emit 'LAL temp; STI size' */
  73. lnp->l_instr = op_lal;
  74. appnd_line(lnp,l);
  75. l = lnp;
  76. assert ((short) size == size);
  77. lnp = newline(OPSHORT);
  78. SHORT(lnp) = size;
  79. lnp->l_instr = op_sti;
  80. }
  81. }
  82. appnd_line(lnp,l);
  83. }
  84. STATIC chg_actuals(c,cal)
  85. call_p c;
  86. line_p cal;
  87. {
  88. /* Change the actual parameter expressions of the call. */
  89. actual_p act;
  90. line_p llast,lfirst,l;
  91. llast = PREV(cal);
  92. for (act = c->cl_actuals; act != (actual_p) 0; act = act->ac_next) {
  93. lfirst = par_expr(llast,act->ac_exp);
  94. /* the code from lfirst to llast is a parameter expression */
  95. if (act->ac_inl) {
  96. /* in line parameter; remove it */
  97. l = llast;
  98. llast = PREV(lfirst);
  99. rem_text(lfirst,l);
  100. } else {
  101. store_tmp(curproc,llast,act->ac_size);
  102. /* put a "STL tmp" -like instruction after the code */
  103. llast = PREV(lfirst);
  104. }
  105. }
  106. }
  107. STATIC rm_callpart(c,cal)
  108. call_p c;
  109. line_p cal;
  110. {
  111. /* Remove the call part, consisting of a CAL,
  112. * an optional ASP and an optional LFR.
  113. */
  114. line_p l;
  115. l= PREV(cal);
  116. rem_line(cal);
  117. if (c->cl_proc->p_nrformals > 0) {
  118. /* called procedure has parameters */
  119. assert (INSTR(l->l_next) == op_asp);
  120. rem_line(l->l_next);
  121. }
  122. if (INSTR(l->l_next) == op_lfr) {
  123. rem_line(l->l_next);
  124. }
  125. }
  126. chg_callseq(c,cal,l_out)
  127. call_p c;
  128. line_p cal,*l_out;
  129. {
  130. /* Change the calling sequence. The actual parameter
  131. * expressions are changed (in line parameters are
  132. * removed, all other ones now store their result
  133. * in a temporary local of the caller);
  134. * the sequence "CAL ; ASP ; LFR" is removed.
  135. */
  136. chg_actuals(c,cal);
  137. *l_out = PREV(cal); /* last instr. of new parameter part */
  138. rm_callpart(c,cal);
  139. }
  140. /* make_label */
  141. line_p make_label(l,p)
  142. line_p l;
  143. proc_p p;
  144. {
  145. /* Make sure that the instruction after l
  146. * contains an instruction label. If this is
  147. * not already the case, create a new label.
  148. */
  149. line_p lab;
  150. if (l->l_next != (line_p) 0 && INSTR(l->l_next) == op_lab) {
  151. return l->l_next;
  152. }
  153. lab = newline(OPINSTRLAB);
  154. lab->l_instr = op_lab;
  155. p->p_nrlabels++;
  156. INSTRLAB(lab) = p->p_nrlabels;
  157. appnd_line(lab,l);
  158. return lab;
  159. }
  160. /* modify */
  161. STATIC act_info(off,acts,ab_off,act_out,off_out)
  162. offset off, ab_off, *off_out;
  163. actual_p acts, *act_out;
  164. {
  165. /* Find the actual parameter that corresponds to
  166. * the formal parameter with the given offset.
  167. * Return it via act_out. If the actual is not
  168. * an in-line actual, determine which temporary
  169. * local is used for it; return the offset of that
  170. * local via off_out.
  171. */
  172. offset sum = 0, tmp = 0;
  173. actual_p act;
  174. for (act = acts; act != (actual_p) 0; act = act->ac_next) {
  175. if (!act->ac_inl) {
  176. tmp -= act->ac_size;
  177. }
  178. if (sum >= off) {
  179. /* found */
  180. *act_out = act;
  181. if (!act->ac_inl) {
  182. *off_out = tmp + sum - off + ab_off;
  183. } else {
  184. assert (sum == off);
  185. }
  186. return;
  187. }
  188. sum += act->ac_size;
  189. }
  190. assert(FALSE);
  191. }
  192. STATIC store_off(off,l)
  193. offset off;
  194. line_p l;
  195. {
  196. if (TYPE(l) == OPSHORT) {
  197. assert ((short) off == off);
  198. SHORT(l) = (short) off;
  199. } else {
  200. OFFSET(l) = off;
  201. }
  202. }
  203. STATIC inl_actual(l,expr)
  204. line_p l, expr;
  205. {
  206. /* Expand an actual parameter in line.
  207. * A LOL or LDL instruction is replaced
  208. * by an expression.
  209. * A SIL or LIL is replaced by the expression
  210. * followed by a STI or LOI.
  211. */
  212. line_p e, lnp, s;
  213. short instr;
  214. instr = INSTR(l);
  215. assert(expr != (line_p) 0);
  216. e = copy_expr(expr); /* make a copy of expr. */
  217. if (instr == op_sil || instr == op_lil) {
  218. s = int_line((offset) ws);
  219. s->l_instr = (instr == op_sil ? op_sti : op_loi);
  220. appnd_line(s,last_line(e));
  221. } else {
  222. assert(instr == op_lol || instr == op_ldl);
  223. }
  224. lnp = PREV(l);
  225. rem_line(l);
  226. app_list(e,lnp);
  227. }
  228. STATIC localref(l,c,ab_off,lb_off)
  229. line_p l;
  230. call_p c;
  231. offset ab_off, lb_off;
  232. {
  233. /* Change a reference to a local variable or parameter
  234. * of the called procedure.
  235. */
  236. offset off, tmpoff;
  237. actual_p act;
  238. off = off_set(l);
  239. if (off < 0) {
  240. /* local variable, only the offset changes */
  241. store_off(lb_off + off,l);
  242. } else {
  243. act_info(off,c->cl_actuals,ab_off,&act,&tmpoff); /* find actual */
  244. if (act->ac_inl) {
  245. /* inline actual parameter */
  246. inl_actual(l,act->ac_exp);
  247. } else {
  248. /* parameter stored in temporary local */
  249. store_off(tmpoff,l);
  250. }
  251. }
  252. }
  253. STATIC chg_mes(l,c,ab_off,lb_off)
  254. line_p l;
  255. call_p c;
  256. offset ab_off, lb_off;
  257. {
  258. /* The register messages of the called procedure
  259. * must be changed. If the message applies to a
  260. * local variable or to a parameter that is not
  261. * expanded in line, the offset of the variable
  262. * is changed; else the entire message is deleted.
  263. */
  264. offset off, tmpoff;
  265. actual_p act;
  266. arg_p arg;
  267. arg = ARG(l);
  268. switch ((int) arg->a_a.a_offset) {
  269. case ms_reg:
  270. if ((arg = arg->a_next) != (arg_p) 0) {
  271. /* "mes 3" without further argument is not changed */
  272. off = arg->a_a.a_offset;
  273. if (off < 0) {
  274. /* local variable */
  275. arg->a_a.a_offset += lb_off;
  276. } else {
  277. act_info(off,c->cl_actuals,ab_off,&act,&tmpoff);
  278. if (act->ac_inl) {
  279. /* in line actual */
  280. rem_line(l);
  281. } else {
  282. arg->a_a.a_offset = tmpoff;
  283. }
  284. }
  285. }
  286. break;
  287. case ms_par:
  288. rem_line(l);
  289. break;
  290. }
  291. }
  292. STATIC chg_ret(l,c,lab)
  293. line_p l,lab;
  294. call_p c;
  295. {
  296. /* Change the RET instruction appearing in the
  297. * expanded text of a call. If the called procedure
  298. * falls through, the RET is just deleted; else it
  299. * is replaced by a branch.
  300. */
  301. line_p lnp, bra;
  302. lnp = PREV(l);
  303. rem_line(l);
  304. if (!FALLTHROUGH(c->cl_proc)) {
  305. bra = newline(OPINSTRLAB);
  306. bra->l_instr = op_bra;
  307. INSTRLAB(bra) = INSTRLAB(lab);
  308. appnd_line(bra,lnp);
  309. }
  310. }
  311. STATIC mod_instr(l,c,lab,ab_off,lb_off,lab_off)
  312. line_p l,lab;
  313. call_p c;
  314. offset ab_off,lb_off;
  315. int lab_off;
  316. {
  317. if (TYPE(l) == OPINSTRLAB) {
  318. INSTRLAB(l) += lab_off;
  319. } else {
  320. switch(INSTR(l)) {
  321. case op_stl:
  322. case op_inl:
  323. case op_del:
  324. case op_zrl:
  325. case op_sdl:
  326. case op_lol:
  327. case op_ldl:
  328. case op_sil:
  329. case op_lil:
  330. case op_lal:
  331. localref(l,c,ab_off,lb_off);
  332. break;
  333. case op_ret:
  334. chg_ret(l,c,lab);
  335. break;
  336. case ps_pro:
  337. case ps_end:
  338. case ps_sym:
  339. case ps_hol:
  340. case ps_bss:
  341. case ps_con:
  342. case ps_rom:
  343. rem_line(l);
  344. break;
  345. case ps_mes:
  346. chg_mes(l,c,ab_off,lb_off);
  347. break;
  348. }
  349. }
  350. }
  351. modify(text,c,lab,ab_off,lb_off,lab_off)
  352. line_p text,lab;
  353. call_p c;
  354. offset ab_off,lb_off;
  355. int lab_off;
  356. {
  357. /* Modify the EM text of the called procedure.
  358. * References to locals and parameters are
  359. * changed; RETs are either deleted or replaced
  360. * by a BRA to the given label; PRO and END pseudos
  361. * are removed; instruction labels are changed, in
  362. * order to make them different from any label used
  363. * by the caller; some messages need to be changed too.
  364. * Note that the first line of the text is a dummy instruction.
  365. */
  366. register line_p l;
  367. line_p next;
  368. for (l = text->l_next; l != (line_p) 0; l = next) {
  369. next = l->l_next;
  370. /* This is rather tricky. An instruction like
  371. * LOL 2 may be replaced by a number of instructions
  372. * (if the parameter is expanded in line). This inserted
  373. * code, however, should not be modified!
  374. */
  375. mod_instr(l,c,lab,ab_off,lb_off,lab_off);
  376. }
  377. }
  378. mod_actuals(nc,c,lab,ab_off,lb_off,lab_off)
  379. call_p nc,c;
  380. line_p lab;
  381. offset ab_off,lb_off;
  382. int lab_off;
  383. {
  384. actual_p act;
  385. line_p l, next, dum;
  386. dum = newline(OPNO);
  387. PREV(dum) = (line_p) 0;
  388. for (act = nc->cl_actuals; act != (actual_p) 0; act = act->ac_next) {
  389. l = act->ac_exp;
  390. assert(l != (line_p) 0);
  391. /* Insert a dummy instruction before l */
  392. dum->l_next = l;
  393. PREV(l) = dum;
  394. while(l != (line_p) 0) {
  395. next = l->l_next;
  396. mod_instr(l,c,lab,ab_off,lb_off,lab_off);
  397. l = next;
  398. }
  399. act->ac_exp = dum->l_next;
  400. PREV(dum->l_next) = (line_p) 0;
  401. }
  402. oldline(dum);
  403. }
  404. /* insert */
  405. STATIC line_p first_nonpseudo(l)
  406. line_p l;
  407. {
  408. /* Find the first non-pseudo instruction of
  409. * a list of instructions.
  410. */
  411. while (l != (line_p) 0 && INSTR(l) >= sp_fpseu &&
  412. INSTR(l) <= ps_last) l = l->l_next;
  413. return l;
  414. }
  415. insert(text,l,firstline)
  416. line_p text,l,firstline;
  417. {
  418. /* Insert the modified EM text of the called
  419. * routine in the calling routine. Pseudos are
  420. * put after the pseudos of the caller; all
  421. * normal instructions are put at the place
  422. * where the CAL originally was.
  423. */
  424. line_p l1,l2,lastpseu;
  425. l1 = text->l_next;
  426. oldline(text); /* remove dummy head instruction */
  427. if (l1 == (line_p) 0) return; /* no text at all! */
  428. l2 = first_nonpseudo(l1);
  429. if (l2 == (line_p) 0) {
  430. /* modified code consists only of pseudos */
  431. app_list(l1,PREV(first_nonpseudo(firstline)));
  432. } else {
  433. if (l1 == l2) {
  434. /* no pseudos */
  435. app_list(l2,l);
  436. } else {
  437. lastpseu = PREV(first_nonpseudo(firstline));
  438. PREV(l2)->l_next = (line_p) 0; /* cut link */
  439. app_list(l2,l); /* insert normal instructions */
  440. app_list(l1,lastpseu);
  441. }
  442. }
  443. }
  444. liquidate(p,text)
  445. proc_p p;
  446. line_p text;
  447. {
  448. /* All calls to procedure p were expanded in line, so
  449. * p is no longer needed. However, we must not throw away
  450. * any data declarations appearing in p.
  451. * The proctable entry of p is not removed, as we do not
  452. * want to create holes in this table; however the PF_BODYSEEN
  453. * flag is cleared, so p gets the same status as a procedure
  454. * whose body is unmkown.
  455. */
  456. line_p l, nextl, lastkept = (line_p) 0;
  457. call_p c, nextc;
  458. for (l = text; l != (line_p) 0; l = nextl) {
  459. nextl = l->l_next;
  460. switch(INSTR(l)) {
  461. case ps_sym:
  462. case ps_hol:
  463. case ps_bss:
  464. case ps_con:
  465. case ps_rom:
  466. lastkept = l;
  467. break;
  468. default:
  469. rem_line(l);
  470. }
  471. }
  472. if (lastkept != (line_p) 0) {
  473. /* There were some data declarations in p,
  474. * so we'll turn p into a data-unit; we'll
  475. * have to append an end-pseudo for this
  476. * purpose.
  477. */
  478. lastkept->l_next = newline(OPNO);
  479. lastkept->l_next->l_instr = (byte) ps_end;
  480. }
  481. /* There may be some calls in the body of p that
  482. * ought to be expanded in line. As p is removed
  483. * anyway, there is no use in really performing
  484. * these substitutions, so the call-descriptors
  485. * are just thrown away.
  486. */
  487. for (c = p->P_CALS; c != (call_p) 0; c = nextc) {
  488. nextc = c->cl_cdr;
  489. rem_call(c);
  490. }
  491. /* change the proctable entry */
  492. p->p_flags1 &= (byte) ~PF_BODYSEEN;
  493. oldchange(p->p_change);
  494. olduse(p->p_use);
  495. }