IRForTarget.h revision 97c924e1ff142ea29a2f7b41cbcdcb0082b76959
1//===-- IRForTarget.h ---------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_IRForTarget_h_
11#define liblldb_IRForTarget_h_
12
13#include "lldb/lldb-include.h"
14#include "lldb/Core/ConstString.h"
15#include "lldb/Core/Stream.h"
16#include "lldb/Symbol/TaggedASTType.h"
17#include "llvm/Pass.h"
18
19namespace llvm {
20    class BasicBlock;
21    class CallInst;
22    class Constant;
23    class Function;
24    class GlobalVariable;
25    class Instruction;
26    class Module;
27    class Value;
28}
29
30namespace lldb_private {
31    class ClangExpressionDeclMap;
32}
33
34//----------------------------------------------------------------------
35/// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
36/// @brief Transforms the IR for a function to run in the target
37///
38/// Once an expression has been parsed and converted to IR, it can run
39/// in two contexts: interpreted by LLDB as a DWARF location expression,
40/// or compiled by the JIT and inserted into the target process for
41/// execution.
42///
43/// IRForTarget makes the second possible, by applying a series of
44/// transformations to the IR which make it relocatable.  These
45/// transformations are discussed in more detail next to their relevant
46/// functions.
47//----------------------------------------------------------------------
48class IRForTarget : public llvm::ModulePass
49{
50public:
51    //------------------------------------------------------------------
52    /// Constructor
53    ///
54    /// @param[in] decl_map
55    ///     The list of externally-referenced variables for the expression,
56    ///     for use in looking up globals and allocating the argument
57    ///     struct.  See the documentation for ClangExpressionDeclMap.
58    ///
59    /// @param[in] resolve_vars
60    ///     True if the external variable references (including persistent
61    ///     variables) should be resolved.  If not, only external functions
62    ///     are resolved.
63    ///
64    /// @param[in] const_result
65    ///     If non-NULL, a shared pointer to a ClangExpressionVariable that
66    ///     is populated with the statically-computed result of the function,
67    ///     if it has no side-effects and the result can be computed
68    ///     statically.
69    ///
70    /// @param[in] error_stream
71    ///     If non-NULL, a stream on which errors can be printed.
72    ///
73    /// @param[in] func_name
74    ///     The name of the function to prepare for execution in the target.
75    //------------------------------------------------------------------
76    IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
77                bool resolve_vars,
78                lldb::ClangExpressionVariableSP *const_result,
79                lldb_private::Stream *error_stream,
80                const char* func_name = "$__lldb_expr");
81
82    //------------------------------------------------------------------
83    /// Destructor
84    //------------------------------------------------------------------
85    ~IRForTarget();
86
87    //------------------------------------------------------------------
88    /// Run this IR transformer on a single module
89    ///
90    /// Implementation of the llvm::ModulePass::runOnModule() function.
91    ///
92    /// @param[in] llvm_module
93    ///     The module to run on.  This module is searched for the function
94    ///     $__lldb_expr, and that function is passed to the passes one by
95    ///     one.
96    ///
97    /// @return
98    ///     True on success; false otherwise
99    //------------------------------------------------------------------
100    virtual bool
101    runOnModule (llvm::Module &llvm_module);
102
103    //------------------------------------------------------------------
104    /// Interface stub
105    ///
106    /// Implementation of the llvm::ModulePass::assignPassManager()
107    /// function.
108    //------------------------------------------------------------------
109    virtual void
110    assignPassManager (llvm::PMStack &pass_mgr_stack,
111                       llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager);
112
113    //------------------------------------------------------------------
114    /// Returns PMT_ModulePassManager
115    ///
116    /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
117    /// function.
118    //------------------------------------------------------------------
119    virtual llvm::PassManagerType
120    getPotentialPassManagerType() const;
121
122private:
123    //------------------------------------------------------------------
124    /// A function-level pass to check whether the function has side
125    /// effects.
126    //------------------------------------------------------------------
127
128    //------------------------------------------------------------------
129    /// The top-level pass implementation
130    ///
131    /// @param[in] llvm_module
132    ///     The module currently being processed.
133    ///
134    /// @param[in] llvm_function
135    ///     The function currently being processed.
136    ///
137    /// @return
138    ///     True if the function has side effects (or if this cannot
139    ///     be determined); false otherwise.
140    //------------------------------------------------------------------
141    bool
142    HasSideEffects (llvm::Module &llvm_module,
143                    llvm::Function &llvm_function);
144
145    //------------------------------------------------------------------
146    /// A function-level pass to take the generated global value
147    /// $__lldb_expr_result and make it into a persistent variable.
148    /// Also see ASTResultSynthesizer.
149    //------------------------------------------------------------------
150
151    //------------------------------------------------------------------
152    /// Set the constant result variable m_const_result to the provided
153    /// constant, assuming it can be evaluated.  The result variable
154    /// will be reset to NULL later if the expression has side effects.
155    ///
156    /// @param[in] initializer
157    ///     The constant initializer for the variable.
158    ///
159    /// @param[in] name
160    ///     The name of the result variable.
161    ///
162    /// @param[in] type
163    ///     The Clang type of the result variable.
164    ///
165    /// @return
166    ///     True on success; false otherwise
167    //------------------------------------------------------------------
168    void
169    MaybeSetConstantResult (llvm::Constant *initializer,
170                            const lldb_private::ConstString &name,
171                            lldb_private::TypeFromParser type);
172
173    //------------------------------------------------------------------
174    /// The top-level pass implementation
175    ///
176    /// @param[in] llvm_module
177    ///     The module currently being processed.
178    ///
179    /// @param[in] llvm_function
180    ///     The function currently being processed.
181    ///
182    /// @return
183    ///     True on success; false otherwise
184    //------------------------------------------------------------------
185    bool
186    CreateResultVariable (llvm::Module &llvm_module,
187                          llvm::Function &llvm_function);
188
189    //------------------------------------------------------------------
190    /// A function-level pass to find Objective-C constant strings and
191    /// transform them to calls to CFStringCreateWithBytes.
192    //------------------------------------------------------------------
193
194    //------------------------------------------------------------------
195    /// Rewrite a single Objective-C constant string.
196    ///
197    /// @param[in] llvm_module
198    ///     The module currently being processed.
199    ///
200    /// @param[in] NSStr
201    ///     The constant NSString to be transformed
202    ///
203    /// @param[in] CStr
204    ///     The constant C string inside the NSString.  This will be
205    ///     passed as the bytes argument to CFStringCreateWithBytes.
206    ///
207    /// @param[in] FirstEntryInstruction
208    ///     An instruction early in the execution of the function.
209    ///     When this function synthesizes a call to
210    ///     CFStringCreateWithBytes, it places the call before this
211    ///     instruction.  The instruction should come before all
212    ///     uses of the NSString.
213    ///
214    /// @return
215    ///     True on success; false otherwise
216    //------------------------------------------------------------------
217    bool
218    RewriteObjCConstString (llvm::Module &llvm_module,
219                            llvm::GlobalVariable *NSStr,
220                            llvm::GlobalVariable *CStr,
221                            llvm::Instruction *FirstEntryInstruction);
222
223    //------------------------------------------------------------------
224    /// The top-level pass implementation
225    ///
226    /// @param[in] llvm_module
227    ///     The module currently being processed.
228    ///
229    /// @param[in] llvm_function
230    ///     The function currently being processed.
231    ///
232    /// @return
233    ///     True on success; false otherwise
234    //------------------------------------------------------------------
235    bool
236    RewriteObjCConstStrings (llvm::Module &llvm_module,
237                             llvm::Function &llvm_function);
238
239    //------------------------------------------------------------------
240    /// A basic block-level pass to find all Objective-C method calls and
241    /// rewrite them to use sel_registerName instead of statically allocated
242    /// selectors.  The reason is that the selectors are created on the
243    /// assumption that the Objective-C runtime will scan the appropriate
244    /// section and prepare them.  This doesn't happen when code is copied
245    /// into the target, though, and there's no easy way to induce the
246    /// runtime to scan them.  So instead we get our selectors from
247    /// sel_registerName.
248    //------------------------------------------------------------------
249
250    //------------------------------------------------------------------
251    /// Replace a single selector reference
252    ///
253    /// @param[in] selector_load
254    ///     The load of the statically-allocated selector.
255    ///
256    /// @param[in] llvm_module
257    ///     The module containing the load.
258    ///
259    /// @return
260    ///     True on success; false otherwise
261    //------------------------------------------------------------------
262    bool
263    RewriteObjCSelector (llvm::Instruction* selector_load,
264                         llvm::Module &llvm_module);
265
266    //------------------------------------------------------------------
267    /// The top-level pass implementation
268    ///
269    /// @param[in] llvm_module
270    ///     The module currently being processed.
271    ///
272    /// @param[in] basic_block
273    ///     The basic block currently being processed.
274    ///
275    /// @return
276    ///     True on success; false otherwise
277    //------------------------------------------------------------------
278    bool
279    RewriteObjCSelectors (llvm::Module &llvm_module,
280                          llvm::BasicBlock &basic_block);
281
282    //------------------------------------------------------------------
283    /// A basic block-level pass to find all newly-declared persistent
284    /// variables and register them with the ClangExprDeclMap.  This
285    /// allows them to be materialized and dematerialized like normal
286    /// external variables.  Before transformation, these persistent
287    /// variables look like normal locals, so they have an allocation.
288    /// This pass excises these allocations and makes references look
289    /// like external references where they will be resolved -- like all
290    /// other external references -- by ResolveExternals().
291    //------------------------------------------------------------------
292
293    //------------------------------------------------------------------
294    /// Handle a single allocation of a persistent variable
295    ///
296    /// @param[in] persistent_alloc
297    ///     The allocation of the persistent variable.
298    ///
299    /// @param[in] llvm_module
300    ///     The module currently being processed.
301    ///
302    /// @return
303    ///     True on success; false otherwise
304    //------------------------------------------------------------------
305    bool
306    RewritePersistentAlloc (llvm::Instruction *persistent_alloc,
307                            llvm::Module &llvm_module);
308
309    //------------------------------------------------------------------
310    /// The top-level pass implementation
311    ///
312    /// @param[in] llvm_module
313    ///     The module currently being processed.
314    ///
315    /// @param[in] basic_block
316    ///     The basic block currently being processed.
317    //------------------------------------------------------------------
318    bool
319    RewritePersistentAllocs (llvm::Module &llvm_module,
320                             llvm::BasicBlock &basic_block);
321
322    //------------------------------------------------------------------
323    /// A function-level pass to find all external variables and functions
324    /// used in the IR.  Each found external variable is added to the
325    /// struct, and each external function is resolved in place, its call
326    /// replaced with a call to a function pointer whose value is the
327    /// address of the function in the target process.
328    //------------------------------------------------------------------
329
330    //------------------------------------------------------------------
331    /// Handle a single externally-defined variable
332    ///
333    /// @param[in] llvm_module
334    ///     The module currently being processed.
335    ///
336    /// @param[in] value
337    ///     The variable.
338    ///
339    /// @return
340    ///     True on success; false otherwise
341    //------------------------------------------------------------------
342    bool
343    MaybeHandleVariable (llvm::Module &llvm_module,
344                         llvm::Value *value);
345
346    //------------------------------------------------------------------
347    /// Handle a single externally-defined symbol
348    ///
349    /// @param[in] llvm_module
350    ///     The module currently being processed.
351    ///
352    /// @param[in] symbol
353    ///     The symbol.
354    ///
355    /// @return
356    ///     True on success; false otherwise
357    //------------------------------------------------------------------
358    bool
359    HandleSymbol (llvm::Module &llvm_module,
360                  llvm::Value *symbol);
361
362    //------------------------------------------------------------------
363    /// Handle all the arguments to a function call
364    ///
365    /// @param[in] llvm_module
366    ///     The module currently being processed.
367    ///
368    /// @param[in] C
369    ///     The call instruction.
370    ///
371    /// @return
372    ///     True on success; false otherwise
373    //------------------------------------------------------------------
374    bool
375    MaybeHandleCallArguments (llvm::Module &llvm_module,
376                              llvm::CallInst *call_inst);
377
378    //------------------------------------------------------------------
379    /// Handle a single external function call
380    ///
381    /// @param[in] llvm_module
382    ///     The module currently being processed.
383    ///
384    /// @param[in] C
385    ///     The call instruction.
386    ///
387    /// @return
388    ///     True on success; false otherwise
389    //------------------------------------------------------------------
390    bool
391    MaybeHandleCall (llvm::Module &llvm_module,
392                     llvm::CallInst *C);
393
394    //------------------------------------------------------------------
395    /// Resolve calls to external functions
396    ///
397    /// @param[in] llvm_module
398    ///     The module currently being processed.
399    ///
400    /// @param[in] basic_block
401    ///     The basic block currently being processed.
402    ///
403    /// @return
404    ///     True on success; false otherwise
405    //------------------------------------------------------------------
406    bool
407    ResolveCalls (llvm::Module &llvm_module,
408                  llvm::BasicBlock &basic_block);
409
410    //------------------------------------------------------------------
411    /// The top-level pass implementation
412    ///
413    /// @param[in] llvm_module
414    ///     The module currently being processed.
415    ///
416    /// @param[in] basic_block
417    ///     The function currently being processed.
418    ///
419    /// @return
420    ///     True on success; false otherwise
421    //------------------------------------------------------------------
422    bool
423    ResolveExternals (llvm::Module &llvm_module,
424                      llvm::Function &llvm_function);
425
426    //------------------------------------------------------------------
427    /// A basic block-level pass to excise guard variables from the code.
428    /// The result for the function is passed through Clang as a static
429    /// variable.  Static variables normally have guard variables to
430    /// ensure that they are only initialized once.
431    //------------------------------------------------------------------
432
433    //------------------------------------------------------------------
434    /// The top-level pass implementation
435    ///
436    /// @param[in] llvm_module
437    ///     The module currently being processed.
438    ///
439    /// @param[in] basic_block
440    ///     The basic block currently being processed.
441    ///
442    /// @return
443    ///     True on success; false otherwise
444    //------------------------------------------------------------------
445    bool
446    RemoveGuards (llvm::Module &llvm_module,
447                  llvm::BasicBlock &basic_block);
448
449    //------------------------------------------------------------------
450    /// A function-level pass to make all external variable references
451    /// point at the correct offsets from the void* passed into the
452    /// function.  ClangExpressionDeclMap::DoStructLayout() must be called
453    /// beforehand, so that the offsets are valid.
454    //------------------------------------------------------------------
455
456    //------------------------------------------------------------------
457    /// The top-level pass implementation
458    ///
459    /// @param[in] llvm_module
460    ///     The module currently being processed.
461    ///
462    /// @param[in] llvm_function
463    ///     The function currently being processed.
464    ///
465    /// @return
466    ///     True on success; false otherwise
467    //------------------------------------------------------------------
468    bool
469    ReplaceVariables (llvm::Module &llvm_module,
470                      llvm::Function &llvm_function);
471
472    /// Flags
473    bool                                    m_resolve_vars;             ///< True if external variable references and persistent variable references should be resolved
474    std::string                             m_func_name;                ///< The name of the function to translate
475    lldb_private::ConstString               m_result_name;              ///< The name of the result variable ($0, $1, ...)
476    lldb_private::ClangExpressionDeclMap   *m_decl_map;                 ///< The DeclMap containing the Decls
477    llvm::Constant                         *m_CFStringCreateWithBytes;  ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type
478    llvm::Constant                         *m_sel_registerName;         ///< The address of the function sel_registerName, cast to the appropriate function pointer type
479    lldb::ClangExpressionVariableSP        *m_const_result;             ///< If non-NULL, this value should be set to the return value of the expression if it is constant and the expression has no side effects
480    lldb_private::Stream                   *m_error_stream;             ///< If non-NULL, the stream on which errors should be printed
481
482    bool                                    m_has_side_effects;         ///< True if the function's result cannot be simply determined statically
483    bool                                    m_result_is_pointer;        ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult)
484
485private:
486    //------------------------------------------------------------------
487    /// UnfoldConstant operates on a constant [Old] which has just been
488    /// replaced with a value [New].  We assume that new_value has
489    /// been properly placed early in the function, in front of the
490    /// first instruction in the entry basic block
491    /// [FirstEntryInstruction].
492    ///
493    /// UnfoldConstant reads through the uses of Old and replaces Old
494    /// in those uses with New.  Where those uses are constants, the
495    /// function generates new instructions to compute the result of the
496    /// new, non-constant expression and places them before
497    /// FirstEntryInstruction.  These instructions replace the constant
498    /// uses, so UnfoldConstant calls itself recursively for those.
499    ///
500    /// @param[in] llvm_module
501    ///     The module currently being processed.
502    ///
503    /// @param[in] llvm_function
504    ///     The function currently being processed.
505    ///
506    /// @return
507    ///     True on success; false otherwise
508    //------------------------------------------------------------------
509    static bool
510    UnfoldConstant (llvm::Constant *old_constant,
511                    llvm::Value *new_constant,
512                    llvm::Instruction *first_entry_inst);
513};
514
515#endif
516