annotate lab4/test/mutual.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 (* Mutual recursion *)
mike@0 2
mike@0 3 proc flip(i: integer): integer;
mike@0 4 var r: integer;
mike@0 5 begin
mike@0 6 if i = 0 then
mike@0 7 r := 1
mike@0 8 else
mike@0 9 r := 2 * flop(i-1)
mike@0 10 end;
mike@0 11 print_string("flip("); print_num(i);
mike@0 12 print_string(") = "); print_num(r);
mike@0 13 newline();
mike@0 14 return r
mike@0 15 end;
mike@0 16
mike@0 17 proc flop(i: integer): integer;
mike@0 18 var r: integer;
mike@0 19 begin
mike@0 20 if i = 0 then
mike@0 21 r := 1
mike@0 22 else
mike@0 23 r := flip(i-1) + k
mike@0 24 end;
mike@0 25 print_string("flop("); print_num(i);
mike@0 26 print_string(") = "); print_num(r);
mike@0 27 newline();
mike@0 28 return r
mike@0 29 end;
mike@0 30
mike@0 31 const k = 5;
mike@0 32
mike@0 33 begin
mike@0 34 print_num(flip(5));
mike@0 35 newline()
mike@0 36 end.
mike@0 37
mike@0 38 (*<<
mike@0 39 flop(0) = 1
mike@0 40 flip(1) = 2
mike@0 41 flop(2) = 7
mike@0 42 flip(3) = 14
mike@0 43 flop(4) = 19
mike@0 44 flip(5) = 38
mike@0 45 38
mike@0 46 >>*)
mike@0 47
mike@0 48 (*[[
mike@0 49 @ picoPascal compiler output
mike@0 50 .include "fixup.s"
mike@0 51 .global pmain
mike@0 52
mike@0 53 @ proc flip(i: integer): integer;
mike@0 54 .text
mike@0 55 _flip:
mike@0 56 mov ip, sp
mike@0 57 stmfd sp!, {r0-r1}
mike@0 58 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 59 mov fp, sp
mike@0 60 @ if i = 0 then
mike@0 61 ldr r0, [fp, #40]
mike@0 62 cmp r0, #0
mike@0 63 bne .L7
mike@0 64 @ r := 1
mike@0 65 mov r4, #1
mike@0 66 b .L8
mike@0 67 .L7:
mike@0 68 @ r := 2 * flop(i-1)
mike@0 69 ldr r0, [fp, #40]
mike@0 70 sub r0, r0, #1
mike@0 71 bl _flop
mike@0 72 lsl r4, r0, #1
mike@0 73 .L8:
mike@0 74 @ print_string("flip("); print_num(i);
mike@0 75 mov r1, #5
mike@0 76 set r0, g1
mike@0 77 bl print_string
mike@0 78 ldr r0, [fp, #40]
mike@0 79 bl print_num
mike@0 80 @ print_string(") = "); print_num(r);
mike@0 81 mov r1, #4
mike@0 82 set r0, g2
mike@0 83 bl print_string
mike@0 84 mov r0, r4
mike@0 85 bl print_num
mike@0 86 @ newline();
mike@0 87 bl newline
mike@0 88 @ return r
mike@0 89 mov r0, r4
mike@0 90 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 91 .ltorg
mike@0 92
mike@0 93 @ proc flop(i: integer): integer;
mike@0 94 _flop:
mike@0 95 mov ip, sp
mike@0 96 stmfd sp!, {r0-r1}
mike@0 97 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 98 mov fp, sp
mike@0 99 @ if i = 0 then
mike@0 100 ldr r0, [fp, #40]
mike@0 101 cmp r0, #0
mike@0 102 bne .L11
mike@0 103 @ r := 1
mike@0 104 mov r4, #1
mike@0 105 b .L12
mike@0 106 .L11:
mike@0 107 @ r := flip(i-1) + k
mike@0 108 ldr r0, [fp, #40]
mike@0 109 sub r0, r0, #1
mike@0 110 bl _flip
mike@0 111 add r4, r0, #5
mike@0 112 .L12:
mike@0 113 @ print_string("flop("); print_num(i);
mike@0 114 mov r1, #5
mike@0 115 set r0, g3
mike@0 116 bl print_string
mike@0 117 ldr r0, [fp, #40]
mike@0 118 bl print_num
mike@0 119 @ print_string(") = "); print_num(r);
mike@0 120 mov r1, #4
mike@0 121 set r0, g4
mike@0 122 bl print_string
mike@0 123 mov r0, r4
mike@0 124 bl print_num
mike@0 125 @ newline();
mike@0 126 bl newline
mike@0 127 @ return r
mike@0 128 mov r0, r4
mike@0 129 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 130 .ltorg
mike@0 131
mike@0 132 pmain:
mike@0 133 mov ip, sp
mike@0 134 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 135 mov fp, sp
mike@0 136 @ print_num(flip(5));
mike@0 137 mov r0, #5
mike@0 138 bl _flip
mike@0 139 bl print_num
mike@0 140 @ newline()
mike@0 141 bl newline
mike@0 142 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 143 .ltorg
mike@0 144
mike@0 145 .data
mike@0 146 g1:
mike@0 147 .byte 102, 108, 105, 112, 40
mike@0 148 .byte 0
mike@0 149 g2:
mike@0 150 .byte 41, 32, 61, 32
mike@0 151 .byte 0
mike@0 152 g3:
mike@0 153 .byte 102, 108, 111, 112, 40
mike@0 154 .byte 0
mike@0 155 g4:
mike@0 156 .byte 41, 32, 61, 32
mike@0 157 .byte 0
mike@0 158 @ End
mike@0 159 ]]*)