githwxi / ATS-Postiats

ATS2: Unleashing the Potentials of Types and Templates
www.ats-lang.org
Other
351 stars 51 forks source link

Syscalls #212

Open antoyo opened 6 years ago

antoyo commented 6 years ago

Hi. Could you please add a way to do system calls in the language? That would be useful when you want to do low-level stuff without using C. Thanks.

githwxi commented 6 years ago

Could you propose a way to do it?

jrfondren commented 6 years ago

you could start with the syscall() library call, to get all the functionality with some overhead

and then use inline ASM (in inline C) on a per-architecture basis to drop the overhead.

I would expect that one of the libc-lite libraries already does this, though.

antoyo commented 5 years ago

For instance, of x86_64, the code would look like:

%{^
long syscall1(long value) {
    long result;
    __asm__("mov %1, %%rax;"
         "syscall"
         : "=r"(result)
         :"r"(value)
         :"%rax", "%rdi");
    return result;
}

long syscall2(long value1, long value2) {
    long result;
    __asm__("mov %1, %%rax;"
         "mov %2, %%rdi;"
         "syscall"
         : "=r"(result)
         :"r"(value1), "r"(value2)
         :"%rax", "%rdi");
    return result;
}
%}

extern fn syscall1(value: lint): lint =
    "ext#"

extern fn syscall2(value1: lint, value2: lint): lint =
    "ext#"

(or perhaps using variadics)

bbarker commented 5 years ago

cc @master-q - seems like may be of interest