Fix negative immediate offset handling for JUMPS and JUMPR opcodes #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When providing negative immediate offset (step) values to the JUMPR and JUMPS opcodes, the resulting instruction contained an incorrect offset.
This PR fixes that behaviour.
According to the ESP32 Technical Reference Manual (TRM) the magnitude of the relative shift from the current position is determined by bits 0-6 of the step field, and the direction is determined by bit 7, with 0 meaning
PC+step
, and 1 meaningPC-step
.(For comparison, the ULP C macros in the ESP-IDF implement this correctly, as described in the TRM (code). All step values passed to the relevant JUMP macros will result in the instruction step field having bit 7 indicating the sign and bits 0-6 holding the absolute value of the offset.)
This PR modifies the
I_JUMP_REL{R,S}
macros to set the step field correctly for negative immediate values. Since symbols, which are resolved later during the BFD relocation phase, always evaluate to 0 at this stage (fromEXPR_VALUE
), this change only affects the case of immediate values (as can be seen from all previous test cases resulting in the same listing output as before). For symbols, the relocation code (in functionesp32ulp_jumprelr_reloc
) already did this correctly, and thus that code remains unchanged.Example of the issue:
For an offset of -2, the step field should have looked as follows:
However, the result was actually:
ESP32-S2
The PR also fixes this (as a separate commit) for the ES32-S2 implementation of the same opcodes.
However, for the ESP32-S2 it appears there are three more issues (which this PR does not address):
step_val>>2
), even though the documentation states that step is expressed in 32-bit words. (see this code, that should probably usestep_val>>2
rather thanstep_val
)Cond
field of the JUMPS instruction as being 2 bit wide, while the code uses 3 bits for this field, which makes sense given that e.g. the GE condition has a value of 7. The "condition to jump" description of theCond
field also shows only 3 options (made up of 2 bits), while there are now 5 natively supported conditions (at least according to the code).