Skip to content

Commit

Permalink
Compiler: rename considered_external to c_calling_convention and …
Browse files Browse the repository at this point in the history
…don't use it for fun linkage.
Ary Borenszweig committed Oct 16, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 130ee24 commit 9119c33
Showing 4 changed files with 25 additions and 25 deletions.
24 changes: 12 additions & 12 deletions src/compiler/crystal/codegen/ast.cr
Original file line number Diff line number Diff line change
@@ -97,20 +97,20 @@ module Crystal
nil
end

@considered_external : Bool? = nil
setter considered_external

# Returns `self` as an `External` if this Def must be considered
# an external in the codegen, meaning we need to respect the C ABI.
def considered_external?
if @considered_external.nil?
@considered_external = compute_considered_external
@c_calling_convention : Bool? = nil
setter c_calling_convention

# Returns `self` as an `External` if this Def is an External
# that must respect the C calling convention.
def c_calling_convention?
if @c_calling_convention.nil?
@c_calling_convention = compute_c_calling_convention
end

@considered_external ? self : nil
@c_calling_convention ? self : nil
end

private def compute_considered_external
private def compute_c_calling_convention
# One case where this is not true if for LLVM instrinsics.
# For example overflow intrincis return a tuple, like {i32, i1}:
# in C ABI that is represented as i64, but we need to keep the original
@@ -129,10 +129,10 @@ module Crystal
return false
end

proc_considered_external?
proc_c_calling_convention?
end

def proc_considered_external?
def proc_c_calling_convention?
# We use C ABI if:
# - all arguments are allowed in lib calls (because then it can be passed to C)
# - at least one argument type, or the return type, is an extern struct
6 changes: 3 additions & 3 deletions src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ class Crystal::CodeGenVisitor

def prepare_call_args(node, owner)
target_def = node.target_def
if external = target_def.considered_external?
if external = target_def.c_calling_convention?
prepare_call_args_external(node, external, owner)
else
prepare_call_args_non_external(node, target_def, owner)
@@ -449,7 +449,7 @@ class Crystal::CodeGenVisitor

set_call_attributes node, target_def, self_type, is_closure, fun_type

external = target_def.try &.considered_external?
external = target_def.try &.c_calling_convention?

if external && (external.type.proc? || external.type.is_a?(NilableProcType))
fun_ptr = bit_cast(@last, LLVM::VoidPointer)
@@ -500,7 +500,7 @@ class Crystal::CodeGenVisitor
end

def set_call_attributes(node : Call, target_def, self_type, is_closure, fun_type)
if external = target_def.considered_external?
if external = target_def.c_calling_convention?
set_call_attributes_external(node, external)
else
set_call_attributes_non_external(node, target_def, self_type, is_closure, fun_type)
10 changes: 5 additions & 5 deletions src/compiler/crystal/codegen/fun.cr
Original file line number Diff line number Diff line change
@@ -136,9 +136,9 @@ class Crystal::CodeGenVisitor
end

def codegen_return(target_def : Def)
# Check if this def is considered external and the return
# Check if this def must use the C calling convention and the return
# value must be either casted or passed by sret
if target_def.considered_external? && (abi_info = target_def.abi_info)
if target_def.c_calling_convention? && (abi_info = target_def.abi_info)
ret_type = abi_info.return_type
if cast = ret_type.cast
casted_last = bit_cast @last, cast.pointer
@@ -158,7 +158,7 @@ class Crystal::CodeGenVisitor
end

def codegen_fun_signature(mangled_name, target_def, self_type, is_fun_literal, is_closure)
if !is_closure && (external = target_def.considered_external?)
if !is_closure && (external = target_def.c_calling_convention?)
codegen_fun_signature_external(mangled_name, external)
else
codegen_fun_signature_non_external(mangled_name, target_def, self_type, is_fun_literal, is_closure)
@@ -207,7 +207,7 @@ class Crystal::CodeGenVisitor

setup_context_fun(mangled_name, target_def, llvm_args_types, llvm_return_type)

if @single_module && !target_def.no_inline? && target_def.considered_external?
if @single_module && !target_def.no_inline? && !target_def.is_a?(External)
context.fun.linkage = LLVM::Linkage::Internal
end

@@ -398,7 +398,7 @@ class Crystal::CodeGenVisitor
else
# If it's an extern struct on a def that must be codegened with C ABI
# compatibility, and it's not passed byval, we must cast the value
if target_def.considered_external? && arg.type.extern? && !value.attributes.by_val?
if target_def.c_calling_convention? && arg.type.extern? && !value.attributes.by_val?
pointer = alloca(llvm_type(var_type), arg.name)
casted_pointer = bit_cast pointer, value.type.pointer
store value, casted_pointer
10 changes: 5 additions & 5 deletions src/compiler/crystal/codegen/primitives.cr
Original file line number Diff line number Diff line change
@@ -648,9 +648,9 @@ class Crystal::CodeGenVisitor
# closures are never generated with C ABI because C doesn't support closures.
# But non-closures use C ABI, so if the target Proc is not a closure we cast the
# arguments according to the ABI.
# For this we temporarily set the target_def's `abi_info` and `considered_external`
# For this we temporarily set the target_def's `abi_info` and `c_calling_convention`
# properties for the non-closure branch, and then reset it.
if target_def.proc_considered_external?
if target_def.proc_c_calling_convention?
null_fun_ptr, null_args = codegen_extern_primitive_proc_call(target_def, args, fun_ptr)
else
null_fun_ptr, null_args = real_fun_ptr, args
@@ -659,9 +659,9 @@ class Crystal::CodeGenVisitor
value = codegen_call_or_invoke(node, target_def, nil, null_fun_ptr, null_args, true, target_def.type, false, proc_type)
phi.add value, node.type

# Reset abi_info + considered_external so the closure part is generated as usual
# Reset abi_info + c_calling_convention so the closure part is generated as usual
target_def.abi_info = nil
target_def.considered_external = nil
target_def.c_calling_convention = nil

position_at_end ctx_is_not_null_block
real_fun_ptr = bit_cast fun_ptr, llvm_closure_type(context.type)
@@ -717,7 +717,7 @@ class Crystal::CodeGenVisitor

null_fun_llvm_type = LLVM::Type.function(null_fun_types, null_fun_return_type)
null_fun_ptr = bit_cast fun_ptr, null_fun_llvm_type.pointer
target_def.considered_external = true
target_def.c_calling_convention = true

{null_fun_ptr, null_args}
end

0 comments on commit 9119c33

Please sign in to comment.