Skip to content

Commit bd9564c

Browse files
committed
[Heavy] Fix internal define initialization
1 parent f742d2a commit bd9564c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

heavy/lib/OpGen.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ heavy::FuncOp OpGen::createFunction(SourceLocation Loc,
420420
mlir::Value OpGen::createLambda(Value Formals, Value Body,
421421
SourceLocation Loc,
422422
llvm::StringRef Name) {
423+
// Flush any internal definitions from containing body if any.
424+
if (IsLocalDefineAllowed)
425+
FinishLocalDefines();
426+
423427
// Ensure we are no longer top level.
424428
if (!TopLevelOp)
425429
InsertTopLevelCommandOp(Loc);
@@ -457,9 +461,9 @@ mlir::Value OpGen::createLambda(Value Formals, Value Body,
457461
// If Result is null then it already
458462
// has a terminator.
459463
if (mlir::Value Result = createBody(Loc, Body)) {
460-
if (IsLocalDefineAllowed) {
464+
// Flush the internal definitions if we have not done so.
465+
if (IsLocalDefineAllowed)
461466
FinishLocalDefines();
462-
}
463467
Result = LocalizeValue(Result);
464468
Builder.create<ContOp>(Result.getLoc(), Result);
465469
}
@@ -706,6 +710,9 @@ bool OpGen::FinishLocalDefines() {
706710
}
707711

708712
mlir::Value OpGen::createBody(SourceLocation Loc, Value Body) {
713+
// Flush any internal definitions before starting a new body.
714+
if (IsLocalDefineAllowed)
715+
FinishLocalDefines();
709716
IsLocalDefineAllowed = true;
710717
return createSequence(Loc, Body);
711718
}

heavy/test/Evaluate/if.scm

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: heavy-scheme %s | FileCheck %s
2+
(import (heavy base))
3+
4+
; CHECK: foo
5+
(write (if #t 'foo 'bar))
6+
(newline)
7+
8+
; CHECK: bar
9+
(write (if #f 'foo 'bar))
10+
(newline)
11+
12+
; CHECK-NEXT: foo
13+
(if #t
14+
(write 'foo)
15+
(write 'bar))
16+
(newline)
17+
18+
; CHECK-NEXT: bar
19+
(if #f
20+
(write 'foo)
21+
(write 'bar))
22+
(newline)
23+
24+
; CHECK-NEXT: foo
25+
(if (null? '())
26+
(write 'foo)
27+
(write 'bar))
28+
(newline)
29+
30+
; CHECK-NEXT: bar
31+
(if (null? 42)
32+
(write 'foo)
33+
(write 'bar))
34+
(newline)
35+
36+
(define fn
37+
((lambda()
38+
(define captured-foo 'foo)
39+
(define captured-bar 'bar)
40+
(lambda (x)
41+
(if (null? x)
42+
(write captured-foo)
43+
(write captured-bar))))))
44+
45+
; CHECK-NEXT: foo
46+
(fn '())
47+
(newline)
48+
49+
; CHECK-NEXT: bar
50+
(fn 42)
51+
(newline)

heavy/test/Evaluate/lambda.scm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
; RUN: heavy-scheme %s | FileCheck %s
22
(import (heavy base))
33

4+
; CHECK: foobar
5+
(define fn
6+
((lambda()
7+
(define captured-foo 'foo)
8+
(define captured-bar 'bar)
9+
(lambda ()
10+
(write captured-foo)
11+
(write captured-bar)))))
12+
(fn)(newline)
13+
414
(define global -1)
515

616
(set! global 1)

0 commit comments

Comments
 (0)