djrieger / mjplusplus

A compiler for the MiniJava language
http://djrieger.github.io/mjplusplus/doc/doxygen/html/
6 stars 1 forks source link

Implement short circuiting in If conditions #69

Closed djrieger closed 9 years ago

djrieger commented 9 years ago

Currently segfaults on If conditions such as 7 == 4 || 1 < 1.

maxvogel commented 9 years ago

I'm not sure whether short circuiting is currently possible with our design or not. However, I gave this some thought, what has to happen in the different binary operations:

ast::ue::Not Fairly obvious, just switch trueTarget and falseTarget.

ast::be::OrOr When the ExpressionVisitor visits the OrOr-node, a new jumptarget/block has to be created. Also, it is not possible to recycle the current ExpressionVisitor because of this. The trueTarget for the left child is the currentTrue target. The falseTarget for the left child is the newly created Block in which the right child is to placed/constructed. The trueTarget for the right child is the original trueTarget and the same applies for the falseTarget.

ast::be::AndAnd Again a new block becomes necessary. However: leftChild.trueTarget = BlockForRightChild, leftChild.falseTarget = current.falseTarget; rightChild.trueTarget = current.trueTarget, rightChild.falseTarget = current.falseTarget

Thus, it doesn't seem to be possible to use the generic visitBinaryExpression(...) for AndAnd and OrOr. If this is implemented and works, the StatementVisitor for IfStatement has to be adapted as then, the (empty) blocks from the appropriate blocks of the jumpTargets have to be used instead of creating new blocks in visitThenOrElse(...). It might be possible to ditch the jumpTarget stuff and just use blocks. If this works, it will probably also work for while statements.

djrieger commented 9 years ago

Completely got rid of all uses of JumpTarget. Short circuiting seems to work correctly for all combinations of Not, AndAnd and OrOr as of e332754861be4a7305afcce3b2f10e8243632a74