2023-02-05 06:44:37 -05:00
|
|
|
#ifndef CREOLE_H
|
|
|
|
#define CREOLE_H
|
2023-02-12 15:54:21 -05:00
|
|
|
/* Copyright (c) 2023 Peter McGoron <code@mcgoron.com>
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or 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. */
|
2023-02-05 06:44:37 -05:00
|
|
|
|
|
|
|
#include <limits.h>
|
2023-02-07 00:25:30 -05:00
|
|
|
#include <stddef.h>
|
2023-02-05 06:44:37 -05:00
|
|
|
|
2023-02-12 15:54:47 -05:00
|
|
|
#define CREOLE_MAJOR 0
|
|
|
|
#define CREOLE_MINOR 1
|
|
|
|
#define CREOLE_BUGFIX 0
|
|
|
|
|
2023-02-05 06:44:37 -05:00
|
|
|
#ifndef CREOLE_WORD
|
|
|
|
# define CREOLE_WORD unsigned int
|
|
|
|
# define CREOLE_WORD_MAX UINT_MAX
|
2023-02-11 15:49:39 -05:00
|
|
|
# define CREOLE_SIGNED_WORD int
|
|
|
|
# define CREOLE_SIGNED_MAX INT_MAX
|
2023-02-05 06:44:37 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CREOLE_MAX_ARG 3
|
|
|
|
|
|
|
|
typedef CREOLE_WORD creole_word;
|
2023-02-11 15:49:39 -05:00
|
|
|
typedef CREOLE_SIGNED_WORD creole_signed;
|
2023-02-05 06:44:37 -05:00
|
|
|
|
|
|
|
enum creole_opcode {
|
2023-02-12 13:06:08 -05:00
|
|
|
CREOLE_NOOP,
|
|
|
|
CREOLE_PUSH,
|
|
|
|
CREOLE_POP,
|
|
|
|
CREOLE_ADD,
|
|
|
|
CREOLE_MUL,
|
|
|
|
CREOLE_DIV,
|
|
|
|
CREOLE_SYS,
|
|
|
|
CREOLE_JL,
|
|
|
|
CREOLE_JLE,
|
|
|
|
CREOLE_JE,
|
|
|
|
CREOLE_JNE,
|
2023-02-18 00:55:04 -05:00
|
|
|
CREOLE_DB,
|
2023-02-05 06:44:37 -05:00
|
|
|
CREOLE_OPCODE_LEN
|
|
|
|
};
|
|
|
|
|
|
|
|
enum creole_word_flag {
|
|
|
|
CREOLE_IMMEDIATE,
|
|
|
|
CREOLE_REGISTER,
|
|
|
|
CREOLE_WORD_FLAGS_LEN
|
|
|
|
};
|
|
|
|
|
|
|
|
enum creole_compiler_ret {
|
|
|
|
CREOLE_COMPILE_OK,
|
2023-02-07 23:15:54 -05:00
|
|
|
CREOLE_OPCODE_READ_ERROR,
|
|
|
|
CREOLE_OPCODE_MALFORMED,
|
|
|
|
CREOLE_ARG_READ_ERROR,
|
|
|
|
CREOLE_ARG_MALFORMED,
|
|
|
|
CREOLE_LAST_READ_ERROR,
|
|
|
|
CREOLE_LAST_MALFORMED,
|
2023-02-18 00:55:04 -05:00
|
|
|
CREOLE_DATA_OVERFLOW,
|
2023-02-05 06:44:37 -05:00
|
|
|
CREOLE_TYPE_ERROR,
|
2023-02-09 11:16:41 -05:00
|
|
|
CREOLE_PROGRAM_OVERFLOW,
|
2023-02-05 06:44:37 -05:00
|
|
|
CREOLE_COMPILE_RET_LEN
|
|
|
|
};
|
|
|
|
|
2023-02-11 09:28:43 -05:00
|
|
|
enum creole_run_ret {
|
|
|
|
CREOLE_STEP_CONTINUE,
|
|
|
|
CREOLE_STEP_SYSCALL,
|
|
|
|
CREOLE_STEP_STOP,
|
|
|
|
CREOLE_STACK_OVERFLOW,
|
|
|
|
CREOLE_STACK_UNDERFLOW,
|
2023-02-18 00:55:04 -05:00
|
|
|
CREOLE_RUN_DECODE_ERROR,
|
2023-02-11 09:28:43 -05:00
|
|
|
CREOLE_REGISTER_OVERFLOW,
|
|
|
|
CREOLE_STEP_UNKNOWN_OPCODE,
|
2023-02-11 15:49:39 -05:00
|
|
|
CREOLE_DIV_BY_ZERO,
|
2023-02-12 13:06:08 -05:00
|
|
|
CREOLE_STEP_HIGH_BIT_MALFORMED,
|
2023-02-18 00:55:04 -05:00
|
|
|
CREOLE_JUMP_OVERFLOW,
|
2023-02-11 09:28:43 -05:00
|
|
|
CREOLE_RUN_RET_LEN
|
|
|
|
};
|
|
|
|
|
2023-02-18 00:55:04 -05:00
|
|
|
struct creole_reader {
|
|
|
|
unsigned char *p;
|
|
|
|
size_t left;
|
2023-02-05 06:44:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct creole_env {
|
2023-02-18 00:55:04 -05:00
|
|
|
unsigned char **dats;
|
|
|
|
size_t datlen;
|
|
|
|
|
2023-02-05 06:44:37 -05:00
|
|
|
creole_word *reg;
|
|
|
|
size_t reglen;
|
|
|
|
|
|
|
|
creole_word *stk;
|
|
|
|
size_t stkptr, stklen;
|
|
|
|
|
2023-02-18 00:55:04 -05:00
|
|
|
struct creole_reader r_current;
|
|
|
|
struct creole_reader r_start;
|
2023-02-05 06:44:37 -05:00
|
|
|
};
|
|
|
|
|
2023-02-07 23:15:54 -05:00
|
|
|
int creole_encode(creole_word i, unsigned encode_to, unsigned high_bits,
|
|
|
|
unsigned char buf[7]);
|
2023-02-18 00:55:04 -05:00
|
|
|
enum creole_compiler_ret creole_compile(struct creole_env *env);
|
2023-02-07 23:15:54 -05:00
|
|
|
|
2023-02-11 12:04:17 -05:00
|
|
|
enum creole_run_ret creole_reg_write(struct creole_env *env, unsigned reg,
|
|
|
|
creole_word w);
|
|
|
|
enum creole_run_ret creole_reg_read(struct creole_env *env, unsigned reg,
|
|
|
|
creole_word *w);
|
2023-02-11 09:28:43 -05:00
|
|
|
enum creole_run_ret creole_push(struct creole_env *env, creole_word w);
|
|
|
|
enum creole_run_ret creole_pop(struct creole_env *env, creole_word *w);
|
2023-02-18 11:05:09 -05:00
|
|
|
int creole_jump(struct creole_env *env, creole_word off);
|
2023-02-11 09:28:43 -05:00
|
|
|
enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc);
|
|
|
|
|
2023-02-05 06:44:37 -05:00
|
|
|
#endif /* CREOLE_H */
|