summaryrefslogtreecommitdiffstatshomepage
path: root/py/vm.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-02-15 22:46:21 +0000
committerDamien George <damien.p.george@gmail.com>2016-02-17 09:02:19 +0000
commit40d8430ee3f62d48482439c080f3a058bcc7fd64 (patch)
tree1fec14d455aa005392cf95e0f7e616b65be30cdb /py/vm.c
parent69d9e7d27d62d54c0625d4181c80478b0313384d (diff)
downloadmicropython-40d8430ee3f62d48482439c080f3a058bcc7fd64.tar.gz
micropython-40d8430ee3f62d48482439c080f3a058bcc7fd64.zip
py/vm: Add macros to hook into various points in the VM.
These can be used to insert arbitrary checks, polling, etc into the VM. They are left general because the VM is a highly tuned loop and it should be up to a given port how that port wants to modify the VM internals. One common use would be to insert a polling check, but only done after a certain number of opcodes were executed, so as not to slow down the VM too much. For example: #define MICROPY_VM_HOOK_COUNT (30) #define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT #define MICROPY_VM_HOOK_POLL if (--vm_hook_divisor == 0) { \ vm_hook_divisor = MICROPY_VM_HOOK_COUNT; extern void vm_hook_function(void); vm_hook_function(); } #define MICROPY_VM_HOOK_LOOP MICROPY_VM_HOOK_POLL #define MICROPY_VM_HOOK_RETURN MICROPY_VM_HOOK_POLL
Diffstat (limited to 'py/vm.c')
-rw-r--r--py/vm.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/py/vm.c b/py/vm.c
index 141315ea8e..b8d38f78e4 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -179,6 +179,7 @@ outer_dispatch_loop:
const byte *ip = code_state->ip;
mp_obj_t *sp = code_state->sp;
mp_obj_t obj_shared;
+ MICROPY_VM_HOOK_INIT
// If we have exception to inject, now that we finish setting up
// execution context, raise it. This works as if RAISE_VARARGS
@@ -1069,6 +1070,7 @@ unwind_return:
nlr_pop();
code_state->sp = sp;
assert(exc_sp == exc_stack - 1);
+ MICROPY_VM_HOOK_RETURN
#if MICROPY_STACKLESS
if (code_state->prev != NULL) {
mp_obj_t res = *sp;
@@ -1252,6 +1254,7 @@ yield:
#endif
pending_exception_check:
+ MICROPY_VM_HOOK_LOOP
if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
MARK_EXC_IP_SELECTIVE();
mp_obj_t obj = MP_STATE_VM(mp_pending_exception);