some docs for devs, added a few examples, implemented syscalls

This commit is contained in:
MCorange 2023-03-14 21:50:14 +02:00
parent 8d72420b04
commit 2c82aebd60
16 changed files with 516 additions and 8 deletions

View File

@ -0,0 +1,318 @@
# linux syscalls
|%rax | Name | Entry point | Implementation |
|-----|------------------------|-------------------------------|------------------------------------|
| 0 | read | sys_read | fs/read_write.c |
| 1 | write | sys_write | fs/read_write.c |
| 2 | open | sys_open | fs/open.c |
| 3 | close | sys_close | fs/open.c |
| 4 | stat | sys_newstat | fs/stat.c |
| 5 | fstat | sys_newfstat | fs/stat.c |
| 6 | lstat | sys_newlstat | fs/stat.c |
| 7 | poll | sys_poll | fs/select.c |
| 8 | lseek | sys_lseek | fs/read_write.c |
| 9 | mmap | sys_mmap | arch/x86/kernel/sys_x86_64.c |
| 10 | mprotect | sys_mprotect | mm/mprotect.c |
| 11 | munmap | sys_munmap | mm/mmap.c |
| 12 | brk | sys_brk | mm/mmap.c |
| 13 | rt_sigaction | sys_rt_sigaction | kernel/signal.c |
| 14 | rt_sigprocmask | sys_rt_sigprocmask | kernel/signal.c |
| 15 | rt_sigreturn | stub_rt_sigreturn | arch/x86/kernel/signal.c |
| 16 | ioctl | sys_ioctl | fs/ioctl.c |
| 17 | pread64 | sys_pread64 | fs/read_write.c |
| 18 | pwrite64 | sys_pwrite64 | fs/read_write.c |
| 19 | readv | sys_readv | fs/read_write.c |
| 20 | writev | sys_writev | fs/read_write.c |
| 21 | access | sys_access | fs/open.c |
| 22 | pipe | sys_pipe | fs/pipe.c |
| 23 | select | sys_select | fs/select.c |
| 24 | sched_yield | sys_sched_yield | kernel/sched/core.c |
| 25 | mremap | sys_mremap | mm/mmap.c |
| 26 | msync | sys_msync | mm/msync.c |
| 27 | mincore | sys_mincore | mm/mincore.c |
| 28 | madvise | sys_madvise | mm/madvise.c |
| 29 | shmget | sys_shmget | ipc/shm.c |
| 30 | shmat | sys_shmat | ipc/shm.c |
| 31 | shmctl | sys_shmctl | ipc/shm.c |
| 32 | dup | sys_dup | fs/file.c |
| 33 | dup2 | sys_dup2 | fs/file.c |
| 34 | pause | sys_pause | kernel/signal.c |
| 35 | nanosleep | sys_nanosleep | kernel/hrtimer.c |
| 36 | getitimer | sys_getitimer | kernel/itimer.c |
| 37 | alarm | sys_alarm | kernel/timer.c |
| 38 | setitimer | sys_setitimer | kernel/itimer.c |
| 39 | getpid | sys_getpid | kernel/sys.c |
| 40 | sendfile | sys_sendfile64 | fs/read_write.c |
| 41 | socket | sys_socket | net/socket.c |
| 42 | connect | sys_connect | net/socket.c |
| 43 | accept | sys_accept | net/socket.c |
| 44 | sendto | sys_sendto | net/socket.c |
| 45 | recvfrom | sys_recvfrom | net/socket.c |
| 46 | sendmsg | sys_sendmsg | net/socket.c |
| 47 | recvmsg | sys_recvmsg | net/socket.c |
| 48 | shutdown | sys_shutdown | net/socket.c |
| 49 | bind | sys_bind | net/socket.c |
| 50 | listen | sys_listen | net/socket.c |
| 51 | getsockname | sys_getsockname | net/socket.c |
| 52 | getpeername | sys_getpeername | net/socket.c |
| 53 | socketpair | sys_socketpair | net/socket.c |
| 54 | setsockopt | sys_setsockopt | net/socket.c |
| 55 | getsockopt | sys_getsockopt | net/socket.c |
| 56 | clone | stub_clone | kernel/fork.c |
| 57 | fork | stub_fork | kernel/fork.c |
| 58 | vfork | stub_vfork | kernel/fork.c |
| 59 | execve | stub_execve | fs/exec.c |
| 60 | exit | sys_exit | kernel/exit.c |
| 61 | wait4 | sys_wait4 | kernel/exit.c |
| 62 | kill | sys_kill | kernel/signal.c |
| 63 | uname | sys_newuname | kernel/sys.c |
| 64 | semget | sys_semget | ipc/sem.c |
| 65 | semop | sys_semop | ipc/sem.c |
| 66 | semctl | sys_semctl | ipc/sem.c |
| 67 | shmdt | sys_shmdt | ipc/shm.c |
| 68 | msgget | sys_msgget | ipc/msg.c |
| 69 | msgsnd | sys_msgsnd | ipc/msg.c |
| 70 | msgrcv | sys_msgrcv | ipc/msg.c |
| 71 | msgctl | sys_msgctl | ipc/msg.c |
| 72 | fcntl | sys_fcntl | fs/fcntl.c |
| 73 | flock | sys_flock | fs/locks.c |
| 74 | fsync | sys_fsync | fs/sync.c |
| 75 | fdatasync | sys_fdatasync | fs/sync.c |
| 76 | truncate | sys_truncate | fs/open.c |
| 77 | ftruncate | sys_ftruncate | fs/open.c |
| 78 | getdents | sys_getdents | fs/readdir.c |
| 79 | getcwd | sys_getcwd | fs/dcache.c |
| 80 | chdir | sys_chdir | fs/open.c |
| 81 | fchdir | sys_fchdir | fs/open.c |
| 82 | rename | sys_rename | fs/namei.c |
| 83 | mkdir | sys_mkdir | fs/namei.c |
| 84 | rmdir | sys_rmdir | fs/namei.c |
| 85 | creat | sys_creat | fs/open.c |
| 86 | link | sys_link | fs/namei.c |
| 87 | unlink | sys_unlink | fs/namei.c |
| 88 | symlink | sys_symlink | fs/namei.c |
| 89 | readlink | sys_readlink | fs/stat.c |
| 90 | chmod | sys_chmod | fs/open.c |
| 91 | fchmod | sys_fchmod | fs/open.c |
| 92 | chown | sys_chown | fs/open.c |
| 93 | fchown | sys_fchown | fs/open.c |
| 94 | lchown | sys_lchown | fs/open.c |
| 95 | umask | sys_umask | kernel/sys.c |
| 96 | gettimeofday | sys_gettimeofday | kernel/time.c |
| 97 | getrlimit | sys_getrlimit | kernel/sys.c |
| 98 | getrusage | sys_getrusage | kernel/sys.c |
| 99 | sysinfo | sys_sysinfo | kernel/sys.c |
| 100 | times | sys_times | kernel/sys.c |
| 101 | ptrace | sys_ptrace | kernel/ptrace.c |
| 102 | getuid | sys_getuid | kernel/sys.c |
| 103 | syslog | sys_syslog | kernel/printk/printk.c |
| 104 | getgid | sys_getgid | kernel/sys.c |
| 105 | setuid | sys_setuid | kernel/sys.c |
| 106 | setgid | sys_setgid | kernel/sys.c |
| 107 | geteuid | sys_geteuid | kernel/sys.c |
| 108 | getegid | sys_getegid | kernel/sys.c |
| 109 | setpgid | sys_setpgid | kernel/sys.c |
| 110 | getppid | sys_getppid | kernel/sys.c |
| 111 | getpgrp | sys_getpgrp | kernel/sys.c |
| 112 | setsid | sys_setsid | kernel/sys.c |
| 113 | setreuid | sys_setreuid | kernel/sys.c |
| 114 | setregid | sys_setregid | kernel/sys.c |
| 115 | getgroups | sys_getgroups | kernel/groups.c |
| 116 | setgroups | sys_setgroups | kernel/groups.c |
| 117 | setresuid | sys_setresuid | kernel/sys.c |
| 118 | getresuid | sys_getresuid | kernel/sys.c |
| 119 | setresgid | sys_setresgid | kernel/sys.c |
| 120 | getresgid | sys_getresgid | kernel/sys.c |
| 121 | getpgid | sys_getpgid | kernel/sys.c |
| 122 | setfsuid | sys_setfsuid | kernel/sys.c |
| 123 | setfsgid | sys_setfsgid | kernel/sys.c |
| 124 | getsid | sys_getsid | kernel/sys.c |
| 125 | capget | sys_capget | kernel/capability.c |
| 126 | capset | sys_capset | kernel/capability.c |
| 127 | rt_sigpending | sys_rt_sigpending | kernel/signal.c |
| 128 | rt_sigtimedwait | sys_rt_sigtimedwait | kernel/signal.c |
| 129 | rt_sigqueueinfo | sys_rt_sigqueueinfo | kernel/signal.c |
| 130 | rt_sigsuspend | sys_rt_sigsuspend | kernel/signal.c |
| 131 | sigaltstack | sys_sigaltstack | kernel/signal.c |
| 132 | utime | sys_utime | fs/utimes.c |
| 133 | mknod | sys_mknod | fs/namei.c |
| 134 | uselib | NONE_NONE | fs/exec.c |
| 135 | personality | sys_personality | kernel/exec_domain.c |
| 136 | ustat | sys_ustat | fs/statfs.c |
| 137 | statfs | sys_statfs | fs/statfs.c |
| 138 | fstatfs | sys_fstatfs | fs/statfs.c |
| 139 | sysfs | sys_sysfs | fs/filesystems.c |
| 140 | getpriority | sys_getpriority | kernel/sys.c |
| 141 | setpriority | sys_setpriority | kernel/sys.c |
| 142 | sched_setparam | sys_sched_setparam | kernel/sched/core.c |
| 143 | sched_getparam | sys_sched_getparam | kernel/sched/core.c |
| 144 | sched_setscheduler | sys_sched_setscheduler | kernel/sched/core.c |
| 145 | sched_getscheduler | sys_sched_getscheduler | kernel/sched/core.c |
| 146 | sched_get_priority_max | sys_sched_get_priority_max | kernel/sched/core.c |
| 147 | sched_get_priority_min | sys_sched_get_priority_min | kernel/sched/core.c |
| 148 | sched_rr_get_interval | sys_sched_rr_get_interval | kernel/sched/core.c |
| 149 | mlock | sys_mlock | mm/mlock.c |
| 150 | munlock | sys_munlock | mm/mlock.c |
| 151 | mlockall | sys_mlockall | mm/mlock.c |
| 152 | munlockall | sys_munlockall | mm/mlock.c |
| 153 | vhangup | sys_vhangup | fs/open.c |
| 154 | modify_ldt | sys_modify_ldt | arch/x86/um/ldt.c |
| 155 | pivot_root | sys_pivot_root | fs/namespace.c |
| 156 | _sysctl | sys_sysctl | kernel/sysctl_binary.c |
| 157 | prctl | sys_prctl | kernel/sys.c |
| 158 | arch_prctl | sys_arch_prctl | arch/x86/um/syscalls_64.c |
| 159 | adjtimex | sys_adjtimex | kernel/time.c |
| 160 | setrlimit | sys_setrlimit | kernel/sys.c |
| 161 | chroot | sys_chroot | fs/open.c |
| 162 | sync | sys_sync | fs/sync.c |
| 163 | acct | sys_acct | kernel/acct.c |
| 164 | settimeofday | sys_settimeofday | kernel/time.c |
| 165 | mount | sys_mount | fs/namespace.c |
| 166 | umount2 | sys_umount | fs/namespace.c |
| 167 | swapon | sys_swapon | mm/swapfile.c |
| 168 | swapoff | sys_swapoff | mm/swapfile.c |
| 169 | reboot | sys_reboot | kernel/reboot.c |
| 170 | sethostname | sys_sethostname | kernel/sys.c |
| 171 | setdomainname | sys_setdomainname | kernel/sys.c |
| 172 | iopl | stub_iopl | arch/x86/kernel/ioport.c |
| 173 | ioperm | sys_ioperm | arch/x86/kernel/ioport.c |
| 174 | create_module | NONE_NONE | NOT_IMPLEMENTED |
| 175 | init_module | sys_init_module | kernel/module.c |
| 176 | delete_module | sys_delete_module | kernel/module.c |
| 177 | get_kernel_syms | NONE_NONE | NOT_IMPLEMENTED |
| 178 | query_module | NONE_NONE | NOT_IMPLEMENTED |
| 179 | quotactl | sys_quotactl | fs/quota/quota.c |
| 180 | nfsservctl | NONE_NONE | NOT_IMPLEMENTED |
| 181 | getpmsg | NONE_NONE | NOT_IMPLEMENTED |
| 182 | putpmsg | NONE_NONE | NOT_IMPLEMENTED |
| 183 | afs_syscall | NONE_NONE | NOT_IMPLEMENTED |
| 184 | tuxcall | NONE_NONE | NOT_IMPLEMENTED |
| 185 | security | NONE_NONE | NOT_IMPLEMENTED |
| 186 | gettid | sys_gettid | kernel/sys.c |
| 187 | readahead | sys_readahead | mm/readahead.c |
| 188 | setxattr | sys_setxattr | fs/xattr.c |
| 189 | lsetxattr | sys_lsetxattr | fs/xattr.c |
| 190 | fsetxattr | sys_fsetxattr | fs/xattr.c |
| 191 | getxattr | sys_getxattr | fs/xattr.c |
| 192 | lgetxattr | sys_lgetxattr | fs/xattr.c |
| 193 | fgetxattr | sys_fgetxattr | fs/xattr.c |
| 194 | listxattr | sys_listxattr | fs/xattr.c |
| 195 | llistxattr | sys_llistxattr | fs/xattr.c |
| 196 | flistxattr | sys_flistxattr | fs/xattr.c |
| 197 | removexattr | sys_removexattr | fs/xattr.c |
| 198 | lremovexattr | sys_lremovexattr | fs/xattr.c |
| 199 | fremovexattr | sys_fremovexattr | fs/xattr.c |
| 200 | tkill | sys_tkill | kernel/signal.c |
| 201 | time | sys_time | kernel/time.c |
| 202 | futex | sys_futex | kernel/futex.c |
| 203 | sched_setaffinity | sys_sched_setaffinity | kernel/sched/core.c |
| 204 | sched_getaffinity | sys_sched_getaffinity | kernel/sched/core.c |
| 205 | set_thread_area | NONE_NONE | arch/x86/kernel/tls.c |
| 206 | io_setup | sys_io_setup | fs/aio.c |
| 207 | io_destroy | sys_io_destroy | fs/aio.c |
| 208 | io_getevents | sys_io_getevents | fs/aio.c |
| 209 | io_submit | sys_io_submit | fs/aio.c |
| 210 | io_cancel | sys_io_cancel | fs/aio.c |
| 211 | get_thread_area | NONE_NONE | arch/x86/kernel/tls.c |
| 212 | lookup_dcookie | sys_lookup_dcookie | fs/dcookies.c |
| 213 | epoll_create | sys_epoll_create | fs/eventpoll.c |
| 214 | epoll_ctl_old | NONE_NONE | NOT_IMPLEMENTED |
| 215 | epoll_wait_old | NONE_NONE | NOT_IMPLEMENTED |
| 216 | remap_file_pages | sys_remap_file_pages | mm/fremap.c |
| 217 | getdents64 | sys_getdents64 | fs/readdir.c |
| 218 | set_tid_address | sys_set_tid_address | kernel/fork.c |
| 219 | restart_syscall | sys_restart_syscall | kernel/signal.c |
| 220 | semtimedop | sys_semtimedop | ipc/sem.c |
| 221 | fadvise64 | sys_fadvise64 | mm/fadvise.c |
| 222 | timer_create | sys_timer_create | kernel/posix-timers.c |
| 223 | timer_settime | sys_timer_settime | kernel/posix-timers.c |
| 224 | timer_gettime | sys_timer_gettime | kernel/posix-timers.c |
| 225 | timer_getoverrun | sys_timer_getoverrun | kernel/posix-timers.c |
| 226 | timer_delete | sys_timer_delete | kernel/posix-timers.c |
| 227 | clock_settime | sys_clock_settime | kernel/posix-timers.c |
| 228 | clock_gettime | sys_clock_gettime | kernel/posix-timers.c |
| 229 | clock_getres | sys_clock_getres | kernel/posix-timers.c |
| 230 | clock_nanosleep | sys_clock_nanosleep | kernel/posix-timers.c |
| 231 | exit_group | sys_exit_group | kernel/exit.c |
| 232 | epoll_wait | sys_epoll_wait | fs/eventpoll.c |
| 233 | epoll_ctl | sys_epoll_ctl | fs/eventpoll.c |
| 234 | tgkill | sys_tgkill | kernel/signal.c |
| 235 | utimes | sys_utimes | fs/utimes.c |
| 236 | vserver | NONE_NONE | NOT_IMPLEMENTED |
| 237 | mbind | sys_mbind | mm/mempolicy.c |
| 238 | set_mempolicy | sys_set_mempolicy | mm/mempolicy.c |
| 239 | get_mempolicy | sys_get_mempolicy | mm/mempolicy.c |
| 240 | mq_open | sys_mq_open | ipc/mqueue.c |
| 241 | mq_unlink | sys_mq_unlink | ipc/mqueue.c |
| 242 | mq_timedsend | sys_mq_timedsend | ipc/mqueue.c |
| 243 | mq_timedreceive | sys_mq_timedreceive | ipc/mqueue.c |
| 244 | mq_notify | sys_mq_notify | ipc/mqueue.c |
| 245 | mq_getsetattr | sys_mq_getsetattr | ipc/mqueue.c |
| 246 | kexec_load | sys_kexec_load | kernel/kexec.c |
| 247 | waitid | sys_waitid | kernel/exit.c |
| 248 | add_key | sys_add_key | security/keys/keyctl.c |
| 249 | request_key | sys_request_key | security/keys/keyctl.c |
| 250 | keyctl | sys_keyctl | security/keys/keyctl.c |
| 251 | ioprio_set | sys_ioprio_set | fs/ioprio.c |
| 252 | ioprio_get | sys_ioprio_get | fs/ioprio.c |
| 253 | inotify_init | sys_inotify_init | fs/notify/inotify/inotify_user.c |
| 254 | inotify_add_watch | sys_inotify_add_watch | fs/notify/inotify/inotify_user.c |
| 255 | inotify_rm_watch | sys_inotify_rm_watch | fs/notify/inotify/inotify_user.c |
| 256 | migrate_pages | sys_migrate_pages | mm/mempolicy.c |
| 257 | openat | sys_openat | fs/open.c |
| 258 | mkdirat | sys_mkdirat | fs/namei.c |
| 259 | mknodat | sys_mknodat | fs/namei.c |
| 260 | fchownat | sys_fchownat | fs/open.c |
| 261 | futimesat | sys_futimesat | fs/utimes.c |
| 262 | newfstatat | sys_newfstatat | fs/stat.c |
| 263 | unlinkat | sys_unlinkat | fs/namei.c |
| 264 | renameat | sys_renameat | fs/namei.c |
| 265 | linkat | sys_linkat | fs/namei.c |
| 266 | symlinkat | sys_symlinkat | fs/namei.c |
| 267 | readlinkat | sys_readlinkat | fs/stat.c |
| 268 | fchmodat | sys_fchmodat | fs/open.c |
| 269 | faccessat | sys_faccessat | fs/open.c |
| 270 | pselect6 | sys_pselect6 | fs/select.c |
| 271 | ppoll | sys_ppoll | fs/select.c |
| 272 | unshare | sys_unshare | kernel/fork.c |
| 273 | set_robust_list | sys_set_robust_list | kernel/futex.c |
| 274 | get_robust_list | sys_get_robust_list | kernel/futex.c |
| 275 | splice | sys_splice | fs/splice.c |
| 276 | tee | sys_tee | fs/splice.c |
| 277 | sync_file_range | sys_sync_file_range | fs/sync.c |
| 278 | vmsplice | sys_vmsplice | fs/splice.c |
| 279 | move_pages | sys_move_pages | mm/migrate.c |
| 280 | utimensat | sys_utimensat | fs/utimes.c |
| 281 | epoll_pwait | sys_epoll_pwait | fs/eventpoll.c |
| 282 | signalfd | sys_signalfd | fs/signalfd.c |
| 283 | timerfd_create | sys_timerfd_create | fs/timerfd.c |
| 284 | eventfd | sys_eventfd | fs/eventfd.c |
| 285 | fallocate | sys_fallocate | fs/open.c |
| 286 | timerfd_settime | sys_timerfd_settime | fs/timerfd.c |
| 287 | timerfd_gettime | sys_timerfd_gettime | fs/timerfd.c |
| 288 | accept4 | sys_accept4 | net/socket.c |
| 289 | signalfd4 | sys_signalfd4 | fs/signalfd.c |
| 290 | eventfd2 | sys_eventfd2 | fs/eventfd.c |
| 291 | epoll_create1 | sys_epoll_create1 | fs/eventpoll.c |
| 292 | dup3 | sys_dup3 | fs/file.c |
| 293 | pipe2 | sys_pipe2 | fs/pipe.c |
| 294 | inotify_init1 | sys_inotify_init1 | fs/notify/inotify/inotify_user.c |
| 295 | preadv | sys_preadv | fs/read_write.c |
| 296 | pwritev | sys_pwritev | fs/read_write.c |
| 297 | rt_tgsigqueueinfo | sys_rt_tgsigqueueinfo | kernel/signal.c |
| 298 | perf_event_open | sys_perf_event_open | kernel/events/core.c |
| 299 | recvmmsg | sys_recvmmsg | net/socket.c |
| 300 | fanotify_init | sys_fanotify_init | fs/notify/fanotify/fanotify_user.c |
| 301 | fanotify_mark | sys_fanotify_mark | fs/notify/fanotify/fanotify_user.c |
| 302 | prlimit64 | sys_prlimit64 | kernel/sys.c |
| 303 | name_to_handle_at | sys_name_to_handle_at | fs/fhandle.c |
| 304 | open_by_handle_at | sys_open_by_handle_at | fs/fhandle.c |
| 305 | clock_adjtime | sys_clock_adjtime | kernel/posix-timers.c |
| 306 | syncfs | sys_syncfs | fs/sync.c |
| 307 | sendmmsg | sys_sendmmsg | net/socket.c |
| 308 | setns | sys_setns | kernel/nsproxy.c |
| 309 | getcpu | sys_getcpu | kernel/sys.c |
| 310 | process_vm_readv | sys_process_vm_readv | mm/process_vm_access.c |
| 311 | process_vm_writev | sys_process_vm_writev | mm/process_vm_access.c |
| 312 | kcmp | sys_kcmp | kernel/kcmp.c |
| 313 | finit_module | sys_finit_module | kernel/module.c |

