Add new LLVM code generator to GHC. (Version 2)
[ghc-hetmet.git] / compiler / llvmGen / Llvm / AbsSyn.hs
1 --------------------------------------------------------------------------------
2 -- | The LLVM abstract syntax.
3 --
4
5 module Llvm.AbsSyn where
6
7 import Llvm.Types
8
9 import Unique
10
11 -- | Block labels
12 type LlvmBlockId = Unique
13
14 -- | A block of LLVM code.
15 data LlvmBlock = LlvmBlock {
16     -- | The code label for this block
17     blockLabel :: LlvmBlockId,
18
19     -- | A list of LlvmStatement's representing the code for this block.
20     -- This list must end with a control flow statement.
21     blockStmts :: [LlvmStatement]
22   }
23
24 type LlvmBlocks = [LlvmBlock]
25
26 -- | An LLVM Module. This is a top level contianer in LLVM.
27 data LlvmModule = LlvmModule  {
28     -- | Comments to include at the start of the module.
29     modComments  :: [LMString],
30
31     -- | Constants to include in the module.
32     modConstants :: [LMConstant],
33
34     -- | Global variables to include in the module.
35     modGlobals   :: [LMGlobal],
36
37     -- | LLVM Functions used in this module but defined in other modules.
38     modFwdDecls  :: LlvmFunctionDecls,
39
40     -- | LLVM Functions defined in this module.
41     modFuncs     :: LlvmFunctions
42   }
43
44 -- | An LLVM Function
45 data LlvmFunction = LlvmFunction {
46     -- | The signature of this declared function.
47     funcDecl    :: LlvmFunctionDecl,
48
49     -- | The function attributes.
50     funcAttrs   :: [LlvmFuncAttr],
51
52     -- | The body of the functions.
53     funcBody    :: LlvmBlocks
54   }
55
56 type LlvmFunctions  = [LlvmFunction]
57
58
59 -- | Llvm Statements
60 data LlvmStatement
61   {- |
62     Assign an expression to an variable:
63       * dest:   Variable to assign to
64       * source: Source expression
65   -}
66   = Assignment LlvmVar LlvmExpression
67
68   {- |
69     Always branch to the target label
70   -}
71   | Branch LlvmVar
72
73   {- |
74     Branch to label targetTrue if cond is true otherwise to label targetFalse
75       * cond:        condition that will be tested, must be of type i1
76       * targetTrue:  label to branch to if cond is true
77       * targetFalse: label to branch to if cond is false
78   -}
79   | BranchIf LlvmVar LlvmVar LlvmVar
80
81   {- |
82     Comment
83     Plain comment.
84   -}
85   | Comment [LMString]
86
87   {- |
88     Set a label on this position.
89       * name: Identifier of this label, unique for this module
90   -}
91   | MkLabel LlvmBlockId
92
93   {- |
94     Store variable value in pointer ptr. If value is of type t then ptr must
95     be of type t*.
96       * value: Variable/Constant to store.
97       * ptr:   Location to store the value in
98   -}
99   | Store LlvmVar LlvmVar
100
101   {- |
102     Mutliway branch
103       * scrutinee: Variable or constant which must be of integer type that is
104                    determines which arm is chosen.
105       * def:       The default label if there is no match in target.
106       * target:    A list of (value,label) where the value is an integer
107                    constant and label the corresponding label to jump to if the
108                    scrutinee matches the value.
109   -}
110   | Switch LlvmVar LlvmVar [(LlvmVar, LlvmVar)]
111
112   {- |
113     Return a result.
114       * result: The variable or constant to return
115   -}
116   | Return (Maybe LlvmVar)
117
118   {- |
119     An instruction for the optimizer that the code following is not reachable
120   -}
121   | Unreachable
122
123   {- |
124     Raise an expression to a statement (if don't want result or want to use
125     Llvm unamed values.
126   -}
127   | Expr LlvmExpression
128
129   deriving (Show, Eq)
130
131
132 -- | Llvm Expressions
133 data LlvmExpression
134   {- |
135     Allocate amount * sizeof(tp) bytes on the stack
136       * tp:     LlvmType to reserve room for
137       * amount: The nr of tp's which must be allocated
138   -}
139   = Alloca LlvmType Int
140
141   {- |
142     Perform the machine operator op on the operands left and right
143       * op:    operator
144       * left:  left operand
145       * right: right operand
146   -}
147   | LlvmOp LlvmMachOp LlvmVar LlvmVar
148
149   {- |
150     Perform a compare operation on the operands left and right
151       * op:    operator
152       * left:  left operand
153       * right: right operand
154   -}
155   | Compare LlvmCmpOp LlvmVar LlvmVar
156
157   {- |
158     Allocate amount * sizeof(tp) bytes on the heap
159       * tp:     LlvmType to reserve room for
160       * amount: The nr of tp's which must be allocated
161   -}
162   | Malloc LlvmType Int
163
164   {- |
165     Load the value at location ptr
166   -}
167   | Load LlvmVar
168
169   {- |
170     Navigate in an structure, selecting elements
171       * ptr:     Location of the structure
172       * indexes: A list of indexes to select the correct value. For example
173                  the first element of the third element of the structure ptr
174                  is selected with [3,1] (zero indexed)
175   -}
176   | GetElemPtr LlvmVar [Int]
177
178   {- |
179      Cast the variable from to the to type. This is an abstraction of three
180      cast operators in Llvm, inttoptr, prttoint and bitcast.
181        * cast: Cast type
182        * from: Variable to cast
183        * to:   type to cast to
184   -}
185   | Cast LlvmCastOp LlvmVar LlvmType
186
187   {- |
188     Call a function. The result is the value of the expression.
189       * tailJumps: CallType to signal if the function should be tail called
190       * fnptrval:  An LLVM value containing a pointer to a function to be
191                    invoked. Can be indirect. Should be LMFunction type.
192       * args:      Concrete arguments for the parameters
193       * attrs:     A list of function attributes for the call. Only NoReturn,
194                    NoUnwind, ReadOnly and ReadNone are valid here.
195   -}
196   | Call LlvmCallType LlvmVar [LlvmVar] [LlvmFuncAttr]
197
198   {- |
199     Merge variables from different basic blocks which are predecessors of this
200     basic block in a new variable of type tp.
201       * tp:         type of the merged variable, must match the types of the
202                     precessors variables.
203       * precessors: A list of variables and the basic block that they originate
204                     from.
205   -}
206   | Phi LlvmType [(LlvmVar,LlvmVar)]
207
208   deriving (Show, Eq)
209