Add and fix test case for #47 (on the generator side at least)

This commit is contained in:
Sasha Koshka 2024-02-28 13:36:21 -05:00
parent ddf1b57799
commit 75b48b7000
3 changed files with 53 additions and 4 deletions

View File

@ -86,3 +86,30 @@ define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
}
`)
}
func TestPtrIntCast (test *testing.T) {
testString (test,
`%"AAAAAAAAAAAAAAAAAAAAAA==::Index" = type i64
define void @"0zNZN147MN2wzMAQ6NS2dQ==::main"() {
0:
%1 = alloca ptr
store ptr zeroinitializer, ptr %1
%2 = alloca %"AAAAAAAAAAAAAAAAAAAAAA==::Index"
%3 = load ptr, ptr %1
%4 = ptrtoint ptr %3 to %"AAAAAAAAAAAAAAAAAAAAAA==::Index"
%5 = add i64 1, %4
store i64 %5, ptr %2
%6 = load %"AAAAAAAAAAAAAAAAAAAAAA==::Index", ptr %2
%7 = inttoptr %"AAAAAAAAAAAAAAAAAAAAAA==::Index" %6 to ptr
store ptr %7, ptr %1
ret void
}
`,
`
[main] = {
ptr:*Byte = nil
nptr:Index = [+ 1 [~~Index ptr]]
ptr = [~~*Byte nptr]
}
`)
}

View File

@ -264,18 +264,40 @@ func (this *generator) generateBitCastVal (cast *entity.BitCast) (llvm.Value, er
irToType, err := this.generateType(cast.Type())
if err != nil { return nil, err }
// Attempt to short circuit if types are equal to avoid LLVM bitching at
// us
// attempt to short circuit if types are equal, or else LLVM complains
if llvm.TypesEqual(irFromType, irToType) {
return this.generateExpressionVal(cast.Value)
}
from, err := this.generateExpressionVal(cast.Value)
if err != nil { return nil, err }
// determine how to *bit cast*. because of course its not that simple.
// thanks LLVM!
// FIXME: there are definetly edge cases that are not being covered here
// such as bit casting an integer to an integer, or a struct to a
// pointer, or other things you'd have to be an absolute blithering
// idiot to even want to do in the first place, but we have to support
// them because we are a compiler and this is a bit cast
fromType := analyzer.ReduceToBase(cast.Value.Type())
toType := analyzer.ReduceToBase(cast.Type())
switch toType.(type) {
case *entity.TypeInt, *entity.TypeWord:
switch fromType.(type) {
case *entity.TypePointer:
return this.blockManager.NewPtrToInt(from, irToType), nil
}
case *entity.TypePointer:
switch fromType.(type) {
case *entity.TypeInt, *entity.TypeWord:
return this.blockManager.NewIntToPtr(from, irToType), nil
}
}
// default to a normal bit cast
return this.blockManager.NewBitCast(from, irToType), nil
}
func (this *generator) generateOperationVal (operation *entity.Operation) (llvm.Value, error) {
nSameType := func (binary func (x, y llvm.Value) llvm.Value) (llvm.Value, error) {
irX, err := this.generateExpressionVal(operation.Arguments[0])

View File

@ -18,7 +18,7 @@ func testString (test *testing.T, correct string, input string) {
tree := analyzer.Tree { }
err := tree.Analyze(address.UUID(), nil, ast)
if err != nil {
test.Error("analyzer returned error:", err)
test.Error("analyzer returned error:\n" + errors.Format(err))
return
}