Skip to content

Commit

Permalink
Rewrite inefficient risc-v assembly.
Browse files Browse the repository at this point in the history
  • Loading branch information
stikonas committed Oct 21, 2021
1 parent b58dbe1 commit ff549d1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 144 deletions.
37 changes: 10 additions & 27 deletions riscv64/Linux/bootstrap.c
Expand Up @@ -29,9 +29,7 @@

int fgetc(FILE* f)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RS1_SP RS2_A0 SD"
"RD_A1 RS1_SP MV"
"RD_A2 !1 ADDI"
Expand All @@ -46,11 +44,8 @@ int fgetc(FILE* f)

void fputc(char s, FILE* f)
{
asm("RD_A0 RS1_FP MV"
"RD_A1 RS1_A0 !-8 ADDI"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-16 LD"
"RD_A1 RS1_FP !-8 ADDI"
"RD_A2 !1 ADDI" /* 1 byte */
"RD_A7 !64 ADDI" /* write */
"ECALL");
Expand All @@ -67,16 +62,10 @@ void fputs(char* s, FILE* f)

FILE* open(char* name, int flag, int mode)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A3 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A3 RS1_FP !-24 LD"
"RD_A7 !56 ADDI" /* openat */
"ECALL");
}
Expand All @@ -103,9 +92,7 @@ FILE* fopen(char* filename, char* mode)

int close(int fd)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !57 ADDI" /* close */
"ECALL");
}
Expand All @@ -118,9 +105,7 @@ int fclose(FILE* stream)

int brk(void *addr)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !214 ADDI" /* brk */
"ECALL");
}
Expand Down Expand Up @@ -179,9 +164,7 @@ void free(void* l)

void exit(int value)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !93 ADDI" /* exit */
"ECALL");
}
14 changes: 4 additions & 10 deletions riscv64/Linux/fcntl.h
Expand Up @@ -28,16 +28,10 @@

int open(char* name, int flag, int mode)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A3 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A3 RS1_FP !-24 LD"
"RD_A7 !56 ADDI"
"ECALL");
}
Expand Down
48 changes: 14 additions & 34 deletions riscv64/Linux/sys/stat.h
Expand Up @@ -37,68 +37,48 @@

int chmod(char *pathname, int mode)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A7 !53 ADDI"
"ECALL");
}


int fchmod(int a, mode_t b)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A7 !52 ADDI"
"ECALL");
}


int mkdir(char const* a, mode_t b)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A7 !34 ADDI"
"ECALL");
}


int mknod(char const* a, mode_t b, dev_t c)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A3 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A3 RS1_FP !-24 LD"
"RD_A7 !33 ADDI"
"ECALL");
}


mode_t umask(mode_t m)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !166 ADDI"
"ECALL");
}
98 changes: 25 additions & 73 deletions riscv64/Linux/unistd.h
Expand Up @@ -22,32 +22,24 @@ void* malloc(unsigned size);

int access(char* pathname, int mode)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 !-100 ADDI" /* AT_FDCWD */
asm("RD_A0 !-100 ADDI" /* AT_FDCWD */
"RD_A1 RS1_FP !-8 LD"
"RD_A2 RS1_FP !-16 LD"
"RD_A3 ADDI" /* flags = 0 */
"RD_A7 !48 ADDI"
"ECALL");
}

int chdir(char* path)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !49 ADDI"
"ECALL");
}

int fchdir(int fd)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !50 ADDI"
"ECALL");
}
Expand All @@ -66,15 +58,9 @@ int fork()
int waitpid (int pid, int* status_ptr, int options)
{
/* Uses wait4 with struct rusage *ru set to NULL */
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A2 RS1_FP !-24 LD"
"RD_A3 ADDI"
"RD_A7 !260 ADDI"
"ECALL");
Expand All @@ -83,83 +69,53 @@ int waitpid (int pid, int* status_ptr, int options)

int execve(char* file_name, char** argv, char** envp)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A2 RS1_FP !-24 LD"
"RD_A7 !221 ADDI"
"ECALL");
}

int read(int fd, char* buf, unsigned count)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A2 RS1_FP !-24 LD"
"RD_A7 !63 ADDI"
"ECALL");
}

int write(int fd, char* buf, unsigned count)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A2 RS1_FP !-24 LD"
"RD_A7 !64 ADDI"
"ECALL");
}

int lseek(int fd, int offset, int whence)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-24 ADDI"
"RD_A2 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A2 RS1_FP !-24 LD"
"RD_A7 !62 ADDI"
"ECALL");
}


int close(int fd)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !57 ADDI" /* close */
"ECALL");
}


int _getcwd(char* buf, int size)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-16 ADDI"
"RD_A1 RS1_A0 LD"
"RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A1 RS1_FP !-16 LD"
"RD_A7 !17 ADDI"
"ECALL");
}
Expand Down Expand Up @@ -187,9 +143,7 @@ char* get_current_dir_name()

int brk(void *addr)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !214 ADDI"
"ECALL");
}
Expand All @@ -205,9 +159,7 @@ struct utsname

int uname(struct utsname* unameData)
{
asm("RD_A0 RS1_FP MV"
"RD_A0 RS1_A0 !-8 ADDI"
"RD_A0 RS1_A0 LD"
asm("RD_A0 RS1_FP !-8 LD"
"RD_A7 !160 ADDI"
"ECALL");
}

0 comments on commit ff549d1

Please sign in to comment.