From 9b45bc56d45362aef93288016c3def79d3c3ddee Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 26 Mar 2024 13:12:38 -0400 Subject: [PATCH 01/21] Resolver is now used behind a pointer --- compiler/compiler.go | 2 +- compiler/resolver.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/compiler.go b/compiler/compiler.go index 265c942..b50d1ac 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -15,7 +15,7 @@ import "git.tebibyte.media/fspl/fspl/parser/meta" import ferrors "git.tebibyte.media/fspl/fspl/errors" type Compiler struct { - Resolver + *Resolver cli.Logger Output string diff --git a/compiler/resolver.go b/compiler/resolver.go index e6ea9c1..39f313a 100644 --- a/compiler/resolver.go +++ b/compiler/resolver.go @@ -21,8 +21,8 @@ type Resolver struct { } // NewResolver creates a new resolver with os.DirFS("/"). -func NewResolver (path ...string) Resolver { - return Resolver { +func NewResolver (path ...string) *Resolver { + return &Resolver { FS: os.DirFS("/"), Path: path, } From 11e7a83eb401b1b2aca2272ea8374e14f9b39f92 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 26 Mar 2024 13:15:03 -0400 Subject: [PATCH 02/21] Add native paths for unix --- cmd/fsplc/main.go | 15 ++++----------- compiler/native/native.go | 10 ++++++++++ compiler/native/native_unix.go | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 compiler/native/native.go create mode 100644 compiler/native/native_unix.go diff --git a/cmd/fsplc/main.go b/cmd/fsplc/main.go index ed1864d..700e83a 100644 --- a/cmd/fsplc/main.go +++ b/cmd/fsplc/main.go @@ -3,30 +3,23 @@ package main import "os" import "io" import "fmt" -import "path/filepath" import "git.tebibyte.media/fspl/fspl/cli" import "git.tebibyte.media/fspl/fspl/entity" import "git.tebibyte.media/fspl/fspl/compiler" import ferrors "git.tebibyte.media/fspl/fspl/errors" +import "git.tebibyte.media/fspl/fspl/compiler/native" func main () { // instantiate the compiler - // FIXME: perhaps we want different paths on Windows? comp := new(compiler.Compiler) comp.Writer = os.Stderr - comp.Resolver = compiler.NewResolver ( - "/usr/local/src/fspl", - "/usr/src/fspl", - "/usr/local/incude/fspl", - "/usr/include/fspl") - homeDir, err := os.UserHomeDir() + + resolver, err := native.NativeResolver() if err != nil { comp.Errorln(err) os.Exit(2) } - comp.Resolver.AddPathFront ( - filepath.Join(homeDir, ".local/src/fspl"), - filepath.Join(homeDir, ".local/include/fspl")) + comp.Resolver = resolver // take in CLI flags debug := cli.NewFlag ( diff --git a/compiler/native/native.go b/compiler/native/native.go new file mode 100644 index 0000000..f4ab7d2 --- /dev/null +++ b/compiler/native/native.go @@ -0,0 +1,10 @@ +// Package native provides OS native parameters for the compilation process. +package native + +import "git.tebibyte.media/fspl/fspl/compiler" + +// NativeResolver returns a resolver that resolves paths native to the operating +// system. +func NativeResolver () (*compiler.Resolver, error) { + return nativeResolver() +} diff --git a/compiler/native/native_unix.go b/compiler/native/native_unix.go new file mode 100644 index 0000000..68d2fc7 --- /dev/null +++ b/compiler/native/native_unix.go @@ -0,0 +1,19 @@ +package native + +import "os" +import "path/filepath" +import "git.tebibyte.media/fspl/fspl/compiler" + +func nativeResolver () (*compiler.Resolver, error) { + resolver := compiler.NewResolver ( + "/usr/local/src/fspl", + "/usr/src/fspl", + "/usr/local/incude/fspl", + "/usr/include/fspl") + homeDir, err := os.UserHomeDir() + if err != nil { return nil, err } + resolver.AddPathFront ( + filepath.Join(homeDir, ".local/src/fspl"), + filepath.Join(homeDir, ".local/include/fspl")) + return resolver, nil +} From 2edda8a96099d567766f68089e74546f75f4f33f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 26 Mar 2024 13:15:56 -0400 Subject: [PATCH 03/21] Add stub for Windows native parameters --- compiler/native/native_windows.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 compiler/native/native_windows.go diff --git a/compiler/native/native_windows.go b/compiler/native/native_windows.go new file mode 100644 index 0000000..6aa3c52 --- /dev/null +++ b/compiler/native/native_windows.go @@ -0,0 +1,9 @@ +package native + +import "os" +import "path/filepath" +import "git.tebibyte.media/fspl/fspl/compiler" + +func nativeResolver () (*compiler.Resolver, error) { + // TODO +} From 10246f72682a04ae8cd99c6a3c1685e8b1d12af8 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 11:24:26 -0400 Subject: [PATCH 04/21] Add untested windows native paths --- compiler/native/native_windows.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/compiler/native/native_windows.go b/compiler/native/native_windows.go index 6aa3c52..31bf129 100644 --- a/compiler/native/native_windows.go +++ b/compiler/native/native_windows.go @@ -1,9 +1,30 @@ package native import "os" +import "errors" import "path/filepath" import "git.tebibyte.media/fspl/fspl/compiler" func nativeResolver () (*compiler.Resolver, error) { - // TODO + localAppData := os.Getenv("LOCALAPPDATA") + if localAppData == "" { + return nil, errors.New("could not get %LOCALAPPDATA%") + } + allUsersProfile := os.Getenv("ALLUSERSPROFILE") + if allUsersProfile == "" { + return nil, errors.New("could not get %ALLUSERSPROFILE%") + } + programFiles := os.Getenv("ProgramFiles") + if programFiles == "" { + return nil, errors.New("could not get %ProgramFiles%") + } + + resolver := compiler.NewResolver ( + filepath.Join(localAppData, "fspl\\src"), + filepath.Join(localAppData, "fspl\\include"), + filepath.Join(allUsersProfile, "fspl\\src"), + filepath.Join(allUsersProfile, "fspl\\include"), + filepath.Join(programFiles, "fspl\\src"), + filepath.Join(programFiles, "fspl\\include")) + return resolver, nil } From 92ad52b2aaf370d509cc4a7b41b55d618ee49c76 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 12:05:30 -0400 Subject: [PATCH 05/21] Add OS field to Target --- generator/generator.go | 7 ++++++- generator/native/native.go | 9 ++++++++- generator/native/native_386.go | 2 +- generator/native/native_amd64.go | 2 +- generator/native/native_amd64p32.go | 2 +- generator/native/native_arm.go | 2 +- generator/native/native_arm64.go | 2 +- generator/native/native_arm64be.go | 2 +- generator/native/native_armbe.go | 2 +- generator/native/native_mips.go | 2 +- generator/native/native_mips64.go | 2 +- generator/native/native_mips64le.go | 2 +- generator/native/native_mipsle.go | 2 +- generator/native/native_ppc.go | 2 +- generator/native/native_ppc64.go | 2 +- generator/native/native_ppc64le.go | 2 +- generator/native/native_riscv.go | 2 +- generator/native/native_sparc.go | 2 +- generator/native/native_sparc64.go | 2 +- generator/native/native_wasm.go | 2 +- generator/native/osnative_aix.go | 5 +++++ generator/native/osnative_android.go | 5 +++++ generator/native/osnative_darwin.go | 5 +++++ generator/native/osnative_dragonfly.go | 5 +++++ generator/native/osnative_freebsd.go | 5 +++++ generator/native/osnative_illumos.go | 6 ++++++ generator/native/osnative_ios.go | 5 +++++ generator/native/osnative_js.go | 5 +++++ generator/native/osnative_linux.go | 5 +++++ generator/native/osnative_nacl.go | 5 +++++ generator/native/osnative_netbsd.go | 5 +++++ generator/native/osnative_openbsd.go | 5 +++++ generator/native/osnative_plan9.go | 6 ++++++ generator/native/osnative_solaris.go | 5 +++++ generator/native/osnative_windows.go | 5 +++++ 35 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 generator/native/osnative_aix.go create mode 100644 generator/native/osnative_android.go create mode 100644 generator/native/osnative_darwin.go create mode 100644 generator/native/osnative_dragonfly.go create mode 100644 generator/native/osnative_freebsd.go create mode 100644 generator/native/osnative_illumos.go create mode 100644 generator/native/osnative_ios.go create mode 100644 generator/native/osnative_js.go create mode 100644 generator/native/osnative_linux.go create mode 100644 generator/native/osnative_nacl.go create mode 100644 generator/native/osnative_netbsd.go create mode 100644 generator/native/osnative_openbsd.go create mode 100644 generator/native/osnative_plan9.go create mode 100644 generator/native/osnative_solaris.go create mode 100644 generator/native/osnative_windows.go diff --git a/generator/generator.go b/generator/generator.go index ea8d9e9..79b8b03 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -16,8 +16,13 @@ type Target struct { // of the Word type. WordSize uint64 - // Arch specifies the machine architecture + // Arch specifies the machine architecture. Values must correspond + // directly to those recognized by LLVM in a target triple. Arch string + + // OS specifies the machine operating system. Values must correspond + // directly to those recognized by LLVM in a target triple. + OS string } type generator struct { diff --git a/generator/native/native.go b/generator/native/native.go index 4250ddc..7f3a7ed 100644 --- a/generator/native/native.go +++ b/generator/native/native.go @@ -4,7 +4,14 @@ package native import "git.tebibyte.media/fspl/fspl/generator" +// LLVM supported operating systems (note: capitalization is wrong) +// https://llvm.org/doxygen/Triple_8h_source.html +// GOOS and GOARCH values: +// https://go.dev/doc/install/source#environment + // NativeTarget returns a target describing the current system. func NativeTarget () generator.Target { - return nativeTarget() + target := nativeArch() + target.OS = nativeOS() + return target } diff --git a/generator/native/native_386.go b/generator/native/native_386.go index 41c14a1..b43ea15 100644 --- a/generator/native/native_386.go +++ b/generator/native/native_386.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "i386", diff --git a/generator/native/native_amd64.go b/generator/native/native_amd64.go index 9fc80ff..a25359c 100644 --- a/generator/native/native_amd64.go +++ b/generator/native/native_amd64.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "x86_64", diff --git a/generator/native/native_amd64p32.go b/generator/native/native_amd64p32.go index 7a80955..82fff57 100644 --- a/generator/native/native_amd64p32.go +++ b/generator/native/native_amd64p32.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { // this may not be accurate, can't find info online about amd64p32 return generator.Target { WordSize: 32, diff --git a/generator/native/native_arm.go b/generator/native/native_arm.go index e1c4c50..6734b84 100644 --- a/generator/native/native_arm.go +++ b/generator/native/native_arm.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "arm", diff --git a/generator/native/native_arm64.go b/generator/native/native_arm64.go index c42d90f..fb3bbc6 100644 --- a/generator/native/native_arm64.go +++ b/generator/native/native_arm64.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "aarch64", diff --git a/generator/native/native_arm64be.go b/generator/native/native_arm64be.go index 5e00b09..a01ff49 100644 --- a/generator/native/native_arm64be.go +++ b/generator/native/native_arm64be.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "aarch64_be", diff --git a/generator/native/native_armbe.go b/generator/native/native_armbe.go index d12d225..b65ecd8 100644 --- a/generator/native/native_armbe.go +++ b/generator/native/native_armbe.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "armeb", diff --git a/generator/native/native_mips.go b/generator/native/native_mips.go index eb1b7cd..4f04d48 100644 --- a/generator/native/native_mips.go +++ b/generator/native/native_mips.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "mips", diff --git a/generator/native/native_mips64.go b/generator/native/native_mips64.go index a31931d..51b8728 100644 --- a/generator/native/native_mips64.go +++ b/generator/native/native_mips64.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "mips64", diff --git a/generator/native/native_mips64le.go b/generator/native/native_mips64le.go index 55c57ef..8388606 100644 --- a/generator/native/native_mips64le.go +++ b/generator/native/native_mips64le.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "mips64el", diff --git a/generator/native/native_mipsle.go b/generator/native/native_mipsle.go index eba2835..8e0eb6a 100644 --- a/generator/native/native_mipsle.go +++ b/generator/native/native_mipsle.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "mipsel", diff --git a/generator/native/native_ppc.go b/generator/native/native_ppc.go index f338477..2b7406f 100644 --- a/generator/native/native_ppc.go +++ b/generator/native/native_ppc.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "ppc32", diff --git a/generator/native/native_ppc64.go b/generator/native/native_ppc64.go index 06334bc..05b52fe 100644 --- a/generator/native/native_ppc64.go +++ b/generator/native/native_ppc64.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "ppc64", diff --git a/generator/native/native_ppc64le.go b/generator/native/native_ppc64le.go index 63d4453..7ebcf6f 100644 --- a/generator/native/native_ppc64le.go +++ b/generator/native/native_ppc64le.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "ppc64le", diff --git a/generator/native/native_riscv.go b/generator/native/native_riscv.go index 3ebcaa9..f2b97e3 100644 --- a/generator/native/native_riscv.go +++ b/generator/native/native_riscv.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "riscv32", diff --git a/generator/native/native_sparc.go b/generator/native/native_sparc.go index 6e1474a..3fde94f 100644 --- a/generator/native/native_sparc.go +++ b/generator/native/native_sparc.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 32, Arch: "sparc", diff --git a/generator/native/native_sparc64.go b/generator/native/native_sparc64.go index 5994637..65be0f6 100644 --- a/generator/native/native_sparc64.go +++ b/generator/native/native_sparc64.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { return generator.Target { WordSize: 64, Arch: "sparcv9", diff --git a/generator/native/native_wasm.go b/generator/native/native_wasm.go index 93e89fe..a82c00e 100644 --- a/generator/native/native_wasm.go +++ b/generator/native/native_wasm.go @@ -2,7 +2,7 @@ package native import "git.tebibyte.media/fspl/fspl/generator" -func nativeTarget () generator.Target { +func nativeArch () generator.Target { // FIXME: golang doesn't discern between 32/64 bit wasm so we assume 64 // bit here return generator.Target { diff --git a/generator/native/osnative_aix.go b/generator/native/osnative_aix.go new file mode 100644 index 0000000..6cf1bf8 --- /dev/null +++ b/generator/native/osnative_aix.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "aix" +} diff --git a/generator/native/osnative_android.go b/generator/native/osnative_android.go new file mode 100644 index 0000000..9952e97 --- /dev/null +++ b/generator/native/osnative_android.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "linux" +} diff --git a/generator/native/osnative_darwin.go b/generator/native/osnative_darwin.go new file mode 100644 index 0000000..8abde5c --- /dev/null +++ b/generator/native/osnative_darwin.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "darwin" +} diff --git a/generator/native/osnative_dragonfly.go b/generator/native/osnative_dragonfly.go new file mode 100644 index 0000000..ee066e3 --- /dev/null +++ b/generator/native/osnative_dragonfly.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "dragonfly" +} diff --git a/generator/native/osnative_freebsd.go b/generator/native/osnative_freebsd.go new file mode 100644 index 0000000..fa277c1 --- /dev/null +++ b/generator/native/osnative_freebsd.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "freebsd" +} diff --git a/generator/native/osnative_illumos.go b/generator/native/osnative_illumos.go new file mode 100644 index 0000000..f1a80ed --- /dev/null +++ b/generator/native/osnative_illumos.go @@ -0,0 +1,6 @@ +package native + +func nativeOS () string { + // does not seem to be supported by LLVM at the moment + return "illumos" +} diff --git a/generator/native/osnative_ios.go b/generator/native/osnative_ios.go new file mode 100644 index 0000000..e77fd31 --- /dev/null +++ b/generator/native/osnative_ios.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "ios" +} diff --git a/generator/native/osnative_js.go b/generator/native/osnative_js.go new file mode 100644 index 0000000..8300389 --- /dev/null +++ b/generator/native/osnative_js.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "unknown" +} diff --git a/generator/native/osnative_linux.go b/generator/native/osnative_linux.go new file mode 100644 index 0000000..9952e97 --- /dev/null +++ b/generator/native/osnative_linux.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "linux" +} diff --git a/generator/native/osnative_nacl.go b/generator/native/osnative_nacl.go new file mode 100644 index 0000000..a81151c --- /dev/null +++ b/generator/native/osnative_nacl.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "nacl" +} diff --git a/generator/native/osnative_netbsd.go b/generator/native/osnative_netbsd.go new file mode 100644 index 0000000..498e1eb --- /dev/null +++ b/generator/native/osnative_netbsd.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "netbsd" +} diff --git a/generator/native/osnative_openbsd.go b/generator/native/osnative_openbsd.go new file mode 100644 index 0000000..7311e48 --- /dev/null +++ b/generator/native/osnative_openbsd.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "openbsd" +} diff --git a/generator/native/osnative_plan9.go b/generator/native/osnative_plan9.go new file mode 100644 index 0000000..7f7f1f0 --- /dev/null +++ b/generator/native/osnative_plan9.go @@ -0,0 +1,6 @@ +package native + +func nativeOS () string { + // does not seem to be supported by LLVM at the moment + return "plan9" +} diff --git a/generator/native/osnative_solaris.go b/generator/native/osnative_solaris.go new file mode 100644 index 0000000..588c0c6 --- /dev/null +++ b/generator/native/osnative_solaris.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "solaris" +} diff --git a/generator/native/osnative_windows.go b/generator/native/osnative_windows.go new file mode 100644 index 0000000..3f14020 --- /dev/null +++ b/generator/native/osnative_windows.go @@ -0,0 +1,5 @@ +package native + +func nativeOS () string { + return "win32" +} From 0404202691579dcdfccc5504c7376b4c42d41617 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 12:10:19 -0400 Subject: [PATCH 06/21] Compiler takes in a target --- compiler/compile-unit.go | 6 +++++- compiler/compiler.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/compile-unit.go b/compiler/compile-unit.go index 2570979..b1377f7 100644 --- a/compiler/compile-unit.go +++ b/compiler/compile-unit.go @@ -22,7 +22,11 @@ func (this *Compiler) CompileUnit (address entity.Address) error { _, err = this.AnalyzeUnit(&semanticTree, path, false) if err != nil { return err } - irModule, err := native.NativeTarget().Generate(semanticTree) + if this.Target == nil { + target := native.NativeTarget() + this.Target = &target + } + irModule, err := this.Target.Generate(semanticTree) if err != nil { return this.bug(err) } diff --git a/compiler/compiler.go b/compiler/compiler.go index b50d1ac..da6aa56 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -10,6 +10,7 @@ import "git.tebibyte.media/fspl/fspl/cli" import "git.tebibyte.media/fspl/fspl/lexer" import "git.tebibyte.media/fspl/fspl/entity" import "git.tebibyte.media/fspl/fspl/analyzer" +import "git.tebibyte.media/fspl/fspl/generator" import "git.tebibyte.media/fspl/fspl/parser/fspl" import "git.tebibyte.media/fspl/fspl/parser/meta" import ferrors "git.tebibyte.media/fspl/fspl/errors" @@ -18,6 +19,7 @@ type Compiler struct { *Resolver cli.Logger + Target *generator.Target Output string Optimization string Filetype Filetype From cd396952f2ad20270c06f25d4b198128dfa9d059 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 12:40:45 -0400 Subject: [PATCH 07/21] Certain filename extentions depend on target os --- compiler/compile-unit.go | 6 ++++-- compiler/filetype.go | 32 +++++++++++++++++++------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/compiler/compile-unit.go b/compiler/compile-unit.go index b1377f7..7433a8b 100644 --- a/compiler/compile-unit.go +++ b/compiler/compile-unit.go @@ -35,7 +35,9 @@ func (this *Compiler) CompileUnit (address entity.Address) error { // extension of the input. if that isn't specified, default to .o if this.Filetype == FiletypeUnknown { var ok bool - this.Filetype, ok = FiletypeFromExt(filepath.Ext(this.Output)) + this.Filetype, ok = FiletypeFromExt ( + *this.Target, + filepath.Ext(this.Output)) if !ok { this.Filetype = FiletypeObject } @@ -46,7 +48,7 @@ func (this *Compiler) CompileUnit (address entity.Address) error { if this.Output == "" { nickname, ok := entity.Address(path).Nickname() if !ok { nickname = "output" } - this.Output = this.Filetype.Extend(nickname) + this.Output = this.Filetype.Extend(*this.Target, nickname) } // do something based on the output extension diff --git a/compiler/filetype.go b/compiler/filetype.go index e229b10..040ea7b 100644 --- a/compiler/filetype.go +++ b/compiler/filetype.go @@ -1,6 +1,7 @@ package compiler import "fmt" +import "git.tebibyte.media/fspl/fspl/generator" // Filetype represents an output filetype. type Filetype int; const ( @@ -24,15 +25,13 @@ func FiletypeFromString (ext string) (Filetype, bool) { } // FiletypeFromExt returns a filetype based on the given filename extension. -func FiletypeFromExt (ext string) (Filetype, bool) { - // FIXME some of these are platform dependent, for example on windows - // FiletypeLib would be .dll +func FiletypeFromExt (target generator.Target, ext string) (Filetype, bool) { switch ext { - case ".o": return FiletypeObject, true - case ".so": return FiletypeLibrary, true - case ".s": return FiletypeAssembly, true - case ".ll": return FiletypeIR, true - default: return FiletypeUnknown, false + case ".o": return FiletypeObject, true + case targetSoExt(target): return FiletypeLibrary, true + case ".s": return FiletypeAssembly, true + case ".ll": return FiletypeIR, true + default: return FiletypeUnknown, false } } @@ -49,12 +48,11 @@ func (filetype Filetype) String () string { } // Ext returns the standard filename extension of the filetype. -func (filetype Filetype) Ext () string { - // FIXME again, some of these are platform dependent +func (filetype Filetype) Ext (target generator.Target) string { switch filetype { case FiletypeUnknown: return "" case FiletypeObject: return ".o" - case FiletypeLibrary: return ".so" + case FiletypeLibrary: return targetSoExt(target) case FiletypeAssembly: return ".s" case FiletypeIR: return ".ll" default: return "" @@ -62,6 +60,14 @@ func (filetype Filetype) Ext () string { } // Extend adds the extension of the filetype onto the specified path. -func (filetype Filetype) Extend (path string) string { - return path + filetype.Ext() +func (filetype Filetype) Extend (target generator.Target, path string) string { + return path + filetype.Ext(target) +} + +func targetSoExt (target generator.Target) string { + // TODO: more research is required here + switch target.OS { + case "windows": return ".dll" + default: return ".so" + } } From 2caed5b4aed0199b8432cd27be484ac8d12dafa3 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 12:52:20 -0400 Subject: [PATCH 08/21] Add an executable filetype --- compiler/filetype.go | 57 +++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/compiler/filetype.go b/compiler/filetype.go index 040ea7b..5348f4e 100644 --- a/compiler/filetype.go +++ b/compiler/filetype.go @@ -10,51 +10,56 @@ type Filetype int; const ( FiletypeLibrary FiletypeAssembly FiletypeIR + FiletypeExecutable ) // FiletypeFromString returns a filetype based on the given name. func FiletypeFromString (ext string) (Filetype, bool) { switch ext { - case "": return FiletypeUnknown, true - case "obj": return FiletypeObject, true - case "lib": return FiletypeLibrary, true - case "asm": return FiletypeAssembly, true - case "ir": return FiletypeIR, true - default: return FiletypeUnknown, false + case "": return FiletypeUnknown, true + case "obj": return FiletypeObject, true + case "lib": return FiletypeLibrary, true + case "asm": return FiletypeAssembly, true + case "ir": return FiletypeIR, true + case "exe": return FiletypeExecutable, true + default: return FiletypeUnknown, false } } // FiletypeFromExt returns a filetype based on the given filename extension. func FiletypeFromExt (target generator.Target, ext string) (Filetype, bool) { switch ext { - case ".o": return FiletypeObject, true - case targetSoExt(target): return FiletypeLibrary, true - case ".s": return FiletypeAssembly, true - case ".ll": return FiletypeIR, true - default: return FiletypeUnknown, false + case ".o": return FiletypeObject, true + case targetSoExt(target): return FiletypeLibrary, true + case ".s": return FiletypeAssembly, true + case ".ll": return FiletypeIR, true + case targetExeExt(target): return FiletypeExecutable, true + default: return FiletypeUnknown, false } } // String returns a string representation of the filetype. func (filetype Filetype) String () string { switch filetype { - case FiletypeUnknown: return "" - case FiletypeObject: return "obj" - case FiletypeLibrary: return "lib" - case FiletypeAssembly: return "asm" - case FiletypeIR: return "ir" - default: return fmt.Sprintf("Filetype(%d)", filetype) + case FiletypeUnknown: return "" + case FiletypeObject: return "obj" + case FiletypeLibrary: return "lib" + case FiletypeAssembly: return "asm" + case FiletypeIR: return "ir" + case FiletypeExecutable: return "exe" + default: return fmt.Sprintf("Filetype(%d)", filetype) } } // Ext returns the standard filename extension of the filetype. func (filetype Filetype) Ext (target generator.Target) string { switch filetype { - case FiletypeUnknown: return "" - case FiletypeObject: return ".o" - case FiletypeLibrary: return targetSoExt(target) - case FiletypeAssembly: return ".s" - case FiletypeIR: return ".ll" + case FiletypeUnknown: return "" + case FiletypeObject: return ".o" + case FiletypeLibrary: return targetSoExt(target) + case FiletypeAssembly: return ".s" + case FiletypeIR: return ".ll" + case FiletypeExecutable: return targetExeExt(target) default: return "" } } @@ -71,3 +76,11 @@ func targetSoExt (target generator.Target) string { default: return ".so" } } + +func targetExeExt (target generator.Target) string { + // TODO: more research is required here + switch target.OS { + case "windows": return ".exe" + default: return "" + } +} From 76615df4d4ba306351a05c4cc2df8fc4b7c60223 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 13:13:22 -0400 Subject: [PATCH 09/21] Add option to specify module search paths manually --- cmd/fsplc/main.go | 10 +++++++++- compiler/resolver.go | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/fsplc/main.go b/cmd/fsplc/main.go index 700e83a..e91ddfb 100644 --- a/cmd/fsplc/main.go +++ b/cmd/fsplc/main.go @@ -47,6 +47,13 @@ func main () { 'O', "optimization", "Optimization level (0-3)", "0", cli.NewValSet("0", "1", "2", "3")) + includePath := cli.NewInputFlag ( + 'u', "unit-directory", + "Extra directory(s) to search for units in", "", + cli.ValString) + includePath.Found = func (application *cli.Cli, value string) { + comp.Resolver.AddPathFront(value) + } application := cli.New ( "Compile FSPL source files", @@ -55,7 +62,8 @@ func main () { quiet, filetype, output, - optimization) + optimization, + includePath) application.Syntax = "[OPTION]... ADDRESS" application.ParseOrExit(os.Args) diff --git a/compiler/resolver.go b/compiler/resolver.go index 39f313a..4dfb0c0 100644 --- a/compiler/resolver.go +++ b/compiler/resolver.go @@ -48,7 +48,7 @@ func (resolver *Resolver) AddPathFront (path ...string) { // - If the address starts with '/', it is treated as an absolute path from // the fs root // - Else, the address is searched for in the resolver's paths -func (resolver Resolver) Resolve (context string, address entity.Address) (string, error) { +func (resolver *Resolver) Resolve (context string, address entity.Address) (string, error) { strAddr := string(address) switch { case strings.HasPrefix(strAddr, "."): @@ -71,7 +71,7 @@ func (resolver Resolver) Resolve (context string, address entity.Address) (strin } } -func (resolver Resolver) search (needle string) (string, error) { +func (resolver *Resolver) search (needle string) (string, error) { for _, dirPath := range resolver.Path { // attempt to open the file as dir. // if we can't open the dir, just skip it, because it is @@ -98,7 +98,7 @@ func (resolver Resolver) search (needle string) (string, error) { // ResolveCwd resolves the address within the context of the current working // directory. -func (resolver Resolver) ResolveCwd (address entity.Address) (string, error) { +func (resolver *Resolver) ResolveCwd (address entity.Address) (string, error) { wd, err := os.Getwd() if err != nil { return "", err } return resolver.Resolve(wd, address) From 43ed297a120724fa93486fab26a4c33fb488a821 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 13:38:13 -0400 Subject: [PATCH 10/21] Add Windows paths to design/units.md --- design/units.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/design/units.md b/design/units.md index ac5f56a..9556628 100644 --- a/design/units.md +++ b/design/units.md @@ -38,7 +38,15 @@ in order of preference: - `/usr/src/fspl` - `/usr/include/fspl` -Files in `include` directories should *not* include program code, and should +On windows, these are used instead: +- `%LOCALAPPDATA%\\fspl\\src` +- `%LOCALAPPDATA%\\fspl\\include` +- `%ALLUSERSPROFILE%\\fspl\\src` +- `%ALLUSERSPROFILE%\\fspl\\include` +- `%ProgramFiles%\\fspl\\src` +- `%ProgramFiles%\\fspl\\include` + +Files in `include` directories *should not* include program code, and should only define types and external functions and methods, similar to header files in C. They may have a corresponding shared object file that programs can dynamically link against. From e574b248e56408d53709da2d12fd4e6730c4b50f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 27 Mar 2024 13:39:38 -0400 Subject: [PATCH 11/21] Total windows path separator death --- design/units.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/design/units.md b/design/units.md index 9556628..1dfc30d 100644 --- a/design/units.md +++ b/design/units.md @@ -39,12 +39,12 @@ in order of preference: - `/usr/include/fspl` On windows, these are used instead: -- `%LOCALAPPDATA%\\fspl\\src` -- `%LOCALAPPDATA%\\fspl\\include` -- `%ALLUSERSPROFILE%\\fspl\\src` -- `%ALLUSERSPROFILE%\\fspl\\include` -- `%ProgramFiles%\\fspl\\src` -- `%ProgramFiles%\\fspl\\include` +- `%LOCALAPPDATA%\fspl\src` +- `%LOCALAPPDATA%\fspl\include` +- `%ALLUSERSPROFILE%\fspl\src` +- `%ALLUSERSPROFILE%\fspl\include` +- `%ProgramFiles%\fspl\src` +- `%ProgramFiles%\fspl\include` Files in `include` directories *should not* include program code, and should only define types and external functions and methods, similar to header files in From eccb2e9a5b5871a8d7739b695fec2baa47671bf0 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 13:11:27 -0400 Subject: [PATCH 12/21] Compiler uses runtime.GOOS instead of conditional compilation --- compiler/native/native.go | 47 ++++++++++++++++++++++++++++++- compiler/native/native_unix.go | 19 ------------- compiler/native/native_windows.go | 30 -------------------- 3 files changed, 46 insertions(+), 50 deletions(-) delete mode 100644 compiler/native/native_unix.go delete mode 100644 compiler/native/native_windows.go diff --git a/compiler/native/native.go b/compiler/native/native.go index f4ab7d2..dbc712e 100644 --- a/compiler/native/native.go +++ b/compiler/native/native.go @@ -1,10 +1,55 @@ // Package native provides OS native parameters for the compilation process. package native +import "os" +import "errors" +import "runtime" +import "path/filepath" import "git.tebibyte.media/fspl/fspl/compiler" // NativeResolver returns a resolver that resolves paths native to the operating // system. func NativeResolver () (*compiler.Resolver, error) { - return nativeResolver() + switch runtime.GOOS { + case "windows": return windowsNativeResolver() + default: return unixNativeResolver() + } +} + +func unixNativeResolver () (*compiler.Resolver, error) { + resolver := compiler.NewResolver ( + "/usr/local/src/fspl", + "/usr/src/fspl", + "/usr/local/incude/fspl", + "/usr/include/fspl") + homeDir, err := os.UserHomeDir() + if err != nil { return nil, err } + resolver.AddPathFront ( + filepath.Join(homeDir, ".local/src/fspl"), + filepath.Join(homeDir, ".local/include/fspl")) + return resolver, nil +} + +func windowsNativeResolver () (*compiler.Resolver, error) { + localAppData := os.Getenv("LOCALAPPDATA") + if localAppData == "" { + return nil, errors.New("could not get %LOCALAPPDATA%") + } + allUsersProfile := os.Getenv("ALLUSERSPROFILE") + if allUsersProfile == "" { + return nil, errors.New("could not get %ALLUSERSPROFILE%") + } + programFiles := os.Getenv("ProgramFiles") + if programFiles == "" { + return nil, errors.New("could not get %ProgramFiles%") + } + + resolver := compiler.NewResolver ( + filepath.Join(localAppData, "fspl\\src"), + filepath.Join(localAppData, "fspl\\include"), + filepath.Join(allUsersProfile, "fspl\\src"), + filepath.Join(allUsersProfile, "fspl\\include"), + filepath.Join(programFiles, "fspl\\src"), + filepath.Join(programFiles, "fspl\\include")) + return resolver, nil } diff --git a/compiler/native/native_unix.go b/compiler/native/native_unix.go deleted file mode 100644 index 68d2fc7..0000000 --- a/compiler/native/native_unix.go +++ /dev/null @@ -1,19 +0,0 @@ -package native - -import "os" -import "path/filepath" -import "git.tebibyte.media/fspl/fspl/compiler" - -func nativeResolver () (*compiler.Resolver, error) { - resolver := compiler.NewResolver ( - "/usr/local/src/fspl", - "/usr/src/fspl", - "/usr/local/incude/fspl", - "/usr/include/fspl") - homeDir, err := os.UserHomeDir() - if err != nil { return nil, err } - resolver.AddPathFront ( - filepath.Join(homeDir, ".local/src/fspl"), - filepath.Join(homeDir, ".local/include/fspl")) - return resolver, nil -} diff --git a/compiler/native/native_windows.go b/compiler/native/native_windows.go deleted file mode 100644 index 31bf129..0000000 --- a/compiler/native/native_windows.go +++ /dev/null @@ -1,30 +0,0 @@ -package native - -import "os" -import "errors" -import "path/filepath" -import "git.tebibyte.media/fspl/fspl/compiler" - -func nativeResolver () (*compiler.Resolver, error) { - localAppData := os.Getenv("LOCALAPPDATA") - if localAppData == "" { - return nil, errors.New("could not get %LOCALAPPDATA%") - } - allUsersProfile := os.Getenv("ALLUSERSPROFILE") - if allUsersProfile == "" { - return nil, errors.New("could not get %ALLUSERSPROFILE%") - } - programFiles := os.Getenv("ProgramFiles") - if programFiles == "" { - return nil, errors.New("could not get %ProgramFiles%") - } - - resolver := compiler.NewResolver ( - filepath.Join(localAppData, "fspl\\src"), - filepath.Join(localAppData, "fspl\\include"), - filepath.Join(allUsersProfile, "fspl\\src"), - filepath.Join(allUsersProfile, "fspl\\include"), - filepath.Join(programFiles, "fspl\\src"), - filepath.Join(programFiles, "fspl\\include")) - return resolver, nil -} From e805060370a98f197f419b41248f679c24aec995 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 16:02:56 -0400 Subject: [PATCH 13/21] Generator native uses GOARCH and GOOS instead of cond. compilation --- generator/native/native.go | 48 ++++++++++++++++++++++++-- generator/native/native_386.go | 10 ------ generator/native/native_amd64.go | 10 ------ generator/native/native_amd64p32.go | 11 ------ generator/native/native_arm.go | 10 ------ generator/native/native_arm64.go | 10 ------ generator/native/native_arm64be.go | 10 ------ generator/native/native_armbe.go | 10 ------ generator/native/native_mips.go | 10 ------ generator/native/native_mips64.go | 10 ------ generator/native/native_mips64le.go | 10 ------ generator/native/native_mipsle.go | 10 ------ generator/native/native_ppc.go | 10 ------ generator/native/native_ppc64.go | 10 ------ generator/native/native_ppc64le.go | 10 ------ generator/native/native_riscv.go | 10 ------ generator/native/native_sparc.go | 10 ------ generator/native/native_sparc64.go | 10 ------ generator/native/native_wasm.go | 12 ------- generator/native/osnative_aix.go | 5 --- generator/native/osnative_android.go | 5 --- generator/native/osnative_darwin.go | 5 --- generator/native/osnative_dragonfly.go | 5 --- generator/native/osnative_freebsd.go | 5 --- generator/native/osnative_illumos.go | 6 ---- generator/native/osnative_ios.go | 5 --- generator/native/osnative_js.go | 5 --- generator/native/osnative_linux.go | 5 --- generator/native/osnative_nacl.go | 5 --- generator/native/osnative_netbsd.go | 5 --- generator/native/osnative_openbsd.go | 5 --- generator/native/osnative_plan9.go | 6 ---- generator/native/osnative_solaris.go | 5 --- generator/native/osnative_windows.go | 5 --- 34 files changed, 46 insertions(+), 262 deletions(-) delete mode 100644 generator/native/native_386.go delete mode 100644 generator/native/native_amd64.go delete mode 100644 generator/native/native_amd64p32.go delete mode 100644 generator/native/native_arm.go delete mode 100644 generator/native/native_arm64.go delete mode 100644 generator/native/native_arm64be.go delete mode 100644 generator/native/native_armbe.go delete mode 100644 generator/native/native_mips.go delete mode 100644 generator/native/native_mips64.go delete mode 100644 generator/native/native_mips64le.go delete mode 100644 generator/native/native_mipsle.go delete mode 100644 generator/native/native_ppc.go delete mode 100644 generator/native/native_ppc64.go delete mode 100644 generator/native/native_ppc64le.go delete mode 100644 generator/native/native_riscv.go delete mode 100644 generator/native/native_sparc.go delete mode 100644 generator/native/native_sparc64.go delete mode 100644 generator/native/native_wasm.go delete mode 100644 generator/native/osnative_aix.go delete mode 100644 generator/native/osnative_android.go delete mode 100644 generator/native/osnative_darwin.go delete mode 100644 generator/native/osnative_dragonfly.go delete mode 100644 generator/native/osnative_freebsd.go delete mode 100644 generator/native/osnative_illumos.go delete mode 100644 generator/native/osnative_ios.go delete mode 100644 generator/native/osnative_js.go delete mode 100644 generator/native/osnative_linux.go delete mode 100644 generator/native/osnative_nacl.go delete mode 100644 generator/native/osnative_netbsd.go delete mode 100644 generator/native/osnative_openbsd.go delete mode 100644 generator/native/osnative_plan9.go delete mode 100644 generator/native/osnative_solaris.go delete mode 100644 generator/native/osnative_windows.go diff --git a/generator/native/native.go b/generator/native/native.go index 7f3a7ed..9b71467 100644 --- a/generator/native/native.go +++ b/generator/native/native.go @@ -2,6 +2,7 @@ // This is accomplished using several conditionally compiled source files. package native +import "runtime" import "git.tebibyte.media/fspl/fspl/generator" // LLVM supported operating systems (note: capitalization is wrong) @@ -11,7 +12,50 @@ import "git.tebibyte.media/fspl/fspl/generator" // NativeTarget returns a target describing the current system. func NativeTarget () generator.Target { - target := nativeArch() - target.OS = nativeOS() + target := generator.Target { } + switch runtime.GOARCH { + case "386": target.WordSize = 32; target.Arch = "i386" + case "amd64": target.WordSize = 64; target.Arch = "x86_64" + case "amd64p32": target.WordSize = 32; target.Arch = "x86" // might be inaccurate + case "arm": target.WordSize = 32; target.Arch = "arm" + case "arm64": target.WordSize = 64; target.Arch = "aarch64" + case "arm64be": target.WordSize = 64; target.Arch = "aarch64_be" + case "armbe": target.WordSize = 32; target.Arch = "armeb" + case "loong": target.WordSize = 32; target.Arch = "loongarch32" + case "loong64": target.WordSize = 64; target.Arch = "loongarch64" + case "mips": target.WordSize = 32; target.Arch = "mips" + case "mips64": target.WordSize = 64; target.Arch = "mips64" + case "mips64le": target.WordSize = 64; target.Arch = "mips64el" + case "mipsle": target.WordSize = 32; target.Arch = "mipsel" + case "ppc": target.WordSize = 32; target.Arch = "ppc32" + case "ppc64": target.WordSize = 64; target.Arch = "ppc64" + case "ppc64le": target.WordSize = 64; target.Arch = "ppc64le" + case "riscv": target.WordSize = 32; target.Arch = "riscv32" + case "riscv64": target.WordSize = 64; target.Arch = "riscv64" + case "sparc": target.WordSize = 32; target.Arch = "sparc" + case "sparc64": target.WordSize = 64; target.Arch = "sparcv9" + case "wasm": target.WordSize = 64; target.Arch = "wasm64" + default: target.WordSize = 64; target.Arch = "unknown" + } + switch runtime.GOOS { + case "aix": target.OS = "aix" + case "android": target.OS = "linux" + case "darwin": target.OS = "darwin" + case "dragonfly": target.OS = "dragonfly" + case "freebsd": target.OS = "freebsd" + case "illumos": target.OS = "illumos" + case "ios": target.OS = "ios" + case "js": target.OS = "unknown" + case "linux": target.OS = "linux" + case "nacl": target.OS = "nacl" + case "netbsd": target.OS = "netbsd" + case "openbsd": target.OS = "openbsd" + case "plan9": target.OS = "plan9" // does not seem to be supported by LLVM at the moment + case "solaris": target.OS = "solaris" + case "wasip1": target.OS = "wasi" + case "windows": target.OS = "win32" + default: target.OS = "unknown" + } + return target } diff --git a/generator/native/native_386.go b/generator/native/native_386.go deleted file mode 100644 index b43ea15..0000000 --- a/generator/native/native_386.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "i386", - } -} diff --git a/generator/native/native_amd64.go b/generator/native/native_amd64.go deleted file mode 100644 index a25359c..0000000 --- a/generator/native/native_amd64.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "x86_64", - } -} diff --git a/generator/native/native_amd64p32.go b/generator/native/native_amd64p32.go deleted file mode 100644 index 82fff57..0000000 --- a/generator/native/native_amd64p32.go +++ /dev/null @@ -1,11 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - // this may not be accurate, can't find info online about amd64p32 - return generator.Target { - WordSize: 32, - Arch: "x86", - } -} diff --git a/generator/native/native_arm.go b/generator/native/native_arm.go deleted file mode 100644 index 6734b84..0000000 --- a/generator/native/native_arm.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "arm", - } -} diff --git a/generator/native/native_arm64.go b/generator/native/native_arm64.go deleted file mode 100644 index fb3bbc6..0000000 --- a/generator/native/native_arm64.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "aarch64", - } -} diff --git a/generator/native/native_arm64be.go b/generator/native/native_arm64be.go deleted file mode 100644 index a01ff49..0000000 --- a/generator/native/native_arm64be.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "aarch64_be", - } -} diff --git a/generator/native/native_armbe.go b/generator/native/native_armbe.go deleted file mode 100644 index b65ecd8..0000000 --- a/generator/native/native_armbe.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "armeb", - } -} diff --git a/generator/native/native_mips.go b/generator/native/native_mips.go deleted file mode 100644 index 4f04d48..0000000 --- a/generator/native/native_mips.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "mips", - } -} diff --git a/generator/native/native_mips64.go b/generator/native/native_mips64.go deleted file mode 100644 index 51b8728..0000000 --- a/generator/native/native_mips64.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "mips64", - } -} diff --git a/generator/native/native_mips64le.go b/generator/native/native_mips64le.go deleted file mode 100644 index 8388606..0000000 --- a/generator/native/native_mips64le.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "mips64el", - } -} diff --git a/generator/native/native_mipsle.go b/generator/native/native_mipsle.go deleted file mode 100644 index 8e0eb6a..0000000 --- a/generator/native/native_mipsle.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "mipsel", - } -} diff --git a/generator/native/native_ppc.go b/generator/native/native_ppc.go deleted file mode 100644 index 2b7406f..0000000 --- a/generator/native/native_ppc.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "ppc32", - } -} diff --git a/generator/native/native_ppc64.go b/generator/native/native_ppc64.go deleted file mode 100644 index 05b52fe..0000000 --- a/generator/native/native_ppc64.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "ppc64", - } -} diff --git a/generator/native/native_ppc64le.go b/generator/native/native_ppc64le.go deleted file mode 100644 index 7ebcf6f..0000000 --- a/generator/native/native_ppc64le.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "ppc64le", - } -} diff --git a/generator/native/native_riscv.go b/generator/native/native_riscv.go deleted file mode 100644 index f2b97e3..0000000 --- a/generator/native/native_riscv.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "riscv32", - } -} diff --git a/generator/native/native_sparc.go b/generator/native/native_sparc.go deleted file mode 100644 index 3fde94f..0000000 --- a/generator/native/native_sparc.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 32, - Arch: "sparc", - } -} diff --git a/generator/native/native_sparc64.go b/generator/native/native_sparc64.go deleted file mode 100644 index 65be0f6..0000000 --- a/generator/native/native_sparc64.go +++ /dev/null @@ -1,10 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - return generator.Target { - WordSize: 64, - Arch: "sparcv9", - } -} diff --git a/generator/native/native_wasm.go b/generator/native/native_wasm.go deleted file mode 100644 index a82c00e..0000000 --- a/generator/native/native_wasm.go +++ /dev/null @@ -1,12 +0,0 @@ -package native - -import "git.tebibyte.media/fspl/fspl/generator" - -func nativeArch () generator.Target { - // FIXME: golang doesn't discern between 32/64 bit wasm so we assume 64 - // bit here - return generator.Target { - WordSize: 64, - Arch: "wasm64", - } -} diff --git a/generator/native/osnative_aix.go b/generator/native/osnative_aix.go deleted file mode 100644 index 6cf1bf8..0000000 --- a/generator/native/osnative_aix.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "aix" -} diff --git a/generator/native/osnative_android.go b/generator/native/osnative_android.go deleted file mode 100644 index 9952e97..0000000 --- a/generator/native/osnative_android.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "linux" -} diff --git a/generator/native/osnative_darwin.go b/generator/native/osnative_darwin.go deleted file mode 100644 index 8abde5c..0000000 --- a/generator/native/osnative_darwin.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "darwin" -} diff --git a/generator/native/osnative_dragonfly.go b/generator/native/osnative_dragonfly.go deleted file mode 100644 index ee066e3..0000000 --- a/generator/native/osnative_dragonfly.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "dragonfly" -} diff --git a/generator/native/osnative_freebsd.go b/generator/native/osnative_freebsd.go deleted file mode 100644 index fa277c1..0000000 --- a/generator/native/osnative_freebsd.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "freebsd" -} diff --git a/generator/native/osnative_illumos.go b/generator/native/osnative_illumos.go deleted file mode 100644 index f1a80ed..0000000 --- a/generator/native/osnative_illumos.go +++ /dev/null @@ -1,6 +0,0 @@ -package native - -func nativeOS () string { - // does not seem to be supported by LLVM at the moment - return "illumos" -} diff --git a/generator/native/osnative_ios.go b/generator/native/osnative_ios.go deleted file mode 100644 index e77fd31..0000000 --- a/generator/native/osnative_ios.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "ios" -} diff --git a/generator/native/osnative_js.go b/generator/native/osnative_js.go deleted file mode 100644 index 8300389..0000000 --- a/generator/native/osnative_js.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "unknown" -} diff --git a/generator/native/osnative_linux.go b/generator/native/osnative_linux.go deleted file mode 100644 index 9952e97..0000000 --- a/generator/native/osnative_linux.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "linux" -} diff --git a/generator/native/osnative_nacl.go b/generator/native/osnative_nacl.go deleted file mode 100644 index a81151c..0000000 --- a/generator/native/osnative_nacl.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "nacl" -} diff --git a/generator/native/osnative_netbsd.go b/generator/native/osnative_netbsd.go deleted file mode 100644 index 498e1eb..0000000 --- a/generator/native/osnative_netbsd.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "netbsd" -} diff --git a/generator/native/osnative_openbsd.go b/generator/native/osnative_openbsd.go deleted file mode 100644 index 7311e48..0000000 --- a/generator/native/osnative_openbsd.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "openbsd" -} diff --git a/generator/native/osnative_plan9.go b/generator/native/osnative_plan9.go deleted file mode 100644 index 7f7f1f0..0000000 --- a/generator/native/osnative_plan9.go +++ /dev/null @@ -1,6 +0,0 @@ -package native - -func nativeOS () string { - // does not seem to be supported by LLVM at the moment - return "plan9" -} diff --git a/generator/native/osnative_solaris.go b/generator/native/osnative_solaris.go deleted file mode 100644 index 588c0c6..0000000 --- a/generator/native/osnative_solaris.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "solaris" -} diff --git a/generator/native/osnative_windows.go b/generator/native/osnative_windows.go deleted file mode 100644 index 3f14020..0000000 --- a/generator/native/osnative_windows.go +++ /dev/null @@ -1,5 +0,0 @@ -package native - -func nativeOS () string { - return "win32" -} From 4e6103418cb3e71d4ba1cdaa0e988fcc20aacd81 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 16:52:31 -0400 Subject: [PATCH 14/21] Compiler normalizes paths before passing them to an fs --- compiler/resolver.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/resolver.go b/compiler/resolver.go index 4dfb0c0..1969150 100644 --- a/compiler/resolver.go +++ b/compiler/resolver.go @@ -106,9 +106,8 @@ func (resolver *Resolver) ResolveCwd (address entity.Address) (string, error) { // openAbsolute exists because fs.FS implementations do not understand absolute // paths, which the FSPL compiler runs on. It converts an absolute path to a -// path relative to "/" and opens the file. +// slash path relative to "/" and opens the file. func openAbsolute (filesystem fs.FS, path string) (fs.File, error) { - path, err := filepath.Rel("/", path) - if err != nil { return nil, err } + path = strings.TrimPrefix(filepath.ToSlash(path), "/") return filesystem.Open(path) } From 1c61235b6376eaedd7af3bd775a218105f511c47 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 17:16:50 -0400 Subject: [PATCH 15/21] openAbsolute should strip out the volume name on Windows --- compiler/resolver.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/resolver.go b/compiler/resolver.go index 1969150..30df7bc 100644 --- a/compiler/resolver.go +++ b/compiler/resolver.go @@ -108,6 +108,13 @@ func (resolver *Resolver) ResolveCwd (address entity.Address) (string, error) { // paths, which the FSPL compiler runs on. It converts an absolute path to a // slash path relative to "/" and opens the file. func openAbsolute (filesystem fs.FS, path string) (fs.File, error) { - path = strings.TrimPrefix(filepath.ToSlash(path), "/") + rootName := "/" + // FIXME: this does not distinguish between volumes on windows. this is + // a limitation of os.DirFS, we might want our own DirFS implementation + // that has volumes as subdirs of /. + if volumeName := filepath.VolumeName(path); volumeName != "" { + rootName = volumeName + } + path = strings.TrimPrefix(filepath.ToSlash(path), rootName) return filesystem.Open(path) } From 15e418d8c14a13fd4f6254a5dda317c2a5f89440 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 18:12:37 -0400 Subject: [PATCH 16/21] Add custom fs implementation --- compiler/fs.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ compiler/resolver.go | 13 +++------- 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 compiler/fs.go diff --git a/compiler/fs.go b/compiler/fs.go new file mode 100644 index 0000000..b0e05f6 --- /dev/null +++ b/compiler/fs.go @@ -0,0 +1,57 @@ +package compiler + +import "os" +import "io/fs" +import "runtime" +import "strings" +import "path/filepath" + +// OsFS returns an fs.FS that represents the operating system's filesystem. In +// Windows, volume names (A:, B:, C:, etc.) are treated as subdirectories within +// the root. +type OsFS struct { } + +// win +// C:/Something -> C:\Something +// "" -> "" + +// unix +// C:/Something -> /C:/Something +// "" -> / + +func (this OsFS) Open (name string) (fs.File, error) { + fullname, err := this.nativize(name) + if err != nil { + return nil, this.err("open", name, err) + } + file, err := os.Open(fullname) + if err != nil { + return nil, this.err("open", name, err.(*os.PathError).Err) + } + return file, nil +} + +func (this OsFS) nativize (name string) (string, error) { + if !fs.ValidPath(name) { + return "", fs.ErrInvalid + } + if filepath.Separator != '/' { + if strings.Contains(name, string(filepath.Separator)) { + return "", fs.ErrInvalid + } + } + if runtime.GOOS != "windows" { + // TODO is this correct for all non-windows systems? + name = "/" + name + } + name = filepath.FromSlash(name) + return name, nil +} + +func (this OsFS) err (op, path string, err error) error { + return &fs.PathError { + Op: "open", + Path: path, + Err: err, + } +} diff --git a/compiler/resolver.go b/compiler/resolver.go index 30df7bc..9a0ebdc 100644 --- a/compiler/resolver.go +++ b/compiler/resolver.go @@ -20,10 +20,10 @@ type Resolver struct { Path []string } -// NewResolver creates a new resolver with os.DirFS("/"). +// NewResolver creates a new resolver with OsFS. func NewResolver (path ...string) *Resolver { return &Resolver { - FS: os.DirFS("/"), + FS: OsFS { }, Path: path, } } @@ -108,13 +108,6 @@ func (resolver *Resolver) ResolveCwd (address entity.Address) (string, error) { // paths, which the FSPL compiler runs on. It converts an absolute path to a // slash path relative to "/" and opens the file. func openAbsolute (filesystem fs.FS, path string) (fs.File, error) { - rootName := "/" - // FIXME: this does not distinguish between volumes on windows. this is - // a limitation of os.DirFS, we might want our own DirFS implementation - // that has volumes as subdirs of /. - if volumeName := filepath.VolumeName(path); volumeName != "" { - rootName = volumeName - } - path = strings.TrimPrefix(filepath.ToSlash(path), rootName) + path = strings.TrimPrefix(filepath.ToSlash(path), "/") return filesystem.Open(path) } From 00110039e2a4e70b99e4701a8b287a7b76ff2f1a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 28 Mar 2024 18:16:58 -0400 Subject: [PATCH 17/21] Compiler defaults to .o when output name isn't specified --- compiler/compile-unit.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/compile-unit.go b/compiler/compile-unit.go index 7433a8b..14304d5 100644 --- a/compiler/compile-unit.go +++ b/compiler/compile-unit.go @@ -34,12 +34,16 @@ func (this *Compiler) CompileUnit (address entity.Address) error { // if the format isn't specified, try to get it from the filename // extension of the input. if that isn't specified, default to .o if this.Filetype == FiletypeUnknown { - var ok bool - this.Filetype, ok = FiletypeFromExt ( - *this.Target, - filepath.Ext(this.Output)) - if !ok { + if this.Output == "" { this.Filetype = FiletypeObject + } else { + var ok bool + this.Filetype, ok = FiletypeFromExt ( + *this.Target, + filepath.Ext(this.Output)) + if !ok { + this.Filetype = FiletypeObject + } } } From b6b0a1e5928686e9cc3e498b027d9e831340246d Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 30 Mar 2024 22:26:07 -0400 Subject: [PATCH 18/21] parent 00110039e2a4e70b99e4701a8b287a7b76ff2f1a author Sasha Koshka 1711851967 -0400 committer Sasha Koshka 1711862974 -0400 Compiler tests now run on Windows Compiler tests show linker log Compiler tests show linker log --- compiler/common_test.go | 12 +++++++----- compiler/filetype.go | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/compiler/common_test.go b/compiler/common_test.go index 8c67c62..98cbbf0 100644 --- a/compiler/common_test.go +++ b/compiler/common_test.go @@ -80,7 +80,6 @@ func testUnit ( } comp.Writer = compOutputBuilder comp.Output = filepath.Join(temp, "output.o") - // compile to object file err := comp.CompileUnit(address) @@ -91,22 +90,25 @@ func testUnit ( outputCompilerLog() // link the object file into an executable - executablePath := filepath.Join(temp, "output") + linkOutputBuilder := new(strings.Builder) + executablePath := FiletypeExecutable.Extend(*comp.Target, filepath.Join(temp, "output")) linkCommand := exec.Command("clang", append ( clangArgs, comp.Output, + "-v", "-o", executablePath)...) - linkCommand.Stdout = compOutputBuilder - linkCommand.Stderr = compOutputBuilder + linkCommand.Stdout = linkOutputBuilder + linkCommand.Stderr = linkOutputBuilder test.Log("running link command: ", linkCommand) err = linkCommand.Run() + test.Log("LINKER LOG (main unit):\n" + linkOutputBuilder.String()) if err != nil { test.Fatal("error linking executable:", err) } // run the executable file (with timeout) and check its output - timeoutDuration := 10 * time.Millisecond + timeoutDuration := 1000 * time.Millisecond ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration) defer cancel() executableCommand := exec.CommandContext(ctx, executablePath, args...) diff --git a/compiler/filetype.go b/compiler/filetype.go index 5348f4e..b7c9e8a 100644 --- a/compiler/filetype.go +++ b/compiler/filetype.go @@ -72,15 +72,15 @@ func (filetype Filetype) Extend (target generator.Target, path string) string { func targetSoExt (target generator.Target) string { // TODO: more research is required here switch target.OS { - case "windows": return ".dll" - default: return ".so" + case "win32": return ".dll" + default: return ".so" } } func targetExeExt (target generator.Target) string { // TODO: more research is required here switch target.OS { - case "windows": return ".exe" - default: return "" + case "win32": return ".exe" + default: return "" } } From 810175b6c99f66f578df7dfaa925b27b57152737 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 30 Mar 2024 22:48:33 -0400 Subject: [PATCH 19/21] Object files have platform specific extensions --- compiler/filetype.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/filetype.go b/compiler/filetype.go index b7c9e8a..5f76580 100644 --- a/compiler/filetype.go +++ b/compiler/filetype.go @@ -29,7 +29,7 @@ func FiletypeFromString (ext string) (Filetype, bool) { // FiletypeFromExt returns a filetype based on the given filename extension. func FiletypeFromExt (target generator.Target, ext string) (Filetype, bool) { switch ext { - case ".o": return FiletypeObject, true + case targetObjExt(target): return FiletypeObject, true case targetSoExt(target): return FiletypeLibrary, true case ".s": return FiletypeAssembly, true case ".ll": return FiletypeIR, true @@ -55,7 +55,7 @@ func (filetype Filetype) String () string { func (filetype Filetype) Ext (target generator.Target) string { switch filetype { case FiletypeUnknown: return "" - case FiletypeObject: return ".o" + case FiletypeObject: return targetObjExt(target) case FiletypeLibrary: return targetSoExt(target) case FiletypeAssembly: return ".s" case FiletypeIR: return ".ll" @@ -77,6 +77,15 @@ func targetSoExt (target generator.Target) string { } } +func targetObjExt (target generator.Target) string { + // TODO: more research is required here + switch target.OS { + case "win32": return ".obj" + default: return ".o" + } +} + + func targetExeExt (target generator.Target) string { // TODO: more research is required here switch target.OS { From 93d4aea1b1813c5a01778196f8bde7c1d47545ba Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 30 Mar 2024 23:01:38 -0400 Subject: [PATCH 20/21] Compiler tests properly add extension to obj files --- compiler/common_test.go | 7 +++++-- compiler/compile-unit.go | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/common_test.go b/compiler/common_test.go index 98cbbf0..52fbe67 100644 --- a/compiler/common_test.go +++ b/compiler/common_test.go @@ -11,6 +11,7 @@ import "path/filepath" import "git.tebibyte.media/fspl/fspl/entity" import "git.tebibyte.media/fspl/fspl/errors" import "git.tebibyte.media/fspl/fspl/testcommon" +import "git.tebibyte.media/fspl/fspl/generator/native" //go:embed all:test-data/* var testData embed.FS @@ -27,6 +28,8 @@ func defaultCompiler () *Compiler { comp.Resolver.FS = testData comp.Filetype = FiletypeObject comp.Debug = true + nativeTarget := native.NativeTarget() + comp.Target = &nativeTarget return comp } @@ -50,7 +53,7 @@ func compileDependency ( if !ok { test.Fatal("could not generate nickname for", address) } - comp.Output = filepath.Join(temp, nickname) + comp.Output = filepath.Join(temp, FiletypeObject.Extend(*comp.Target, nickname)) // compile to object file err := comp.CompileUnit(address) @@ -79,7 +82,7 @@ func testUnit ( test.Log("COMPILER LOG (main unit):\n" + compOutputBuilder.String()) } comp.Writer = compOutputBuilder - comp.Output = filepath.Join(temp, "output.o") + comp.Output = filepath.Join(temp, FiletypeObject.Extend(*comp.Target, "output")) // compile to object file err := comp.CompileUnit(address) diff --git a/compiler/compile-unit.go b/compiler/compile-unit.go index 14304d5..9088199 100644 --- a/compiler/compile-unit.go +++ b/compiler/compile-unit.go @@ -34,6 +34,7 @@ func (this *Compiler) CompileUnit (address entity.Address) error { // if the format isn't specified, try to get it from the filename // extension of the input. if that isn't specified, default to .o if this.Filetype == FiletypeUnknown { + this.Debugln("filetype was not specified") if this.Output == "" { this.Filetype = FiletypeObject } else { From 88e10158ae6ff6a847acaa18ca2b1e0439021a91 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 31 Mar 2024 00:39:43 -0400 Subject: [PATCH 21/21] It is now possible for tests to pass on windows --- compiler/common_test.go | 2 +- compiler/compiler_test.go | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/compiler/common_test.go b/compiler/common_test.go index 52fbe67..b31de48 100644 --- a/compiler/common_test.go +++ b/compiler/common_test.go @@ -111,7 +111,7 @@ func testUnit ( } // run the executable file (with timeout) and check its output - timeoutDuration := 1000 * time.Millisecond + timeoutDuration := 4000 * time.Millisecond ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration) defer cancel() executableCommand := exec.CommandContext(ctx, executablePath, args...) diff --git a/compiler/compiler_test.go b/compiler/compiler_test.go index cfb15e3..19b7fcf 100644 --- a/compiler/compiler_test.go +++ b/compiler/compiler_test.go @@ -1,18 +1,26 @@ package compiler import "testing" +import "runtime" + +var nativeLineBreak = "\n" +func init () { + if runtime.GOOS == "windows" { + nativeLineBreak = "\r\n" + } +} func TestHelloWorld (test *testing.T) { testUnit (test, "/test-data/data/hello", nil, -"", "Hello, world!\n", +"", "Hello, world!" + nativeLineBreak, 0, )} func TestPuts (test *testing.T) { testUnit (test, "/test-data/data/puts", nil, -"", "hello\n", +"", "hello" + nativeLineBreak, 0, )} @@ -26,7 +34,7 @@ testUnit (test, func TestSystemInclude (test *testing.T) { testUnit (test, "/test-data/data/system-include", nil, -"", "Hello, /usr/include!\n", +"", "Hello, /usr/include!" + nativeLineBreak, 0, )} @@ -36,7 +44,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/system-src", dependencies, -"", "Hello, /usr/src!\n", +"", "Hello, /usr/src!" + nativeLineBreak, 0, )} @@ -68,7 +76,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/writer", dependencies, -"", "well hello their\n", +"", "well hello their" + nativeLineBreak, 0, )} @@ -85,7 +93,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/match-print", dependencies, -"", "F64\n", +"", "F64" + nativeLineBreak, 0, )} @@ -95,7 +103,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/match-default-print", dependencies, -"", "something else\n", +"", "something else" + nativeLineBreak, 0, )} @@ -112,7 +120,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/return-assign", dependencies, -"", "false\n", +"", "false" + nativeLineBreak, 0, )} @@ -143,7 +151,8 @@ dependencies := []string { } testUnit (test, "/test-data/data/for-string-array", dependencies, -"", "a\nb\nc\na\nb\nc\n", +"", "a" + nativeLineBreak + "b" + nativeLineBreak + "c" + nativeLineBreak + + "a" + nativeLineBreak + "b" + nativeLineBreak + "c" + nativeLineBreak, 0, )} @@ -153,7 +162,7 @@ dependencies := []string { } testUnit (test, "/test-data/data/for-string-array-once", dependencies, -"", "abc\n", +"", "abc" + nativeLineBreak, 0, )} @@ -163,6 +172,6 @@ dependencies := []string { } testUnit (test, "/test-data/data/for-break-branch", dependencies, -"", "iter\niter\n", +"", "iter" + nativeLineBreak + "iter" + nativeLineBreak, 0, )}