annotate keiko/trace.c @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /*
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 * trace.c
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 * This file is part of the Oxford Oberon-2 compiler
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 * Copyright (c) 2006--2016 J. M. Spivey
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 * All rights reserved
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions are met:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 * this list of conditions and the following disclaimer.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 * this list of conditions and the following disclaimer in the documentation
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 * and/or other materials provided with the distribution.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 * 3. The name of the author may not be used to endorse or promote products
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 * derived from this software without specific prior written permission.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 #define TRACE
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 #include "obx.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 #include "keiko.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 struct _opcode {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 const char *i_name; /* Name */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 const char *i_patt; /* Argument template */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 int i_arg; /* Argument packed in opcode */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 int i_len; /* Total length in bytes */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 #define __o1__(op, inst, patt, arg, len) { #inst, patt, arg, len },
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 struct _opcode optable[256] = { __OPCODES__(__o1__) };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 char *fmt_inst(uchar *pc) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 uchar *args = pc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 struct _opcode *ip = &optable[*pc++];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 static char buf[80];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 char *s = buf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 if (ip->i_name == NULL) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 strcpy(buf, "UNKNOWN");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 return buf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 s += sprintf(s, "%s", ip->i_name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 for (const char *p = ip->i_patt; *p != '\0'; p++) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 switch (*p) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 case '1': case 'K':
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 s += sprintf(s, " %d", get1(pc)); pc++; break;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 case '2': case 'L':
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 s += sprintf(s, " %d", get2(pc)); pc += 2; break;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 case 'R':
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 s += sprintf(s, " %ld", (long) (get2(pc)+(args-imem)));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 pc += 2; break;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 case 'S':
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 s += sprintf(s, " %ld", (long) (get1(pc)+(args-imem)));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 pc += 1; break;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 case 'N':
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 s += sprintf(s, " %d", ip->i_arg); break;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 default:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 s += sprintf(s, " ?%c?", *p);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 return buf;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 void dump(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 for (int k = 0; k < nprocs; k++) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 proc p = proctab[k];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 value *cp = p->p_addr;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 uchar *pc, *limit;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 if (! interpreted(cp)) continue;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 pc = pointer(cp[CP_CODE]); limit = pc + cp[CP_SIZE].i;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 printf("Procedure %s:\n", proctab[k]->p_name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 while (pc < limit) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 int op = *pc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 uchar *pc1 = pc + optable[op].i_len;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 printf("%6ld: %-30s", (long) (pc-imem), fmt_inst(pc));
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 while (pc < pc1) printf(" %d", *pc++);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97 printf("\n");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 if (op == K_JCASE_1) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100 int n = pc[-1];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 for (int i = 0; i < n; i++) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 printf("%6ld: CASEL %-22ld %d %d\n",
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 (long) (pc-imem), (long) (get2(pc)+(pc-imem)),
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 pc[0], pc[1]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 pc += 2;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 const char *prim_name(value *p) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 if (pointer(p[1]) != NULL) return (char *) pointer(p[1]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 return "(unknown)";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 }