@@ -1005,7 +1005,7 @@ int *create_c_image (char *source, int *process) {
1005
1005
return 0 ;
1006
1006
}
1007
1007
1008
- if (src ) { // Print source
1008
+ if (src == 1 ) { // Print source
1009
1009
// Print data
1010
1010
dprintf (STDERR , ".origin data %ld" , process [B_p_data ]);
1011
1011
data_ptr = (char * )process [B_p_data ]; // tmp: data segment
@@ -1051,6 +1051,43 @@ int *create_c_image (char *source, int *process) {
1051
1051
cp_ptr = cp_ptr + codepoint_size ;
1052
1052
}
1053
1053
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 );
1054
1091
}
1055
1092
1056
1093
idmain = lookup_symbol (entry , sym );
@@ -1254,8 +1291,29 @@ void free_process(int *process) {
1254
1291
if ( (ptr = (void * )process )) free (ptr );
1255
1292
}
1256
1293
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
+
1257
1311
int c5_lispmain (int argc , char * * argv ) {
1258
1312
int result ;
1313
+
1314
+ // Clear C4 memory
1315
+ early_exit (EE_ENTER_LISP );
1316
+
1259
1317
// Requires libscheme
1260
1318
if (platform_init (runtime_path ("scheme" ))) {
1261
1319
return 1 ;
@@ -1267,13 +1325,6 @@ int c5_lispmain(int argc, char **argv) {
1267
1325
return result ;
1268
1326
}
1269
1327
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
-
1277
1328
// Override name when compiling natively
1278
1329
#define main c5_main
1279
1330
int main (int argc , char * * argv )
@@ -1289,7 +1340,11 @@ int main(int argc, char **argv)
1289
1340
setup_opcodes ();
1290
1341
sym = le = e = 0 ;
1291
1342
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
+
1293
1348
verbose = 0 ;
1294
1349
vm_cycle_count = 1000 ;
1295
1350
poolsz = 256 * 1024 ; // arbitrary size
@@ -1298,12 +1353,20 @@ int main(int argc, char **argv)
1298
1353
// Allocate vm_processes
1299
1354
vm_proc_max = 32 ;
1300
1355
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
+ }
1302
1361
memset (vm_processes , 0 , vm_proc_max * sizeof (int * ));
1303
1362
vm_active_process = 0 ;
1304
1363
1305
1364
// 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
+ }
1307
1370
// copy pointers for search paths
1308
1371
tmp = ".\0" // default: use path as given
1309
1372
"./c4_modules\0" // c4_includes/
@@ -1319,13 +1382,20 @@ int main(int argc, char **argv)
1319
1382
1320
1383
// Allocate first code page
1321
1384
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
+ }
1322
1390
1323
1391
-- argc ; ++ argv ;
1324
1392
early_param_exit = 0 ; // when to exit parameter parsing
1325
1393
while (argc > 0 && * * argv == '-' && early_param_exit == 0 ) {
1326
1394
ch = (* argv )[1 ];
1327
1395
// -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 ; }
1329
1399
// -d show source during execution
1330
1400
else if (ch == 'd' ) { debug = 1 ; }
1331
1401
// -c 123 set cycle count to 123
@@ -1364,18 +1434,15 @@ int main(int argc, char **argv)
1364
1434
}
1365
1435
// -L enter lispmain
1366
1436
else if (ch == 'L' ) {
1367
- #if 0 // Run under c4 only
1437
+ #if __C4__ // Run under c4 only
1368
1438
// start lisp4.c instead, and end parameter passing
1369
1439
vm_processes [vm_proc_count ++ ] = (int )"lisp4.c" ;
1370
1440
early_param_exit = 1 ;
1371
1441
++ argc ; -- argv ; // set arguments correctly
1372
- if (0 ) // Dummy out the next call
1373
- #else
1374
- early_exit ();
1375
- #if 0
1442
+ #if __C4__
1376
1443
if (0 ) // Dummy out the next call
1377
1444
#endif
1378
- return c5_lispmain (argc , argv );
1445
+ return c5_lispmain (argc , argv );
1379
1446
#endif
1380
1447
}
1381
1448
// -v enable verbose mode
@@ -1389,6 +1456,7 @@ int main(int argc, char **argv)
1389
1456
printf ("usage: %s [-L] [-s] [-d] [-v] [-c nn] [-r file] [-p [+]nn] file args...\n" , argv0 );
1390
1457
printf (" -L Load default platform library, and enter main\n" );
1391
1458
printf (" -s Print source and exit\n" );
1459
+ printf (" -X Print source in hex mode and exit\n" );
1392
1460
printf (" -d Enable debugging mode\n" );
1393
1461
printf (" -v Enable verbose mode\n" );
1394
1462
printf (" -c nn Set process cycle count to nnn\n" );
@@ -1404,17 +1472,18 @@ int main(int argc, char **argv)
1404
1472
printf (" number size : %i bytes\n" , sizeof (int ));
1405
1473
printf (" pointer size: %i bytes\n" , sizeof (void * ));
1406
1474
printf (" process size: %i bytes (%i words)\n" , sizeof (int ) * __B , __B );
1475
+ early_exit (EE_NO_PROCS );
1407
1476
return 1 ;
1408
1477
} else {
1409
1478
dprintf (STDERR , "Invalid argument: %s, try -h\n" , * argv );
1410
- early_exit ();
1479
+ early_exit (EE_NO_PROCS );
1411
1480
return -1 ;
1412
1481
}
1413
1482
-- argc ; ++ argv ;
1414
1483
}
1415
1484
if (vm_proc_count < 1 && argc < 1 ) {
1416
- early_exit ();
1417
1485
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 );
1418
1487
return -1 ;
1419
1488
}
1420
1489
// Only add next arg as process if early parameter parsing was not invoked
@@ -1466,7 +1535,7 @@ int main(int argc, char **argv)
1466
1535
}
1467
1536
1468
1537
// Perform normal cleanup
1469
- early_exit ();
1538
+ early_exit (EE_NONE );
1470
1539
1471
1540
return exitcode ;
1472
1541
}
0 commit comments