move.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include "assert.h"
  5. #include "param.h"
  6. #include "tables.h"
  7. #include "types.h"
  8. #include <cg_pattern.h>
  9. #include "data.h"
  10. #include "result.h"
  11. #include "extern.h"
  12. /*
  13. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  14. * See the copyright notice in the ACK home directory, in the file "Copyright".
  15. *
  16. * Author: Hans van Staveren
  17. */
  18. unsigned costcalc();
  19. move(tp1,tp2,ply,toplevel,maxcost) token_p tp1,tp2; unsigned maxcost; {
  20. register move_p mp;
  21. register unsigned t;
  22. register struct reginfo *rp;
  23. tkdef_p tdp;
  24. int i;
  25. unsigned codegen();
  26. if (eqtoken(tp1,tp2))
  27. return(0);
  28. if (tp2->t_token == -1) {
  29. if (tp1->t_token == -1) {
  30. if (eqtoken(&machregs[tp1->t_att[0].ar].r_contents,
  31. &machregs[tp2->t_att[0].ar].r_contents) &&
  32. machregs[tp1->t_att[0].ar].r_contents.t_token!=0)
  33. return(0);
  34. if (tp1->t_att[0].ar!=1) { /* COCO reg; tmp kludge */
  35. erasereg(tp2->t_att[0].ar);
  36. machregs[tp2->t_att[0].ar].r_contents =
  37. machregs[tp1->t_att[0].ar].r_contents ;
  38. } else
  39. machregs[tp1->t_att[0].ar].r_contents =
  40. machregs[tp2->t_att[0].ar].r_contents ;
  41. } else {
  42. if (eqtoken(&machregs[tp2->t_att[0].ar].r_contents,tp1))
  43. return(0);
  44. erasereg(tp2->t_att[0].ar);
  45. machregs[tp2->t_att[0].ar].r_contents = *tp1;
  46. }
  47. for (rp=machregs;rp<machregs+NREGS;rp++) {
  48. if (rp->r_contents.t_token == 0)
  49. continue;
  50. assert(rp->r_contents.t_token > 0);
  51. tdp = &tokens[rp->r_contents.t_token];
  52. for (i=0;i<TOKENSIZE;i++)
  53. if (tdp->t_type[i] == EV_REG &&
  54. clash(rp->r_contents.t_att[i].ar,tp2->t_att[0].ar)) {
  55. erasereg((int)(rp-machregs));
  56. break;
  57. }
  58. }
  59. } else if (tp1->t_token == -1) {
  60. if (eqtoken(tp2,&machregs[tp1->t_att[0].ar].r_contents))
  61. return(0);
  62. machregs[tp1->t_att[0].ar].r_contents = *tp2;
  63. }
  64. /*
  65. * If we arrive here the move must really be executed
  66. */
  67. for (mp=moves;mp<moves+NMOVES;mp++) {
  68. if (!match(tp1,&machsets[mp->m_set1],mp->m_expr1))
  69. continue;
  70. if (match(tp2,&machsets[mp->m_set2],mp->m_expr2))
  71. break;
  72. /*
  73. * Correct move rule is found
  74. */
  75. }
  76. assert(mp<moves+NMOVES);
  77. /*
  78. * To get correct interpretation of things like %[1]
  79. * in move code we stack tp2 and tp1. This little trick
  80. * saves a lot of testing in other places.
  81. */
  82. if (mp->m_cindex!=0) {
  83. fakestack[stackheight] = *tp2;
  84. fakestack[stackheight+1] = *tp1;
  85. stackheight += 2;
  86. t = codegen(&coderules[mp->m_cindex],ply,toplevel,maxcost,0);
  87. if (t <= maxcost)
  88. t += costcalc(mp->m_cost);
  89. stackheight -= 2;
  90. } else {
  91. t = 0;
  92. }
  93. return(t);
  94. }