-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIR.hs
More file actions
91 lines (84 loc) · 3.89 KB
/
IR.hs
File metadata and controls
91 lines (84 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module IR(CType(..),
IRExpression(..),
IRDefinition(..),
IRStatement(..),
IRTrlanslationUnit,
cTypeOf) where
data CType = CInt | CFloat | CBool | CVoid | CChar
| CSelf -- used in recursive structures
| CTypedefType String CType
| CStructType String [(CType, String)]
| CPointerType CType
deriving Show
instance Eq CType where
CInt == CInt = True
CFloat == CFloat = True
CBool == CBool = True
CChar == CChar = True
CSelf == CSelf = True
(CTypedefType _ t1) == (CTypedefType _ t2) = t1 == t2
(CStructType _ t1) == (CStructType _ t2) = t1 == t2
(CPointerType t1) == (CPointerType t2) = t1 == t2
CVoid == CVoid = True
_ == _ = False
data IRExpression = IRStringLiteral String
| IRCharLiteral Char
| IRIntLiteral Integer
| IRVariable CType String
| IRAssign CType IRExpression IRExpression
| IRDereference CType IRExpression
| IRAddressOf CType IRExpression
| IRArrayRef CType IRExpression IRExpression
| IRBinDot CType IRExpression IRExpression
| IRBinLessThan IRExpression IRExpression
| IRBinGreaterThan IRExpression IRExpression
| IRBinLessEquals IRExpression IRExpression
| IRBinGreaterEquals IRExpression IRExpression
| IREquals IRExpression IRExpression
| IRNotEquals IRExpression IRExpression
| IRBinPlus CType IRExpression IRExpression
| IRBinMinus CType IRExpression IRExpression
| IRBinMul CType IRExpression IRExpression
| IRBinDiv CType IRExpression IRExpression
| IRUnPlus CType IRExpression
| IRUnMinus CType IRExpression
| IRCall CType String [IRExpression]
| IRExternCall CType String String [IRExpression]
deriving (Show)
cTypeOf (IRStringLiteral _) = CPointerType CChar
cTypeOf (IRCharLiteral _) = CChar
cTypeOf (IRIntLiteral _) = CInt
cTypeOf (IRVariable t _) = t
cTypeOf (IRAssign t _ _) = t
cTypeOf (IRDereference t _) = t
cTypeOf (IRAddressOf t _) = t
cTypeOf (IRArrayRef t _ _) = t
cTypeOf (IRBinDot t _ _) = t
cTypeOf (IRBinLessThan _ _) = CBool
cTypeOf (IRBinGreaterThan _ _) = CBool
cTypeOf (IRBinLessEquals _ _) = CBool
cTypeOf (IRBinGreaterEquals _ _) = CBool
cTypeOf (IREquals _ _) = CBool
cTypeOf (IRNotEquals _ _) = CBool
cTypeOf (IRBinPlus t _ _) = t
cTypeOf (IRBinMinus t _ _) = t
cTypeOf (IRBinMul t _ _) = t
cTypeOf (IRBinDiv t _ _) = t
cTypeOf (IRUnPlus t _) = t
cTypeOf (IRUnMinus t _) = t
cTypeOf (IRCall t _ _) = t
cTypeOf (IRExternCall t _ _ _) = t
data IRDefinition = IRVariableDefinition CType String (Maybe IRExpression)
| IRFunctionDefinition CType String [(CType, String)] [IRStatement]
| IRStructDefinition String [(CType, String)]
deriving Show
data IRStatement = IRBlock [IRStatement]
| IRAllocate CType String IRExpression
| IRExpressionStatement IRExpression
| IRIfElse IRExpression IRStatement (Maybe IRStatement)
| IRWhile IRExpression IRStatement
| IRLet [IRDefinition] IRStatement
| IRSkip
| IRReturn (Maybe IRExpression)
deriving Show
type IRTrlanslationUnit = [IRDefinition]