what does function nlr_jump do?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

what does function nlr_jump do?

Post by jickster » Thu Sep 14, 2017 1:24 pm

I'm refactoring micro python into a C++ class but I don't know if I need to include the following function

__attribute__((noreturn)) __attribute__((naked)) void nlr_jump(void *val) {
nlr_buf_t **top_ptr = &(mp_state_ctx.thread.nlr_top);
nlr_buf_t *top = *top_ptr;
if (top == ((void *)0)) {
nlr_jump_fail(val);
}

top->ret_val = val;
*top_ptr = top->prev;

__asm volatile (
"mov r0, %0 \n" // r0 points to nlr_buf
"ldr r4, [r0, #12] \n" // load r4 from nlr_buf
"ldr r5, [r0, #16] \n" // load r5 from nlr_buf
"ldr r6, [r0, #20] \n" // load r6 from nlr_buf
"ldr r7, [r0, #24] \n" // load r7 from nlr_buf
# 119 "../py/nlrthumb.c"
"ldr r8, [r0, #28] \n" // load r8 from nlr_buf
"ldr r9, [r0, #32] \n" // load r9 from nlr_buf
"ldr r10, [r0, #36] \n" // load r10 from nlr_buf
"ldr r11, [r0, #40] \n" // load r11 from nlr_buf
"ldr r13, [r0, #44] \n" // load r13=sp from nlr_buf
"ldr lr, [r0, #8] \n" // load lr from nlr_buf

"movs r0, #1 \n" // return 1, non-local return
"bx lr \n" // return
: // output operands
: "r"(top) // input operands
: // clobbered registers
);

for (;;); // needed to silence compiler warning
}

What does this function do?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: what does function nlr_jump do?

Post by dhylands » Thu Sep 14, 2017 5:48 pm

nlr_jump is esentially a specialized version of setjmp. nlr_raise is the coorresponding version of longjmp.

It is possible to emulate nlr_hump/raise with setjmp/longjmp.

nlr_jump/raise is the core functionality used to implement exceptions in MicroPython.

I'm not sure how things are today, but when I was doing extensive C++ programming (early 2000) setjmp/longjmp were incompatible with the C++ exception handling mechanism, so you needed to be VERY careful when trying to intermix the two.

Post Reply