Cesium icon indicating copy to clipboard operation
Cesium copied to clipboard

`BinaryOperatorExpression`'s try to handle pointer arithmetic is problematic

Open ForNeVeR opened this issue 1 year ago • 0 comments

In BinaryOperatorExpression::LowerPointerArithmetics, we are trying to essentially multiply the pointer operands by pointer size. Note that the expressions still keep the same pointer type.

This means that:

  1. Initial arithmetics that's getting into the BinaryOperatorExpression is essentially incorrect, or at least doesn't correspond to the internal model of how the calculation is represented.
  2. Lower().Lower() called twice on the same node would give incorrect results, since it'd lower the pointer arithmetics twice.

For example, let's consider that we are lowering the expression of (int*)0 + 4. In C's pointer arithmetic, this means that the result is 0 + 4 * sizeof(int*).

BinaryOperatorExpression::LowerPointerArithmetics essentially converts this to (int*)0 + sizeof(int*) * 4, which looks correct w.r.t. runtime code gen (.NET runtime doesn't do any pointer arithmetics under the cover), but doesn't change the types of the arguments, which is not how it should work.

Please see the code marked by number 516 in the compiler code, and fix that, either by introducing some new model, or removing this lowering somewhere else.

ForNeVeR avatar Jan 16 '24 23:01 ForNeVeR