6163fc842cd0b1f91c090e883d0963c0376c5c1e
[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 -- | The body of the functions.
63 funcBody :: LlvmBlocks
64 }
65
66 type LlvmFunctions = [LlvmFunction]
67
68 -- | LLVM function call arguments.
69 data MetaArgs
70 = ArgVar LlvmVar -- ^ Regular LLVM variable as argument.
71 | ArgMeta MetaExpr -- ^ Metadata as argument.
72 deriving (Eq)
73
74 instance Show MetaArgs where
75 show (ArgVar v) = show v
76 show (ArgMeta m) = show m
77
78 -- | LLVM ordering types for synchronization purposes. (Introduced in LLVM
79 -- 3.0). Please see the LLVM documentation for a better description.
80 data LlvmSyncOrdering
81 -- | Some partial order of operations exists.
82 = SyncUnord
83 -- | A single total order for operations at a single address exists.
84 | SyncMonotonic
85 -- | Acquire synchronization operation.
86 | SyncAcquire
87 -- | Release synchronization operation.
88 | SyncRelease
89 -- | Acquire + Release synchronization operation.
90 | SyncAcqRel
91 -- | Full sequential Consistency operation.
92 | SyncSeqCst
93 deriving (Show, Eq)
94
95 -- | Llvm Statements
96 data LlvmStatement
97 {- |
98 Assign an expression to an variable:
99 * dest: Variable to assign to
100 * source: Source expression
101 -}
102 = Assignment LlvmVar LlvmExpression
103
104 {- |
105 Memory fence operation
106 -}
107 | Fence Bool LlvmSyncOrdering
108
109 {- |
110 Always branch to the target label
111 -}
112 | Branch LlvmVar
113
114 {- |
115 Branch to label targetTrue if cond is true otherwise to label targetFalse
116 * cond: condition that will be tested, must be of type i1
117 * targetTrue: label to branch to if cond is true
118 * targetFalse: label to branch to if cond is false
119 -}
120 | BranchIf LlvmVar LlvmVar LlvmVar
121
122 {- |
123 Comment
124 Plain comment.
125 -}
126 | Comment [LMString]
127
128 {- |
129 Set a label on this position.
130 * name: Identifier of this label, unique for this module
131 -}
132 | MkLabel LlvmBlockId
133
134 {- |
135 Store variable value in pointer ptr. If value is of type t then ptr must
136 be of type t*.
137 * value: Variable/Constant to store.
138 * ptr: Location to store the value in
139 -}
140 | Store LlvmVar LlvmVar
141
142 {- |
143 Mutliway branch
144 * scrutinee: Variable or constant which must be of integer type that is
145 determines which arm is chosen.
146 * def: The default label if there is no match in target.
147 * target: A list of (value,label) where the value is an integer
148 constant and label the corresponding label to jump to if the
149 scrutinee matches the value.
150 -}
151 | Switch LlvmVar LlvmVar [(LlvmVar, LlvmVar)]
152
153 {- |
154 Return a result.
155 * result: The variable or constant to return
156 -}
157 | Return (Maybe LlvmVar)
158
159 {- |
160 An instruction for the optimizer that the code following is not reachable
161 -}
162 | Unreachable
163
164 {- |
165 Raise an expression to a statement (if don't want result or want to use
166 Llvm unnamed values.
167 -}
168 | Expr LlvmExpression
169
170 {- |
171 A nop LLVM statement. Useful as its often more efficient to use this
172 then to wrap LLvmStatement in a Just or [].
173 -}
174 | Nop
175
176 {- |
177 A LLVM statement with metadata attached to it.
178 -}
179 | MetaStmt [MetaData] LlvmStatement
180
181 deriving (Show, Eq)
182
183
184 -- | Llvm Expressions
185 data LlvmExpression
186 {- |
187 Allocate amount * sizeof(tp) bytes on the stack
188 * tp: LlvmType to reserve room for
189 * amount: The nr of tp's which must be allocated
190 -}
191 = Alloca LlvmType Int
192
193 {- |
194 Perform the machine operator op on the operands left and right
195 * op: operator
196 * left: left operand
197 * right: right operand
198 -}
199 | LlvmOp LlvmMachOp LlvmVar LlvmVar
200
201 {- |
202 Perform a compare operation on the operands left and right
203 * op: operator
204 * left: left operand
205 * right: right operand
206 -}
207 | Compare LlvmCmpOp LlvmVar LlvmVar
208
209 {- |
210 Extract a scalar element from a vector
211 * val: The vector
212 * idx: The index of the scalar within the vector
213 -}
214 | Extract LlvmVar LlvmVar
215
216 {- |
217 Insert a scalar element into a vector
218 * val: The source vector
219 * elt: The scalar to insert
220 * index: The index at which to insert the scalar
221 -}
222 | Insert LlvmVar LlvmVar LlvmVar
223
224 {- |
225 Allocate amount * sizeof(tp) bytes on the heap
226 * tp: LlvmType to reserve room for
227 * amount: The nr of tp's which must be allocated
228 -}
229 | Malloc LlvmType Int
230
231 {- |
232 Load the value at location ptr
233 -}
234 | Load LlvmVar
235
236 {- |
237 Navigate in an structure, selecting elements
238 * inbound: Is the pointer inbounds? (computed pointer doesn't overflow)
239 * ptr: Location of the structure
240 * indexes: A list of indexes to select the correct value.
241 -}
242 | GetElemPtr Bool LlvmVar [LlvmVar]
243
244 {- |
245 Cast the variable from to the to type. This is an abstraction of three
246 cast operators in Llvm, inttoptr, prttoint and bitcast.
247 * cast: Cast type
248 * from: Variable to cast
249 * to: type to cast to
250 -}
251 | Cast LlvmCastOp LlvmVar LlvmType
252
253 {- |
254 Call a function. The result is the value of the expression.
255 * tailJumps: CallType to signal if the function should be tail called
256 * fnptrval: An LLVM value containing a pointer to a function to be
257 invoked. Can be indirect. Should be LMFunction type.
258 * args: Concrete arguments for the parameters
259 * attrs: A list of function attributes for the call. Only NoReturn,
260 NoUnwind, ReadOnly and ReadNone are valid here.
261 -}
262 | Call LlvmCallType LlvmVar [LlvmVar] [LlvmFuncAttr]
263
264 {- |
265 Call a function as above but potentially taking metadata as arguments.
266 * tailJumps: CallType to signal if the function should be tail called
267 * fnptrval: An LLVM value containing a pointer to a function to be
268 invoked. Can be indirect. Should be LMFunction type.
269 * args: Arguments that may include metadata.
270 * attrs: A list of function attributes for the call. Only NoReturn,
271 NoUnwind, ReadOnly and ReadNone are valid here.
272 -}
273 | CallM LlvmCallType LlvmVar [MetaArgs] [LlvmFuncAttr]
274
275 {- |
276 Merge variables from different basic blocks which are predecessors of this
277 basic block in a new variable of type tp.
278 * tp: type of the merged variable, must match the types of the
279 predecessor variables.
280 * precessors: A list of variables and the basic block that they originate
281 from.
282 -}
283 | Phi LlvmType [(LlvmVar,LlvmVar)]
284
285 {- |
286 Inline assembly expression. Syntax is very similar to the style used by GCC.
287 * assembly: Actual inline assembly code.
288 * constraints: Operand constraints.
289 * return ty: Return type of function.
290 * vars: Any variables involved in the assembly code.
291 * sideeffect: Does the expression have side effects not visible from the
292 constraints list.
293 * alignstack: Should the stack be conservatively aligned before this
294 expression is executed.
295 -}
296 | Asm LMString LMString LlvmType [LlvmVar] Bool Bool
297
298 {- |
299 A LLVM expression with metadata attached to it.
300 -}
301 | MExpr [MetaData] LlvmExpression
302
303 deriving (Show, Eq)
304