Revert "file.c: introduce output buffering for x86"

This reverts commit 5afeb0743e.
This commit is contained in:
Jeremiah Orians 2021-01-01 15:28:30 -05:00
parent 07baf02efe
commit cc1a17a040
No known key found for this signature in database
GPG Key ID: 6B3A3F198708F894
10 changed files with 57 additions and 174 deletions

View File

@ -19,139 +19,31 @@
// CONSTANT stdout 1
// CONSTANT stderr 2
// CONSTANT EOF 0xFFFFFFFF
//
// CONSTANT __FILE_BUFFER_SIZE 512
// CONSTANT __FILE_BUFMODE_EMPTY 0
// CONSTANT __FILE_BUFMODE_READ 1
// CONSTANT __FILE_BUFMODE_WRITE 2
void* calloc(int count, int size);
struct __fileinfo {
int fd;
int bufmode; /* 0 = empty, 1 = read, 2 = write */
int bufpos;
int buflen;
char* buffer;
};
struct __fileinfo** __file_std_handles = 0;
struct __fileinfo* __file_lookup_handle(FILE* f)
{
unsigned fno = f;
if (fno < 3) {
if (0 == __file_std_handles)
{
__file_std_handles = calloc(3, sizeof(struct __fileinfo*));
}
if (0 == __file_std_handles[fno])
{
__file_std_handles[fno] = calloc(1, sizeof(struct __fileinfo));
__file_std_handles[fno]->fd = fno;
__file_std_handles[fno]->buffer = calloc(__FILE_BUFFER_SIZE, sizeof(char));
}
return __file_std_handles[fno];
}
return f;
}
int syscall_read(int fd, char* buf, unsigned count) {
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %3"
"INT_80");
}
int syscall_write(int fd, char* buf, unsigned count) {
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %4"
"INT_80");
}
// CONSTANT SEEK_SET 0
// CONSTANT SEEK_CUR 1
// CONSTANT SEEK_END 2
int syscall_lseek(int fd, int offset, int whence)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %19"
"INT_80");
}
void __file_set_bufmode(struct __fileinfo* fi, int newmode)
{
if (fi->bufmode != newmode)
{
if (__FILE_BUFMODE_READ == fi->bufmode && fi->bufpos < fi->buflen)
{
syscall_lseek(fi->fd, fi->bufpos - fi->buflen, SEEK_CUR);
}
else if (__FILE_BUFMODE_WRITE == fi->bufmode && fi->buflen > 0)
{
syscall_write(fi->fd, fi->buffer, fi->buflen);
}
fi->bufmode = newmode;
fi->bufpos = 0;
fi->buflen = 0;
}
}
int fgetc(FILE* f)
{
struct __fileinfo* fi = __file_lookup_handle(f);
if (__FILE_BUFMODE_READ == fi->bufmode && fi->bufpos < fi->buflen)
{
fi->bufpos = fi->bufpos + 1;
return fi->buffer[fi->bufpos - 1];
}
__file_set_bufmode(fi, __FILE_BUFMODE_READ);
fi->buflen = syscall_read(fi->fd, fi->buffer, __FILE_BUFFER_SIZE);
if(0 < fi->buflen)
{
fi->bufpos = 1;
return fi->buffer[0];
}
else
{
fi->bufpos = 0;
return EOF;
}
asm("LOAD_IMMEDIATE_eax %3"
"LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"PUSH_ebx"
"COPY_esp_to_ecx"
"LOAD_IMMEDIATE_edx %1"
"INT_80"
"TEST"
"POP_eax"
"JUMP_NE8 !FUNCTION_fgetc_Done"
"LOAD_IMMEDIATE_eax %-1"
":FUNCTION_fgetc_Done");
}
void fputc(char s, FILE* f)
{
struct __fileinfo* fi = __file_lookup_handle(f);
__file_set_bufmode(fi, __FILE_BUFMODE_WRITE);
if (fi->buflen == __FILE_BUFFER_SIZE)
{
syscall_write(fi->fd, fi->buffer, fi->buflen);
fi->buflen = 0;
}
fi->buffer[fi->buflen] = s;
fi->buflen = fi->buflen + 1;
if (fi->fd < 3)
{
/* flush std streams immediately */
syscall_write(fi->fd, fi->buffer, fi->buflen);
fi->buflen = 0;
}
asm("LOAD_IMMEDIATE_eax %4"
"LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_IMMEDIATE_edx %1"
"INT_80");
}
/* Important values needed for open */
@ -169,7 +61,7 @@ void fputc(char s, FILE* f)
/* 00400 in octal is 256 */
// CONSTANT S_IRUSR 256
int open(char* name, int flag, int mode)
FILE* open(char* name, int flag, int mode)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
@ -183,7 +75,7 @@ int open(char* name, int flag, int mode)
FILE* fopen(char* filename, char* mode)
{
int f;
FILE* f;
if('w' == mode[0])
{ /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
f = open(filename, 577 , 384);
@ -198,13 +90,10 @@ FILE* fopen(char* filename, char* mode)
{
return 0;
}
struct __fileinfo* fi = calloc(1, sizeof(struct __fileinfo));
fi->buffer = calloc(__FILE_BUFFER_SIZE, sizeof(char));
fi->fd = f;
return fi;
return f;
}
int syscall_close(int fd)
int close(int fd)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
@ -214,24 +103,30 @@ int syscall_close(int fd)
int fclose(FILE* stream)
{
struct __fileinfo* fi = __file_lookup_handle(stream);
__file_set_bufmode(fi, __FILE_BUFMODE_EMPTY);
int error = syscall_close(fi->fd);
int error = close(stream);
return error;
}
int fflush(FILE *stream)
{
struct __fileinfo* fi = __file_lookup_handle(stream);
__file_set_bufmode(fi, __FILE_BUFMODE_EMPTY);
int fflush(FILE *stream){
/* We don't buffer, nothing to flush */
return 0;
}
// CONSTANT SEEK_SET 0
// CONSTANT SEEK_CUR 1
// CONSTANT SEEK_END 2
/* We just use lseek directly */
int fseek(FILE* f, long offset, int whence)
{
struct __fileinfo* fi = __file_lookup_handle(f);
__file_set_bufmode(fi, __FILE_BUFMODE_EMPTY);
return syscall_lseek(fi->fd, offset, whence);
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %19"
"INT_80");
}
void rewind(FILE* f)

View File

@ -88,15 +88,15 @@ a2a2308a472736f56530fc66016503f2ecda3072175728b9950baa42af588d0a test/results/t
9e731aeb4a7b9b65c83ef2e4c137c960bc371ddc28b172bfb55c2b7ccf9ffe35 test/results/test0014-knight-posix-binary
e783d1a24a892f326ef8166d9fd0bf318d93fa7112b244973569e4332d9c5f0d test/results/test0014-x86-binary
c060cca19b96fc6e31fd92a15527bfba428f41ff017900cc0ec7a2546c8e4277 test/results/test0015-aarch64-binary
987fd7780cbcf6940650035e8a9887fce77464506da6062ceafe00268a65b5b5 test/results/test0015-amd64-binary
0aeb06505099217e667c75fa2560c1669546d0114430ac728970b8d5015fc526 test/results/test0015-amd64-binary
faa2be23f7e0e4ad26b047f040a1d05701d2ea808e3c5b2ca8396df13ddac282 test/results/test0015-armv7l-binary
b200e867ecfec96b33425e67e06cc553b68b5ea5ff8c8331964d56264bcd7de1 test/results/test0015-knight-posix-binary
84277f601594d06cff12a7e4c5af500a7e3ad298dca1e7eb369834899e1fd512 test/results/test0015-x86-binary
053b8fbe43796a30af109b80313c257f3acf1c9bd5e5e699e7b996e99c26cf77 test/results/test0015-x86-binary
580e15ab50b46e00c46b64b028acec2ecb0fa45c356fdcccf40eda8943869d68 test/results/test0016-aarch64-binary
c958d7552b737d9c5f085f1f26b2f8ea02989194a1ca45387cd6258639535f37 test/results/test0016-amd64-binary
a392314cece371cfd14548a80ebcffb946d5fb5976a4232930d0cc6808d87044 test/results/test0016-amd64-binary
853dcdc7389018f8ea01c7e8255b5b3e82ab999049753bd33bcc3c16ac697c4e test/results/test0016-armv7l-binary
9d47ad6f4124521063c0894e5219871ed0561320ba52910c58102bb4cea2b820 test/results/test0016-knight-posix-binary
24a733d7d6c9c59b5a6ea0463ee724f6af169f58fc9b63beb906e097fa269989 test/results/test0016-x86-binary
e4875f981f3b51627a2b17bd25f8f316bc37f8428263ebaeb5641e45b7def712 test/results/test0016-x86-binary
ca186d12b91ad4f89141c05345680fe83a161766ba4bb49ff0ea28bde77e89b1 test/results/test0017-aarch64-binary
bd70143ec9764bd5681c72e173b6baaca7a486c9330974ca82fb7e997249f572 test/results/test0017-amd64-binary
4618cba0ac8a2aa09c434388be9e2275cfb512c457a9f10e2ec64c06a3f4cf2c test/results/test0017-armv7l-binary
@ -108,12 +108,12 @@ bdb95323821eea5b757445d1d4a8f79256a4b665c2a0666cbbb7931550ba4a6f test/results/t
2ad21d7b2536304f9effd97d8614036b0d453a10ba4da3c09b490d6447271a42 test/results/test0018-armv7l-binary
f31fbd190e6dceda05b33a71692926845127f4f73eab08a143a17b5f2611e6e3 test/results/test0018-knight-native-binary
f3b5326969cb68a0f8e4e3e138af24e70114510dda2ad5a7cc27e608e4aecc9a test/results/test0018-knight-posix-binary
ae449ca1f31d286ce4d7b531993b53d9ccb0dacbd113d544acb7ccdc829a9799 test/results/test0018-x86-binary
ef7af7ff1109f2b99709664e0f7453da42336bf6c7b13aa3911315647dd59fb3 test/results/test0018-x86-binary
19a5ae1de1a04ca9ac77c8271a7edb30bc8f0e75e1fbb3992127c56effaab3d4 test/results/test0019-aarch64-binary
d7a708d2a02f49ba61c52f0fa77f488e0f8d9333f2b0c7f3212d3e94fee1dd45 test/results/test0019-amd64-binary
35f88e82c2ff88176074ec812f1fdde7cf83a8940bd1dc5cabf8db8bd6793004 test/results/test0019-armv7l-binary
1e11972b0addb19aa820bc912a4412e430cc87e67dcf8ddd1ab441f142dd1302 test/results/test0019-knight-posix-binary
b474385874c58e3b9cb80d33f6a5248b4798c2e8c7d5e6c2d94b24c3debab56a test/results/test0019-x86-binary
18d99f572e164f1fb699bdea0dd8f33dfaa1a82b283dbf8a6f7553b6881aa143 test/results/test0019-x86-binary
6b31e8ee54270d38a1801f7614ae6247f59d44a2758f344b07a72794019290f4 test/results/test0020-aarch64-binary
6b2956b106dd16bb55d8d011b48e674ee3b70bbb0bb83efadca01d446666e21e test/results/test0020-amd64-binary
b29aca7f0b63659915fe431e290f821cf17071983613021aacb8985d376bb206 test/results/test0020-armv7l-binary
@ -124,45 +124,45 @@ baa510d2b782c31d1c9edd402271e2ddf00542aefd6f4780b6975db5a1665e0c test/results/t
f10d5378c91d716270ca8e876ed1acb0eb68b48a6e04d5d5e39b22dd4e3bf98c test/results/test0021-amd64-binary
a9c09864fd326b9b42bc5a32dc32d248dbfcfa87ccdd97cfd21ee95af39a3db9 test/results/test0021-armv7l-binary
0715ba9a1b0d6bb862fbd6ff11526455d2a7e85f4c38b7a021598701e3cf14f1 test/results/test0021-knight-posix-binary
a15eb906ec578aa930ad884a62668f71a8b0572b8a7f7122f1701af8e544e6b5 test/results/test0021-x86-binary
fde9fb7fd3a1a19a8b1f4451cbdb98901adb6de468cd65bda2beb11408233b86 test/results/test0021-x86-binary
14b9a108bdee811c0e9ff3f1be1299767a0d8b49319efbfd9b5f269bf5a057ec test/results/test0022-aarch64-binary
1d47282bf5d8bc97bfa34ae1762e8cbb4d74001c8dbbbd0cb6e6ec285e003c1d test/results/test0022-amd64-binary
2d63c3a5a2c5b5ae2ea2e93c430027b4b418e229a963c66b9a3bc34307a55eba test/results/test0022-armv7l-binary
a23a357cabc995330776fa01c7cf41978b100c36b0e3c76d1297db0e953c1bc3 test/results/test0022-knight-posix-binary
9d6b943006a2d56d6702b9695e5866f024298e06ed7d32b526c335d9b44c8d8c test/results/test0022-x86-binary
2f59be9ef800c4f84f1f39ac8765e3aae1b48de73cbd142ea2571da4670cfc6c test/results/test0022-x86-binary
835928a11aae4288a665818f052f8149aca9ed5f420e0dfe7cf2cd33efdfc8d8 test/results/test0023-aarch64-binary
2dd1e5fc9c55ba3fa4af837cbfdd4203d90f84ff9b122854f8606db9f421b9e5 test/results/test0023-amd64-binary
0c31258687156c27e6792d6bea30b36f79d2626784b928869cb827a28abf6e31 test/results/test0023-amd64-binary
d7b2ad248521de84f3b4d8194ab6b330f3e5fda0043368f57be0d30f9949172b test/results/test0023-armv7l-binary
f793452848e538381213923ecbd209253fabe391bd56c03f4f95d3ce1c4d5982 test/results/test0023-knight-posix-binary
6f7bfca720543c691dfa26e6a675333868291fd0a64175234c62fc6fdccdac11 test/results/test0023-x86-binary
b86fc421e50fd551ab3d6296551b3ed03821f6f134906575d932516b26a40285 test/results/test0023-x86-binary
65a25f4ee09e7ad4a0c44f6dad7e5a64aa1e62ac98d8f8d58d91a8b3a34ebe00 test/results/test0100-aarch64-binary
4d4d7dc249a4fc1788f4443a7a5feba51392a73dc1b80297a510cbca41afd71e test/results/test0100-amd64-binary
98ebb4ac496ed255d769383d027b0bf358f280b248a0994b30c047d2d5506ec4 test/results/test0100-armv7l-binary
7de66384bcf268d71f780853690e90cd12daebbb9227d3f0af7f51238185c690 test/results/test0100-knight-posix-binary
149131e095f7ed477776dde44daffc7df7e477c9ed56c63a5bffe54e02317bb7 test/results/test0100-x86-binary
a783829261dd45186da513e04bb727b6647e5712ccd11e8b19c33a286cbfc975 test/results/test0100-x86-binary
24199867cd2dea9b68c5df1499e220f08ac97eb09debd3b3621fb80ae588eb71 test/results/test0101-aarch64-binary
70ab6a4a4bd9d973ab058af976d99acfb2085be7343357c199281afbd85ac2f9 test/results/test0101-amd64-binary
252237eaaa9940b65aaf82a3667e8c59ccb78222c58e0b66b9a1dff6ee2e72e8 test/results/test0101-armv7l-binary
d120df140cc77037e3d16d1aee7482d270f55660a594c19be91cddc66de687f7 test/results/test0101-knight-posix-binary
018d8151abdec152377a64a6c8a00fadee9c3ffc448c9d49cdae7a6e798a5da7 test/results/test0101-x86-binary
a3e1762201e06c0dc6f26162abb88777f8e948de0df39dc66b39bb5141c4b44f test/results/test0101-x86-binary
bdd6707d00c4798e9ff9ccd8cc30c8d747972c902f2df566463cfb8e1a248c8e test/results/test0102-aarch64-binary
9f93061b9a70cde84865c36e49a6cb0b36b29c978d8db77b77a784887b3e89c9 test/results/test0102-amd64-binary
9b3e49b90e387c5f66160558b0bcce3cb0ce077ab4b35848a1b0809118b0d1b4 test/results/test0102-armv7l-binary
bf72fdc0514c83b93bf15f383a20b04f8ffd8c8b6c59b79c2963311b0657c6e9 test/results/test0102-knight-posix-binary
739d1173e98350138f7a2bf82c05eae94868d9cde5349ed258c8e8c2170b9179 test/results/test0102-x86-binary
0798274eba6646c6552207c4fa069483f961ef9b5dda8532bb6680461e3e91ee test/results/test0102-x86-binary
603fd4fe17f8ef9eac12116003941702849b720021da23dc32582ca41192a792 test/results/test0103-aarch64-binary
f2114b5217c12952a85d580ad5914dd679888d93d176ee132ace9e8773916b3b test/results/test0103-amd64-binary
79cbb69a747b07d729db736bc177b52b344106387831a0210ff18fce92edf1cc test/results/test0103-armv7l-binary
e0e387777249ec8abb841271544a74361207a4c347d304f87a1e5c5d22353a7a test/results/test0103-knight-posix-binary
0c2d649cc31421403d4b0a60d5358e21ad6a5385b79a83199c78ca7d1c828d06 test/results/test0103-x86-binary
e96a60d11b7e8261100b4f1f87c85b1c2432b43e4520fb4909e7cdafcae69b7c test/results/test0103-x86-binary
6f790c101e6c6c257726ef453511e517b2380331712766f9c8dd0d432fee4012 test/results/test0104-aarch64-binary
c579a2dcf41b5ece5700c08e73d78dac9cf46652f3b4bea8063c621a316b8bae test/results/test0104-amd64-binary
846ab4a92ab210a1bed125420fc9d5a92f44d747e24d7364ba0b21e30aeb8a2b test/results/test0104-armv7l-binary
d5f1a6e0848cbf76b3ff46976ea3637b7b9a61dce1833f9ccac457c5450fb9d0 test/results/test0104-x86-binary
39d3040403bc89dca901377115617d0caa4fb4c4cbee121e3d287cd3f1e2ace7 test/results/test0104-x86-binary
b7de190073aa310fbf0b1ffdb6200715e058bb094d07c72975b9fbf24a947197 test/results/test0105-aarch64-binary
ae9d5a82037664a104999a2625d5a66bcf7530a28c2b2b0aea66e8b38c8f775a test/results/test0105-amd64-binary
7ae287efefe4abeb9db6730da8c4db9273475b9666371bc4ea6c58c7e0486b74 test/results/test0105-armv7l-binary
523a747ec4ade1ec5511feaabc075279462303dbff591d26b34ada9491ebff70 test/results/test0105-x86-binary
82ce98e1fd70b8e3c0cf962b475ff4a296114386c0284cf8b7b0b7a03cb58ae7 test/results/test0105-x86-binary
682b63f332ee299bf4afb91356004b96a223e309dc44a7efeb1cbcd9f21aeb4c test/results/test0106-aarch64-binary
503b06b04ca1c5452489e20ff6b5288e592d74f312515246e6d992d2cb0f36fc test/results/test0106-amd64-binary
a2a83f42119e646b389b98647cf6cf2aa9597185997c9453db746178c8c4c0bf test/results/test0106-armv7l-binary
@ -173,4 +173,4 @@ a2a83f42119e646b389b98647cf6cf2aa9597185997c9453db746178c8c4c0bf test/results/t
8fc961311dcb3e373788e9c7cf502fef181732f57f7e5e34867216718d8ad9ed test/results/test1000-amd64-binary
5a5c427b932fc46d06a600672cff96113c23dc2a71d746606eceadb1aef1e2a9 test/results/test1000-armv7l-binary
b2b9eacf785e498e86c358cfd87834016fc0b8c5ee6efa625bd5a2c9ffb4e1a1 test/results/test1000-knight-posix-binary
1905036e649101ba7d9cf135c7f376eb860439ec81bb68f516b8133ee5de2ddb test/results/test1000-x86-binary
88c8e6465f4f618da3906a00371faac872c1b91d24462ecf20482876b77bd40f test/results/test1000-x86-binary

View File

@ -18,8 +18,6 @@
set -ex
# Build the test
bin/M2-Planet --architecture amd64 -f test/common_amd64/functions/file.c \
-f test/common_amd64/functions/malloc.c \
-f functions/calloc.c \
-f test/common_amd64/functions/putchar.c \
-f test/test0015/file_read.c \
-o test/test0015/file_read.M1 || exit 1

View File

@ -18,8 +18,6 @@
set -ex
# Build the test
bin/M2-Planet --architecture x86 -f test/common_x86/functions/file.c \
-f test/common_x86/functions/malloc.c \
-f functions/calloc.c \
-f test/common_x86/functions/putchar.c \
-f test/test0015/file_read.c \
-o test/test0015/file_read.M1 || exit 1

View File

@ -18,8 +18,6 @@
set -ex
# Build the test
bin/M2-Planet --architecture amd64 -f test/common_amd64/functions/file.c \
-f test/common_amd64/functions/malloc.c \
-f functions/calloc.c \
-f test/common_amd64/functions/putchar.c \
-f test/test0016/file_write.c \
-o test/test0016/file_write.M1 || exit 1

View File

@ -18,8 +18,6 @@
set -ex
# Build the test
bin/M2-Planet --architecture x86 -f test/common_x86/functions/file.c \
-f test/common_x86/functions/malloc.c \
-f functions/calloc.c \
-f test/common_x86/functions/putchar.c \
-f test/test0016/file_write.c \
-o test/test0016/file_write.M1 || exit 1

View File

@ -76,7 +76,7 @@ int main()
getcwd(current_path, MAX_STRING);
/* Test fchdir works */
int fchdir_fd = open(prepend_string(base_path, "/test/test0021"), 0, 0);
FILE* fchdir_fd = open(prepend_string(base_path, "/test/test0021"), 0, 0);
int fchdir_rc = fchdir(fchdir_fd);
if(fchdir_rc != 0)
{

View File

@ -19,8 +19,6 @@ set -x
# Build the test
bin/M2-Planet --architecture amd64 \
-f test/common_amd64/functions/file.c \
-f test/common_amd64/functions/malloc.c \
-f functions/calloc.c \
-f test/test0023/fseek.c \
--debug \
-o test/test0023/fseek.M1 || exit 1

View File

@ -19,8 +19,6 @@ set -x
# Build the test
bin/M2-Planet --architecture x86 \
-f test/common_x86/functions/file.c \
-f test/common_x86/functions/malloc.c \
-f functions/calloc.c \
-f test/test0023/fseek.c \
--debug \
-o test/test0023/fseek.M1 || exit 1

View File

@ -1 +1 @@
fce731f1c0914873c1d882c45fa7c606673e923504b40812231c282a02cfddd9 test/test1000/proof
a5f0e537e84c2db97fafb72865431f778abb3f189de196af00222d90f940c56d test/test1000/proof