Add more internal allocation errors
This commit is contained in:
		@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Architecture.RiscV
 | 
			
		||||
    ( BaseOpcode(..)
 | 
			
		||||
    , RelocationType(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Backend.Allocator
 | 
			
		||||
    ( MachineConfiguration(..)
 | 
			
		||||
    , Store(..)
 | 
			
		||||
@@ -28,12 +32,19 @@ data Store r
 | 
			
		||||
    = RegisterStore r
 | 
			
		||||
    | StackStore Int32 r
 | 
			
		||||
 | 
			
		||||
data AllocationError = AllocationError
 | 
			
		||||
data AllocationError
 | 
			
		||||
    = OutOfRegistersError
 | 
			
		||||
    | UnexpectedProcedureInfoError Info
 | 
			
		||||
    | UndefinedSymbolError Identifier
 | 
			
		||||
    deriving Eq
 | 
			
		||||
 | 
			
		||||
instance Show AllocationError
 | 
			
		||||
  where
 | 
			
		||||
    show = const "Ran out of registers during register allocation"
 | 
			
		||||
    show OutOfRegistersError = "Ran out of registers during register allocation"
 | 
			
		||||
    show (UnexpectedProcedureInfoError info) =
 | 
			
		||||
        "Expected to encounter a procedure, got: " <> show info
 | 
			
		||||
    show (UndefinedSymbolError identifier) =
 | 
			
		||||
        concat ["Symbol \"", show identifier, "\" is not defined"]
 | 
			
		||||
 | 
			
		||||
newtype MachineConfiguration r = MachineConfiguration
 | 
			
		||||
    { temporaryRegisters :: [r]
 | 
			
		||||
@@ -74,10 +85,14 @@ allocate machineConfiguration globalTable = HashMap.traverseWithKey function
 | 
			
		||||
        . runAllocator
 | 
			
		||||
        . mapM quadruple
 | 
			
		||||
    function :: Identifier -> Vector (Quadruple Variable) -> Either AllocationError (ProcedureQuadruples (Store r))
 | 
			
		||||
    function identifier quadruples' =
 | 
			
		||||
        let Just (ProcedureInfo localTable _) = SymbolTable.lookup identifier globalTable
 | 
			
		||||
            (result, lastState) = run localTable quadruples'
 | 
			
		||||
         in makeResult lastState <$> result
 | 
			
		||||
    function procedureName quadruples' =
 | 
			
		||||
        case SymbolTable.lookup procedureName globalTable of
 | 
			
		||||
            Just (ProcedureInfo localTable _) ->
 | 
			
		||||
                let (result, lastState) = run localTable quadruples'
 | 
			
		||||
                 in makeResult lastState <$> result
 | 
			
		||||
            Just anotherInfo -> Left $ UnexpectedProcedureInfoError anotherInfo
 | 
			
		||||
            Nothing -> Left $ UndefinedSymbolError procedureName
 | 
			
		||||
 | 
			
		||||
    makeResult MachineState{ symbolTable } result = ProcedureQuadruples
 | 
			
		||||
        { quadruples = result
 | 
			
		||||
        , stackSize = fromIntegral $ SymbolTable.size symbolTable * 4
 | 
			
		||||
@@ -155,13 +170,13 @@ operand (VariableOperand variableOperand) =
 | 
			
		||||
storeVariable :: Variable -> Allocator r (Store r)
 | 
			
		||||
storeVariable (TempVariable index) = do
 | 
			
		||||
    temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters"
 | 
			
		||||
    maybe (Allocator $ throwE AllocationError) (pure . RegisterStore)
 | 
			
		||||
    maybe (Allocator $ throwE OutOfRegistersError) (pure . RegisterStore)
 | 
			
		||||
        $ temporaryRegisters' !? fromIntegral index
 | 
			
		||||
storeVariable (LocalVariable index) = do
 | 
			
		||||
    temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters"
 | 
			
		||||
    maybe (Allocator $ throwE AllocationError) (pure . StackStore (fromIntegral (succ index) * (-4)))
 | 
			
		||||
    maybe (Allocator $ throwE OutOfRegistersError) (pure . StackStore (fromIntegral (succ index) * (-4)))
 | 
			
		||||
        $ temporaryRegisters' !? pred (length temporaryRegisters' - fromIntegral index)
 | 
			
		||||
storeVariable (ParameterVariable index) = do
 | 
			
		||||
    temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters"
 | 
			
		||||
    maybe (Allocator $ throwE AllocationError) (pure . StackStore (fromIntegral index * 4))
 | 
			
		||||
    maybe (Allocator $ throwE OutOfRegistersError) (pure . StackStore (fromIntegral index * 4))
 | 
			
		||||
        $ temporaryRegisters' !? fromIntegral index
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Backend.Intermediate
 | 
			
		||||
    ( ProcedureQuadruples(..)
 | 
			
		||||
    , Operand(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.CommandLine
 | 
			
		||||
    ( CommandLine(..)
 | 
			
		||||
    , commandLine
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.AST
 | 
			
		||||
    ( Declaration(..)
 | 
			
		||||
    , Identifier(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.NameAnalysis
 | 
			
		||||
    ( nameAnalysis
 | 
			
		||||
    , Error(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.Parser
 | 
			
		||||
    ( Parser
 | 
			
		||||
    , programP
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.SymbolTable
 | 
			
		||||
    ( SymbolTable
 | 
			
		||||
    , Info(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.TypeAnalysis
 | 
			
		||||
    ( typeAnalysis
 | 
			
		||||
    , -- Error(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Frontend.Types
 | 
			
		||||
    ( Type(..)
 | 
			
		||||
    , addressByteSize
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Glue
 | 
			
		||||
    ( glue
 | 
			
		||||
    ) where
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Location
 | 
			
		||||
    ( Identifier(..)
 | 
			
		||||
    , Location(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Object.Elf
 | 
			
		||||
    ( ByteOrder(..)
 | 
			
		||||
    , Elf32_Addr
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
-- | Object file generation.
 | 
			
		||||
module Language.Elna.Object.ElfCoder
 | 
			
		||||
    ( ElfEnvironment(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.Object.StringTable
 | 
			
		||||
    ( StringTable
 | 
			
		||||
    , append
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.RiscV.CodeGenerator
 | 
			
		||||
    ( Directive(..)
 | 
			
		||||
    , Statement(..)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
-- | Writer assembler to an object file.
 | 
			
		||||
module Language.Elna.RiscV.ElfWriter
 | 
			
		||||
    ( riscv32Elf
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
# This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
# obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
require 'pathname'
 | 
			
		||||
require 'uri'
 | 
			
		||||
require 'net/http'
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,5 @@
 | 
			
		||||
# This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
# obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
TMP = Pathname.new('./build')
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
# This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
# obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
require 'open3'
 | 
			
		||||
require 'rake/clean'
 | 
			
		||||
require_relative 'shared'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Main
 | 
			
		||||
    ( main
 | 
			
		||||
    ) where
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.NameAnalysisSpec
 | 
			
		||||
    ( spec
 | 
			
		||||
    ) where
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
module Language.Elna.ParserSpec
 | 
			
		||||
    ( spec
 | 
			
		||||
    ) where
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,5 @@
 | 
			
		||||
{- This Source Code Form is subject to the terms of the Mozilla Public License,
 | 
			
		||||
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
 | 
			
		||||
   obtain one at https://mozilla.org/MPL/2.0/. -}
 | 
			
		||||
 | 
			
		||||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user