-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[WIP] Intrinsics now platform dependent. LibC::PtrdiffT type introduced. #4624
Conversation
…g two pointers. ptrdiff_t is used for pointer arithmetic and array indexing, if negative values are possible. Refs crystal-lang#823, crystal-lang#4589.
Got failure on 32bit build. Will check. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for trying. You hit the same pitfall than me.
@@ -0,0 +1,20 @@ | |||
lib Intrinsics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intrinsics aren't LibC bindings (i.e. calls to external symbols) but LLVM intrinsics (inlined calls); they won't be generated automatically; they aren't system dependent (but arch dependent).
I.e.: they have nothing to do under LibC
, please put them back as src/intrinsics.cr
or maybe src/crystal/intrinsics.cr
, along with the flag macro to select either the u32 or u64 calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like idea of separated files per arch. Will try to rework it accordingly.
@@ -242,7 +242,7 @@ struct Pointer(T) | |||
raise ArgumentError.new("Negative count") if count < 0 | |||
|
|||
if self.class == source.class | |||
Intrinsics.memcpy(self.as(Void*), source.as(Void*), (count * sizeof(T)).to_u32, 0_u32, false) | |||
Intrinsics.memcpy(self.as(Void*), source.as(Void*), Intrinsics::SizeT.new(count) * sizeof(T), 0_u32, false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why introduce Intrinsics::SizeT
but use LibC::SizeT
below? This is confusing and a duplication.
@@ -395,7 +395,7 @@ struct Pointer(T) | |||
# ptr.address # => 0 | |||
# ``` | |||
def self.null | |||
new 0_u64 | |||
new LibC::SizeT.new(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
size_t
is a length type (as in allocation size), you meantintptr_t
(pointer size);- you must hack the compiler for this to compile on 32bit platforms (
Pointer.new(UInt64)
is a Crystal intrinsic).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it was a big mistake.
@@ -405,7 +405,7 @@ struct Pointer(T) | |||
# ptr.address # => 5678 | |||
# ``` | |||
def self.new(address : Int) | |||
new address.to_u64 | |||
new LibC::SizeT.new(address) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@@ -78,7 +78,7 @@ struct Pointer(T) | |||
# ptr2.address # => 1238 | |||
# ``` | |||
def +(other : Int) | |||
self + other.to_i64 | |||
self + LibC::PtrdiffT.new(other) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must hack the compiler for this to compile on 32bit platforms (Pointer#+(Int64)
is a Crystal intrinsic). See below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
I should close this because another approach has been landed - #4960. |
Intrinsics now declared per architecture.
LibC::PtrdiffT is the signed integer type of the result of subtracting two pointers.
LibC::PtrdiffT is used for pointer arithmetic and array indexing, if negative values are possible.
Refs #823, Closes #4589.
/cc @ysbaddaden