From 207924beac1ef56d31b825f160fb6d70b295918d Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Mon, 18 Oct 2021 16:35:14 +0200 Subject: [PATCH] [lonetix/bgp] Fix Bgp_VmDoBreak() logic for nested BLKs --- lonetix/include/df/bgp/vmintrin.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lonetix/include/df/bgp/vmintrin.h b/lonetix/include/df/bgp/vmintrin.h index 8416d6e..005958b 100755 --- a/lonetix/include/df/bgp/vmintrin.h +++ b/lonetix/include/df/bgp/vmintrin.h @@ -174,19 +174,28 @@ FORCE_INLINE void Bgp_VmDoLoadn(Bgpvm *vm) } /// Break out of current `BLK`. -FORCE_INLINE void Bgp_VmDoBreak(Bgpvm *vm) +INLINE void Bgp_VmDoBreak(Bgpvm *vm) { Bgpvmopc opc; + unsigned n = 1; - do - opc = BGP_VMOPC(vm->prog[vm->pc++]); - while (opc != BGP_VMOP_ENDBLK && opc != BGP_VMOP_END); + do { + opc = BGP_VMOPC(vm->prog[vm->pc]); + if (opc == BGP_VMOP_END) // shouldn't happen with decent bytecode... + break; + + if (opc == BGP_VMOP_BLK) + n++; + if (opc == BGP_VMOP_ENDBLK) + n--; + + vm->pc++; + } while (n > 0); if (opc == BGP_VMOP_ENDBLK) vm->nblk--; } - /// Execute `CALL` of function `vm->funcs[idx]`. FORCE_INLINE void Bgp_VmDoCall(Bgpvm *vm, Uint8 idx) {