IRForTarget.h revision 86d6ac2a3d920622c1ee7a68b5ca28b09dc18142
168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//===-- IRForTarget.h ---------------------------------------------*- C++ -*-===//
268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//
368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//
568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// License. See LICENSE.TXT for details.
768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//
868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//===----------------------------------------------------------------------===//
968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#ifndef liblldb_IRForTarget_h_
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define liblldb_IRForTarget_h_
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "lldb/lldb-public.h"
1468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "lldb/Core/ConstString.h"
1568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "lldb/Core/Error.h"
1668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "lldb/Core/Stream.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "lldb/Core/StreamString.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "lldb/Symbol/TaggedASTType.h"
1968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "llvm/Pass.h"
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace llvm {
2268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class BasicBlock;
2368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class CallInst;
2468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Constant;
2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class ConstantInt;
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Function;
2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class GlobalValue;
2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class GlobalVariable;
2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Instruction;
3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Module;
3168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class StoreInst;
3268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class DataLayout;
3368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Type;
3468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class Value;
3568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
3668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace lldb_private {
3868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class ClangExpressionDeclMap;
3968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class IRExecutionUnit;
4068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    class IRMemoryMap;
4168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
4268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
4368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//----------------------------------------------------------------------
4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// @brief Transforms the IR for a function to run in the target
4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///
4768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// Once an expression has been parsed and converted to IR, it can run
4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// in two contexts: interpreted by LLDB as a DWARF location expression,
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// or compiled by the JIT and inserted into the target process for
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// execution.
5168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)///
5268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// IRForTarget makes the second possible, by applying a series of
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// transformations to the IR which make it relocatable.  These
5468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// transformations are discussed in more detail next to their relevant
5568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)/// functions.
5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)//----------------------------------------------------------------------
5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)class IRForTarget : public llvm::ModulePass
5868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles){
5968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)public:
6068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    //------------------------------------------------------------------
6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// Constructor
6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] decl_map
6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     The list of externally-referenced variables for the expression,
6568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     for use in looking up globals and allocating the argument
6668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     struct.  See the documentation for ClangExpressionDeclMap.
6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] resolve_vars
6968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     True if the external variable references (including persistent
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     variables) should be resolved.  If not, only external functions
7168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     are resolved.
7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] execution_policy
7468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     Determines whether an IR interpreter can be used to statically
7568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     evaluate the expression.
7668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] const_result
7868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     This variable is populated with the statically-computed result
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     of the function, if it has no side-effects and the result can
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///     be computed statically.
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] execution_unit
8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     The holder for raw data associated with the expression.
8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] error_stream
8668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     If non-NULL, a stream on which errors can be printed.
8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] func_name
8968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     The name of the function to prepare for execution in the target.
9068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    //------------------------------------------------------------------
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                bool resolve_vars,
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                lldb_private::ExecutionPolicy execution_policy,
9468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                lldb::ClangExpressionVariableSP &const_result,
9568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                lldb_private::IRExecutionUnit &execution_unit,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                lldb_private::Stream *error_stream,
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                const char* func_name = "$__lldb_expr");
9868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Destructor
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual ~IRForTarget();
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    //------------------------------------------------------------------
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Run this IR transformer on a single module
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    /// Implementation of the llvm::ModulePass::runOnModule() function.
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ///
10968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] llvm_module
11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     The module to run on.  This module is searched for the function
11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     $__lldb_expr, and that function is passed to the passes one by
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     one.
11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
11468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    /// @param[in] interpreter_error
11568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     An error.  If the expression fails to be interpreted, this error
11668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///     is set to a reason why.
11768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    ///
118    /// @return
119    ///     True on success; false otherwise
120    //------------------------------------------------------------------
121    virtual bool
122    runOnModule (llvm::Module &llvm_module);
123
124    //------------------------------------------------------------------
125    /// Interface stub
126    ///
127    /// Implementation of the llvm::ModulePass::assignPassManager()
128    /// function.
129    //------------------------------------------------------------------
130    virtual void
131    assignPassManager (llvm::PMStack &pass_mgr_stack,
132                       llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager);
133
134    //------------------------------------------------------------------
135    /// Returns PMT_ModulePassManager
136    ///
137    /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
138    /// function.
139    //------------------------------------------------------------------
140    virtual llvm::PassManagerType
141    getPotentialPassManagerType() const;
142
143    //------------------------------------------------------------------
144    /// Checks whether the IR interpreter successfully interpreted the
145    /// expression.
146    ///
147    /// Returns true if it did; false otherwise.
148    //------------------------------------------------------------------
149    lldb_private::Error &
150    getInterpreterError ()
151    {
152        return m_interpreter_error;
153    }
154
155private:
156    //------------------------------------------------------------------
157    /// Ensures that the current function's linkage is set to external.
158    /// Otherwise the JIT may not return an address for it.
159    ///
160    /// @param[in] llvm_function
161    ///     The function whose linkage is to be fixed.
162    ///
163    /// @return
164    ///     True on success; false otherwise.
165    //------------------------------------------------------------------
166    bool
167    FixFunctionLinkage (llvm::Function &llvm_function);
168
169    //------------------------------------------------------------------
170    /// A module-level pass to replace all function pointers with their
171    /// integer equivalents.
172    //------------------------------------------------------------------
173
174    //------------------------------------------------------------------
175    /// The top-level pass implementation
176    ///
177    /// @param[in] llvm_module
178    ///     The module currently being processed.
179    ///
180    /// @param[in] llvm_function
181    ///     The function currently being processed.
182    ///
183    /// @return
184    ///     True on success; false otherwise.
185    //------------------------------------------------------------------
186    bool
187    HasSideEffects (llvm::Function &llvm_function);
188
189    //------------------------------------------------------------------
190    /// A function-level pass to check whether the function has side
191    /// effects.
192    //------------------------------------------------------------------
193
194    //------------------------------------------------------------------
195    /// Get the address of a fuction, and a location to put the complete
196    /// Value of the function if one is available.
197    ///
198    /// @param[in] function
199    ///     The function to find the location of.
200    ///
201    /// @param[out] ptr
202    ///     The location of the function in the target.
203    ///
204    /// @param[out] name
205    ///     The resolved name of the function (matters for intrinsics).
206    ///
207    /// @param[out] value_ptr
208    ///     A variable to put the function's completed Value* in, or NULL
209    ///     if the Value* shouldn't be stored anywhere.
210    ///
211    /// @return
212    ///     The pointer.
213    //------------------------------------------------------------------
214    bool
215    GetFunctionAddress (llvm::Function *function,
216                        uint64_t &ptr,
217                        lldb_private::ConstString &name,
218                        llvm::Constant **&value_ptr);
219
220    //------------------------------------------------------------------
221    /// Build a function pointer given a type and a raw pointer.
222    ///
223    /// @param[in] type
224    ///     The type of the function pointer to be built.
225    ///
226    /// @param[in] ptr
227    ///     The value of the pointer.
228    ///
229    /// @return
230    ///     The pointer.
231    //------------------------------------------------------------------
232    llvm::Constant *
233    BuildFunctionPointer (llvm::Type *type,
234                          uint64_t ptr);
235
236    void
237    RegisterFunctionMetadata (llvm::LLVMContext &context,
238                              llvm::Value *function_ptr,
239                              const char *name);
240
241    //------------------------------------------------------------------
242    /// The top-level pass implementation
243    ///
244    /// @param[in] llvm_function
245    ///     The function currently being processed.
246    ///
247    /// @return
248    ///     True if the function has side effects (or if this cannot
249    ///     be determined); false otherwise.
250    //------------------------------------------------------------------
251    bool
252    ResolveFunctionPointers (llvm::Module &llvm_module,
253                             llvm::Function &llvm_function);
254
255    //------------------------------------------------------------------
256    /// A function-level pass to take the generated global value
257    /// $__lldb_expr_result and make it into a persistent variable.
258    /// Also see ASTResultSynthesizer.
259    //------------------------------------------------------------------
260
261    //------------------------------------------------------------------
262    /// Find the NamedDecl corresponding to a Value.  This interface is
263    /// exposed for the IR interpreter.
264    ///
265    /// @param[in] module
266    ///     The module containing metadata to search
267    ///
268    /// @param[in] global
269    ///     The global entity to search for
270    ///
271    /// @return
272    ///     The corresponding variable declaration
273    //------------------------------------------------------------------
274public:
275    static clang::NamedDecl *
276    DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module);
277private:
278    clang::NamedDecl *
279    DeclForGlobal (llvm::GlobalValue *global);
280
281    //------------------------------------------------------------------
282    /// Set the constant result variable m_const_result to the provided
283    /// constant, assuming it can be evaluated.  The result variable
284    /// will be reset to NULL later if the expression has side effects.
285    ///
286    /// @param[in] initializer
287    ///     The constant initializer for the variable.
288    ///
289    /// @param[in] name
290    ///     The name of the result variable.
291    ///
292    /// @param[in] type
293    ///     The Clang type of the result variable.
294    //------------------------------------------------------------------
295    void
296    MaybeSetConstantResult (llvm::Constant *initializer,
297                            const lldb_private::ConstString &name,
298                            lldb_private::TypeFromParser type);
299
300    //------------------------------------------------------------------
301    /// If the IR represents a cast of a variable, set m_const_result
302    /// to the result of the cast.  The result variable will be reset to
303    /// NULL latger if the expression has side effects.
304    ///
305    /// @param[in] type
306    ///     The Clang type of the result variable.
307    //------------------------------------------------------------------
308    void
309    MaybeSetCastResult (lldb_private::TypeFromParser type);
310
311    //------------------------------------------------------------------
312    /// The top-level pass implementation
313    ///
314    /// @param[in] llvm_function
315    ///     The function currently being processed.
316    ///
317    /// @return
318    ///     True on success; false otherwise
319    //------------------------------------------------------------------
320    bool
321    CreateResultVariable (llvm::Function &llvm_function);
322
323    //------------------------------------------------------------------
324    /// A function-level pass to find Objective-C constant strings and
325    /// transform them to calls to CFStringCreateWithBytes.
326    //------------------------------------------------------------------
327
328    //------------------------------------------------------------------
329    /// Rewrite a single Objective-C constant string.
330    ///
331    /// @param[in] NSStr
332    ///     The constant NSString to be transformed
333    ///
334    /// @param[in] CStr
335    ///     The constant C string inside the NSString.  This will be
336    ///     passed as the bytes argument to CFStringCreateWithBytes.
337    ///
338    /// @param[in] FirstEntryInstruction
339    ///     An instruction early in the execution of the function.
340    ///     When this function synthesizes a call to
341    ///     CFStringCreateWithBytes, it places the call before this
342    ///     instruction.  The instruction should come before all
343    ///     uses of the NSString.
344    ///
345    /// @return
346    ///     True on success; false otherwise
347    //------------------------------------------------------------------
348    bool
349    RewriteObjCConstString (llvm::GlobalVariable *NSStr,
350                            llvm::GlobalVariable *CStr,
351                            llvm::Instruction *FirstEntryInstruction);
352
353    //------------------------------------------------------------------
354    /// The top-level pass implementation
355    ///
356    /// @param[in] llvm_function
357    ///     The function currently being processed.
358    ///
359    /// @return
360    ///     True on success; false otherwise
361    //------------------------------------------------------------------
362    bool
363    RewriteObjCConstStrings (llvm::Function &llvm_function);
364
365    //------------------------------------------------------------------
366    /// A basic block-level pass to find all Objective-C method calls and
367    /// rewrite them to use sel_registerName instead of statically allocated
368    /// selectors.  The reason is that the selectors are created on the
369    /// assumption that the Objective-C runtime will scan the appropriate
370    /// section and prepare them.  This doesn't happen when code is copied
371    /// into the target, though, and there's no easy way to induce the
372    /// runtime to scan them.  So instead we get our selectors from
373    /// sel_registerName.
374    //------------------------------------------------------------------
375
376    //------------------------------------------------------------------
377    /// Replace a single selector reference
378    ///
379    /// @param[in] selector_load
380    ///     The load of the statically-allocated selector.
381    ///
382    /// @return
383    ///     True on success; false otherwise
384    //------------------------------------------------------------------
385    bool
386    RewriteObjCSelector (llvm::Instruction* selector_load);
387
388    //------------------------------------------------------------------
389    /// The top-level pass implementation
390    ///
391    /// @param[in] basic_block
392    ///     The basic block currently being processed.
393    ///
394    /// @return
395    ///     True on success; false otherwise
396    //------------------------------------------------------------------
397    bool
398    RewriteObjCSelectors (llvm::BasicBlock &basic_block);
399
400    //------------------------------------------------------------------
401    /// A basic block-level pass to find all newly-declared persistent
402    /// variables and register them with the ClangExprDeclMap.  This
403    /// allows them to be materialized and dematerialized like normal
404    /// external variables.  Before transformation, these persistent
405    /// variables look like normal locals, so they have an allocation.
406    /// This pass excises these allocations and makes references look
407    /// like external references where they will be resolved -- like all
408    /// other external references -- by ResolveExternals().
409    //------------------------------------------------------------------
410
411    //------------------------------------------------------------------
412    /// Handle a single allocation of a persistent variable
413    ///
414    /// @param[in] persistent_alloc
415    ///     The allocation of the persistent variable.
416    ///
417    /// @return
418    ///     True on success; false otherwise
419    //------------------------------------------------------------------
420    bool
421    RewritePersistentAlloc (llvm::Instruction *persistent_alloc);
422
423    //------------------------------------------------------------------
424    /// The top-level pass implementation
425    ///
426    /// @param[in] basic_block
427    ///     The basic block currently being processed.
428    //------------------------------------------------------------------
429    bool
430    RewritePersistentAllocs (llvm::BasicBlock &basic_block);
431
432    //------------------------------------------------------------------
433    /// A function-level pass to find all external variables and functions
434    /// used in the IR.  Each found external variable is added to the
435    /// struct, and each external function is resolved in place, its call
436    /// replaced with a call to a function pointer whose value is the
437    /// address of the function in the target process.
438    //------------------------------------------------------------------
439
440    //------------------------------------------------------------------
441    /// Write an initializer to a memory array of assumed sufficient
442    /// size.
443    ///
444    /// @param[in] data
445    ///     A pointer to the data to write to.
446    ///
447    /// @param[in] initializer
448    ///     The initializer itself.
449    ///
450    /// @return
451    ///     True on success; false otherwise
452    //------------------------------------------------------------------
453    bool
454    MaterializeInitializer (uint8_t *data, llvm::Constant *initializer);
455
456    //------------------------------------------------------------------
457    /// Move an internal variable into the static allocation section.
458    ///
459    /// @param[in] global_variable
460    ///     The variable.
461    ///
462    /// @return
463    ///     True on success; false otherwise
464    //------------------------------------------------------------------
465    bool
466    MaterializeInternalVariable (llvm::GlobalVariable *global_variable);
467
468    //------------------------------------------------------------------
469    /// Handle a single externally-defined variable
470    ///
471    /// @param[in] value
472    ///     The variable.
473    ///
474    /// @return
475    ///     True on success; false otherwise
476    //------------------------------------------------------------------
477    bool
478    MaybeHandleVariable (llvm::Value *value);
479
480    //------------------------------------------------------------------
481    /// Handle a single externally-defined symbol
482    ///
483    /// @param[in] symbol
484    ///     The symbol.
485    ///
486    /// @return
487    ///     True on success; false otherwise
488    //------------------------------------------------------------------
489    bool
490    HandleSymbol (llvm::Value *symbol);
491
492    //------------------------------------------------------------------
493    /// Handle a single externally-defined Objective-C class
494    ///
495    /// @param[in] classlist_reference
496    ///     The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
497    ///     where n (if present) is an index.
498    ///
499    /// @return
500    ///     True on success; false otherwise
501    //------------------------------------------------------------------
502    bool
503    HandleObjCClass(llvm::Value *classlist_reference);
504
505    //------------------------------------------------------------------
506    /// Handle all the arguments to a function call
507    ///
508    /// @param[in] C
509    ///     The call instruction.
510    ///
511    /// @return
512    ///     True on success; false otherwise
513    //------------------------------------------------------------------
514    bool
515    MaybeHandleCallArguments (llvm::CallInst *call_inst);
516
517    //------------------------------------------------------------------
518    /// Resolve variable references in calls to external functions
519    ///
520    /// @param[in] basic_block
521    ///     The basic block currently being processed.
522    ///
523    /// @return
524    ///     True on success; false otherwise
525    //------------------------------------------------------------------
526    bool
527    ResolveCalls (llvm::BasicBlock &basic_block);
528
529    //------------------------------------------------------------------
530    /// Remove calls to __cxa_atexit, which should never be generated by
531    /// expressions.
532    ///
533    /// @param[in] call_inst
534    ///     The call instruction.
535    ///
536    /// @return
537    ///     True if the scan was successful; false if some operation
538    ///     failed
539    //------------------------------------------------------------------
540    bool
541    RemoveCXAAtExit (llvm::BasicBlock &basic_block);
542
543    //------------------------------------------------------------------
544    /// The top-level pass implementation
545    ///
546    /// @param[in] basic_block
547    ///     The function currently being processed.
548    ///
549    /// @return
550    ///     True on success; false otherwise
551    //------------------------------------------------------------------
552    bool
553    ResolveExternals (llvm::Function &llvm_function);
554
555    //------------------------------------------------------------------
556    /// A basic block-level pass to excise guard variables from the code.
557    /// The result for the function is passed through Clang as a static
558    /// variable.  Static variables normally have guard variables to
559    /// ensure that they are only initialized once.
560    //------------------------------------------------------------------
561
562    //------------------------------------------------------------------
563    /// Rewrite a load to a guard variable to return constant 0.
564    ///
565    /// @param[in] guard_load
566    ///     The load instruction to zero out.
567    //------------------------------------------------------------------
568    void
569    TurnGuardLoadIntoZero(llvm::Instruction* guard_load);
570
571    //------------------------------------------------------------------
572    /// The top-level pass implementation
573    ///
574    /// @param[in] basic_block
575    ///     The basic block currently being processed.
576    ///
577    /// @return
578    ///     True on success; false otherwise
579    //------------------------------------------------------------------
580    bool
581    RemoveGuards (llvm::BasicBlock &basic_block);
582
583    //------------------------------------------------------------------
584    /// A module-level pass to allocate all string literals in a separate
585    /// allocation and redirect references to them.
586    //------------------------------------------------------------------
587
588    //------------------------------------------------------------------
589    /// The top-level pass implementation
590    ///
591    /// @return
592    ///     True on success; false otherwise
593    //------------------------------------------------------------------
594    bool
595    ReplaceStrings ();
596
597    //------------------------------------------------------------------
598    /// A basick block-level pass to find all literals that will be
599    /// allocated as statics by the JIT (in contrast to the Strings,
600    /// which already are statics) and synthesize loads for them.
601    //------------------------------------------------------------------
602
603    //------------------------------------------------------------------
604    /// The top-level pass implementation
605    ///
606    /// @param[in] basic_block
607    ///     The basic block currently being processed.
608    ///
609    /// @return
610    ///     True on success; false otherwise
611    //------------------------------------------------------------------
612    bool
613    ReplaceStaticLiterals (llvm::BasicBlock &basic_block);
614
615    //------------------------------------------------------------------
616    /// A function-level pass to make all external variable references
617    /// point at the correct offsets from the void* passed into the
618    /// function.  ClangExpressionDeclMap::DoStructLayout() must be called
619    /// beforehand, so that the offsets are valid.
620    //------------------------------------------------------------------
621
622    //------------------------------------------------------------------
623    /// The top-level pass implementation
624    ///
625    /// @param[in] llvm_function
626    ///     The function currently being processed.
627    ///
628    /// @return
629    ///     True on success; false otherwise
630    //------------------------------------------------------------------
631    bool
632    ReplaceVariables (llvm::Function &llvm_function);
633
634    //------------------------------------------------------------------
635    /// A module-level pass to remove all global variables from the
636    /// module since it no longer should export or import any symbols.
637    //------------------------------------------------------------------
638
639    //------------------------------------------------------------------
640    /// The top-level pass implementation
641    ///
642    /// @param[in] llvm_module
643    ///     The module currently being processed.
644    ///
645    /// @return
646    ///     True on success; false otherwise
647    //------------------------------------------------------------------
648    bool
649    StripAllGVs (llvm::Module &llvm_module);
650
651    class StaticDataAllocator {
652    public:
653        StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit);
654        lldb_private::StreamString &GetStream()
655        {
656            return m_stream_string;
657        }
658        lldb::addr_t Allocate();
659    private:
660        lldb_private::IRExecutionUnit  &m_execution_unit;
661        lldb_private::StreamString      m_stream_string;
662        lldb::addr_t                    m_allocation;
663    };
664
665    /// Flags
666    bool                                    m_resolve_vars;             ///< True if external variable references and persistent variable references should be resolved
667    lldb_private::ExecutionPolicy           m_execution_policy;         ///< True if the interpreter should be used to attempt to get a static result
668    bool                                    m_interpret_success;        ///< True if the interpreter successfully handled the whole expression
669    std::string                             m_func_name;                ///< The name of the function to translate
670    lldb_private::ConstString               m_result_name;              ///< The name of the result variable ($0, $1, ...)
671    lldb_private::TypeFromParser            m_result_type;              ///< The type of the result variable.
672    llvm::Module                           *m_module;                   ///< The module being processed, or NULL if that has not been determined yet.
673    std::auto_ptr<llvm::DataLayout>         m_target_data;              ///< The target data for the module being processed, or NULL if there is no module.
674    lldb_private::ClangExpressionDeclMap   *m_decl_map;                 ///< The DeclMap containing the Decls
675    StaticDataAllocator                     m_data_allocator;           ///< The allocator to use for constant strings
676    lldb_private::IRMemoryMap              &m_memory_map;               ///< The memory map to pass to the IR interpreter
677    llvm::Constant                         *m_CFStringCreateWithBytes;  ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
678    llvm::Constant                         *m_sel_registerName;         ///< The address of the function sel_registerName, cast to the appropriate function pointer type
679    lldb::ClangExpressionVariableSP        &m_const_result;             ///< This value should be set to the return value of the expression if it is constant and the expression has no side effects
680    lldb_private::Stream                   *m_error_stream;             ///< If non-NULL, the stream on which errors should be printed
681    lldb_private::Error                     m_interpreter_error;        ///< The error result from the IR interpreter
682
683    bool                                    m_has_side_effects;         ///< True if the function's result cannot be simply determined statically
684    llvm::StoreInst                        *m_result_store;             ///< If non-NULL, the store instruction that writes to the result variable.  If m_has_side_effects is true, this is NULL.
685    bool                                    m_result_is_pointer;        ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
686
687    llvm::GlobalVariable                   *m_reloc_placeholder;        ///< A placeholder that will be replaced by a pointer to the final location of the static allocation.
688
689    //------------------------------------------------------------------
690    /// UnfoldConstant operates on a constant [Old] which has just been
691    /// replaced with a value [New].  We assume that new_value has
692    /// been properly placed early in the function, in front of the
693    /// first instruction in the entry basic block
694    /// [FirstEntryInstruction].
695    ///
696    /// UnfoldConstant reads through the uses of Old and replaces Old
697    /// in those uses with New.  Where those uses are constants, the
698    /// function generates new instructions to compute the result of the
699    /// new, non-constant expression and places them before
700    /// FirstEntryInstruction.  These instructions replace the constant
701    /// uses, so UnfoldConstant calls itself recursively for those.
702    ///
703    /// @param[in] llvm_function
704    ///     The function currently being processed.
705    ///
706    /// @return
707    ///     True on success; false otherwise
708    //------------------------------------------------------------------
709    static bool
710    UnfoldConstant (llvm::Constant *old_constant,
711                    llvm::Value *new_constant,
712                    llvm::Instruction *first_entry_inst);
713
714    //------------------------------------------------------------------
715    /// Construct a reference to m_reloc_placeholder with a given type
716    /// and offset.  This typically happens after inserting data into
717    /// m_data_allocator.
718    ///
719    /// @param[in] type
720    ///     The type of the value being loaded.
721    ///
722    /// @param[in] offset
723    ///     The offset of the value from the base of m_data_allocator.
724    ///
725    /// @return
726    ///     The Constant for the reference, usually a ConstantExpr.
727    //------------------------------------------------------------------
728    llvm::Constant *
729    BuildRelocation(llvm::Type *type,
730                    uint64_t offset);
731
732    //------------------------------------------------------------------
733    /// Commit the allocation in m_data_allocator and use its final
734    /// location to replace m_reloc_placeholder.
735    ///
736    /// @param[in] module
737    ///     The module that m_data_allocator resides in
738    ///
739    /// @return
740    ///     True on success; false otherwise
741    //------------------------------------------------------------------
742    bool
743    CompleteDataAllocation ();
744
745};
746
747#endif
748