summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Backend/Allocator.hs
blob: 1ffc85df90b0bb03017bf607781fff6dfa2c7047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Language.Elna.Backend.Allocator
    ( MachineConfiguration(..)
    , Store(..)
    , allocate
    ) where

import Data.HashMap.Strict (HashMap)
import Data.Vector (Vector)
import Language.Elna.Backend.Intermediate (Operand(..), Quadruple(..), Variable(..))
import Language.Elna.Location (Identifier(..))

newtype Store r = Store r

newtype MachineConfiguration r = MachineConfiguration
    { temporaryRegister :: r
    }

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
    quadruple (AddQuadruple operand1 operand2 _) =
        AddQuadruple (operand operand1) (operand operand2) (Store temporaryRegister)
    quadruple (SubtractionQuadruple operand1 operand2 _) =
        SubtractionQuadruple (operand operand1) (operand operand2) (Store temporaryRegister)
    quadruple (NegationQuadruple operand1 _) =
        NegationQuadruple (operand operand1) (Store temporaryRegister)
    quadruple (ProductQuadruple operand1 operand2 _) =
        ProductQuadruple (operand operand1) (operand operand2) (Store temporaryRegister)
    operand :: Operand Variable -> Operand (Store r)
    operand (IntOperand x) = IntOperand x
    operand (VariableOperand _) = VariableOperand (Store temporaryRegister)