This commit is contained in:
parent
ba2314db4a
commit
b77d876ebb
11 changed files with 36 additions and 37 deletions
|
@ -2,14 +2,21 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.urbach.dev/cli/q/src/errors"
|
"git.urbach.dev/cli/q/src/errors"
|
||||||
|
"git.urbach.dev/cli/q/src/ssa"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckDeadCode checks for dead values.
|
// CheckDeadCode checks for dead values.
|
||||||
func (f *Function) CheckDeadCode() error {
|
func (f *Function) CheckDeadCode() error {
|
||||||
for instr := range f.Values {
|
for instr := range f.Values {
|
||||||
if instr.IsConst() && instr.CountUsers() == 0 {
|
if !instr.IsConst() {
|
||||||
return errors.New(&UnusedValue{Value: instr.String()}, f.File, instr.Start())
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(instr.(ssa.HasLiveness).Users()) > 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New(&UnusedValue{Value: instr.String()}, f.File, instr.(ssa.HasSource).Start())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -144,7 +144,7 @@ func (f *Function) Evaluate(expr *expression.Expression) (ssa.Value, error) {
|
||||||
Encountered: param.Type().Name(),
|
Encountered: param.Type().Name(),
|
||||||
Expected: fn.Input[i].Typ.Name(),
|
Expected: fn.Input[i].Typ.Name(),
|
||||||
ParameterName: fn.Input[i].Name,
|
ParameterName: fn.Input[i].Name,
|
||||||
}, f.File, param.Start())
|
}, f.File, param.(ssa.HasSource).Start())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ type Block struct {
|
||||||
// Append adds a new instruction to the block.
|
// Append adds a new instruction to the block.
|
||||||
func (block *Block) Append(instr Value) Value {
|
func (block *Block) Append(instr Value) Value {
|
||||||
for _, dep := range instr.Dependencies() {
|
for _, dep := range instr.Dependencies() {
|
||||||
dep.AddUser(instr)
|
dep.(HasLiveness).AddUser(instr)
|
||||||
}
|
}
|
||||||
|
|
||||||
block.Instructions = append(block.Instructions, instr)
|
block.Instructions = append(block.Instructions, instr)
|
||||||
|
|
6
src/ssa/HasLiveness.go
Normal file
6
src/ssa/HasLiveness.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package ssa
|
||||||
|
|
||||||
|
type HasLiveness interface {
|
||||||
|
AddUser(Value)
|
||||||
|
Users() []Value
|
||||||
|
}
|
8
src/ssa/HasSource.go
Normal file
8
src/ssa/HasSource.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package ssa
|
||||||
|
|
||||||
|
import "git.urbach.dev/cli/q/src/token"
|
||||||
|
|
||||||
|
type HasSource interface {
|
||||||
|
Start() token.Position
|
||||||
|
End() token.Position
|
||||||
|
}
|
|
@ -8,6 +8,6 @@ func (v *Liveness) AddUser(user Value) {
|
||||||
v.users = append(v.users, user)
|
v.users = append(v.users, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Liveness) CountUsers() int {
|
func (v *Liveness) Users() []Value {
|
||||||
return len(v.users)
|
return v.users
|
||||||
}
|
}
|
|
@ -1,6 +0,0 @@
|
||||||
package ssa
|
|
||||||
|
|
||||||
type NoLiveness struct{}
|
|
||||||
|
|
||||||
func (a *NoLiveness) AddUser(user Value) { panic("value does not have liveness") }
|
|
||||||
func (a *NoLiveness) CountUsers() int { return 0 }
|
|
|
@ -11,7 +11,6 @@ type Return struct {
|
||||||
Id
|
Id
|
||||||
Arguments
|
Arguments
|
||||||
Source
|
Source
|
||||||
NoLiveness
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Return) Equals(v Value) bool {
|
func (a *Return) Equals(v Value) bool {
|
||||||
|
|
|
@ -4,14 +4,10 @@ import "git.urbach.dev/cli/q/src/token"
|
||||||
|
|
||||||
type Source token.List
|
type Source token.List
|
||||||
|
|
||||||
func (v Source) End() token.Position {
|
|
||||||
return v[len(v)-1].End()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *Source) SetSource(source token.List) {
|
|
||||||
*v = Source(source)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v Source) Start() token.Position {
|
func (v Source) Start() token.Position {
|
||||||
return v[0].Position
|
return v[0].Position
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v Source) End() token.Position {
|
||||||
|
return v[len(v)-1].End()
|
||||||
}
|
}
|
|
@ -1,29 +1,16 @@
|
||||||
package ssa
|
package ssa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.urbach.dev/cli/q/src/token"
|
|
||||||
"git.urbach.dev/cli/q/src/types"
|
"git.urbach.dev/cli/q/src/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Value interface {
|
type Value interface {
|
||||||
// Essentials
|
|
||||||
Debug(bool) string
|
Debug(bool) string
|
||||||
|
Dependencies() []Value
|
||||||
|
Equals(Value) bool
|
||||||
ID() int
|
ID() int
|
||||||
IsConst() bool
|
IsConst() bool
|
||||||
SetID(int)
|
SetID(int)
|
||||||
String() string
|
String() string
|
||||||
Type() types.Type
|
Type() types.Type
|
||||||
|
|
||||||
// Arguments
|
|
||||||
Dependencies() []Value
|
|
||||||
Equals(Value) bool
|
|
||||||
|
|
||||||
// Liveness
|
|
||||||
AddUser(Value)
|
|
||||||
CountUsers() int
|
|
||||||
|
|
||||||
// Source
|
|
||||||
SetSource(token.List)
|
|
||||||
Start() token.Position
|
|
||||||
End() token.Position
|
|
||||||
}
|
}
|
|
@ -16,7 +16,9 @@ func (f *Compiler) GenerateAssembly(ir ssa.IR, isLeaf bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for instr := range ir.Values {
|
for instr := range ir.Values {
|
||||||
if instr.CountUsers() != 0 {
|
live, isLive := instr.(ssa.HasLiveness)
|
||||||
|
|
||||||
|
if isLive && len(live.Users()) > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue