annotate keiko/iskel.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
mike@0 1 /*
mike@0 2 * iskel.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 /* This file is the skeleton of the bytecode interpreter; the parts
mike@0 32 specific to each instruction are inserted from the file
mike@0 33 'keiko.iset' by the script 'iset.tcl'. There are three places that
mike@0 34 code is inserted, each marked by two dollar signs. In order of
mike@0 35 appearance, they are:
mike@0 36
mike@0 37 1. A jump table for quick dispatching (used if JTABLE is defined).
mike@0 38
mike@0 39 2. Macro definitions used in the action routines.
mike@0 40
mike@0 41 3. Action routines for each instruction, forming the cases in a big
mike@0 42 switch. */
mike@0 43
mike@0 44 #include <math.h>
mike@0 45 #include "obx.h"
mike@0 46 #include "keiko.h"
mike@0 47
mike@0 48 #ifdef HAVE_INDEXED_JUMPS
mike@0 49 #define JTABLE 1
mike@0 50 #endif
mike@0 51
mike@0 52 #ifdef TRACE
mike@0 53 #define DISASS 1
mike@0 54 #undef JTABLE
mike@0 55 #define do_find_proc if (dflag > 1) thisproc = find_proc(cp)
mike@0 56 #else
mike@0 57 #define do_find_proc
mike@0 58 #endif
mike@0 59
mike@0 60 #ifdef PROFILE
mike@0 61 #undef JTABLE
mike@0 62 #endif
mike@0 63
mike@0 64 $$ macro definitions
mike@0 65
mike@0 66 /* interp -- main loop of the interpreter */
mike@0 67 void interp(value *sp0) {
mike@0 68 register value *cp = valptr(sp0[CP]);
mike@0 69 uchar *pc = pointer(cp[CP_CODE]);
mike@0 70 register uchar *pc0 = NULL;
mike@0 71 register value *sp = sp0;
mike@0 72 register uchar ir = 0;
mike@0 73 #ifdef PROFILE
mike@0 74 register counter ticks = 0;
mike@0 75 #endif
mike@0 76 register value *bp = NULL;
mike@0 77 value *base = sp0;
mike@0 78 #ifdef TRACE
mike@0 79 proc thisproc = NULL;
mike@0 80 #endif
mike@0 81
mike@0 82 #ifdef JTABLE
mike@0 83 /* Save time by using gcc's label array feature */
mike@0 84 static void *jtable[256] = {
mike@0 85 $$ jump table
mike@0 86 };
mike@0 87 #endif
mike@0 88
mike@0 89 #ifdef JTABLE
mike@0 90 /* Each action ends with an indexed jump to the next */
mike@0 91 #define ACTION(op) lbl_ ## op:
mike@0 92 #define ALSO(op)
mike@0 93 #define DEFAULT
mike@0 94 #define NEXT goto *jtable[ir = *(pc0 = pc)]
mike@0 95 #else
mike@0 96 /* Actions are just cases in a big switch */
mike@0 97 #define ACTION(op) case K_ ## op:
mike@0 98 #define ALSO(op) case K_ ## op:
mike@0 99 #define DEFAULT default:
mike@0 100 #define NEXT break
mike@0 101 #endif
mike@0 102
mike@0 103 level++;
mike@0 104 do_find_proc;
mike@0 105
mike@0 106 #ifdef PROFILE
mike@0 107 prof_enter(cp, 0, PROF_CALL);
mike@0 108 #endif
mike@0 109
mike@0 110 frame();
mike@0 111
mike@0 112 #ifdef JTABLE
mike@0 113 NEXT;
mike@0 114 #else
mike@0 115 while (TRUE) {
mike@0 116 #ifdef TRACE
mike@0 117 if (dflag > 1) {
mike@0 118 printf("pc=%s+%ld(%p) sp=%p bp=%p cp=%p",
mike@0 119 thisproc->p_name,
mike@0 120 (long) (pc - (uchar *) pointer(cp[CP_CODE])),
mike@0 121 pc, sp, bp, cp);
mike@0 122 fflush(stdout);
mike@0 123 for (int i = 0; i < 8; i++) printf(" %x", sp[i].i);
mike@0 124 printf("\n");
mike@0 125 printf("%6ld: %s\n", (long) (pc-imem), fmt_inst(pc));
mike@0 126 fflush(stdout);
mike@0 127 }
mike@0 128 #endif
mike@0 129
mike@0 130 #ifdef PROFILE
mike@0 131 ticks++;
mike@0 132 #endif
mike@0 133
mike@0 134 switch (ir = *(pc0 = pc)) {
mike@0 135 #endif
mike@0 136
mike@0 137 $$ action routines
mike@0 138
mike@0 139 ACTION(ILLEGAL)
mike@0 140 DEFAULT
mike@0 141 panic("*illegal instruction %d", ir);
mike@0 142 #ifndef JTABLE
mike@0 143 }
mike@0 144 }
mike@0 145 #endif
mike@0 146 }