diff --git a/examples/thread/thread.q b/examples/thread/thread.q index 8e33e07..6d0493b 100644 --- a/examples/thread/thread.q +++ b/examples/thread/thread.q @@ -1,5 +1,4 @@ import io -import sys import thread main() { @@ -12,5 +11,4 @@ main() { work() { io.out("[ ] start\n") io.out("[x] end\n") - sys.exit(0) } \ No newline at end of file diff --git a/lib/thread/thread_linux.q b/lib/thread/thread_linux.q index be70f4f..5179bed 100644 --- a/lib/thread/thread_linux.q +++ b/lib/thread/thread_linux.q @@ -1,9 +1,22 @@ import sys +const clone { + vm 0x100 + fs 0x200 + files 0x400 + sighand 0x800 + parent 0x8000 + thread 0x10000 + io 0x80000000 +} + create(func *Any) -> Int { size := 4096 stack := sys.mmap(0, size, 0x1|0x2, 0x02|0x20|0x100|0x20000) - rip := stack + size - 8 - store(rip, 8, func) - return sys.clone(0x100|0x200|0x400|0x800|0x8000|0x10000|0x80000000, rip) + stack += size + stack -= 8 + store(stack, 8, _exit) + stack -= 8 + store(stack, 8, func) + return sys.clone(clone.vm|clone.fs|clone.files|clone.sighand|clone.parent|clone.thread|clone.io, stack) } \ No newline at end of file diff --git a/src/compiler/finalize.go b/src/compiler/finalize.go index dc21b8a..c317754 100644 --- a/src/compiler/finalize.go +++ b/src/compiler/finalize.go @@ -21,6 +21,7 @@ func (r *Result) finalize() { } final.Call("main.main") + final.Label(asm.LABEL, "_exit") switch config.TargetOS { case config.Linux: diff --git a/src/core/ExpressionToMemory.go b/src/core/ExpressionToMemory.go index 21f01f6..461cc1c 100644 --- a/src/core/ExpressionToMemory.go +++ b/src/core/ExpressionToMemory.go @@ -26,6 +26,11 @@ func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Me return types.AnyPointer, nil } + if name == "_exit" { + f.MemoryLabel(asm.STORE, memory, "_exit") + return types.AnyPointer, nil + } + return nil, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, node.Token.Position) } diff --git a/src/core/Fold.go b/src/core/Fold.go index 46615de..eaaf0cd 100644 --- a/src/core/Fold.go +++ b/src/core/Fold.go @@ -27,6 +27,23 @@ func (f *Function) Fold(expr *expression.Expression) error { return nil } + if expr.Token.Kind == token.Period { + left := expr.Children[0] + leftText := left.Token.Text(f.File.Bytes) + right := expr.Children[1] + rightText := right.Token.Text(f.File.Bytes) + constant, isConst := f.Constants[f.Package+"."+leftText+"."+rightText] + + if !isConst { + return nil + } + + value, err := f.ToNumber(constant.Value) + expr.Value = value + expr.IsFolded = true + return err + } + canFold := true for _, child := range expr.Children {