Mercurial > repos > rhope
diff number.rhope @ 184:c6ba9fe45910
Strip _c from filenames of current compiler
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 27 Jul 2011 21:28:43 -0700 |
parents | number_c.rhope@fd06fb07762a |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/number.rhope Wed Jul 27 21:28:43 2011 -0700 @@ -0,0 +1,283 @@ + + +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 Inline[func,op,type,a,b,outvar:out] +{ + ,ina <- [ + [[func]Move[a, outvar] + ]Copy[outvar] + ]Read Field[Cast[outvar, type], "Num"] + { ,inb <- [~]Read Field[Cast[Strip Addref[b], type], "Num"] + { ,outa <- [~]Write Field[Cast[outvar, type], "Num"] + { out <- [op]Call[~, ina, inb, outa] }}} +} + +Compile Number Comp Inline[func,op,type,a,b,outvar:out] +{ + ,ina <- [ + [func]Allocate Var["compres", [Type Instance["Int32"]]Set Variant["Naked"]] + ]Read Field[Cast[Strip Addref[a], type], "Num"] + { ,inb <- [~]Read Field[Cast[Strip Addref[b], type], "Num"] + { after op <- [op]Call[~, ina, inb, "compres"] }} + + [after op]Do If["compres", [[after op]Instruction Stream]Move[Constant["Boolean_Yes"], outvar]] + { out <- [~]Do If[NotCond["compres"], [[after op]Instruction Stream]Move[Constant["Boolean_No"], outvar]] } +} + +Compile Number Inline Check[func,op,type a,type b,a,b,outvar:out,no inline] +{ + opmap <- [[[[[[[[Dictionary[] + ]Set["+", Add[?]] + ]Set["-", Sub[?]] + ]Set["*", Multiply[?]] + ]Set["/", Divide[?]] + ]Set["LShift", DoLShift[?]] + ]Set["RShift", DoRShift[?]] + ]Set["&", BitAnd[?]] + ]Set["|", BitOr[?]] + backend op <- [opmap]Index[op] + { inline func <- Val[Compile Number Inline[?]] } + { + comp opmap <- [[[[[[Dictionary[] + ]Set["<", CompLess[?]] + ]Set[">", CompGreater[?]] + ]Set["=", CompEqual[?]] + ]Set["<=", CompLessEqual[?]] + ]Set[">=", CompGreaterEqual[?]] + ]Set["!=", CompNotEqual[?]] + backend op, no inline <- [comp opmap]Index[op] + { inline func <- Val[Compile Number Comp Inline[?]] } + } + + + Val[backend op] + { + signed?,int,size,no inline <- [[type a]Name >>]Partition["Int"] + { + ,no inline <- If[[[signed?] = ["U"]] Or [[signed?] = [""]]] + { + ,no inline <- [("8","16","32","64")]Find[=[size, ?]] + { + //TODO: Do an inline type check/conversion rather than falling back on a standard call + ,no inline <- If[[type b] = [type a]] + { + out <- [inline func]Call[func, backend op, type a, a, b, outvar] + } + } + } + } + } +} + +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] +} + +Supports Type?[backend,type:out] +{ + [[backend]Supported Number Types]Find[=[type,?]] + { out <- Yes } + { out <- No } +} + +_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, ?, ""], ~, Filter[Legal Conversions[type], [backend]Supports Type?[?]]] + { + almost <- Fold[Compile Conversion Method[?, type, ?, "Trunc "], ~, Filter[Truncations[type], [backend]Supports 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] +{ + out <- Fold[_Register Number Methods[?], program, [program]Supported Number Types] +} +