Added file scanner

This commit is contained in:
Eduard Urbach 2023-10-30 16:17:41 +01:00
parent 5fe83543fd
commit 8b19989372
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 178 additions and 71 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.akyoto.dev/cli/q
go 1.21
require git.akyoto.dev/go/assert v0.1.3

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
git.akyoto.dev/go/assert v0.1.3 h1:QwCUbmG4aZYsNk/OuRBz1zWVKmGlDUHhOnnDBfn8Qw8=
git.akyoto.dev/go/assert v0.1.3/go.mod h1:0GzMaM0eURuDwtGkJJkCsI7r2aUKr+5GmWNTFPgDocM=

View File

@ -7,6 +7,7 @@ import (
"git.akyoto.dev/cli/q/src/cli"
"git.akyoto.dev/cli/q/src/log"
"git.akyoto.dev/go/assert"
)
func TestMain(m *testing.M) {
@ -29,16 +30,13 @@ func TestCLI(t *testing.T) {
{[]string{"build", "examples/hello/hello.q"}, 1},
{[]string{"build", "examples/hello", "--invalid"}, 2},
{[]string{"build", "examples/hello", "--dry"}, 0},
{[]string{"build", "examples/hello", "--dry", "--verbose"}, 0},
}
for _, test := range tests {
t.Log(test.arguments)
exitCode := cli.Main(test.arguments)
if exitCode != test.expectedExitCode {
t.Errorf("exit code %d (expected %d)", exitCode, test.expectedExitCode)
t.FailNow()
}
assert.Equal(t, exitCode, test.expectedExitCode)
}
}

View File

@ -2,6 +2,3 @@ package asm
// Address represents a memory address.
type Address = uint32
// Index references an instruction by its position.
// type Index = uint32

View File

