aboutsummaryrefslogtreecommitdiff
path: root/Sil/Syntax.icl
blob: f78ba833a3d11eb329695adfcb4f0c7b2640e1b6 (plain) (blame)
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
implementation module Sil.Syntax

from StdFunc import o
import StdOverloaded
import StdString
import StdTuple

import Data.List
import Data.Maybe
import Text

import Sil.Util.Printer

instance toString Statement
where
	toString (Declaration n a) = n <+ " := " <+ a <+ ";"
	toString (Application e)   = toString e <+ ";"
	toString (Return Nothing)  = "return;"
	toString (Return (Just a)) = "return " <+ a <+ ";"
	toString (If bs e)         = "if ..."
	toString (MachineStm s)    = "|~" <+ s
	toString _                 = "<<unimplemented Statement>>"

instance toString Type
where
	toString TBool = "Bool"
	toString TInt  = "Int"
	toString TVoid = "Void"

instance toString Arg where toString arg = arg.arg_type <+ " " <+ arg.arg_name

instance toString Expression
where
	toString (Name n)               = n
	toString (Literal lit)          = toString lit
	toString (App n args)           = n <+ "(" <+ printersperse ", " args <+ ")"
	toString (BuiltinApp op e)      = op <+ "(" <+ e <+ ")"
	toString (BuiltinApp2 e1 op e2) = "(" <+ e1 <+ ") " <+ op <+ " (" <+ e2 <+ ")"

instance toString Op1
where
	toString Neg = "~"
	toString Not = "!"

instance toString Op2
where
	toString Add    = "+"
	toString Sub    = "-"
	toString Mul    = "*"
	toString Div    = "/"
	toString Rem    = "%"
	toString Equals = "=="
	toString LogOr  = "||"
	toString LogAnd = "&&"

instance toString Literal
where
	toString (BLit b) = toString b
	toString (ILit i) = toString i

instance allStatements Program
where allStatements p = concatMap allStatements p.p_funs

instance allStatements Function
where allStatements f = allStatements f.f_code

instance allStatements CodeBlock
where allStatements cb = concatMap allStatements cb.cb_content

instance allStatements Statement
where
	allStatements st=:(Declaration _ _) = [st]
	allStatements st=:(Application _)   = [st]
	allStatements st=:(Return _)        = [st]
	allStatements st=:(If bs Nothing)   = [st:concatMap (allStatements o snd) bs]
	allStatements st=:(If bs (Just e))  = [st:allStatements e ++ concatMap (allStatements o snd) bs]
	allStatements st=:(While _ cb)      = [st:allStatements cb]
	allStatements st=:(MachineStm _)    = [st]

typeSize :: Type -> Int
typeSize TVoid = 0
typeSize TBool = 1
typeSize TInt  = 1