Repository navigation
Conversation
Unsigned 8- and 16-bit return values use an int layout, but their conversions did not take an int: the unsigned 16-bit ones take a char, so a method returning char or @as(u16) failed to link with "Return type mismatch", and the unsigned 8-bit ones went through the unsigned 32-bit ones, which take a long on 64-bit platforms, so a method returning @as(u8) failed with "target and filter types do not match". The int return value is now narrowed to char, or masked to its low 8 bits, before the existing conversion, as unsigned arguments are converted to int after it. Test unsigned 8- and 16-bit returns of every integer Java type, and signed 8- and 16-bit ones for comparison.
Instead of narrowing the int return value with a second method handle before the existing conversion, outputConvOfU8 and outputConvOfU16 now take the int their layout gives and clear the upper bits in the conversion itself, with new helpers (ub2s, us2i, us2l, ...) where no existing one fits. A 16-bit wchar_t keeps its char layout and conversion. Test the unsigned 8- and 16-bit return values of the remaining Java types.
Contributor
Author
|
Following your comment on #86, I've changed this PR the same way: |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A method whose return value is an unsigned 16-bit type fails to link, whatever its Java type. For example:
fails at the first call with:
layoutOfgives unsigned 8- and 16-bit values anintlayout, for return values as well as parameters, butoutputConvOfU16's conversions take achar. Forcharthe conversion isnull, so theintreturned by the downcall handle is left as it is; for the other types (c2s,c2i,c2l, ...)filterReturnValuerejects the mismatched types.Unsigned 8-bit return values have the same problem in another form:
outputConvOfU8uses the unsigned 32-bit conversions, which on 64-bit platforms take along, so@As(u8) int(or any other@As(u8)return) fails withtarget and filter types do not match: (MemorySegment,int)int, (long)byte. Signed 8- and 16-bit return values usebyteandshortlayouts and are not affected.I found this while answering the review on #86, which fixes the same mismatch for unsigned 16-bit parameters.
The fix
outputConvOfU8andoutputConvOfU16now take theintfrom the layout and clear the upper bits inside a single conversion (new helpersub2z,ub2s,ub2c,ub2l,ub2f,ub2d,us2z,us2i,us2l,us2f,us2d; the existingi2b,i2s,i2candi2ubwhere they already do the right thing), so each return value gets at most one conversion handle. The only two-step case isNativeIntValued, which already chains toofNativeValueonmain.Masking the value also means that the result does not depend on the upper bits of the return register, which the C ABIs do not require the callee to clear for narrow return types.
wchar_treturn values, which use the canonicalwchar_tlayout (acharon Windows), keep their conversion (outputConvOfWchar16). This PR does not depend on #86 and merges cleanly with it.Tests
ReturnTypesTestgains tests for unsigned 16-bit returns (char,@As(u16) short,@As(stdc_unsigned_short) int,@As(u16) long), unsigned 8-bit returns (@As(u8) byte,@As(stdc_unsigned_char) short,@As(u8) int,@As(u8) long), signed 8- and 16-bit returns for comparison, andboolean,char,float,doubleandErrnoreturns with values that need the upper bits cleared. Since there is no function returningunsigned shortin the default lookup on all three CI platforms (htonsis not in it on Windows), they linkabs, which returns its argument unchanged for these values; theintlayout makes that call ABI-compatible, and values above the narrow type's range (such as0x1fedc) check the narrowing.On
mainthe unsigned tests fail with the errors above; with the fix,mvn verifypasses: 56 tests (3 skipped, as onmain: platform- or library-specific tests).