Enabled forgotten cleanup scripts and add logic to catch ?alloc errors

This commit is contained in:
Jeremiah Orians 2020-01-25 14:15:45 -05:00
parent 4601fbe0e0
commit 0709770730
No known key found for this signature in database
GPG Key ID: 5410E91C14959E87
11 changed files with 40 additions and 17 deletions

4
cc.c
View File

@ -25,13 +25,11 @@ struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* fi
struct token_list* reverse_list(struct token_list* head); struct token_list* reverse_list(struct token_list* head);
void program(); void program();
void recursive_output(struct token_list* i, FILE* out); void recursive_output(struct token_list* i, FILE* out);
int match(char* a, char* b);
void file_print(char* s, FILE* f);
char* parse_string(char* string);
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
hold_string = calloc(MAX_STRING, sizeof(char)); hold_string = calloc(MAX_STRING, sizeof(char));
require(NULL != hold_string, "Impossible Exhustion has occured\n");
int DEBUG = FALSE; int DEBUG = FALSE;
FILE* in = stdin; FILE* in = stdin;
FILE* destination_file = stdout; FILE* destination_file = stdout;

8
cc.h
View File

@ -38,11 +38,13 @@
#define ARMV7L 4 #define ARMV7L 4
void file_print(char* s, FILE* f);
int match(char* a, char* b);
char* copy_string(char* target, char* source); char* copy_string(char* target, char* source);
void reset_hold_string();
int in_set(int c, char* s); int in_set(int c, char* s);
int match(char* a, char* b);
void file_print(char* s, FILE* f);
void require(int bool, char* error);
void reset_hold_string();
struct type struct type
{ {

View File

@ -49,6 +49,7 @@ void require(int bool, char* error);
struct token_list* emit(char *s, struct token_list* head) struct token_list* emit(char *s, struct token_list* head)
{ {
struct token_list* t = calloc(1, sizeof(struct token_list)); struct token_list* t = calloc(1, sizeof(struct token_list));
require(NULL != t, "Exhusted memory while generating token to emit\n");
t->next = head; t->next = head;
t->s = s; t->s = s;
return t; return t;
@ -73,6 +74,7 @@ void uniqueID_out(char* s, char* num)
struct token_list* sym_declare(char *s, struct type* t, struct token_list* list) struct token_list* sym_declare(char *s, struct type* t, struct token_list* list)
{ {
struct token_list* a = calloc(1, sizeof(struct token_list)); struct token_list* a = calloc(1, sizeof(struct token_list));
require(NULL != a, "Exhusted memory while attempting to declare a symbol\n");
a->next = list; a->next = list;
a->s = s; a->s = s;
a->type = t; a->type = t;

View File

@ -17,10 +17,6 @@
#include "cc.h" #include "cc.h"
/* imported functions */
int in_set(int c, char* s);
void require(int bool, char* error);
/* Globals */ /* Globals */
FILE* input; FILE* input;
struct token_list* token; struct token_list* token;
@ -106,6 +102,7 @@ void reset_hold_string()
int get_token(int c) int get_token(int c)
{ {
struct token_list* current = calloc(1, sizeof(struct token_list)); struct token_list* current = calloc(1, sizeof(struct token_list));
require(NULL != current, "Exhusted memory while getting token\n");
reset: reset:
reset_hold_string(); reset_hold_string();
@ -173,6 +170,7 @@ reset:
/* More efficiently allocate memory for string */ /* More efficiently allocate memory for string */
current->s = calloc(string_index + 2, sizeof(char)); current->s = calloc(string_index + 2, sizeof(char));
require(NULL != current->s, "Exhusted memory while trying to copy a token\n");
copy_string(current->s, hold_string); copy_string(current->s, hold_string);
current->prev = token; current->prev = token;

View File

@ -126,6 +126,7 @@ collect_regular_string_reset:
hold_string[string_index] = '"'; hold_string[string_index] = '"';
hold_string[string_index + 1] = '\n'; hold_string[string_index + 1] = '\n';
char* message = calloc(string_index + 3, sizeof(char)); char* message = calloc(string_index + 3, sizeof(char));
require(NULL != message, "Exhusted memory while storing regular string\n");
copy_string(message, hold_string); copy_string(message, hold_string);
reset_hold_string(); reset_hold_string();
return message; return message;
@ -163,6 +164,7 @@ collect_weird_string_reset:
hold_string[string_index + 4] = '\n'; hold_string[string_index + 4] = '\n';
char* hold = calloc(string_index + 6, sizeof(char)); char* hold = calloc(string_index + 6, sizeof(char));
require(NULL != hold, "Exhusted available memory while attempting to collect a weird string\n");
copy_string(hold, hold_string); copy_string(hold, hold_string);
reset_hold_string(); reset_hold_string();
return hold; return hold;

View File

@ -31,6 +31,7 @@ void initialize_types()
/* Define void */ /* Define void */
global_types = calloc(1, sizeof(struct type)); global_types = calloc(1, sizeof(struct type));
require(NULL != global_types, "Exhusted memory while intializing VOID\n");
global_types->name = "void"; global_types->name = "void";
global_types->is_signed = FALSE; global_types->is_signed = FALSE;
global_types->size = register_size; global_types->size = register_size;
@ -38,8 +39,9 @@ void initialize_types()
/* void* has the same properties as void */ /* void* has the same properties as void */
global_types->indirect = global_types; global_types->indirect = global_types;
/* Define UNis_signed LONG */ /* Define unsigned LONG */
struct type* a = calloc(1, sizeof(struct type)); struct type* a = calloc(1, sizeof(struct type));
require(NULL != a, "Exhusted memory while intializing SCM\n");
a->name = "SCM"; a->name = "SCM";
a->is_signed = FALSE; a->is_signed = FALSE;
a->size = register_size; a->size = register_size;
@ -48,6 +50,7 @@ void initialize_types()
/* Define LONG */ /* Define LONG */
struct type* b = calloc(1, sizeof(struct type)); struct type* b = calloc(1, sizeof(struct type));
require(NULL != b, "Exhusted memory while intializing LONG\n");
b->name = "long"; b->name = "long";
b->is_signed = TRUE; b->is_signed = TRUE;
b->size = register_size; b->size = register_size;
@ -56,6 +59,7 @@ void initialize_types()
/* Define UNSIGNED */ /* Define UNSIGNED */
struct type* c = calloc(1, sizeof(struct type)); struct type* c = calloc(1, sizeof(struct type));
require(NULL != c, "Exhusted memory while intializing UNSIGNE\n");
c->name = "unsigned"; c->name = "unsigned";
c->is_signed = FALSE; c->is_signed = FALSE;
c->size = register_size; c->size = register_size;
@ -65,6 +69,7 @@ void initialize_types()
/* Define int */ /* Define int */
struct type* d = calloc(1, sizeof(struct type)); struct type* d = calloc(1, sizeof(struct type));
require(NULL != d, "Exhusted memory while intializing INT\n");
d->name = "int"; d->name = "int";
d->is_signed = TRUE; d->is_signed = TRUE;
d->size = register_size; d->size = register_size;
@ -74,6 +79,7 @@ void initialize_types()
/* Define char* */ /* Define char* */
struct type* e = calloc(1, sizeof(struct type)); struct type* e = calloc(1, sizeof(struct type));
require(NULL != e, "Exhusted memory while intializing CHAR*\n");
e->name = "char*"; e->name = "char*";
e->is_signed = FALSE; e->is_signed = FALSE;
e->size = register_size; e->size = register_size;
@ -81,6 +87,7 @@ void initialize_types()
/* Define char */ /* Define char */
struct type* f = calloc(1, sizeof(struct type)); struct type* f = calloc(1, sizeof(struct type));
require(NULL != f, "Exhusted memory while intializing CHAR\n");
f->name = "char"; f->name = "char";
f->is_signed = FALSE; f->is_signed = FALSE;
f->size = 1; f->size = 1;
@ -88,6 +95,7 @@ void initialize_types()
/* Define char** */ /* Define char** */
struct type* g = calloc(1, sizeof(struct type)); struct type* g = calloc(1, sizeof(struct type));
require(NULL != g, "Exhusted memory while intializing CHAR**\n");
g->name = "char**"; g->name = "char**";
g->is_signed = FALSE; g->is_signed = FALSE;
g->size = register_size; g->size = register_size;
@ -100,6 +108,7 @@ void initialize_types()
/* Define FILE */ /* Define FILE */
struct type* h = calloc(1, sizeof(struct type)); struct type* h = calloc(1, sizeof(struct type));
require(NULL != h, "Exhusted memory while intializing FILE\n");
h->name = "FILE"; h->name = "FILE";
h->is_signed = FALSE; h->is_signed = FALSE;
h->size = register_size; h->size = register_size;
@ -109,6 +118,7 @@ void initialize_types()
/* Define FUNCTION */ /* Define FUNCTION */
struct type* i = calloc(1, sizeof(struct type)); struct type* i = calloc(1, sizeof(struct type));
require(NULL != i, "Exhusted memory while intializing FUNCTION\n");
i->name = "FUNCTION"; i->name = "FUNCTION";
i->is_signed = FALSE; i->is_signed = FALSE;
i->size = register_size; i->size = register_size;
@ -118,12 +128,14 @@ void initialize_types()
/* Primitives mes.c wanted */ /* Primitives mes.c wanted */
struct type* j = calloc(1, sizeof(struct type)); struct type* j = calloc(1, sizeof(struct type));
require(NULL != j, "Exhusted memory while intializing SIZE_T\n");
j->name = "size_t"; j->name = "size_t";
j->is_signed = FALSE; j->is_signed = FALSE;
j->size = register_size; j->size = register_size;
j->indirect = j; j->indirect = j;
struct type* k = calloc(1, sizeof(struct type)); struct type* k = calloc(1, sizeof(struct type));
require(NULL != k, "Exhusted memory while intializing SSIZE_T\n");
k->name = "ssize_t"; k->name = "ssize_t";
k->is_signed = FALSE; k->is_signed = FALSE;
k->size = register_size; k->size = register_size;
@ -182,6 +194,7 @@ int member_size;
struct type* build_member(struct type* last, int offset) struct type* build_member(struct type* last, int offset)
{ {
struct type* i = calloc(1, sizeof(struct type)); struct type* i = calloc(1, sizeof(struct type));
require(NULL != i, "Exhusted memory while building a struct member\n");
i->members = last; i->members = last;
i->offset = offset; i->offset = offset;
@ -240,7 +253,9 @@ void create_struct()
int offset = 0; int offset = 0;
member_size = 0; member_size = 0;
struct type* head = calloc(1, sizeof(struct type)); struct type* head = calloc(1, sizeof(struct type));
require(NULL != head, "Exhusted memory while creating a struct\n");
struct type* i = calloc(1, sizeof(struct type)); struct type* i = calloc(1, sizeof(struct type));
require(NULL != i, "Exhusted memory while creating a struct indirection\n");
head->name = global_token->s; head->name = global_token->s;
head->type = head; head->type = head;
i->name = global_token->s; i->name = global_token->s;

View File

@ -19,11 +19,13 @@
#include<string.h> #include<string.h>
#include<stdio.h> #include<stdio.h>
// void* calloc(int count, int size); // void* calloc(int count, int size);
void file_print(char* s, FILE* f);
int hex2char(int c); int hex2char(int c);
void file_print(char* s, FILE* f);
void require(int bool, char* error);
char* number_to_hex(int a, int bytes) char* number_to_hex(int a, int bytes)
{ {
require(bytes > 0, "number to hex must have a positive number of bytes greater than zero\n");
char* result = calloc(1 + (bytes << 1), sizeof(char)); char* result = calloc(1 + (bytes << 1), sizeof(char));
if(NULL == result) if(NULL == result)
{ {
@ -33,6 +35,7 @@ char* number_to_hex(int a, int bytes)
int i = 0; int i = 0;
int divisor = (bytes << 3); int divisor = (bytes << 3);
require(divisor > 0, "unexpected wrap around in number_to_hex\n");
/* Simply collect numbers until divisor is gone */ /* Simply collect numbers until divisor is gone */
while(0 != divisor) while(0 != divisor)

View File

@ -71,6 +71,8 @@ clean:
./test/test22/cleanup.sh ./test/test22/cleanup.sh
./test/test23/cleanup.sh ./test/test23/cleanup.sh
./test/test24/cleanup.sh ./test/test24/cleanup.sh
./test/test25/cleanup.sh
./test/test26/cleanup.sh
./test/test99/cleanup.sh ./test/test99/cleanup.sh
./test/test100/cleanup.sh ./test/test100/cleanup.sh

View File

@ -53,10 +53,10 @@ a0ae067746e7a2b01d33950da1cf640e12c3a70a045ab331ea2025af59dec9af test/results/t
1154f39f25dcd6d914e9a542306f95280926baf985d011b2152c7ea0b87ab42d test/results/test10-knight-native-binary 1154f39f25dcd6d914e9a542306f95280926baf985d011b2152c7ea0b87ab42d test/results/test10-knight-native-binary
c1b5a2a3cd46c5e95e5540e871c2a916e028684ca80f51c001ef489342e27625 test/results/test10-knight-posix-binary c1b5a2a3cd46c5e95e5540e871c2a916e028684ca80f51c001ef489342e27625 test/results/test10-knight-posix-binary
b3e13d54aab689137628fb9c4487bfd8288f9bd18bef8fe756577c8d2dce1f1f test/results/test10-x86-binary b3e13d54aab689137628fb9c4487bfd8288f9bd18bef8fe756577c8d2dce1f1f test/results/test10-x86-binary
9f8083a7b8a546ed6d88e8b0b6a40d198a71f22940d8b8916a07c784f9c6eff8 test/results/test100-amd64-binary 61036eb3bc1b7039b5f6a240fb661728b57b0d16b641fa5f8d2740fe6b6e50e3 test/results/test100-amd64-binary
f4c4ecf1c76bbda3f5bd0ad1e8ebf02a2a65191b082b631e183b47c2f77af905 test/results/test100-armv7l-binary 3987986885bfc3edb542b361b3f8d56f418b197f6974af2dcba320363faf24a6 test/results/test100-armv7l-binary
bc9552bede2942ee73b66d95e226119a0e743dcd89333a53c14fd97b99f7f038 test/results/test100-knight-posix-binary 120233fe5773c21d83c40201cda4c858214593896919f62f42282cb5eb7b86e7 test/results/test100-knight-posix-binary
5c2e4304819ccfa154b5ea732e9eab58efd7ba1dd1baf8cc88a18447c34b9256 test/results/test100-x86-binary c7726290b1bd72e519da56e2a82a8fc11da22aecdd144e0498e7f2469c847649 test/results/test100-x86-binary
34e6d535e30ef8826a4ad1a4d08b76cfa370c54595599ad3be784b64c9cd8ec5 test/results/test11-amd64-binary 34e6d535e30ef8826a4ad1a4d08b76cfa370c54595599ad3be784b64c9cd8ec5 test/results/test11-amd64-binary
893695e6f300a0fe055fad935a56abd549bba70d1d39c535a680f41bbb73f117 test/results/test11-armv7l-binary 893695e6f300a0fe055fad935a56abd549bba70d1d39c535a680f41bbb73f117 test/results/test11-armv7l-binary
955b564d2c89abf2cfc6c80d766cd11479d146b828dec69e654b0958a62d5e6e test/results/test11-knight-native-binary 955b564d2c89abf2cfc6c80d766cd11479d146b828dec69e654b0958a62d5e6e test/results/test11-knight-native-binary

View File

@ -16,6 +16,7 @@
## along with M2-Planet. If not, see <http://www.gnu.org/licenses/>. ## along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
rm -f test/test100/cc.M1 rm -f test/test100/cc.M1
rm -f test/test100/cc-footer.M1
rm -f test/test100/cc.hex2 rm -f test/test100/cc.hex2
rm -f test/test100/proof rm -f test/test100/proof
exit 0 exit 0

View File

@ -1 +1 @@
fd4328489c1e301b162557e6d21a44753690e002c43ba3b5bd942d6cb810d74c test/test100/proof c2e77ffce6bd293ef6f268626b7807dfbb05f0a8c17e9bd52f12c978a36fc830 test/test100/proof