0
editor/vscode/LICENSE Normal file
View File

View File

@ -3,6 +3,10 @@
"displayName": "mclang",
"description": "Code highlighting for mclang",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "git@github.com:mc-lang/mclang2.git"
},
"engines": {
"vscode": "^1.54.0"
},

View File

@ -123,7 +123,7 @@
"patterns": [
{
"name": "variable.name.source.mclang",
"match": "(?<=^|\\s)(\\+|-|\\*|max|divmod|print|=|>|<|>=|<=|!=|>>|<<|\\||&|not|dup|swap|drop|over|rot|argc|argv|here|syscall0|syscall1|syscall2|syscall3|syscall4|syscall5|syscall6|\\?\\?\\?)(?=>$|\\s)"
"match": "(?<=^|\\s)(\\+|-|\\*|max|divmod|print|=|>|<|>=|<=|!=|>>|<<|\\||&|not|dup|swap|drop|over|rot|argc|argv|here|syscall0|syscall1|syscall2|syscall3|syscall4|syscall5|syscall6|mem|\\?\\?\\?)(?=>$|\\s)"
}
]
},

18
examples/hello_world.mcl Normal file
View File

@ -0,0 +1,18 @@
mem dup 72 @8
1 + dup 101 @8
1 + dup 110 @8
1 + dup 108 @8
1 + dup 111 @8
1 + dup 32 @8
1 + dup 119 @8
1 + dup 111 @8
1 + dup 114 @8
1 + dup 108 @8
1 + dup 100 @8
1 + dup 33 @8
1 + dup 10 @8
1 +
mem - mem 1 1 syscall3

