         Edinburgh IMP77 Compiler - Version 8.4-2024.12.18

     1  !
     2  ! The hand-generated code below should be producable by PSR's compiler if it:
     3  ! A) delays stores as late as possible
     4  !
     5  ! B) remembers registers over procedure calls
     6  !
     7  ! C) has a general algorithm for drop-through conditionally executed
     8  !    instructions, i.e. if there are no instructions between a conditional 
     9  !    branch and its destination which set the condition codes, and the
    10  !    sequence is less that a certain length, then all the instructions
    11  !    within the sequence can be made conditional.
    12  !
    13  ! D) does as above, but also allows instructions within the sequence
    14  !    whose subsequent execution depends on the same condition, e.g.
    15  !
    16  !    %if a=b %and c=d %then a=b
    17  !
    18  !    CmpS   R0,R1            NAIVE STYLE
    19  !    Bne    elsepart
    20  !    Cmps   R2,R3
    21  !    Bne    elsepart
    22  !    Mov    R0,R2
    23  !
    24  !    CmpS   R0,R1            RULE (C) ABOVE
    25  !    Bne    elsepart
    26  !    CmpS   R2,R3
    27  !    MovEQ  R0,R2
    28  !
    29  !    CmpS   R0,R1            RULE (D) SPECIAL CASE
    30  !    CmpSEQ R2,R3
    31  !    MovEQ  R0,R2
    32  !
    33  ! E) alters conditions to generate similar condition codes on
    34  !    condition AND condition AND condition to generate more cases
    35  !    to which rule (D) can be applied.
    36  !
    37  ! F) adjusts constants in < or <= cases to favour above, i.e. turning
    38  !    CMP R,#const & BLE ... into CMP R,#const-1 & BLT ... etc.
    39  !
    40  ! G) changes Newline back from the current implicit printsymbol(10) -
    41  !    there is no run-time overhead in moving the MOV R0,#10 over to
    42  !    the procedure body instead of having it in every call where it
    43  !    takes up space - after all, Peter's code-moving stuff ought to
    44  !    get %permroutine newline;printsymbol(10);%end correct - it will
    45  !    place the Mov R0,#10 before the body of %permroutine printsymbol
    46  !    and generate a suitable entry point for it...
    47  !       Personally, I would recommend stacking/restoring R0 as well
    48  !    as it might keep another variable in a register for a bit longer...
    49  !
    50  ! I've only suggested optimisations which fit into the style of PSR's
    51  ! compilers...
    52  !
    53  
    54  
    55  %begin
 0000 C8 00 00 01                           ENTER 0000,1
    56  %integer a,i,b
    57  
    58    read(a)
 0004 68 83 00 00 00                        PUSH #131
 0009 8D 45 F8                              LEA %EAX,[%EBP-8]
 000C 50                                    PUSH %EAX
 000D E8 4E 00                              CALL 'READ' (EXTERN 78)
 0010 83 C4 08                              ADD %ESP,#8
    59  ! BL     read_fn
    60  
    61  ! Str    R0,a
    62    read(i)
 0013 68 83 00 00 00                        PUSH #131
 0018 8D 45 F4                              LEA %EAX,[%EBP-12]
 001B 50                                    PUSH %EAX
 001C E8 4E 00                              CALL 'READ' (EXTERN 78)
 001F 83 C4 08                              ADD %ESP,#8
    63  ! BL     read_fn
    64  
    65  ! Str    R0,i
    66    read(b)
 0022 68 83 00 00 00                        PUSH #131
 0027 8D 45 F0                              LEA %EAX,[%EBP-16]
 002A 50                                    PUSH %EAX
 002B E8 4E 00                              CALL 'READ' (EXTERN 78)
 002E 83 C4 08                              ADD %ESP,#8
    67  ! BL     read_fn
    68  
    69    %if a <= i <= b %start
 0031 8B 45 F4                              MOV %EAX,[%EBP-12]
 0034 3B 45 F8                              CMP %EAX,[%EBP-8]
 0037 7C 00                                 JL L1001
 0039 3B 45 F0                              CMP %EAX,[%EBP-16]
 003C 7F 00                                 JG L1001
    70  ! Ldr R1,a
    71  ! Ldr R2,i
    72  ! CmpS   R1,R2
    73  ! CmpSLE R2,R0
    74      i = 22
 003E C7 45 F4 16 00 00 00                  MOV DWORD PTR [%EBP-12],#22
    75  ! MovLE  R2,#22
    76    %finish
 0045                      L1001  EQU $
    77  
    78    %if 22 <= i <= b %start
 0045 8B 45 F4                              MOV %EAX,[%EBP-12]
 0048 3D 16 00 00 00                        CMP %EAX,#22
 004D 7C 00                                 JL L1002
 004F 3B 45 F0                              CMP %EAX,[%EBP-16]
 0052 7F 00                                 JG L1002
    79  ! CmpS   R2,#22-1
    80  ! CmpSGT R2,R0
    81      i = 44
 0054 C7 45 F4 2C 00 00 00                  MOV DWORD PTR [%EBP-12],#44
    82  ! MovGT  R2,#44
    83    %finish
 005B                      L1002  EQU $
    84  
    85    %if a <= i <= 77 %start
 005B 8B 45 F4                              MOV %EAX,[%EBP-12]
 005E 3B 45 F8                              CMP %EAX,[%EBP-8]
 0061 7C 00                                 JL L1003
 0063 3D 4D 00 00 00                        CMP %EAX,#77
 0068 7F 00                                 JG L1003
    86  ! CmpS   R1,R2
    87  ! CmpSLE R2,#77
    88      i = 99
 006A C7 45 F4 63 00 00 00                  MOV DWORD PTR [%EBP-12],#99
    89  ! MovLE  R2,#99
    90    %finish
 0071                      L1003  EQU $
    91  
    92    %if 11 <= i <= 100 %start     ;! Peter's special case when both const is good
 0071 8B 45 F4                              MOV %EAX,[%EBP-12]
 0074 3D 0B 00 00 00                        CMP %EAX,#11
 0079 7C 00                                 JL L1004
 007B 3D 64 00 00 00                        CMP %EAX,#100
 0080 7F 00                                 JG L1004
    93  ! Sub   R2,#11                  ;! but usually corrupts a register needed later
    94  ! CmpS  R2,#100-11
    95      i = 1
 0082 C7 45 F4 01 00 00 00                  MOV DWORD PTR [%EBP-12],#1
    96  ! MovLS R2,#1
    97    %finish
 0089                      L1004  EQU $
    98  
    99  ! Str   R0,b
   100    write(a,0)
 0089 FF 75 F8                              PUSH DWORD PTR [%EBP-8]
 008C 6A 00                                 PUSH #0
 008E E8 40 00                              CALL 'WRITE' (EXTERN 64)
 0091 83 C4 08                              ADD %ESP,#8
   101  ! Mov   R0,R1
   102  ! Mov   R1,#0
   103  ! Bl    Write
   104  
   105    newline
 0094 E8 43 00                              CALL 'NEWLINE' (EXTERN 67)
   106  ! Bl    Newline
   107  
   108  ! Mov   R0,R2
   109    write(i,0)
 0097 FF 75 F4                              PUSH DWORD PTR [%EBP-12]
 009A 6A 00                                 PUSH #0
 009C E8 40 00                              CALL 'WRITE' (EXTERN 64)
 009F 83 C4 08                              ADD %ESP,#8
   110  ! Bl    write
   111  
   112    newline
 00A2 E8 43 00                              CALL 'NEWLINE' (EXTERN 67)
   113  ! Bl    newline
   114  
   115    write(b,0)
 00A5 FF 75 F0                              PUSH DWORD PTR [%EBP-16]
 00A8 6A 00                                 PUSH #0
 00AA E8 40 00                              CALL 'WRITE' (EXTERN 64)
 00AD 83 C4 08                              ADD %ESP,#8
   116  ! Ldr   R0,b
   117  ! Bl    write
   118  
   119    newline
 00B0 E8 43 00                              CALL 'NEWLINE' (EXTERN 67)
   120  ! Bl    newline
   121  
   122  %endofprogram
 00B3 C9                                    LEAVE
 00B4 C3                                    RET

    24 Statements compiled



 ELF object file generated from IMP source file: '/home/gtoal/src/compilers101/new-parser/imps/tests/cond2s.imp'
 +----------+---------------------+---------+---------+---------+------------+
 | Sections |       Symbols       | Code    | Data    | Diag    | Total size |
 +----------+----------+----------+---------+---------+---------+------------+
 |  (count) | Internal | External | (bytes) | (bytes) | (bytes) | (bytes)    |
 +----------+----------+----------+---------+---------+---------+------------+
 |       11 |        4 |        9 |     199 |       0 |      32 |        231 |
 +----------+----------+----------+---------+---------+---------+------------+


