This is an archive of the discontinued LLVM Phabricator instance.

Make Scalar::GetBytes and RegisterValue::GetBytes const
ClosedPublic

Authored by uweigand on Apr 11 2016, 11:37 AM.

Details

Summary

Scalar::GetBytes provides a non-const access to the underlying bytes
of the scalar value, supposedly allowing for modification of those
bytes. However, even with the current implementation, this is not
really possible. For floating-point scalars, the pointer returned
by GetBytes refers to a temporary copy; modifications to that copy
will be simply ignored. For integer scalars, the pointer refers
to internal memory of the APInt implementation, which isn't
supposed to be directly modifyable; GetBytes simply casts aways
the const-ness of the pointer ...

With my upcoming patch to fix Scalar::GetBytes for big-endian
systems, this problem is going to get worse, since there we need
temporary copies even for some integer scalars. Therefore, this
patch makes Scalar::GetBytes const, fixing all those problems.

As a follow-on change, RegisterValues::GetBytes must be made const
as well. This in turn means that the way of initializing a
RegisterValue by doing a SetType followed by writing to GetBytes
no longer works. Instead, I've changed SetValueFromData to do
the equivalent of SetType itself, and then re-implemented
SetFromMemoryData to work on top of SetValueFromData.

There is still a need for RegisterValue::SetType, since some
platform-specific code uses it to reinterpret the contents of
an already filled RegisterValue. To make this usage work in
all cases (even changing from a type implemented via Scalar
to a type implemented as a byte buffer), SetType now simply
copies the old contents out, and then reloads the RegisterValue
from this data using the new type via SetValueFromData.

This in turn means that there is no remaining caller of
Scalar::SetType, so it can be removed.

The only other follow-on change was in MIPS EmulateInstruction
code, where some uses of RegisterValue::GetBytes could be made
const trivially.

Diff Detail

Repository
rL LLVM

Event Timeline

uweigand updated this revision to Diff 53295.Apr 11 2016, 11:37 AM
uweigand retitled this revision from to Make Scalar::GetBytes and RegisterValue::GetBytes const.
uweigand updated this object.
uweigand added a subscriber: lldb-commits.
clayborg accepted this revision.Apr 11 2016, 1:54 PM
clayborg edited edge metadata.

Looks good.

This revision is now accepted and ready to land.Apr 11 2016, 1:54 PM
tberghammer accepted this revision.Apr 12 2016, 9:02 AM
tberghammer edited edge metadata.

Looks good

This revision was automatically updated to reflect the committed changes.