on master help sign in
Compile to nested if rather than switch for small cases
commitce9fa938a3434ae91fb4d752dbdfb23be819112f authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSun 9 Dec 2012, 18:44 (14 years ago) parent47fb39a
1 file changed+26−4
src/IRTS/CodegenC.hs+26−4
src/IRTS/CodegenC.hs+26−4
@@ -124,6 +124,20 @@ bcc i (PROJECT l loc a) = indent i ++ "PROJECT(vm, " ++ creg l ++ ", " ++ show l
124124 ", " ++ show a ++ ");\n"
125125bcc i (PROJECTINTO r t idx)
126126 = indent i ++ creg r ++ " = GETARG(" ++ creg t ++ ", " ++ show idx ++ ");\n"
127+bcc i (CASE True r code def)
128+ | length code < 4 = showCase i def code
129+ where
130+ showCode :: Int -> [BC] -> String
131+ showCode i bc = "{\n" ++ indent i ++ concatMap (bcc (i + 1)) bc ++
132+ indent i ++ "}\n"
133+
134+ showCase :: Int -> Maybe [BC] -> [(Int, [BC])] -> String
135+ showCase i Nothing [(t, c)] = showCode i c
136+ showCase i (Just def) [] = showCode i def
137+ showCase i def ((t, c) : cs)
138+ = "if (CTAG(" ++ creg r ++ ") == " ++ show t ++ ") " ++ showCode i c
139+ ++ "else " ++ showCase i def cs
140+
127141bcc i (CASE safe r code def)
128142 = indent i ++ "switch(" ++ ctag safe ++ "(" ++ creg r ++ ")) {\n" ++
129143 concatMap (showCase i) code ++
@@ -140,10 +154,12 @@ bcc i (CASE safe r code def)
140154 ++ concatMap (bcc (i+1)) c ++ indent (i + 1) ++ "break;\n"
141155bcc i (CONSTCASE r code def)
142156 | intConsts code
143− = indent i ++ "switch(GETINT(" ++ creg r ++ ")) {\n" ++
144− concatMap (showCase i) code ++
145− showDef i def ++
146− indent i ++ "}\n"
157+-- = indent i ++ "switch(GETINT(" ++ creg r ++ ")) {\n" ++
158+-- concatMap (showCase i) code ++
159+-- showDef i def ++
160+-- indent i ++ "}\n"
161+ = concatMap (iCase (creg r)) code ++
162+ indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
147163 | strConsts code
148164 = concatMap (strCase ("GETSTR(" ++ creg r ++ ")")) code ++
149165 indent i ++ "{\n" ++ showDefS i def ++ indent i ++ "}\n"
@@ -168,6 +184,12 @@ bcc i (CONSTCASE r code def)
168184 biCase bv (BI b, bc) =
169185 indent i ++ "if (bigEqConst(" ++ bv ++ ", " ++ show b ++ ")) {\n"
170186 ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
187+ iCase v (I b, bc) =
188+ indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
189+ ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
190+ iCase v (Ch b, bc) =
191+ indent i ++ "if (GETINT(" ++ v ++ ") == " ++ show b ++ ") {\n"
192+ ++ concatMap (bcc (i+1)) bc ++ indent i ++ "} else\n"
171193
172194 showCase i (t, bc) = indent i ++ "case " ++ show t ++ ":\n"
173195 ++ concatMap (bcc (i+1)) bc ++