Files
llgo/cl/compile.go

237 lines
5.7 KiB
Go
Raw Normal View History

2023-12-10 11:24:08 +08:00
/*
2024-04-20 15:58:34 +08:00
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
2023-12-10 11:24:08 +08:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cl
2024-04-20 15:58:34 +08:00
import (
2024-04-20 17:31:49 +08:00
"fmt"
2024-04-20 15:58:34 +08:00
"sort"
llssa "github.com/goplus/llgo/ssa"
"golang.org/x/tools/go/ssa"
)
type Config struct {
}
2024-04-20 17:31:49 +08:00
// -----------------------------------------------------------------------------
type instrAndValue interface {
ssa.Instruction
ssa.Value
}
2024-04-20 15:58:34 +08:00
type context struct {
2024-04-20 23:15:10 +08:00
prog llssa.Program
pkg llssa.Package
fn llssa.Function
bvals map[ssa.Value]llssa.Expr // block values
2024-04-21 15:12:57 +08:00
inits []func()
2024-04-20 23:15:10 +08:00
}
func (p *context) compileType(pkg llssa.Package, member *ssa.Type) {
panic("todo")
2024-04-20 15:58:34 +08:00
}
// Global variable.
2024-04-21 15:12:57 +08:00
func (p *context) compileGlobal(pkg llssa.Package, gbl *ssa.Global) {
2024-04-20 17:31:49 +08:00
g := pkg.NewVar(gbl.Name(), gbl.Type())
2024-04-21 00:22:39 +08:00
g.Init(p.prog.Null(g.Type))
2024-04-20 15:58:34 +08:00
}
2024-04-21 15:12:57 +08:00
func (p *context) compileFunc(pkg llssa.Package, f *ssa.Function) {
fn := pkg.NewFunc(f.Name(), f.Signature)
p.inits = append(p.inits, func() {
p.fn = fn
defer func() {
p.fn = nil
}()
nblk := len(f.Blocks)
if nblk == 0 { // external function
return
}
fn.MakeBlocks(nblk)
b := fn.NewBuilder()
for _, block := range f.Blocks {
p.compileBlock(b, block)
}
})
2024-04-20 17:31:49 +08:00
}
2024-04-20 15:58:34 +08:00
2024-04-20 22:05:45 +08:00
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock) llssa.BasicBlock {
ret := p.fn.Block(block.Index)
b.SetBlock(ret)
2024-04-20 23:15:10 +08:00
p.bvals = make(map[ssa.Value]llssa.Expr)
2024-04-20 17:31:49 +08:00
for _, instr := range block.Instrs {
p.compileInstr(b, instr)
2024-04-20 15:58:34 +08:00
}
2024-04-20 22:05:45 +08:00
return ret
2024-04-20 15:58:34 +08:00
}
2024-04-20 22:16:28 +08:00
func (p *context) compileInstrAndValue(b llssa.Builder, iv instrAndValue) (ret llssa.Expr) {
2024-04-20 23:15:10 +08:00
if v, ok := p.bvals[iv]; ok {
2024-04-20 22:16:28 +08:00
return v
}
2024-04-20 17:31:49 +08:00
switch v := iv.(type) {
2024-04-20 23:15:10 +08:00
case *ssa.Call:
2024-04-21 15:12:57 +08:00
if false {
call := v.Call
fn := p.compileValue(b, call.Value)
args := p.compileValues(b, call.Args)
ret = b.Call(fn, args...)
}
2024-04-20 23:15:10 +08:00
case *ssa.BinOp:
x := p.compileValue(b, v.X)
y := p.compileValue(b, v.Y)
ret = b.BinOp(v.Op, x, y)
2024-04-20 17:31:49 +08:00
case *ssa.UnOp:
x := p.compileValue(b, v.X)
2024-04-20 22:16:28 +08:00
ret = b.UnOp(v.Op, x)
2024-04-21 15:12:57 +08:00
case *ssa.IndexAddr:
x := p.compileValue(b, v.X)
idx := p.compileValue(b, v.Index)
ret = b.IndexAddr(x, idx)
case *ssa.Alloc:
t := v.Type()
ret = b.Alloc(p.prog.Type(t), v.Heap)
2024-04-20 22:16:28 +08:00
default:
panic(fmt.Sprintf("compileInstrAndValue: unknown instr - %T\n", iv))
2024-04-20 17:31:49 +08:00
}
2024-04-20 23:15:10 +08:00
p.bvals[iv] = ret
2024-04-20 22:16:28 +08:00
return ret
2023-12-10 13:43:44 +08:00
}
2024-04-20 17:31:49 +08:00
func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
if iv, ok := instr.(instrAndValue); ok {
p.compileInstrAndValue(b, iv)
return
}
switch v := instr.(type) {
2024-04-20 22:05:45 +08:00
case *ssa.Store:
ptr := p.compileValue(b, v.Addr)
val := p.compileValue(b, v.Val)
b.Store(ptr, val)
case *ssa.Jump:
fn := p.fn
succs := v.Block().Succs
jmpb := fn.Block(succs[0].Index)
b.Jump(jmpb)
case *ssa.Return:
var results []llssa.Expr
if n := len(v.Results); n > 0 {
results = make([]llssa.Expr, n)
for i, r := range v.Results {
results[i] = p.compileValue(b, r)
}
}
b.Return(results...)
2024-04-20 17:31:49 +08:00
case *ssa.If:
2024-04-20 22:05:45 +08:00
fn := p.fn
cond := p.compileValue(b, v.Cond)
succs := v.Block().Succs
thenb := fn.Block(succs[0].Index)
elseb := fn.Block(succs[1].Index)
b.If(cond, thenb, elseb)
default:
panic(fmt.Sprintf("compileInstr: unknown instr - %T\n", instr))
2024-04-20 17:31:49 +08:00
}
2023-12-10 13:43:44 +08:00
}
2024-04-20 22:16:28 +08:00
func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
2024-04-20 22:05:45 +08:00
if iv, ok := v.(instrAndValue); ok {
2024-04-20 22:16:28 +08:00
return p.compileInstrAndValue(b, iv)
}
switch v := v.(type) {
2024-04-20 23:15:10 +08:00
case *ssa.Parameter:
fn := v.Parent()
for idx, param := range fn.Params {
if param == v {
return p.fn.Param(idx)
}
}
case *ssa.Function:
2024-04-21 15:12:57 +08:00
fn := p.pkg.FuncOf(v.Name())
2024-04-20 23:15:10 +08:00
return fn.Expr
2024-04-20 22:16:28 +08:00
case *ssa.Global:
2024-04-21 15:12:57 +08:00
g := p.pkg.VarOf(v.Name())
2024-04-20 22:16:28 +08:00
return g.Expr
case *ssa.Const:
2024-04-21 15:12:57 +08:00
t := v.Type()
return b.Const(v.Value, p.prog.Type(t))
2024-04-20 17:31:49 +08:00
}
2024-04-20 23:15:10 +08:00
panic(fmt.Sprintf("compileValue: unknown value - %T\n", v))
}
func (p *context) compileValues(b llssa.Builder, vals []ssa.Value) []llssa.Expr {
ret := make([]llssa.Expr, len(vals))
for i, v := range vals {
ret[i] = p.compileValue(b, v)
}
return ret
2023-12-10 13:43:44 +08:00
}
2024-04-20 17:31:49 +08:00
// -----------------------------------------------------------------------------
2023-12-10 13:43:44 +08:00
2024-04-20 17:31:49 +08:00
// NewPackage compiles a Go package to LLVM IR package.
func NewPackage(prog llssa.Program, pkg *ssa.Package, conf *Config) (ret llssa.Package, err error) {
2023-12-10 15:34:42 +08:00
type namedMember struct {
name string
val ssa.Member
}
// Sort by position, so that the order of the functions in the IR matches
// the order of functions in the source file. This is useful for testing,
// for example.
var members []*namedMember
for name, v := range pkg.Members {
members = append(members, &namedMember{name, v})
}
sort.Slice(members, func(i, j int) bool {
iPos := members[i].val.Pos()
jPos := members[j].val.Pos()
return iPos < jPos
})
2024-04-20 17:31:49 +08:00
pkgTypes := pkg.Pkg
ret = prog.NewPackage(pkgTypes.Name(), pkgTypes.Path())
ctx := &context{
prog: prog,
pkg: ret,
}
2023-12-10 15:34:42 +08:00
for _, m := range members {
member := m.val
switch member := member.(type) {
case *ssa.Function:
if member.TypeParams() != nil {
// Do not try to build generic (non-instantiated) functions.
continue
}
2024-04-20 22:05:45 +08:00
ctx.compileFunc(ret, member)
2023-12-10 15:34:42 +08:00
case *ssa.Type:
2024-04-20 17:31:49 +08:00
ctx.compileType(ret, member)
2023-12-10 15:34:42 +08:00
case *ssa.Global:
2024-04-20 17:31:49 +08:00
ctx.compileGlobal(ret, member)
2023-12-10 15:34:42 +08:00
}
}
2024-04-21 15:12:57 +08:00
for _, ini := range ctx.inits {
ini()
}
2023-12-11 00:37:09 +08:00
return
}
2024-04-20 17:31:49 +08:00
// -----------------------------------------------------------------------------