From 67a9855319bffcfd6326aec5d81b544ff9aa7816 Mon Sep 17 00:00:00 2001 From: CharlesKWON Date: Sat, 18 Apr 2026 08:45:58 +0900 Subject: [PATCH] perf(gengo): fold DO WHILE .T. / .F. at compile time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DO WHILE .T. now emits a bare for-loop with no PushBool/PopLogical per iteration — saves a stack roundtrip on every trip through the idiomatic infinite-loop pattern (9 .prg files use it). DO WHILE .F. emits nothing. Loop exits still work via EXIT / RETURN. FiveSql2 43/43, Harbour compat 56/56. Co-Authored-By: Claude Opus 4.7 (1M context) --- compiler/gengo/gengo.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/gengo/gengo.go b/compiler/gengo/gengo.go index 121e42a..640b812 100644 --- a/compiler/gengo/gengo.go +++ b/compiler/gengo/gengo.go @@ -1391,6 +1391,11 @@ emit: } func (g *Generator) emitDoWhile(s *ast.DoWhileStmt, locals localMap) { + // DO WHILE .F. — body is unreachable; emit nothing. + if v, ok := boolLiteralValue(s.Cond); ok && !v { + return + } + // Detect RDD commands in body for WA hoisting hasRDD := hasRDDCommands(s.Body) safeToHoist := hasRDD && !hasWorkareaChange(s.Body) @@ -1405,8 +1410,12 @@ func (g *Generator) emitDoWhile(s *ast.DoWhileStmt, locals localMap) { g.writeln("for {") g.indent++ - g.emitExpr(s.Cond) - g.writeln("if !t.PopLogical() { break }") + // DO WHILE .T. — the idiomatic infinite loop. Skip the per-iteration + // PushBool/PopLogical; exit only through EXIT / LOOP / RETURN. + if v, ok := boolLiteralValue(s.Cond); !ok || !v { + g.emitExpr(s.Cond) + g.writeln("if !t.PopLogical() { break }") + } for _, stmt := range s.Body { g.emitStmt(stmt, locals) }