diff number_c.rhope @ 169:fd06fb07762a

Basic inlining of arithmetic and comparison operations on integer types
author Mike Pavone <pavone@retrodev.com>
date Sun, 08 May 2011 01:37:44 -0700
parents fc3815b7462f
children
line wrap: on
line diff
--- a/number_c.rhope	Sun May 01 18:41:17 2011 -0700
+++ b/number_c.rhope	Sun May 08 01:37:44 2011 -0700
@@ -20,6 +20,74 @@
 	
 }
 
+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]