148101Sbostic /*-
2*62133Sbostic  * Copyright (c) 1980, 1993
3*62133Sbostic  *	The Regents of the University of California.  All rights reserved.
448101Sbostic  *
548101Sbostic  * %sccs.include.redist.c%
622367Sdist  */
75485Slinton 
822367Sdist #ifndef lint
9*62133Sbostic static char sccsid[] = "@(#)printinst.c	8.1 (Berkeley) 06/06/93";
1048101Sbostic #endif /* not lint */
1148101Sbostic 
125485Slinton /*
135485Slinton  * decode and print the instructions
145485Slinton  */
155485Slinton 
165485Slinton #include "defs.h"
175485Slinton #include "machine.h"
185485Slinton #include "process.h"
195485Slinton #include "pxops.h"
205485Slinton #include "optab.h"
215485Slinton #include "object.h"
2230845Smckusick #include "process/process.rep"
2330845Smckusick #include "process/pxinfo.h"
245485Slinton 
255485Slinton LOCAL ADDRESS printop(), docase();
265485Slinton 
275485Slinton /*
285485Slinton  * print instructions within the given address range
295485Slinton  */
305485Slinton 
printinst(lowaddr,highaddr)315485Slinton printinst(lowaddr, highaddr)
325485Slinton ADDRESS lowaddr;
335485Slinton ADDRESS highaddr;
345485Slinton {
355743Slinton     register ADDRESS addr;
365485Slinton 
375743Slinton     for (addr = lowaddr; addr <= highaddr; ) {
385743Slinton 	addr = printop(addr);
395743Slinton     }
405485Slinton }
415485Slinton 
425485Slinton /*
435485Slinton  * print the opcode at the given address, return the address
445485Slinton  * of the next instruction
455485Slinton  */
465485Slinton 
printop(addr)475485Slinton LOCAL ADDRESS printop(addr)
485485Slinton register ADDRESS addr;
495485Slinton {
505743Slinton     int i;
515743Slinton     PXOP op;
525743Slinton     OPTAB *o;
535743Slinton     char subop;
545743Slinton     short arg;
555743Slinton     long longarg;
565743Slinton     union {
575743Slinton 	short i;
585743Slinton 	struct { char c1, c2; } opword;
595743Slinton     } u;
605485Slinton 
615743Slinton     iread(&u.i, addr, sizeof(u.i));
625743Slinton     op = (PXOP) u.opword.c1;
635743Slinton     subop = u.opword.c2;
645743Slinton     o = &optab[op];
655743Slinton     printf("%5d   %s", addr, o->opname);
665743Slinton     addr += sizeof(u);
675743Slinton     for (i = 0; o->argtype[i] != 0; i++) {
685743Slinton 	if (i == 0) {
695743Slinton 	    putchar('\t');
705743Slinton 	} else {
715743Slinton 	    putchar(',');
725743Slinton 	}
735743Slinton 	switch(o->argtype[i]) {
745743Slinton 	    case ADDR4:
755743Slinton 	    case LWORD:
7630845Smckusick #ifdef tahoe
7730845Smckusick 		addr = (ADDRESS)(((int)addr + 3) & ~3);
7830845Smckusick #endif
795743Slinton 		iread(&longarg, addr, sizeof(longarg));
805743Slinton 		printf("%d", longarg);
815743Slinton 		addr += sizeof(long);
825743Slinton 		break;
835743Slinton 
845743Slinton 	    case SUBOP:
855743Slinton 		printf("%d", subop);
865743Slinton 		break;
875743Slinton 
885743Slinton 	    case ADDR2:
895743Slinton 	    case DISP:
905743Slinton 	    case PSUBOP:
915743Slinton 	    case VLEN:
925743Slinton 	    case HWORD:
935743Slinton 		if (i != 0 || subop == 0) {
945743Slinton 		    iread(&arg, addr, sizeof(arg));
955743Slinton 		    addr += sizeof(short);
965485Slinton 		} else {
975743Slinton 		    arg = subop;
985485Slinton 		}
995743Slinton 		printf("%d", arg);
1005743Slinton 		break;
1015485Slinton 
1025743Slinton 	    case STRING: {
1035743Slinton 		char c;
1045485Slinton 
1055743Slinton 		putchar('\'');
1065743Slinton 		while (subop > 0) {
1075743Slinton 		    iread(&c, addr, sizeof(c));
1085743Slinton 		    if (c == '\0') {
1095743Slinton 			break;
1105743Slinton 		    }
1115743Slinton 		    putchar(c);
1125743Slinton 		    subop--;
1135743Slinton 		    addr++;
1145743Slinton 		}
1155743Slinton 		addr++;
1165743Slinton 		putchar('\'');
11730845Smckusick #ifdef tahoe
11830845Smckusick 		addr = (ADDRESS)(((int)addr + 3) & ~3);
11930845Smckusick #else
1205743Slinton 		if ((addr&1) != 0) {
1215743Slinton 		    addr++;
1225743Slinton 		}
12330845Smckusick #endif
1245743Slinton 		break;
1255743Slinton 	    }
1265485Slinton 
1275743Slinton 	    default:
1285743Slinton 		panic("bad argtype %d", o->argtype[i]);
1295743Slinton 		/*NOTREACHED*/
1305743Slinton 	}
1315743Slinton     }
1325743Slinton     switch(op) {
1335743Slinton 	case O_CON:
1345743Slinton 	    addr += arg;
13530845Smckusick #ifdef tahoe
13630845Smckusick 	    addr = (ADDRESS)(((int)addr + 3) & ~3);
13730845Smckusick #endif
1385743Slinton 	    break;
1395485Slinton 
1405743Slinton 	case O_CASE1OP:
1415743Slinton 	    addr = docase(addr, 1, subop);
1425743Slinton 	    break;
1435485Slinton 
1445743Slinton 	case O_CASE2OP:
1455743Slinton 	    addr = docase(addr, 2, subop);
1465743Slinton 	    break;
1475485Slinton 
1485743Slinton 	case O_CASE4OP:
1495743Slinton 	    addr = docase(addr, 4, subop);
1505743Slinton 	    break;
1515743Slinton     }
1525743Slinton     putchar('\n');
1535743Slinton     return(addr);
1545485Slinton }
1555485Slinton 
1565485Slinton /*
1575485Slinton  * print out the destinations and cases
1585485Slinton  */
1595485Slinton 
docase(addr,size,n)1605485Slinton LOCAL ADDRESS docase(addr, size, n)
1615485Slinton ADDRESS addr;
1625485Slinton int size;
1635485Slinton int n;
1645485Slinton {
1655743Slinton     register int i;
1665743Slinton     char c;
1675743Slinton     short arg;
1685743Slinton     long longarg;
1695485Slinton 
1705743Slinton     iread(&arg, addr, sizeof(arg));
1715743Slinton     printf("\n\t%5d", arg);
1725743Slinton     addr += 2;
1735743Slinton     for (i = 1; i < n; i++) {
1745485Slinton 	iread(&arg, addr, sizeof(arg));
1755743Slinton 	printf(", %5d", arg);
1765485Slinton 	addr += 2;
1775743Slinton     }
1785743Slinton     printf("\n\t");
1795743Slinton     for (i = 0; i < n; i++) {
1805743Slinton 	switch(size) {
1815743Slinton 	    case 1:
1825743Slinton 		iread(&c, addr, sizeof(c));
1835743Slinton 		printf("%5d", c);
1845743Slinton 		break;
1855743Slinton 
1865743Slinton 	    case 2:
1875485Slinton 		iread(&arg, addr, sizeof(arg));
1885743Slinton 		printf("%5d", arg);
1895743Slinton 		break;
1905485Slinton 
1915743Slinton 	    case 4:
19230845Smckusick #ifdef tahoe
19330845Smckusick 		addr = (ADDRESS)(((int)addr + 3) & ~3);
19430845Smckusick #endif
1955743Slinton 		iread(&longarg, addr, sizeof(longarg));
1965743Slinton 		printf("%5d", longarg);
1975743Slinton 		break;
1985485Slinton 	}
1995743Slinton 	addr += size;
2005743Slinton 	if (i < n - 1) {
2015743Slinton 	    printf(", ");
2025485Slinton 	}
2035743Slinton     }
20430845Smckusick #ifdef tahoe
20530845Smckusick     addr = (ADDRESS)(((int)addr + 3) & ~3);
20630845Smckusick #else
2075743Slinton     if ((addr&01) == 01) {
2085743Slinton 	addr++;
2095743Slinton     }
21030845Smckusick #endif
2115743Slinton     return(addr);
2125485Slinton }
213