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-11-06 22:23:49 +01:00
|
|
|
quadruple (AddQuadruple operand1 operand2 variable)
|
2024-10-06 18:07:57 +02:00
|
|
|
= AddQuadruple (operand operand1) (operand operand2)
|
2024-11-06 22:23:49 +01:00
|
|
|
$ storeVariable variable
|
|
|
|
quadruple (SubtractionQuadruple operand1 operand2 variable)
|
2024-10-06 18:07:57 +02:00
|
|
|
= SubtractionQuadruple (operand operand1) (operand operand2)
|
2024-11-06 22:23:49 +01:00
|
|
|
$ storeVariable variable
|
|
|
|
quadruple (NegationQuadruple operand1 variable)
|
2024-10-06 18:07:57 +02:00
|
|
|
= NegationQuadruple (operand operand1)
|
2024-11-06 22:23:49 +01:00
|
|
|
$ storeVariable variable
|
|
|
|
quadruple (ProductQuadruple operand1 operand2 variable)
|
2024-10-06 18:07:57 +02:00
|
|
|
= ProductQuadruple (operand operand1) (operand operand2)
|
2024-11-06 22:23:49 +01:00
|
|
|
$ storeVariable variable
|
|
|
|
quadruple (DivisionQuadruple operand1 operand2 variable)
|
2024-10-06 18:07:57 +02:00
|
|
|
= DivisionQuadruple (operand operand1) (operand operand2)
|
2024-11-06 22:23:49 +01:00
|
|
|
$ storeVariable variable
|
2024-10-11 16:14:01 +02:00
|
|
|
quadruple (LabelQuadruple label) = LabelQuadruple label
|
|
|
|
quadruple (GoToQuadruple label) = GoToQuadruple label
|
|
|
|
quadruple (EqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
EqualQuadruple (operand operand1) (operand operand2) goToLabel
|
2024-10-13 12:59:47 +02:00
|
|
|
quadruple (NonEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
NonEqualQuadruple (operand operand1) (operand operand2) goToLabel
|
|
|
|
quadruple (LessQuadruple operand1 operand2 goToLabel) =
|
|
|
|
LessQuadruple (operand operand1) (operand operand2) goToLabel
|
|
|
|
quadruple (GreaterQuadruple operand1 operand2 goToLabel) =
|
|
|
|
GreaterQuadruple (operand operand1) (operand operand2) goToLabel
|
|
|
|
quadruple (LessOrEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
LessOrEqualQuadruple (operand operand1) (operand operand2) goToLabel
|
|
|
|
quadruple (GreaterOrEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
GreaterOrEqualQuadruple (operand operand1) (operand operand2) goToLabel
|
2024-11-06 22:23:49 +01:00
|
|
|
quadruple (AssignQuadruple operand1 variable)
|
|
|
|
= AssignQuadruple (operand operand1)
|
|
|
|
$ storeVariable variable
|
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
|
2024-11-06 22:23:49 +01:00
|
|
|
operand (VariableOperand (LocalVariable index))
|
|
|
|
= VariableOperand
|
|
|
|
$ Store
|
|
|
|
$ temporaryRegisters !! fromIntegral index
|
|
|
|
storeVariable (TempVariable index) =
|
|
|
|
Store $ temporaryRegisters !! fromIntegral index
|
|
|
|
storeVariable (LocalVariable index) =
|
|
|
|
Store $ temporaryRegisters !! fromIntegral index
|