ic_io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* I N T E R M E D I A T E C O D E
  7. *
  8. * I C _ I O . C
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <em_pseu.h>
  13. #include <em_spec.h>
  14. #include <arch.h>
  15. #include "../share/types.h"
  16. #include "../share/debug.h"
  17. #include "ic.h"
  18. #include "ic_lookup.h"
  19. #include "../share/alloc.h"
  20. #include "ic_io.h"
  21. static short libstate;
  22. static long bytecnt;
  23. static FILE *infile; /* The current EM input file */
  24. static int readbyte()
  25. {
  26. if (libstate == ARCHIVE && bytecnt-- == 0L) {
  27. /* If we're reading from an archive file, we'll
  28. * have to count the number of characters read,
  29. * to know where the current module ends.
  30. */
  31. return EOF;
  32. }
  33. return getc(infile);
  34. }
  35. short readshort()
  36. {
  37. int l_byte, h_byte;
  38. l_byte = readbyte();
  39. h_byte = readbyte();
  40. if ( h_byte>=128 ) h_byte -= 256 ;
  41. return l_byte | (h_byte*256) ;
  42. }
  43. #ifdef LONGOFF
  44. offset readoffset()
  45. {
  46. long l;
  47. int h_byte;
  48. l = readbyte();
  49. l |= ((unsigned) readbyte())*256 ;
  50. l |= readbyte()*256L*256L ;
  51. h_byte = readbyte() ;
  52. if ( h_byte>=128 ) h_byte -= 256 ;
  53. return l | (h_byte*256L*256*256L) ;
  54. }
  55. #endif
  56. short get_int()
  57. {
  58. switch(table2()) {
  59. default: error("int expected");
  60. case CSTX1:
  61. return(tabval);
  62. }
  63. }
  64. char readchar()
  65. {
  66. return(readbyte());
  67. }
  68. offset get_off()
  69. {
  70. switch (table2()) {
  71. default: error("offset expected");
  72. case CSTX1:
  73. return((offset) tabval);
  74. #ifdef LONGOFF
  75. case CSTX2:
  76. return(tabval2);
  77. #endif
  78. }
  79. }
  80. static void make_string(int n)
  81. {
  82. sprintf(string,".%u",n);
  83. }
  84. static void inident()
  85. {
  86. int n;
  87. char *p = string;
  88. int c;
  89. n = get_int();
  90. while (n--) {
  91. c = readbyte();
  92. if (p<&string[IDL])
  93. *p++ = c;
  94. }
  95. *p++ = 0;
  96. }
  97. int table3(int n)
  98. {
  99. switch (n) {
  100. case sp_ilb1: tabval = readbyte(); return(ILBX);
  101. case sp_ilb2: tabval = readshort(); return(ILBX);
  102. case sp_dlb1: make_string(readbyte()); return(DLBX);
  103. case sp_dlb2: make_string(readshort()); return(DLBX);
  104. case sp_dnam: inident(); return(DLBX);
  105. case sp_pnam: inident(); return(n);
  106. case sp_cst2: tabval = readshort(); return(CSTX1);
  107. #ifdef LONGOFF
  108. case sp_cst4: tabval2 = readoffset(); return(CSTX2);
  109. #endif
  110. case sp_doff: if (table2()!=DLBX) error("symbol expected");
  111. switch(table2()) {
  112. default: error("offset expected");
  113. case CSTX1: return(VALX1);
  114. #ifdef LONGOFF
  115. case CSTX2: return(VALX2);
  116. #endif
  117. }
  118. default: return(n);
  119. }
  120. }
  121. int table1()
  122. {
  123. int n;
  124. n = readbyte();
  125. if (n == EOF)
  126. return(ATEOF);
  127. if ((n <= sp_lmnem) && (n >= sp_fmnem)) {
  128. tabval = n;
  129. return(INST);
  130. }
  131. if ((n <= sp_lpseu) && (n >= sp_fpseu)) {
  132. tabval = n;
  133. return(PSEU);
  134. }
  135. if ((n < sp_filb0 + sp_nilb0) && (n >= sp_filb0)) {
  136. tabval = n - sp_filb0;
  137. return(ILBX);
  138. }
  139. return(table3(n));
  140. }
  141. int table2()
  142. {
  143. int n;
  144. n = readbyte();
  145. if ((n < sp_fcst0 + sp_ncst0) && (n >= sp_fcst0)) {
  146. tabval = n - sp_zcst0;
  147. return(CSTX1);
  148. }
  149. return(table3(n));
  150. }
  151. void file_init(FILE *f, short state, long length)
  152. {
  153. short n;
  154. infile = f;
  155. libstate = state;
  156. bytecnt = length;
  157. linecount = 0;
  158. n = readshort();
  159. if (n != (short) sp_magic) {
  160. error("wrong magic number: %d", n);
  161. }
  162. }
  163. void arch_init(FILE *arch)
  164. {
  165. short n;
  166. infile = arch;
  167. n = readshort();
  168. if (n != (short)ARMAG && n != (short)AALMAG) {
  169. error("wrong archive magic number: %d",n);
  170. }
  171. }