raz / test / mac / NarrowArgs.raz

48 lines · 1.7 KiB · raw · history · last change b6a29ae3

1(* Calling C with arguments of every width past the registers (PLAN.md 5,
2 step 6): Apple's convention packs a C procedure's own arguments on the
3 stack by their natural size, where a raz procedure's take 8 bytes
4 each. narrow has eight integers in x0 to x7, six more on the stack
5 (an I8, a U8, an I16, an I32, an I8, an I64), eight reals in v0 to v7
6 and an F32, an F64 and an I8 past them. varsum is variadic, whose
7 arguments past its own go on the stack, 8 bytes each. callback has C
8 call a raz procedure of ten integers. Each answer is a hash C makes of
9 what it received; NarrowArgs.c, built as a program, prints the same. *)
10module NarrowArgs;
11import Out;
12import SYSTEM;
13link "NarrowArgs";
14
15extern proc narrow(a: I8; b: U8; c: I16; d: I32; e: I64; f: U8; g: I16; h: I32;
16 i: I8; j: U8; k: I16; l: I32; m: I8; n: I64;
17 p: F32; q: F64; r: F32; s: F64; t: F32; u: F64; v: F32; w: F64;
18 y: F32; z: F64; last: I8): I64;
19extern proc varsum(n: I32; ...): I64;
20extern proc callback(cb: proc(a, b, c, d, e, f, g, h, i, j: I64): I64): I64;
21
22(* the same hash as C's mix *)
23proc mix(h, v: I64): I64;
24begin
25 return I64(U64(h) * 1000003 + U64(v));
26end;
27
28proc mine(a, b, c, d, e, f, g, h, i, j: I64): I64;
29var x: I64;
30begin
31 x := 7;
32 x := mix(x, a); x := mix(x, b); x := mix(x, c); x := mix(x, d); x := mix(x, e);
33 x := mix(x, f); x := mix(x, g); x := mix(x, h); x := mix(x, i); x := mix(x, j);
34 return x;
35end;
36
37proc main*;
38begin
39 Out.int(narrow(-1, 200, -300, -70000, -5000000000, 255, -32768, 2147483647,
40 -128, 129, -2, -100000, 127, 9000000000,
41 1.5, -2.25, 3.5, 4.75, -5.5, 6.25, 7.5, -8.75,
42 9.5, 10.25, -100)); Out.ln;
43 Out.int(varsum(5, 1, 1.5, -3, -2.25, 5)); Out.ln;
44 Out.int(callback(mine)); Out.ln;
45 Out.flush;
46end;
47
48end NarrowArgs.