1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os_linux.v

This commit is contained in:
Alexander Medvednikov 2019-11-15 03:17:47 +03:00
parent 1dadf9d966
commit d1b8d34dd5
7 changed files with 84 additions and 27 deletions

View File

@ -1,28 +1,9 @@
module builtin module builtin
pub fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr //pub fn syscall5(number, arg1, arg2, arg3, arg4, arg5 voidptr) voidptr
//pub fn syscall6(number, arg1, arg2, arg3, arg4, arg5, arg6 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 panic(s string) { 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)
}

View File

@ -24,12 +24,14 @@ pub fn tos(s byteptr, len int) string {
} }
} }
/*
pub fn tos_clone(s byteptr) string { pub fn tos_clone(s byteptr) string {
if s == 0 { if s == 0 {
panic('tos: nil string') panic('tos: nil string')
} }
return tos2(s).clone() return tos2(s).clone()
} }
*/
// Same as `tos`, but calculates the length. Called by `string(bytes)` casts. // Same as `tos`, but calculates the length. Called by `string(bytes)` casts.
// Used only internally. // Used only internally.
@ -53,6 +55,7 @@ pub fn tos3(s *C.char) string {
} }
} }
/*
pub fn (a string) clone() string { pub fn (a string) clone() string {
mut b := string { mut b := string {
len: a.len len: a.len
@ -64,3 +67,4 @@ pub fn (a string) clone() string {
b[a.len] = `\0` b[a.len] = `\0`
return b return b
} }
*/

View File

@ -123,7 +123,7 @@ fn (v mut V) cc() {
v.out_name = v.out_name + '.so' v.out_name = v.out_name + '.so'
} }
if v.pref.is_bare { 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 { if v.pref.build_mode == .build_module {
// Create the modules & out directory if it's not there. // Create the modules & out directory if it's not there.

View File

@ -26,3 +26,14 @@
syscall syscall
ret 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

View File

@ -1,4 +0,0 @@
fn main() {
write(1, c'Hello!\n', 7)
println('println test\n')
}

View File

@ -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)
}

48
vlib/os/os_linux.v Normal file
View File

@ -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) {
}
*/