comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 /*
2 * iskel.c
3 *
4 * This file is part of the Oxford Oberon-2 compiler
5 * Copyright (c) 2006--2016 J. M. Spivey
6 * All rights reserved
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* This file is the skeleton of the bytecode interpreter; the parts
32 specific to each instruction are inserted from the file
33 'keiko.iset' by the script 'iset.tcl'. There are three places that
34 code is inserted, each marked by two dollar signs. In order of
35 appearance, they are:
36
37 1. A jump table for quick dispatching (used if JTABLE is defined).
38
39 2. Macro definitions used in the action routines.
40
41 3. Action routines for each instruction, forming the cases in a big
42 switch. */
43
44 #include <math.h>
45 #include "obx.h"
46 #include "keiko.h"
47
48 #ifdef HAVE_INDEXED_JUMPS
49 #define JTABLE 1
50 #endif
51
52 #ifdef TRACE
53 #define DISASS 1
54 #undef JTABLE
55 #define do_find_proc if (dflag > 1) thisproc = find_proc(cp)
56 #else
57 #define do_find_proc
58 #endif
59
60 #ifdef PROFILE
61 #undef JTABLE
62 #endif
63
64 $$ macro definitions
65
66 /* interp -- main loop of the interpreter */
67 void interp(value *sp0) {
68 register value *cp = valptr(sp0[CP]);
69 uchar *pc = pointer(cp[CP_CODE]);
70 register uchar *pc0 = NULL;
71 register value *sp = sp0;
72 register uchar ir = 0;
73 #ifdef PROFILE
74 register counter ticks = 0;
75 #endif
76 register value *bp = NULL;
77 value *base = sp0;
78 #ifdef TRACE
79 proc thisproc = NULL;
80 #endif
81
82 #ifdef JTABLE
83 /* Save time by using gcc's label array feature */
84 static void *jtable[256] = {
85 $$ jump table
86 };
87 #endif
88
89 #ifdef JTABLE
90 /* Each action ends with an indexed jump to the next */
91 #define ACTION(op) lbl_ ## op:
92 #define ALSO(op)
93 #define DEFAULT
94 #define NEXT goto *jtable[ir = *(pc0 = pc)]
95 #else
96 /* Actions are just cases in a big switch */
97 #define ACTION(op) case K_ ## op:
98 #define ALSO(op) case K_ ## op:
99 #define DEFAULT default:
100 #define NEXT break
101 #endif
102
103 level++;
104 do_find_proc;
105
106 #ifdef PROFILE
107 prof_enter(cp, 0, PROF_CALL);
108 #endif
109
110 frame();
111
112 #ifdef JTABLE
113 NEXT;
114 #else
115 while (TRUE) {
116 #ifdef TRACE
117 if (dflag > 1) {
118 printf("pc=%s+%ld(%p) sp=%p bp=%p cp=%p",
119 thisproc->p_name,
120 (long) (pc - (uchar *) pointer(cp[CP_CODE])),
121 pc, sp, bp, cp);
122 fflush(stdout);
123 for (int i = 0; i < 8; i++) printf(" %x", sp[i].i);
124 printf("\n");
125 printf("%6ld: %s\n", (long) (pc-imem), fmt_inst(pc));
126 fflush(stdout);
127 }
128 #endif
129
130 #ifdef PROFILE
131 ticks++;
132 #endif
133
134 switch (ir = *(pc0 = pc)) {
135 #endif
136
137 $$ action routines
138
139 ACTION(ILLEGAL)
140 DEFAULT
141 panic("*illegal instruction %d", ir);
142 #ifndef JTABLE
143 }
144 }
145 #endif
146 }