authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-06-10 11:46:27+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-10 10:22:08-07:00
log29386f0d3021f4868ac021f05ff47455f011d674
treeb77b8d4ea600eb576f26b0ae7c4c94863fc98f12
parent80e1797fafb27b780135207de3e4a1fd6e36a254

stage1: Fix handling of C ABI parameters split in multiple regs

Take into account the increased number of parameters when flattening a structure into one or more SSE registers. Fixes #9061

3 files changed, 12 insertions(+), 2 deletions(-)

src/stage1/codegen.cpp+2-2
...@@ -2161,11 +2161,11 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2161,11 +2161,11 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2161 // Register 2: (ptr + 1).*2161 // Register 2: (ptr + 1).*
21622162
2163 // One floating point register per f64 or 2 f32's2163 // One floating point register per f64 or 2 f32's
2164 size_t number_of_fp_regs = (size_t)ceilf((float)ty_size / (float)8);2164 size_t number_of_fp_regs = (ty_size + 7) / 8;
21652165
2166 switch (fn_walk->id) {2166 switch (fn_walk->id) {
2167 case FnWalkIdAttrs: {2167 case FnWalkIdAttrs: {
2168 fn_walk->data.attrs.gen_i += 1;2168 fn_walk->data.attrs.gen_i += number_of_fp_regs;
2169 break;2169 break;
2170 }2170 }
2171 case FnWalkIdCall: {2171 case FnWalkIdCall: {
test/stage1/c_abi/cfuncs.c+8
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1#include <inttypes.h>1#include <inttypes.h>
2#include <stdlib.h>2#include <stdlib.h>
3#include <stdbool.h>3#include <stdbool.h>
4#include <string.h>
45
5void zig_panic();6void zig_panic();
67
...@@ -253,6 +254,13 @@ void c_small_struct_floats(Vector3 vec) {...@@ -253,6 +254,13 @@ void c_small_struct_floats(Vector3 vec) {
253 assert_or_panic(vec.z == 12.0);254 assert_or_panic(vec.z == 12.0);
254}255}
255256
257void c_small_struct_floats_extra(Vector3 vec, const char *str) {
258 assert_or_panic(vec.x == 3.0);
259 assert_or_panic(vec.y == 6.0);
260 assert_or_panic(vec.z == 12.0);
261 assert_or_panic(!strcmp(str, "hello"));
262}
263
256void c_big_struct_floats(Vector5 vec) {264void c_big_struct_floats(Vector5 vec) {
257 assert_or_panic(vec.x == 76.0);265 assert_or_panic(vec.x == 76.0);
258 assert_or_panic(vec.y == -1.0);266 assert_or_panic(vec.y == -1.0);
test/stage1/c_abi/main.zig+2
...@@ -257,6 +257,7 @@ const Vector3 = extern struct {...@@ -257,6 +257,7 @@ const Vector3 = extern struct {
257 z: f32,257 z: f32,
258};258};
259extern fn c_small_struct_floats(Vector3) void;259extern fn c_small_struct_floats(Vector3) void;
260extern fn c_small_struct_floats_extra(Vector3, ?[*]const u8) void;
260261
261const Vector5 = extern struct {262const Vector5 = extern struct {
262 x: f32,263 x: f32,
...@@ -274,6 +275,7 @@ test "C ABI structs of floats as parameter" {...@@ -274,6 +275,7 @@ test "C ABI structs of floats as parameter" {
274 .z = 12.0,275 .z = 12.0,
275 };276 };
276 c_small_struct_floats(v3);277 c_small_struct_floats(v3);
278 c_small_struct_floats_extra(v3, "hello");
277279
278 var v5 = Vector5{280 var v5 = Vector5{
279 .x = 76.0,281 .x = 76.0,