Simplified config package
This commit is contained in:
parent
b7685fd7ec
commit
3b76919fec
@ -39,7 +39,7 @@ Commands:
|
|||||||
|
|
||||||
build [directory | file] build an executable from a file or directory
|
build [directory | file] build an executable from a file or directory
|
||||||
--arch [arch] cross-compile for another CPU architecture [x86|arm|riscv]
|
--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
|
--dry, -d skip writing the executable to disk
|
||||||
--os [os] cross-compile for another OS [linux|mac|windows]
|
--os [os] cross-compile for another OS [linux|mac|windows]
|
||||||
--statistics, -s show statistics
|
--statistics, -s show statistics
|
||||||
|
@ -28,18 +28,18 @@ func buildExecutable(args []string) (*build.Build, error) {
|
|||||||
|
|
||||||
for i := 0; i < len(args); i++ {
|
for i := 0; i < len(args); i++ {
|
||||||
switch args[i] {
|
switch args[i] {
|
||||||
case "-a", "--assembler":
|
case "-a", "--assembly":
|
||||||
config.Assembler = true
|
config.ShowAssembly = true
|
||||||
|
|
||||||
case "-d", "--dry":
|
case "-d", "--dry":
|
||||||
config.Dry = true
|
config.Dry = true
|
||||||
|
|
||||||
case "-s", "--statistics":
|
case "-s", "--statistics":
|
||||||
config.Statistics = true
|
config.ShowStatistics = true
|
||||||
|
|
||||||
case "-v", "--verbose":
|
case "-v", "--verbose":
|
||||||
config.Assembler = true
|
config.ShowAssembly = true
|
||||||
config.Statistics = true
|
config.ShowStatistics = true
|
||||||
|
|
||||||
case "--arch":
|
case "--arch":
|
||||||
i++
|
i++
|
||||||
@ -97,11 +97,11 @@ func start(b *build.Build) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Assembler {
|
if config.ShowAssembly {
|
||||||
result.PrintInstructions()
|
result.PrintInstructions()
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Statistics {
|
if config.ShowStatistics {
|
||||||
result.PrintStatistics()
|
result.PrintStatistics()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ Commands:
|
|||||||
|
|
||||||
build [directory | file] build an executable from a file or directory
|
build [directory | file] build an executable from a file or directory
|
||||||
--arch [arch] cross-compile for another CPU architecture [x86|arm|riscv]
|
--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
|
--dry, -d skip writing the executable to disk
|
||||||
--os [os] cross-compile for another OS [linux|mac|windows]
|
--os [os] cross-compile for another OS [linux|mac|windows]
|
||||||
--statistics, -s show statistics
|
--statistics, -s show statistics
|
||||||
|
@ -2,45 +2,21 @@ package config
|
|||||||
|
|
||||||
import "runtime"
|
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 (
|
var (
|
||||||
// Shows the assembly instructions at the end.
|
ConstantFold bool // Calculates the result of operations on constants at compile time.
|
||||||
Assembler bool
|
Dry bool // Skips writing the executable to disk.
|
||||||
|
ShowAssembly bool // Shows assembly instructions at the end.
|
||||||
// Shows statistics at the end.
|
ShowStatistics bool // Shows statistics at the end.
|
||||||
Statistics bool
|
TargetArch string // Target architecture.
|
||||||
|
TargetOS OS // Target platform.
|
||||||
// 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
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reset resets the configuration to its default values.
|
// Reset resets the configuration to its default values.
|
||||||
func Reset() {
|
func Reset() {
|
||||||
Assembler = false
|
ShowAssembly = false
|
||||||
Statistics = false
|
ShowStatistics = false
|
||||||
ConstantFold = true
|
|
||||||
Dry = false
|
Dry = false
|
||||||
TargetArch = runtime.GOARCH
|
TargetArch = runtime.GOARCH
|
||||||
TargetOS = Unknown
|
|
||||||
|
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "linux":
|
case "linux":
|
||||||
@ -49,5 +25,14 @@ func Reset() {
|
|||||||
TargetOS = Mac
|
TargetOS = Mac
|
||||||
case "windows":
|
case "windows":
|
||||||
TargetOS = 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
12
src/config/const.go
Normal 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
25
src/config/findLibrary.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,6 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
@ -45,22 +44,3 @@ func init() {
|
|||||||
findLibrary()
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,7 @@ package register
|
|||||||
import "git.akyoto.dev/cli/q/src/config"
|
import "git.akyoto.dev/cli/q/src/config"
|
||||||
|
|
||||||
func (f *Machine) postInstruction() {
|
func (f *Machine) postInstruction() {
|
||||||
if !config.Assembler {
|
if !config.ShowAssembly {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,12 +33,12 @@ func TestExamples(t *testing.T) {
|
|||||||
directory := filepath.Join("..", "examples", test.Name)
|
directory := filepath.Join("..", "examples", test.Name)
|
||||||
|
|
||||||
t.Run(test.Name+"/debug", func(t *testing.T) {
|
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)
|
run(t, directory, "debug", test.Input, test.Output, test.ExitCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run(test.Name+"/release", func(t *testing.T) {
|
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)
|
run(t, directory, "release", test.Input, test.Output, test.ExitCode)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,12 @@ func TestPrograms(t *testing.T) {
|
|||||||
file := filepath.Join("programs", test.Name+".q")
|
file := filepath.Join("programs", test.Name+".q")
|
||||||
|
|
||||||
t.Run(test.Name+"/debug", func(t *testing.T) {
|
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)
|
run(t, file, "debug", test.Input, test.Output, test.ExitCode)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run(test.Name+"/release", func(t *testing.T) {
|
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)
|
run(t, file, "release", test.Input, test.Output, test.ExitCode)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user