Reduced number of packages

This commit is contained in:
Eduard Urbach 2024-08-26 15:34:34 +02:00
parent b225717775
commit e2a6a31d8e
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
12 changed files with 36 additions and 69 deletions

View File

@ -37,4 +37,8 @@ const (
SP // Stack pointer SP // Stack pointer
) )
var SyscallInputRegisters = []cpu.Register{X8, X0, X1, X2, X3, X4, X5} var (
SyscallInputRegisters = []cpu.Register{X8, X0, X1, X2, X3, X4, X5}
WindowsInputRegisters = []cpu.Register{X0, X1, X2, X3, X4, X5, X6, X7}
WindowsOutputRegisters = []cpu.Register{X0, X1}
)

View File

@ -8,7 +8,7 @@ import (
"git.akyoto.dev/cli/q/src/config" "git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/dll" "git.akyoto.dev/cli/q/src/dll"
"git.akyoto.dev/cli/q/src/exe" "git.akyoto.dev/cli/q/src/fs"
"git.akyoto.dev/cli/q/src/sizeof" "git.akyoto.dev/cli/q/src/sizeof"
"git.akyoto.dev/cli/q/src/x64" "git.akyoto.dev/cli/q/src/x64"
) )
@ -372,7 +372,7 @@ restart:
data, dataLabels = a.Data.Finalize() data, dataLabels = a.Data.Finalize()
dataStart := config.BaseAddress + config.CodeOffset + len(code) dataStart := config.BaseAddress + config.CodeOffset + len(code)
dataStart, _ = exe.Align(dataStart, config.Align) dataStart, _ = fs.Align(dataStart, config.Align)
for _, pointer := range dataPointers { for _, pointer := range dataPointers {
address := Address(dataStart) + pointer.Resolve() address := Address(dataStart) + pointer.Resolve()

View File

@ -1,7 +1,7 @@
package linux package compiler
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/entry/syscalls/syscall_64.tbl // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/entry/syscalls/syscall_64.tbl
const ( const (
Write = 1 LinuxWrite = 1
Exit = 60 LinuxExit = 60
) )

View File

@ -1,10 +1,10 @@
package mac package compiler
// Syscall numbers are divided into classes, here we need the BSD inherited syscalls. // Syscall numbers are divided into classes, here we need the BSD inherited syscalls.
const SyscallClassUnix = 0x2000000 const classUnix = 0x2000000
// https://github.com/apple-oss-distributions/xnu/blob/main/bsd/kern/syscalls.master // https://github.com/apple-oss-distributions/xnu/blob/main/bsd/kern/syscalls.master
const ( const (
Exit = 1 | SyscallClassUnix MacExit = 1 | classUnix
Write = 4 | SyscallClassUnix MacWrite = 4 | classUnix
) )

View File

@ -11,11 +11,8 @@ import (
"git.akyoto.dev/cli/q/src/core" "git.akyoto.dev/cli/q/src/core"
"git.akyoto.dev/cli/q/src/dll" "git.akyoto.dev/cli/q/src/dll"
"git.akyoto.dev/cli/q/src/elf" "git.akyoto.dev/cli/q/src/elf"
"git.akyoto.dev/cli/q/src/linux"
"git.akyoto.dev/cli/q/src/mac"
"git.akyoto.dev/cli/q/src/macho" "git.akyoto.dev/cli/q/src/macho"
"git.akyoto.dev/cli/q/src/pe" "git.akyoto.dev/cli/q/src/pe"
"git.akyoto.dev/cli/q/src/windows"
"git.akyoto.dev/cli/q/src/x64" "git.akyoto.dev/cli/q/src/x64"
) )
@ -42,15 +39,15 @@ func (r *Result) finalize() ([]byte, []byte, dll.List) {
switch config.TargetOS { switch config.TargetOS {
case "linux": case "linux":
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], linux.Exit) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], LinuxExit)
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0)
final.Syscall() final.Syscall()
case "mac": case "mac":
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], mac.Exit) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], MacExit)
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0)
final.Syscall() final.Syscall()
case "windows": case "windows":
final.RegisterNumber(asm.MOVE, windows.X64InputRegisters[0], 0) final.RegisterNumber(asm.MOVE, x64.WindowsInputRegisters[0], 0)
final.DLLCall("kernel32.ExitProcess") final.DLLCall("kernel32.ExitProcess")
} }
@ -74,15 +71,15 @@ func (r *Result) finalize() ([]byte, []byte, dll.List) {
switch config.TargetOS { switch config.TargetOS {
case "linux": case "linux":
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], linux.Exit) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], LinuxExit)
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1)
final.Syscall() final.Syscall()
case "mac": case "mac":
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], mac.Exit) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], MacExit)
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1) final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1)
final.Syscall() final.Syscall()
case "windows": case "windows":
final.RegisterNumber(asm.MOVE, windows.X64InputRegisters[0], 1) final.RegisterNumber(asm.MOVE, x64.WindowsInputRegisters[0], 1)
final.DLLCall("kernel32.ExitProcess") final.DLLCall("kernel32.ExitProcess")
} }

