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