1
examples/rule110.mcl Normal file
View File

@ -0,0 +1 @@
// TODO: Make this

5
examples/seq_100.mcl Normal file
View File

@ -0,0 +1,5 @@
0 while dup 100 < do
dup print
1 +
end

View File

@ -196,6 +196,77 @@ pub fn compile(tokens: Vec<Operator>, args: Args) -> Result<()>{
writeln!(writer, " jmp addr_{}", token.jmp)?;
ti += 1;
},
OpType::Syscall0 => {
writeln!(writer, " ; -- SYSCALL0")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall1 => {
writeln!(writer, " ; -- SYSCALL1")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall2 => {
writeln!(writer, " ; -- SYSCALL2")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " pop rsi")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall3 => {
writeln!(writer, " ; -- SYSCALL3")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " pop rsi")?;
writeln!(writer, " pop rdx")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall4 => {
writeln!(writer, " ; -- SYSCALL4")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " pop rsi")?;
writeln!(writer, " pop rdx")?;
writeln!(writer, " pop r10")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall5 => {
writeln!(writer, " ; -- SYSCALL5")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " pop rsi")?;
writeln!(writer, " pop rdx")?;
writeln!(writer, " pop r10")?;
writeln!(writer, " pop r8")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
OpType::Syscall6 => {
writeln!(writer, " ; -- SYSCALL6")?;
writeln!(writer, " pop rax")?;
writeln!(writer, " pop rdi")?;
writeln!(writer, " pop rsi")?;
writeln!(writer, " pop rdx")?;
writeln!(writer, " pop r10")?;
writeln!(writer, " pop r8")?;
writeln!(writer, " pop r9")?;
writeln!(writer, " syscall")?;
writeln!(writer, " push rax")?;
ti += 1;
},
}
}
writeln!(writer, "addr_{}:", ti)?;

