annotate lab4/test/twice.p @ 1:b5139af1a420 tip basis

Fixed permissions on compile scripts
author Mike Spivey <mike@cs.ox.ac.uk>
date Fri, 13 Oct 2017 17:27:58 +0100
parents bfdcc3820b32
children
rev   line source
mike@0 1 (* Another test of higher-order functions *)
mike@0 2
mike@0 3 type int = integer;
mike@0 4
mike@0 5 proc square(x: int): int; begin return x * x end;
mike@0 6
mike@0 7 proc twice(proc f(y: int): int; x: int): int;
mike@0 8 begin return f(f(x)) end;
mike@0 9
mike@0 10 proc ap_to_sq(proc ff(proc f(x: int): int; x: int): int; x: int): int;
mike@0 11 begin return ff(square, x) end;
mike@0 12
mike@0 13 begin
mike@0 14 print_num(ap_to_sq(twice, 3));
mike@0 15 newline()
mike@0 16 end.
mike@0 17
mike@0 18 (*<<
mike@0 19 81
mike@0 20 >>*)
mike@0 21
mike@0 22 (*[[
mike@0 23 @ picoPascal compiler output
mike@0 24 .include "fixup.s"
mike@0 25 .global pmain
mike@0 26
mike@0 27 @ proc square(x: int): int; begin return x * x end;
mike@0 28 .text
mike@0 29 _square:
mike@0 30 mov ip, sp
mike@0 31 stmfd sp!, {r0-r1}
mike@0 32 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 33 mov fp, sp
mike@0 34 @ proc square(x: int): int; begin return x * x end;
mike@0 35 ldr r4, [fp, #40]
mike@0 36 mul r0, r4, r4
mike@0 37 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 38 .ltorg
mike@0 39
mike@0 40 @ proc twice(proc f(y: int): int; x: int): int;
mike@0 41 _twice:
mike@0 42 mov ip, sp
mike@0 43 stmfd sp!, {r0-r3}
mike@0 44 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 45 mov fp, sp
mike@0 46 @ begin return f(f(x)) end;
mike@0 47 ldr r4, [fp, #40]
mike@0 48 ldr r5, [fp, #44]
mike@0 49 ldr r0, [fp, #48]
mike@0 50 mov r10, r5
mike@0 51 blx r4
mike@0 52 mov r10, r5
mike@0 53 blx r4
mike@0 54 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 55 .ltorg
mike@0 56
mike@0 57 @ proc ap_to_sq(proc ff(proc f(x: int): int; x: int): int; x: int): int;
mike@0 58 _ap_to_sq:
mike@0 59 mov ip, sp
mike@0 60 stmfd sp!, {r0-r3}
mike@0 61 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 62 mov fp, sp
mike@0 63 @ begin return ff(square, x) end;
mike@0 64 ldr r2, [fp, #48]
mike@0 65 mov r1, #0
mike@0 66 set r0, _square
mike@0 67 ldr r10, [fp, #44]
mike@0 68 ldr r3, [fp, #40]
mike@0 69 blx r3
mike@0 70 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 71 .ltorg
mike@0 72
mike@0 73 pmain:
mike@0 74 mov ip, sp
mike@0 75 stmfd sp!, {r4-r10, fp, ip, lr}
mike@0 76 mov fp, sp
mike@0 77 @ print_num(ap_to_sq(twice, 3));
mike@0 78 mov r2, #3
mike@0 79 mov r1, #0
mike@0 80 set r0, _twice
mike@0 81 bl _ap_to_sq
mike@0 82 bl print_num
mike@0 83 @ newline()
mike@0 84 bl newline
mike@0 85 ldmfd fp, {r4-r10, fp, sp, pc}
mike@0 86 .ltorg
mike@0 87
mike@0 88 @ End
mike@0 89 ]]*)