1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* Copyright (C) 2016 Jeremiah Orians
* This file is part of M2-Planet.
*
* M2-Planet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* M2-Planet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
*/
// CONSTANT stdin 0
// CONSTANT stdout 1
// CONSTANT stderr 2
// CONSTANT EOF 0xFFFFFFFF
// CONSTANT NULL 0
// CONSTANT EXIT_FAILURE 1
// CONSTANT EXIT_SUCCESS 0
// CONSTANT TRUE 1
// CONSTANT FALSE 0
int fgetc(FILE* f)
{
asm("lea_rdi,[rsp+DWORD] %8"
"mov_rdi,[rdi]"
"mov_rax, %0"
"push_rax"
"lea_rsi,[rsp+DWORD] %0"
"mov_rdx, %1"
"syscall"
"mov_rbx, %0"
"cmp_rbx,rax"
"pop_rax"
"jne %FUNCTION_fgetc_Done"
"mov_rax, %-1"
":FUNCTION_fgetc_Done");
}
void fputc(char s, FILE* f)
{
asm("mov_rax, %1"
"lea_rdi,[rsp+DWORD] %8"
"mov_rdi,[rdi]"
"lea_rsi,[rsp+DWORD] %16"
"mov_rdx, %1"
"syscall");
}
void fputs(char* s, FILE* f)
{
while(0 != s[0])
{
fputc(s[0], f);
s = s + 1;
}
}
FILE* open(char* name, int flag, int mode)
{
asm("lea_rdi,[rsp+DWORD] %24"
"mov_rdi,[rdi]"
"lea_rsi,[rsp+DWORD] %16"
"mov_rsi,[rsi]"
"lea_rdx,[rsp+DWORD] %8"
"mov_rdx,[rdx]"
"mov_rax, %2"
"syscall");
}
FILE* fopen(char* filename, char* mode)
{
FILE* f;
if('w' == mode[0])
{ /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
f = open(filename, 577 , 384);
}
else
{ /* Everything else is a read */
f = open(filename, 0, 0);
}
/* Negative numbers are error codes */
if(0 > f)
{
return 0;
}
return f;
}
int close(int fd)
{
asm("lea_rdi,[rsp+DWORD] %8"
"mov_rdi,[rdi]"
"mov_rax, %3"
"syscall");
}
int fclose(FILE* stream)
{
int error = close(stream);
return error;
}
int brk(void *addr)
{
asm("mov_rax,[rsp+DWORD] %8"
"push_rax"
"mov_rax, %12"
"pop_rbx"
"mov_rdi,rbx"
"syscall");
}
long _malloc_ptr;
long _brk_ptr;
void* malloc(int size)
{
if(NULL == _brk_ptr)
{
_brk_ptr = brk(0);
_malloc_ptr = _brk_ptr;
}
if(_brk_ptr < _malloc_ptr + size)
{
_brk_ptr = brk(_malloc_ptr + size);
if(-1 == _brk_ptr) return 0;
}
long old_malloc = _malloc_ptr;
_malloc_ptr = _malloc_ptr + size;
return old_malloc;
}
int strlen(char* str )
{
int i = 0;
while(0 != str[i]) i = i + 1;
return i;
}
void* memset(void* ptr, int value, int num)
{
char* s;
for(s = ptr; 0 < num; num = num - 1)
{
s[0] = value;
s = s + 1;
}
}
void* calloc(int count, int size)
{
void* ret = malloc(count * size);
if(NULL == ret) return NULL;
memset(ret, 0, (count * size));
return ret;
}
void free(void* l)
{
return;
}
void exit(int value)
{
asm("pop_rbx"
"pop_rdi"
"mov_rax, %0x3C"
"syscall");
}
|