-
Notifications
You must be signed in to change notification settings - Fork 1
/
if.go
43 lines (33 loc) · 1.5 KB
/
if.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*---------------------------------------------------------------------------------------------
* Copyright (c) Peter Bjorklund. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package generate_ir
import (
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/value"
decorated "github.com/swamp/compiler/src/decorated/expression"
"log"
)
func generateIf(ifExpr *decorated.If, isLeafNode bool, genContext *generateContext) (value.Value, error) {
conditionVar, testErr := generateExpression(ifExpr.Condition(), false, genContext)
if testErr != nil {
return nil, testErr
}
consequenceContext := genContext.NewBlock("consequence")
consequenceValue, consErr := generateExpression(ifExpr.Consequence(), isLeafNode, consequenceContext)
if consErr != nil {
return nil, consErr
}
consequenceIncoming := ir.NewIncoming(consequenceValue, consequenceContext.block)
alternativeContext := genContext.NewBlock("alternative")
alternativeValue, altErr := generateExpression(ifExpr.Alternative(), isLeafNode, alternativeContext)
if altErr != nil {
return nil, altErr
}
alternativeIncoming := ir.NewIncoming(alternativeValue, alternativeContext.block)
genContext.block.NewCondBr(conditionVar, consequenceContext.block, alternativeContext.block)
phi := ir.NewPhi(consequenceIncoming, alternativeIncoming)
log.Printf("\nif:%v\n", phi)
return phi, nil
}