Negate integral expressions
This commit is contained in:
41
lib/Language/Elna/Backend/Allocator.hs
Normal file
41
lib/Language/Elna/Backend/Allocator.hs
Normal file
@ -0,0 +1,41 @@
|
||||
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)
|
||||
operand :: Operand Variable -> Operand (Store r)
|
||||
operand (IntOperand x) = IntOperand x
|
||||
operand (VariableOperand _) = VariableOperand (Store temporaryRegister)
|
46
lib/Language/Elna/Backend/Intermediate.hs
Normal file
46
lib/Language/Elna/Backend/Intermediate.hs
Normal file
@ -0,0 +1,46 @@
|
||||
module Language.Elna.Backend.Intermediate
|
||||
( Operand(..)
|
||||
, Quadruple(..)
|
||||
{- , Label(..) -}
|
||||
, Variable(..)
|
||||
) where
|
||||
|
||||
import Data.Int (Int32)
|
||||
import Data.Word (Word32)
|
||||
import Data.Text (Text)
|
||||
|
||||
newtype Variable = TempVariable Word32 -- | Variable Text
|
||||
deriving Eq
|
||||
|
||||
instance Show Variable
|
||||
where
|
||||
-- show (Variable variable) = '$' : Text.unpack variable
|
||||
show (TempVariable variable) = '$' : show variable
|
||||
|
||||
data Operand v
|
||||
= IntOperand Int32
|
||||
| VariableOperand v
|
||||
deriving (Eq, Show)
|
||||
|
||||
data Quadruple v
|
||||
= StartQuadruple
|
||||
| StopQuadruple
|
||||
| ParameterQuadruple (Operand v)
|
||||
| CallQuadruple Text Word32
|
||||
| AddQuadruple (Operand v) (Operand v) v
|
||||
| SubtractionQuadruple (Operand v) (Operand v) v
|
||||
| NegationQuadruple (Operand v) v
|
||||
{-| GoToQuadruple Label
|
||||
| AssignQuadruple Operand Variable
|
||||
| ArrayQuadruple Variable Operand Variable
|
||||
| ArrayAssignQuadruple Operand Operand Variable
|
||||
| ProductQuadruple Operand Operand Variable
|
||||
| DivisionQuadruple Operand Operand Variable
|
||||
| EqualQuadruple Operand Operand Label
|
||||
| NonEqualQuadruple Operand Operand Label
|
||||
| LessQuadruple Operand Operand Label
|
||||
| GreaterQuadruple Operand Operand Label
|
||||
| LessOrEqualQuadruple Operand Operand Label
|
||||
| GreaterOrEqualQuadruple Operand Operand Label
|
||||
| LabelQuadruple Label -}
|
||||
deriving (Eq, Show)
|
Reference in New Issue
Block a user