Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Cannot perform pointer arithmetic in complex assignment #121

Closed
jyn514 opened this issue Nov 28, 2019 · 9 comments
Closed

Cannot perform pointer arithmetic in complex assignment #121

jyn514 opened this issue Nov 28, 2019 · 9 comments
Labels
blocked Waiting for another project to implement a feature or fix a bug parser Issue to do with parsing the abstract syntax tree
Milestone

Comments

@jyn514
Copy link
Owner

jyn514 commented Nov 28, 2019

int main() {
        int i, *p;
        p += i;
}
<stdin>:3:8: error: invalid program: cannot implicitly convert 'int' to 'int *'. help: use an explicit cast: (int *)
@jyn514 jyn514 added this to the MVP milestone Nov 28, 2019
@jyn514 jyn514 pinned this issue Dec 2, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Dec 5, 2019

This is actually because of the +=, we currently don't handle binary assignment very well (see #25). I'm considering removing this from the AST altogether and just adding a is_assign boolean to operations that could be an assignment.

@jyn514 jyn514 changed the title Cannot perform pointer arithmetic Cannot perform pointer arithmetic in complex assignment Dec 5, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Dec 5, 2019

We can't desugar this to p = p + i because p could be the result of an arbitrary expression and the standard requires that we only evaluate it once:

int *a[10];
int main() {    
        int i;
        *a = &i;
        *a += 1;     
}

@jyn514
Copy link
Owner Author

jyn514 commented Dec 5, 2019

I mean ... we could desugar it and just violate the standard ...

@jyn514 jyn514 added the hard Extra attention is needed label Dec 6, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Dec 6, 2019

That doesn't work for function calls though:

int x;
int *f() {
    // mutates global state!
    x += 5;
    return &x;
}
int main() {
    *f() += 5;
}

@jyn514
Copy link
Owner Author

jyn514 commented Dec 10, 2019

Oh! I can just desugar to tmp = f(x); *tmp = *tmp + 5;

@jyn514
Copy link
Owner Author

jyn514 commented Dec 10, 2019

That involves a needless load and store though ... maybe I should finally implement the register keyword in the backend?

@jyn514
Copy link
Owner Author

jyn514 commented Dec 10, 2019

Even simpler: emit a declaration of the variable that gets sent to the backend, then register is just an optimization instead of a requirement

jyn514 added a commit that referenced this issue Dec 10, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Dec 10, 2019

I have a start on this in the compound-assignment branch, but it requires separating out parsing from semantic analysis. I've always meant to do this, it's just a little earlier than I planned. I think I'll make separate analysis a different branch and merge that before I merge compound assignment.

@jyn514 jyn514 added parser Issue to do with parsing the abstract syntax tree blocked Waiting for another project to implement a feature or fix a bug and removed hard Extra attention is needed labels Dec 10, 2019
@jyn514
Copy link
Owner Author

jyn514 commented Dec 10, 2019

Waiting on #151

@jyn514 jyn514 closed this as completed May 1, 2020
@jyn514 jyn514 unpinned this issue May 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked Waiting for another project to implement a feature or fix a bug parser Issue to do with parsing the abstract syntax tree
Projects
None yet
Development

No branches or pull requests

1 participant