From d1b8d34dd5e9f5f10b28a2514350b261df1f083c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 15 Nov 2019 03:17:47 +0300 Subject: [PATCH] os_linux.v --- vlib/builtin/bare/builtin_bare.v | 25 ++-------------- vlib/builtin/bare/string_bare.v | 4 +++ vlib/compiler/cc.v | 2 +- vlib/os/bare/bare.S | 11 +++++++ vlib/os/bare/bare.v | 4 --- vlib/os/bare/bare_example_linux.v | 17 +++++++++++ vlib/os/os_linux.v | 48 +++++++++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 27 deletions(-) delete mode 100644 vlib/os/bare/bare.v create mode 100644 vlib/os/bare/bare_example_linux.v create mode 100644 vlib/os/os_linux.v diff --git a/vlib/builtin/bare/builtin_bare.v b/vlib/builtin/bare/builtin_bare.v index 9019a5fd07..1c074cf991 100644 --- a/vlib/builtin/bare/builtin_bare.v +++ b/vlib/builtin/bare/builtin_bare.v @@ -1,28 +1,9 @@ module builtin -pub fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr - -// TODO no pub => error -pub fn write(fd int, data voidptr, nbytes int) int { - return syscall5( - 1, // SYS_write - fd, - data, - nbytes, - 0, // ignored - 0 // ignored - ) -} - -pub fn println(s string) { - write(1, s.str, s.len) -} +//pub fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr +//pub fn syscall6(number, arg1, arg2, arg3, arg4, arg5, arg6 voidptr) voidptr pub fn panic(s string) { - write(1, s.str, s.len) + //write(1, s.str, s.len) } -pub fn malloc(n int) voidptr { - return syscall5(0,0,0,0,0,0) - -} diff --git a/vlib/builtin/bare/string_bare.v b/vlib/builtin/bare/string_bare.v index 1366c5fce5..df67c69c18 100644 --- a/vlib/builtin/bare/string_bare.v +++ b/vlib/builtin/bare/string_bare.v @@ -24,12 +24,14 @@ pub fn tos(s byteptr, len int) string { } } +/* pub fn tos_clone(s byteptr) string { if s == 0 { panic('tos: nil string') } return tos2(s).clone() } +*/ // Same as `tos`, but calculates the length. Called by `string(bytes)` casts. // Used only internally. @@ -53,6 +55,7 @@ pub fn tos3(s *C.char) string { } } +/* pub fn (a string) clone() string { mut b := string { len: a.len @@ -64,3 +67,4 @@ pub fn (a string) clone() string { b[a.len] = `\0` return b } +*/ diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index 3ea0b0a21a..a71566ab59 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -123,7 +123,7 @@ fn (v mut V) cc() { v.out_name = v.out_name + '.so' } if v.pref.is_bare { - a << '-static -nostdlib $vdir/vlib/os/bare/bare.S' + a << '-static -ffreestanding -nostdlib $vdir/vlib/os/bare/bare.S' } if v.pref.build_mode == .build_module { // Create the modules & out directory if it's not there. diff --git a/vlib/os/bare/bare.S b/vlib/os/bare/bare.S index bcc72a259b..c5fcb6ce51 100644 --- a/vlib/os/bare/bare.S +++ b/vlib/os/bare/bare.S @@ -26,3 +26,14 @@ syscall ret + syscall6: + mov rax,rdi + mov rdi,rsi + mov rsi,rdx + mov rdx,rcx + mov r10,r8 + mov r8,r9 + mov r9, [rsp+8] + syscall + ret + diff --git a/vlib/os/bare/bare.v b/vlib/os/bare/bare.v deleted file mode 100644 index 27f08ada78..0000000000 --- a/vlib/os/bare/bare.v +++ /dev/null @@ -1,4 +0,0 @@ -fn main() { - write(1, c'Hello!\n', 7) - println('println test\n') -} diff --git a/vlib/os/bare/bare_example_linux.v b/vlib/os/bare/bare_example_linux.v new file mode 100644 index 0000000000..3876e2b18b --- /dev/null +++ b/vlib/os/bare/bare_example_linux.v @@ -0,0 +1,17 @@ +fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr + +fn write(fd int, data voidptr, nbytes u64) int { + return syscall5( + 1, // SYS_write + fd, + data, + nbytes, + 0, // ignored + 0 // ignored + ) +} + +fn main() { + write(1, c'hallo\n', 6) +} + diff --git a/vlib/os/os_linux.v b/vlib/os/os_linux.v new file mode 100644 index 0000000000..649be2b3c0 --- /dev/null +++ b/vlib/os/os_linux.v @@ -0,0 +1,48 @@ +// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + +module os + +const ( + PROT_READ = 1 + PROT_WRITE = 2 + + MAP_PRIVATE = 0x02 + MAP_ANONYMOUS = 0x20 +) + + +/* +// TODO no pub => error +pub fn write(fd int, data voidptr, nbytes int) int { + return syscall5( + 1, // SYS_write + fd, + data, + nbytes, + 0, // ignored + 0 // ignored + ) +} + +pub fn println(s string) { + write(1, (s + '\n').str, s.len) +} + +fn mmap(start voidptr, len, prot, flags, fd, off int) byteptr { + return syscall6(9, start, len, prot, flags, fd, off) // sys_mmap +} + +pub fn malloc(n int) byteptr { + println('malloc($n)') + return mmap(0, n, 3, 4098, //PROT_READ|PROT_WRITE, + -1,0) //MAP_PRIVATE|MAP_ANONYMOUS, +} + +pub fn free(b byteptr) { + +} +*/ + +