Add more internal allocation errors

This commit is contained in:
Eugen Wissner 2024-12-11 21:44:32 +01:00
parent 7fc90f1d2d
commit fbd08f2707
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
24 changed files with 116 additions and 9 deletions

View File

@ -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 module Language.Elna.Architecture.RiscV
( BaseOpcode(..) ( BaseOpcode(..)
, RelocationType(..) , RelocationType(..)

View File

@ -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 module Language.Elna.Backend.Allocator
( MachineConfiguration(..) ( MachineConfiguration(..)
, Store(..) , Store(..)
@ -28,12 +32,19 @@ data Store r
= RegisterStore r = RegisterStore r
| StackStore Int32 r | StackStore Int32 r
data AllocationError = AllocationError data AllocationError
= OutOfRegistersError
| UnexpectedProcedureInfoError Info
| UndefinedSymbolError Identifier
deriving Eq deriving Eq
instance Show AllocationError instance Show AllocationError
where 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 newtype MachineConfiguration r = MachineConfiguration
{ temporaryRegisters :: [r] { temporaryRegisters :: [r]
@ -74,10 +85,14 @@ allocate machineConfiguration globalTable = HashMap.traverseWithKey function
. runAllocator . runAllocator
. mapM quadruple . mapM quadruple
function :: Identifier -> Vector (Quadruple Variable) -> Either AllocationError (ProcedureQuadruples (Store r)) function :: Identifier -> Vector (Quadruple Variable) -> Either AllocationError (ProcedureQuadruples (Store r))
function identifier quadruples' = function procedureName quadruples' =
let Just (ProcedureInfo localTable _) = SymbolTable.lookup identifier globalTable case SymbolTable.lookup procedureName globalTable of
(result, lastState) = run localTable quadruples' Just (ProcedureInfo localTable _) ->
let (result, lastState) = run localTable quadruples'
in makeResult lastState <$> result in makeResult lastState <$> result
Just anotherInfo -> Left $ UnexpectedProcedureInfoError anotherInfo
Nothing -> Left $ UndefinedSymbolError procedureName
makeResult MachineState{ symbolTable } result = ProcedureQuadruples makeResult MachineState{ symbolTable } result = ProcedureQuadruples
{ quadruples = result { quadruples = result
, stackSize = fromIntegral $ SymbolTable.size symbolTable * 4 , stackSize = fromIntegral $ SymbolTable.size symbolTable * 4
@ -155,13 +170,13 @@ operand (VariableOperand variableOperand) =
storeVariable :: Variable -> Allocator r (Store r) storeVariable :: Variable -> Allocator r (Store r)
storeVariable (TempVariable index) = do storeVariable (TempVariable index) = do
temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters" temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters"
maybe (Allocator $ throwE AllocationError) (pure . RegisterStore) maybe (Allocator $ throwE OutOfRegistersError) (pure . RegisterStore)
$ temporaryRegisters' !? fromIntegral index $ temporaryRegisters' !? fromIntegral index
storeVariable (LocalVariable index) = do storeVariable (LocalVariable index) = do
temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters" 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) $ temporaryRegisters' !? pred (length temporaryRegisters' - fromIntegral index)
storeVariable (ParameterVariable index) = do storeVariable (ParameterVariable index) = do
temporaryRegisters' <- Allocator $ lift $ asks $ getField @"temporaryRegisters" 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 $ temporaryRegisters' !? fromIntegral index

View File

@ -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 module Language.Elna.Backend.Intermediate
( ProcedureQuadruples(..) ( ProcedureQuadruples(..)
, Operand(..) , Operand(..)

View File

@ -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 module Language.Elna.CommandLine
( CommandLine(..) ( CommandLine(..)
, commandLine , commandLine

View File

@ -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 module Language.Elna.Frontend.AST
( Declaration(..) ( Declaration(..)
, Identifier(..) , Identifier(..)

View File

@ -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 module Language.Elna.Frontend.NameAnalysis
( nameAnalysis ( nameAnalysis
, Error(..) , Error(..)

View File

@ -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 module Language.Elna.Frontend.Parser
( Parser ( Parser
, programP , programP

View File

@ -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 module Language.Elna.Frontend.SymbolTable
( SymbolTable ( SymbolTable
, Info(..) , Info(..)

View File

@ -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 module Language.Elna.Frontend.TypeAnalysis
( typeAnalysis ( typeAnalysis
, -- Error(..) , -- Error(..)

View File

@ -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 module Language.Elna.Frontend.Types
( Type(..) ( Type(..)
, addressByteSize , addressByteSize

View File

@ -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 module Language.Elna.Glue
( glue ( glue
) where ) where

View File

@ -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 module Language.Elna.Location
( Identifier(..) ( Identifier(..)
, Location(..) , Location(..)

View File

@ -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 module Language.Elna.Object.Elf
( ByteOrder(..) ( ByteOrder(..)
, Elf32_Addr , Elf32_Addr

View File

@ -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. -- | Object file generation.
module Language.Elna.Object.ElfCoder module Language.Elna.Object.ElfCoder
( ElfEnvironment(..) ( ElfEnvironment(..)

View File

@ -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 module Language.Elna.Object.StringTable
( StringTable ( StringTable
, append , append

View File

@ -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 module Language.Elna.RiscV.CodeGenerator
( Directive(..) ( Directive(..)
, Statement(..) , Statement(..)

View File

@ -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. -- | Writer assembler to an object file.
module Language.Elna.RiscV.ElfWriter module Language.Elna.RiscV.ElfWriter
( riscv32Elf ( riscv32Elf

View File

@ -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 'pathname'
require 'uri' require 'uri'
require 'net/http' require 'net/http'

View File

@ -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') TMP = Pathname.new('./build')

View File

@ -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 'open3'
require 'rake/clean' require 'rake/clean'
require_relative 'shared' require_relative 'shared'

View File

@ -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 module Main
( main ( main
) where ) where

View File

@ -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 module Language.Elna.NameAnalysisSpec
( spec ( spec
) where ) where

View File

@ -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 module Language.Elna.ParserSpec
( spec ( spec
) where ) where

View File

@ -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 #-} {-# OPTIONS_GHC -F -pgmF hspec-discover #-}