Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen: promote C variadic args as needed #9747

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions spec/compiler/codegen/c_abi/c_abi_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,50 @@ describe "Code gen: C ABI" do
(str.x + str.y + str.z).to_i32
), &.to_i.should eq(6))
end

it "promotes variadic args (float to double)" do
test_c(
%(
#include <stdarg.h>

double foo(int n, ...) {
va_list args;
va_start(args, n);
return va_arg(args, double);
}
),
%(
lib LibFoo
fun foo(n : Int32, ...) : Float64
end

LibFoo.foo(1, 1.0_f32)
), &.to_f64.should eq(1.0))
end

[{"i8", -123},
{"u8", 255},
{"i16", -123},
{"u16", 65535},
].each do |int_kind, int_value|
it "promotes variadic args (#{int_kind} to i32) (#9742)" do
test_c(
%(
#include <stdarg.h>

int foo(int n, ...) {
va_list args;
va_start(args, n);
return va_arg(args, int);
}
),
%(
lib LibFoo
fun foo(n : Int32, ...) : Int32
end

LibFoo.foo(1, #{int_value}_#{int_kind})
), &.to_i.should eq(int_value))
end
end
end
19 changes: 17 additions & 2 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class Crystal::CodeGenVisitor
call_args << sret_value
end

target_def_args_size = target_def.args.size

node.args.each_with_index do |arg, i|
if arg.is_a?(Out)
has_out = true
Expand Down Expand Up @@ -217,13 +219,26 @@ class Crystal::CodeGenVisitor
case abi_arg_type.kind
when LLVM::ABI::ArgKind::Direct
call_arg = codegen_direct_abi_call(call_arg, abi_arg_type) unless arg.type.nil_type?
call_args << call_arg
when LLVM::ABI::ArgKind::Indirect
# Pass argument as is (will be passed byval)
call_args << call_arg
when LLVM::ABI::ArgKind::Ignore
# Ignore
next
end

# If we are passing variadic arguments there are some special rules
if i >= target_def_args_size
arg_type = arg.type.remove_indirection
if arg_type.is_a?(FloatType) && arg_type.kind == :f32
# Floats must be passed as doubles (there are no float varargs)
call_arg = extend_float @program.float64, call_arg
elsif arg_type.is_a?(IntegerType) && arg_type.kind.in?(:i8, :u8, :i16, :u16)
# Integer with a size less that `int` must be converted to `int`
call_arg = extend_int arg_type, @program.int32, call_arg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess technically this should check what the target platform C int type is, but in practice all platforms we support so far have int = int32_t. Just something to keep in mind in case somebody wants to target something weird or old.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought of this and reached the same conclusion. Also, I don't know how to know what's the target platform size of int.

Copy link
Contributor

@yxhuvud yxhuvud Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sizeof(LibC::Int) ? Or that is perhaps only the local compiler size of it?

Copy link
Member Author

@asterite asterite Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the size of the host machine. It won't work when cross-compiling.

end
end

call_args << call_arg
end

@needs_value = old_needs_value
Expand Down