-
Notifications
You must be signed in to change notification settings - Fork 1
/
arithmetic_operator_int.go
51 lines (45 loc) · 1.89 KB
/
arithmetic_operator_int.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
44
45
46
47
48
49
50
51
/*---------------------------------------------------------------------------------------------
* 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 (
"fmt"
"log"
"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
"github.com/llir/llvm/ir/value"
decorated "github.com/swamp/compiler/src/decorated/expression"
)
func generateArithmeticInt(operator *decorated.ArithmeticOperator, genContext *generateContext) (value.Value, error) {
irValueLeft, leftErr := generateExpression(operator.Left(), false, genContext)
if leftErr != nil {
return nil, leftErr
}
irValueRight, rightErr := generateExpression(operator.Left(), false, genContext)
if rightErr != nil {
return nil, rightErr
}
log.Printf("left is %T and right is %T", irValueLeft, irValueRight)
switch operator.OperatorType() {
case decorated.ArithmeticPlus:
return ir.NewAdd(irValueLeft, irValueRight), nil
case decorated.ArithmeticMinus:
return ir.NewSub(irValueLeft, irValueRight), nil
case decorated.ArithmeticMultiply:
return ir.NewMul(irValueLeft, irValueRight), nil
case decorated.ArithmeticDivide:
return ir.NewSDiv(irValueLeft, irValueRight), nil
case decorated.ArithmeticRemainder:
return ir.NewSRem(irValueLeft, irValueRight), nil
// case decorated.ArithmeticAppend: // n/a for ints
// case decorated.ArithmeticCons: // n/a for ints
case decorated.ArithmeticFixedMultiply:
return ir.NewSDiv(ir.NewMul(irValueLeft, irValueRight), constant.NewInt(types.I32, 100)), nil
case decorated.ArithmeticFixedDivide:
return ir.NewSDiv(ir.NewSRem(irValueLeft, irValueRight), constant.NewInt(types.I32, 100)), nil
default:
return nil, fmt.Errorf("unknown int operator")
}
}