mktables.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. *
  4. * This product is part of the Amsterdam Compiler Kit.
  5. *
  6. * Permission to use, sell, duplicate or disclose this software must be
  7. * obtained in writing. Requests for such permissions may be sent to
  8. *
  9. * Dr. Andrew S. Tanenbaum
  10. * Wiskundig Seminarium
  11. * Vrije Universiteit
  12. * Postbox 7161
  13. * 1007 MC Amsterdam
  14. * The Netherlands
  15. *
  16. */
  17. /* Author: E.G. Keizer */
  18. #include <stdio.h>
  19. #include "/usr/em/util/ass/ip_spec.h"
  20. #include "/usr/em/h/em_spec.h"
  21. #include "/usr/em/h/em_flag.h"
  22. /* This program reads the human readable interpreter specification
  23. and produces a efficient machine representation that can be
  24. translated by a C-compiler.
  25. */
  26. #define ESCAP 256
  27. int nerror = 0 ;
  28. int atend = 0 ;
  29. int line = 1 ;
  30. int maxinsl= 0 ;
  31. extern char em_mnem[][4] ;
  32. char esca[] = "escape" ;
  33. #define ename(no) ((no)==ESCAP?esca:em_mnem[(no)])
  34. extern char em_flag[] ;
  35. main(argc,argv) char **argv ; {
  36. if ( argc>1 ) {
  37. if ( freopen(argv[1],"r",stdin)==NULL) {
  38. fatal("Cannot open %s",argv[1]) ;
  39. }
  40. }
  41. if ( argc>2 ) {
  42. if ( freopen(argv[2],"w",stdout)==NULL) {
  43. fatal("Cannot create %s",argv[2]) ;
  44. }
  45. }
  46. if ( argc>3 ) {
  47. fatal("%s [ file [ file ] ]",argv[0]) ;
  48. }
  49. atend=0 ;
  50. readin();
  51. atend=1 ;
  52. return nerror ;
  53. }
  54. readin() {
  55. char *ident();
  56. char *firstid ;
  57. int opcode,flags;
  58. int c;
  59. while ( !feof(stdin) ) {
  60. firstid=ident() ;
  61. if ( *firstid=='\n' || feof(stdin) ) continue ;
  62. opcode = getmnem(firstid) ;
  63. printf("%d ",opcode+1) ;
  64. flags = decflag(ident(),opcode) ;
  65. switch(em_flag[opcode]&EM_PAR) {
  66. case PAR_D: case PAR_F: case PAR_B: case PAR_L: case PAR_C:
  67. putchar('S') ;
  68. }
  69. putchar(' ');
  70. while ( (c=readchar())!='\n' && c!=EOF ) putchar(c) ;
  71. putchar('\n') ;
  72. }
  73. }
  74. char *ident() {
  75. /* skip spaces and tabs, anything up to space,tab or eof is
  76. a identifier.
  77. Anything from # to end-of-line is an end-of-line.
  78. End-of-line is an identifier all by itself.
  79. */
  80. static char array[200] ;
  81. register int c ;
  82. register char *cc ;
  83. do {
  84. c=readchar() ;
  85. } while ( c==' ' || c=='\t' ) ;
  86. for ( cc=array ; cc<&array[(sizeof array) - 1] ; cc++ ) {
  87. if ( c=='#' ) {
  88. do {
  89. c=readchar();
  90. } while ( c!='\n' && c!=EOF ) ;
  91. }
  92. *cc = c ;
  93. if ( c=='\n' && cc==array ) break ;
  94. c=readchar() ;
  95. if ( c=='\n' ) {
  96. pushback(c) ;
  97. break ;
  98. }
  99. if ( c==' ' || c=='\t' || c==EOF ) break ;
  100. }
  101. *++cc=0 ;
  102. return array ;
  103. }
  104. int getmnem(str) char *str ; {
  105. char (*ptr)[4] ;
  106. for ( ptr = em_mnem ; *ptr<= &em_mnem[sp_lmnem][0] ; ptr++ ) {
  107. if ( strcmp(*ptr,str)==0 ) return (ptr-em_mnem) ;
  108. }
  109. error("Illegal mnemonic") ;
  110. return 0 ;
  111. }
  112. error(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
  113. if ( !atend ) fprintf(stderr,"line %d: ",line) ;
  114. fprintf(stderr,str,a1,a2,a3,a4,a5,a6) ;
  115. fprintf(stderr,"\n");
  116. nerror++ ;
  117. }
  118. mess(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
  119. if ( !atend ) fprintf(stderr,"line %d: ",line) ;
  120. fprintf(stderr,str,a1,a2,a3,a4,a5,a6) ;
  121. fprintf(stderr,"\n");
  122. }
  123. fatal(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
  124. error(str,a1,a2,a3,a4,a5,a6) ;
  125. exit(1) ;
  126. }
  127. #define ILLGL -1
  128. check(val) int val ; {
  129. if ( val!=ILLGL ) error("Illegal flag combination") ;
  130. }
  131. int decflag(str,opc) char *str ; {
  132. int type ;
  133. int escape ;
  134. int range ;
  135. int wordm ;
  136. int notzero ;
  137. char c;
  138. type=escape=range=wordm=notzero= ILLGL ;
  139. while ( c= *str++ ) {
  140. switch ( c ) {
  141. case 'm' :
  142. check(type) ; type=OPMINI ; break ;
  143. case 's' :
  144. check(type) ; type=OPSHORT ; break ;
  145. case '-' :
  146. check(type) ; type=OPNO ;
  147. if ( (em_flag[opc]&EM_PAR)==PAR_W ) c='i' ;
  148. break ;
  149. case '1' :
  150. check(type) ; type=OP8 ; break ;
  151. case '2' :
  152. check(type) ; type=OP16 ; break ;
  153. case '4' :
  154. check(type) ; type=OP32 ; break ;
  155. case '8' :
  156. check(type) ; type=OP64 ; break ;
  157. case 'e' :
  158. check(escape) ; escape=0 ; break ;
  159. case 'N' :
  160. check(range) ; range= 2 ; break ;
  161. case 'P' :
  162. check(range) ; range= 1 ; break ;
  163. case 'w' :
  164. check(wordm) ; wordm=0 ; break ;
  165. case 'o' :
  166. check(notzero) ; notzero=0 ; break ;
  167. default :
  168. error("Unknown flag") ;
  169. }
  170. putchar(c);
  171. }
  172. if ( type==ILLGL ) error("Type must be specified") ;
  173. switch ( type ) {
  174. case OP64 :
  175. case OP32 :
  176. if ( escape!=ILLGL ) error("Conflicting escapes") ;
  177. escape=ILLGL ;
  178. case OP16 :
  179. case OP8 :
  180. case OPSHORT :
  181. case OPNO :
  182. if ( notzero!=ILLGL ) mess("Improbable OPNZ") ;
  183. if ( type==OPNO && range!=ILLGL ) {
  184. mess("No operand in range") ;
  185. }
  186. }
  187. if ( escape!=ILLGL ) type|=OPESC ;
  188. if ( wordm!=ILLGL ) type|=OPWORD ;
  189. switch ( range) {
  190. case ILLGL : type|=OP_BOTH ; break ;
  191. case 1 : type|=OP_POS ; break ;
  192. case 2 : type|=OP_NEG ; break ;
  193. }
  194. if ( notzero!=ILLGL ) type|=OPNZ ;
  195. return type ;
  196. }
  197. static int pushchar ;
  198. static int pushf ;
  199. int readchar() {
  200. int c ;
  201. if ( pushf ) {
  202. pushf=0 ;
  203. c = pushchar ;
  204. } else {
  205. if ( feof(stdin) ) return EOF ;
  206. c=getc(stdin) ;
  207. }
  208. if ( c=='\n' ) line++ ;
  209. return c ;
  210. }
  211. pushback(c) {
  212. if ( pushf ) {
  213. fatal("Double pushback") ;
  214. }
  215. pushf++ ;
  216. pushchar=c ;
  217. if ( c=='\n' ) line-- ;
  218. }