mktables.c 4.8 KB

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