Skip to content

Support for raw pointers #532

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

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

Support for raw pointers #532

wants to merge 7 commits into from

Conversation

dkcumming
Copy link
Collaborator

Raw pointers through AddressOf and AggregateKind::RawPtr are required. Supporting pointers means we are going to have to support or stub Alignment and Layout in some manner to handle those MIR rvalues.

@dkcumming dkcumming self-assigned this Apr 23, 2025
// | Ptr( Address, MaybeValue ) // FIXME why maybe? why value?
// address, metadata for ref/ptr
| Ptr( Int, Place, Mutability, Int, Int)
// stack depth (initially zero), place, mutablility, address, offset NOTE: First 3 fields are the same as Reference, last are needed for low level instructions
Copy link
Member

Choose a reason for hiding this comment

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

Nit-pick: long line

Suggested change
// stack depth (initially zero), place, mutablility, address, offset NOTE: First 3 fields are the same as Reference, last are needed for low level instructions
// stack depth (initially zero), place, mutablility, address, offset
// NOTE: First 3 fields are the same as Reference, last are needed for low level instructions

Comment on lines +1422 to +1432
rule #compute(
BOP,
typedValue(Integer(ARG1, WIDTH, _), _, _),
typedValue(Integer(ARG2, WIDTH, _), _, _),
false) // unchecked
=>
typedValue(
Integer(onInt(BOP, ARG1, ARG2), WIDTH, false),
TyUnknown,
mutabilityNot
)
Copy link
Member

Choose a reason for hiding this comment

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

Two things to correct:

  • Your result is always unsigned but we have to keep the signedness flag from the arguments.
Suggested change
rule #compute(
BOP,
typedValue(Integer(ARG1, WIDTH, _), _, _),
typedValue(Integer(ARG2, WIDTH, _), _, _),
false) // unchecked
=>
typedValue(
Integer(onInt(BOP, ARG1, ARG2), WIDTH, false),
TyUnknown,
mutabilityNot
)
rule #compute(
BOP,
typedValue(Integer(ARG1, WIDTH, SIGNED), TY, _),
typedValue(Integer(ARG2, WIDTH, SIGNED), TY, _),
false) // unchecked
=>
typedValue(
Integer(onInt(BOP, ARG1, ARG2), WIDTH, SIGNED),
TY,
mutabilityNot
)
  • We have to ensure the Ty are the same for both arguments (I added the match on TY above). There are rules that checks this, and the suitability of the operands, for arithmetic operations. Maybe we should add similar rules for bit-oriented ones, to signal TypeMismatch or OperandMismatch errors

Comment on lines +1438 to +1439
typedValue(Ptr(_, _, _, ADDRESS, _), _, _),
typedValue(Integer(VAL, WIDTH, _), _, _),
Copy link
Member

Choose a reason for hiding this comment

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

??? Do we really need this rule? I thought the Ptr value gets cast to usize before that, or not?

EDIT after reading the related cast rule: Maybe the cast should just be different and we delete this rule.

// requires TY_TO #in TYPEMAP
// requires #is_valid_cast(TY_FROM.layout(), TY_TO.layout())

rule <k> #cast( typedValue(VALUE, _TY_FROM, LOCAL_MUT), castKindTransmute, TY_TO) => typedValue(VALUE, TY_TO, LOCAL_MUT) ... </k>
Copy link
Member

Choose a reason for hiding this comment

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

Well... this rule is certainly too generic.
Right now we only want to cast raw pointers and return their "Address" (IIUC, or something that is derived from it?), as a usize value that will suit the example program...

Comment on lines +584 to +587
rule <k> #projectedUpdate(
_DEST,
typedValue(Ptr(OFFSET, place(LOCAL, PLACEPROJ), MUT, _ADDRESS, _OFFSET2), _, _),
projectionElemDeref PROJS,
Copy link
Member

Choose a reason for hiding this comment

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

There is also #readProjection earlier in this module, which will require the same kind of extension to Deref a pointer.

rule <k> #readProjection(
typedValue(Reference(FRAME, place(LOCAL:Local, PLACEPROJS), _), _, _),
projectionElemDeref PROJS
)
=>
#readProjection(
{#localFromFrame({STACK[FRAME -Int 1]}:>StackFrame, LOCAL, FRAME)}:>TypedValue,
appendP(PLACEPROJS, PROJS)
)

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.

3 participants