cross compilation

This commit is contained in:
Li Jie
2025-04-08 09:26:27 +08:00
parent a85d937482
commit e6c7627ee8
12 changed files with 496 additions and 102 deletions

View File

@@ -24,6 +24,8 @@ import (
"go/types"
"io"
"log"
"os"
"runtime"
"testing"
"github.com/goplus/llgo/cl/cltest"
@@ -65,7 +67,13 @@ func TestFromTestdata(t *testing.T) {
}
func TestMakeInterface(t *testing.T) {
prog := ssatest.NewProgram(t, &ssa.Target{GOARCH: "x86"})
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
os.Chdir("../runtime")
defer os.Chdir(wd)
prog := ssatest.NewProgram(t, &ssa.Target{GOARCH: runtime.GOARCH})
pkg := prog.NewPackage("foo", "foo")
fn := pkg.NewFunc("main", types.NewSignatureType(nil, nil, nil, nil, nil, false), ssa.InC)
b := fn.MakeBody(1)

View File

@@ -245,7 +245,7 @@ func (b Builder) checkIndex(idx Expr, max Expr) Expr {
} else {
typ = prog.Uint()
}
if prog.SizeOf(idx.Type) < prog.SizeOf(typ) {
if prog.SizeOf(idx.Type) != prog.SizeOf(typ) {
idx.Type = typ
idx.impl = castUintptr(b, idx.impl, typ)
}

View File

@@ -210,6 +210,22 @@ type aProgram struct {
// A Program presents a program.
type Program = *aProgram
var arch32 = map[string]bool{
"386": true,
"arm": true,
"mips": true,
"mipsle": true,
"s390x": true,
"wasm": true,
}
func is32Bits(arch string) bool {
if v, ok := arch32[arch]; ok {
return v
}
return false
}
// NewProgram creates a new program.
func NewProgram(target *Target) Program {
if target == nil {
@@ -231,7 +247,7 @@ func NewProgram(target *Target) Program {
// TODO(xsw): Finalize may cause panic, so comment it.
ctx.Finalize()
*/
is32Bits := (td.PointerSize() == 4 || target.GOARCH == "x86") // TODO(xsw): remove temp code
is32Bits := (td.PointerSize() == 4 || is32Bits(target.GOARCH))
return &aProgram{
ctx: ctx, gocvt: newGoTypes(), fnsCompiled: fnsCompiled,
target: target, td: td, is32Bits: is32Bits,
@@ -379,6 +395,12 @@ func (p Program) tyComplex128() llvm.Type {
// NewPackage creates a new package.
func (p Program) NewPackage(name, pkgPath string) Package {
mod := p.ctx.NewModule(pkgPath)
// TODO(lijie): enable target output will check module override, but can't
// pass the snapshot test, so disable it for now
// if p.target.GOARCH != runtime.GOARCH && p.target.GOOS != runtime.GOOS {
// mod.SetTarget(p.target.Spec().Triple)
// }
// TODO(xsw): Finalize may cause panic, so comment it.
// mod.Finalize()
gbls := make(map[string]Global)

View File

@@ -23,6 +23,7 @@ import (
"go/constant"
"go/token"
"go/types"
"os"
"testing"
"unsafe"
@@ -40,6 +41,12 @@ func TestEndDefer(t *testing.T) {
}
func TestUnsafeString(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
os.Chdir("../../runtime")
defer os.Chdir(wd)
prog := NewProgram(nil)
prog.SetRuntime(func() *types.Package {
fset := token.NewFileSet()

View File

@@ -17,6 +17,8 @@
package ssa
import (
"runtime"
"github.com/goplus/llvm"
)
@@ -29,15 +31,15 @@ type Target struct {
}
func (p *Target) targetData() llvm.TargetData {
spec := p.toSpec()
if spec.triple == "" {
spec.triple = llvm.DefaultTargetTriple()
spec := p.Spec()
if spec.Triple == "" {
spec.Triple = llvm.DefaultTargetTriple()
}
t, err := llvm.GetTargetFromTriple(spec.triple)
t, err := llvm.GetTargetFromTriple(spec.Triple)
if err != nil {
panic(err)
}
machine := t.CreateTargetMachine(spec.triple, spec.cpu, spec.features, llvm.CodeGenLevelDefault, llvm.RelocDefault, llvm.CodeModelDefault)
machine := t.CreateTargetMachine(spec.Triple, spec.CPU, spec.Features, llvm.CodeGenLevelDefault, llvm.RelocDefault, llvm.CodeModelDefault)
return machine.CreateTargetData()
}
@@ -62,19 +64,13 @@ func (p *Program) targetMachine() llvm.TargetMachine {
}
*/
type targetSpec struct {
triple string
cpu string
features string
type TargetSpec struct {
Triple string
CPU string
Features string
}
// TODO config
func (p *Target) toSpec() (spec targetSpec) {
return
}
/*
func (p *Target) toSpec() (spec targetSpec) {
func (p *Target) Spec() (spec TargetSpec) {
// Configure based on GOOS/GOARCH environment variables (falling back to
// runtime.GOOS/runtime.GOARCH), and generate a LLVM target based on it.
var llvmarch string
@@ -113,56 +109,55 @@ func (p *Target) toSpec() (spec targetSpec) {
case "darwin":
// Use macosx* instead of darwin, otherwise darwin/arm64 will refer
// to iOS!
llvmos = "macosx10.12.0"
llvmos = "macosx"
if llvmarch == "aarch64" {
// Looks like Apple prefers to call this architecture ARM64
// instead of AArch64.
llvmarch = "arm64"
llvmos = "macosx11.0.0"
llvmos = "macosx"
}
llvmvendor = "apple"
case "wasip1":
llvmos = "wasi"
llvmos = "wasip1"
}
// Target triples (which actually have four components, but are called
// triples for historical reasons) have the form:
// arch-vendor-os-environment
spec.triple = llvmarch + "-" + llvmvendor + "-" + llvmos
spec.Triple = llvmarch + "-" + llvmvendor + "-" + llvmos
if llvmos == "windows" {
spec.triple += "-gnu"
spec.Triple += "-gnu"
} else if goarch == "arm" {
spec.triple += "-gnueabihf"
spec.Triple += "-gnueabihf"
}
switch goarch {
case "386":
spec.cpu = "pentium4"
spec.features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
spec.CPU = "pentium4"
spec.Features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
case "amd64":
spec.cpu = "x86-64"
spec.features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
spec.CPU = "x86-64"
spec.Features = "+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
case "arm":
spec.cpu = "generic"
spec.CPU = "generic"
switch llvmarch {
case "armv5":
spec.features = "+armv5t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
spec.Features = "+armv5t,+strict-align,-aes,-bf16,-d32,-dotprod,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fp64,-fpregs,-fullfp16,-mve.fp,-neon,-sha2,-thumb-mode,-vfp2,-vfp2sp,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
case "armv6":
spec.features = "+armv6,+dsp,+fp64,+strict-align,+vfp2,+vfp2sp,-aes,-d32,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-neon,-sha2,-thumb-mode,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
spec.Features = "+armv6,+dsp,+fp64,+strict-align,+vfp2,+vfp2sp,-aes,-d32,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-neon,-sha2,-thumb-mode,-vfp3,-vfp3d16,-vfp3d16sp,-vfp3sp,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
case "armv7":
spec.features = "+armv7-a,+d32,+dsp,+fp64,+neon,+vfp2,+vfp2sp,+vfp3,+vfp3d16,+vfp3d16sp,+vfp3sp,-aes,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-sha2,-thumb-mode,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
spec.Features = "+armv7-a,+d32,+dsp,+fp64,+neon,+vfp2,+vfp2sp,+vfp3,+vfp3d16,+vfp3d16sp,+vfp3sp,-aes,-fp-armv8,-fp-armv8d16,-fp-armv8d16sp,-fp-armv8sp,-fp16,-fp16fml,-fullfp16,-sha2,-thumb-mode,-vfp4,-vfp4d16,-vfp4d16sp,-vfp4sp"
}
case "arm64":
spec.cpu = "generic"
spec.CPU = "generic"
if goos == "darwin" {
spec.features = "+neon"
spec.Features = "+neon"
} else { // windows, linux
spec.features = "+neon,-fmv"
spec.Features = "+neon,-fmv"
}
case "wasm":
spec.cpu = "generic"
spec.features = "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext"
spec.CPU = "generic"
spec.Features = "+bulk-memory,+mutable-globals,+nontrapping-fptoint,+sign-ext"
}
return
}
*/
// -----------------------------------------------------------------------------