How to handle nested delay slot instructions? #6297
Replies: 4 comments 3 replies
-
Sleigh cannot support this recursive delay slot condition as it can lead to very bad conditions. There are generally other solutions which can be employed to avoid this. What processor are you modeling? This situation can arise with parallel instruction pipelines. |
Beta Was this translation helpful? Give feedback.
-
I can't specify exactly the architecture I'm working on as it is proprietary. However, I've reconstructed the issue in a standalone slaspec I can share here with examples of what I'm running into. slaspec.zip Unfortunately, it's not a parallel instruction pipeline. It's a fairly standard RISC architecture, with a delay slot, and the added wrinkle of allowing multiple consecutive jump instructions as long as only one branch is actually taken. I've developed a workaround using context variables to simulate delay slot behavior, but it's pretty fragile and janky, and requires significant duplication of instruction definitions. It also breaks under certain flow conditions, e.g. if an instruction is both in a delayslot (it comes immediately after a branch) and also the target of a branch, ghidra emits something like:
I'd welcome any more elegant suggestions. |
Beta Was this translation helpful? Give feedback.
-
I would discourage the use of such a large context if it can be avoided. Why are your context field so large (e.g., delay_depth is defined as 32-bits when 2-bits would seem to suffice)? I am having a tough time decipering the true instruction specification from your sample slaspec and how the processors instruction pipeline would operate. Which instruction dictates the existence of a required delayslot? Is it simply every |
Beta Was this translation helpful? Give feedback.
-
You may also like to examine the PA-RISC processor implementation within Ghidra which was unable to use delayslots to address its needs which may be similar. It too allows a branch instruction within the previous branch instruction's delay slot. (see page 4-10 of PA-RISC 1.1 Architecture) |
Beta Was this translation helpful? Give feedback.
-
Since release 6 (see: 1486) nested delay slot instructions are treated as an error (Reserve Instruction Exception).
I am developing SLEIGH definitions for a language which allows nested delay slots, so long as both branches can never be taken at once. This is used extensively in programs on this language to implement jumptables.
I imagine it might be reasonably low effort - though I've no idea where to start - to patch ghidra to allow nested delay slot instructions. In this particular case, if both branches are taken it results in a CPU exception - the disassembler and decompiler can essentially ignore the delayslot for branch instructions where the next instruction is also a branch.
As for a pure sleigh solution, I'm not sure how to handle this without lookahead. from 4390 it looks like lookahead is not supported. I could store off the branch target in a context register and only emit the branch pcode while processing the next instruction, but that runs into issues if a delay instruction is ever a branch target as well
I would appreciate any advice or ideas
Beta Was this translation helpful? Give feedback.
All reactions