rev 510 - in trunk: . include/prothon src

SVN User <[email protected]> Wed, 19 May 2004 19:16:52 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-19 19:16:49 -0400 (Wed, 19 May 2004)
New Revision: 510

Modified:
   trunk/STATUS.txt
   trunk/include/prothon/prothon.h
   trunk/src/argproc.c
   trunk/src/interp.c
   trunk/src/interp.h
   trunk/src/main.c
   trunk/src/memory_mgr.c
   trunk/src/parser.h
   trunk/src/parser_routines.c
Log:
added command-line -d option to turn on debug flags

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/STATUS.txt	2004-05-19 23:16:49 UTC (rev 510)
@@ -19,7 +19,7 @@
 
 --- help() in interactive console()
 
---- -d -m -l cmd-line options missing
+--- -m -l cmd-line options missing
 --- memory bounds -> mem-mgr priorities
 
 --- plug memory leaks -- add del_unlocks

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/include/prothon/prothon.h	2004-05-19 23:16:49 UTC (rev 510)
@@ -56,14 +56,16 @@
 #define PROTHON_H
 
 //**************************** DEBUG DEFINITIONS ******************************
-  
-//#define TRACE_PARSER
-//#define DUMP_MODULE_CODE
-//#define TRACE_INTERPRETER
-//#define DUMP_OBJECTS_AT_END
+ 
+#define CMDLINE_FLAGS
+#define TRACE_PARSER
+#define DUMP_MODULE_CODE
+#define TRACE_INTERPRETER
+#define DUMP_OBJECTS_AT_END
+#define DEBUG_THREADS
+
+//#define DEBUG_MEM_MGR
 //#define PROSIST_DEBUG
-//#define DEBUG_THREADS
-//#define DEBUG_MEM_MGR
 
 
 //*****************************************************************************

Modified: trunk/src/argproc.c
===================================================================
--- trunk/src/argproc.c	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/argproc.c	2004-05-19 23:16:49 UTC (rev 510)
@@ -55,6 +55,8 @@
 
 #include "object.h"
 #include "memory_mgr.h"
