Generator generates constants

This commit is contained in:
Sasha Koshka 2024-04-21 00:19:33 -04:00
parent 8248135255
commit f05efb749d
3 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,93 @@
package generator
import "testing"
func TestConstantValueSpecified (test *testing.T) {
testString (test,
`%"0zNZN147MN2wzMAQ6NS2dQ==::E" = type i64
define void @"0zNZN147MN2wzMAQ6NS2dQ==::f"() {
0:
%1 = call i64 @"0zNZN147MN2wzMAQ6NS2dQ==::plus4"(i64 2)
ret void
}
define i64 @"0zNZN147MN2wzMAQ6NS2dQ==::plus4"(%"0zNZN147MN2wzMAQ6NS2dQ==::E" %e) {
0:
%1 = alloca %"0zNZN147MN2wzMAQ6NS2dQ==::E"
store %"0zNZN147MN2wzMAQ6NS2dQ==::E" %e, ptr %1
%2 = load %"0zNZN147MN2wzMAQ6NS2dQ==::E", ptr %1
switch %"0zNZN147MN2wzMAQ6NS2dQ==::E" %2, label %8 [
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 1, label %5
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 2, label %6
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 3, label %7
]
3:
%4 = phi i64 [ 5, %5 ], [ 6, %6 ], [ 7, %7 ], [ -1, %8 ]
ret i64 %4
5:
br label %3
6:
br label %3
7:
br label %3
8:
br label %3
}
`,
`
E: Int
| one = 1
| two = 2
| three = 3
[plus4 e:E]:Int = switch e
| E.one 5
| E.two 6
| E.three 7
* -1
[f] = [plus4 E.two]
`)}
func TestConstantValueUnspecified (test *testing.T) {
testString (test,
`%"0zNZN147MN2wzMAQ6NS2dQ==::E" = type i64
define void @"0zNZN147MN2wzMAQ6NS2dQ==::f"() {
0:
%1 = call i64 @"0zNZN147MN2wzMAQ6NS2dQ==::plus4"(i64 1)
ret void
}
define i64 @"0zNZN147MN2wzMAQ6NS2dQ==::plus4"(%"0zNZN147MN2wzMAQ6NS2dQ==::E" %e) {
0:
%1 = alloca %"0zNZN147MN2wzMAQ6NS2dQ==::E"
store %"0zNZN147MN2wzMAQ6NS2dQ==::E" %e, ptr %1
%2 = load %"0zNZN147MN2wzMAQ6NS2dQ==::E", ptr %1
switch %"0zNZN147MN2wzMAQ6NS2dQ==::E" %2, label %8 [
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 0, label %5
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 1, label %6
%"0zNZN147MN2wzMAQ6NS2dQ==::E" 2, label %7
]
3:
%4 = phi i64 [ 4, %5 ], [ 5, %6 ], [ 6, %7 ], [ -1, %8 ]
ret i64 %4
5:
br label %3
6:
br label %3
7:
br label %3
8:
br label %3
}
`,
`
E: Int
| one
| two
| three
[plus4 e:E]:Int = switch e
| E.one 4
| E.two 5
| E.three 6
* -1
[f] = [plus4 E.two]
`)}

View File

@ -101,6 +101,8 @@ func (this *generator) generateExpressionAny (expression entity.Expression) (reg
return this.generateLoop(expression, resultModeAny)
case *entity.For:
return this.generateFor(expression, resultModeAny)
case *entity.Constant:
return this.generateConstant(expression, resultModeAny)
// we get nothing from these
case *entity.Assignment:
@ -171,6 +173,9 @@ func (this *generator) generateExpressionVal (expression entity.Expression) (llv
case *entity.For:
val, _, err := this.generateFor(expression, resultModeVal)
return val, err
case *entity.Constant:
val, _, err := this.generateConstant(expression, resultModeVal)
return val, err
case *entity.LiteralInt:
return this.generateLiteralInt(expression)
case *entity.LiteralFloat:
@ -249,6 +254,9 @@ func (this *generator) generateExpressionLoc (expression entity.Expression) (llv
case *entity.For:
loc, _, err := this.generateFor(expression, resultModeLoc)
return loc, err
case *entity.Constant:
loc, _, err := this.generateConstant(expression, resultModeLoc)
return loc, err
case *entity.LiteralArray:
return this.generateLiteralArrayLoc(expression, nil)
case *entity.LiteralString:

View File

@ -429,3 +429,10 @@ func (this *generator) generateFor (loop *entity.For, mode resultMode) (llvm.Val
}
return irValue, loopEntry.loc, nil
}
func (this *generator) generateConstant (constant *entity.Constant, mode resultMode) (llvm.Value, bool, error) {
// TODO for strings arrays and structs, make constant data in the
// module.
value := constant.Declaration.Value
return this.generateExpression(value, mode)
}