Files
mercury/bytecode/mb_stack.c
Levi Cameron 49438027b7 Major changes to bytecode interpreter.
Estimated hours taken: 200

Major changes to bytecode interpreter.
Beginnings of native code integration

bytecode/bytecode.c:
bytecode/bytecode.h:
bytecode/dict.c:
bytecode/dict.h:
bytecode/disasm.c:
bytecode/disasm.h:
bytecode/machine.c:
bytecode/machine.h:
bytecode/mbi_main.c:
bytecode/mdb.m:
bytecode/mem.c:
bytecode/mem.h:
bytecode/slist.c:
bytecode/slist.h:
bytecode/template.c:
bytecode/template.h:
bytecode/util.c:
bytecode/util.h:
        Removed. These are all the old bytecode files from
        before I started. Any parts that were useful have already
        been salvaged and used in the new interpreter.

bytecode/*:
	Added MB_Bytecode_Addr and MB_Native_Addr types to remove abiguity
	as to what type of code an instruction pointer points to, and
	provide compiler help for erroneously mixing pointer types.

bytecode/Mmakefile:
bytecode/Mmake.params:
        Makefile for test bytecode program. Note that any library
        functions that are called from bytecode must be compiled
        with trace information. (So their entry labels can be
        looked up)

bytecode/mb_basetypes.h:
        Added. Contains basic type definitions.

bytecode/mb_bytecode.c:
bytecode/mb_bytecode.h:
        Better error messages.
        Changed var_lists to direct pointers rather than
        lookups through data stacks (much simpler but stop you
        using realloc() on the bytecode argument data)
        Label addresses are computed at module load time rather
        than being looked up each jump
        Added endof_negation_goal
        Temporary stack slot numbers are translated to variable
        numbers (now there is no distinction between temps & vars)
        MB_read_cstring return value convention changed (see comments
	for how to now free the returned memory)
        Added distinction between functions and predicates
        Added enter_else
        Code addresses are all pointers rather than simple ints
	Added MB_Code_Addr type for pred_const and call instructions

bytecode/mb_disasm.c:
bytecode/mb_disasm.h:
        Added endof_negation_goal & enter_else
        Output strings are now easier to read
        MB_listing does not display anything for invalid addresses
        MB_listing takes line length argument

bytecode/mb_interface.c:
bytecode/mb_interface.h:
bytecode/mb_interface_stub.m:
        Interfacing between native/bytecode

bytecode/mb_machine.c:
bytecode/mb_machine.h:
bytecode/mb_machine_def.h:
        Large sections of code branched off into mb_module.?
        Most instructions completed, but not integrated with native
        code.
        Most of mb_machine_def has been removed as the native
        code functions provide the same functionality.

bytecode/mb_machine_show.c:
bytecode/mb_machine_show.h:
        Completely changed. Less information now as a lot of what
        was being displayed before cannot be determined as easily
        now that it is stored in the mercury runtime.

bytecode/mb_mem.c:
bytecode/mb_mem.h:
        Added routines for garbage collected memory

bytecode/mb_module.c:
bytecode/mb_module.h:
        Loading & accessing bytecode. Argument data indexes & id are now
        stored in a single word. (see MB_BCID_xxx macros).
        Call & label addresses are now calculated at load time.

bytecode/mb_stack.c:
bytecode/mb_stack.h:
        Added options for garbage collection of MB_Stack memory

bytecode/mb_util.c:
bytecode/mb_util.h:
        Miscellaneous string functions added and SAY() for debugging

bytecode/simple01.m:
        Added. Simple test program. (replace with whatever
        program is being tested at the time).
2001-02-01 05:20:32 +00:00

166 lines
3.2 KiB
C

/*
** Copyright (C) 2000-2001 The University of Melbourne.
** This file may only be copied under the terms of the GNU Library General
** Public License - see the file COPYING.LIB in the Mercury distribution.
**
** High-water marked stack of 'MB_Word's
**
*/
/* Imports */
#include <assert.h>
#include "mb_bytecode.h"
#include "mb_mem.h"
#include "mb_stack.h"
/* Exported definitions */
MB_Stack MB_stack_new(MB_Word init_size, MB_Bool gc);
MB_Word MB_stack_size(MB_Stack *s);
MB_Word MB_stack_push(MB_Stack *s, MB_Word x);
MB_Word MB_stack_pop(MB_Stack *s);
MB_Word MB_stack_alloc(MB_Stack *s, MB_Word num_words);
void MB_stack_free(MB_Stack *s, MB_Word num_words);
MB_Word MB_stack_peek(MB_Stack *s, MB_Word index);
MB_Word MB_stack_peek_rel(MB_Stack *s, MB_Word rel_index);
MB_Word *MB_stack_peek_p(MB_Stack *s, MB_Word index);
MB_Word *MB_stack_peek_rel_p(MB_Stack *s, MB_Word rel_index);
void MB_stack_poke(MB_Stack *s, MB_Word index, MB_Word x);
void MB_stack_poke_rel(MB_Stack *s, MB_Word rel_idx, MB_Word value);
void MB_stack_delete(MB_Stack *s);
/* Local declarations */
/* Implementation */
MB_Stack
MB_stack_new(MB_Word init_size, MB_Bool gc) {
MB_Stack s;
s.max_size = init_size;
s.gc = gc;
if (init_size == 0) {
s.data = NULL;
} else {
s.data = (gc)
? MB_GC_NEW_ARRAY(MB_Word, init_size)
: MB_NEW_ARRAY(MB_Word, init_size);
if (s.data == NULL) {
MB_fatal("Unable to allocate memory");
}
}
s.sp = 0;
return s;
}
MB_Word
MB_stack_size(MB_Stack *s) {
return s->sp;
}
MB_Word
MB_stack_push(MB_Stack *s, MB_Word x)
{
if (s->sp == s->max_size) {
s->max_size *= 2;
if (s->data == NULL) {
s->data = (s->gc)
? MB_GC_NEW_ARRAY(MB_Word, s->max_size)
: MB_NEW_ARRAY(MB_Word, s->max_size);
} else {
s->data = (s->gc)
? MB_GC_RESIZE_ARRAY(s->data, MB_Word,
s->max_size)
: MB_RESIZE_ARRAY(s->data, MB_Word,
s->max_size);
}
assert(s->data != NULL);
}
s->data[s->sp] = x;
return s->sp++;
}
MB_Word
MB_stack_pop(MB_Stack *s) {
assert(s->sp != 0);
s->sp--;
return s->data[s->sp];
}
MB_Word
MB_stack_alloc(MB_Stack *s, MB_Word num_words)
{
MB_Word orig_sp = s->sp;
while (s->sp + num_words > s->max_size) {
num_words -= (s->max_size - s->sp);
s->sp = s->max_size;
num_words--;
MB_stack_push(s, 0);
}
s->sp += num_words;
return orig_sp;
}
void
MB_stack_free(MB_Stack *s, MB_Word num_words) {
s->sp -= num_words;
assert(s->sp >= 0);
}
MB_Word
MB_stack_peek(MB_Stack *s, MB_Word index) {
assert(index >= 0);
assert(index < s->sp);
return s->data[index];
}
MB_Word
MB_stack_peek_rel(MB_Stack *s, MB_Word rel_index) {
return MB_stack_peek(s, s->sp - rel_index);
}
MB_Word *
MB_stack_peek_p(MB_Stack *s, MB_Word index) {
assert(index >= 0);
assert(index < s->sp);
return s->data + index;
}
MB_Word *
MB_stack_peek_rel_p(MB_Stack *s, MB_Word rel_index) {
return MB_stack_peek_p(s, s->sp - rel_index);
}
void
MB_stack_poke(MB_Stack *s, MB_Word index, MB_Word x) {
assert(index >= 0);
assert(index < s->sp);
s->data[index] = x;
}
void
MB_stack_poke_rel(MB_Stack *s, MB_Word rel_idx, MB_Word value) {
MB_stack_poke(s, s->sp - rel_idx, value);
}
void
MB_stack_delete(MB_Stack *s) {
if (s->gc) {
MB_GC_free(s->data);
} else {
MB_free(s->data);
}
}