+#include "parser.h"
+#include "interp.h"
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -109,7 +111,15 @@
 			for (p=argv[i]; *p == '-'; p++);
 			switch (*p) {
 			case 'd':
-				set_attr(ist, res, sym(ist, "debug"), NEW_STRING(argv[i]));
+				for (p++; *p; p++)
+					switch (*p) {
+					case 'p': trace_parser = 1;			break;
+					case 't': debug_threads = 1;		break;
+					case 'b': dump_module_code = 1;		break;
+					case 'c': trace_interpreter = 1;	break;
+					case 'o': dump_objects_at_end = 1;	break;
+					case 'G': disable_mem_mgr = 1;		break;
+					}
 				break;
 			case 'm': 
 				if (++i == argc) goto err;
@@ -186,16 +196,31 @@
 				printf("      Execute code in quotes before any code in files are run.  This option\n");
 				printf("      may co-exist with the main file execution.\n");
 				printf("\n");
+#ifdef CMDLINE_FLAGS
 				printf("   -d<flags>\n");
-				printf("      (Not implemented yet)\n");
 				printf("      Single letter flags must immediately follow the d to indicate what debug\n");
 				printf("      options you wish turned on for this run of Prothon.  Choose from:\n");
-				printf("         p: Parser tracing\n");
+#ifdef TRACE_PARSER
+				printf("         p: Parser trace dumps\n");
+#endif
+#ifdef DUMP_MODULE_CODE
+				printf("         b: Bytecode disassembly dumps\n");
+#endif
+#ifdef TRACE_INTERPRETER
+				printf("         c: code trace dumps\n");
+#endif
+#ifdef DUMP_OBJECTS_AT_END
+				printf("         o: Dump all objects at end of execution\n");
+#endif
+#ifdef DEBUG_THREADS
 				printf("         t: Thread status messages\n");
+#endif
+#ifdef DEBUG_MEM_MGR
 				printf("         g: Garbage collector status messages\n");
-				printf("         p: Interpreter (instruction execution) tracing\n");
-				printf("         o: Dump all objects to object-dump.txt at end of execution\n");
+#endif
+				printf("         G: Disable garbage collector\n");
 				printf("\n");
+#endif
 				printf("   -V -- version\n");
 				printf("      Print version and quit\n");
 				printf("\n");

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/interp.c	2004-05-19 23:16:49 UTC (rev 510)
@@ -70,6 +70,12 @@
 i32_t main_threads_started=0;
 i32_t main_threads_running=0;
 
+int debug_threads = 0;
+int dump_module_code = 0;
+int trace_interpreter = 0;
+int dump_objects_at_end = 0;
+int disable_mem_mgr = 0;
+
 static FILE* tfout=NULL;
 
 static int prt_code_line(frame_p frame, code_p code, int pc, FILE* fout);
@@ -718,9 +724,11 @@
 	obj_p dump_code_key;
 #endif
 #ifdef TRACE_INTERPRETER
-	if (!tfout) tfout = fopen("!code_trace.txt", "w");
-	fprintf(tfout, "--- starting exec_loop at frame %8x, pc: %d\n", (intptr_t) frame, frame->pc);
-	fflush(tfout);
+	if (trace_interpreter) {
+		if (!tfout) tfout = fopen("!code_trace.txt", "w");
+		fprintf(tfout, "--- starting exec_loop at frame %8x, pc: %d\n", (intptr_t) frame, frame->pc);
+		fflush(tfout);
+	}
 #endif
 
 	have_exstk = (fr_exstk && clist_len(fr_exstk));
@@ -746,8 +754,10 @@
 		}
 		if (fr_pc >= fr_code->len){
 #ifdef TRACE_INTERPRETER
-			fprintf(tfout, "--- ran off end of code at pc: %d\n", (intptr_t) frame->pc);
-			fflush(tfout);
+			if (trace_interpreter) {
+				fprintf(tfout, "--- ran off end of code at pc: %d\n", (intptr_t) frame->pc);
+				fflush(tfout);
+			}
 #endif
 			return_value = do_return(ist, frame, &switch_frame, &free_frame, return_value);
 			if (intrp_exobj) goto endop;
@@ -922,7 +932,7 @@
 				}
 				set_attr(ist, func, SYM(PREV_SCOPE_),  frame->locals);
 #ifdef DUMP_MODULE_CODE
