annotate lab4/test/flip.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 (* Nested functions with mutual recursion *)
mike@0 2
mike@0 3 proc flip(x: integer): integer;
mike@0 4 proc flop(y: integer): integer;
mike@0 5 begin
mike@0 6 if y = 0 then return 1 else return flip(y-1) + x end
mike@0 7 end;
mike@0 8 begin
mike@0 9 if x = 0 then return 1 else return 2 * flop(x-1) end
mike@0 10 end;
mike@0 11
mike@0 12 begin
mike@0 13 print_num(flip(5));
mike@0 14 newline()
mike@0 15 end.
mike@0 16
mike@0 17 (* flip(5) = 2 * flop(4) = 2 * (flip(3) + 5)
mike@0 18 = 4 * flop(2) + 10 = 4 * (flip(1) + 3) + 10
mike@0 19 = 8 * flop(0) + 22 = 30 *)
mike@0 20
mike@0 21 (*<<
mike@0 22 30
mike@0 23 >>*)
mike@0 24
mike@0 25 (*[[
mike@0 26 @ picoPascal compiler output
mike@0 27 .include "fixup.s"
mike@0 28 .global pmain
mike@0 29
mike@0 30 @ proc flip(x: integer): integer;
mike@0 31 .text
mike@0 32 _flip:
mike@0 33 mov ip, sp
mike@0 34 stmfd sp!, {r0-r1}
mike@0 35 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 36 mov fp, sp
mike@0 37 @ if x = 0 then return 1 else return 2 * flop(x-1) end
mike@0 38 ldr r0, [fp, #40]
mike@0 39 cmp r0, #0
mike@0 40 bne .L3
mike@0 41 mov r0, #1
mike@0 42 b .L1
mike@0 43 .L3:
mike@0 44 ldr r0, [fp, #40]
mike@0 45 sub r0, r0, #1
mike@0 46 mov r10, fp
mike@0 47 bl _flop
mike@0 48 lsl r0, r0, #1
mike@0 49 .L1:
mike@0 50 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 51 .ltorg
mike@0 52
mike@0 53 @ proc flop(y: integer): integer;
mike@0 54 _flop:
mike@0 55 mov ip, sp
mike@0 56 stmfd sp!, {r0-r1}
mike@0 57 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 58 mov fp, sp
mike@0 59 @ if y = 0 then return 1 else return flip(y-1) + x end
mike@0 60 ldr r0, [fp, #40]
mike@0 61 cmp r0, #0
mike@0 62 bne .L7
mike@0 63 mov r0, #1
mike@0 64 b .L5
mike@0 65 .L7:
mike@0 66 ldr r0, [fp, #40]
mike@0 67 sub r0, r0, #1
mike@0 68 bl _flip
mike@0 69 ldr r1, [fp, #24]
mike@0 70 ldr r1, [r1, #40]
mike@0 71 add r0, r0, r1
mike@0 72 .L5:
mike@0 73 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 74 .ltorg
mike@0 75
mike@0 76 pmain:
mike@0 77 mov ip, sp
mike@0 78 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 79 mov fp, sp
mike@0 80 @ print_num(flip(5));
mike@0 81 mov r0, #5
mike@0 82 bl _flip
mike@0 83 bl print_num
mike@0 84 @ newline()
mike@0 85 bl newline
mike@0 86 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 87 .ltorg
mike@0 88
mike@0 89 @ End
mike@0 90 ]]*)