1-- SPDX-FileCopyrightText: 2023-2024 University of Bremen2-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>3--4-- SPDX-License-Identifier: MIT AND GPL-3.0-only56-- | This module provides an implementation of a simple byte-addressable memory7-- based on "Data.Array".8module Language.QBE.Simulator.Memory9 ( -- * Type Aliases10 Address,11 Size,12 showAddr,1314 -- * Value Representation15 Storable (toBytes, fromBytes),1617 -- * Memory Representation18 Memory,19 mkMemory,20 memSize,21 loadBytes,22 storeBytes,2324 -- * Memory Address25 toMemAddr,26 addrOverlap,27 alignAddr,28 )29where3031import Data.Array.IO32 ( MArray,33 getBounds,34 newArray_,35 readArray,36 writeArray,37 )38import Data.Bits (complement, (.&.))39import Data.Word (Word64)40import Language.QBE.Types qualified as QBE41import Numeric (showHex)4243-- | Type used to represent an address in memory.44type Address = Word644546-- | Type used to represent the memory's size.47type Size = Word644849-- | Represent an address as a hexadecimal string.50showAddr :: Address -> String51showAddr addr = "0x" ++ showHex addr ""5253------------------------------------------------------------------------5455-- | Type class for types that can be stored in memory. That is, types whose56-- values can be converted to the given byte representation and vice versa.57class Storable valTy byteTy where58 -- | Convert a value type to a list of byte types.59 toBytes :: valTy -> [byteTy]6061 -- | Convert a list of bytes to a value type of 'QBE.LoadType'. Returns62 -- 'Nothing' if the length of the list is incompatible with the given63 -- 'QBE.LoadType'.64 fromBytes :: QBE.LoadType -> [byteTy] -> Maybe valTy6566-- | Memory parameterized over the Array type (e.g. 'Data.Array.IO.IOUArray')67-- and a byte polymorphic representation (e.g. 'Data.Word.Word8').68data Memory a v = Memory69 { memStart :: Address,70 memBytes :: a Address v71 }7273-- | Create a new t'Memory' which starts at the given base address and74-- has a maximum capacity (i.e., can store up to the given amount of bytes).75-- The memory is not initialized, reading an uninitialized values results76-- in an error.77mkMemory :: (MArray t a IO) => Address -> Size -> IO (Memory t a)78mkMemory startAddr size = do79 ary <- newArray_ (0, size - 1)80 return $ Memory startAddr ary8182-- | Translate global address to a memory-local address. That is, performs83-- address translation relative to the base address of the t'Memory'.84toMemAddr :: Memory t a -> Address -> Address85toMemAddr mem addr = addr - memStart mem8687-- | Returns true if the given addresses, passed in the first and second88-- argument, overlap in the given range (i.e., the given amount of bytes).89addrOverlap :: Address -> Address -> Size -> Bool90addrOverlap addr1 addr2 range =91 addr1 < endAddr addr2 && endAddr addr1 > addr292 where93 endAddr :: Address -> Address94 endAddr a = a + range9596-- | Align an address upwards for the given alignment.97alignAddr :: Address -> Size -> Address98alignAddr addr align = (addr + (align - 1)) .&. complement (align - 1)99100-- | Returns the size of the memory in bytes.101memSize :: (MArray t a IO) => Memory t a -> IO Size102memSize = fmap ((+ 1) . snd) . getBounds . memBytes103104-- | Write the list of bytes to memory at the given address.105storeBytes :: (MArray t a IO) => Memory t a -> Address -> [a] -> IO ()106storeBytes mem addr bytes =107 mapM_ (\(off, val) -> storeByte mem (addr + off) val) $108 zip [0 ..] bytes109 where110 storeByte :: (MArray t a IO) => Memory t a -> Address -> a -> IO ()111 storeByte m a = writeArray (memBytes m) $ toMemAddr mem a112{-# INLINEABLE storeBytes #-}113114-- | Load the given amount of bytes at the given address.115loadBytes :: (MArray t a IO) => Memory t a -> Address -> Size -> IO [a]116loadBytes mem addr byteSize =117 mapM (\off -> loadByte mem (addr + off)) [0 .. byteSize - 1]118 where119 loadByte :: (MArray t a IO) => Memory t a -> Address -> IO a120 loadByte m = readArray (memBytes m) . toMemAddr m121{-# INLINEABLE loadBytes #-}