nextem.c 2.6 KB

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