This commit is contained in:
Yuichiro MASUI
2013-03-02 23:40:34 +09:00
parent 7d47096434
commit 2842583678
8 changed files with 587 additions and 1216 deletions
+76 -78
View File
@@ -18,116 +18,104 @@ extern "C" {
#include <stdint.h>
#ifdef ENABLE_STDIO
int mrb_dump_irep(mrb_state*,int,FILE*);
int mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname);
int mrb_read_irep_file(mrb_state*,FILE*);
int mrb_dump_irep_binary(mrb_state*, int, FILE*);
int mrb_dump_irep_cfunc(mrb_state *mrb, int n, FILE *f, const char *initname);
int mrb_read_irep_file(mrb_state*, FILE*);
#endif
int mrb_read_irep(mrb_state*,const char*);
int mrb_read_irep(mrb_state*, const unsigned char*);
#ifdef ENABLE_STDIO
mrb_value mrb_load_irep_file(mrb_state*,FILE*);
#endif
/* dump type */
#define DUMP_TYPE_CODE 0
#define DUMP_TYPE_BIN 1
#define DUMP_TYPE_HEX 2
/* dump/load error code
*
* NOTE: MRB_DUMP_GENERAL_FAILURE is caused by
* unspecified issues like malloc failed.
*/
#define MRB_DUMP_OK 0
#define MRB_DUMP_GENERAL_FAILURE -1
#define MRB_DUMP_WRITE_FAULT -2
#define MRB_DUMP_READ_FAULT -3
#define MRB_DUMP_CRC_ERROR -4
#define MRB_DUMP_INVALID_FILE_HEADER -5
#define MRB_DUMP_INVALID_IREP -6
#define MRB_DUMP_INVALID_ARGUMENT -7
/* size of long/int/short value on dump/load */
#define MRB_DUMP_SIZE_OF_LONG 4
#define MRB_DUMP_SIZE_OF_INT 4
#define MRB_DUMP_SIZE_OF_SHORT 2
#define MRB_DUMP_SIZE_OF_CHAR 1
#define MRB_DUMP_OK 0
#define MRB_DUMP_GENERAL_FAILURE -1
#define MRB_DUMP_WRITE_FAULT -2
#define MRB_DUMP_READ_FAULT -3
#define MRB_DUMP_CRC_ERROR -4
#define MRB_DUMP_INVALID_FILE_HEADER -5
#define MRB_DUMP_INVALID_IREP -6
#define MRB_DUMP_INVALID_ARGUMENT -7
/* null symbol length */
#define MRB_DUMP_NULL_SYM_LEN 0xFFFF
/* Use HEX format string */
#define RITE_FILE_IS_HEX
#ifdef RITE_FILE_IS_HEX
#define RITE_FILE_HEX_SIZE 2
#else
#define RITE_FILE_HEX_SIZE 1
#endif
#define MRB_DUMP_NULL_SYM_LEN 0xFFFF
/* Rite Binary File header */
#define RITE_FILE_IDENFIFIER "RITE"
#define RITE_FILE_FORMAT_VER "00090000"
#define RITE_VM_VER "00090000"
#define RITE_COMPILER_TYPE "MATZ "
#define RITE_COMPILER_VER "00090000"
#define RITE_RESERVED " "
#define RITE_BINARY_IDENFIFIER "RITE"
#define RITE_BINARY_FORMAT_VER "0000"
#define RITE_COMPILER_NAME "MATZ"
#define RITE_COMPILER_VERSION "0000"
/* irep header */
#define RITE_IREP_IDENFIFIER 'S'
#define RITE_IREP_TYPE_CLASS 'C'
#define RITE_IREP_TYPE_MODULE 'M'
#define RITE_VM_VER "0000"
#define MRB_DUMP_DEFAULT_STR_LEN 128
#define RITE_BINARY_EOF "END\0"
#define RITE_SECTION_IREP_IDENTIFIER "IREP"
//Rite Binary file_header
typedef struct {
unsigned char rbfi[4]; //Rite Binary File Identify
unsigned char rbfv[8]; //Rite Binary File Format Version
unsigned char risv[8]; //Rite Instruction Specification Version
unsigned char rct[8]; //Rite Compiler Type
unsigned char rcv[8]; //Rite Compiler Version
unsigned char rbds[4]; //Rite Binary Data Size
unsigned char nirep[2]; //Number of ireps
unsigned char sirep[2]; //Start index
unsigned char rsv[8]; //Reserved
} rite_binary_header;
#define MRB_DUMP_DEFAULT_STR_LEN 128
// Rite File file_header
typedef struct {
unsigned char rbfi[4]; //Rite Binary File Identify
unsigned char rbfv[8]; //Rite Binary File Format Version
unsigned char risv[8]; //Rite Instruction Specification Version
unsigned char rct[8]; //Rite Compiler Type
unsigned char rcv[8]; //Rite Compiler Version
unsigned char rbds[8]; //Rite Binary Data Size
unsigned char nirep[4]; //Number of ireps
unsigned char sirep[4]; //Start index
unsigned char rsv[8]; //Reserved
unsigned char hcrc[4]; //HCRC
} rite_file_header;
// Rite binary header
struct rite_binary_header {
unsigned char binary_identify[4]; // Rite Binary Identify
unsigned char binary_version[4]; // Rite Binary Format Version
unsigned char binary_crc[2]; // Rite Binary CRC
unsigned char binary_size[4]; // Rite Binary Size
unsigned char compiler_name[4]; // Rite Compiler name
unsigned char compiler_version[4];
};
// Rite section header
#define RITE_SECTION_HEADER \
unsigned char section_identify[4]; \
unsigned char section_size[4];
struct rite_section_header {
RITE_SECTION_HEADER;
};
struct rite_section_irep_header {
RITE_SECTION_HEADER;
unsigned char rite_version[4]; // Rite Instruction Specification Version
unsigned char nirep[2]; // Number of ireps
unsigned char sirep[2]; // Start index
};
struct rite_binary_footer {
RITE_SECTION_HEADER;
};
static inline int
uint16_to_bin(uint16_t s, char *bin)
uint8_to_bin(uint8_t s, unsigned char *bin)
{
*bin++ = (s >> 8) & 0xff;
*bin = s & 0xff;
return (MRB_DUMP_SIZE_OF_SHORT);
*bin = s;
return sizeof(uint8_t);
}
static inline int
uint32_to_bin(uint32_t l, char *bin)
uint16_to_bin(uint16_t s, unsigned char *bin)
{
*bin++ = (s >> 8) & 0xff;
*bin = s & 0xff;
return sizeof(uint16_t);
}
static inline int
uint32_to_bin(uint32_t l, unsigned char *bin)
{
*bin++ = (l >> 24) & 0xff;
*bin++ = (l >> 16) & 0xff;
*bin++ = (l >> 8) & 0xff;
*bin = l & 0xff;
return (MRB_DUMP_SIZE_OF_LONG);
return sizeof(uint32_t);
}
static inline uint32_t
bin_to_uint32(unsigned char bin[])
bin_to_uint32(const unsigned char *bin)
{
return (uint32_t)bin[0] << 24 |
(uint32_t)bin[1] << 16 |
@@ -136,12 +124,22 @@ bin_to_uint32(unsigned char bin[])
}
static inline uint16_t
bin_to_uint16(unsigned char bin[])
bin_to_uint16(const unsigned char *bin)
{
return (uint16_t)bin[0] << 8 |
(uint16_t)bin[1];
}
static inline uint8_t
bin_to_uint8(const unsigned char *bin)
{
return (uint8_t)bin[0];
}
/* crc.c */
uint32_t
calc_crc_16_ccitt(const unsigned char *src, uint32_t nbytes, uint16_t crcwk);
#if defined(__cplusplus)
} /* extern "C" { */
#endif
+1 -1
View File
@@ -31,7 +31,7 @@ typedef struct mrb_irep {
#define MRB_ISEQ_NO_FREE 1
mrb_irep *mrb_add_irep(mrb_state *mrb);
mrb_value mrb_load_irep(mrb_state*,const char*);
mrb_value mrb_load_irep(mrb_state*,const unsigned char*);
#if defined(__cplusplus)
} /* extern "C" { */