creole/creole.h

111 lines
2.3 KiB
C

#ifndef CREOLE_H
#define CREOLE_H
#include <limits.h>
#include <stddef.h>
#ifndef CREOLE_WORD
# define CREOLE_WORD unsigned int
# define CREOLE_WORD_MAX UINT_MAX
# define CREOLE_SIGNED_WORD int
# define CREOLE_SIGNED_MAX INT_MAX
#endif
#define CREOLE_MAX_ARG 3
typedef CREOLE_WORD creole_word;
typedef CREOLE_SIGNED_WORD creole_signed;
enum creole_opcode {
CREOLE_NOOP,
CREOLE_PUSH,
CREOLE_POP,
CREOLE_ADD,
CREOLE_MUL,
CREOLE_DIV,
CREOLE_SYS,
CREOLE_CLB,
CREOLE_JL,
CREOLE_JLE,
CREOLE_JE,
CREOLE_JNE,
CREOLE_OPCODE_LEN
};
enum creole_word_flag {
CREOLE_IMMEDIATE,
CREOLE_REGISTER,
CREOLE_WORD_FLAGS_LEN
};
enum creole_compiler_ret {
CREOLE_COMPILE_OK,
CREOLE_OPCODE_READ_ERROR,
CREOLE_OPCODE_MALFORMED,
CREOLE_ARG_READ_ERROR,
CREOLE_ARG_MALFORMED,
CREOLE_LAST_READ_ERROR,
CREOLE_LAST_MALFORMED,
CREOLE_LABEL_OVERFLOW,
CREOLE_TYPE_ERROR,
CREOLE_COMPILE_CLEARED_INSTRUCTION,
CREOLE_PROGRAM_OVERFLOW,
CREOLE_COMPILE_RET_LEN
};
enum creole_run_ret {
CREOLE_STEP_CONTINUE,
CREOLE_STEP_SYSCALL,
CREOLE_STEP_STOP,
CREOLE_STACK_OVERFLOW,
CREOLE_STACK_UNDERFLOW,
CREOLE_RUN_LABEL_OVERFLOW,
CREOLE_REGISTER_OVERFLOW,
CREOLE_STEP_UNKNOWN_OPCODE,
CREOLE_DIV_BY_ZERO,
CREOLE_STEP_HIGH_BIT_MALFORMED,
CREOLE_RUN_RET_LEN
};
struct creole_ins {
enum creole_opcode opcode;
unsigned char w_flags[3];
creole_word w[3];
};
struct creole_env {
creole_word *reg;
size_t reglen;
size_t *lab;
size_t lablen;
creole_word *stk;
size_t stkptr, stklen;
struct creole_ins *prg;
size_t prgptr, prgend, prglen;
};
struct creole_reader {
unsigned char *p;
size_t left;
};
int creole_encode(creole_word i, unsigned encode_to, unsigned high_bits,
unsigned char buf[7]);
enum creole_compiler_ret
creole_parse_line(struct creole_ins *ins, struct creole_reader *r);
enum creole_compiler_ret
creole_compile(struct creole_env *env, struct creole_reader *r);
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);
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);
enum creole_run_ret creole_step(struct creole_env *env, creole_word *sc);
#endif /* CREOLE_H */