nextem.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <em_spec.h>
  8. #include <em_flag.h>
  9. #include "assert.h"
  10. #include "param.h"
  11. #include "tables.h"
  12. #include "types.h"
  13. #include <cgg_cg.h>
  14. #include "data.h"
  15. #include "result.h"
  16. #include "extern.h"
  17. #include "compute.h"
  18. #include "fillem.h"
  19. #include "nextem.h"
  20. #ifndef NDEBUG
  21. #include <stdio.h>
  22. extern char em_mnem[][4];
  23. #endif
  24. byte *trypat(byte *bp, int len)
  25. {
  26. int patlen,i;
  27. result_t result;
  28. getint(patlen,bp);
  29. if (len == 3) {
  30. if (patlen < 3)
  31. return(0);
  32. } else {
  33. if (patlen != len)
  34. return(0);
  35. }
  36. for(i=0;i<patlen;i++)
  37. if (emp[i].em_instr != (*bp++&BMASK))
  38. return(0);
  39. for (i=0;i<patlen;i++)
  40. if (emp[i].em_optyp==OPNO)
  41. dollar[i].e_typ=EV_UNDEF;
  42. else if ((dollar[i].e_typ=argtyp(emp[i].em_instr))==EV_INT)
  43. dollar[i].e_v.e_con=emp[i].em_u.em_ioper;
  44. else {
  45. dollar[i].e_v.e_addr.ea_str=emp[i].em_soper;
  46. dollar[i].e_v.e_addr.ea_off=0;
  47. }
  48. getint(i,bp);
  49. if (i!=0) {
  50. struct emline *svp = saveemp;
  51. saveemp = emp;
  52. compute(&enodes[i], &result);
  53. if (result.e_typ != EV_INT || result.e_v.e_con == 0) {
  54. saveemp = svp;
  55. return(0);
  56. }
  57. }
  58. #ifndef NDEBUG
  59. if (Debug) {
  60. fprintf(stderr,"Matched:");
  61. for (i=0;i<patlen;i++) {
  62. #ifdef USE_TES
  63. if (emp[i].em_instr == op_lab)
  64. fprintf(stderr," lab");
  65. else
  66. #endif
  67. fprintf(stderr," %3.3s",em_mnem[emp[i].em_instr-sp_fmnem]);
  68. if (emp[i].em_soper)
  69. fprintf(stderr," %s",emp[i].em_soper);
  70. }
  71. fprintf(stderr,"\n");
  72. }
  73. #endif
  74. saveemp = emp;
  75. emp += patlen;
  76. return(bp);
  77. }
  78. extern char em_flag[];
  79. int argtyp(int mn)
  80. {
  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(int toplevel)
  97. {
  98. int i;
  99. short hash[3];
  100. byte *bp;
  101. byte *cp;
  102. int index;
  103. struct emline *ep;
  104. if (toplevel) {
  105. if (nemlines && emp>emlines) {
  106. nemlines -= emp-emlines;
  107. for (i=0,ep=emlines;i<nemlines;i++)
  108. *ep++ = *emp++;
  109. emp=emlines;
  110. }
  111. fillemlines();
  112. }
  113. hash[0] = emp[0].em_instr;
  114. hash[1] = (hash[0]<<4) ^ emp[1].em_instr;
  115. hash[2] = (hash[1]<<4) ^ emp[2].em_instr;
  116. for (i=2;i>=0;i--) {
  117. index = pathash[hash[i]&BMASK];
  118. while (index != 0) {
  119. bp = &pattern[index];
  120. if ( bp[PO_HASH] == (hash[i]>>8))
  121. if ((cp=trypat(&bp[PO_MATCH],i+1)) != 0)
  122. return(cp);
  123. index = (bp[PO_NEXT]&BMASK) | (bp[PO_NEXT+1]<<8);
  124. }
  125. }
  126. return(0);
  127. }