diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index 199e694..036a7f5 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -17,7 +17,7 @@ with this program. If not, see . -} -- | Generate C code from the mangled AST. -module GenerateC (cgenOps, cgenType, cintroduceSpec, cPreReq, genComma, genCPasses, generate, generateC, genLeftB, genMeta, genName, genRightB, seqComma, withIf ) where +module GenerateC (cgenOps, cgenReplicatorLoop, cgenType, cintroduceSpec, cPreReq, genComma, genCPasses, generate, generateC, genLeftB, genMeta, genName, genRightB, seqComma, withIf ) where import Data.Char import Data.Generics @@ -91,6 +91,8 @@ cgenOps = GenOps { genInputItem = cgenInputItem, genIntrinsicFunction = cgenIntrinsicFunction, genIntrinsicProc = cgenIntrinsicProc, + genListLiteral = cgenListLiteral, + genListSize = cgenListSize, genLiteral = cgenLiteral, genLiteralRepr = cgenLiteralRepr, genMissing = cgenMissing, @@ -511,6 +513,12 @@ genLitSuffix A.UInt64 = tell ["ULL"] genLitSuffix A.Real32 = tell ["F"] genLitSuffix _ = return () +cgenListLiteral :: [A.Expression] -> A.Type -> CGen() +cgenListLiteral _ _ = call genMissing "C backend does not yet support lists" + +cgenListSize :: A.Variable -> CGen () +cgenListSize _ = call genMissing "C backend does not yet support lists" + cgenLiteralRepr :: A.LiteralRepr -> A.Type -> CGen () cgenLiteralRepr (A.RealLiteral m s) t = tell [s] >> genLitSuffix t cgenLiteralRepr (A.IntLiteral m s) t @@ -533,7 +541,8 @@ cgenLiteralRepr (A.RecordLiteral _ es) _ = do genLeftB seqComma $ map (call genUnfoldedExpression) es genRightB - +cgenLiteralRepr (A.ListLiteral _ es) t = call genListLiteral es t + -- | Generate an expression inside a record literal. -- -- This is awkward: the sort of literal that this produces when there's a @@ -837,7 +846,7 @@ cgenExpression (A.SizeExpr m e) cgenExpression (A.SizeVariable m v) = do A.Array (d:_) _ <- typeOfVariable v case d of - A.Dimension n -> call genExpression n + A.Dimension n -> tell [show n] A.UnknownDimension -> do call genVariable v call genSizeSuffix "0" cgenExpression (A.Conversion m cm t e) = call genConversion m cm t e @@ -1052,7 +1061,7 @@ cgenReplicatorLoop (A.For m index base count) tell [";", counter, ">0;", counter, "--,"] genName index tell ["++"] - +cgenReplicatorLoop _ = cgenMissing "ForEach loops not yet supported in the C backend" --}}} --{{{ abbreviations diff --git a/backends/GenerateCBased.hs b/backends/GenerateCBased.hs index 7a3c272..a2fc67c 100644 --- a/backends/GenerateCBased.hs +++ b/backends/GenerateCBased.hs @@ -132,6 +132,8 @@ data GenOps = GenOps { genInputItem :: A.Variable -> A.InputItem -> CGen (), genIntrinsicFunction :: Meta -> String -> [A.Expression] -> CGen (), genIntrinsicProc :: Meta -> String -> [A.Actual] -> CGen (), + genListLiteral :: [A.Expression] -> A.Type -> CGen (), + genListSize :: A.Variable -> CGen (), genLiteral :: A.LiteralRepr -> A.Type -> CGen (), genLiteralRepr :: A.LiteralRepr -> A.Type -> CGen (), genMissing :: String -> CGen (), diff --git a/backends/GenerateCPPCSP.hs b/backends/GenerateCPPCSP.hs index d9a2161..387a1ee 100644 --- a/backends/GenerateCPPCSP.hs +++ b/backends/GenerateCPPCSP.hs @@ -39,7 +39,7 @@ import System.IO import qualified AST as A import CompState -import GenerateC (cgenOps, cintroduceSpec, cgenType, generate, genComma, genLeftB, genMeta, genName, genRightB, seqComma, withIf) +import GenerateC (cgenOps, cintroduceSpec, cgenReplicatorLoop, cgenType, generate, genComma, genLeftB, genMeta, genName, genRightB, seqComma, withIf) import GenerateCBased import Metadata import Pass @@ -65,10 +65,13 @@ cppgenOps = cgenOps { genGetTime = cppgenGetTime, genIf = cppgenIf, genInputItem = cppgenInputItem, + genListSize = cppgenListSize, + genListLiteral = cppgenListLiteral, genOutputCase = cppgenOutputCase, genOutputItem = cppgenOutputItem, genPar = cppgenPar, genProcCall = cppgenProcCall, + genReplicatorLoop = cppgenReplicatorLoop, genStop = cppgenStop, genTimerRead = cppgenTimerRead, genTimerWait = cppgenTimerWait, @@ -679,6 +682,34 @@ cppgenType t Just s -> tell [s] Nothing -> call genMissingC $ formatCode "genType %" t +cppgenListSize :: A.Variable -> CGen () +cppgenListSize v + = do call genVariable v + tell [".size()"] + +cppgenListLiteral :: [A.Expression] -> A.Type -> CGen () +cppgenListLiteral es t + = do call genType t + tell ["()"] + mapM_ (\e -> tell ["("] >> call genExpression e >> tell [")"]) es + +cppgenReplicatorLoop :: A.Replicator -> CGen () +cppgenReplicatorLoop rep@(A.For {}) = cgenReplicatorLoop rep +cppgenReplicatorLoop (A.ForEach m n (A.ExprVariable _ v)) + = do t <- typeOfVariable v + call genType t + tell ["::iterator "] + genName n + tell ["="] + call genVariable v + tell [".beginSeqEach();"] --TODO what if this is a pareach? + genName n + tell ["!="] + call genVariable v + tell [".limitIterator();"] + genName n + tell ["++"] + -- TODO call endSeqEach -- | Helper function for prefixing an underscore to a name. prefixUnderscore :: A.Name -> A.Name diff --git a/support/tock_support_cppcsp.h b/support/tock_support_cppcsp.h index d2ce5a4..e2f9809 100644 --- a/support/tock_support_cppcsp.h +++ b/support/tock_support_cppcsp.h @@ -224,17 +224,17 @@ public: typedef typename std::list::iterator iterator; - inline iterator beginSeqEach() + inline iterator beginSeqEach() const { return data.begin(); } - inline iterator limitIterator() + inline iterator limitIterator() const { return data.end(); } - inline void endSeqEach() + inline void endSeqEach() const { } @@ -242,6 +242,11 @@ public: { data.erase(_it); } + + inline unsigned size() + { + return data.size(); + } //TODO add beginParEach //TODO add functions for concatenation @@ -256,7 +261,7 @@ public: class derefTockList { private: - tockList* ref; + tockList* const ref; public: inline explicit derefTockList(tockList* _ref) : ref(_ref) @@ -264,7 +269,7 @@ public: } //If you dereference both sides, you get a proper copy-assignment: - void operator=(const derefTockList& _rhs) + inline void operator=(const derefTockList& _rhs) { ref->data = _rhs.ref->data; }