elna/lib/Language/Elna/Backend/Allocator.hs

59 lines
2.3 KiB
Haskell
Raw Normal View History

2024-10-02 22:56:15 +02:00
module Language.Elna.Backend.Allocator
2024-10-01 00:02:19 +02:00
( MachineConfiguration(..)
, Store(..)
, allocate
2024-09-27 00:22:44 +02:00
) where
2024-10-01 00:02:19 +02:00
import Data.HashMap.Strict (HashMap)
import Data.Vector (Vector)
2024-10-02 22:56:15 +02:00
import Language.Elna.Backend.Intermediate (Operand(..), Quadruple(..), Variable(..))
2024-10-01 00:02:19 +02:00
import Language.Elna.Location (Identifier(..))
newtype Store r = Store r
newtype MachineConfiguration r = MachineConfiguration
2024-10-06 18:07:57 +02:00
{ temporaryRegisters :: [r]
2024-10-01 00:02:19 +02:00
}
allocate
:: forall r
. MachineConfiguration r
-> HashMap Identifier (Vector (Quadruple Variable))
-> HashMap Identifier (Vector (Quadruple (Store r)))
allocate MachineConfiguration{..} = fmap function
where
function :: Vector (Quadruple Variable) -> Vector (Quadruple (Store r))
function = fmap quadruple
quadruple :: Quadruple Variable -> Quadruple (Store r)
quadruple StartQuadruple = StartQuadruple
quadruple StopQuadruple = StopQuadruple
quadruple (ParameterQuadruple operand1) =
ParameterQuadruple (operand operand1)
quadruple (CallQuadruple name count) = CallQuadruple name count
2024-10-06 18:07:57 +02:00
quadruple (AddQuadruple operand1 operand2 (TempVariable index))
= AddQuadruple (operand operand1) (operand operand2)
$ Store
$ temporaryRegisters !! fromIntegral index
quadruple (SubtractionQuadruple operand1 operand2 (TempVariable index))
= SubtractionQuadruple (operand operand1) (operand operand2)
$ Store
$ temporaryRegisters !! fromIntegral index
quadruple (NegationQuadruple operand1 (TempVariable index))
= NegationQuadruple (operand operand1)
$ Store
$ temporaryRegisters !! fromIntegral index
quadruple (ProductQuadruple operand1 operand2 (TempVariable index))
= ProductQuadruple (operand operand1) (operand operand2)
$ Store
$ temporaryRegisters !! fromIntegral index
quadruple (DivisionQuadruple operand1 operand2 (TempVariable index))
= DivisionQuadruple (operand operand1) (operand operand2)
$ Store
$ temporaryRegisters !! fromIntegral index
2024-10-01 00:02:19 +02:00
operand :: Operand Variable -> Operand (Store r)
operand (IntOperand x) = IntOperand x
2024-10-06 18:07:57 +02:00
operand (VariableOperand (TempVariable index))
= VariableOperand
$ Store
$ temporaryRegisters !! fromIntegral index