Skip to content

Commit ed017c1

Browse files
committed
Fix some problems with pointer dereferences inside rangerefs
1 parent bb9177b commit ed017c1

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

frontends/hltransform.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Spin to C/C++ converter
3-
* Copyright 2011-2025 Total Spectrum Software Inc.
3+
* Copyright 2011-2025 Total Spectrum Software Inc. and contributors
44
* See the file COPYING for terms of use
55
*
66
* various high level transformations that should take
@@ -356,6 +356,24 @@ static AST *ExpandLhsSingle(AST *item) {
356356
}
357357
}
358358

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+
359377
void
360378
doSimplifyAssignments(AST **astptr, int insertCasts, int atTopLevel)
361379
{
@@ -620,30 +638,33 @@ doSimplifyAssignments(AST **astptr, int insertCasts, int atTopLevel)
620638
++i -> (i = i+1, i) */
621639
if (ast->left) {
622640
/* i++ case */
641+
/* for anything complicated (not just i++), do
642+
the transform, for safety
643+
*/
623644
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)));
630651

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);
636656
}
637-
} else {
657+
} else if (ast->right) {
638658
AST *ident = ast->right;
639659
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);
647668
}
648669
}
649670
}

frontends/spin/spinlang.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,8 @@ doSpinTransform(AST **astptr, int level, AST *parent)
856856
} break;
857857
case AST_RANGEREF:
858858
*astptr = ast = TransformRangeUse(ast);
859+
doSpinTransform(&ast->left, 0, ast);
860+
doSpinTransform(&ast->right, 0, ast);
859861
break;
860862
case AST_ALLOCA:
861863
doSpinTransform(&ast->right, 0, ast);

0 commit comments

Comments
 (0)