### comparison lab4/test/flip.p @ 0:bfdcc3820b32

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