diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index 5682b93..36baf64 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -757,7 +757,7 @@ cgenArraySubscript ops checkValid v es = do t <- typeOfVariable v let numDims = case t of A.Array ds _ -> length ds tell ["["] - sequence_ $ intersperse (tell [" + "]) $ genPlainSub v es [0..(numDims - 1)] + sequence_ $ intersperse (tell ["+"]) $ genPlainSub v es [0..(numDims - 1)] tell ["]"] where -- | Generate the individual offsets that need adding together to find the @@ -769,14 +769,14 @@ cgenArraySubscript ops checkValid v es genPlainSub v (e:es) (sub:subs) = gen : genPlainSub v es subs where - gen = sequence_ $ intersperse (tell [" * "]) $ genSub : genChunks + gen = sequence_ $ intersperse (tell ["*"]) $ genSub : genChunks genSub = if checkValid - then do tell ["occam_check_index ("] + then do tell ["occam_check_index("] call genExpression ops e - tell [", "] + tell [","] call genVariable ops v - tell ["_sizes[", show sub, "], "] + tell ["_sizes[", show sub, "],"] genMeta (findMeta e) tell [")"] else call genExpression ops e diff --git a/backends/GenerateCPPCSP.hs b/backends/GenerateCPPCSP.hs index dddd2cd..ebe52ad 100644 --- a/backends/GenerateCPPCSP.hs +++ b/backends/GenerateCPPCSP.hs @@ -1137,12 +1137,15 @@ cppgenSlice ops _ v ty start count ds call genExpression ops count tell [")"] --- | Changed from GenerateC to use Blitz++ subscripting (round brackets with commas) rather than traditional C indexing +-- | Changed from GenerateC to use multiple subscripting (e.g. [1][2][3]) rather than the combined indexing of the C method (e.g. [1*x*y+2*y+3]) cppgenArraySubscript :: GenOps -> Bool -> A.Variable -> [A.Expression] -> CGen () cppgenArraySubscript ops checkValid v es = do t <- typeOfVariable v let numDims = case t of A.Array ds _ -> length ds sequence_ $ genPlainSub v es [0..(numDims - 1)] + --To index an actual element of an array we must use the .access() function + --Only needed when we have applied enough subscripts to get out an element: + when (numDims == (length es)) (tell [".access()"]) where -- | Generate the individual offsets that need adding together to find the -- right place in the array. @@ -1154,17 +1157,17 @@ cppgenArraySubscript ops checkValid v es genPlainSub :: A.Variable -> [A.Expression] -> [Int] -> [CGen ()] genPlainSub _ _ [] = [] - genPlainSub v [] (sub:subs) = (tell [" "]) : (genPlainSub v [] subs) + genPlainSub v [] (sub:subs) = (return ()) : (genPlainSub v [] subs) genPlainSub v (e:es) (sub:subs) = (tell ["["] >> genSub >> tell ["]"]) : genPlainSub v es subs where genSub = if checkValid - then do tell ["occam_check_index ("] + then do tell ["occam_check_index("] call genExpression ops e - tell [", "] + tell [","] call genVariable ops v - tell [".extent(", show sub, "), "] + tell [".extent(", show sub, "),"] genMeta (findMeta e) tell [")"] else call genExpression ops e @@ -1315,10 +1318,6 @@ cppgenVariable' ops checkValid v = do let (es, v) = collectSubs sv call genVariable ops v call genArraySubscript ops checkValid v es - t <- typeOfVariable v - --To index an actual element of an array we must use the .access() function - --Only needed when we have applied enough subscripts to get out an element: - case t of A.Array dims _ -> when ((length dims) == (length es)) (tell [" .access() "]) inner (A.SubscriptedVariable _ (A.SubscriptField m n) v) = do call genVariable ops v tell ["->"] diff --git a/backends/GenerateCTest.hs b/backends/GenerateCTest.hs index f7b0118..30592a9 100644 --- a/backends/GenerateCTest.hs +++ b/backends/GenerateCTest.hs @@ -216,6 +216,32 @@ testActuals = TestList ] where over = (\ops -> ops {genVariable = override1 at, genExpression = override1 dollar}) + +testArraySubscript :: Test +testArraySubscript = TestList + [ + testBothS "genArraySubscript 0" "[5*foo_sizes[1]*foo_sizes[2]]" "[5]" + (tcall3 genArraySubscript False (A.Variable emptyMeta foo) [intLiteral 5]) stateTrans + ,testBothS "genArraySubscript 1" "[5*foo_sizes[1]*foo_sizes[2]+6*foo_sizes[2]]" "[5][6]" + (tcall3 genArraySubscript False (A.Variable emptyMeta foo) [intLiteral 5, intLiteral 6]) stateTrans + ,testBothS "genArraySubscript 2" "[5*foo_sizes[1]*foo_sizes[2]+6*foo_sizes[2]+7]" "[5][6][7].access()" + (tcall3 genArraySubscript False (A.Variable emptyMeta foo) [intLiteral 5, intLiteral 6, intLiteral 7]) stateTrans + + ,testBothS "genArraySubscript 3" ("[occam_check_index(5,foo_sizes[0]," ++ m ++ ")*foo_sizes[1]*foo_sizes[2]]") ("[occam_check_index(5,foo.extent(0)," ++ m ++ ")]") + (tcall3 genArraySubscript True (A.Variable emptyMeta foo) [intLiteral 5]) stateTrans + ,testBothS "genArraySubscript 4" + ("[occam_check_index(5,foo_sizes[0]," ++ m ++ ")*foo_sizes[1]*foo_sizes[2]+occam_check_index(6,foo_sizes[1]," ++ m ++ ")*foo_sizes[2]]") + ("[occam_check_index(5,foo.extent(0)," ++ m ++ ")][occam_check_index(6,foo.extent(1)," ++ m ++ ")]") + (tcall3 genArraySubscript True (A.Variable emptyMeta foo) [intLiteral 5, intLiteral 6]) stateTrans + ,testBothS "genArraySubscript 5" + ("[occam_check_index(5,foo_sizes[0]," ++ m ++ ")*foo_sizes[1]*foo_sizes[2]+occam_check_index(6,foo_sizes[1]," ++ m ++ ")*foo_sizes[2]+occam_check_index(7,foo_sizes[2]," ++ m ++ ")]") + ("[occam_check_index(5,foo.extent(0)," ++ m ++ ")][occam_check_index(6,foo.extent(1)," ++ m ++ ")][occam_check_index(7,foo.extent(2)," ++ m ++ ")].access()") + (tcall3 genArraySubscript True (A.Variable emptyMeta foo) [intLiteral 5, intLiteral 6, intLiteral 7]) stateTrans + + ] + where + stateTrans = defineName (simpleName "foo") $ simpleDefDecl "foo" (A.Array [A.Dimension 7,A.Dimension 8,A.Dimension 8] A.Int) + m = "\"" ++ show emptyMeta ++ "\"" ---Returns the list of tests: tests :: Test @@ -223,6 +249,7 @@ tests = TestList [ testActuals ,testArraySizes + ,testArraySubscript ,testGenType ,testStop ]