diff number_c.rhope @ 92:e73a93fb5de1

Beginning of port of compiler to itself, some bugfixes and a refcount optimization
author Mike Pavone <pavone@retrodev.com>
date Mon, 02 Aug 2010 00:58:55 -0400
parents
children 7361d70fbba6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/number_c.rhope	Mon Aug 02 00:58:55 2010 -0400
@@ -0,0 +1,207 @@
+
+
+Compile Number Method[backend, op, type:out]
+{
+	name <- [[[op]Index[0]]Append["@"]]Append[type]
+	backend func <- [op]Index[1]
+	type inst <- Type Instance[type]
+	func <- [[[[backend]Create Function[name,("a","b"), ("out"), "rhope"]
+	]Set Input Type[type inst, 0]
+	]Set Input Type[type inst, 1]
+	]Set Output Type[type inst, 0]
+	
+	,ina <- [[func]Copy["a"]
+	]Read Field["a", "Num"]
+	{ ,inb <- [~]Read Field["b", "Num"]
+	{ ,outa <- [~]Write Field["a", "Num"]
+	{ after op <- [backend func]Call[~, ina, inb, outa] }}}
+	
+	out <- [backend]Store Function[ [[after op]Move["a","out"]]Release["b"] ]
+	
+}
+
+Compile Number Comp Method[backend, op, type:out]
+{
+	name <- [ [ [op]Index[0] ]Append["@"] ]Append[type]
+	backend func <- [op]Index[1]
+	type inst <- Type Instance[type]
+	func <- [[[[[[ [backend]Create Function[name,("a","b"), ("out"), "rhope"]
+	]Set Input Type[type inst, 0]
+	]Set Input Type[type inst, 1]
+	]Set Output Type[Type Instance["Boolean"], 0]
+	]Register Constant["Blueprint_Boolean", Type Instance["Boolean"]]
+	]Call["Build", [()]Append[Constant["Blueprint_Boolean"]]]
+	]Move[Result[0], "out"]
+	
+	,ina <- [func]Read Field["a", "Num"]
+	{ ,inb <- [~]Read Field["b", "Num"]
+	{ ,outa <- [~]Write Field["out", "Val"]
+	{ after op <- [backend func]Call[~, ina, inb, outa] }}}
+	
+	out <- [backend]Store Function[ [[after op]Release["a"]]Release["b"] ]
+	
+}
+
+Compile Conversion Method[backend,intype,outtype,prefix:out]
+{
+	func <- [[[[[[backend]Create Function[[prefix]Append[[[outtype]Append["@"]]Append[intype]], ("in"), ("out"), "rhope"]
+	]Set Input Type[Type Instance[intype], 0]
+	]Set Output Type[Type Instance[outtype], 0]
+	]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]]
+	]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]]
+	]Move[Result[0], "out"]
+
+	,src <- [func]Read Field["in", "Num"]
+	{ ,dst <- [~]Write Field["out", "Num"]
+	{ ffunc <- [[~]Move[src, dst]]Release["in"] }}
+
+	out <- [backend]Store Function[ffunc]
+}
+
+Compile Abs UInt Method[backend,type:out]
+{
+	outtype <- ["U"]Append[type]
+	func <- [[[[[[[[[backend]Create Function[["Abs UInt@"]Append[type], ("in"), ("out"), "rhope"]
+	]Set Input Type[Type Instance[type], 0]
+	]Set Output Type[Type Instance[["U"]Append[type]], 0]
+	]Register Constant[["Blueprint_"]Append[outtype], Type Instance[outtype]]
+	]Allocate Var["abs", Type Instance[type]]
+	]Call["Build", [()]Append[ Constant[["Blueprint_"]Append[outtype]] ]]
+	]Move[Result[0], "out"]
+	]Call["Abs", [()]Append["in"]]
+	]Move[Result[0], "abs"]
+	{ Print["After Call to Abs"] }
+
+	,src <- [func]Read Field["abs", "Num"]
+	{ ,dst <- [~]Write Field["out", "Num"]
+	{ ffunc <- [[~]Move[src, dst]]Release["abs"] }}
+
+	out <- [backend]Store Function[ffunc]
+}
+
+_Generate Number Methods[backend, type:out]
+{
+	//Old crappy parser doesn't like Worker literals in List literals, work around for now
+	opmap <- [[[[[[()
+		]Append[ [("+")]Append[Add[?]] ]
+		]Append[ [("-")]Append[Sub[?]] ]
+		]Append[ [("*")]Append[Multiply[?]] ]
+		]Append[ [("/")]Append[Divide[?]] ]
+		]Append[ [("LShift")]Append[DoLShift[?]] ]
+		]Append[ [("RShift")]Append[DoRShift[?]] ]
+	//(("+", Add[?]), ("-", Sub[?]), ("*", Multiply[?]), ("/", Divide[?]), ("LShift", DoLShift[?]), ("RShift", DoRShift[?]))
+	compops <- [[[[[[()
+		]Append[ [("<")]Append[CompLess[?]] ]
+		]Append[ [(">")]Append[CompGreater[?]] ]
+		]Append[ [("=")]Append[CompEqual[?]] ]
+		]Append[ [("<=")]Append[CompLessEqual[?]] ]
+		]Append[ [(">=")]Append[CompGreaterEqual[?]] ]
+		]Append[ [("!=")]Append[CompNotEqual[?]] ]
+	//(("<", CompLess[?]), (">", CompGreater[?]), ("=", CompEqual[?]), ("<=", CompLessEqual[?]), (">=", CompGreaterEqual[?]), ("!=", CompNotEqual[?]))
+	
+	Fold[Compile Number Method[?, ?, type], backend, opmap]
+	{ Fold[Compile Number Comp Method[?, ?, type], ~, compops]
+	{ Fold[Compile Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]]
+	{ 
+		almost <- Fold[Compile Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]] 
+		If[[type]Starts With["I"]]
+		{
+			out <- Compile Abs UInt Method[almost,type]
+		}{
+			out <- Val[almost]
+		}
+	}}}
+}
+		
+Generate Number Methods[backend:out]
+{	
+	numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64")
+	
+	out <- Fold[_Generate Number Methods[?], backend, numtypes]
+}
+
+Register Number Method[program, method, type, outtype:out]
+{
+	name <- [[method]Append["@"]]Append[type]
+	out <- [[program]Register Worker[name, "rhope", 2, 1]
+	]Bind Worker[name, 
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("left","right")]
+		]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ]
+		]Outputs <<[("out")]
+		]Output Types <<[ [()]Append[Type Instance[outtype]] ]
+		]Builtin? <<[Yes]
+	]
+}
+
+Register Conversion Method[program, intype, outtype,prefix:out]
+{
+	name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]]
+	out <- [[program]Register Worker[name, "rhope", 1, 1]
+	]Bind Worker[name,
+		[[[[[NWorker["rhope"]
+		]Inputs <<[("in")]
+		]Input Types <<[ [()]Append[Type Instance[intype]] ]
+		]Outputs <<[("out")]
+		]Output Types <<[ [()]Append[Type Instance[outtype]] ]
+		]Builtin? <<[Yes]
+	]
+}
+
+Legal Conversions[type:convs]
+{
+	bigger <- Map[Filter[(16,32,64), >[?, size]], String[?]]
+    base convs <- Map[bigger, Append["Int", ?]]
+    If[[type]Starts With["U"]]
+    {
+            [type]Slice[4] {}
+            { size <- Int32[~] }
+            convs <- Concatenate[base convs, Map[bigger, Append["UInt", ?]]]
+    }{
+            [type]Slice[3] {}
+            { size <- Int32[~] }
+            convs <- Val[base convs]
+    }
+}
+
+Truncations[type:truncs]
+{
+	u <- [type]Partition["Int"] {} {}
+	{ size <- Int32[~] }
+	truncs <- Map[Map[Filter[(8,16,32), <[?, size]], String[?]] Append[[u]Append["Int"], ?]]
+}
+
+_Register Number Methods[program,type:out]
+{
+	methods <- ("+", "-", "*", "/", "LShift", "RShift")
+	compmethods <- ("<", ">", "=", "<=", ">=", "!=")
+	register <- Val[Register Number Method[?, ?, type]]
+	Fold[[register]Set Input[3, type], program, methods]
+	{ Fold[[register]Set Input[3, "Boolean"], ~, compmethods]
+ 	{ Fold[Register Conversion Method[?, type, ?, ""], ~, Legal Conversions[type]]
+	{ 
+		almost <- Fold[Register Conversion Method[?, type, ?, "Trunc "], ~, Truncations[type]]
+		If[[type]Starts With["I"]]
+		{
+			name <- ["Abs UInt@"]Append[type]
+			out <- [[almost]Register Worker[name, "rhope", 1, 1]
+			]Bind Worker[name
+				[[[[[NWorker["rhope"]
+				]Inputs <<[("in")]
+				]Input Types <<[ [()]Append[Type Instance[type]] ]
+				]Outputs <<[("out")]
+				]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ]
+				]Builtin? <<[Yes]
+			]
+		}{
+			out <- Val[almost]
+		}
+	}}}
+}
+
+Register Number Methods[program:out]
+{
+	numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64")
+	out <- Fold[_Register Number Methods[?], program, numtypes]
+}
+