changeset 13:23dd9c766699

C backend close to being useable
author mpavone@
date Tue, 26 May 2009 23:49:46 +0000
parents 31f8182f3433
children d05184970c1c
files backendutils.rhope cbackend.rhope datum.c datum.h extendlib.rhope interp.c makefile parser.vistxt testc.rhope
diffstat 9 files changed, 339 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/backendutils.rhope	Mon May 25 23:34:36 2009 -0400
+++ b/backendutils.rhope	Tue May 26 23:49:46 2009 +0000
@@ -13,3 +13,65 @@
 		]Replace["<","_LT_"]
 		]Replace["<","_GT_"]
 }
+
+Blueprint AddRef
+{
+	Value
+}
+
+AddRef[value:out]
+{
+	out <- [Build["AddRef"]]Value <<[value]
+}
+
+Make Op@AddRef[addref,func:out]
+{
+	out <- [["add_ref("]Append[ [[addref]Value >>]Make Op[func] ]]Append[")"]
+}
+
+Make Op@String[string,func:out]
+{
+	out <- [func]Resolve[string]
+}
+
+Make Op@Whole Number[num,func:out]
+{
+	out <- num
+}
+
+Make Op@Real Number[num,func:out]
+{
+	out <- num
+}
+
+Blueprint Constant
+{
+	Var
+}
+
+Constant[var:out]
+{
+	out <- [Build["Constant"]]Var <<[var]
+}
+
+Make Op@Constant[const,func:out]
+{
+	out <- [const]Var >>
+}
+
+Blueprint Result
+{
+	Output Num
+}
+
+Result[num:out]
+{
+	out <- [Build["Result"]]Output Num <<[num]
+}
+
+Make Op@Result[result,func:out]
+{
+	out <- [func]Result Reference[[result]Output Num>>]
+}
+
+
--- a/cbackend.rhope	Mon May 25 23:34:36 2009 -0400
+++ b/cbackend.rhope	Tue May 26 23:49:46 2009 +0000
@@ -14,6 +14,49 @@
 	out <- [[[[Build["Blueprint Def"]]Name <<[name]]Fixed Size <<[0]]Fields <<[()]]Methods <<[New@Dictionary[]]
 }
 
+Blueprint C Method Registry
+{
+	Lookup
+	Next ID
+}
+
+C Method Registry[:out]
+{
+	builtins <- [[[[[[[[[[[[[New@Dictionary[]
+		]Set["+", "METHOD_ADD"]
+		]Set["-", "METHOD_SUB"]
+		]Set["/", "METHOD_DIV"]
+		]Set["*", "METHOD_MUL"]
+		]Set["LShift", "METHOD_LSHIFT"]
+		]Set["RShift", "METHOD_RSHIFT"]
+		]Set["=", "METHOD_EQUALS"]
+		]Set[">", "METHOD_GREATER"]
+		]Set["<", "METHOD_LESS"]
+		]Set["If", "METHOD_IF"]
+		]Set["Set Missing Field", "METHOD_SETFIELDMISSING"]
+		]Set["Get Missing Field", "METHOD_GETFIELDMISSING"]
+		]Set["Missing Method", "METHOD_MISSING"]
+	out <- [[Build["C Method Registry"]]Lookup <<[builtins]]Next ID<<[0]
+
+}
+
+Register Method@C Method Registry[reg,method:out]
+{
+	[[reg]Lookup >>]Index[method]
+	{
+		out <- reg
+	}{
+		method ID <- [reg]Next ID>>
+		new lookup <- [[reg]Lookup >>]Set[method, ["METHOD_FIRST_USER+"]Append[method ID]]
+		out <- [[reg]Lookup <<[new lookup]]Next ID <<[[method ID]+[1]]
+	}
+}
+
+Method ID@C Method Registry[reg,method:out]
+{
+	out <- [[reg]Lookup >>]Index[method]
+}
+
 Blueprint C Function
 {
 	Name
@@ -22,17 +65,24 @@
 	Convention
 	Variables
 	Statements
+	Method Registry
 }
 
 C Function[name,inputs,outputs,convention:out]
 {
-	out <- [[[[[[Build["C Function"]
+	out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[]]
+}
+
+C Function With Registry[name,inputs,outputs,convention,registry:out]
+{
+	out <- [[[[[[[Build["C Function"]
 		]Name <<[name]
 		]Inputs <<[inputs]
 		]Outputs <<[outputs]
 		]Convention <<[convention]
 		]Variables <<[New@Dictionary[]]
 		]Statements <<[()]
+		]Method Registry <<[registry]
 }
 
 Allocate Var@C Function[func,name,type:out]
@@ -45,8 +95,16 @@
 	out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ]
 }
 
-Add Operator Statement@C Function[func,source1,source2,dest,op:out]
+Add Raw Line@C Function[func,line:out]
 {
+	out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[line]Append["\n"]]] ]
+}
+
+Add Operator Statement@C Function[func,psource1,psource2,pdest,op:out]
+{
+	source1 <- [psource1]Make Op[func]
+	source2 <- [psource2]Make Op[func]
+	dest <- [pdest]Make Op[func]
 	out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]]
 }
 
