Added Windows PE support
This commit is contained in:
parent
2e354befc3
commit
3dffa69f37
20 changed files with 373 additions and 103 deletions
|
@ -14,6 +14,7 @@ import (
|
|||
"git.akyoto.dev/cli/q/src/os/linux/elf"
|
||||
"git.akyoto.dev/cli/q/src/os/mac"
|
||||
"git.akyoto.dev/cli/q/src/os/mac/macho"
|
||||
"git.akyoto.dev/cli/q/src/os/windows/pe"
|
||||
)
|
||||
|
||||
// Result contains all the compiled functions in a build.
|
||||
|
@ -35,20 +36,22 @@ func (r *Result) finalize() ([]byte, []byte) {
|
|||
Data: make(map[string][]byte, r.DataCount),
|
||||
}
|
||||
|
||||
sysExit := 0
|
||||
final.Call("main.main")
|
||||
|
||||
switch config.TargetOS {
|
||||
case "linux":
|
||||
sysExit = linux.Exit
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], linux.Exit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0)
|
||||
final.Syscall()
|
||||
case "mac":
|
||||
sysExit = mac.Exit
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], mac.Exit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0)
|
||||
final.Syscall()
|
||||
case "windows":
|
||||
final.RegisterNumber(asm.MOVE, x64.RAX, 0)
|
||||
final.Return()
|
||||
}
|
||||
|
||||
final.Call("main.main")
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], sysExit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 0)
|
||||
final.Syscall()
|
||||
|
||||
// This will place the main function immediately after the entry point
|
||||
// and also add everything the main function calls recursively.
|
||||
r.eachFunction(r.Main, map[*core.Function]bool{}, func(f *core.Function) {
|
||||
|
@ -56,9 +59,20 @@ func (r *Result) finalize() ([]byte, []byte) {
|
|||
})
|
||||
|
||||
final.Label(asm.LABEL, "_crash")
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], sysExit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1)
|
||||
final.Syscall()
|
||||
|
||||
switch config.TargetOS {
|
||||
case "linux":
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], linux.Exit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1)
|
||||
final.Syscall()
|
||||
case "mac":
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[0], mac.Exit)
|
||||
final.RegisterNumber(asm.MOVE, x64.SyscallInputRegisters[1], 1)
|
||||
final.Syscall()
|
||||
case "windows":
|
||||
final.RegisterNumber(asm.MOVE, x64.RAX, 1)
|
||||
final.Return()
|
||||
}
|
||||
|
||||
code, data := final.Finalize()
|
||||
return code, data
|
||||
|
@ -138,6 +152,9 @@ func write(writer io.Writer, code []byte, data []byte) error {
|
|||
case "mac":
|
||||
exe := macho.New(code, data)
|
||||
exe.Write(buffer)
|
||||
case "windows":
|
||||
exe := pe.New(code, data)
|
||||
exe.Write(buffer)
|
||||
default:
|
||||
return fmt.Errorf("unsupported platform '%s'", config.TargetOS)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue