Skip to content

Commit 501c99a

Browse files
committed
copilot-c99: Refactor variable update functionality. Refs #431.
The previous commit introduced functionality for handling array arguments in trigger argument functions as a special case. The special-casing needed to handle array arguments in trigger argument functions is nearly identical to the special-casing needed in stream generator functions. This patch refactors the code to share the special-casing in common between the two sorts of functions.
1 parent 25f3131 commit 501c99a

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

copilot-c99/src/Copilot/Compile/C99/CodeGen.hs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,24 @@ mkStep cSettings streams triggers exts =
163163
(triggerDeclns, triggerStmts) =
164164
unzip $ map mkTriggerCheck triggers
165165

166+
-- Update the value of a variable with the result of calling a function that
167+
-- generates the next value in a stream expression. If the type of the
168+
-- variable is an array, then we cannot perform a direct C assignment, so
169+
-- we instead pass the variable as an output array to the function.
170+
updateVar :: C.Ident -> C.Ident -> Type a -> C.Expr
171+
updateVar varName genName (Array _) =
172+
C.Funcall (C.Ident genName) [C.Ident varName]
173+
updateVar varName genName _ =
174+
C.AssignOp C.Assign (C.Ident varName) (C.Funcall (C.Ident genName) [])
175+
166176
-- Write code to update global stream buffers and index.
167177
mkUpdateGlobals :: Stream -> (C.Decln, C.Stmt, C.Stmt, C.Stmt)
168178
mkUpdateGlobals (Stream sId buff _expr ty) =
169179
(tmpDecln, tmpAssign, bufferUpdate, indexUpdate)
170180
where
171181
tmpDecln = C.VarDecln Nothing cTy tmpVar Nothing
172182

173-
tmpAssign = case ty of
174-
Array _ -> C.Expr $ C.Funcall (C.Ident $ generatorName sId)
175-
[ C.Ident tmpVar ]
176-
_ -> C.Expr $ C.Ident tmpVar C..= val
183+
tmpAssign = C.Expr $ updateVar tmpVar (generatorName sId) ty
177184

178185
bufferUpdate = case ty of
179186
Array _ -> C.Expr $ memcpy dest (C.Ident tmpVar) size
@@ -193,7 +200,6 @@ mkStep cSettings streams triggers exts =
193200
tmpVar = streamName sId ++ "_tmp"
194201
buffVar = C.Ident $ streamName sId
195202
indexVar = C.Ident $ indexName sId
196-
val = C.Funcall (C.Ident $ generatorName sId) []
197203
cTy = transType ty
198204

199205
-- Make code that copies an external variable to its local one.
@@ -273,18 +279,11 @@ mkStep cSettings streams triggers exts =
273279

274280
assign :: C.Ident -> C.Ident -> UExpr -> C.Expr
275281
assign aTempName aArgName (UExpr { uExprType = ty }) =
276-
case ty of
277-
Array _ -> argCall aArgName [C.Ident aTempName]
278-
_ -> C.AssignOp C.Assign
279-
(C.Ident aTempName)
280-
(argCall aArgName [])
282+
updateVar aTempName aArgName ty
281283

282284
aArgNames :: [C.Ident]
283285
aArgNames = take (length args) (argNames name)
284286

285-
argCall :: C.Ident -> [C.Expr] -> C.Expr
286-
argCall name' = C.Funcall (C.Ident name')
287-
288287
-- Build an expression to pass a temporary variable as argument
289288
-- to a trigger handler.
290289
--

copilot-c99/src/Copilot/Compile/C99/Compile.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ compileC cSettings spec = C.TransUnit declns funs
119119
accessDecln (Stream sId buff _ ty) = mkAccessDecln sId ty buff
120120

121121
streamGen :: Stream -> C.FunDef
122-
streamGen (Stream sId _ expr ty@(Array _)) =
123-
mkGenFunArray (generatorName sId) (generatorOutputArgName sId) expr ty
124-
streamGen (Stream sId _ expr ty) = mkGenFun (generatorName sId) expr ty
122+
streamGen (Stream sId _ expr ty) =
123+
exprGen (generatorName sId) (generatorOutputArgName sId) expr ty
125124

126125
triggerGen :: Trigger -> [C.FunDef]
127126
triggerGen (Trigger name guard args) = guardDef : argDefs
@@ -131,9 +130,18 @@ compileC cSettings spec = C.TransUnit declns funs
131130

132131
argGen :: String -> UExpr -> C.FunDef
133132
argGen argName (UExpr ty expr) =
134-
case ty of
135-
Array _ -> mkGenFunArray argName (argName ++ "_output") expr ty
136-
_ -> mkGenFun argName expr ty
133+
exprGen argName (argName ++ "_output") expr ty
134+
135+
-- Create a function that calculates the current value generated by an
136+
-- expression `expr` of type `ty`. The generator treats arrays
137+
-- specially, and the function takes an output array as a parameter.
138+
-- The second identifier `outputArrName` is not used if `expr` is not an
139+
-- array.
140+
exprGen :: C.Ident -> C.Ident -> Expr a -> Type a -> C.FunDef
141+
exprGen funName outputArrName expr ty@(Array _) =
142+
mkGenFunArray funName outputArrName expr ty
143+
exprGen funName _ expr ty =
144+
mkGenFun funName expr ty
137145

138146
-- | Generate the .h file from a 'Spec'.
139147
compileH :: CSettings -> Spec -> C.TransUnit

0 commit comments

Comments
 (0)