Skip to content

Commit 00beefa

Browse files
committed
Better exit method, additional output mode
- Now uses an "early exit code" to denote reason for early exit. Simplifies memory accounting for early exits due to errors. - Specify "-X" flag to print source in hex mode. - Magic number now uses more of the available number bits
1 parent 2e25cb8 commit 00beefa

File tree

1 file changed

+90
-21
lines changed

1 file changed

+90
-21
lines changed

core/c4.c

+90-21
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ int *create_c_image (char *source, int *process) {
10051005
return 0;
10061006
}
10071007

1008-
if(src) { // Print source
1008+
if(src == 1) { // Print source
10091009
// Print data
10101010
dprintf(STDERR, ".origin data %ld", process[B_p_data]);
10111011
data_ptr = (char*)process[B_p_data]; // tmp: data segment
@@ -1051,6 +1051,43 @@ int *create_c_image (char *source, int *process) {
10511051
cp_ptr = cp_ptr + codepoint_size;
10521052
}
10531053
free(codepoint_base);
1054+
} else if(src == 2) { // Print source in hex mode
1055+
// Print data
1056+
dprintf(STDERR, ".DATA\n");
1057+
data_ptr = (char*)process[B_p_data]; // tmp: data segment
1058+
data_len = 0; // tmp: current number of chars
1059+
while(data_ptr <= data) {
1060+
if(c4_isprint(*data_ptr)) {
1061+
dprintf(STDERR, "%c", *data_ptr);
1062+
data_len = data_len + 1;
1063+
} else {
1064+
dprintf(STDERR, "\\%.2X", *data_ptr);
1065+
data_len = data_len + 3;
1066+
}
1067+
if(data_len == 0) dprintf(STDERR, "\n");
1068+
if(data_len >= 50) data_len = 0;
1069+
++data_ptr;
1070+
}
1071+
dprintf(STDERR, "\n");
1072+
// Print code
1073+
cp_ptr = (int*)process[B_p_e];
1074+
// line break counter
1075+
cp_len = 0;
1076+
dprintf(STDERR, ".CODE\n");
1077+
while(cp_ptr <= e) {
1078+
if(++cp_len % sizeof(int) == 0) {
1079+
dprintf(STDERR, "\n");
1080+
}
1081+
if(sizeof(int) == 4) {
1082+
dprintf(STDERR, "%X ", *cp_ptr++);
1083+
} else if(sizeof(int) == 8) {
1084+
dprintf(STDERR, "%X ", *cp_ptr++);
1085+
} else {
1086+
dprintf(STDERR, "\\%ld ", *cp_ptr++);
1087+
}
1088+
}
1089+
if(cp_len) dprintf(STDERR, "\n");
1090+
free(codepoint_base);
10541091
}
10551092

10561093
idmain = lookup_symbol(entry, sym);
@@ -1254,8 +1291,29 @@ void free_process(int *process) {
12541291
if( (ptr = (void*)process)) free(ptr);
12551292
}
12561293

1294+
// Early exit reasons
1295+
enum /* EarlyExits */ {
1296+
EE_ALLOC_PROCS, // Failed to allocate procs
1297+
EE_ALLOC_PATHS, // Failed to allocate search paths
1298+
EE_ALLOC_PAGES, // Failed to allocate code pages
1299+
EE_ENTER_LISP, // Entering Lisp module
1300+
EE_NO_PROCS, // No processes to run
1301+
EE_NONE // Not an early exit (normal cleanup)
1302+
};
1303+
1304+
// Perform actions (mostly freeing memory) that should occur before the application exits
1305+
void early_exit (int reason) {
1306+
if(reason > EE_ALLOC_PROCS) free((void*)vm_processes);
1307+
if(reason > EE_ALLOC_PATHS) free((void*)search_paths);
1308+
if(reason > EE_ALLOC_PAGES) code_pages_free();
1309+
}
1310+
12571311
int c5_lispmain(int argc, char **argv) {
12581312
int result;
1313+
1314+
// Clear C4 memory
1315+
early_exit(EE_ENTER_LISP);
1316+
12591317
// Requires libscheme
12601318
if(platform_init(runtime_path("scheme"))) {
12611319
return 1;
@@ -1267,13 +1325,6 @@ int c5_lispmain(int argc, char **argv) {
12671325
return result;
12681326
}
12691327

1270-
// Perform actions (mostly freeing memory) that should occur before the application exits
1271-
void early_exit () {
1272-
free((void*)vm_processes);
1273-
free((void*)search_paths);
1274-
code_pages_free();
1275-
}
1276-
12771328
// Override name when compiling natively
12781329
#define main c5_main
12791330
int main(int argc, char **argv)
@@ -1289,7 +1340,11 @@ int main(int argc, char **argv)
12891340
setup_opcodes();
12901341
sym = le = e = 0;
12911342
data = 0;
1292-
B_MAGIC = 0xBEEF;
1343+
// Construct a number to match number of bytes
1344+
B_MAGIC = sizeof(int) << sizeof(int) << sizeof(int) << sizeof(int);
1345+
// Overwrite some of it
1346+
B_MAGIC = B_MAGIC ^ (int)0xDEADBEEF;
1347+
12931348
verbose = 0;
12941349
vm_cycle_count = 1000;
12951350
poolsz = 256*1024; // arbitrary size
@@ -1298,12 +1353,20 @@ int main(int argc, char **argv)
12981353
// Allocate vm_processes
12991354
vm_proc_max = 32;
13001355
vm_proc_count = 0;
1301-
if (!(vm_processes = (int*)malloc(vm_proc_max * sizeof(int*)))) { dprintf(STDERR, "Failed to allocate vm_processes area\n"); return -1; }
1356+
if (!(vm_processes = (int*)malloc(vm_proc_max * sizeof(int*)))) {
1357+
early_exit(EE_ALLOC_PROCS);
1358+
dprintf(STDERR, "Failed to allocate vm_processes area\n");
1359+
return -1;
1360+
}
13021361
memset(vm_processes, 0, vm_proc_max * sizeof(int*));
13031362
vm_active_process = 0;
13041363

13051364
// Allocate search paths
1306-
if(!(search_paths = (char**)malloc(SEARCH_PATHS_MAX * sizeof(char*)))) { dprintf(STDERR, "Failed to allocate search_paths\n"); return -1; }
1365+
if(!(search_paths = (char**)malloc(SEARCH_PATHS_MAX * sizeof(char*)))) {
1366+
early_exit(EE_ALLOC_PATHS);
1367+
dprintf(STDERR, "Failed to allocate search_paths\n");
1368+
return -1;
1369+
}
13071370
// copy pointers for search paths
13081371
tmp = ".\0" // default: use path as given
13091372
"./c4_modules\0" // c4_includes/
@@ -1319,13 +1382,20 @@ int main(int argc, char **argv)
13191382

13201383
// Allocate first code page
13211384
code_pages = c4_list_new(0, 0); // empty entry as tail
1385+
if(!code_pages) {
1386+
early_exit(EE_ALLOC_PAGES);
1387+
dprintf(STDERR, "Failed to allocate code pages\n");
1388+
return -1;
1389+
}
13221390

13231391
--argc; ++argv;
13241392
early_param_exit = 0; // when to exit parameter parsing
13251393
while(argc > 0 && **argv == '-' && early_param_exit == 0) {
13261394
ch = (*argv)[1];
13271395
// -s show source and exit
1328-
if ((*argv)[1] == 's') { src = 1; }
1396+
if (ch == 's') { src = 1; }
1397+
// -X show source in hex mode and exit
1398+
else if (ch == 'X') { src = 2; }
13291399
// -d show source during execution
13301400
else if (ch == 'd') { debug = 1; }
13311401
// -c 123 set cycle count to 123
@@ -1364,18 +1434,15 @@ int main(int argc, char **argv)
13641434
}
13651435
// -L enter lispmain
13661436
else if(ch == 'L') {
1367-
#if 0 // Run under c4 only
1437+
#if __C4__ // Run under c4 only
13681438
// start lisp4.c instead, and end parameter passing
13691439
vm_processes[vm_proc_count++] = (int)"lisp4.c";
13701440
early_param_exit = 1;
13711441
++argc; --argv; // set arguments correctly
1372-
if(0) // Dummy out the next call
1373-
#else
1374-
early_exit();
1375-
#if 0
1442+
#if __C4__
13761443
if(0) // Dummy out the next call
13771444
#endif
1378-
return c5_lispmain(argc, argv);
1445+
return c5_lispmain(argc, argv);
13791446
#endif
13801447
}
13811448
// -v enable verbose mode
@@ -1389,6 +1456,7 @@ int main(int argc, char **argv)
13891456
printf("usage: %s [-L] [-s] [-d] [-v] [-c nn] [-r file] [-p [+]nn] file args...\n", argv0);
13901457
printf(" -L Load default platform library, and enter main\n");
13911458
printf(" -s Print source and exit\n");
1459+
printf(" -X Print source in hex mode and exit\n");
13921460
printf(" -d Enable debugging mode\n");
13931461
printf(" -v Enable verbose mode\n");
13941462
printf(" -c nn Set process cycle count to nnn\n");
@@ -1404,17 +1472,18 @@ int main(int argc, char **argv)
14041472
printf(" number size : %i bytes\n", sizeof(int));
14051473
printf(" pointer size: %i bytes\n", sizeof(void*));
14061474
printf(" process size: %i bytes (%i words)\n", sizeof(int) * __B, __B);
1475+
early_exit(EE_NO_PROCS);
14071476
return 1;
14081477
} else {
14091478
dprintf(STDERR, "Invalid argument: %s, try -h\n", *argv);
1410-
early_exit();
1479+
early_exit(EE_NO_PROCS);
14111480
return -1;
14121481
}
14131482
--argc; ++argv;
14141483
}
14151484
if (vm_proc_count < 1 && argc < 1) {
1416-
early_exit();
14171485
dprintf(STDERR, "usage: %s [-L] [-s] [-d] [-v] [-c nn] [-r file] [-p nn] [-M] file args...\n", argv0);
1486+
early_exit(EE_NO_PROCS);
14181487
return -1;
14191488
}
14201489
// Only add next arg as process if early parameter parsing was not invoked
@@ -1466,7 +1535,7 @@ int main(int argc, char **argv)
14661535
}
14671536

14681537
// Perform normal cleanup
1469-
early_exit();
1538+
early_exit(EE_NONE);
14701539

14711540
return exitcode;
14721541
}

0 commit comments

Comments
 (0)