annotate keiko/trace.c @ 1:b5139af1a420 tip basis

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