1 --------------------------------------------------------------------------------
2 -- | The LLVM abstract syntax.
5 module Llvm.AbsSyn where
12 type LlvmBlockId = Unique
14 -- | A block of LLVM code.
15 data LlvmBlock = LlvmBlock {
16 -- | The code label for this block
17 blockLabel :: LlvmBlockId,
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]
24 type LlvmBlocks = [LlvmBlock]
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],
31 -- | Constants to include in the module.
32 modConstants :: [LMConstant],
34 -- | Global variables to include in the module.
35 modGlobals :: [LMGlobal],
37 -- | LLVM Functions used in this module but defined in other modules.
38 modFwdDecls :: LlvmFunctionDecls,
40 -- | LLVM Functions defined in this module.
41 modFuncs :: LlvmFunctions
45 data LlvmFunction = LlvmFunction {
46 -- | The signature of this declared function.
47 funcDecl :: LlvmFunctionDecl,
49 -- | The function attributes.
50 funcAttrs :: [LlvmFuncAttr],
52 -- | The section to put the function into,
53 funcSect :: LMSection,
55 -- | The body of the functions.
56 funcBody :: LlvmBlocks
59 type LlvmFunctions = [LlvmFunction]
65 Assign an expression to an variable:
66 * dest: Variable to assign to
67 * source: Source expression
69 = Assignment LlvmVar LlvmExpression
72 Always branch to the target label
77 Branch to label targetTrue if cond is true otherwise to label targetFalse
78 * cond: condition that will be tested, must be of type i1
79 * targetTrue: label to branch to if cond is true
80 * targetFalse: label to branch to if cond is false
82 | BranchIf LlvmVar LlvmVar LlvmVar
91 Set a label on this position.
92 * name: Identifier of this label, unique for this module
97 Store variable value in pointer ptr. If value is of type t then ptr must
99 * value: Variable/Constant to store.
100 * ptr: Location to store the value in
102 | Store LlvmVar LlvmVar
106 * scrutinee: Variable or constant which must be of integer type that is
107 determines which arm is chosen.
108 * def: The default label if there is no match in target.
109 * target: A list of (value,label) where the value is an integer
110 constant and label the corresponding label to jump to if the
111 scrutinee matches the value.
113 | Switch LlvmVar LlvmVar [(LlvmVar, LlvmVar)]
117 * result: The variable or constant to return
119 | Return (Maybe LlvmVar)
122 An instruction for the optimizer that the code following is not reachable
127 Raise an expression to a statement (if don't want result or want to use
130 | Expr LlvmExpression
135 -- | Llvm Expressions
138 Allocate amount * sizeof(tp) bytes on the stack
139 * tp: LlvmType to reserve room for
140 * amount: The nr of tp's which must be allocated
142 = Alloca LlvmType Int
145 Perform the machine operator op on the operands left and right
148 * right: right operand
150 | LlvmOp LlvmMachOp LlvmVar LlvmVar
153 Perform a compare operation on the operands left and right
156 * right: right operand
158 | Compare LlvmCmpOp LlvmVar LlvmVar
161 Allocate amount * sizeof(tp) bytes on the heap
162 * tp: LlvmType to reserve room for
163 * amount: The nr of tp's which must be allocated
165 | Malloc LlvmType Int
168 Load the value at location ptr
173 Navigate in an structure, selecting elements
174 * ptr: Location of the structure
175 * indexes: A list of indexes to select the correct value. For example
176 the first element of the third element of the structure ptr
177 is selected with [3,1] (zero indexed)
179 | GetElemPtr LlvmVar [Int]
182 Cast the variable from to the to type. This is an abstraction of three
183 cast operators in Llvm, inttoptr, prttoint and bitcast.
185 * from: Variable to cast
186 * to: type to cast to
188 | Cast LlvmCastOp LlvmVar LlvmType
191 Call a function. The result is the value of the expression.
192 * tailJumps: CallType to signal if the function should be tail called
193 * fnptrval: An LLVM value containing a pointer to a function to be
194 invoked. Can be indirect. Should be LMFunction type.
195 * args: Concrete arguments for the parameters
196 * attrs: A list of function attributes for the call. Only NoReturn,
197 NoUnwind, ReadOnly and ReadNone are valid here.
199 | Call LlvmCallType LlvmVar [LlvmVar] [LlvmFuncAttr]
202 Merge variables from different basic blocks which are predecessors of this
203 basic block in a new variable of type tp.
204 * tp: type of the merged variable, must match the types of the
205 precessors variables.
206 * precessors: A list of variables and the basic block that they originate
209 | Phi LlvmType [(LlvmVar,LlvmVar)]