il3_change.c 12 KB

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