...
This commit is contained in:
2
src/runtime/.gitignore
vendored
2
src/runtime/.gitignore
vendored
@@ -1 +1 @@
|
||||
runtime.o
|
||||
*.o
|
||||
@@ -1,5 +1,5 @@
|
||||
runtime:
|
||||
cc -c runtime.c
|
||||
x86_64:
|
||||
gcc -nostdlib -ffreestanding -c platform/x86_64.s runtime.c
|
||||
|
||||
clean:
|
||||
rm runtime.o
|
||||
|
||||
17
src/runtime/platform/x86_64.s
Normal file
17
src/runtime/platform/x86_64.s
Normal file
@@ -0,0 +1,17 @@
|
||||
.intel_syntax noprefix
|
||||
|
||||
.text
|
||||
.globl nub_write_string
|
||||
# void nub_write_string(const char* str)
|
||||
nub_write_string:
|
||||
mov rsi, rdi
|
||||
mov rdi, 1
|
||||
mov rax, 1
|
||||
syscall
|
||||
|
||||
.text
|
||||
.globl nub_exit
|
||||
# void nub_exit(int code)
|
||||
nub_exit:
|
||||
mov rax, 60
|
||||
syscall
|
||||
@@ -1,16 +1,17 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <syscall.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define NUB_PANIC_ERROR_CODE 101
|
||||
|
||||
uint64_t nub_cstring_length(char* string)
|
||||
extern void nub_write_string(const char* str);
|
||||
extern void nub_exit(int code);
|
||||
|
||||
uint64_t nub_cstring_length(const char* string)
|
||||
{
|
||||
return strlen(string);
|
||||
uint64_t len = 0;
|
||||
while (string[len] != '\0') {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
uint64_t nub_string_length()
|
||||
@@ -18,32 +19,37 @@ uint64_t nub_string_length()
|
||||
return 0;
|
||||
}
|
||||
|
||||
void nub_memcpy(void* source, void* destination, uint64_t length)
|
||||
void nub_memcpy(uint8_t* source, uint8_t* destination, uint64_t length)
|
||||
{
|
||||
memcpy(destination, source, length);
|
||||
for (uint64_t i = 0; i < length; i++) {
|
||||
destination[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
void nub_memset(void* destination, int value, uint64_t count)
|
||||
void nub_memset(uint8_t* destination, uint8_t value, uint64_t count)
|
||||
{
|
||||
memset(destination, value, count);
|
||||
for (uint64_t i = 0; i < count; i++) {
|
||||
destination[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void nub_panic_array_oob()
|
||||
{
|
||||
puts("Array out of bounds");
|
||||
exit(NUB_PANIC_ERROR_CODE);
|
||||
nub_write_string("Array out of bounds\n");
|
||||
nub_exit(NUB_PANIC_ERROR_CODE);
|
||||
}
|
||||
|
||||
void nub_panic_oom()
|
||||
{
|
||||
puts("Out of memory");
|
||||
exit(NUB_PANIC_ERROR_CODE);
|
||||
nub_write_string("Out of memory\n");
|
||||
nub_exit(NUB_PANIC_ERROR_CODE);
|
||||
}
|
||||
|
||||
extern int64_t main();
|
||||
extern int main();
|
||||
|
||||
void _start()
|
||||
__attribute__((noreturn)) void _start()
|
||||
{
|
||||
int64_t exit_code = main();
|
||||
exit(exit_code);
|
||||
}
|
||||
int exit_code = main();
|
||||
nub_exit(exit_code);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user