Simplified config package

This commit is contained in:
Eduard Urbach 2025-02-17 23:36:11 +01:00
parent b7685fd7ec
commit 3b76919fec
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 68 additions and 66 deletions

View File

@ -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

View File

@ -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()
}

View File

@ -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

View File

@ -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
}

12
src/config/const.go Normal file
View File

@ -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
)

25
src/config/findLibrary.go Normal file
View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
})
}

View File

@ -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)
})
}