|
|
|
@ -6,7 +6,6 @@ module Language.Elna.RiscV.CodeGenerator
|
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
import Control.Monad.Trans.State (State, get, evalState, modify')
|
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
|
import Data.Int (Int32)
|
|
|
|
|
import Data.Word (Word32)
|
|
|
|
@ -14,7 +13,12 @@ import Data.Vector (Vector)
|
|
|
|
|
import qualified Data.Vector as Vector
|
|
|
|
|
import qualified Language.Elna.Architecture.RiscV as RiscV
|
|
|
|
|
import Language.Elna.Backend.Allocator (MachineConfiguration(..), Store(..))
|
|
|
|
|
import Language.Elna.Backend.Intermediate (Label(..), Operand(..), Quadruple(..))
|
|
|
|
|
import Language.Elna.Backend.Intermediate
|
|
|
|
|
( Label(..)
|
|
|
|
|
, Operand(..)
|
|
|
|
|
, ProcedureQuadruples(..)
|
|
|
|
|
, Quadruple(..)
|
|
|
|
|
)
|
|
|
|
|
import Language.Elna.Location (Identifier(..))
|
|
|
|
|
import Data.Bits (Bits(..))
|
|
|
|
|
import Data.Foldable (Foldable(..), foldlM)
|
|
|
|
@ -80,11 +84,11 @@ createLabel = do
|
|
|
|
|
$ Text.Builder.toLazyText
|
|
|
|
|
$ Text.Builder.decimal currentCounter
|
|
|
|
|
|
|
|
|
|
generateRiscV :: HashMap Identifier (Vector RiscVQuadruple) -> Vector Statement
|
|
|
|
|
generateRiscV = flip evalState 0
|
|
|
|
|
. runRiscVGenerator
|
|
|
|
|
. foldlM go Vector.empty
|
|
|
|
|
. HashMap.toList
|
|
|
|
|
generateRiscV :: ProcedureQuadruples RiscVStore -> Vector Statement
|
|
|
|
|
generateRiscV (ProcedureQuadruples quadruples) = flip evalState 0
|
|
|
|
|
$ runRiscVGenerator
|
|
|
|
|
$ foldlM go Vector.empty
|
|
|
|
|
$ HashMap.toList quadruples
|
|
|
|
|
where
|
|
|
|
|
go accumulator (Identifier key, value) =
|
|
|
|
|
let code = Vector.cons (JumpLabel key [GlobalDirective, FunctionDirective])
|
|
|
|
@ -114,32 +118,10 @@ quadruple StopQuadruple = pure $ Vector.fromList
|
|
|
|
|
, Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.SP RiscV.ADDI RiscV.SP 4)
|
|
|
|
|
, Instruction (RiscV.BaseInstruction RiscV.Jalr $ RiscV.I RiscV.RA RiscV.JALR RiscV.Zero 0)
|
|
|
|
|
]
|
|
|
|
|
quadruple (AddQuadruple operand1 operand2 (Store register))
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
pure $ lui (immediateOperand1 + immediateOperand2) register
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
in pure $ Vector.singleton $ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register RiscV.ADD operandRegister1 operandRegister2 (RiscV.Funct7 0b0000000)
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
addImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
addImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
addImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand register
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register RiscV.ADD register operandRegister
|
|
|
|
|
$ RiscV.Funct7 0b0000000
|
|
|
|
|
quadruple (AddQuadruple operand1 operand2 store) =
|
|
|
|
|
commutativeBinary (+) RiscV.ADD (RiscV.Funct7 0b0000000) (operand1, operand2) store
|
|
|
|
|
quadruple (ProductQuadruple operand1 operand2 store) =
|
|
|
|
|
commutativeBinary (*) RiscV.MUL (RiscV.Funct7 0b0000001) (operand1, operand2) store
|
|
|
|
|
quadruple (SubtractionQuadruple operand1 operand2 (Store register))
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
@ -180,32 +162,6 @@ quadruple (NegationQuadruple operand1 (Store register))
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register RiscV.SUB RiscV.Zero operandRegister1
|
|
|
|
|
$ RiscV.Funct7 0b0100000
|
|
|
|
|
quadruple (ProductQuadruple operand1 operand2 (Store register))
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
pure $ lui (immediateOperand1 * immediateOperand2) register
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
in pure $ Vector.singleton $ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register RiscV.MUL operandRegister1 operandRegister2 (RiscV.Funct7 0b0000001)
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
multiplyImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
multiplyImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
multiplyImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand register
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register RiscV.MUL register operandRegister
|
|
|
|
|
$ RiscV.Funct7 0b0000001
|
|
|
|
|
quadruple (DivisionQuadruple operand1 operand2 (Store register))
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
@ -261,198 +217,18 @@ quadruple (DivisionQuadruple operand1 operand2 (Store register))
|
|
|
|
|
]
|
|
|
|
|
quadruple (LabelQuadruple (Label label)) = pure $ Vector.singleton $ JumpLabel label mempty
|
|
|
|
|
quadruple (GoToQuadruple label) = pure $ Vector.singleton $ unconditionalJal label
|
|
|
|
|
quadruple (EqualQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 == immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BEQ operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
compareImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand immediateRegister
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BEQ operandRegister immediateRegister
|
|
|
|
|
quadruple (NonEqualQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 /= immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BNE operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
compareImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand immediateRegister
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BNE operandRegister immediateRegister
|
|
|
|
|
quadruple (LessQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 < immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister1 immediateRegister
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT immediateRegister operandRegister2
|
|
|
|
|
quadruple (GreaterQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 > immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister2 operandRegister1
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT immediateRegister operandRegister1
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister2 immediateRegister
|
|
|
|
|
quadruple (LessOrEqualQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 <= immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister2 operandRegister1
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE immediateRegister operandRegister1
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister2 immediateRegister
|
|
|
|
|
quadruple (GreaterOrEqualQuadruple operand1 operand2 goToLabel)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 >= immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister1 immediateRegister
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE immediateRegister operandRegister2
|
|
|
|
|
quadruple (EqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
commutativeComparison (==) RiscV.BEQ (operand1, operand2) goToLabel
|
|
|
|
|
quadruple (NonEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
commutativeComparison (/=) RiscV.BNE (operand1, operand2) goToLabel
|
|
|
|
|
quadruple (LessQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
lessThan (operand1, operand2) goToLabel
|
|
|
|
|
quadruple (GreaterQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
lessThan (operand2, operand1) goToLabel
|
|
|
|
|
quadruple (LessOrEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
lessOrEqualThan (operand1, operand2) goToLabel
|
|
|
|
|
quadruple (GreaterOrEqualQuadruple operand1 operand2 goToLabel) =
|
|
|
|
|
lessOrEqualThan (operand2, operand1) goToLabel
|
|
|
|
|
quadruple (AssignQuadruple operand1 (Store register))
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1 = pure
|
|
|
|
|
$ lui immediateOperand1 register
|
|
|
|
@ -489,3 +265,143 @@ lui intValue targetRegister
|
|
|
|
|
where
|
|
|
|
|
hi = intValue `shiftR` 12
|
|
|
|
|
lo = fromIntegral intValue
|
|
|
|
|
|
|
|
|
|
commutativeBinary
|
|
|
|
|
:: (Int32 -> Int32 -> Int32)
|
|
|
|
|
-> RiscV.Funct3
|
|
|
|
|
-> RiscV.Funct7
|
|
|
|
|
-> (Operand RiscVStore, Operand RiscVStore)
|
|
|
|
|
-> Store RiscV.XRegister
|
|
|
|
|
-> RiscVGenerator (Vector Statement)
|
|
|
|
|
commutativeBinary immediateOperation funct3 funct7 (operand1, operand2) (Store register)
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 = pure
|
|
|
|
|
$ lui (immediateOperation immediateOperand1 immediateOperand2) register
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
in pure $ Vector.singleton $ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register funct3 operandRegister1 operandRegister2 funct7
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
commutativeImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
commutativeImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
commutativeImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand register
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.BaseInstruction RiscV.Op
|
|
|
|
|
$ RiscV.R register funct3 register operandRegister funct7
|
|
|
|
|
|
|
|
|
|
commutativeComparison
|
|
|
|
|
:: (Int32 -> Int32 -> Bool)
|
|
|
|
|
-> RiscV.Funct3
|
|
|
|
|
-> (Operand RiscVStore, Operand RiscVStore)
|
|
|
|
|
-> Label
|
|
|
|
|
-> RiscVGenerator (Vector Statement)
|
|
|
|
|
commutativeComparison immediateOperation funct3 (operand1, operand2) goToLabel
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperation immediateOperand1 immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' funct3 operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand1 immediateOperand2
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
compareImmediateRegister variableOperand2 immediateOperand1
|
|
|
|
|
where
|
|
|
|
|
compareImmediateRegister variableOperand immediateOperand =
|
|
|
|
|
let statements = lui immediateOperand immediateRegister
|
|
|
|
|
Store operandRegister = variableOperand
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' funct3 operandRegister immediateRegister
|
|
|
|
|
|
|
|
|
|
lessThan :: (Operand RiscVStore, Operand RiscVStore) -> Label -> RiscVGenerator (Vector Statement)
|
|
|
|
|
lessThan (operand1, operand2) goToLabel
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 < immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister1 operandRegister2
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT operandRegister1 immediateRegister
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BLT immediateRegister operandRegister2
|
|
|
|
|
|
|
|
|
|
lessOrEqualThan :: (Operand RiscVStore, Operand RiscVStore) -> Label -> RiscVGenerator (Vector Statement)
|
|
|
|
|
lessOrEqualThan (operand1, operand2) goToLabel
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
if immediateOperand1 <= immediateOperand2
|
|
|
|
|
then pure $ Vector.singleton $ unconditionalJal goToLabel
|
|
|
|
|
else pure Vector.empty
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 = do
|
|
|
|
|
let Store operandRegister1 = variableOperand1
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
pure $ Vector.singleton
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister2 operandRegister1
|
|
|
|
|
| VariableOperand variableOperand1 <- operand1
|
|
|
|
|
, IntOperand immediateOperand2 <- operand2 =
|
|
|
|
|
let statements2 = lui immediateOperand2 immediateRegister
|
|
|
|
|
Store operandRegister1 = variableOperand1
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements2
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE immediateRegister operandRegister1
|
|
|
|
|
| IntOperand immediateOperand1 <- operand1
|
|
|
|
|
, VariableOperand variableOperand2 <- operand2 =
|
|
|
|
|
let statements1 = lui immediateOperand1 immediateRegister
|
|
|
|
|
Store operandRegister2 = variableOperand2
|
|
|
|
|
Label goToLabel' = goToLabel
|
|
|
|
|
in pure $ Vector.snoc statements1
|
|
|
|
|
$ Instruction
|
|
|
|
|
$ RiscV.RelocatableInstruction RiscV.Branch
|
|
|
|
|
$ RiscV.RBranch goToLabel' RiscV.BGE operandRegister2 immediateRegister
|
|
|
|
|