Skip to content

Unnecessary instructions in LOOP forms #8

@takagi

Description

@takagi

Situation
The language's loop form, currently implemented as a PIC macro, is compiled into instructions including SUBWF, BTFSC and a recursive function call.

For example, the following expression:

(loop 10
  0)        ; nop

is expanded into:

(let ((_loop (i)
        (if (= i 0)
          0
          (progn
            0
            (_loop (- i 1))))))
  (_loop 10)

then, is compiled into:

        MOVLW    00Ah
        MOVLW    I0
        GOTO     __LOOP1014
__LOOP1014
        MOVF     I0,W
        MOVWF    L0
        MOVLW   000h
        SUBWF   L0,W
        BTFSC   STATUS,Z
        GOTO    _ELSE10
        RETLW   000h
_ELSE10
        MOVLW   001h
        SUBWF   L0,W
        MOVWF   L1
        MOVF    L1,W
        MOVWF   I0
        GOTO    __LOOP1014

Problem
There are unnecessary instructions in the compiled assembly compared with the case using DECFSZ instruction.

  • get from and put into an input pseudo-register
  • the first GOTO instruction
  • a conditional branch with SUBWF and BTFSC instructions
  • SUBWF instruction for decrement

Goal
Make loop forms compiled into instructions using DECFSZ instruction.

        MOVLW    00Ah
        MOVWF    L0
__LOOP
        DECFSZ   L0,F
        GOTO     __LOOP
        RETLW    000h

Approach
The following approaches are considerable:

  • introducing loop syntax, not as a PIC macro
  • identifying loop structure to be optimized (chains of recurrences?)

Notes
There are some notes.

  • updating mdelay1 function's magic number

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions