| 528 | } |
| 529 | |
| 530 | LogicExp* MakeBoolean(GluaDecompileContextP ctx, Function* F, int* thenaddr, int* endif) { |
| 531 | int i; |
| 532 | int firstaddr, elseaddr; |
| 533 | BoolOp *first, *realLast, *last, *tmpLast, *curr; |
| 534 | int lastCount; |
| 535 | LogicExp *currExp = nullptr, *firstExp = nullptr; |
| 536 | int dest; |
| 537 | |
| 538 | if (endif) { |
| 539 | *endif = 0; |
| 540 | } |
| 541 | |
| 542 | if (F->bools.size == 0) { |
| 543 | SET_ERROR(ctx, F, "Attempted to build a boolean expression without a pending context"); |
| 544 | return nullptr; |
| 545 | } |
| 546 | |
| 547 | first = (BoolOp*)FirstItem(&(F->bools)); |
| 548 | realLast = (BoolOp*)LastItem(&(F->bools)); |
| 549 | last = realLast; |
| 550 | firstaddr = first->pc + 2; |
| 551 | *thenaddr = last->pc + 2; |
| 552 | elseaddr = last->dest; |
| 553 | |
| 554 | for (curr = realLast; curr; curr = lua_cast(BoolOp*, curr->super.prev)) { |
| 555 | int dest = curr->dest; |
| 556 | if ((elseaddr > *thenaddr) && |
| 557 | (((curr->op == OP_TEST) || (curr->op == OP_TESTSET)) ? (dest > elseaddr + 1) : |
| 558 | (dest > elseaddr))) { |
| 559 | last = curr; |
| 560 | *thenaddr = curr->pc + 2; |
| 561 | elseaddr = dest; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | tmpLast = last; |
| 566 | for (curr = first; curr && curr != tmpLast; curr = lua_cast(BoolOp*, curr->super.next)) { |
| 567 | int dest = curr->dest; |
| 568 | if (elseaddr > firstaddr) { |
| 569 | if (dest < firstaddr) { |
| 570 | last = curr; |
| 571 | *thenaddr = curr->pc + 2; |
| 572 | elseaddr = dest; |
| 573 | } |
| 574 | } |
| 575 | else { |
| 576 | if (dest == firstaddr) { |
| 577 | last = curr; |
| 578 | *thenaddr = curr->pc + 2; |
| 579 | elseaddr = dest; |
| 580 | } |
| 581 | else { |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | dest = first->dest; |
no test coverage detected