View File

@ -17,7 +17,14 @@ pub enum OpType {
Do,
Mem,
Load8,
Store8
Store8,
Syscall0,
Syscall1,
Syscall2,
Syscall3,
Syscall4,
Syscall5,
Syscall6
}

View File

@ -1,7 +1,11 @@
use crate::constants::OpType;
use std::fmt::format;
use crate::{constants::OpType, util::logger};
// use crate::util::logger;
use color_eyre::Result;
mod syscalls;
fn stack_pop(stack: &mut Vec<u64>) -> Result<u64, &'static str> {
match stack.pop() {
Some(i) => Ok(i),
@ -12,7 +16,8 @@ fn stack_pop(stack: &mut Vec<u64>) -> Result<u64, &'static str> {
pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
let mut stack: Vec<u64> = Vec::new();
let mut ti = 0;
let mut mem: Vec<u64> = Vec::with_capacity(crate::compile::MEM_SZ as usize);
let mut mem: Vec<u8> = vec![0; crate::compile::MEM_SZ as usize];
while ti < tokens.len() {
let token = &tokens[ti];
@ -48,14 +53,14 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
OpType::Load8 => {
let a = stack_pop(&mut stack)?;
stack.push(mem[a as usize]);
stack.push(mem[a as usize] as u64);
ti += 1;
}
OpType::Store8 => {
let a = stack_pop(&mut stack)?;
let b = stack_pop(&mut stack)?;
mem[b as usize] = a;
mem[b as usize] = a as u8;
ti += 1;
}
@ -118,6 +123,46 @@ pub fn run(tokens: Vec<crate::constants::Operator>) -> Result<(), &'static str>{
OpType::End => {
ti = token.jmp as usize;
}
OpType::Syscall0 => {
todo!();
ti += 1;
},
OpType::Syscall1 => {
todo!();
ti += 1;
},
OpType::Syscall2 => {
todo!();
ti += 1;
},
OpType::Syscall3 => {
let rax = stack_pop(&mut stack)?;
let rdi = stack_pop(&mut stack)?;
let rsi = stack_pop(&mut stack)?;
let rdx = stack_pop(&mut stack)?;
let ret = match rax {
1 => syscalls::sys_write(rax, rdi, rsi, rdx, &mem),
_ => {
logger::error(format!("Syscall(3) #{} is not implemented", rax).as_str());
return Err("Exiting");
}
};
stack.push(ret);
ti += 1;
},
OpType::Syscall4 => {
todo!();
ti += 1;
},
OpType::Syscall5 => {
todo!();
ti += 1;
},
OpType::Syscall6 => {
todo!();
ti += 1;
},
}
}
Ok(())