@@ -70,12 +128,86 @@
 	out <- [func]Add Operator Statement[source1,source2,dest," / "]
 }
 
-Move@C Function[func,source,dest:out]
+Move@C Function[func,psource,pdest:out]
 {
+	source <- [psource]Make Op[func]
+	dest <- [pdest]Make Op[func]
 	out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]]
 }
 
-Init Outputs@C Function[func]
+AddRef@C Function[func,psource,pdest:out]
+{
+        source <- [psource]Make Op[func] 
+        dest <- [pdest]Make Op[func]
+        out <- [func]Add Statement[[[[dest]Append[" = add_ref("]]Append[source]]Append[")"]]
+}
+
+Release@C Function[func,psource:out]
+{
+	source <- [psource]Make Op[func]
+	out <- [func]Add Statement[[["release_ref("]Append[source]]Append[")"]]
+}
+
+Null@C Function[func,pdest:out]
+{
+	dest <- [pdest]Make Op[func]
+	out <- [func]Add Statement[[dest]Append[" = NULL;"]]
+}
+
+_Method Arg[func,val,inputnum:out]
+{
+	out <- [func]Add Statement[
+		[[["call->params["
+		]Append[inputnum]
+		]Append["] = "]
+		]Append[val]
+	]
+}
+
+Method Call@C Function[func,method,args:out]
+{
+	rargs <- Map[args, ["Make Op"]Set Input[1, func]]
+	out <- [Fold["_Method Arg", func, rargs]
+	]Add Raw Line[[[[["MCall("]Append[ [[func]Method Registry >>]Method ID[method] ]]Append[", "]]Append[ [rargs]Length ]]Append[")"]]
+}
+
+Resolve@C Function[func,op:out]
+{
+	[[func]Inputs >>]Find[op]
+	{
+		out <- [["cdata->params["]Append[~]]Append["]"]
+	}{
+		out <- ["locals->"]Append[Escape Rhope Name[op]]
+	}
+}
+
+Instruction Stream@C Function[func:out]
+{
+	out <- [func]Statements <<[()]
+}
+
+_If C[func, statement:out]
+{
+	out <- [func]Statements <<[ [[func]Statements >>]Append[ ["\t"]Append[statement] ] ]
+}
+
+Do If@C Function[func,condition,stream:out]
+{
+	cond <- [condition]Make Op[func]
+	out <- [Fold["_If C", [[func
+		]Add Raw Line[ [["if("]Append[cond]]Append[")"] ]
+		]Add Raw Line["{"], [stream]Statements >>]
+		]Add Raw Line["}"]
+	{ Print["Do if done"] }
+
+}
+
+Result Reference@C Function[func,output:out]
+{
+	out <- [["call->params["]Append[output]]Append["]"]
+}
+
+Init Outputs@C Function[func:out]
 {
 	If[[[[func]Outputs >>]Length ] > [0]]
 	{
@@ -85,30 +217,69 @@
 	}
 }
 
-Release Inputs@C Function[func]
+_Release Inputs[string,inputname,inputnum:out]
+{
+	out <- [[[string
+		]Append["\trelease_ref(cdata->params["]
+		]Append[inputnum]
+		]Append["]);\n"]
+}
+
+Release Inputs@C Function[func:out]
 {
 	If[[[[func]Inputs >>]Length ] > [0]]
 	{
-		out <- [["\trelease_ref(->"]Append[ [[func]Outputs >>]Join[" = NULL;\n\tlocals->"] ]]Append[" = NULL;\n"]
+		out <- Fold["_Release Inputs", "", [func]Inputs >>] 
 	}{
 		out <- ""
 	}
 }
 
+_Set Outputs C[string,inputname,inputnum:out]
+{
+	out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[", locals->"]]Append[inputname]]Append[")\n"]]
+}
+
+Set Outputs@C Function[func:out]
+{
+	If[[[[func]Outputs >>]Length ] > [0]]
+	{
+		out <- Fold["_Set Outputs C", "", [func]Outputs >>]
+	}{
+		out <- ""
+	}
+}
+_Output Defs C[string,varname:out]
+{
+	Print[varname]
+	out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"]
+}
+_Var Defs C[string,type,varname:out]
+{
+	Print[type]
+	{Print[varname]}
+	out <- [[[string]Append["\tobject *"]]Append[Escape Rhope Name[varname]]]Append[";\n"]
+}
+
 Definitions@C Function[func:out]
 {
-	
+	Print["Definitions"]
+	out <- [[[Fold["_Definitions C", Fold["_Definitions","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} l_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"]
 }
 
 Text@C Function[func:out]
 {
 	cname <- Escape Rhope Name[[func]Name >>]
-	out <- [[[[[[["Func("
+	out <- [[[[[[[[["Func("
 		]Append[cname]
 		]Append[",\n\tNumParams "]
 		]Append[ [[func]Inputs >>]Length ]
 		]Append[",\n\tCallSpace 32,\n\t"]//TODO: Fill in with calculated callspace value
 		]Append[["l_"]Append[cname]]
 		]Append[")\n\n"]
-		]Append[[func]Init Outputs]
-}
\ No newline at end of file
+		]Append[ [[func]Statements >>]Join[""] ]
+		]Append[[func]Set Outputs]
+		]Append["EndFunc"]
+
+}
+
--- a/datum.c	Mon May 25 23:34:36 2009 -0400
+++ b/datum.c	Tue May 26 23:49:46 2009 +0000
@@ -398,7 +398,7 @@
 						}
 					release_program_ref(worker->def->program);
 				}
-				#ifdef SYLLABLE
+				#ifdef GUI_LIB 
 				else if(adatum->company->type_id == BUILTIN_TYPE_WINDOW)
 				{
 					release_ref(((vis_window *)adatum->c.generic.data)->title);
--- a/datum.h	Mon May 25 23:34:36 2009 -0400
+++ b/datum.h	Tue May 26 23:49:46 2009 +0000
@@ -92,11 +92,11 @@
 #define DATUM_YES	1
 #define DATUM_NO	0
 
-#define BUILTIN_TYPE_SCREEN_CUSTOM		17
-#define BUILTIN_TYPE_CUSTOM_WIDGET		16
-#define BUILTIN_TYPE_CHECKBOX			20
-#define BUILTIN_TYPE_DROPDOWN			21
 #ifdef GUI_LIB
+	#define BUILTIN_TYPE_SCREEN_CUSTOM		17
+	#define BUILTIN_TYPE_CUSTOM_WIDGET		16
+	#define BUILTIN_TYPE_CHECKBOX			20
+	#define BUILTIN_TYPE_DROPDOWN			21
 	#ifdef SYLLABLE
 		#define USER_DEFINED_TYPES			22
 		#define BUILTIN_TYPE_BLUEPRINT		19
--- a/extendlib.rhope	Mon May 25 23:34:36 2009 -0400
+++ b/extendlib.rhope	Tue May 26 23:49:46 2009 +0000
@@ -594,3 +594,46 @@
 		not found <- text
 	}
 }
+
+Blueprint Range
+{
+	Start
+	End
+}
+
+Range[start,end:out]
+{
+	out <- [[Build["Range"]]Start <<[start]]End <<[end]
+}
+
+First@Range[range:out]
+{
+	out <- [range]Start >>
+}
+
+Next@Range[range,val:out,done]
+{
+	next <- [val]+[1]
+	If[[next] < [[range]End >>]]
+	{
+		out <- Val[next]
+	}{
+		done <- range
+	}
+}
+
+Index@Range[range,index:out,notfound]
+{
+	If[[index] < [[range]End >>]]
+	{
+		If[[index] < [[range]Start >>]]
+		{
+			notfound <- index
+		}{
+			out <- index
+		}
+	}{
+		notfound <- index
+	}
+}
+
--- a/interp.c	Mon May 25 23:34:36 2009 -0400
+++ b/interp.c	Tue May 26 23:49:46 2009 +0000
@@ -998,10 +998,12 @@
 	VIS_InitializeCriticalSection(worker_queue_lock);
 	DEBUGPUTS("Initializing text_buf_lock\n");
 	VIS_InitializeCriticalSection(text_buf_lock);
+#ifdef GUI_LIB
 #ifdef SYLLABLE
 	DEBUGPUTS("vis_window_init\n");
 	vis_window_init();
 #endif
+#endif //GUI_LIB
 	VIS_CreateEvent(queue_add_event);//No security parameter, auto reset, starts signaled, no name
 	
 }
@@ -2132,6 +2134,7 @@
 	aworker->input_types[0] = BUILTIN_TYPE_PROGRAM;
 	aworker->input_types[1] = BUILTIN_TYPE_STRING;
 
+#ifdef GUI_LIB
 #ifdef SYLLABLE
 	create_company(prog, "Checkbox", 2, 0, FALSE);
 
@@ -2172,6 +2175,7 @@
 	aworker->input_types[0] = BUILTIN_TYPE_DROPDOWN;
 	aworker->input_types[1] = BUILTIN_TYPE_WHOLE;
 #endif
+#endif
 
 	/*if(current_def > num_defs)
 		num_defs = current_def;
@@ -3085,6 +3089,7 @@
 			{
 				if(worker_entry.instance) {
 					ERRORPRINTF("Error: Needed conversion from %s to %s for input %d of %s in worker %s\n", params[i]->company->name, process_def->program->companylist[process_def->input_types[i]].name, i, process_def->name, worker_entry.instance->def->name);
+					print_stack_trace(worker_entry.instance);
 				} else {
 					DEBUGPRINTF("Error: Needed conversion from %s to %s for input %d of %s\n", params[i]->company->name, process_def->program->companylist[process_def->input_types[i]].name, i, process_def->name);
 					printf("Error: Needed conversion from %s to %s for input %d of %s", params[i]->company->name, process_def->program->companylist[process_def->input_types[i]].name, i, process_def->name);
--- a/makefile	Mon May 25 23:34:36 2009 -0400
+++ b/makefile	Tue May 26 23:49:46 2009 +0000
@@ -1,5 +1,5 @@
-CFLAGS = -DCONSOLE -DGUI_LIB -DSYLLABLE
-OBJS = datum.o dict.o file.o interp.o list.o net.o number.o parser.o saveload.o string.o visuality_cmd.o worker.o vis_threading.o mt19937ar.o buffer.o blueprint.o window.o syl_window.o syl_generic.o
+CFLAGS = -DCONSOLE -DSYLLABLE
+OBJS = datum.o dict.o file.o interp.o list.o net.o number.o parser.o saveload.o string.o visuality_cmd.o worker.o vis_threading.o mt19937ar.o buffer.o blueprint.o
 
 rhope: $(OBJS)
 	g++ -o rhope -lsyllable $(OBJS)
--- a/parser.vistxt	Mon May 25 23:34:36 2009 -0400
+++ b/parser.vistxt	Tue May 26 23:49:46 2009 +0000
@@ -864,6 +864,7 @@
 						If[[before] = [""]]
 						|:
 							Print[[["Found "]Append[delim]]Append[" where a named pipe or literal was expected"]]
+							|: Print[["Near: "]Append[ [afterdelim]Slice[40]]] :|
 						:||:
 							If[[before]Contains[[params(1)]Global Separator >>]]
 							|:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testc.rhope	Tue May 26 23:49:46 2009 +0000
@@ -0,0 +1,40 @@
+Import cbackend.rhope
+
+Main[]
+{
+	base <- [[[[[[C Function["Fib",("n"),("out"),""]
+		]Allocate Var["work1", "Any Type"]
+		]Allocate Var["work2", "Any Type"]
+		]Null["out"]
+		]Method Call["<", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_2"]]]]
+		]Move[Result[0], "work1"]
+		]Method Call["If", [()]Append["work1"]]
+	{Print["base done"] }
+
+	 stream1 <- [[[base]Instruction Stream
+		]Release[Result[0]]
+		]Move[AddRef[Constant["const_1"]], "out"]
+	{Print["stream1 done"]}
+
+	stream2 <- [[[[[[base]Instruction Stream
+		]Release[Result[0]]
+		]Method Call["-", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_1"]]]]
+		]Move[Result[0], "work1"]
+		]Method Call["-", [[()]Append[AddRef["n"]]]Append[AddRef[Constant["const_2"]]]]
+		]Move[Result[0], "work2"]
+	{Print["stream2 done"]}
+
+	func <- [[base
+		]Do If[Result[0], stream1]
+		]Do If[Result[1], stream2]
+	{Print["func done"] }
+		
+	Print[[func]Definitions]
+	{
+	Print[[func]Text] }
+}
+
+Foo[]
+{
+}
+