-				{
+				if (dump_module_code) {
 					char dump_name[256];
 					static int cntr = 1;
 					apr_snprintf(dump_name, sizeof(dump_name),
@@ -947,14 +957,14 @@
 				break;
 			case OP_BR:
 #ifdef TRACE_INTERPRETER
-				trace_step(frame);
+				if (trace_interpreter) trace_step(frame);
 #endif
 				fr_pc += param;
 				continue;
 			case OP_BEQ:
 				if (fr_tos == fr_data(1)) {
 #ifdef TRACE_INTERPRETER
-					trace_step(frame);
+					if (trace_interpreter) trace_step(frame);
 #endif
 					fr_pc += param;
 					continue;
@@ -989,7 +999,7 @@
 				if (!has_proto_QUES(ist, exc_waiting, exc_proto)) { // need to support list XXX
 					IF_EXC_BREAK;
 #ifdef TRACE_INTERPRETER
-					trace_step(frame);
+					if (trace_interpreter) trace_step(frame);
 #endif
 					fr_pc += param;
 					continue;
@@ -1109,7 +1119,7 @@
 				obj_p new_obj, key;
 				fr_sp -= (num_protos+1)*2;
 #ifdef DUMP_MODULE_CODE
-				dump_code_key = fr_stack[fr_sp+1];
+				if (dump_module_code) dump_code_key = fr_stack[fr_sp+1];
 #endif
 				if (num_protos == 0) new_obj = new_object(ist, NULL);
 				else {
@@ -1145,7 +1155,7 @@
 			case OP_WITH:
 #ifdef DUMP_MODULE_CODE
 				dump_code_key = NULL;
-op_with:		{
+op_with:		if (dump_module_code) {
 					char dump_name[256];
 					static int with_num = 1;
 					obj_p func = fr_data(1);
@@ -1371,7 +1381,7 @@
 endcase:
 
 #ifdef TRACE_INTERPRETER
-		trace_step(frame);
+		if (trace_interpreter) trace_step(frame);
 #endif
 		if (!intrp_exobj) {
 			if (op < OP_PARAM_WORDS_BOUNDARY)
@@ -1387,8 +1397,10 @@
 			int last_called_from_c = FALSE;
 
 #ifdef TRACE_INTERPRETER
-			fprintf(tfout, "---exception---\n");
-			fflush(tfout);
+			if (trace_interpreter) {
+				fprintf(tfout, "---exception---\n");
+				fflush(tfout);
+			}
 #endif
 			add_frame_to_exception(ist, frame);
 			while (!have_exstk && frame->prev_frame && !last_called_from_c) {
@@ -1396,9 +1408,11 @@
 				last_called_from_c = frame->called_from_c;
 				frame = ist->frame = frame->prev_frame;
 #ifdef TRACE_INTERPRETER
-				fprintf(tfout, "--- delete top frame %8x\n", (intptr_t) tmp_frame);
-				fprintf(tfout, "--- new top frame is %8x\n", (intptr_t) frame);
-				fflush(tfout);
+				if (trace_interpreter) {
+					fprintf(tfout, "--- delete top frame %8x\n", (intptr_t) tmp_frame);
+					fprintf(tfout, "--- new top frame is %8x\n", (intptr_t) frame);
+					fflush(tfout);
+				}
 #endif
 				stack_lim = frame->code->max_stack_depth;
 				// free_frame(tmp_frame);  XXX
@@ -1439,9 +1453,11 @@
 				switch_frame = switch_frame->next_frame;
 			frame = ist->frame = switch_frame;
 #ifdef TRACE_INTERPRETER
-			if (frame != DONE_FRAME_FLAG)
-				fprintf(tfout, "--- switch to frame %8x\n", (intptr_t) frame);
-			fflush(tfout);
+			if (trace_interpreter) {
+				if (frame != DONE_FRAME_FLAG)
+					fprintf(tfout, "--- switch to frame %8x\n", (intptr_t) frame);
+				fflush(tfout);
+			}
 #endif
 			if (frame != DONE_FRAME_FLAG)
 				stack_lim = frame->code->max_stack_depth;
@@ -1454,8 +1470,10 @@
 		}
 		if (return_flag || frame == DONE_FRAME_FLAG) {
 #ifdef TRACE_INTERPRETER
-			fprintf(tfout, "--- exiting exec_loop, value: %8x\n", (intptr_t) return_value);
-			fflush(tfout);
+			if (trace_interpreter) {
+				fprintf(tfout, "--- exiting exec_loop, value: %8x\n", (intptr_t) return_value);
+				fflush(tfout);
+			}
 #endif
 			if (frame == DONE_FRAME_FLAG) frame = ist->frame = NULL;
 			return return_value;
@@ -1592,7 +1610,7 @@
 		return NULL;
 
 #ifdef DUMP_MODULE_CODE
-	{
+	if (dump_module_code) {
 		char name[256], buf[65], *p, *d, *e = buf+64;
 		for (p = str, d = buf; *p && d < e; p++) {
 			if (*p >= 'A' && *p <= 'Z') *(d++) = *p;
@@ -1661,7 +1679,7 @@
 	name = symch(ist, module_name);
 
 #ifdef DUMP_MODULE_CODE
-	{
+	if (dump_module_code) {
 		char dump_name[256];
 		apr_snprintf(dump_name, sizeof(dump_name), "!module_code_%s.txt", name);
 		dump_code(ist, NULL, code, dump_name);
@@ -1700,7 +1718,7 @@
 	char* filename = pr_strptr(list_item(ist, (obj_p)argv, 0));
 
 #ifdef DEBUG_THREADS
-	printf("Starting a main thread\n");
+	if (debug_threads) printf("Starting a main thread\n");
 #endif
 
 	thread_obj = register_thread(handle);
@@ -1735,8 +1753,10 @@
 	pr_unlock(&start_main_lock);
 
 #ifdef DEBUG_THREADS
-	printf("%s:%d thread has started\n", symch(ist, main_sym), main_index);
-	SetThreadName(symch(ist, main_sym));
+	if (debug_threads) {
+		printf("%s:%d thread has started\n", symch(ist, main_sym), main_index);
+		SetThreadName(symch(ist, main_sym));
+	}
 #endif
 
 	set_attr(ist, module, sym(ist, "argv_"), (obj_p) argv);
@@ -1754,7 +1774,7 @@
 	thread_p->running = FALSE;
 
 #ifdef DEBUG_THREADS
-	printf("%s:%d thread has terminated\n", symch(ist, main_sym), main_index);
+	if (debug_threads) printf("%s:%d thread has terminated\n", symch(ist, main_sym), main_index);
 #endif
 
 	return NULL;

Modified: trunk/src/interp.h
===================================================================
--- trunk/src/interp.h	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/interp.h	2004-05-19 23:16:49 UTC (rev 510)
@@ -63,6 +63,12 @@
 
 isp new_ist(access_t access);
 
+extern int debug_threads;
+extern int dump_module_code;
+extern int trace_interpreter; 
+extern int dump_objects_at_end;
+extern int disable_mem_mgr;
+
 extern i32_t main_threads_started;
 extern i32_t main_threads_running;
 

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/main.c	2004-05-19 23:16:49 UTC (rev 510)
@@ -113,11 +113,6 @@
 	/* Now that apr is ready, on to our stuff */
 	ist = new_ist(ACC_SYSTEM);
 
-
-#ifdef TRACE_PARSER
-	yydebug = 1;
-#endif
-
 	object_store_init(ist, 20);
 
 	thread_init(ist);
@@ -125,6 +120,10 @@
 	sys_argv_obj = arginit(ist, argc, argv);
 	sysinit(ist, argv[0], sys_argv_obj);
 
+#ifdef TRACE_PARSER
+	yydebug = trace_parser;
+#endif
+
 	if ((code_str_obj = get_attr(ist, sys_argv_obj, sym(ist, "code")))) {
 		exec_string(ist, pr_strptr(code_str_obj), FALSE, NULL);
 		check_exceptions(ist);
@@ -149,15 +148,15 @@
 				            NULL, (int) acc_level->data.i64 );
 		}
 	}
+	if (!disable_mem_mgr)
+		new_thread_obj(ist, (apr_thread_start_t)mem_mgr_thread, NULL, NULL, ACC_SYSTEM);
 
-	new_thread_obj(ist, (apr_thread_start_t)mem_mgr_thread, NULL, NULL, ACC_SYSTEM);
-
 #ifdef DEBUG_THREADS
-	SetThreadName("main");
+	if (debug_threads) SetThreadName("main");
 #endif
 
 #ifdef DEBUG_THREADS
-	printf("All threads started, now waiting for mains to finish...\n");
+	if (debug_threads) printf("All threads started, now waiting for mains to finish...\n");
 #endif
 
 	/* Grab this lock, so we know all threads are started */
@@ -189,19 +188,26 @@
 	}
 
 #ifdef DEBUG_THREADS
-	printf("All main threads have terminated, now stopping memory manager ...\n");
+	if (debug_threads && !disable_mem_mgr) 
+		printf("All main threads have terminated, now stopping memory manager ...\n");
 #endif
 
-	mem_mgr_req_termination = TRUE;
-	mem_mgr_count = 0;
-	while(!mem_mgr_terminated && ++mem_mgr_count < MEMMGR_TIMEOUT) apr_sleep(PAUSE_MS*1000);
+	if (!disable_mem_mgr) {
+		mem_mgr_req_termination = TRUE;
+		mem_mgr_count = 0;
+		while(!mem_mgr_terminated && ++mem_mgr_count < MEMMGR_TIMEOUT) apr_sleep(PAUSE_MS*1000);
+	}
 
 #ifdef DEBUG_THREADS
-	printf("Memory manager terminated, shutting down\n");
+	if (debug_threads && !disable_mem_mgr) 
+		printf("Memory manager terminated\n");
+	if (debug_threads)
+		printf("Shutting down ...\n");
 #endif
-	
+
 #ifdef DUMP_OBJECTS_AT_END
-	dump(ist, "object-dump.txt", OBJ(ROOT_GLOBALS));
+	if (dump_objects_at_end)
+		dump(ist, "object-dump.txt", OBJ(ROOT_GLOBALS));
 #endif
 
 	return 0;

Modified: trunk/src/memory_mgr.c
===================================================================
--- trunk/src/memory_mgr.c	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/memory_mgr.c	2004-05-19 23:16:49 UTC (rev 510)
@@ -116,8 +116,10 @@
 	int last_obj_del_count = 0, last_obj_count = 0;     
 
 #ifdef DEBUG_THREADS
-	printf("Memory manager thread started\n");
-	SetThreadName("mmgr");
+	if (debug_threads) {
+		printf("Memory manager thread started\n");
+		SetThreadName("mmgr");
+	}
 #endif
 	while(!mem_mgr_req_termination) {
 		obj_p obj, last_obj, min_obj = (obj_p) 10;
@@ -297,7 +299,7 @@
 	mem_mgr_running = FALSE;
 	mem_mgr_terminated = TRUE;
 #ifdef DEBUG_THREADS
-	printf("Memory manager thread terminating\n");
+	if (debug_threads) printf("Memory manager thread terminating\n");
 #endif
 
 	return NULL;

Modified: trunk/src/parser.h
===================================================================
--- trunk/src/parser.h	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/parser.h	2004-05-19 23:16:49 UTC (rev 510)
@@ -90,6 +90,8 @@
 int yyparse(void* yylex_param);
 int yyerror (char* s);
 
+extern int trace_parser;
+
 typedef struct {
 	isp			ist;
 	int			line;

Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-05-19 20:58:06 UTC (rev 509)
+++ trunk/src/parser_routines.c	2004-05-19 23:16:49 UTC (rev 510)
@@ -60,12 +60,15 @@
 #include "parser.h"
 #include "bytecodes.h"
 
+int trace_parser = 0;
+
 #ifdef TRACE_PARSER
 
 FILE* parse_debug_fout;
 
 code_p debug_retrn(int line_no, code_p res){
-	fprintf(parse_debug_fout, "******> lin:%4d, len:%3d, dep:%3d, max:%3d\n", line_no, res->len, res->stack_depth, res->max_stack_depth);
+	if (trace_parser)
+		fprintf(parse_debug_fout, "******> lin:%4d, len:%3d, dep:%3d, max:%3d\n", line_no, res->len, res->stack_depth, res->max_stack_depth);
 	return res;
 }
 #endif
@@ -116,7 +119,8 @@
 		if ( !(parse_state = new_parse_state(ist, NULL, string)) ) return NULL;
 	}
 #ifdef TRACE_PARSER
-	{	char *d, *s, *dfname = pr_malloc(35);
+	if (trace_parser){
+		char *d, *s, *dfname = pr_malloc(35);
 		for (s=filename, d=dfname; *s && d < dfname+30; s++) 
 			if ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || 
 				(*s >= '0' && *s <= '9') || *s == '_')
@@ -136,7 +140,8 @@
 	yyparse(parse_state);
 
 #ifdef TRACE_PARSER
-	fclose(parse_debug_fout);
+	if (trace_parser)
+		fclose(parse_debug_fout);
 #endif
 
 	if (parse_state->parse_results)