@ -12,20 +12,17 @@ import (
// Assembler contains a list of instructions.
type Assembler struct {
Instructions []Instruction
// labels map[string]Index
Verbose bool
}
// New creates a new assembler.
func New() *Assembler {
return &Assembler{
Instructions: make([]Instruction, 0, 8),
// labels: map[string]Index{},
}
}
// Finalize generates the final machine code.
func (a *Assembler) Finalize() ([]byte, []byte) {
func (a *Assembler) Finalize(verbose bool) ([]byte, []byte) {
code := make([]byte, 0, len(a.Instructions)*8)
data := make(Data, 0, 16)
pointers := []Pointer{}
@ -46,8 +43,10 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
case SYSCALL:
code = x64.Syscall(code)
}
}
if a.Verbose {
if verbose {
for _, x := range a.Instructions {
log.Info.Println(x.String())
}
}
@ -63,11 +62,6 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
return code, data
}
// AddLabel creates a new label at the current position.
// func (a *Assembler) AddLabel(name string) {
// a.labels[name] = Index(len(a.instructions))
// }
// MoveRegisterData moves a data section address into the given register.
func (a *Assembler) MoveRegisterData(reg register.ID, data []byte) {
a.Instructions = append(a.Instructions, Instruction{

41
src/asm/Assembler_test.go Normal file
View File

@ -0,0 +1,41 @@
package asm_test
import (
"testing"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/linux"
"git.akyoto.dev/cli/q/src/register"
"git.akyoto.dev/go/assert"
)
func TestHello(t *testing.T) {
a := asm.New()
hello := []byte("Hello\n")
a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1)
a.MoveRegisterData(register.Syscall2, hello)
a.MoveRegisterNumber(register.Syscall3, uint64(len(hello)))
a.Syscall()
a.MoveRegisterNumber(register.Syscall0, linux.Exit)
a.MoveRegisterNumber(register.Syscall1, 0)
a.Syscall()
code, data := a.Finalize(false)
assert.DeepEqual(t, code, []byte{
0xb8, 0x01, 0x00, 0x00, 0x00,
0xbf, 0x01, 0x00, 0x00, 0x00,
0xbe, 0xa2, 0x00, 0x40, 0x00,
0xba, 0x06, 0x00, 0x00, 0x00,
0x0f, 0x05,
0xb8, 0x3c, 0x00, 0x00, 0x00,
0xbf, 0x00, 0x00, 0x00, 0x00,
0x0f, 0x05,
})
assert.DeepEqual(t, data, hello)
}

View File

@ -4,10 +4,8 @@ import (
"bufio"
"os"
"path/filepath"
"strings"
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/directory"
"git.akyoto.dev/cli/q/src/elf"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/linux"
@ -30,42 +28,14 @@ func New(directory string) *Build {
}
}
var (
hello = []byte("Hello\n")
world = []byte("World\n")
)
// Run parses the input files and generates an executable file.
func (build *Build) Run() error {
// err := build.Compile()
code, data, err := build.Compile()
// if err != nil {
// return err
// }
a := asm.Assembler{
Instructions: make([]asm.Instruction, 0, 8),
Verbose: build.Verbose,
if err != nil {
return err
}
a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1)
a.MoveRegisterData(register.Syscall2, hello)
a.MoveRegisterNumber(register.Syscall3, uint64(len(hello)))
a.Syscall()
a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1)
a.MoveRegisterData(register.Syscall2, world)
a.MoveRegisterNumber(register.Syscall3, uint64(len(world)))
a.Syscall()
a.MoveRegisterNumber(register.Syscall0, linux.Exit)
a.MoveRegisterNumber(register.Syscall1, 0)
a.Syscall()
code, data := a.Finalize()
if !build.WriteExecutable {
return nil
}
@ -74,26 +44,54 @@ func (build *Build) Run() error {
}
// Compile compiles all the functions.
func (build *Build) Compile() error {
func (build *Build) Compile() ([]byte, []byte, error) {
stat, err := os.Stat(build.Directory)
if err != nil {
return err
return nil, nil, err
}
if !stat.IsDir() {
return &errors.InvalidDirectory{Path: build.Directory}
return nil, nil, &errors.InvalidDirectory{Path: build.Directory}
}
directory.Walk(build.Directory, func(file string) {
if !strings.HasSuffix(file, ".q") {
return
functions, errors := Scan(build.Directory)
for functions != nil || errors != nil {
select {
case err, ok := <-errors:
if !ok {
errors = nil
continue
}
log.Info.Println(err)
case function, ok := <-functions:
if !ok {
functions = nil
continue
}
log.Info.Println(function)
}
}
log.Info.Println(file)
})
a := asm.New()
return nil
hello := []byte("Hello\n")
a.MoveRegisterNumber(register.Syscall0, linux.Write)
a.MoveRegisterNumber(register.Syscall1, 1)
a.MoveRegisterData(register.Syscall2, hello)
a.MoveRegisterNumber(register.Syscall3, uint64(len(hello)))
a.Syscall()
a.MoveRegisterNumber(register.Syscall0, linux.Exit)
a.MoveRegisterNumber(register.Syscall1, 0)
a.Syscall()
code, data := a.Finalize(build.Verbose)
return code, data, nil
}
// Executable returns the path to the executable.

3
src/build/Function.go Normal file
View File

@ -0,0 +1,3 @@
package build
type Function struct{}

63
src/build/Scan.go Normal file
View File

@ -0,0 +1,63 @@
package build
import (
"os"
"path/filepath"
"strings"
"sync"
"git.akyoto.dev/cli/q/src/directory"
"git.akyoto.dev/cli/q/src/log"
)
// Scan scans the directory.
func Scan(path string) (<-chan *Function, <-chan error) {
functions := make(chan *Function, 16)
errors := make(chan error)
go func() {
scanDirectory(path, functions, errors)
close(functions)
close(errors)
}()
return functions, errors
}
// scanDirectory scans the directory without channel allocations.
func scanDirectory(path string, functions chan<- *Function, errors chan<- error) {
wg := sync.WaitGroup{}
directory.Walk(path, func(name string) {
if !strings.HasSuffix(name, ".q") {
return
}
fullPath := filepath.Join(path, name)
wg.Add(1)
go func() {
defer wg.Done()
err := scanFile(fullPath, functions)
if err != nil {
errors <- err
}
}()
})
wg.Wait()
}
// scanFile scans a single file.
func scanFile(path string, functions chan<- *Function) error {
log.Info.Println(path)
contents, err := os.ReadFile(path)
if err != nil {
return err
}
log.Info.Println(string(contents))
return nil
}

View File

@ -1,21 +1,18 @@
package cli
import (
"path/filepath"
"strings"
"git.akyoto.dev/cli/q/src/build"
"git.akyoto.dev/cli/q/src/log"
)
// Build builds an executable.
func Build(args []string) int {
directory := "."
b := build.New(".")
if len(args) > 0 {
directory = args[0]
}
b := build.New(directory)
for i := 1; i < len(args); i++ {
for i := 0; i < len(args); i++ {
switch args[i] {
case "--dry":
b.WriteExecutable = false
@ -24,12 +21,24 @@ func Build(args []string) int {
b.Verbose = true
default:
log.Error.Printf("Unknown parameter: %s\n", args[i])
return 2
if strings.HasPrefix(args[i], "-") {
log.Error.Printf("Unknown parameter: %s\n", args[i])
return 2
}
b.Directory = args[i]
}
}
err := b.Run()
fullPath, err := filepath.Abs(b.Directory)
if err != nil {
log.Error.Println(err)
return 1
}
b.Directory = fullPath
err = b.Run()
if err != nil {
log.Error.Println(err)