2005-05-13 16:45:55 +00:00
|
|
|
/*
|
2005-08-16 15:49:04 +00:00
|
|
|
* $Id: interp.c,v 1.6 2005-08-16 15:49:04 bacon Exp $
|
2005-05-13 16:45:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/stx/interp.h>
|
2005-08-15 16:03:57 +00:00
|
|
|
#include <xp/stx/method.h>
|
|
|
|
#include <xp/stx/object.h>
|
|
|
|
#include <xp/stx/array.h>
|
2005-08-16 15:49:04 +00:00
|
|
|
#include <xp/bas/assert.h>
|
2005-05-13 16:45:55 +00:00
|
|
|
|
2005-08-16 15:49:04 +00:00
|
|
|
#define XP_STX_CONTEXT_SIZE 5
|
2005-08-15 16:03:57 +00:00
|
|
|
#define XP_STX_CONTEXT_STACK 0
|
|
|
|
#define XP_STX_CONTEXT_STACK_TOP 1
|
|
|
|
#define XP_STX_CONTEXT_METHOD 2
|
2005-08-16 15:49:04 +00:00
|
|
|
#define XP_STX_CONTEXT_RECEIVER 3
|
|
|
|
#define XP_STX_CONTEXT_IP 4
|
2005-05-13 16:45:55 +00:00
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
struct xp_stx_context_t
|
2005-05-15 18:37:00 +00:00
|
|
|
{
|
2005-08-15 16:03:57 +00:00
|
|
|
xp_stx_objhdr_t header;
|
|
|
|
xp_word_t stack;
|
|
|
|
xp_word_t stack_top;
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_word_t method;
|
2005-08-16 15:49:04 +00:00
|
|
|
xp_word_t receiver;
|
2005-08-15 16:03:57 +00:00
|
|
|
xp_word_t ip;
|
|
|
|
};
|
2005-05-15 18:37:00 +00:00
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
typedef struct xp_stx_context_t xp_stx_context_t;
|
2005-05-15 18:37:00 +00:00
|
|
|
|
2005-08-16 15:49:04 +00:00
|
|
|
xp_word_t xp_stx_new_context (xp_stx_t* stx, xp_word_t method, xp_word_t receiver)
|
2005-05-13 16:45:55 +00:00
|
|
|
{
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_word_t context;
|
2005-08-15 16:03:57 +00:00
|
|
|
xp_stx_context_t* ctxobj;
|
2005-05-13 16:45:55 +00:00
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
context = xp_stx_alloc_word_object(
|
|
|
|
stx, XP_NULL, XP_STX_CONTEXT_SIZE, XP_NULL, 0);
|
2005-05-15 18:37:00 +00:00
|
|
|
XP_STX_CLASS(stx,context) = stx->class_context;
|
2005-05-13 16:45:55 +00:00
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
ctxobj = (xp_stx_context_t*)XP_STX_OBJECT(stx,context);
|
|
|
|
ctxobj->stack = xp_stx_new_array (stx, 256); /* TODO: initial stack size */
|
|
|
|
ctxobj->stack_top = XP_STX_TO_SMALLINT(0);
|
|
|
|
ctxobj->method = method;
|
2005-08-16 15:49:04 +00:00
|
|
|
ctxobj->receiver = receiver;
|
2005-08-15 16:03:57 +00:00
|
|
|
ctxobj->ip = XP_STX_TO_SMALLINT(0);
|
2005-05-13 16:45:55 +00:00
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
return context;
|
2005-05-15 18:37:00 +00:00
|
|
|
}
|
|
|
|
|
2005-08-16 15:49:04 +00:00
|
|
|
static int __push_receiver_variable (
|
|
|
|
xp_stx_t* stx, int index, xp_stx_context_t* ctxobj);
|
|
|
|
static int __push_temporary_variable (
|
|
|
|
xp_stx_t* stx, int index, xp_stx_context_t* ctxobj);
|
|
|
|
|
2005-08-15 16:03:57 +00:00
|
|
|
int xp_stx_interp (xp_stx_t* stx, xp_word_t context)
|
2005-05-13 16:45:55 +00:00
|
|
|
{
|
2005-08-15 16:03:57 +00:00
|
|
|
xp_stx_context_t* ctxobj;
|
|
|
|
xp_stx_method_t* mthobj;
|
|
|
|
xp_stx_byte_object_t* bytecodes;
|
|
|
|
xp_word_t bytecode_size;
|
|
|
|
xp_word_t* literals;
|
|
|
|
xp_word_t pc = 0;
|
|
|
|
int code, next, next2;
|
|
|
|
|
|
|
|
ctxobj = (xp_stx_context_t*)XP_STX_OBJECT(stx,context);
|
|
|
|
mthobj = (xp_stx_method_t*)XP_STX_OBJECT(stx, ctxobj->method);
|
|
|
|
|
|
|
|
literals = mthobj->literals;
|
|
|
|
bytecodes = XP_STX_BYTE_OBJECT(stx, mthobj->bytecodes);
|
|
|
|
bytecode_size = XP_STX_SIZE(stx, mthobj->bytecodes);
|
|
|
|
|
|
|
|
while (pc < bytecode_size) {
|
|
|
|
code = bytecodes->data[pc++];
|
|
|
|
|
|
|
|
if (code >= 0x00 && code <= 0x3F) {
|
|
|
|
/* stack - push */
|
|
|
|
int what = code >> 4;
|
|
|
|
int index = code & 0x0F;
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
case 0: /* receiver variable */
|
2005-08-16 15:49:04 +00:00
|
|
|
__push_receiver_variable (stx, index, ctxobj);
|
2005-08-15 16:03:57 +00:00
|
|
|
break;
|
|
|
|
case 1: /* temporary variable */
|
2005-08-16 15:49:04 +00:00
|
|
|
__push_temporary_variable (stx, index, ctxobj);
|
2005-08-15 16:03:57 +00:00
|
|
|
break;
|
|
|
|
case 2: /* literal constant */
|
|
|
|
break;
|
|
|
|
case 3: /* literal variable */
|
|
|
|
break;
|
|
|
|
}
|
2005-05-13 16:45:55 +00:00
|
|
|
}
|
2005-08-15 16:03:57 +00:00
|
|
|
else if (code >= 0x40 && code <= 0x5F) {
|
|
|
|
/* stack - store */
|
|
|
|
int what = code >> 4;
|
|
|
|
int index = code & 0x0F;
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
case 4: /* receiver variable */
|
|
|
|
break;
|
|
|
|
case 5: /* temporary location */
|
|
|
|
break;
|
|
|
|
}
|
2005-05-13 16:45:55 +00:00
|
|
|
}
|
2005-08-15 16:03:57 +00:00
|
|
|
}
|
2005-05-13 16:45:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-08-16 15:49:04 +00:00
|
|
|
|
|
|
|
static int __push_receiver_variable (
|
|
|
|
xp_stx_t* stx, int index, xp_stx_context_t* ctxobj)
|
|
|
|
{
|
|
|
|
xp_word_t* stack;
|
|
|
|
xp_word_t stack_top;
|
|
|
|
|
|
|
|
xp_assert (XP_STX_IS_WORD_OBJECT(stx, ctxobj->receiver));
|
|
|
|
|
|
|
|
stack_top = XP_STX_FROM_SMALLINT(ctxobj->stack_top);
|
|
|
|
stack = XP_STX_DATA(stx, ctxobj->stack);
|
|
|
|
stack[stack_top++] = XP_STX_WORD_AT(stx, ctxobj->receiver, index);
|
|
|
|
ctxobj->stack_top = XP_STX_TO_SMALLINT(stack_top);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __push_temporary_variable (
|
|
|
|
xp_stx_t* stx, int index, xp_stx_context_t* ctxobj)
|
|
|
|
{
|
|
|
|
xp_word_t* stack;
|
|
|
|
xp_word_t stack_top;
|
|
|
|
|
|
|
|
xp_assert (XP_STX_IS_WORD_OBJECT(stx, ctxobj->receiver));
|
|
|
|
|
|
|
|
stack_top = XP_STX_FROM_SMALLINT(ctxobj->stack_top);
|
|
|
|
stack = XP_STX_DATA(stx, ctxobj->stack);
|
|
|
|
stack[stack_top++] = XP_STX_WORD_AT(stx, ctxobj->receiver, index);
|
|
|
|
ctxobj->stack_top = XP_STX_TO_SMALLINT(stack_top);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|