Altered the backends to support lists and foreach loops over lists (at least in the C++ part)

This commit is contained in:
Neil Brown 2008-03-19 17:20:52 +00:00
parent 43b77ff1a0
commit 839d92546b
4 changed files with 57 additions and 10 deletions

View File

@ -17,7 +17,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
-}
-- | 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,6 +541,7 @@ 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.
--
@ -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

View File

@ -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 (),

View File

@ -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

View File

@ -224,17 +224,17 @@ public:
typedef typename std::list<T>::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
{
}
@ -243,6 +243,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<T>* ref;
tockList<T>* const ref;
public:
inline explicit derefTockList(tockList<T>* _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;
}