diff --git a/rts/memory.c b/rts/memory.c index e2b2628..c946605 100644 --- a/rts/memory.c +++ b/rts/memory.c @@ -77,6 +77,12 @@ void sixten_increase_reference_count(uintptr_t heap_object) { ++header->reference_count; } +void sixten_increase_reference_counts(uintptr_t* data, uint32_t count) { + for (uint32_t i = 0; i < count; ++i) { + sixten_increase_reference_count(data[i]); + } +} + void sixten_decrease_reference_count(uintptr_t heap_object) { uint8_t* pointer = heap_object_pointer(heap_object); if (pointer == 0) { diff --git a/rts/memory.h b/rts/memory.h index 8bd7914..b5a4ec6 100644 --- a/rts/memory.h +++ b/rts/memory.h @@ -17,5 +17,6 @@ void sixten_copy( uint32_t non_pointer_bytes ); void sixten_increase_reference_count(uintptr_t heap_object); +void sixten_increase_reference_counts(uintptr_t* data, uint32_t count); void sixten_decrease_reference_count(uintptr_t heap_object); void sixten_decrease_reference_counts(uintptr_t* data, uint32_t count); diff --git a/src/Lower.hs b/src/Lower.hs index eacffe9..119f2db 100644 --- a/src/Lower.hs +++ b/src/Lower.hs @@ -187,7 +187,7 @@ definition name = \case seq_ $ Store (Global Representation.int initedName) (Literal $ Literal.Integer 1) Representation.int _ <- storeTerm CC.empty Index.Seq.Empty (Global repr (Name.Lowered name Name.Original)) term pure $ Undefined Representation.Empty - letValue Representation.Empty "case_result" $ Case inited [LiteralBranch (Literal.Integer 0) initBranch] $ Just $ Operand $ Undefined Representation.Empty + _ <- letValue Representation.Empty "case_result" $ Case inited [LiteralBranch (Literal.Integer 0) initBranch] $ Just $ Operand $ Undefined Representation.Empty pure $ Undefined Representation.Empty let init = readback Index.Map.Empty initValue pure @@ -620,7 +620,7 @@ moduleInit moduleName definitions = do forM_ constantsToInitialize \defName -> letValue Representation.Empty "init-result" $ Call defName [] pure $ Undefined Representation.Empty - letValue Representation.Empty "case-result" $ Case inited [LiteralBranch (Literal.Integer 0) initBranch] $ Just $ Operand $ Undefined Representation.Empty + _ <- letValue Representation.Empty "case-result" $ Case inited [LiteralBranch (Literal.Integer 0) initBranch] $ Just $ Operand $ Undefined Representation.Empty pure $ Undefined Representation.Empty let init = readback Index.Map.Empty initValue pure diff --git a/src/ReferenceCounting.hs b/src/ReferenceCounting.hs index 8afff13..cd118f5 100644 --- a/src/ReferenceCounting.hs +++ b/src/ReferenceCounting.hs @@ -251,8 +251,7 @@ referenceCount passBy value = case value of pure (Let passValBy name var dead val''' body'', Just $ Owned (PassBy.Value repr) 1) _ -> pure (Let passValBy name var dead val'' body', bodyProvenance) Seq lhs rhs -> do - (lhs', lhsProvenance) <- referenceCount (PassBy.Value mempty) lhs - when (isJust lhsProvenance) $ panic $ "Seq with provenance " <> show lhs' + (increaseBefores, decreaseAfters) <- referenceCountSeqOperation lhs (rhs', rhsProvenance) <- referenceCount passBy rhs pure (Seq lhs' rhs', rhsProvenance) @@ -318,23 +317,23 @@ referenceCountLetOperation passBy operation = case operation of pure (operation, Child <$> maybeParent, maybeToList decreaseSrc) referenceCountSeqOperation - :: PassBy - -> SeqOperation - -> ReferenceCount ([(Var, Representation)], [(Var, Representation)]) -referenceCountSeqOperation passBy operation = case operation of - Copy dst src _ -> do + :: SeqOperation + -> ReferenceCount ([(Operand, Either Representation Operand)], [(Var, Representation)]) +referenceCountSeqOperation operation = case operation of + Copy dst src repr -> do decreaseDst <- referenceCountOperand dst decreaseSrc <- referenceCountOperand src - -- TODO - pure ([], catMaybes [decreaseDst decreaseSrc]) + pure case (src, decreaseSrc) of + (Var Killed srcVar, Just (srcVar', _)) + | srcVar == srcVar' -> ([], maybeToList decreaseDst) + _ -> ([(src, Right repr)], catMaybes [decreaseDst, decreaseSrc]) Store dst src repr -> do decreaseDst <- referenceCountOperand dst decreaseSrc <- referenceCountOperand src pure case (src, decreaseSrc) of (Var Killed srcVar, Just (srcVar', _)) - | srcVar == srcVar' -> - ([], maybeToList decreaseDst) - _ -> ([(src, repr)], catMaybes [decreaseDst, decreaseSrc]) + | srcVar == srcVar' -> ([], maybeToList decreaseDst) + _ -> ([(src, Left repr)], catMaybes [decreaseDst, decreaseSrc]) IncreaseReferenceCount {} -> panic "RC operations before reference counting" DecreaseReferenceCount {} -> panic "RC operations before reference counting"