Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix identity comparison for boxed values
Previously, the identity comparison for boxed values worked by comparing the addresses of the boxes regardless of their contents. This was leading to confusing behaviour, for example let a: U32 = 0 let b: U32 = 0 let boxa: Any = a let boxb: Any = b a is b // True. boxa is boxb // False. Now, the identity of a boxed value (either a numeric primitive or a tuple) depends on its content rather than its address. This affects the `is`, `isnt` and `digestof` operators. Several general changes were made to the code generator in order to make the identity comparison of a boxed value as efficient as possible. When comparing a possibly boxed value to an unboxed value, the type of the box is checked first by looking at its type descriptor. If the types of the values are the same, the boxed value is unboxed and the comparison is done on the raw objects. When comparing two possibly boxed values, the check depends on the underlying types. - First, the addresses are compared. If they are the same, the objects have the same identity regardless of whether they are boxed values or not - If the addresses are different, we check whether the objects are two boxed values of the same type. This is done by checking the type IDs with a method described afterwards - If the values are two boxed numbers, the identity check is done with `memcmp` without unboxing - If the values are two boxed tuples, the identity check is done by calling a type-specific `__is` function through the vtable. This function unboxes the tuples and does the identity check - If the values aren't boxed or if they don't have the same type, they don't have the same identity. When computing the digest of a possibly boxed value, we first check whether the value is boxed by checking the type ID with the same method as for the identity comparison. If the value is boxed, a type-specific `__digestof` function is called through the vtable. This function unboxes the value and computes the digest. For both identity comparison and digest computing, the compiler generates the checks depending on the possible subtypes of the base types that it is looking at. This means that checking for the identity of an object that cannot be boxed (because the interface type has no numeric primitive/tuple as subtypes) doesn't involve checking whether it is boxed. This commit changes how type IDs are assigned. Previously, they were assigned incrementally as types were reached without considering boxed types. Now, type IDs are assigned as - Object type IDs: 1, 3, 5, 7, 9, ... - Numeric type IDs: 0, 4, 8, 12, 16, ... - Tuple type IDs: 2, 6, 10, 14, 18, ... There are two reasons for this - Speed. Checking whether a value is boxed or not consists in checking if its type ID is a multiple of 2, and further discriminating between a boxed number and a boxed tuple consists in checking if it is a multiple of 4. In machine code, this boils down to checking whether the first or second bit of the type ID are set, respectively - Forward compatibility. We won't have to change this method when dynamic code loading is added to Pony The `__is` and `__digestof` functions introduce a change to the reachability algorithm. These functions are automatically added to numeric primitives and tuples, are generated by the compiler and are marked as "internal". An internal method isn't added to subtypes and since it doesn't exist before reachability analysis, it is added to supertypes in order to be visible when generating identity checks. Closes #1567.
- Loading branch information