retro-compiler: continue cleanup, refactoring to separate out common bits

FossilOrigin-Name: c33321dd6375683b53dd22e201d9b455eb6b601edff7b303f32de55234cdfafd
This commit is contained in:
crc 2020-11-02 12:49:34 +00:00
parent d4175bd769
commit 4fb2f1456f
2 changed files with 17 additions and 139 deletions

View file

@ -10,9 +10,10 @@
Due to the way this works, it requires a Unix-like OS
and the `objcopy` binary in the path.
Copyright (c) 2016 - 2019, Charles Childers
Copyright (c) 2016 - 2020, Charles Childers
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -21,20 +22,12 @@
#include <sys/stat.h>
#include <limits.h>
#ifndef BIT64
#define CELL int32_t
#define CELL_MIN INT_MIN + 1
#define CELL_MAX INT_MAX - 1
#else
#define CELL int64_t
#define CELL_MIN LLONG_MIN + 1
#define CELL_MAX LLONG_MAX - 1
#include "config.h"
#ifdef NUM_DEVICES
#undef NUM_DEVICES
#define NUM_DEVICES 1
#endif
#define IMAGE_SIZE 524288 * 8
#define ADDRESSES 2048
#define STACK_DEPTH 512
CELL sp, rp, ip;
CELL data[STACK_DEPTH];
CELL address[ADDRESSES];
@ -43,7 +36,6 @@ CELL memory[IMAGE_SIZE + 1];
#define NOS data[sp-1]
#define TORS address[rp]
#define NUM_DEVICES 1
typedef void (*Handler)(void);
@ -56,6 +48,15 @@ void ngaProcessOpcode(CELL opcode);
void ngaProcessPackedOpcodes(int opcode);
int ngaValidatePackedOpcodes(CELL opcode);
CELL stack_pop();
void stack_push(CELL value);
int string_inject(char *str, int buffer);
char *string_extract(int at);
CELL d_xt_for(char *Name, CELL Dictionary);
void execute(int cell);
void evaluate(char *s);
void read_token(FILE *file, char *token_buffer, int echo);
/* This assumes some knowledge of the ngaImage format for the
Retro language. If things change there, these will need to
be adjusted to match. */
@ -69,24 +70,6 @@ int ngaValidatePackedOpcodes(CELL opcode);
extern CELL Dictionary, Heap, Compiler;
extern CELL notfound;
CELL stack_pop();
void stack_push(CELL value);
int string_inject(char *str, int buffer);
char *string_extract(int at);
int d_link(CELL dt);
int d_xt(CELL dt);
int d_class(CELL dt);
int d_name(CELL dt);
int d_lookup(CELL Dictionary, char *name);
CELL d_xt_for(char *Name, CELL Dictionary);
CELL d_class_for(char *Name, CELL Dictionary);
void update_rx();
void execute(int cell);
void evaluate(char *s);
int not_eol(int ch);
void read_token(FILE *file, char *token_buffer, int echo);
char *read_token_str(char *s, char *token_buffer, int echo);
void generic_output() {
putc(stack_pop(), stdout);
fflush(stdout);
@ -357,15 +340,6 @@ void read_token(FILE *file, char *token_buffer, int echo) {
Copyright (c) 2011, Kenneth Keating
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
enum vm_opcode {
VM_NOP, VM_LIT, VM_DUP, VM_DROP, VM_SWAP, VM_PUSH, VM_POP,
VM_JUMP, VM_CALL, VM_CCALL, VM_RETURN, VM_EQ, VM_NEQ, VM_LT,
VM_GT, VM_FETCH, VM_STORE, VM_ADD, VM_SUB, VM_MUL, VM_DIVMOD,
VM_AND, VM_OR, VM_XOR, VM_SHIFT, VM_ZRET, VM_HALT, VM_IE,
VM_IQ, VM_II
};
#define NUM_OPS VM_II + 1
#ifndef NUM_DEVICES
#define NUM_DEVICES 0
#endif
@ -390,7 +364,7 @@ CELL ngaLoadImage(char *imageFile) {
void ngaPrepare() {
ip = sp = rp = 0;
for (ip = 0; ip < IMAGE_SIZE; ip++)
memory[ip] = VM_NOP;
memory[ip] = 0; /* NOP */
for (ip = 0; ip < STACK_DEPTH; ip++)
data[ip] = 0;
for (ip = 0; ip < ADDRESSES; ip++)
@ -586,7 +560,7 @@ void inst_ii() {
IO_deviceHandlers[Device]();
}
Handler instructions[NUM_OPS] = {
Handler instructions[] = {
inst_nop, inst_lit, inst_dup, inst_drop, inst_swap, inst_push, inst_pop,
inst_jump, inst_call, inst_ccall, inst_return, inst_eq, inst_neq, inst_lt,
inst_gt, inst_fetch, inst_store, inst_add, inst_sub, inst_mul, inst_divmod,

View file

@ -57,11 +57,6 @@ CELL memory[IMAGE_SIZE + 1]; /* The memory for the image */
#include "prototypes.h"
#if defined __GNU_LIBRARY__ || defined __GLIBC__
size_t strlcat(char *dst, const char *src, size_t dsize);
size_t strlcpy(char *dst, const char *src, size_t dsize);
#endif
void loadEmbeddedImage(char *arg);
@ -904,96 +899,6 @@ void ngaProcessPackedOpcodes(CELL opcode) {
}
}
/*=====================================================================*/
/*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef strlcat
/*
* Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= dsize, truncation occurred.
*/
size_t
strlcat(char *dst, const char *src, size_t dsize)
{
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
src++;
}
*dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */
}
#endif
#ifndef strlcpy
/*
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}
#endif
/*=====================================================================*/
@ -1118,4 +1023,3 @@ void loadEmbeddedImage(char *arg) {
}
return;
}