Implementing setjmp.h

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
xvedejas
Member
Member
Posts: 168
Joined: Thu Jun 04, 2009 5:01 pm

Implementing setjmp.h

Post by xvedejas »

Has anyone implemented setjmp() and longjmp() in their kernel? I thought these might've been builtin functions in C (like sizeof() and offsetof()) but it doesn't seem to be that simple. How would I go about implementing setjmp?
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Implementing setjmp.h

Post by Owen »

GCC provides __builtin_setjmp and __builtin_longjmp. The alternative is to implement it using (inline) assembly.

Ignoring SSE and the FPU, one i386 option would be

Code: Select all

setjmp:	mov ecx, 4[esp]
	mov 0[ecx], ebp
	mov 4[ecx], esi
	mov 8[ecx], edi
	mov 12[ecx], ebx
	mov eax, 0[esp]
	mov 16[ecx], eax
	xor eax, eax
	ret
longjmp:	mov ecx, 4[esp]
	mov ebp, 0[ecx]
	mov esi, 4[ecx]
	mov edi, 8[ecx]
	mov ebx, 12[ecx]
	mov 8[esp], eax
	jmp 16[ecx]16
However, the builtin option will probably perform better. If my memory serves me right, the builtins use a jmp_buf type of "typedef void* env[4];"
Above assembly code untested. MASM dialect
Post Reply