SYSTEM.ALIGN

Report any suspected bugs that you find
Locked
gray
Posts: 113
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

SYSTEM.ALIGN

Post by gray » Mon Nov 27, 2023 1:49 pm

I am having troubles using SYSTEM.ALIGN. My use case was this. Traps.mod contains several distinct exceptions handlers for the different MCU faults, such as Hard Fault, and some more for the M3, which are identical, mutatis mutandis the hard-coded fault number. Below is an attempt to use the same fault handler for all, which simply reads the ISPR to get the current exception number.

Code: Select all

MODULE TestAlign;

  IMPORT SYSTEM;

  CONST MRS_R8_IPSR = 0F3EF8801H; (* assembly instruction to store IPSR in register r8 *)

  PROCEDURE IdentifyTrap(trapNo: INTEGER);
  END IdentifyTrap;

  PROCEDURE FaultTrap;
  BEGIN
    (* ... *)
    SYSTEM.ALIGN;
    SYSTEM.EMIT(MRS_R8_IPSR);
    IdentifyTrap(SYSTEM.REG(8) + 0); (* '+0': work around compiler issue[1] *)
    (* ... *)
    WHILE TRUE DO END
  END FaultTrap;

END TestAlign.
To access the ISPR, I use SYSTEM.EMIT to insert a 32 bit MRS instruction. Whether I actually need to align this code to a word boundary is another question (see below, and [2]), but for the discussion's sake here let's say I do. As the above code shows, I tried to use SYSTEM.ALIGN for this.

From the Astrobe docs:
ALIGN inserts a NOP instruction if necessary at the current code location to ensure that the next instruction is aligned on a word boundary.
On compilation, I get...

Code: Select all

13   18 Error: not a procedure
... with line 13 being the SYSTEM.ALIGN instruction.

This code using SYSTEM.ALIGN() ...

Code: Select all

MODULE TestAlign;

  IMPORT SYSTEM;

  CONST MRS_R8_IPSR = 0F3EF8801H; (* assembly instruction to store IPSR in register r8 *)

  PROCEDURE IdentifyTrap(trapNo: INTEGER);
  END IdentifyTrap;

  PROCEDURE FaultTrap;
  BEGIN
    (* ... *)
    SYSTEM.ALIGN();
    SYSTEM.EMIT(MRS_R8_IPSR);
    IdentifyTrap(SYSTEM.REG(8) + 0);
    (* ... *)
    WHILE TRUE DO END
  END FaultTrap;

END TestAlign.
... results in this compilation error:

Code: Select all

13   19 Error: expression expected
14   11 Error: no )
Clearly I miss something about SYSTEM.ALIGN. How do I use it correctly?

Alignment required?

Regarding code alignment, the ARMv6-M Architecture Reference Manual says:
Instruction alignment and byte ordering
ARMv6-M enforces 16-bit alignment on all instructions. This means that 32-bit instructions are treated as two halfwords, hw1 and hw2 , with hw1 at the lower address.
From the ARMv7-M Architecture Reference Manual:
Instruction alignment and byte ordering
Thumb instruction execution enforces 16-bit alignment on all instructions. This means that 32-bit instructions are treated as two halfwords, hw1 and hw2, with hw1 at the lower address
When do I need to enforce word boundary alignment against this background, ie. when do I need to use SYSTEM.ALIGN? The compiler does not word-align the 32 bit bl.w instruction, but appears to do so for pop.

PS: replacing the different fault handlers by the "unified" one actually works nicely. :)

[1] reported and confirmed, will be fixed
[2] actually no need for alignment in this case

cfbsoftware
Site Admin
Posts: 496
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: SYSTEM.ALIGN

Post by cfbsoftware » Wed Nov 29, 2023 10:30 am

Thank you for your error reports. The ALIGN problems that you have described have now been fixed in maintenance release v9.0.3 of Astrobe for Cortex-M0. This can be downloaded by following your original v9.0 download instructions.

See the Problems fixed section of What's New in Astrobe for Cortex-M0, M3, M4 and M7 for more information.

When you have installed v9.0.3, look at the latest source code of Traps. You will see there our solution for your suggestion to get the IPSR using the MRS instruction (via EMIT) to allow a generic fault handler to be implemented. ALIGN was not required.

You may only need to consider alignment if an instruction is accessing data in flash or RAM. Check the details of each instruction in the ARM Architecture Reference Manual. e.g. LDR (literal)
The immediate offset added to the Align(PC, 4) value of the instruction to form the address.
Permitted values are multiples of four in the range 0 to 1020 for encoding T1.
Many of the NOPs associated with POP and PUSH in v9.0 were a result of us misunderstanding the documentation and have been removed in v9.0.3. I'm sorry if this contributed to the confusion :(

gray
Posts: 113
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

Re: SYSTEM.ALIGN

Post by gray » Sun Dec 03, 2023 5:03 am

Thanks for the fixes and updates with v9.0.3.

Locked