| 1 |
/** |
| 2 |
** @file mruby/irep.h - mrb_irep structure |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef MRUBY_IREP_H |
| 8 |
#define MRUBY_IREP_H |
| 9 |
|
| 10 |
#include "common.h" |
| 11 |
#include <mruby/compile.h> |
| 12 |
|
| 13 |
/** |
| 14 |
* Compiled mruby scripts. |
| 15 |
*/ |
| 16 |
MRB_BEGIN_DECL |
| 17 |
|
| 18 |
enum irep_pool_type { |
| 19 |
IREP_TT_STR = 0, /* string (need free) */ |
| 20 |
IREP_TT_SSTR = 2, /* string (static) */ |
| 21 |
IREP_TT_INT32 = 1, /* 32bit integer */ |
| 22 |
IREP_TT_INT64 = 3, /* 64bit integer */ |
| 23 |
IREP_TT_BIGINT = 7, /* big integer (not yet supported) */ |
| 24 |
IREP_TT_FLOAT = 5, /* float (double/float) */ |
| 25 |
}; |
| 26 |
|
| 27 |
#define IREP_TT_NFLAG 1 /* number (non string) flag */ |
| 28 |
#define IREP_TT_SFLAG 2 /* static string flag */ |
| 29 |
|
| 30 |
typedef struct mrb_pool_value { |
| 31 |
uint32_t tt; /* packed type and length (for string) */ |
| 32 |
union { |
| 33 |
const char *str; |
| 34 |
int32_t i32; |
| 35 |
int64_t i64; |
| 36 |
#ifndef MRB_NO_FLOAT |
| 37 |
mrb_float f; |
| 38 |
#endif |
| 39 |
} u; |
| 40 |
} mrb_pool_value; |
| 41 |
|
| 42 |
enum mrb_catch_type { |
| 43 |
MRB_CATCH_RESCUE = 0, |
| 44 |
MRB_CATCH_ENSURE = 1, |
| 45 |
}; |
| 46 |
|
| 47 |
struct mrb_irep_catch_handler { |
| 48 |
uint8_t type; /* enum mrb_catch_type */ |
| 49 |
uint8_t begin[4]; /* The starting address to match the handler. Includes this. */ |
| 50 |
uint8_t end[4]; /* The endpoint address that matches the handler. Not Includes this. */ |
| 51 |
uint8_t target[4]; /* The address to jump to if a match is made. */ |
| 52 |
}; |
| 53 |
|
| 54 |
/* Program data array struct */ |
| 55 |
struct mrb_irep { |
| 56 |
uint16_t nlocals; /* Number of local variables */ |
| 57 |
uint16_t nregs; /* Number of register variables */ |
| 58 |
uint16_t clen; /* Number of catch handlers */ |
| 59 |
uint8_t flags; |
| 60 |
|
| 61 |
const mrb_code *iseq; |
| 62 |
/* |
| 63 |
* A catch handler table is placed after the iseq entity. |
| 64 |
* The reason it doesn't add fields to the structure is to keep the mrb_irep structure from bloating. |
| 65 |
* The catch handler table can be obtained with `mrb_irep_catch_handler_table(irep)`. |
| 66 |
*/ |
| 67 |
const mrb_pool_value *pool; |
| 68 |
const mrb_sym *syms; |
| 69 |
const struct mrb_irep * const *reps; |
| 70 |
|
| 71 |
const mrb_sym *lv; |
| 72 |
/* debug info */ |
| 73 |
struct mrb_irep_debug_info* debug_info; |
| 74 |
|
| 75 |
uint32_t ilen; |
| 76 |
uint16_t plen, slen; |
| 77 |
uint16_t rlen; |
| 78 |
uint16_t refcnt; |
| 79 |
}; |
| 80 |
|
| 81 |
#define MRB_ISEQ_NO_FREE 1 |
| 82 |
#define MRB_IREP_NO_FREE 2 |
| 83 |
#define MRB_IREP_STATIC (MRB_ISEQ_NO_FREE | MRB_IREP_NO_FREE) |
| 84 |
|
| 85 |
MRB_API mrb_irep *mrb_add_irep(mrb_state *mrb); |
| 86 |
|
| 87 |
/** load mruby bytecode functions |
| 88 |
* Please note! Currently due to interactions with the GC calling these functions will |
| 89 |
* leak one RProc object per function call. |
| 90 |
* To prevent this save the current memory arena before calling and restore the arena |
| 91 |
* right after, like so |
| 92 |
* int ai = mrb_gc_arena_save(mrb); |
| 93 |
* mrb_value status = mrb_load_irep(mrb, buffer); |
| 94 |
* mrb_gc_arena_restore(mrb, ai); |
| 95 |
*/ |
| 96 |
|
| 97 |
/* @param [const uint8_t*] irep code, expected as a literal */ |
| 98 |
MRB_API mrb_value mrb_load_irep(mrb_state*, const uint8_t*); |
| 99 |
|
| 100 |
/* |
| 101 |
* @param [const void*] irep code |
| 102 |
* @param [size_t] size of irep buffer. |
| 103 |
*/ |
| 104 |
MRB_API mrb_value mrb_load_irep_buf(mrb_state*, const void*, size_t); |
| 105 |
|
| 106 |
/* @param [const uint8_t*] irep code, expected as a literal */ |
| 107 |
MRB_API mrb_value mrb_load_irep_cxt(mrb_state*, const uint8_t*, mrbc_context*); |
| 108 |
|
| 109 |
/* |
| 110 |
* @param [const void*] irep code |
| 111 |
* @param [size_t] size of irep buffer. |
| 112 |
*/ |
| 113 |
MRB_API mrb_value mrb_load_irep_buf_cxt(mrb_state*, const void*, size_t, mrbc_context*); |
| 114 |
|
| 115 |
struct mrb_insn_data { |
| 116 |
uint8_t insn; |
| 117 |
uint32_t a; |
| 118 |
uint16_t b; |
| 119 |
uint16_t c; |
| 120 |
const mrb_code *addr; |
| 121 |
}; |
| 122 |
|
| 123 |
#define mrb_irep_catch_handler_pack(n, v) uint32_to_bin(n, v) |
| 124 |
#define mrb_irep_catch_handler_unpack(v) bin_to_uint32(v) |
| 125 |
|
| 126 |
void mrb_irep_incref(mrb_state*, struct mrb_irep*); |
| 127 |
void mrb_irep_decref(mrb_state*, struct mrb_irep*); |
| 128 |
void mrb_irep_cutref(mrb_state*, struct mrb_irep*); |
| 129 |
|
| 130 |
MRB_END_DECL |
| 131 |
|
| 132 |
#endif /* MRUBY_IREP_H */ |