View File

@ -7,7 +7,7 @@ import (
"git.akyoto.dev/cli/q/src/errors" "git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/expression" "git.akyoto.dev/cli/q/src/expression"
"git.akyoto.dev/cli/q/src/types" "git.akyoto.dev/cli/q/src/types"
"git.akyoto.dev/cli/q/src/windows" "git.akyoto.dev/cli/q/src/x64"
) )
// CompileCall executes a function call. // CompileCall executes a function call.
@ -36,7 +36,7 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
if pkg == "kernel32" || pkg == "user32" || pkg == "gdi32" || pkg == "comctl32" { if pkg == "kernel32" || pkg == "user32" || pkg == "gdi32" || pkg == "comctl32" {
parameters := root.Children[1:] parameters := root.Children[1:]
registers := windows.X64InputRegisters[:len(parameters)] registers := x64.WindowsInputRegisters[:len(parameters)]
for i := len(parameters) - 1; i >= 0; i-- { for i := len(parameters) - 1; i >= 0; i-- {
_, err := f.ExpressionToRegister(parameters[i], registers[i]) _, err := f.ExpressionToRegister(parameters[i], registers[i])

View File

@ -6,7 +6,7 @@ import (
"io" "io"
"git.akyoto.dev/cli/q/src/config" "git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/exe" "git.akyoto.dev/cli/q/src/fs"
) )
// ELF represents an ELF file. // ELF represents an ELF file.
@ -21,8 +21,8 @@ func Write(writer io.Writer, code []byte, data []byte) {
const HeaderEnd = HeaderSize + ProgramHeaderSize*2 const HeaderEnd = HeaderSize + ProgramHeaderSize*2
var ( var (
codeStart, codePadding = exe.Align(HeaderEnd, config.Align) codeStart, codePadding = fs.Align(HeaderEnd, config.Align)
dataStart, dataPadding = exe.Align(codeStart+len(code), config.Align) dataStart, dataPadding = fs.Align(codeStart+len(code), config.Align)
) )
elf := &ELF{ elf := &ELF{

View File

@ -1,4 +1,4 @@
package exe package fs
// Align calculates the next aligned address and the padding needed. // Align calculates the next aligned address and the padding needed.
func Align[T int | uint | int64 | uint64 | int32 | uint32](n T, alignment T) (T, T) { func Align[T int | uint | int64 | uint64 | int32 | uint32](n T, alignment T) (T, T) {

View File

@ -6,7 +6,7 @@ import (
"io" "io"
"git.akyoto.dev/cli/q/src/config" "git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/exe" "git.akyoto.dev/cli/q/src/fs"
) )
// MachO is the executable format used on MacOS. // MachO is the executable format used on MacOS.
@ -26,8 +26,8 @@ func Write(writer io.Writer, code []byte, data []byte) {
) )
var ( var (
codeStart, codePadding = exe.Align(HeaderEnd, config.Align) codeStart, codePadding = fs.Align(HeaderEnd, config.Align)
dataStart, dataPadding = exe.Align(codeStart+len(code), config.Align) dataStart, dataPadding = fs.Align(codeStart+len(code), config.Align)
) )
m := &MachO{ m := &MachO{

View File

@ -7,7 +7,7 @@ import (
"git.akyoto.dev/cli/q/src/config" "git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/dll" "git.akyoto.dev/cli/q/src/dll"
"git.akyoto.dev/cli/q/src/exe" "git.akyoto.dev/cli/q/src/fs"
) )
// EXE is the portable executable format used on Windows. // EXE is the portable executable format used on Windows.
@ -26,9 +26,9 @@ func Write(writer io.Writer, code []byte, data []byte, dlls dll.List) {
NumSections := 3 NumSections := 3
HeaderEnd := DOSHeaderSize + NTHeaderSize + OptionalHeader64Size + SectionHeaderSize*NumSections HeaderEnd := DOSHeaderSize + NTHeaderSize + OptionalHeader64Size + SectionHeaderSize*NumSections
codeStart, codePadding := exe.Align(HeaderEnd, config.Align) codeStart, codePadding := fs.Align(HeaderEnd, config.Align)
dataStart, dataPadding := exe.Align(codeStart+len(code), config.Align) dataStart, dataPadding := fs.Align(codeStart+len(code), config.Align)
importsStart, importsPadding := exe.Align(dataStart+len(data), config.Align) importsStart, importsPadding := fs.Align(dataStart+len(data), config.Align)
subSystem := IMAGE_SUBSYSTEM_WINDOWS_CUI subSystem := IMAGE_SUBSYSTEM_WINDOWS_CUI
@ -91,7 +91,7 @@ func Write(writer io.Writer, code []byte, data []byte, dlls dll.List) {
importSectionSize := len(imports)*8 + len(dllData) + importDirectorySize importSectionSize := len(imports)*8 + len(dllData) + importDirectorySize
imageSize := importsStart + importSectionSize imageSize := importsStart + importSectionSize
imageSize, _ = exe.Align(imageSize, config.Align) imageSize, _ = fs.Align(imageSize, config.Align)
pe := &EXE{ pe := &EXE{
DOSHeader: DOSHeader{ DOSHeader: DOSHeader{

View File

@ -1,36 +0,0 @@
package windows
import (
"git.akyoto.dev/cli/q/src/arm64"
"git.akyoto.dev/cli/q/src/cpu"
"git.akyoto.dev/cli/q/src/x64"
)
var (
X64InputRegisters = []cpu.Register{
x64.RCX,
x64.RDX,
x64.R8,
x64.R9,
}
X64OutputRegisters = []cpu.Register{
x64.RAX,
}
ARM64InputRegisters = []cpu.Register{
arm64.X0,
arm64.X1,
arm64.X2,
arm64.X3,
arm64.X4,
arm64.X5,
arm64.X6,
arm64.X7,
}
ARM64OutputRegisters = []cpu.Register{
arm64.X0,
arm64.X1,
}
)

View File

@ -28,4 +28,6 @@ var (
GeneralRegisters = []cpu.Register{RCX, RBX, RBP, R11, R12, R13, R14, R15} GeneralRegisters = []cpu.Register{RCX, RBX, RBP, R11, R12, R13, R14, R15}
InputRegisters = SyscallInputRegisters InputRegisters = SyscallInputRegisters
OutputRegisters = SyscallInputRegisters OutputRegisters = SyscallInputRegisters
WindowsInputRegisters = []cpu.Register{RCX, RDX, R8, R9}
WindowsOutputRegisters = []cpu.Register{RAX}
) )