Files
mercury/bytecode/mb_util.c
Levi Cameron 0921310ca5 More work on the bytecode interpreter.
With these changes, it now passes all tests/general/* test cases
  except those with floats.

  Changes to the compiler:
  - Added extra argument to test instruction (string comparisons were
    being treated as integer comparisons; properly deals with different
    atomic type unifications now)
  - Changed bytecode stub functions

  Changes to the bytecode interpreter:
  - Cleaned up comments
  - Forked part of mb_machine to mb_exec
  - Added support for submodules
  - Added support for nondet procedures
  - Added support for cc_xxx procedures
  - Finished higher order calls
  - Added (very basic) debug interface
  - Added support for type information
  - Added memory corruption checking
  - Changed machine state dump formatting
  - Fixed bug in nested switches
  - Resolved builtin__unify and builtin_compare failures
  - Modified bytecode tags generation so .c & .m tag files are separate
  - Header usage rationalised

  Changes to test suite:
  - Added test cases for the bytecode interpreter.
  - More work on the bytecode interpreter.

bytecode/Mmakefile:
        Modified bytecode tags generation so .c & .m tag files are separate.
        mb_machine split into mb_exec.
        test file renamed to simple.m (copy over tests/simple??.m to test).

bytecode/TODO:
        Updated.

bytecode/mb_basetypes.h:
        Removed redundant MB_WORD_BITS (use MR_WORDBITS instead).

bytecode/mb_bytecode.h:
bytecode/mpb_bytecode.c:
        Formatting changes
        Third test instruction argument added.

bytecode/mb_disasm.h:
bytecode/mb_disasm.c:
        Formatting changes.
        Third test instruction argument added.
        Added MB_FMT_INTWIDE.

bytecode/mb_exec.h:
bytecode/mb_exec.c:
bytecode/mb_machine.h:
bytecode/mb_machine.c:
        mb_machine* split into mb_exec* and mb_machine*.
        Almost all instructions now work (see important changes above).

bytecode/mb_interface.h:
bytecode/mb_interface.c:
        Added nondet stub functions.
        Added functions to lookup builtin compiler procedures:
                do_redo, do_fail, __unify, __compare.
        Removed old debugging code.
        Stack layout changed to support nondet procedures.

bytecode/mb_interface_stub.c:
bytecode/mb_interface_stub.h:
        Split off bare minimum of includes for bytecode stubs.
        Added nondet stubs.

bytecode/mb_machine_show.c:
        Made code cleaner (added subfunctions for MB_show_state).
        Added variable names to machine state dump.

bytecode/mb_mem.h:
bytecode/mb_mem.c:
        Added limited memory corruption checking.

bytecode/mb_module.h:
bytecode/mb_module.c:
        Swapped order of temps & vars on stack.
        Fixed nested switches causing random crashes.
        Added nested module support.

bytecode/test/simple??.m:
        Various test files - just to check that it doesn't crash.
        (Most do not output anything & must be verified by stepping through
        manually).

compiler/bytecode.m:
compiler/bytecode_gen.m:
        Added extra argument to test instruction (otherwise
        string comparisons would be treated as integer comparisons).

compiler/code_gen.m:
        Changed call structure name in bytecode stub to resolve
        issues with illegal characters in C structure names.
        Changed bytecode stub header file name.
2001-02-19 02:05:59 +00:00

127 lines
2.4 KiB
C

/*
** Copyright (C) 1997,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.
**
*/
#define NOSAY 0 /* To disable SAYings */
/* Imports */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "mb_mem.h"
#include "mb_util.h"
/* Exported definitions */
void MB_util_error(const char *fmt, ...);
void MB_fatal(const char *message);
int MB_str_cmp(MB_CString_Const a, MB_CString_Const b);
MB_CString MB_str_new(MB_Word len);
MB_CString MB_str_new_cat(MB_CString_Const a, MB_CString_Const b);
MB_CString MB_str_dup(MB_CString_Const str);
void MB_str_delete(MB_CString str);
/* Local declarations */
/* Implementation */
/* Prints an error to standard err (doesn't exit) */
void
MB_util_error(const char *fmt, ...)
{
va_list arg_p;
fprintf(stderr, "Error: ");
va_start(arg_p, fmt);
vfprintf(stderr, fmt, arg_p);
va_end(argp);
fprintf(stderr, "\n");
}
#ifndef NOSAY
#define NOSAY 1
#endif
void MB_SAY(const char *fmt, ...)
{
#if !NOSAY
va_list arg_p;
va_start(arg_p, fmt);
vfprintf(stderr, fmt, arg_p);
va_end(argp);
fprintf(stderr, "\n");
fflush(stdout); /* in case redirected to stdout */
#endif
}
/* prints an error and aborts program */
void
MB_fatal(const char *message)
{
MB_util_error(message);
fprintf(stderr, " NOTE: The program will now abort.\n");
abort();
return; /* not reached */
}
/* compare two strings */
int MB_str_cmp(MB_CString_Const a, MB_CString_Const b) {
return strcmp(a, b);
}
/*
** Allocate space for a new string
** Allocates in atomic garbage collected memory
*/
MB_CString
MB_str_new(MB_Word len)
{
MB_CString c = MB_GC_NEW_ARRAY_ATOMIC(char, len + 1);
if (c == NULL) MB_fatal("Not enough string space");
return c;
}
/*
** Create a new string that is the concatenation of two existing strings
*/
MB_CString
MB_str_new_cat(MB_CString_Const a, MB_CString_Const b)
{
MB_Word len_a = strlen(a);
MB_Word len_b = strlen(b);
MB_CString new_str = MB_str_new(len_a + len_b + 1);
memcpy(new_str, a, len_a);
memcpy(new_str + len_a, b, len_b);
new_str[len_a + len_b] = 0;
return new_str;
}
/* Duplicate a string */
MB_CString
MB_str_dup(MB_CString_Const str)
{
MB_CString c = MB_str_new(strlen(str) + 1);
strcpy(c, str);
return c;
}
/* Free storage associated with a given string */
void
MB_str_delete(MB_CString str)
{
MB_GC_free(str);
}