Closed marcusnaslund closed 8 years ago
Will take a look at it today.
Sounds weird, probably some generics bug (what I'm thinking is happening is that the whole function becomes a single return statement and isn't executed when you don't store the result).
@shamanas I ran into something similar a few weeks ago, and I found that the generated C-code is checking if the receiver is a null pointer, and if it is, nothing is done. That is, instead of a pointer to an lvalue, Literal NULL
is passed to the generated C-function.
void stack_pop(stack_t* stack, int* returnValue) {
if (returnValue != NULL) {
// do stuff
}
}
and the generated call without an lvalue would be: stack_pop(theStack, NULL)
Yes, generic return is implementing by taking a generic pointer for output, so if we discard the return value we pass NULL, than NULL checks are done around every return statement, so it's exactly like I suspected.
Now, the solution is obvious:
Instead of just generating an if statement, also generate an else branch that just executes the expression.
Actually, it looks like rock already tries to take this into account but hasSideEffects doesn't detect the side effects here :P
@marcusnaslund By the way, there is no decrement or increment operators in ooc (postfix or prefix), prefix operators just incidentally work because we have prefix +
and -
and it is apparently legal to have more than one.
Should probably fix that too.
There's no side effects in pop_broken
- if you don't store the result, it's a no-op (it isn't currently due to OOC emitting "--" in C, but semantically should be)
Yeah, I agree, I am talking about the exact same code replaced with the correct way to do things this count -= 1
.
hasSideEffects always returned false for array accesses.
Also, BinaryOp returned the opposite of what it should (it returned !isAssign()
, while it should return isAssign()
)
Ah, makes sense
Consider a very simple stack:
Popping the stack using
temp := myStack pop_broken()
works, but if the line is simplymyStack pop_broken()
, thenhis _data[--this _count]
isn't executed at all (and thusthis _count
is never decreased). Thepop_works
method always works becausethis _count
is decreased, and then the last (returning) line is not executed, but that's fine, since we don't care about it.A simple example demonstrating what works and what does not: