Can I set the stack pointer in LLVM?

I'm working on a small c++-like language which I'll be compiling to LLVM. One of the things I want to implement is cooperative multitasking; there will be a "yield" operator which will hopefully switch the stack pointer and program counter to the next "thread" in my program.

Is it possible to do this in llvm? Can I set the stack pointer register? If not, is there anything else similar I can do?

Edit: LLVM coroutines (http://llvm.org/docs/Coroutines.html) sound promising, though https://internals.rust-lang.org/t/llvm-coroutines-to-bring-awarness/3708/12 brings up some questions regarding stackful or stackless coroutines. I wonder, can they be used to implement a general yield-like operator?

Edit 2: In c++ boost has something called a "context" which can implement stackful coroutines. Still trying to figure out how they do it though. Anyone know?


Assuming you have the gcd library available: You can easily implement cooperative multitasking by using a semaphore (dispatch_semaphore_t). The semaphore count is set up so that exactly one of your threads can run at the same time. The yield () function signals and immediately locks the semaphore - the signal () wakes up another thread, and the lock stops the thread that called yield.

链接地址: http://www.djcxy.com/p/53192.html

上一篇: 具有`上下文与发生器/协程/任务的Python'

下一篇: 我可以在LLVM中设置堆栈指针吗?