Reduced the number of IR methods
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Eduard Urbach 2025-07-03 19:25:07 +02:00
parent 72ace483e4
commit 0eaeb726b9
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
8 changed files with 112 additions and 101 deletions

View file

@ -11,12 +11,12 @@ import (
func TestBinaryOp(t *testing.T) {
fn := ssa.IR{}
a := fn.AppendInt(1)
b := fn.AppendInt(2)
a := fn.Append(&ssa.Int{Int: 1})
b := fn.Append(&ssa.Int{Int: 2})
c := fn.Append(&ssa.BinaryOp{Op: token.Add, Left: a, Right: b})
fn.AddBlock()
d := fn.AppendInt(3)
e := fn.AppendInt(4)
d := fn.Append(&ssa.Int{Int: 3})
e := fn.Append(&ssa.Int{Int: 4})
f := fn.Append(&ssa.BinaryOp{Op: token.Add, Left: d, Right: e})
assert.Equal(t, c.String(), "1 + 2")
@ -26,11 +26,11 @@ func TestBinaryOp(t *testing.T) {
func TestBinaryOpEquals(t *testing.T) {
fn := ssa.IR{}
one := fn.AppendInt(1)
two := fn.AppendInt(2)
one := fn.Append(&ssa.Int{Int: 1})
two := fn.Append(&ssa.Int{Int: 2})
binOp := fn.Append(&ssa.BinaryOp{Op: token.Add, Left: one, Right: two})
oneDup := fn.AppendInt(1)
twoDup := fn.AppendInt(2)
oneDup := fn.Append(&ssa.Int{Int: 1})
twoDup := fn.Append(&ssa.Int{Int: 2})
binOpDup := fn.Append(&ssa.BinaryOp{Op: token.Add, Left: oneDup, Right: twoDup})
binOpDiff := fn.Append(&ssa.BinaryOp{Op: token.Add, Left: oneDup, Right: oneDup})

View file

@ -13,7 +13,7 @@ func TestBytes(t *testing.T) {
hello := fn.Append(&ssa.Bytes{Bytes: []byte("Hello")})
world := fn.Append(&ssa.Bytes{Bytes: []byte("World")})
helloDup := fn.Append(&ssa.Bytes{Bytes: []byte("Hello")})
one := fn.AppendInt(1)
one := fn.Append(&ssa.Int{Int: 1})
assert.False(t, hello.Equals(world))
assert.False(t, hello.Equals(one))

View file

@ -12,7 +12,7 @@ func TestCall(t *testing.T) {
fn := ssa.IR{}
myfunc := fn.Append(&ssa.Function{UniqueName: "myfunc", Typ: &types.Function{}})
call := fn.Append(&ssa.Call{Arguments: ssa.Arguments{myfunc}})
one := fn.AppendInt(1)
one := fn.Append(&ssa.Int{Int: 1})
call2 := fn.Append(&ssa.Call{Arguments: ssa.Arguments{myfunc, one}})
assert.True(t, call.Type() == types.Void)
@ -31,8 +31,8 @@ func TestCallEquals(t *testing.T) {
},
})
one := fn.AppendInt(1)
two := fn.AppendInt(2)
one := fn.Append(&ssa.Int{Int: 1})
two := fn.Append(&ssa.Int{Int: 2})
call1 := fn.Append(&ssa.Call{Arguments: ssa.Arguments{sum, one, two}})
call2 := fn.Append(&ssa.Call{Arguments: ssa.Arguments{sum, one, two}})
@ -51,8 +51,8 @@ func TestCallReturnType(t *testing.T) {
},
})
one := fn.AppendInt(1)
two := fn.AppendInt(2)
one := fn.Append(&ssa.Int{Int: 1})
two := fn.Append(&ssa.Int{Int: 2})
call := fn.Append(&ssa.Call{Arguments: ssa.Arguments{sum, one, two}})
assert.Equal(t, call.String(), "sum(1, 2)")

View file

@ -1,9 +1,5 @@
package ssa
import (
"git.urbach.dev/cli/q/src/types"
)
// IR is a list of basic blocks.
type IR struct {
Blocks []*Block
@ -26,12 +22,10 @@ func (f *IR) Append(instr Value) Value {
f.AddBlock()
}
if instr.IsConst() {
for existing := range f.Values {
if existing.IsConst() && instr.Equals(existing) {
return existing
}
}
existing := f.FindExisting(instr)
if existing != nil {
return existing
}
instr.SetID(f.nextId)
@ -39,24 +33,19 @@ func (f *IR) Append(instr Value) Value {
return f.Blocks[len(f.Blocks)-1].Append(instr)
}
// AppendInt adds a new integer value to the last block.
func (f *IR) AppendInt(x int) Value {
return f.Append(&Int{Int: x})
}
// FindExisting returns an equal instruction that's already appended or `nil` if none could be found.
func (f *IR) FindExisting(instr Value) Value {
if !instr.IsConst() {
return nil
}
// AppendFunction adds a new function value to the last block.
func (f *IR) AppendFunction(name string, typ *types.Function, extern bool) Value {
return f.Append(&Function{UniqueName: name, Typ: typ, IsExtern: extern})
}
for existing := range f.Values {
if existing.IsConst() && instr.Equals(existing) {
return existing
}
}
// AppendBytes adds a new byte slice value to the last block.
func (f *IR) AppendBytes(s []byte) Value {
return f.Append(&Bytes{Bytes: s})
}
// AppendString adds a new string value to the last block.
func (f *IR) AppendString(s string) Value {
return f.Append(&Bytes{Bytes: []byte(s)})
return nil
}
// Values yields on each value.

View file

@ -1,7 +1,7 @@
package ssa
import (
"fmt"
"strconv"
"git.urbach.dev/cli/q/src/types"
)
@ -36,7 +36,7 @@ func (v *Int) Debug(expand bool) string {
}
func (v *Int) String() string {
return fmt.Sprintf("%d", v.Int)
return strconv.Itoa(v.Int)
}
func (v *Int) Type() types.Type {

View file

@ -11,9 +11,9 @@ import (
func TestReturn(t *testing.T) {
fn := ssa.IR{}
ret := fn.Append(&ssa.Return{})
one := fn.AppendInt(1)
one := fn.Append(&ssa.Int{Int: 1})
ret2 := fn.Append(&ssa.Return{Arguments: ssa.Arguments{one}})
two := fn.AppendInt(2)
two := fn.Append(&ssa.Int{Int: 2})
ret3 := fn.Append(&ssa.Return{Arguments: ssa.Arguments{one, two}})
ret4 := fn.Append(&ssa.Return{Arguments: ssa.Arguments{two, one}})
ret5 := fn.Append(&ssa.Return{Arguments: ssa.Arguments{one, two}})

View file

@ -11,9 +11,9 @@ import (
func TestSyscall(t *testing.T) {
fn := ssa.IR{}
syscall := fn.Append(&ssa.Syscall{})
one := fn.AppendInt(1)
one := fn.Append(&ssa.Int{Int: 1})
syscall2 := fn.Append(&ssa.Syscall{Arguments: ssa.Arguments{one}})
two := fn.AppendInt(2)
two := fn.Append(&ssa.Int{Int: 2})
syscall3 := fn.Append(&ssa.Syscall{Arguments: ssa.Arguments{one, two}})
syscall4 := fn.Append(&ssa.Syscall{Arguments: ssa.Arguments{one, two}})