mruby/c VM Source Code master (2026/08/06)
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1
16
17#ifndef MRBC_SRC_VM_H_
18#define MRBC_SRC_VM_H_
19
20/***** Feature test switches ************************************************/
21/***** System headers *******************************************************/
22//@cond
23#include "vm_config.h"
24#include <stdint.h>
25//@endcond
26
27
28/***** Local headers ********************************************************/
29#include "value.h"
30#include "symbol.h"
31#include "c_proc.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36/***** Constat values *******************************************************/
37/***** Macros ***************************************************************/
38/***** Typedefs *************************************************************/
39//================================================================
43typedef struct IREP {
44#if defined(MRBC_DEBUG)
45 uint8_t obj_mark_[2];
46#endif
47
48 uint16_t ref_count;
49#if defined(MRBC_DEBUG)
50 uint16_t nlocals;
51#endif
52 uint16_t nregs;
53 uint16_t rlen;
54 uint16_t clen;
55 uint16_t ilen;
56#if defined(MRBC_DEBUG)
57 uint16_t plen;
58 uint16_t slen;
59#endif
60 uint16_t ofs_pools;
61 uint16_t ofs_ireps;
62
63 const uint8_t *inst;
64 const uint8_t *pool;
65
66 uint8_t data[];
71//@cond
72typedef struct IREP mrb_irep;
73//@endcond
74
75// mrbc_irep manipulate macro.
77#define mrbc_irep_tbl_syms(irep) ((mrbc_sym *)(irep)->data)
78
80#define mrbc_irep_symbol_id(irep, n) mrbc_irep_tbl_syms(irep)[(n)]
81
83#define mrbc_irep_symbol_cstr(irep, n) mrbc_symid_to_str( mrbc_irep_symbol_id(irep, n) )
84
85
87#define mrbc_irep_tbl_pools(irep) \
88 ( (uint16_t *)((irep)->data + (irep)->ofs_pools) )
89
91#define mrbc_irep_pool_ptr(irep, n) \
92 ( (irep)->pool + mrbc_irep_tbl_pools(irep)[(n)] )
93
94
96#define mrbc_irep_tbl_ireps(irep) \
97 ( (mrbc_irep **)((irep)->data + (irep)->ofs_ireps) )
98
100#define mrbc_irep_child_irep(irep, n) \
101 ( mrbc_irep_tbl_ireps(irep)[(n)] )
102
103
104
105//================================================================
109typedef struct IREP_CATCH_HANDLER {
110 uint8_t type;
111 uint8_t begin[4];
112 uint8_t end[4];
113 uint8_t target[4];
115
116
117//================================================================
121typedef struct CALLINFO {
122#if defined(MRBC_DEBUG)
123 uint8_t obj_mark_[2];
124#endif
125
127 const uint8_t *inst;
130 struct CALLINFO *prev;
131
133 struct RHash *karg_keep;
135 uint8_t reg_offset;
136 uint8_t n_args;
139
141//@cond
142typedef struct CALLINFO mrb_callinfo;
143//@endcond
144
145
146//================================================================
150typedef struct VM {
151#if defined(MRBC_DEBUG)
152 uint8_t obj_mark_[2]; // set "VM" for debug
153#endif
154 uint8_t vm_id;
155 volatile int8_t flag_preemption;
156 unsigned int flag_need_memfree : 1;
157 unsigned int flag_stop : 1;
158 unsigned int flag_permanence : 1;
159
161
163 const uint8_t *inst;
167
168 struct RProc *ret_blk;
171 uint16_t regs_size;
173
175//@cond
176typedef struct VM mrb_vm;
177//@endcond
178
179
180/***** Global variables *****************************************************/
181/***** Function prototypes **************************************************/
182//@cond
183void mrbc_cleanup_vm(void);
184mrbc_callinfo *mrbc_push_callinfo(mrbc_vm *vm, mrbc_sym method_id, int reg_offset, int n_args);
185void mrbc_pop_callinfo(mrbc_vm *vm);
188void mrbc_vm_begin(mrbc_vm *vm);
189void mrbc_vm_end(mrbc_vm *vm);
190void mrbc_vm_close(mrbc_vm *vm);
191int mrbc_vm_run(mrbc_vm *vm);
192//@endcond
193
194
195/***** Inline functions *****************************************************/
196//================================================================
204{
205 return mrbc_type(regs[0]) == MRBC_TT_PROC ? &(regs[0].proc->self) : &regs[0];
206}
207
208
209//================================================================
215static inline mrbc_sym mrbc_get_callee_symid( const mrbc_vm *vm )
216{
217 return vm->callee_sym_id;
218}
219
220
221//================================================================
227static inline const char *mrbc_get_callee_name( const mrbc_vm *vm )
228{
229 return mrbc_symid_to_str( vm->callee_sym_id );
230}
231
232
233//================================================================
241static inline int mrbc_c_block_given( mrbc_vm *vm, mrbc_value v[], int argc )
242{
243 int ofs = 1 + (mrbc_type(v[argc+1]) == MRBC_TT_HASH);
244
245 return mrbc_type(v[argc + ofs]) == MRBC_TT_PROC;
246}
247
248#ifdef __cplusplus
249}
250#endif
251#endif
#define mrbc_type(o)
Definition boxing_no.h:57
struct RObject mrbc_value
Value object. Default version.
mruby/c Proc class
Call information.
Definition vm.h:121
uint8_t is_called_super
flags when called by OP_SUPER.
Definition vm.h:137
const mrbc_irep * cur_irep
copy from mrbc_vm.
Definition vm.h:126
struct RClass * target_class
copy from mrbc_vm.
Definition vm.h:129
mrbc_sym method_id
called method ID.
Definition vm.h:134
struct RHash * karg_keep
keyword argument backup for OP_ARGARY.
Definition vm.h:133
uint8_t n_args
num of arguments.
Definition vm.h:136
struct CALLINFO * prev
previous linked list.
Definition vm.h:130
const uint8_t * inst
copy from mrbc_vm.
Definition vm.h:127
mrbc_value * cur_regs
copy from mrbc_vm.
Definition vm.h:128
struct RClass * own_class
class that owns method.
Definition vm.h:132
uint8_t reg_offset
register offset after call.
Definition vm.h:135
uint8_t is_called_block
flags when block calls.
Definition vm.h:138
IREP Catch Handler.
Definition vm.h:109
uint8_t target[4]
The address to jump to if a match is made.
Definition vm.h:113
uint8_t begin[4]
The starting address to match the handler. Includes this.
Definition vm.h:111
uint8_t type
enum mrb_catch_type, 1 byte. 0=rescue, 1=ensure
Definition vm.h:110
uint8_t end[4]
The endpoint address that matches the handler. Not Includes this.
Definition vm.h:112
IREP Internal REPresentation.
Definition vm.h:43
uint16_t ofs_pools
offset of data->tbl_pools.
Definition vm.h:60
const uint8_t * inst
pointer to instruction in RITE binary
Definition vm.h:63
const uint8_t * pool
pointer to pool in RITE binary
Definition vm.h:64
uint16_t ilen
num of bytes in OpCode
Definition vm.h:55
uint16_t ref_count
reference counter
Definition vm.h:48
uint16_t ofs_ireps
offset of data->tbl_ireps. (pointer aligned)
Definition vm.h:61
uint16_t rlen
num of child IREP blocks
Definition vm.h:53
uint16_t clen
num of catch handlers
Definition vm.h:54
uint16_t nregs
num of register variables
Definition vm.h:52
uint8_t data[]
Definition vm.h:66
Class object.
Definition class.h:87
Hash object.
Definition c_hash.h:42
struct RProc * proc
Definition boxing_no.h:30
Proc object.
Definition c_proc.h:43
mrbc_value self
Copy of self object. Valid when 'B'.
Definition c_proc.h:50
Virtual Machine.
Definition vm.h:150
unsigned int flag_need_memfree
Definition vm.h:156
mrbc_sym callee_sym_id
Current called method.
Definition vm.h:170
uint16_t regs_size
size of regs[]
Definition vm.h:171
const mrbc_irep * cur_irep
IREP currently running.
Definition vm.h:162
struct RProc * ret_blk
Return block.
Definition vm.h:168
unsigned int flag_stop
Definition vm.h:157
struct RClass * target_class
Target class.
Definition vm.h:165
mrbc_callinfo * callinfo_tail
Last point of CALLINFO link.
Definition vm.h:166
uint8_t vm_id
vm_id : 1..MAX_VM_COUNT
Definition vm.h:154
volatile int8_t flag_preemption
Definition vm.h:155
mrbc_value exception
Raised exception or nil.
Definition vm.h:169
unsigned int flag_permanence
Definition vm.h:158
const uint8_t * inst
Instruction pointer.
Definition vm.h:163
mrbc_value regs[]
Definition vm.h:172
mrbc_value * cur_regs
Current register top.
Definition vm.h:164
mrbc_irep * top_irep
IREP tree top.
Definition vm.h:160
const char * mrbc_symid_to_str(mrbc_sym sym_id)
Definition symbol.c:239
mruby/c Symbol class
mruby/c value definitions
int16_t mrbc_sym
mruby/c symbol ID
Definition value.h:62
@ MRBC_TT_PROC
Proc.
Definition value.h:96
@ MRBC_TT_HASH
Hash.
Definition value.h:100
void mrbc_vm_close(mrbc_vm *vm)
Definition vm.c:402
void mrbc_pop_callinfo(mrbc_vm *vm)
Definition vm.c:241
mrbc_vm * mrbc_vm_new(int regs_size)
Definition vm.c:287
mrbc_vm * mrbc_vm_open(mrbc_vm *vm)
Definition vm.c:309
int mrbc_vm_run(mrbc_vm *vm)
Definition vm.c:3160
void mrbc_vm_end(mrbc_vm *vm)
Definition vm.c:366
void mrbc_cleanup_vm(void)
Definition vm.c:201
void mrbc_vm_begin(mrbc_vm *vm)
Definition vm.c:340
mrbc_callinfo * mrbc_push_callinfo(mrbc_vm *vm, mrbc_sym method_id, int reg_offset, int n_args)
Definition vm.c:210
static mrbc_value * mrbc_get_self(mrbc_vm *vm, mrbc_value *regs)
Definition vm.h:203
struct CALLINFO mrbc_callinfo
Call information.
struct IREP_CATCH_HANDLER mrbc_irep_catch_handler
IREP Catch Handler.
static int mrbc_c_block_given(mrbc_vm *vm, mrbc_value v[], int argc)
Definition vm.h:241
static const char * mrbc_get_callee_name(const mrbc_vm *vm)
Definition vm.h:227
struct IREP mrbc_irep
IREP Internal REPresentation.
struct VM mrbc_vm
Virtual Machine.
static mrbc_sym mrbc_get_callee_symid(const mrbc_vm *vm)
Definition vm.h:215
Global configuration of mruby/c VM's.