292 lines
7.5 KiB
Go
292 lines
7.5 KiB
Go
package generator
|
|
|
|
import "testing"
|
|
|
|
func TestLiteral (test *testing.T) {
|
|
testString (test,
|
|
`%Byte = type i8
|
|
%Rune = type i32
|
|
%Index = type i64
|
|
%String = type { ptr, %Index }
|
|
define void @main() {
|
|
0:
|
|
%1 = alloca i64
|
|
store i64 382, ptr %1
|
|
%2 = alloca i64
|
|
store i64 -4392, ptr %2
|
|
%3 = alloca %Byte
|
|
store i8 128, ptr %3
|
|
%4 = alloca i8
|
|
store i8 -127, ptr %4
|
|
%5 = alloca %Byte
|
|
store i8 104, ptr %5
|
|
%6 = load %Byte, ptr %5
|
|
%7 = alloca %Byte
|
|
store %Byte %6, ptr %7
|
|
%8 = alloca %Rune
|
|
store i32 48, ptr %8
|
|
%9 = load %Rune, ptr %8
|
|
%10 = alloca %Rune
|
|
store %Rune %9, ptr %10
|
|
%11 = alloca [5 x i8]
|
|
%12 = getelementptr [5 x i8], ptr %11, i32 0, i32 0
|
|
store i8 104, ptr %12
|
|
%13 = getelementptr [5 x i8], ptr %11, i32 0, i32 1
|
|
store i8 101, ptr %13
|
|
%14 = getelementptr [5 x i8], ptr %11, i32 0, i32 2
|
|
store i8 108, ptr %14
|
|
%15 = getelementptr [5 x i8], ptr %11, i32 0, i32 3
|
|
store i8 108, ptr %15
|
|
%16 = getelementptr [5 x i8], ptr %11, i32 0, i32 4
|
|
store i8 111, ptr %16
|
|
%17 = load [5 x %Byte], ptr %11
|
|
%18 = alloca [5 x %Byte]
|
|
store [5 x %Byte] %17, ptr %18
|
|
%19 = alloca [5 x i32]
|
|
%20 = getelementptr [5 x i32], ptr %19, i32 0, i32 0
|
|
store i32 104, ptr %20
|
|
%21 = getelementptr [5 x i32], ptr %19, i32 0, i32 1
|
|
store i32 101, ptr %21
|
|
%22 = getelementptr [5 x i32], ptr %19, i32 0, i32 2
|
|
store i32 108, ptr %22
|
|
%23 = getelementptr [5 x i32], ptr %19, i32 0, i32 3
|
|
store i32 108, ptr %23
|
|
%24 = getelementptr [5 x i32], ptr %19, i32 0, i32 4
|
|
store i32 111, ptr %24
|
|
%25 = load [5 x %Rune], ptr %19
|
|
%26 = alloca [5 x %Rune]
|
|
store [5 x %Rune] %25, ptr %26
|
|
%27 = alloca [11 x i8]
|
|
%28 = getelementptr [11 x i8], ptr %27, i32 0, i32 0
|
|
store i8 104, ptr %28
|
|
%29 = getelementptr [11 x i8], ptr %27, i32 0, i32 1
|
|
store i8 101, ptr %29
|
|
%30 = getelementptr [11 x i8], ptr %27, i32 0, i32 2
|
|
store i8 108, ptr %30
|
|
%31 = getelementptr [11 x i8], ptr %27, i32 0, i32 3
|
|
store i8 108, ptr %31
|
|
%32 = getelementptr [11 x i8], ptr %27, i32 0, i32 4
|
|
store i8 111, ptr %32
|
|
%33 = getelementptr [11 x i8], ptr %27, i32 0, i32 5
|
|
store i8 32, ptr %33
|
|
%34 = getelementptr [11 x i8], ptr %27, i32 0, i32 6
|
|
store i8 119, ptr %34
|
|
%35 = getelementptr [11 x i8], ptr %27, i32 0, i32 7
|
|
store i8 111, ptr %35
|
|
%36 = getelementptr [11 x i8], ptr %27, i32 0, i32 8
|
|
store i8 114, ptr %36
|
|
%37 = getelementptr [11 x i8], ptr %27, i32 0, i32 9
|
|
store i8 108, ptr %37
|
|
%38 = getelementptr [11 x i8], ptr %27, i32 0, i32 10
|
|
store i8 100, ptr %38
|
|
%39 = alloca %String
|
|
%40 = getelementptr %String, ptr %39, i32 0, i32 0
|
|
store ptr %27, ptr %40
|
|
%41 = getelementptr %String, ptr %39, i32 0, i32 1
|
|
store %Index 11, ptr %41
|
|
%42 = load %String, ptr %39
|
|
%43 = alloca %String
|
|
store %String %42, ptr %43
|
|
%44 = alloca [12 x i8]
|
|
%45 = getelementptr [12 x i8], ptr %44, i32 0, i32 0
|
|
store i8 104, ptr %45
|
|
%46 = getelementptr [12 x i8], ptr %44, i32 0, i32 1
|
|
store i8 101, ptr %46
|
|
%47 = getelementptr [12 x i8], ptr %44, i32 0, i32 2
|
|
store i8 108, ptr %47
|
|
%48 = getelementptr [12 x i8], ptr %44, i32 0, i32 3
|
|
store i8 108, ptr %48
|
|
%49 = getelementptr [12 x i8], ptr %44, i32 0, i32 4
|
|
store i8 111, ptr %49
|
|
%50 = getelementptr [12 x i8], ptr %44, i32 0, i32 5
|
|
store i8 32, ptr %50
|
|
%51 = getelementptr [12 x i8], ptr %44, i32 0, i32 6
|
|
store i8 119, ptr %51
|
|
%52 = getelementptr [12 x i8], ptr %44, i32 0, i32 7
|
|
store i8 111, ptr %52
|
|
%53 = getelementptr [12 x i8], ptr %44, i32 0, i32 8
|
|
store i8 114, ptr %53
|
|
%54 = getelementptr [12 x i8], ptr %44, i32 0, i32 9
|
|
store i8 108, ptr %54
|
|
%55 = getelementptr [12 x i8], ptr %44, i32 0, i32 10
|
|
store i8 100, ptr %55
|
|
%56 = getelementptr [12 x i8], ptr %44, i32 0, i32 11
|
|
store i8 0, ptr %56
|
|
%57 = alloca ptr
|
|
store ptr %44, ptr %57
|
|
%58 = load ptr, ptr %57
|
|
%59 = alloca ptr
|
|
store ptr %58, ptr %59
|
|
%60 = alloca [5 x i64]
|
|
%61 = getelementptr [5 x i64], ptr %60, i32 0, i32 0
|
|
store i64 1, ptr %61
|
|
%62 = getelementptr [5 x i64], ptr %60, i32 0, i32 1
|
|
store i64 2, ptr %62
|
|
%63 = getelementptr [5 x i64], ptr %60, i32 0, i32 2
|
|
store i64 3, ptr %63
|
|
%64 = getelementptr [5 x i64], ptr %60, i32 0, i32 3
|
|
store i64 4, ptr %64
|
|
%65 = getelementptr [5 x i64], ptr %60, i32 0, i32 4
|
|
store i64 5, ptr %65
|
|
%66 = load [5 x i64], ptr %60
|
|
%67 = alloca [5 x i64]
|
|
store [5 x i64] %66, ptr %67
|
|
%68 = alloca [5 x i64]
|
|
%69 = getelementptr [5 x i64], ptr %68, i32 0, i32 0
|
|
store i64 1, ptr %69
|
|
%70 = getelementptr [5 x i64], ptr %68, i32 0, i32 1
|
|
store i64 2, ptr %70
|
|
%71 = getelementptr [5 x i64], ptr %68, i32 0, i32 2
|
|
store i64 3, ptr %71
|
|
%72 = getelementptr [5 x i64], ptr %68, i32 0, i32 3
|
|
store i64 4, ptr %72
|
|
%73 = getelementptr [5 x i64], ptr %68, i32 0, i32 4
|
|
store i64 5, ptr %73
|
|
%74 = alloca { ptr, %Index }
|
|
%75 = getelementptr { ptr, %Index }, ptr %74, i32 0, i32 0
|
|
store ptr %68, ptr %75
|
|
%76 = getelementptr { ptr, %Index }, ptr %74, i32 0, i32 1
|
|
store %Index 5, ptr %76
|
|
%77 = load { ptr, %Index }, ptr %74
|
|
%78 = alloca { ptr, %Index }
|
|
store { ptr, %Index } %77, ptr %78
|
|
%79 = alloca { i64, { i64, i64 } }
|
|
%80 = getelementptr { i64, { i64, i64 } }, ptr %79, i32 0, i32 0
|
|
store i64 1, ptr %80
|
|
%81 = alloca { i64, i64 }
|
|
%82 = getelementptr { i64, i64 }, ptr %81, i32 0, i32 0
|
|
store i64 2, ptr %82
|
|
%83 = getelementptr { i64, i64 }, ptr %81, i32 0, i32 1
|
|
store i64 3, ptr %83
|
|
%84 = load { i64, i64 }, ptr %81
|
|
%85 = getelementptr { i64, { i64, i64 } }, ptr %79, i32 0, i32 1
|
|
store { i64, i64 } %84, ptr %85
|
|
%86 = load { i64, { i64, i64 } }, ptr %79
|
|
%87 = alloca { i64, { i64, i64 } }
|
|
store { i64, { i64, i64 } } %86, ptr %87
|
|
ret void
|
|
}
|
|
`,
|
|
`
|
|
[main] = {
|
|
; integers
|
|
ia:Int = 382
|
|
ib:Int = -4392
|
|
ic:Byte = 128
|
|
id:I8 = -127
|
|
|
|
; strings
|
|
sa:Byte = 'h'
|
|
sb:Rune = '0'
|
|
sc:5:Byte = 'hello'
|
|
sd:5:Rune = 'hello'
|
|
se:String = 'hello world'
|
|
sf:*Byte = 'hello world'
|
|
|
|
; arrays
|
|
aa:5:Int = (* 1 2 3 4 5)
|
|
ab:*:Int = (* 1 2 3 4 5)
|
|
|
|
; structs
|
|
ta:(x:Int y:(z:Int a:Int)) = (x: 1 y: (z: 2 a: 3))
|
|
}
|
|
`)
|
|
}
|
|
|
|
func TestTypedStructInstantiation (test *testing.T) {
|
|
testString (test,
|
|
`%A = type { i64 }
|
|
define void @main() {
|
|
0:
|
|
%1 = alloca %A
|
|
%2 = getelementptr %A, ptr %1, i32 0, i32 0
|
|
store i64 5, ptr %2
|
|
%3 = load %A, ptr %1
|
|
%4 = alloca %A
|
|
store %A %3, ptr %4
|
|
ret void
|
|
}
|
|
`,
|
|
`
|
|
A: (x:Int)
|
|
[main] 'main' = {
|
|
a:A = (x: 5)
|
|
}
|
|
`)
|
|
}
|
|
|
|
func TestTypedArrayInstantiation (test *testing.T) {
|
|
testString (test,
|
|
`%A = type [2 x i64]
|
|
define void @main() {
|
|
0:
|
|
%1 = alloca %A
|
|
%2 = getelementptr %A, ptr %1, i32 0, i32 0
|
|
store i64 1, ptr %2
|
|
%3 = getelementptr %A, ptr %1, i32 0, i32 1
|
|
store i64 2, ptr %3
|
|
%4 = load %A, ptr %1
|
|
%5 = alloca %A
|
|
store %A %4, ptr %5
|
|
ret void
|
|
}
|
|
`,
|
|
`
|
|
A: 2:Int
|
|
[main] 'main' = {
|
|
a:A = (* 1 2)
|
|
}
|
|
`)
|
|
}
|
|
|
|
func TestTypedStringInstantiation (test *testing.T) {
|
|
testString (test,
|
|
`%Byte = type i8
|
|
%A = type [1 x %Byte]
|
|
%Index = type i64
|
|
%B = type { ptr, %Index }
|
|
%C = type ptr
|
|
define void @main() {
|
|
0:
|
|
%1 = alloca [1 x i8]
|
|
%2 = getelementptr [1 x i8], ptr %1, i32 0, i32 0
|
|
store i8 104, ptr %2
|
|
%3 = load %A, ptr %1
|
|
%4 = alloca %A
|
|
store %A %3, ptr %4
|
|
%5 = alloca [1 x i8]
|
|
%6 = getelementptr [1 x i8], ptr %5, i32 0, i32 0
|
|
store i8 104, ptr %6
|
|
%7 = alloca %B
|
|
%8 = getelementptr %B, ptr %7, i32 0, i32 0
|
|
store ptr %5, ptr %8
|
|
%9 = getelementptr %B, ptr %7, i32 0, i32 1
|
|
store %Index 1, ptr %9
|
|
%10 = load %B, ptr %7
|
|
%11 = alloca %B
|
|
store %B %10, ptr %11
|
|
%12 = alloca [2 x i8]
|
|
%13 = getelementptr [2 x i8], ptr %12, i32 0, i32 0
|
|
store i8 104, ptr %13
|
|
%14 = getelementptr [2 x i8], ptr %12, i32 0, i32 1
|
|
store i8 0, ptr %14
|
|
%15 = alloca ptr
|
|
store ptr %12, ptr %15
|
|
%16 = load %C, ptr %15
|
|
%17 = alloca %C
|
|
store %C %16, ptr %17
|
|
ret void
|
|
}
|
|
`,
|
|
`
|
|
A: 1:Byte
|
|
B: *:Byte
|
|
C: *Byte
|
|
[main] 'main' = {
|
|
a:A = 'h'
|
|
b:B = 'h'
|
|
c:C = 'h'
|
|
}
|
|
`)
|
|
}
|