view number.rhope @ 168:d2b941f82d74

Fix type of list constants in inference pass and return type of some Array related workers
author Mike Pavone <pavone@retrodev.com>
date Sun, 01 May 2011 18:41:17 -0700
parents e73a93fb5de1
children
line wrap: on
line source



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]Do[ 
				[
					[
						[
							[()]Append[~]
						]Append[ina]
					]Append[inb]
				]Append[outa] ]
	]Index[0] }}}
	
	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]Do[ 
				[
					[
						[
							[()]Append[~]
						]Append[ina]
					]Append[inb]
				]Append[outa] ]
	]Index[0] }}}
	
	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]
{
	opmap <- (("+", "Add"), ("-", "Sub"), ("*", "Multiply"), ("/", "Divide"), ("LShift", "DoLShift"), ("RShift", "DoRShift"))
	compops <- (("<", "CompLess"), (">", "CompGreater"), ("=", "CompEqual"), ("<=", "CompLessEqual"), (">=", "CompGreaterEqual"), ("!=", "CompNotEqual"))
	
	Fold[["Compile Number Method"]Set Input[2, type], backend, opmap]
	{ Fold[["Compile Number Comp Method"]Set Input[2, type], ~, compops]
	{ Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]]
	{ 
		almost <- Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, "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 <- Filter[(16,32,64), [">"]Set Input[1, size]]
        base convs <- Map[bigger, ["Append"]Set Input[0, "Int"]]
        If[[type]Starts With["U"]]
        {
                [type]Slice[4] {}
                { size <- <String@Whole Number[~] }
                convs <- Concatenate[base convs, Map[bigger, ["Append"]Set Input[0, "UInt"]]]
        }{
                [type]Slice[3] {}
                { size <- <String@Whole Number[~] }
                convs <- Val[base convs]
        }
}

Truncations[type:truncs]
{
	,u <- [type]Get DString["Int"]
	{ size <- <String@Whole Number[~] }
	truncs <- Map[Filter[(8,16,32), ["<"]Set Input[1, size]], ["Append"]Set Input[0, [u]Append["Int"]]]
}

_Register Number Methods[program,type:out]
{
	methods <- ("+", "-", "*", "/", "LShift", "RShift")
	compmethods <- ("<", ">", "=", "<=", ">=", "!=")
	register <- ["Register Number Method"]Set Input[2, type]
	Fold[[register]Set Input[3, type], program, methods]
	{ Fold[[register]Set Input[3, "Boolean"], ~, compmethods]
 	{ Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]]
	{ 
		almost <- Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, "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]
}