Added crash function

This commit is contained in:
2025-03-14 09:52:00 +01:00
parent 06f3af256b
commit 9726f1d832
8 changed files with 19 additions and 27 deletions

View File

@ -6,3 +6,7 @@ init() {
exit() { exit() {
syscall(93, 0) syscall(93, 0)
} }
crash() {
syscall(93, 1)
}

View File

@ -6,3 +6,7 @@ init() {
exit() { exit() {
syscall(60, 0) syscall(60, 0)
} }
crash() {
syscall(60, 1)
}

View File

@ -6,3 +6,7 @@ init() {
exit() { exit() {
syscall(0x2000001, 0) syscall(0x2000001, 0)
} }
crash() {
syscall(0x2000001, 1)
}

View File

@ -9,6 +9,10 @@ exit() {
kernel32.ExitProcess(0) kernel32.ExitProcess(0)
} }
crash() {
kernel32.ExitProcess(1)
}
const cp { const cp {
utf8 65001 utf8 65001
} }

View File

@ -16,7 +16,7 @@ restart:
for i, pointer := range c.codePointers { for i, pointer := range c.codePointers {
address := pointer.Resolve() address := pointer.Resolve()
if sizeof.Signed(int32(address)) > int(pointer.Size) { if config.TargetArch == config.X86 && sizeof.Signed(int32(address)) > int(pointer.Size) {
left := c.code[:pointer.Position-Address(pointer.OpSize)] left := c.code[:pointer.Position-Address(pointer.OpSize)]
right := c.code[pointer.Position+Address(pointer.Size):] right := c.code[pointer.Position+Address(pointer.Size):]
size := pointer.Size + pointer.OpSize size := pointer.Size + pointer.OpSize

View File

@ -3,9 +3,7 @@ package compiler
import ( import (
"git.urbach.dev/cli/q/src/asm" "git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/asmc" "git.urbach.dev/cli/q/src/asmc"
"git.urbach.dev/cli/q/src/config"
"git.urbach.dev/cli/q/src/core" "git.urbach.dev/cli/q/src/core"
"git.urbach.dev/cli/q/src/x86"
) )
// finalize generates the final machine code. // finalize generates the final machine code.
@ -45,25 +43,6 @@ func (r *Result) finalize() {
} }
}) })
final.Label(asm.LABEL, asm.Label{ final.Merge(&r.Functions["core.crash"].Assembler)
Name: "_crash",
Type: asm.ControlLabel,
})
switch config.TargetOS {
case config.Linux:
final.RegisterNumber(asm.MOVE, x86.SyscallInputRegisters[0], LinuxExit)
final.RegisterNumber(asm.MOVE, x86.SyscallInputRegisters[1], 1)
final.Syscall()
case config.Mac:
final.RegisterNumber(asm.MOVE, x86.SyscallInputRegisters[0], MacExit)
final.RegisterNumber(asm.MOVE, x86.SyscallInputRegisters[1], 1)
final.Syscall()
case config.Windows:
final.RegisterNumber(asm.MOVE, x86.WindowsInputRegisters[0], 1)
final.RegisterNumber(asm.AND, x86.RSP, -16)
final.DLLCall("kernel32.ExitProcess")
}
r.Code, r.Data = asmc.Finalize(&final, r.DLLs) r.Code, r.Data = asmc.Finalize(&final, r.DLLs)
} }

View File

@ -20,7 +20,7 @@ func (f *Function) CompileAssert(assert *ast.Assert) error {
f.Defer(func() { f.Defer(func() {
f.AddLabel(fail) f.AddLabel(fail)
f.Jump(asm.JUMP, asm.Label{Name: "_crash", Type: asm.ControlLabel}) f.Jump(asm.JUMP, asm.Label{Name: "core.crash", Type: asm.ControlLabel})
}) })
return err return err

View File

@ -1,6 +1,3 @@
main() { main() {
assert 1 != 0
assert 1 == 0 || 1 != 0
assert 1 != 0 && 2 != 0
assert 1 == 0 assert 1 == 0
} }