ic_io.c 3.4 KB

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