view number_c.rhope @ 135:18a4403fe576

Javascript backend can now produce broken output. Needs fixes plus port of standard lib
author Mike Pavone <pavone@retrodev.com>
date Sun, 14 Nov 2010 03:09:49 -0500
parents 7361d70fbba6
children fc3815b7462f
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]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["Boolean_Yes", Yes]
	]Register Constant["Boolean_No", No]
	]Allocate Var["compres", [Type Instance["Int32"]]Set Variant["Naked"]]
	
	,ina <- [func]Read Field["a", "Num"]
	{ ,inb <- [~]Read Field["b", "Num"]
	{ after op <- [backend func]Call[~, ina, inb, "compres"] }}

	[after op]Do If["compres", [[after op]Instruction Stream]Move[Constant["Boolean_Yes"], "out"]]
	{ after out <- [~]Do If[NotCond["compres"], [[after op]Instruction Stream]Move[Constant["Boolean_No"], "out"]] }
	
	out <- [backend]Store Function[ [[after out]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[?]] ]
		]Append[ [("&")]Append[BitAnd[?]] ]
		]Append[ [("|")]Append[BitOr[?]] ]
	//(("+", 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]
{
	out <- Fold[_Generate Number Methods[?], backend, [backend]Supported Number Types]
}

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]
}