summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Allocator.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Language/Elna/Allocator.hs')
-rw-r--r--lib/Language/Elna/Allocator.hs38
1 files changed, 37 insertions, 1 deletions
diff --git a/lib/Language/Elna/Allocator.hs b/lib/Language/Elna/Allocator.hs
index a5ca574..3b32de4 100644
--- a/lib/Language/Elna/Allocator.hs
+++ b/lib/Language/Elna/Allocator.hs
@@ -1,3 +1,39 @@
module Language.Elna.Allocator
- (
+ ( MachineConfiguration(..)
+ , Store(..)
+ , allocate
) where
+
+import Data.HashMap.Strict (HashMap)
+import Data.Vector (Vector)
+import Language.Elna.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)
+ operand :: Operand Variable -> Operand (Store r)
+ operand (IntOperand x) = IntOperand x
+ operand (VariableOperand _) = VariableOperand (Store temporaryRegister)