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 body of the functions.
53 funcBody :: LlvmBlocks
56 type LlvmFunctions = [LlvmFunction]
62 Assign an expression to an variable:
63 * dest: Variable to assign to
64 * source: Source expression
66 = Assignment LlvmVar LlvmExpression
69 Always branch to the target label
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
79 | BranchIf LlvmVar LlvmVar LlvmVar
88 Set a label on this position.
89 * name: Identifier of this label, unique for this module
94 Store variable value in pointer ptr. If value is of type t then ptr must
96 * value: Variable/Constant to store.
97 * ptr: Location to store the value in
99 | Store LlvmVar LlvmVar
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.
110 | Switch LlvmVar LlvmVar [(LlvmVar, LlvmVar)]
114 * result: The variable or constant to return
116 | Return (Maybe LlvmVar)
119 An instruction for the optimizer that the code following is not reachable
124 Raise an expression to a statement (if don't want result or want to use
127 | Expr LlvmExpression
132 -- | Llvm Expressions
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
139 = Alloca LlvmType Int
142 Perform the machine operator op on the operands left and right
145 * right: right operand
147 | LlvmOp LlvmMachOp LlvmVar LlvmVar
150 Perform a compare operation on the operands left and right
153 * right: right operand
155 | Compare LlvmCmpOp LlvmVar LlvmVar
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
162 | Malloc LlvmType Int
165 Load the value at location ptr
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)
176 | GetElemPtr LlvmVar [Int]
179 Cast the variable from to the to type. This is an abstraction of three
180 cast operators in Llvm, inttoptr, prttoint and bitcast.
182 * from: Variable to cast
183 * to: type to cast to
185 | Cast LlvmCastOp LlvmVar LlvmType
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.
196 | Call LlvmCallType LlvmVar [LlvmVar] [LlvmFuncAttr]
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
206 | Phi LlvmType [(LlvmVar,LlvmVar)]