45d79f5251ae12b5ce8610759f65b8928f53f8b1
[ghc.git] / compiler / llvmGen / Llvm / AbsSyn.hs
1 --------------------------------------------------------------------------------
2 -- | The LLVM abstract syntax.
3 --
4
5 module Llvm.AbsSyn where
6
7 import Llvm.MetaData
8 import Llvm.Types
9
10 import Unique
11
12 -- | Block labels
13 type LlvmBlockId = Unique
14
15 -- | A block of LLVM code.
16 data LlvmBlock = LlvmBlock {
17 -- | The code label for this block
18 blockLabel :: LlvmBlockId,
19
20 -- | A list of LlvmStatement's representing the code for this block.
21 -- This list must end with a control flow statement.
22 blockStmts :: [LlvmStatement]
23 }
24
25 type LlvmBlocks = [LlvmBlock]
26
27 -- | An LLVM Module. This is a top level container in LLVM.
28 data LlvmModule = LlvmModule {
29 -- | Comments to include at the start of the module.
30 modComments :: [LMString],
31
32 -- | LLVM Alias type definitions.
33 modAliases :: [LlvmAlias],
34
35 -- | LLVM meta data.
36 modMeta :: [MetaDecl],
37
38 -- | Global variables to include in the module.
39 modGlobals :: [LMGlobal],
40
41 -- | LLVM Functions used in this module but defined in other modules.
42 modFwdDecls :: LlvmFunctionDecls,
43
44 -- | LLVM Functions defined in this module.
45 modFuncs :: LlvmFunctions
46 }
47
48 -- | An LLVM Function
49 data LlvmFunction = LlvmFunction {
50 -- | The signature of this declared function.
51 funcDecl :: LlvmFunctionDecl,
52
53 -- | The functions arguments
54 funcArgs :: [LMString],
55
56 -- | The function attributes.
57 funcAttrs :: [LlvmFuncAttr],
58
59 -- | The section to put the function into,
60 funcSect :: LMSection,
61
62 -- | Prefix data
63 funcPrefix :: Maybe LlvmStatic,
64
65 -- | The body of the functions.
66 funcBody :: LlvmBlocks
67 }
68
69 type LlvmFunctions = [LlvmFunction]
70
71 type SingleThreaded = Bool
72
73 -- | LLVM ordering types for synchronization purposes. (Introduced in LLVM
74 -- 3.0). Please see the LLVM documentation for a better description.
75 data LlvmSyncOrdering
76 -- | Some partial order of operations exists.
77 = SyncUnord
78 -- | A single total order for operations at a single address exists.
79 | SyncMonotonic
80 -- | Acquire synchronization operation.
81 | SyncAcquire
82 -- | Release synchronization operation.
83 | SyncRelease
84 -- | Acquire + Release synchronization operation.
85 | SyncAcqRel
86 -- | Full sequential Consistency operation.
87 | SyncSeqCst
88 deriving (Show, Eq)
89
90 -- | LLVM atomic operations. Please see the @atomicrmw@ instruction in
91 -- the LLVM documentation for a complete description.
92 data LlvmAtomicOp
93 = LAO_Xchg
94 | LAO_Add
95 | LAO_Sub
96 | LAO_And
97 | LAO_Nand
98 | LAO_Or
99 | LAO_Xor
100 | LAO_Max
101 | LAO_Min
102 | LAO_Umax
103 | LAO_Umin
104 deriving (Show, Eq)
105
106 -- | Llvm Statements
107 data LlvmStatement
108 {- |
109 Assign an expression to an variable:
110 * dest: Variable to assign to
111 * source: Source expression
112 -}
113 = Assignment LlvmVar LlvmExpression
114
115 {- |
116 Memory fence operation
117 -}
118 | Fence Bool LlvmSyncOrdering
119
120 {- |
121 Always branch to the target label
122 -}
123 | Branch LlvmVar
124
125 {- |
126 Branch to label targetTrue if cond is true otherwise to label targetFalse
127 * cond: condition that will be tested, must be of type i1
128 * targetTrue: label to branch to if cond is true
129 * targetFalse: label to branch to if cond is false
130 -}
131 | BranchIf LlvmVar LlvmVar LlvmVar
132
133 {- |
134 Comment
135 Plain comment.
136 -}
137 | Comment [LMString]
138
139 {- |
140 Set a label on this position.
141 * name: Identifier of this label, unique for this module
142 -}
143 | MkLabel LlvmBlockId
144
145 {- |
146 Store variable value in pointer ptr. If value is of type t then ptr must
147 be of type t*.
148 * value: Variable/Constant to store.
149 * ptr: Location to store the value in
150 -}
151 | Store LlvmVar LlvmVar
152
153 {- |
154 Multiway branch
155 * scrutinee: Variable or constant which must be of integer type that is
156 determines which arm is chosen.
157 * def: The default label if there is no match in target.
158 * target: A list of (value,label) where the value is an integer
159 constant and label the corresponding label to jump to if the
160 scrutinee matches the value.
161 -}
162 | Switch LlvmVar LlvmVar [(LlvmVar, LlvmVar)]
163
164 {- |
165 Return a result.
166 * result: The variable or constant to return
167 -}
168 | Return (Maybe LlvmVar)
169
170 {- |
171 An instruction for the optimizer that the code following is not reachable
172 -}
173 | Unreachable
174
175 {- |
176 Raise an expression to a statement (if don't want result or want to use
177 Llvm unnamed values.
178 -}
179 | Expr LlvmExpression
180
181 {- |
182 A nop LLVM statement. Useful as its often more efficient to use this
183 then to wrap LLvmStatement in a Just or [].
184 -}
185 | Nop
186
187 {- |
188 A LLVM statement with metadata attached to it.
189 -}
190 | MetaStmt [MetaAnnot] LlvmStatement
191
192 deriving (Eq)
193
194
195 -- | Llvm Expressions
196 data LlvmExpression
197 {- |
198 Allocate amount * sizeof(tp) bytes on the stack
199 * tp: LlvmType to reserve room for
200 * amount: The nr of tp's which must be allocated
201 -}
202 = Alloca LlvmType Int
203
204 {- |
205 Perform the machine operator op on the operands left and right
206 * op: operator
207 * left: left operand
208 * right: right operand
209 -}
210 | LlvmOp LlvmMachOp LlvmVar LlvmVar
211
212 {- |
213 Perform a compare operation on the operands left and right
214 * op: operator
215 * left: left operand
216 * right: right operand
217 -}
218 | Compare LlvmCmpOp LlvmVar LlvmVar
219
220 {- |
221 Extract a scalar element from a vector
222 * val: The vector
223 * idx: The index of the scalar within the vector
224 -}
225 | Extract LlvmVar LlvmVar
226
227 {- |
228 Extract a scalar element from a structure
229 * val: The structure
230 * idx: The index of the scalar within the structure
231 Corresponds to "extractvalue" instruction.
232 -}
233 | ExtractV LlvmVar Int
234
235 {- |
236 Insert a scalar element into a vector
237 * val: The source vector
238 * elt: The scalar to insert
239 * index: The index at which to insert the scalar
240 -}
241 | Insert LlvmVar LlvmVar LlvmVar
242
243 {- |
244 Allocate amount * sizeof(tp) bytes on the heap
245 * tp: LlvmType to reserve room for
246 * amount: The nr of tp's which must be allocated
247 -}
248 | Malloc LlvmType Int
249
250 {- |
251 Load the value at location ptr
252 -}
253 | Load LlvmVar
254
255 {- |
256 Atomic load of the value at location ptr
257 -}
258 | ALoad LlvmSyncOrdering SingleThreaded LlvmVar
259
260 {- |
261 Navigate in an structure, selecting elements
262 * inbound: Is the pointer inbounds? (computed pointer doesn't overflow)
263 * ptr: Location of the structure
264 * indexes: A list of indexes to select the correct value.
265 -}
266 | GetElemPtr Bool LlvmVar [LlvmVar]
267
268 {- |
269 Cast the variable from to the to type. This is an abstraction of three
270 cast operators in Llvm, inttoptr, prttoint and bitcast.
271 * cast: Cast type
272 * from: Variable to cast
273 * to: type to cast to
274 -}
275 | Cast LlvmCastOp LlvmVar LlvmType
276
277 {- |
278 Atomic read-modify-write operation
279 * op: Atomic operation
280 * addr: Address to modify
281 * operand: Operand to operation
282 * ordering: Ordering requirement
283 -}
284 | AtomicRMW LlvmAtomicOp LlvmVar LlvmVar LlvmSyncOrdering
285
286 {- |
287 Compare-and-exchange operation
288 * addr: Address to modify
289 * old: Expected value
290 * new: New value
291 * suc_ord: Ordering required in success case
292 * fail_ord: Ordering required in failure case, can be no stronger than
293 suc_ord
294
295 Result is an @i1@, true if store was successful.
296 -}
297 | CmpXChg LlvmVar LlvmVar LlvmVar LlvmSyncOrdering LlvmSyncOrdering
298
299 {- |
300 Call a function. The result is the value of the expression.
301 * tailJumps: CallType to signal if the function should be tail called
302 * fnptrval: An LLVM value containing a pointer to a function to be
303 invoked. Can be indirect. Should be LMFunction type.
304 * args: Concrete arguments for the parameters
305 * attrs: A list of function attributes for the call. Only NoReturn,
306 NoUnwind, ReadOnly and ReadNone are valid here.
307 -}
308 | Call LlvmCallType LlvmVar [LlvmVar] [LlvmFuncAttr]
309
310 {- |
311 Call a function as above but potentially taking metadata as arguments.
312 * tailJumps: CallType to signal if the function should be tail called
313 * fnptrval: An LLVM value containing a pointer to a function to be
314 invoked. Can be indirect. Should be LMFunction type.
315 * args: Arguments that may include metadata.
316 * attrs: A list of function attributes for the call. Only NoReturn,
317 NoUnwind, ReadOnly and ReadNone are valid here.
318 -}
319 | CallM LlvmCallType LlvmVar [MetaExpr] [LlvmFuncAttr]
320
321 {- |
322 Merge variables from different basic blocks which are predecessors of this
323 basic block in a new variable of type tp.
324 * tp: type of the merged variable, must match the types of the
325 predecessor variables.
326 * precessors: A list of variables and the basic block that they originate
327 from.
328 -}
329 | Phi LlvmType [(LlvmVar,LlvmVar)]
330
331 {- |
332 Inline assembly expression. Syntax is very similar to the style used by GCC.
333 * assembly: Actual inline assembly code.
334 * constraints: Operand constraints.
335 * return ty: Return type of function.
336 * vars: Any variables involved in the assembly code.
337 * sideeffect: Does the expression have side effects not visible from the
338 constraints list.
339 * alignstack: Should the stack be conservatively aligned before this
340 expression is executed.
341 -}
342 | Asm LMString LMString LlvmType [LlvmVar] Bool Bool
343
344 {- |
345 A LLVM expression with metadata attached to it.
346 -}
347 | MExpr [MetaAnnot] LlvmExpression
348
349 deriving (Eq)
350