From 3b76919fec352be58a732c5bc25770a0ced2317e Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 17 Feb 2025 23:36:11 +0100 Subject: [PATCH] Simplified config package --- docs/readme.md | 2 +- src/cli/Build.go | 14 +++++----- src/cli/Help.go | 2 +- src/config/config.go | 49 ++++++++++++--------------------- src/config/const.go | 12 ++++++++ src/config/findLibrary.go | 25 +++++++++++++++++ src/config/init.go | 20 -------------- src/register/postInstruction.go | 2 +- tests/examples_test.go | 4 +-- tests/programs_test.go | 4 +-- 10 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 src/config/const.go create mode 100644 src/config/findLibrary.go diff --git a/docs/readme.md b/docs/readme.md index d144e65..4a48a13 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -39,7 +39,7 @@ Commands: build [directory | file] build an executable from a file or directory --arch [arch] cross-compile for another CPU architecture [x86|arm|riscv] - --assembler, -a show assembler instructions + --assembly, -a show assembly instructions --dry, -d skip writing the executable to disk --os [os] cross-compile for another OS [linux|mac|windows] --statistics, -s show statistics diff --git a/src/cli/Build.go b/src/cli/Build.go index bfb7e37..82daf25 100644 --- a/src/cli/Build.go +++ b/src/cli/Build.go @@ -28,18 +28,18 @@ func buildExecutable(args []string) (*build.Build, error) { for i := 0; i < len(args); i++ { switch args[i] { - case "-a", "--assembler": - config.Assembler = true + case "-a", "--assembly": + config.ShowAssembly = true case "-d", "--dry": config.Dry = true case "-s", "--statistics": - config.Statistics = true + config.ShowStatistics = true case "-v", "--verbose": - config.Assembler = true - config.Statistics = true + config.ShowAssembly = true + config.ShowStatistics = true case "--arch": i++ @@ -97,11 +97,11 @@ func start(b *build.Build) error { return err } - if config.Assembler { + if config.ShowAssembly { result.PrintInstructions() } - if config.Statistics { + if config.ShowStatistics { result.PrintStatistics() } diff --git a/src/cli/Help.go b/src/cli/Help.go index 7faf7dc..03dfd9c 100644 --- a/src/cli/Help.go +++ b/src/cli/Help.go @@ -15,7 +15,7 @@ Commands: build [directory | file] build an executable from a file or directory --arch [arch] cross-compile for another CPU architecture [x86|arm|riscv] - --assembler, -a show assembler instructions + --assembly, -a show assembly instructions --dry, -d skip writing the executable to disk --os [os] cross-compile for another OS [linux|mac|windows] --statistics, -s show statistics diff --git a/src/config/config.go b/src/config/config.go index 89644c8..b603eed 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -2,45 +2,21 @@ package config import "runtime" -const ( - // This is the absolute virtual minimum address we can load a program at, see `sysctl vm.mmap_min_addr`. - MinAddress = 0x10000 - - // The base address is the virtual address for our ELF file. - BaseAddress = 0x40 * MinAddress - - // Align is the alignment of the sections and it must be a multiple of the page size. - Align = 0x1000 -) - var ( - // Shows the assembly instructions at the end. - Assembler bool - - // Shows statistics at the end. - Statistics bool - - // Calculates the result of operations on constants at compile time. - ConstantFold bool - - // Skips writing the executable to disk. - Dry bool - - // Target architecture. - TargetArch string - - // Target platform. - TargetOS OS + ConstantFold bool // Calculates the result of operations on constants at compile time. + Dry bool // Skips writing the executable to disk. + ShowAssembly bool // Shows assembly instructions at the end. + ShowStatistics bool // Shows statistics at the end. + TargetArch string // Target architecture. + TargetOS OS // Target platform. ) // Reset resets the configuration to its default values. func Reset() { - Assembler = false - Statistics = false - ConstantFold = true + ShowAssembly = false + ShowStatistics = false Dry = false TargetArch = runtime.GOARCH - TargetOS = Unknown switch runtime.GOOS { case "linux": @@ -49,5 +25,14 @@ func Reset() { TargetOS = Mac case "windows": TargetOS = Windows + default: + TargetOS = Unknown } + + Optimize(true) +} + +// Optimize enables or disables all the optimizations at once. +func Optimize(enable bool) { + ConstantFold = enable } diff --git a/src/config/const.go b/src/config/const.go new file mode 100644 index 0000000..09d9946 --- /dev/null +++ b/src/config/const.go @@ -0,0 +1,12 @@ +package config + +const ( + // This is the absolute virtual minimum address we can load a program at, see `sysctl vm.mmap_min_addr`. + MinAddress = 0x10000 + + // The base address is the virtual address for our ELF file. + BaseAddress = 0x40 * MinAddress + + // Align is the alignment of the sections and it must be a multiple of the page size. + Align = 0x1000 +) diff --git a/src/config/findLibrary.go b/src/config/findLibrary.go new file mode 100644 index 0000000..f83de1b --- /dev/null +++ b/src/config/findLibrary.go @@ -0,0 +1,25 @@ +package config + +import ( + "os" + "path/filepath" +) + +func findLibrary() { + dir := WorkingDirectory + + for { + Library = filepath.Join(dir, "lib") + stat, err := os.Stat(Library) + + if !os.IsNotExist(err) && stat != nil && stat.IsDir() { + return + } + + if dir == "/" { + panic("standard library not found") + } + + dir = filepath.Dir(dir) + } +} diff --git a/src/config/init.go b/src/config/init.go index d1f9d75..52e87dc 100644 --- a/src/config/init.go +++ b/src/config/init.go @@ -2,7 +2,6 @@ package config import ( "os" - "path" "path/filepath" "runtime/debug" ) @@ -45,22 +44,3 @@ func init() { findLibrary() } } - -func findLibrary() { - dir := WorkingDirectory - - for { - Library = path.Join(dir, "lib") - stat, err := os.Stat(Library) - - if !os.IsNotExist(err) && stat != nil && stat.IsDir() { - return - } - - if dir == "/" { - panic("standard library not found") - } - - dir = filepath.Dir(dir) - } -} diff --git a/src/register/postInstruction.go b/src/register/postInstruction.go index 18f9120..fd55acb 100644 --- a/src/register/postInstruction.go +++ b/src/register/postInstruction.go @@ -3,7 +3,7 @@ package register import "git.akyoto.dev/cli/q/src/config" func (f *Machine) postInstruction() { - if !config.Assembler { + if !config.ShowAssembly { return } diff --git a/tests/examples_test.go b/tests/examples_test.go index 5096c62..9d788f6 100644 --- a/tests/examples_test.go +++ b/tests/examples_test.go @@ -33,12 +33,12 @@ func TestExamples(t *testing.T) { directory := filepath.Join("..", "examples", test.Name) t.Run(test.Name+"/debug", func(t *testing.T) { - config.ConstantFold = false + config.Optimize(false) run(t, directory, "debug", test.Input, test.Output, test.ExitCode) }) t.Run(test.Name+"/release", func(t *testing.T) { - config.ConstantFold = true + config.Optimize(true) run(t, directory, "release", test.Input, test.Output, test.ExitCode) }) } diff --git a/tests/programs_test.go b/tests/programs_test.go index 23ec235..2b3b55e 100644 --- a/tests/programs_test.go +++ b/tests/programs_test.go @@ -71,12 +71,12 @@ func TestPrograms(t *testing.T) { file := filepath.Join("programs", test.Name+".q") t.Run(test.Name+"/debug", func(t *testing.T) { - config.ConstantFold = false + config.Optimize(false) run(t, file, "debug", test.Input, test.Output, test.ExitCode) }) t.Run(test.Name+"/release", func(t *testing.T) { - config.ConstantFold = true + config.Optimize(true) run(t, file, "release", test.Input, test.Output, test.ExitCode) }) }