Commit 27d5e1f36c

Sahnvour <sahnvour@pm.me>
2019-03-19 22:09:12
c_abi: add some tests for int and float parameter passing potentially using both registers and stack
1 parent c17b163
Changed files (2)
test
stage1
test/stage1/c_abi/cfuncs.c
@@ -18,9 +18,11 @@ void zig_i8(int8_t);
 void zig_i16(int16_t);
 void zig_i32(int32_t);
 void zig_i64(int64_t);
+void zig_five_integers(int32_t, int32_t, int32_t, int32_t, int32_t);
 
 void zig_f32(float);
 void zig_f64(double);
+void zig_five_floats(float, float, float, float, float);
 
 void zig_ptr(void *);
 
@@ -71,9 +73,11 @@ void run_c_tests(void) {
     zig_i16(-2);
     zig_i32(-3);
     zig_i64(-4);
+    zig_five_integers(12, 34, 56, 78, 90);
 
     zig_f32(12.34f);
     zig_f64(56.78);
+    zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f);
 
     zig_ptr((void*)0xdeadbeefL);
 
@@ -156,6 +160,22 @@ void c_bool(bool x) {
     assert_or_panic(x);
 }
 
+void c_five_integers(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e) {
+    assert_or_panic(a == 12);
+    assert_or_panic(b == 34);
+    assert_or_panic(c == 56);
+    assert_or_panic(d == 78);
+    assert_or_panic(e == 90);
+}
+
+void c_five_floats(float a, float b, float c, float d, float e) {
+    assert_or_panic(a == 1.0);
+    assert_or_panic(b == 2.0);
+    assert_or_panic(c == 3.0);
+    assert_or_panic(d == 4.0);
+    assert_or_panic(e == 5.0);
+}
+
 void c_array(uint8_t x[10]) {
     assert_or_panic(x[0] == '1');
     assert_or_panic(x[1] == '2');
test/stage1/c_abi/main.zig
@@ -20,6 +20,17 @@ extern fn c_i16(i16) void;
 extern fn c_i32(i32) void;
 extern fn c_i64(i64) void;
 
+// On windows x64, the first 4 are passed via registers, others on the stack.
+extern fn c_five_integers(i32, i32, i32, i32, i32) void;
+
+export fn zig_five_integers(a: i32, b: i32, c: i32, d: i32, e: i32) void {
+    expect(a == 12);
+    expect(b == 34);
+    expect(c == 56);
+    expect(d == 78);
+    expect(e == 90);
+}
+
 test "C ABI integers" {
     c_u8(0xff);
     c_u16(0xfffe);
@@ -30,6 +41,7 @@ test "C ABI integers" {
     c_i16(-2);
     c_i32(-3);
     c_i64(-4);
+    c_five_integers(12, 34, 56, 78, 90);
 }
 
 export fn zig_u8(x: u8) void {
@@ -60,9 +72,21 @@ export fn zig_i64(x: i64) void {
 extern fn c_f32(f32) void;
 extern fn c_f64(f64) void;
 
+// On windows x64, the first 4 are passed via registers, others on the stack.
+extern fn c_five_floats(f32, f32, f32, f32, f32) void;
+
+export fn zig_five_floats(a: f32, b: f32, c: f32, d: f32, e: f32) void {
+    expect(a == 1.0);
+    expect(b == 2.0);
+    expect(c == 3.0);
+    expect(d == 4.0);
+    expect(e == 5.0);
+}
+
 test "C ABI floats" {
     c_f32(12.34);
     c_f64(56.78);
+    c_five_floats(1.0, 2.0, 3.0, 4.0, 5.0);
 }
 
 export fn zig_f32(x: f32) void {