Implemented compilation to SSA form
Some checks failed
/ test (push) Failing after 31s

This commit is contained in:
Eduard Urbach 2025-06-23 00:09:52 +02:00
parent f7be86a3d9
commit d1ce1ef839
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
24 changed files with 534 additions and 59 deletions

View file

@ -1,5 +1,9 @@
package ssa
import (
"git.urbach.dev/cli/q/src/cpu"
)
// Function is a list of basic blocks.
type Function struct {
Blocks []*Block
@ -10,4 +14,48 @@ func (f *Function) AddBlock() *Block {
block := &Block{}
f.Blocks = append(f.Blocks, block)
return block
}
// Append adds a new value to the last block.
func (f *Function) Append(instr Value) *Value {
if len(f.Blocks) == 0 {
f.Blocks = append(f.Blocks, &Block{})
}
if instr.IsConst() {
for _, b := range f.Blocks {
for _, existing := range b.Instructions {
if instr.Equals(existing) {
return &existing
}
}
}
}
return f.Blocks[len(f.Blocks)-1].Append(instr)
}
// AppendInt adds a new integer value to the last block.
func (f *Function) AppendInt(x int) *Value {
return f.Append(Value{Type: Int, Int: x})
}
// AppendRegister adds a new register value to the last block.
func (f *Function) AppendRegister(reg cpu.Register) *Value {
return f.Append(Value{Type: Register, Register: reg})
}
// AppendFunction adds a new function value to the last block.
func (f *Function) AppendFunction(name string) *Value {
return f.Append(Value{Type: Func, Text: name})
}
// AppendBytes adds a new byte slice value to the last block.
func (f *Function) AppendBytes(s []byte) *Value {
return f.Append(Value{Type: String, Text: string(s)})
}
// AppendString adds a new string value to the last block.
func (f *Function) AppendString(s string) *Value {
return f.Append(Value{Type: String, Text: s})
}