116962Smckusick /* Copyright (c) 1984 Regents of the University of California */ 216962Smckusick 316962Smckusick #ifndef lint 4*16975Smckusick static char sccsid[] = "@(#)main.c 1.3 (Berkeley) 08/20/84"; 516962Smckusick #endif not lint 616962Smckusick 716962Smckusick #include <stdio.h> 816962Smckusick #include <ctype.h> 916963Smckusick #include "inline.h" 1016962Smckusick 11*16975Smckusick /* 12*16975Smckusick * These are the pattern tables to be loaded 13*16975Smckusick */ 14*16975Smckusick struct pats *inittables[] = { 15*16975Smckusick language_ptab, 16*16975Smckusick libc_ptab, 17*16975Smckusick machine_ptab, 18*16975Smckusick 0 19*16975Smckusick }; 20*16975Smckusick 2116962Smckusick main(argc, argv) 2216962Smckusick int argc; 2316962Smckusick char *argv[]; 2416962Smckusick { 2516962Smckusick register struct pats *pp, **hp; 2616962Smckusick register char *cp, *lp; 2716962Smckusick register char *bufp; 28*16975Smckusick register struct pats **tablep; 2916962Smckusick int size; 3016962Smckusick extern char *index(); 3116962Smckusick 3216962Smckusick if (argc > 1) 3316962Smckusick freopen(argv[1], "r", stdin); 3416962Smckusick if (argc > 2) 3516962Smckusick freopen(argv[2], "w", stdout); 3616962Smckusick /* 3716962Smckusick * set up the hash table 3816962Smckusick */ 39*16975Smckusick for (tablep = inittables; *tablep; tablep++) { 40*16975Smckusick for (pp = *tablep; pp->name[0] != '\0'; pp++) { 41*16975Smckusick hp = hash(pp->name, &size); 42*16975Smckusick pp->size = size; 43*16975Smckusick pp->next = *hp; 44*16975Smckusick *hp = pp; 45*16975Smckusick } 4616962Smckusick } 4716962Smckusick /* 4816962Smckusick * check each line and replace as appropriate 4916962Smckusick */ 5016962Smckusick buftail = bufhead = 0; 5116962Smckusick bufp = line[0]; 5216962Smckusick while (fgets(bufp, MAXLINELEN, stdin)) { 5316962Smckusick lp = index(bufp, LABELCHAR); 5416962Smckusick if (lp != NULL) { 5516962Smckusick bufp = newline(); 5616962Smckusick if (*++lp == '\n') { 5716962Smckusick emptyqueue(); 5816962Smckusick continue; 5916962Smckusick } 6016962Smckusick strcpy(bufp, lp); 6116962Smckusick *lp++ = '\n'; 6216962Smckusick *lp = '\0'; 6316962Smckusick emptyqueue(); 6416962Smckusick } 6516962Smckusick for (cp = bufp; isspace(*cp); cp++) 6616962Smckusick /* void */; 6716962Smckusick if ((cp = doreplaceon(cp)) == 0) { 6816962Smckusick bufp = newline(); 6916962Smckusick continue; 7016962Smckusick } 7116962Smckusick for (pp = *hash(cp, &size); pp; pp = pp->next) { 7216962Smckusick if (pp->size == size && bcmp(pp->name, cp, size) == 0) { 7316962Smckusick expand(pp->replace); 7416962Smckusick bufp = line[bufhead]; 7516962Smckusick break; 7616962Smckusick } 7716962Smckusick } 7816962Smckusick if (!pp) { 7916962Smckusick emptyqueue(); 8016962Smckusick fputs(bufp, stdout); 8116962Smckusick } 8216962Smckusick } 8316962Smckusick emptyqueue(); 8416962Smckusick exit(0); 8516962Smckusick } 8616962Smckusick 8716962Smckusick /* 8816962Smckusick * Integrate an expansion into the assembly stream 8916962Smckusick */ 9016962Smckusick expand(replace) 9116962Smckusick char *replace; 9216962Smckusick { 9316962Smckusick register int curptr; 9416962Smckusick char *nextreplace, *argv[MAXARGS]; 9516962Smckusick int argc, argreg, queueempty, mod = 0; 9616962Smckusick char parsebuf[BUFSIZ]; 9716962Smckusick 9816962Smckusick for (curptr = bufhead; curptr != buftail; ) { 9916962Smckusick queueempty = (curptr == buftail); 10016962Smckusick curptr = PRED(curptr); 10116962Smckusick nextreplace = copyline(replace, line[bufhead]); 10216962Smckusick argc = parseline(line[bufhead], argv, parsebuf); 10316962Smckusick argreg = nextarg(argc, argv); 10416962Smckusick if (argreg == -1) 10516962Smckusick break; 10616962Smckusick while (!queueempty) { 10716962Smckusick argc = parseline(line[curptr], argv, parsebuf); 10816962Smckusick if (ispusharg(argc, argv)) 10916962Smckusick break; 11016962Smckusick mod |= 1 << modifies(argc, argv); 11116962Smckusick queueempty = (curptr == buftail); 11216962Smckusick curptr = PRED(curptr); 11316962Smckusick } 11416962Smckusick if (queueempty) 11516962Smckusick break; 11616962Smckusick replace = nextreplace; 11716962Smckusick if (mod & (1 << argreg)) { 11816962Smckusick (void)newline(); 11916962Smckusick } else { 12016962Smckusick rewrite(line[curptr], argc, argv, argreg); 12116962Smckusick mod |= 1 << argreg; 12216962Smckusick } 12316962Smckusick } 12416962Smckusick emptyqueue(); 12516962Smckusick fputs(replace, stdout); 12616962Smckusick } 12716962Smckusick 12816962Smckusick /* 12916962Smckusick * Parse a line of assembly language into opcode and arguments. 13016962Smckusick */ 13116962Smckusick parseline(linep, argv, linebuf) 13216962Smckusick char *linep; 13316962Smckusick char *argv[]; 13416962Smckusick char *linebuf; 13516962Smckusick { 13616962Smckusick register char *bufp = linebuf, *cp = linep; 13716962Smckusick register int argc = 0; 13816962Smckusick 13916962Smckusick for (;;) { 14016962Smckusick /* 14116962Smckusick * skip over white space 14216962Smckusick */ 14316962Smckusick while (isspace(*cp)) 14416962Smckusick cp++; 14516962Smckusick if (*cp == '\0') 14616962Smckusick return (argc); 14716962Smckusick /* 14816962Smckusick * copy argument 14916962Smckusick */ 15016962Smckusick if (argc == MAXARGS - 1) { 15116962Smckusick fprintf(stderr, "instruction too long->%s", linep); 15216962Smckusick return (argc); 15316962Smckusick } 15416962Smckusick argv[argc++] = bufp; 15516962Smckusick while (!isspace(*cp) && *cp != ',' && *cp != COMMENTCHAR) 15616962Smckusick *bufp++ = *cp++; 15716962Smckusick *bufp++ = '\0'; 15816962Smckusick if (*cp == COMMENTCHAR) 15916962Smckusick return (argc); 16016962Smckusick if (*cp == ',') 16116962Smckusick cp++; 16216962Smckusick } 16316962Smckusick } 16416962Smckusick 16516962Smckusick /* 16616962Smckusick * Copy a newline terminated string. 16716962Smckusick * Return pointer to character following last character copied. 16816962Smckusick */ 16916962Smckusick char * 17016962Smckusick copyline(from, to) 17116962Smckusick register char *from, *to; 17216962Smckusick { 17316962Smckusick 17416962Smckusick while (*from != '\n') 17516962Smckusick *to++ = *from++; 17616962Smckusick *to++ = *from++; 17716962Smckusick *to = '\0'; 17816962Smckusick return (from); 17916962Smckusick } 18016962Smckusick 18116962Smckusick /* 18216962Smckusick * open space for next line in the queue 18316962Smckusick */ 18416962Smckusick char * 18516962Smckusick newline() 18616962Smckusick { 18716962Smckusick bufhead = SUCC(bufhead); 18816962Smckusick if (bufhead == buftail) { 18916962Smckusick fputs(line[buftail], stdout); 19016962Smckusick buftail = SUCC(buftail); 19116962Smckusick } 19216962Smckusick return (line[bufhead]); 19316962Smckusick } 19416962Smckusick 19516962Smckusick /* 19616962Smckusick * empty the queue by printing out all its lines. 19716962Smckusick */ 19816962Smckusick emptyqueue() 19916962Smckusick { 20016962Smckusick while (buftail != bufhead) { 20116962Smckusick fputs(line[buftail], stdout); 20216962Smckusick buftail = SUCC(buftail); 20316962Smckusick } 20416962Smckusick } 20516962Smckusick 20616962Smckusick /* 20716962Smckusick * Compute the hash of a string. 20816962Smckusick * Return the hash and the size of the item hashed 20916962Smckusick */ 21016962Smckusick struct pats ** 21116962Smckusick hash(cp, size) 21216962Smckusick char *cp; 21316962Smckusick int *size; 21416962Smckusick { 21516962Smckusick register char *cp1 = cp; 21616962Smckusick register int hash; 21716962Smckusick 21816962Smckusick hash = 1; 21916962Smckusick while (*cp1 && *cp1 != '\n') 22016962Smckusick hash += (int)*cp1++; 22116962Smckusick *size = cp1 - cp + 1; 22216962Smckusick hash &= HSHSIZ - 1; 22316962Smckusick return (&hashhdr[hash]); 22416962Smckusick } 225