View File

@ -0,0 +1,13 @@
pub fn sys_write(rax: u64, rdi: u64, rsi: u64, rdx: u64, mem: &Vec<u8> ) -> u64 {
for c in rsi..rdx {
match rdi {
1 => print!("{}", unsafe { char::from_u32_unchecked((mem[(rsi + c) as usize]) as u32) }),
2 => eprint!("{}", unsafe { char::from_u32_unchecked((mem[(rsi + c) as usize]) as u32) }),
_ => panic!("Unknown file {}", rdi)
};
let _ = std::io::Write::flush(&mut std::io::stdout());
let _ = std::io::Write::flush(&mut std::io::stderr());
}
rax
}

View File

@ -112,6 +112,14 @@ impl Parser {
"!8" => tokens.push(Operator::new(OpType::Load8, 0, token.file.clone(), token.line, token.col)),
"@8" => tokens.push(Operator::new(OpType::Store8, 0, token.file.clone(), token.line, token.col)),
"syscall0" => tokens.push(Operator::new(OpType::Syscall0, 0, token.file.clone(), token.line, token.col)),
"syscall1" => tokens.push(Operator::new(OpType::Syscall1, 0, token.file.clone(), token.line, token.col)),
"syscall2" => tokens.push(Operator::new(OpType::Syscall2, 0, token.file.clone(), token.line, token.col)),
"syscall3" => tokens.push(Operator::new(OpType::Syscall3, 0, token.file.clone(), token.line, token.col)),
"syscall4" => tokens.push(Operator::new(OpType::Syscall4, 0, token.file.clone(), token.line, token.col)),
"syscall5" => tokens.push(Operator::new(OpType::Syscall5, 0, token.file.clone(), token.line, token.col)),
"syscall6" => tokens.push(Operator::new(OpType::Syscall6, 0, token.file.clone(), token.line, token.col)),

View File

@ -1,3 +1,18 @@
mem 255 @8
mem !8 print
mem dup 72 @8
1 + dup 101 @8
1 + dup 110 @8
1 + dup 108 @8
1 + dup 111 @8
1 + dup 32 @8
1 + dup 119 @8
1 + dup 111 @8
1 + dup 114 @8
1 + dup 108 @8
1 + dup 100 @8
1 + dup 33 @8
1 + dup 10 @8
1 +
mem - mem 1 1 syscall3

3
test.py Normal file
View File

@ -0,0 +1,3 @@
txt = "TODO: Make this!\n"
sp = list(txt)
print([ord(x) for x in sp])