123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- /*
- * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
- *
- * This product is part of the Amsterdam Compiler Kit.
- *
- * Permission to use, sell, duplicate or disclose this software must be
- * obtained in writing. Requests for such permissions may be sent to
- *
- * Dr. Andrew S. Tanenbaum
- * Wiskundig Seminarium
- * Vrije Universiteit
- * Postbox 7161
- * 1007 MC Amsterdam
- * The Netherlands
- *
- */
- /* Author: E.G. Keizer */
- #include <stdio.h>
- #include "/usr/em/util/ass/ip_spec.h"
- #include "/usr/em/h/em_spec.h"
- #include "/usr/em/h/em_flag.h"
- /* This program reads the human readable interpreter specification
- and produces a efficient machine representation that can be
- translated by a C-compiler.
- */
- #define ESCAP 256
- int nerror = 0 ;
- int atend = 0 ;
- int line = 1 ;
- int maxinsl= 0 ;
- extern char em_mnem[][4] ;
- char esca[] = "escape" ;
- #define ename(no) ((no)==ESCAP?esca:em_mnem[(no)])
- extern char em_flag[] ;
- main(argc,argv) char **argv ; {
- if ( argc>1 ) {
- if ( freopen(argv[1],"r",stdin)==NULL) {
- fatal("Cannot open %s",argv[1]) ;
- }
- }
- if ( argc>2 ) {
- if ( freopen(argv[2],"w",stdout)==NULL) {
- fatal("Cannot create %s",argv[2]) ;
- }
- }
- if ( argc>3 ) {
- fatal("%s [ file [ file ] ]",argv[0]) ;
- }
- atend=0 ;
- readin();
- atend=1 ;
- return nerror ;
- }
- readin() {
- char *ident();
- char *firstid ;
- int opcode,flags;
- int c;
- while ( !feof(stdin) ) {
- firstid=ident() ;
- if ( *firstid=='\n' || feof(stdin) ) continue ;
- opcode = getmnem(firstid) ;
- printf("%d ",opcode+1) ;
- flags = decflag(ident(),opcode) ;
- switch(em_flag[opcode]&EM_PAR) {
- case PAR_D: case PAR_F: case PAR_B: case PAR_L: case PAR_C:
- putchar('S') ;
- }
- putchar(' ');
- while ( (c=readchar())!='\n' && c!=EOF ) putchar(c) ;
- putchar('\n') ;
- }
- }
- char *ident() {
- /* skip spaces and tabs, anything up to space,tab or eof is
- a identifier.
- Anything from # to end-of-line is an end-of-line.
- End-of-line is an identifier all by itself.
- */
- static char array[200] ;
- register int c ;
- register char *cc ;
- do {
- c=readchar() ;
- } while ( c==' ' || c=='\t' ) ;
- for ( cc=array ; cc<&array[(sizeof array) - 1] ; cc++ ) {
- if ( c=='#' ) {
- do {
- c=readchar();
- } while ( c!='\n' && c!=EOF ) ;
- }
- *cc = c ;
- if ( c=='\n' && cc==array ) break ;
- c=readchar() ;
- if ( c=='\n' ) {
- pushback(c) ;
- break ;
- }
- if ( c==' ' || c=='\t' || c==EOF ) break ;
- }
- *++cc=0 ;
- return array ;
- }
- int getmnem(str) char *str ; {
- char (*ptr)[4] ;
- for ( ptr = em_mnem ; *ptr<= &em_mnem[sp_lmnem][0] ; ptr++ ) {
- if ( strcmp(*ptr,str)==0 ) return (ptr-em_mnem) ;
- }
- error("Illegal mnemonic") ;
- return 0 ;
- }
- error(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
- if ( !atend ) fprintf(stderr,"line %d: ",line) ;
- fprintf(stderr,str,a1,a2,a3,a4,a5,a6) ;
- fprintf(stderr,"\n");
- nerror++ ;
- }
- mess(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
- if ( !atend ) fprintf(stderr,"line %d: ",line) ;
- fprintf(stderr,str,a1,a2,a3,a4,a5,a6) ;
- fprintf(stderr,"\n");
- }
- fatal(str,a1,a2,a3,a4,a5,a6) /* VARARGS1 */ char *str ; {
- error(str,a1,a2,a3,a4,a5,a6) ;
- exit(1) ;
- }
- #define ILLGL -1
- check(val) int val ; {
- if ( val!=ILLGL ) error("Illegal flag combination") ;
- }
- int decflag(str,opc) char *str ; {
- int type ;
- int escape ;
- int range ;
- int wordm ;
- int notzero ;
- char c;
- type=escape=range=wordm=notzero= ILLGL ;
- while ( c= *str++ ) {
- switch ( c ) {
- case 'm' :
- check(type) ; type=OPMINI ; break ;
- case 's' :
- check(type) ; type=OPSHORT ; break ;
- case '-' :
- check(type) ; type=OPNO ;
- if ( (em_flag[opc]&EM_PAR)==PAR_W ) c='i' ;
- break ;
- case '1' :
- check(type) ; type=OP8 ; break ;
- case '2' :
- check(type) ; type=OP16 ; break ;
- case '4' :
- check(type) ; type=OP32 ; break ;
- case '8' :
- check(type) ; type=OP64 ; break ;
- case 'e' :
- check(escape) ; escape=0 ; break ;
- case 'N' :
- check(range) ; range= 2 ; break ;
- case 'P' :
- check(range) ; range= 1 ; break ;
- case 'w' :
- check(wordm) ; wordm=0 ; break ;
- case 'o' :
- check(notzero) ; notzero=0 ; break ;
- default :
- error("Unknown flag") ;
- }
- putchar(c);
- }
- if ( type==ILLGL ) error("Type must be specified") ;
- switch ( type ) {
- case OP64 :
- case OP32 :
- if ( escape!=ILLGL ) error("Conflicting escapes") ;
- escape=ILLGL ;
- case OP16 :
- case OP8 :
- case OPSHORT :
- case OPNO :
- if ( notzero!=ILLGL ) mess("Improbable OPNZ") ;
- if ( type==OPNO && range!=ILLGL ) {
- mess("No operand in range") ;
- }
- }
- if ( escape!=ILLGL ) type|=OPESC ;
- if ( wordm!=ILLGL ) type|=OPWORD ;
- switch ( range) {
- case ILLGL : type|=OP_BOTH ; break ;
- case 1 : type|=OP_POS ; break ;
- case 2 : type|=OP_NEG ; break ;
- }
- if ( notzero!=ILLGL ) type|=OPNZ ;
- return type ;
- }
- static int pushchar ;
- static int pushf ;
- int readchar() {
- int c ;
- if ( pushf ) {
- pushf=0 ;
- c = pushchar ;
- } else {
- if ( feof(stdin) ) return EOF ;
- c=getc(stdin) ;
- }
- if ( c=='\n' ) line++ ;
- return c ;
- }
- pushback(c) {
- if ( pushf ) {
- fatal("Double pushback") ;
- }
- pushf++ ;
- pushchar=c ;
- if ( c=='\n' ) line-- ;
- }
|