module Compiler.Instruction
( Instruction(..)
, Immediate(..)
, immediateToTypeID
, immediateSize
, getInstCode
, instructionSize
) where
import Data.Word (Word8)
import Common.Type.Integer (IntValue(..))
data Immediate
= ImmBool Bool
| ImmInt IntValue
deriving (Int -> Immediate -> ShowS
[Immediate] -> ShowS
Immediate -> String
(Int -> Immediate -> ShowS)
-> (Immediate -> String)
-> ([Immediate] -> ShowS)
-> Show Immediate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Immediate -> ShowS
showsPrec :: Int -> Immediate -> ShowS
$cshow :: Immediate -> String
show :: Immediate -> String
$cshowList :: [Immediate] -> ShowS
showList :: [Immediate] -> ShowS
Show, Immediate -> Immediate -> Bool
(Immediate -> Immediate -> Bool)
-> (Immediate -> Immediate -> Bool) -> Eq Immediate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Immediate -> Immediate -> Bool
== :: Immediate -> Immediate -> Bool
$c/= :: Immediate -> Immediate -> Bool
/= :: Immediate -> Immediate -> Bool
Eq, Eq Immediate
Eq Immediate =>
(Immediate -> Immediate -> Ordering)
-> (Immediate -> Immediate -> Bool)
-> (Immediate -> Immediate -> Bool)
-> (Immediate -> Immediate -> Bool)
-> (Immediate -> Immediate -> Bool)
-> (Immediate -> Immediate -> Immediate)
-> (Immediate -> Immediate -> Immediate)
-> Ord Immediate
Immediate -> Immediate -> Bool
Immediate -> Immediate -> Ordering
Immediate -> Immediate -> Immediate
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Immediate -> Immediate -> Ordering
compare :: Immediate -> Immediate -> Ordering
$c< :: Immediate -> Immediate -> Bool
< :: Immediate -> Immediate -> Bool
$c<= :: Immediate -> Immediate -> Bool
<= :: Immediate -> Immediate -> Bool
$c> :: Immediate -> Immediate -> Bool
> :: Immediate -> Immediate -> Bool
$c>= :: Immediate -> Immediate -> Bool
>= :: Immediate -> Immediate -> Bool
$cmax :: Immediate -> Immediate -> Immediate
max :: Immediate -> Immediate -> Immediate
$cmin :: Immediate -> Immediate -> Immediate
min :: Immediate -> Immediate -> Immediate
Ord)
immediateToTypeID :: Immediate -> Word8
immediateToTypeID :: Immediate -> Word8
immediateToTypeID (ImmBool Bool
_) = Word8
0x00
immediateToTypeID (ImmInt (I8 Int8
_)) = Word8
0x01
immediateToTypeID (ImmInt (UI8 Word8
_)) = Word8
0x02
immediateToTypeID (ImmInt (IChar Int8
_)) = Word8
0x09
immediateToTypeID (ImmInt (UIChar Word8
_)) = Word8
0x10
immediateToTypeID (ImmInt (I16 Int16
_)) = Word8
0x03
immediateToTypeID (ImmInt (UI16 Word16
_)) = Word8
0x04
immediateToTypeID (ImmInt (I32 Int32
_)) = Word8
0x05
immediateToTypeID (ImmInt (UI32 Word32
_)) = Word8
0x06
immediateToTypeID (ImmInt (I64 Int64
_)) = Word8
0x07
immediateToTypeID (ImmInt (UI64 Word64
_)) = Word8
0x08
immediateSize :: Immediate -> Int
immediateSize :: Immediate -> Int
immediateSize (ImmBool Bool
_) = Int
1
immediateSize (ImmInt (I8 Int8
_)) = Int
1
immediateSize (ImmInt (UI8 Word8
_)) = Int
1
immediateSize (ImmInt (IChar Int8
_)) = Int
1
immediateSize (ImmInt (UIChar Word8
_)) = Int
1
immediateSize (ImmInt (I16 Int16
_)) = Int
2
immediateSize (ImmInt (UI16 Word16
_)) = Int
2
immediateSize (ImmInt (I32 Int32
_)) = Int
4
immediateSize (ImmInt (UI32 Word32
_)) = Int
4
immediateSize (ImmInt (I64 Int64
_)) = Int
8
immediateSize (ImmInt (UI64 Word64
_)) = Int
8
data Instruction
= Push Immediate
| Pop
| Dup
| Swap
| Add
| Sub
| Mul
| Div
| Mod
| Eq
| TEq
| Lt
| Le
| Not
| And
| Or
| Jump Int
| JumpIfFalse Int
| JumpIfTrue Int
| Call Int
| TailCall Int
| CallIndirect
| Ret Int
| LoadLocal Int
| StoreLocal Int
| LoadGlobal Int
| StoreGlobal Int
| LoadCapture Int
| StoreCapture Int
| MakeClosure Int Int
| GetFuncAddr Int
| BuildStruct Int
| GetStructField Int
| AttrUpdate
| Cast Word8
| TypeOf
| SizeOf
| Cons
| Head
| Tail
| Nth
| NthUpdate
| BuildList Int
| Open
| Read
| Write
| Close
| Input
| Print
| Halt
| Exit
| CheckStack Int
| Nop
deriving (Int -> Instruction -> ShowS
[Instruction] -> ShowS
Instruction -> String
(Int -> Instruction -> ShowS)
-> (Instruction -> String)
-> ([Instruction] -> ShowS)
-> Show Instruction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Instruction -> ShowS
showsPrec :: Int -> Instruction -> ShowS
$cshow :: Instruction -> String
show :: Instruction -> String
$cshowList :: [Instruction] -> ShowS
showList :: [Instruction] -> ShowS
Show, Instruction -> Instruction -> Bool
(Instruction -> Instruction -> Bool)
-> (Instruction -> Instruction -> Bool) -> Eq Instruction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Instruction -> Instruction -> Bool
== :: Instruction -> Instruction -> Bool
$c/= :: Instruction -> Instruction -> Bool
/= :: Instruction -> Instruction -> Bool
Eq, Eq Instruction
Eq Instruction =>
(Instruction -> Instruction -> Ordering)
-> (Instruction -> Instruction -> Bool)
-> (Instruction -> Instruction -> Bool)
-> (Instruction -> Instruction -> Bool)
-> (Instruction -> Instruction -> Bool)
-> (Instruction -> Instruction -> Instruction)
-> (Instruction -> Instruction -> Instruction)
-> Ord Instruction
Instruction -> Instruction -> Bool
Instruction -> Instruction -> Ordering
Instruction -> Instruction -> Instruction
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Instruction -> Instruction -> Ordering
compare :: Instruction -> Instruction -> Ordering
$c< :: Instruction -> Instruction -> Bool
< :: Instruction -> Instruction -> Bool
$c<= :: Instruction -> Instruction -> Bool
<= :: Instruction -> Instruction -> Bool
$c> :: Instruction -> Instruction -> Bool
> :: Instruction -> Instruction -> Bool
$c>= :: Instruction -> Instruction -> Bool
>= :: Instruction -> Instruction -> Bool
$cmax :: Instruction -> Instruction -> Instruction
max :: Instruction -> Instruction -> Instruction
$cmin :: Instruction -> Instruction -> Instruction
min :: Instruction -> Instruction -> Instruction
Ord)
getInstCode :: Instruction -> Word8
getInstCode :: Instruction -> Word8
getInstCode (Push Immediate
_) = Word8
0x01
getInstCode Instruction
Pop = Word8
0x02
getInstCode Instruction
Dup = Word8
0x03
getInstCode Instruction
Swap = Word8
0x04
getInstCode Instruction
Add = Word8
0x10
getInstCode Instruction
Sub = Word8
0x11
getInstCode Instruction
Mul = Word8
0x12
getInstCode Instruction
Div = Word8
0x13
getInstCode Instruction
Mod = Word8
0x14
getInstCode Instruction
Eq = Word8
0x20
getInstCode Instruction
Lt = Word8
0x21
getInstCode Instruction
Not = Word8
0x22
getInstCode Instruction
And = Word8
0x23
getInstCode Instruction
Or = Word8
0x24
getInstCode Instruction
Le = Word8
0x25
getInstCode Instruction
TEq = Word8
0x26
getInstCode (Jump Int
_) = Word8
0x30
getInstCode (JumpIfFalse Int
_) = Word8
0x31
getInstCode (JumpIfTrue Int
_) = Word8
0x32
getInstCode (Call Int
_) = Word8
0x40
getInstCode (TailCall Int
_) = Word8
0x41
getInstCode Instruction
CallIndirect = Word8
0x42
getInstCode (Ret Int
_) = Word8
0x43
getInstCode (LoadLocal Int
_) = Word8
0x50
getInstCode (StoreLocal Int
_) = Word8
0x51
getInstCode (LoadGlobal Int
_) = Word8
0x52
getInstCode (StoreGlobal Int
_) = Word8
0x53
getInstCode (LoadCapture Int
_) = Word8
0x54
getInstCode (StoreCapture Int
_) = Word8
0x55
getInstCode (MakeClosure Int
_ Int
_) = Word8
0x60
getInstCode (GetFuncAddr Int
_) = Word8
0x61
getInstCode (BuildStruct Int
_) = Word8
0x62
getInstCode (GetStructField Int
_) = Word8
0x63
getInstCode Instruction
AttrUpdate = Word8
0x64
getInstCode (Cast Word8
_) = Word8
0x80
getInstCode Instruction
TypeOf = Word8
0x81
getInstCode Instruction
SizeOf = Word8
0x82
getInstCode Instruction
Cons = Word8
0x90
getInstCode Instruction
Head = Word8
0x91
getInstCode Instruction
Tail = Word8
0x92
getInstCode Instruction
Nth = Word8
0x93
getInstCode (BuildList Int
_) = Word8
0x94
getInstCode Instruction
NthUpdate = Word8
0x95
getInstCode Instruction
Open = Word8
0xA0
getInstCode Instruction
Read = Word8
0xA1
getInstCode Instruction
Write = Word8
0xA2
getInstCode Instruction
Close = Word8
0xA3
getInstCode Instruction
Input = Word8
0xA4
getInstCode Instruction
Print = Word8
0x70
getInstCode Instruction
Halt = Word8
0x71
getInstCode Instruction
Exit = Word8
0x72
getInstCode (CheckStack Int
_) = Word8
0xFE
getInstCode Instruction
Nop = Word8
0xFF
instructionSize :: Instruction -> Int
instructionSize :: Instruction -> Int
instructionSize (Push Immediate
imm) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Immediate -> Int
immediateSize Immediate
imm
instructionSize Instruction
Pop = Int
1
instructionSize Instruction
Dup = Int
1
instructionSize Instruction
Swap = Int
1
instructionSize Instruction
Add = Int
1
instructionSize Instruction
Sub = Int
1
instructionSize Instruction
Mul = Int
1
instructionSize Instruction
Div = Int
1
instructionSize Instruction
Mod = Int
1
instructionSize Instruction
Eq = Int
1
instructionSize Instruction
TEq = Int
1
instructionSize Instruction
Lt = Int
1
instructionSize Instruction
Le = Int
1
instructionSize Instruction
Not = Int
1
instructionSize Instruction
And = Int
1
instructionSize Instruction
Or = Int
1
instructionSize (Jump Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (JumpIfFalse Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (JumpIfTrue Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (Call Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (TailCall Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize Instruction
CallIndirect = Int
1
instructionSize (Ret Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (LoadLocal Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (StoreLocal Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (LoadGlobal Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (StoreGlobal Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (LoadCapture Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (StoreCapture Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (MakeClosure Int
_ Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (GetFuncAddr Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (BuildStruct Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize (GetStructField Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize Instruction
AttrUpdate = Int
1
instructionSize (Cast Word8
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
instructionSize Instruction
TypeOf = Int
1
instructionSize Instruction
SizeOf = Int
1
instructionSize Instruction
Cons = Int
1
instructionSize Instruction
Head = Int
1
instructionSize Instruction
Tail = Int
1
instructionSize Instruction
Nth = Int
1
instructionSize (BuildList Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize Instruction
NthUpdate = Int
1
instructionSize Instruction
Open = Int
1
instructionSize Instruction
Read = Int
1
instructionSize Instruction
Write = Int
1
instructionSize Instruction
Close = Int
1
instructionSize Instruction
Input = Int
1
instructionSize Instruction
Print = Int
1
instructionSize Instruction
Halt = Int
1
instructionSize Instruction
Exit = Int
1
instructionSize (CheckStack Int
_) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
instructionSize Instruction
Nop = Int
1