|
1 | 1 | /* |
2 | 2 | * Spin to C/C++ converter |
3 | | - * Copyright 2011-2025 Total Spectrum Software Inc. |
| 3 | + * Copyright 2011-2025 Total Spectrum Software Inc. and contributors |
4 | 4 | * See the file COPYING for terms of use |
5 | 5 | * |
6 | 6 | * various high level transformations that should take |
@@ -356,6 +356,24 @@ static AST *ExpandLhsSingle(AST *item) { |
356 | 356 | } |
357 | 357 | } |
358 | 358 |
|
| 359 | +static bool |
| 360 | +NeedIncDecTransform(AST *expr, AST *typ) |
| 361 | +{ |
| 362 | + if (typ) { |
| 363 | + if (IsFloatType(typ) || IsInt64Type(typ) || IsBoolType(typ)) |
| 364 | + return true; |
| 365 | + } |
| 366 | + |
| 367 | + /* keep inc/dec for traditional output, or at least be more |
| 368 | + conservative about transforms |
| 369 | + */ |
| 370 | + if (TraditionalBytecodeOutput()) |
| 371 | + return false; |
| 372 | + if (IsIdentifier(expr) || expr->kind == AST_HWREG) |
| 373 | + return false; |
| 374 | + return true; |
| 375 | +} |
| 376 | + |
359 | 377 | void |
360 | 378 | doSimplifyAssignments(AST **astptr, int insertCasts, int atTopLevel) |
361 | 379 | { |
@@ -620,30 +638,33 @@ doSimplifyAssignments(AST **astptr, int insertCasts, int atTopLevel) |
620 | 638 | ++i -> (i = i+1, i) */ |
621 | 639 | if (ast->left) { |
622 | 640 | /* i++ case */ |
| 641 | + /* for anything complicated (not just i++), do |
| 642 | + the transform, for safety |
| 643 | + */ |
623 | 644 | AST *typ = ExprType(ast->left); |
624 | | - if (typ) { |
625 | | - if (IsFloatType(typ) || IsInt64Type(typ) || IsBoolType(typ)) { |
626 | | - AstReportAs(ast, &saveinfo); |
627 | | - AST *temp = AstTempLocalVariable("_temp_", typ); |
628 | | - AST *save = AstAssign(temp, ast->left); |
629 | | - AST *update = AstAssign(ast->left, AstOperator(newop, ast->left, AstInteger(1))); |
| 645 | + bool needTransform = NeedIncDecTransform(ast->left, typ); |
| 646 | + if (needTransform) { |
| 647 | + AstReportAs(ast, &saveinfo); |
| 648 | + AST *temp = AstTempLocalVariable("_temp_", typ); |
| 649 | + AST *save = AstAssign(temp, ast->left); |
| 650 | + AST *update = AstAssign(ast->left, AstOperator(newop, ast->left, AstInteger(1))); |
630 | 651 |
|
631 | | - ast = *astptr = NewAST(AST_SEQUENCE, |
632 | | - NewAST(AST_SEQUENCE, save, update), |
633 | | - temp); |
634 | | - AstReportDone(&saveinfo); |
635 | | - } |
| 652 | + ast = *astptr = NewAST(AST_SEQUENCE, |
| 653 | + NewAST(AST_SEQUENCE, save, update), |
| 654 | + temp); |
| 655 | + AstReportDone(&saveinfo); |
636 | 656 | } |
637 | | - } else { |
| 657 | + } else if (ast->right) { |
638 | 658 | AST *ident = ast->right; |
639 | 659 | AST *typ = ExprType(ident); |
640 | | - if (typ) { |
641 | | - if (IsFloatType(typ) || IsInt64Type(typ) || IsBoolType(typ)) { |
642 | | - ast->kind = AST_ASSIGN; |
643 | | - ast->d.ival = K_ASSIGN; |
644 | | - ast->left = ident; |
645 | | - ast->right = AstOperator(newop, ident, AstInteger(1)); |
646 | | - } |
| 660 | + bool needTransform = NeedIncDecTransform(ast->right, typ); |
| 661 | + if (needTransform) { |
| 662 | + AstReportAs(ast, &saveinfo); |
| 663 | + ast->kind = AST_ASSIGN; |
| 664 | + ast->d.ival = K_ASSIGN; |
| 665 | + ast->left = ident; |
| 666 | + ast->right = AstOperator(newop, ident, AstInteger(1)); |
| 667 | + AstReportDone(&saveinfo); |
647 | 668 | } |
648 | 669 | } |
649 | 670 | } |
|
0 commit comments