nextem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <em_spec.h>
  7. #include <em_flag.h>
  8. #include "assert.h"
  9. #include "param.h"
  10. #include "tables.h"
  11. #include "types.h"
  12. #include <cgg_cg.h>
  13. #include "data.h"
  14. #include "result.h"
  15. #include "extern.h"
  16. /*
  17. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  18. * See the copyright notice in the ACK home directory, in the file "Copyright".
  19. *
  20. * Author: Hans van Staveren
  21. */
  22. #ifndef NDEBUG
  23. #include <stdio.h>
  24. extern char em_mnem[][4];
  25. #endif
  26. byte *trypat(bp,len) register byte *bp; {
  27. register patlen,i;
  28. result_t result;
  29. getint(patlen,bp);
  30. if (len == 3) {
  31. if (patlen < 3)
  32. return(0);
  33. } else {
  34. if (patlen != len)
  35. return(0);
  36. }
  37. for(i=0;i<patlen;i++)
  38. if (emp[i].em_instr != (*bp++&BMASK))
  39. return(0);
  40. for (i=0;i<patlen;i++)
  41. if (emp[i].em_optyp==OPNO)
  42. dollar[i].e_typ=EV_UNDEF;
  43. else if ((dollar[i].e_typ=argtyp(emp[i].em_instr))==EV_INT)
  44. dollar[i].e_v.e_con=emp[i].em_u.em_ioper;
  45. else {
  46. dollar[i].e_v.e_addr.ea_str=emp[i].em_soper;
  47. dollar[i].e_v.e_addr.ea_off=0;
  48. }
  49. getint(i,bp);
  50. if (i!=0) {
  51. struct emline *svp = saveemp;
  52. saveemp = emp;
  53. compute(&enodes[i], &result);
  54. if (result.e_typ != EV_INT || result.e_v.e_con == 0) {
  55. saveemp = svp;
  56. return(0);
  57. }
  58. }
  59. #ifndef NDEBUG
  60. if (Debug) {
  61. fprintf(stderr,"Matched:");
  62. for (i=0;i<patlen;i++) {
  63. #ifdef USE_TES
  64. if (emp[i].em_instr == op_lab)
  65. fprintf(stderr," lab");
  66. else
  67. #endif
  68. fprintf(stderr," %3.3s",em_mnem[emp[i].em_instr-sp_fmnem]);
  69. if (emp[i].em_soper)
  70. fprintf(stderr," %s",emp[i].em_soper);
  71. }
  72. fprintf(stderr,"\n");
  73. }
  74. #endif
  75. saveemp = emp;
  76. emp += patlen;
  77. return(bp);
  78. }
  79. extern char em_flag[];
  80. argtyp(mn) {
  81. switch(em_flag[mn-sp_fmnem]&EM_PAR) {
  82. case PAR_W:
  83. case PAR_S:
  84. case PAR_Z:
  85. case PAR_O:
  86. case PAR_N:
  87. case PAR_L:
  88. case PAR_F:
  89. case PAR_R:
  90. case PAR_C:
  91. return(EV_INT);
  92. default:
  93. return(EV_ADDR);
  94. }
  95. }
  96. byte *nextem(toplevel) {
  97. register i;
  98. short hash[3];
  99. register byte *bp;
  100. byte *cp;
  101. int index;
  102. register struct emline *ep;
  103. if (toplevel) {
  104. if (nemlines && emp>emlines) {
  105. nemlines -= emp-emlines;
  106. for (i=0,ep=emlines;i<nemlines;i++)
  107. *ep++ = *emp++;
  108. emp=emlines;
  109. }
  110. fillemlines();
  111. }
  112. hash[0] = emp[0].em_instr;
  113. hash[1] = (hash[0]<<4) ^ emp[1].em_instr;
  114. hash[2] = (hash[1]<<4) ^ emp[2].em_instr;
  115. for (i=2;i>=0;i--) {
  116. index = pathash[hash[i]&BMASK];
  117. while (index != 0) {
  118. bp = &pattern[index];
  119. if ( bp[PO_HASH] == (hash[i]>>8))
  120. if ((cp=trypat(&bp[PO_MATCH],i+1)) != 0)
  121. return(cp);
  122. index = (bp[PO_NEXT]&BMASK) | (bp[PO_NEXT+1]<<8);
  123. }
  124. }
  125. return(0);
  126. }