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

[WIP] Intrinsics now platform dependent. LibC::PtrdiffT type introduced. #4624

Closed
wants to merge 3 commits into from

Conversation

akzhan
Copy link
Contributor

@akzhan akzhan commented Jun 26, 2017

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

…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.
@akzhan akzhan changed the title Intrinsics now platform dependent. PtrdiffT type introduced. Intrinsics now platform dependent. LibC::PtrdiffT type introduced. Jun 26, 2017
@akzhan
Copy link
Contributor Author

akzhan commented Jun 26, 2017

Got failure on 32bit build. Will check.

Copy link
Contributor

@ysbaddaden ysbaddaden left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. size_t is a length type (as in allocation size), you meant intptr_t (pointer size);
  2. you must hack the compiler for this to compile on 32bit platforms (Pointer.new(UInt64) is a Crystal intrinsic).

Copy link
Contributor Author

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)
Copy link
Contributor

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)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the explanation.

@akzhan akzhan changed the title Intrinsics now platform dependent. LibC::PtrdiffT type introduced. [WIP] Intrinsics now platform dependent. LibC::PtrdiffT type introduced. Jun 26, 2017
@akzhan
Copy link
Contributor Author

akzhan commented Sep 17, 2017

I should close this because another approach has been landed - #4960.

@akzhan akzhan closed this Sep 17, 2017
@akzhan akzhan deleted the ptrdiff_t branch September 17, 2017 14:59
@akzhan akzhan restored the ptrdiff_t branch September 17, 2017 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants