changeset 40:789a146a48e1

Started adding support for naked values in user defined objects
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Oct 2009 01:01:26 -0400
parents 3d92bc1352c2
children 1b86a1ee500a
files cbackend.rhope nworker.rhope parser_old.rhope runtime/object.c runtime/object.h
diffstat 5 files changed, 148 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/cbackend.rhope	Thu Oct 08 00:37:24 2009 -0400
+++ b/cbackend.rhope	Fri Oct 09 01:01:26 2009 -0400
@@ -154,13 +154,36 @@
 			}{
 				typename <- "Array"
 			}
+			primitive <- No
 		}{
-			typename <- [type]Name >>
+			,regulartype <- [("Naked","Raw Pointer")]Find[variant]
+			{
+				[("Int32","Int16","Int8")]Find[[type]Name >>]
+				{
+					primitive <- Yes
+					[[type]Name >>]Slice[3] {}
+					{ typename <- [["int"]Append[~]]Append["_t"] }
+				}{
+					,regulartype <- [("UInt32","UInt16","UInt8")]Find[[type]Name >>]
+					{
+						primitive <- Yes
+						[[type]Name >>]Slice[4] {}
+						{ typename <- [["uint"]Append[~]]Append["_t"] }
+					}
+				}
+			}
+			
+			Val[regulartype]
+			{
+				typename <- [type]Name >>
+				primitive <- No
+			}
 		}
 	}{
 		typename <- type
 		param <- "Any Type"
 		variant <- "boxed"
+		primitive <- No
 	}
 	Val[typename]
 	{ array <- "" }
@@ -170,7 +193,12 @@
 	}{
 		[("Naked","Raw Pointer")]Find[variant]
 		{
-			prefix <- "nt_"
+			If[primitive]
+			{
+				prefix <- ""
+			}{
+				prefix <- "nt_"
+			}
 		}{
 			prefix <- "t_"
 		}
@@ -182,7 +210,13 @@
 			postfix <- " *"
 		}
 	}
-	out <- [[prefix]Append[Escape Rhope Name[typename]]]Append[postfix]
+	If[primitive]
+	{
+		escaped <- Val[typename]	
+	}{
+		escaped <- Escape Rhope Name[typename]
+	}
+	out <- [[prefix]Append[escaped]]Append[postfix]
 }
 
 _Type Def C Type[text,field:out]
@@ -484,6 +518,31 @@
 	out <- [func]Add Statement[ [dest]Append[[[" = copy_object("]Append[dest]]Append[")"]] ]
 }
 
+Box@C Function[func,pdest,psource,type:out]
+{
+	dest <- [pdest]Make Op[func]
+	source <- [psource]Make Op[func]
+	out <- [func]Add Statement[ 
+		[[[[[dest
+		]Append[" = naked_to_boxed("]
+		]Append[ [[func]Type Registry >>]Type ID[[type]Name >>] ]
+		]Append[", &"]
+		]Append[source]
+		]Append[")"] ]
+}
+
+Unbox@C Function[func,pdest,psource:out]
+{
+	dest <- [pdest]Make Op[func]
+	source <- [psource]Make Op[func]
+	out <- [func]Add Statement[ 
+		[[[["boxed_to_naked("
+		]Append[source]
+		]Append[", &"]
+		]Append[dest]
+		]Append[")"] ]
+}
+
 _Function Arg C[func,val,inputnum:out]
 {
 	out <- [func]Add Statement[
--- a/nworker.rhope	Thu Oct 08 00:37:24 2009 -0400
+++ b/nworker.rhope	Fri Oct 09 01:01:26 2009 -0400
@@ -766,28 +766,46 @@
 Make Init[func,field:out]
 {
 	name <- [field]Index[0]
-	out <- [func]Set Field Null["obj", name]
+	variant <- [[field]Index[1]]Variant >>
+	If[[variant] = ["Boxed"]]
+	{
+		out <- [func]Set Field Null["obj", name]
+	}{
+		out <- func
+	}
 }
 
 Make Copy[func,field:out]
 {
 	name <- [field]Index[0]
-	got <- [func]Read Field["obj", name] {}
-	{ 
-		stream <- [[got]Instruction Stream
-			]AddRef No Dest[~] 
-		out <- [got]Do If[~, stream]
+	variant <- [[field]Index[1]]Variant >>
+	If[[variant] = ["Boxed"]]
+	{
+		got <- [func]Read Field["obj", name] {}
+		{ 
+			stream <- [[got]Instruction Stream
+				]AddRef No Dest[~] 
+			out <- [got]Do If[~, stream]
+		}
+	}{
+		out <- func
 	}
 }
 
 Make Cleanup[func,field:out]
 {
 	name <- [field]Index[0]
-	got <- [func]Read Field["obj", name] {}
-	{ 
-		stream <- [[got]Instruction Stream
-			]Release[~] 
-		out <- [got]Do If[~, stream]
+	variant <- [[field]Index[1]]Variant >>
+	If[[variant] = ["Boxed"]]
+	{
+		got <- [func]Read Field["obj", name] {}
+		{ 
+			stream <- [[got]Instruction Stream
+				]Release[~] 
+			out <- [got]Do If[~, stream]
+		}
+	}{
+		out <- func
 	}
 }
 
--- a/parser_old.rhope	Thu Oct 08 00:37:24 2009 -0400
+++ b/parser_old.rhope	Fri Oct 09 01:01:26 2009 -0400
@@ -666,14 +666,39 @@
 	out <- [[Build["Blueprint Field"]]Name <<[name]]Type <<[type]
 }
 
-Process Blueprint Field[list,field,delim:out]
+Process Blueprint Field[list,field,params:out]
 {
-	parts <- [field]Split[delim]
-	If[[[parts]Length] > [1]]
+	,name <- [field]Get DString[[params]List Begin >>]
 	{
-		out <- [list]Append[New@Blueprint Field[[parts]Index[1], [parts]Index[0]]]
-	}{
-		out <- [list]Append[New@Blueprint Field[[parts]Index[0], "Any Type"]]
+		type info <- Parse List[~,params,()]
+		type <- [type info]Index[0]
+		{
+			If[[Type Of[~]] = ["Named Pipe Node"]]
+			{
+				before variant <- Type Instance[[type]Name >>]
+			}{
+				before variant <-  [type]Params <<[ Map[[type]Params >>, "Remove Named Pipe Node"] ]
+			}
+			variant <- [type info]Index[1]
+			{
+				,warn <- If[[Type Of[~]] = ["Named Pipe Node"]]
+				{
+					fieldtype,warn <- [before variant]Set Variant[[variant]Name >>]
+				}
+				Val[warn]
+				{
+					Print[[[["Warning: Invalid variant for type "]Append[[before variant]Name >>]]Append[" on field "]]Append[name]]
+					fieldtype <- Val[before variant]
+				}
+			}{
+				fieldtype <- Val[before variant]
+			}
+		}{
+			fieldtype <- Type Instance["Any Type"]
+		}
+		out <- [list]Append[New@Blueprint Field[name, fieldtype]]
+	} {} {} {
+		out <- [list]Append[New@Blueprint Field[name, Type Instance["Any Type"]]]
 	}
 }
 
@@ -779,7 +804,7 @@
 		{
 			body lines <- [body]Split["\n"]
 			more lines <- [[[body lines]Length] - [1]] + [name lines]
-			fields <- Fold[["Process Blueprint Field"]Set Input[2, [params]Blueprint Type Delim >>], New@List[], Filter[Map[body lines, ["Trim"]Set Input[1,"\n\r\t "]], "Filter Empty"]]
+			fields <- Fold[["Process Blueprint Field"]Set Input[2, params], New@List[], Filter[Map[body lines, ["Trim"]Set Input[1,"\n\r\t "]], "Filter Empty"]]
 			new tree <- [tree]Blueprints << [ [[tree]Blueprints >>]Set[name, New@Blueprint Definition[name, fields]] ]
 			out <- Null[~, params, new tree, [lines] + [more lines]]
 		} {} {
--- a/runtime/object.c	Thu Oct 08 00:37:24 2009 -0400
+++ b/runtime/object.c	Fri Oct 09 01:01:26 2009 -0400
@@ -210,6 +210,29 @@
 	return copy;
 }
 
+object * naked_to_boxed(uint32_t type, void * rawdata)
+{
+	object * dest;
+	blueprint * bp = get_blueprint_byid(type);
+	if(!bp->size)
+		return NULL; //We don't know how big a naked multi-size object is so we can't do anything with it
+	dest = alloc_object(bp);
+	memcpy(((char *)dest) + sizeof(object), rawdata, bp->size);
+	dest->bprint = bp;
+	rh_atomic_set(dest, refcount, 1);
+	bp->copy(dest);
+	return dest;
+}
+
+void boxed_to_naked(object * src, void * dest)
+{
+	blueprint * bp = get_blueprint(src);
+	if(!bp->size)
+		return; //We don't know how big a naked multi-size object is so we can't do anything with it
+	memcpy(dest, ((char *)src) + sizeof(object), bp->size);
+	bp->copy(src);
+}
+
 void free_object(object * obj)
 {
 	blueprint * bp = get_blueprint(obj);
--- a/runtime/object.h	Thu Oct 08 00:37:24 2009 -0400
+++ b/runtime/object.h	Fri Oct 09 01:01:26 2009 -0400
@@ -77,6 +77,8 @@
 returntype convert_to(uint32_t convertto, calldata * params);
 returntype convert_from(uint32_t convertfrom, calldata * params);
 object * copy_object(object * tocopy);
+object * naked_to_boxed(uint32_t type, void * rawdata);
+void boxed_to_naked(object * src, void * dest);
 returntype coerce_value(uint32_t type, calldata * params);
 blueprint * get_blueprint_byid(uint32_t type);