move.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * Author: Hans van Staveren
  6. */
  7. #include "assert.h"
  8. #include "param.h"
  9. #include "tables.h"
  10. #include "types.h"
  11. #include <cgg_cg.h>
  12. #include "data.h"
  13. #include "result.h"
  14. #include "extern.h"
  15. #include "subr.h"
  16. #include "reg.h"
  17. #include "utils.h"
  18. #include "codegen.h"
  19. #include "move.h"
  20. int move(token_p tp1, token_p tp2, int ply, int toplevel, unsigned int maxcost)
  21. {
  22. move_p mp;
  23. unsigned int t;
  24. struct reginfo *rp;
  25. byte *tdpb;
  26. int i;
  27. if (eqtoken(tp1,tp2))
  28. return(0);
  29. if (tp2->t_token == -1) {
  30. if (tp1->t_token == -1) {
  31. if (eqtoken(&machregs[tp1->t_att[0].ar].r_contents,
  32. &machregs[tp2->t_att[0].ar].r_contents) &&
  33. machregs[tp1->t_att[0].ar].r_contents.t_token!=0)
  34. return(0);
  35. } else {
  36. if (eqtoken(&machregs[tp2->t_att[0].ar].r_contents,tp1))
  37. return(0);
  38. }
  39. erasereg(tp2->t_att[0].ar);
  40. } else if (tp1->t_token == -1) {
  41. if (eqtoken(tp2,&machregs[tp1->t_att[0].ar].r_contents))
  42. return(0);
  43. }
  44. /*
  45. * If we arrive here the move must really be executed
  46. */
  47. for (mp=moves;mp->m_set1>=0;mp++) {
  48. if (!match(tp1,&machsets[mp->m_set1],mp->m_expr1))
  49. continue;
  50. if (match(tp2,&machsets[mp->m_set2],mp->m_expr2))
  51. break;
  52. /*
  53. * Correct move rule is found
  54. */
  55. }
  56. assert(mp->m_set1>=0);
  57. /*
  58. * To get correct interpretation of things like %[1]
  59. * in move code we stack tp2 and tp1. This little trick
  60. * saves a lot of testing in other places.
  61. */
  62. fakestack[stackheight] = *tp2;
  63. fakestack[stackheight+1] = *tp1;
  64. stackheight += 2;
  65. tokpatlen += 2;
  66. t = codegen(&coderules[mp->m_cindex],ply,toplevel,maxcost,0);
  67. tokpatlen -= 2;
  68. stackheight -= 2;
  69. if (tp2->t_token == -1) {
  70. rp = &machregs[tp2->t_att[0].ar];
  71. if (tp1->t_token == -1) {
  72. rp->r_contents =
  73. machregs[tp1->t_att[0].ar].r_contents ;
  74. }
  75. else rp->r_contents = *tp1;
  76. if (rp->r_contents.t_token > 0) {
  77. tdpb = &(tokens[rp->r_contents.t_token].t_type[0]);
  78. for (i=0;i<TOKENSIZE;i++)
  79. if (*tdpb++ == EV_REG &&
  80. clash(rp->r_contents.t_att[i].ar,tp2->t_att[0].ar)) {
  81. rp->r_contents.t_token = 0;
  82. for (i = 0; i < TOKENSIZE; i++)
  83. rp->r_contents.t_att[i].aw = 0;
  84. break;
  85. }
  86. }
  87. }
  88. else if (tp1->t_token == -1)
  89. machregs[tp1->t_att[0].ar].r_contents = *tp2;
  90. return(t);
  91. }
  92. #define cocoreg machregs[0].r_contents
  93. void setcc(token_p tp)
  94. {
  95. cocoreg = *tp;
  96. }
  97. int test(token_p tp, int ply, int toplevel, unsigned int maxcost)
  98. {
  99. test_p mp;
  100. unsigned int t;
  101. if (cocoreg.t_token!=0) {
  102. if (eqtoken(tp,&cocoreg))
  103. return(0);
  104. if (tp->t_token == -1) {
  105. if (eqtoken(&machregs[tp->t_att[0].ar].r_contents,&cocoreg))
  106. return(0);
  107. }
  108. }
  109. /*
  110. * If we arrive here the test must really be executed
  111. */
  112. for (mp=tests;mp->t_set>=0;mp++) {
  113. if (match(tp,&machsets[mp->t_set],mp->t_expr))
  114. break;
  115. /*
  116. * Correct move rule is found
  117. */
  118. }
  119. assert(mp->t_set>=0);
  120. /*
  121. * To get correct interpretation of things like %[1]
  122. * in test code we stack tp. This little trick
  123. * saves a lot of testing in other places.
  124. */
  125. fakestack[stackheight] = *tp;
  126. stackheight++;
  127. tokpatlen++;
  128. t = codegen(&coderules[mp->t_cindex],ply,toplevel,maxcost,0);
  129. tokpatlen--;
  130. stackheight--;
  131. return(t);
  132. }