summaryrefslogtreecommitdiff
path: root/lib/Language/Elna/Architecture/RiscV.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Language/Elna/Architecture/RiscV.hs')
-rw-r--r--lib/Language/Elna/Architecture/RiscV.hs31
1 files changed, 26 insertions, 5 deletions
diff --git a/lib/Language/Elna/Architecture/RiscV.hs b/lib/Language/Elna/Architecture/RiscV.hs
index f4c3887..9964911 100644
--- a/lib/Language/Elna/Architecture/RiscV.hs
+++ b/lib/Language/Elna/Architecture/RiscV.hs
@@ -1,5 +1,6 @@
module Language.Elna.Architecture.RiscV
( BaseOpcode(..)
+ , RelocationType(..)
, Funct3(..)
, Funct7(..)
, Funct12(..)
@@ -15,6 +16,7 @@ module Language.Elna.Architecture.RiscV
import qualified Data.ByteString.Builder as ByteString.Builder
import Data.Bits (Bits(..))
+import Data.Text (Text)
import Data.Word (Word8, Word32)
data XRegister
@@ -137,8 +139,18 @@ data Type
| U XRegister Word32
| J XRegister Word32
| Type XRegister Funct3 XRegister Funct12 -- Privileged.
+ deriving Eq
-data Instruction = Instruction BaseOpcode Type
+data RelocationType
+ = Lower12I XRegister Funct3 XRegister Text
+ | Lower12S Text Funct3 XRegister XRegister
+ | Higher20 XRegister Text -- Type U.
+ deriving Eq
+
+data Instruction
+ = Instruction BaseOpcode Type
+ | RelocatableInstruction BaseOpcode RelocationType
+ deriving Eq
xRegister :: XRegister -> Word8
xRegister Zero = 0
@@ -285,8 +297,17 @@ type' (Type rd funct3' rs1 funct12')
.|. (fromIntegral (xRegister rs1) `shiftL` 15)
.|. (fromIntegral (funct12 funct12') `shiftL` 20);
+relocationType :: RelocationType -> Word32
+relocationType (Lower12I rd funct3' rs1 _) = type' $ I rd funct3' rs1 0
+relocationType (Lower12S _ funct3' rs1 rs2) = type' $ S 0 funct3' rs1 rs2
+relocationType (Higher20 rd _) = type' $ U rd 0
+
instruction :: Instruction -> ByteString.Builder.Builder
-instruction (Instruction base instructionType)
- = ByteString.Builder.word32LE
- $ fromIntegral (baseOpcode base)
- .|. type' instructionType
+instruction = \case
+ (Instruction base instructionType) -> go base $ type' instructionType
+ (RelocatableInstruction base instructionType) -> go base $ relocationType instructionType
+ where
+ go base instructionType
+ = ByteString.Builder.word32LE
+ $ fromIntegral (baseOpcode base)
+ .|. instructionType