]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/ExpressionParser/Clang/IRForTarget.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / ExpressionParser / Clang / IRForTarget.h
1 //===-- IRForTarget.h ---------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef liblldb_IRForTarget_h_
12 #define liblldb_IRForTarget_h_
13
14 #include "lldb/Core/ConstString.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/StreamString.h"
18 #include "lldb/Symbol/TaggedASTType.h"
19 #include "lldb/lldb-public.h"
20 #include "llvm/Pass.h"
21
22 #include <functional>
23 #include <map>
24
25 namespace llvm {
26 class BasicBlock;
27 class CallInst;
28 class Constant;
29 class ConstantInt;
30 class Function;
31 class GlobalValue;
32 class GlobalVariable;
33 class Instruction;
34 class IntegerType;
35 class Module;
36 class StoreInst;
37 class DataLayout;
38 class Type;
39 class Value;
40 }
41
42 namespace lldb_private {
43 class ClangExpressionDeclMap;
44 class IRExecutionUnit;
45 class IRMemoryMap;
46 }
47
48 //----------------------------------------------------------------------
49 /// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h"
50 /// @brief Transforms the IR for a function to run in the target
51 ///
52 /// Once an expression has been parsed and converted to IR, it can run
53 /// in two contexts: interpreted by LLDB as a DWARF location expression,
54 /// or compiled by the JIT and inserted into the target process for
55 /// execution.
56 ///
57 /// IRForTarget makes the second possible, by applying a series of
58 /// transformations to the IR which make it relocatable.  These
59 /// transformations are discussed in more detail next to their relevant
60 /// functions.
61 //----------------------------------------------------------------------
62 class IRForTarget : public llvm::ModulePass {
63 public:
64   enum class LookupResult { Success, Fail, Ignore };
65
66   //------------------------------------------------------------------
67   /// Constructor
68   ///
69   /// @param[in] decl_map
70   ///     The list of externally-referenced variables for the expression,
71   ///     for use in looking up globals and allocating the argument
72   ///     struct.  See the documentation for ClangExpressionDeclMap.
73   ///
74   /// @param[in] resolve_vars
75   ///     True if the external variable references (including persistent
76   ///     variables) should be resolved.  If not, only external functions
77   ///     are resolved.
78   ///
79   /// @param[in] execution_policy
80   ///     Determines whether an IR interpreter can be used to statically
81   ///     evaluate the expression.
82   ///
83   /// @param[in] const_result
84   ///     This variable is populated with the statically-computed result
85   ///     of the function, if it has no side-effects and the result can
86   ///     be computed statically.
87   ///
88   /// @param[in] execution_unit
89   ///     The holder for raw data associated with the expression.
90   ///
91   /// @param[in] error_stream
92   ///     If non-NULL, a stream on which errors can be printed.
93   ///
94   /// @param[in] func_name
95   ///     The name of the function to prepare for execution in the target.
96   //------------------------------------------------------------------
97   IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars,
98               lldb_private::IRExecutionUnit &execution_unit,
99               lldb_private::Stream &error_stream,
100               const char *func_name = "$__lldb_expr");
101
102   //------------------------------------------------------------------
103   /// Destructor
104   //------------------------------------------------------------------
105   ~IRForTarget() override;
106
107   //------------------------------------------------------------------
108   /// Run this IR transformer on a single module
109   ///
110   /// Implementation of the llvm::ModulePass::runOnModule() function.
111   ///
112   /// @param[in] llvm_module
113   ///     The module to run on.  This module is searched for the function
114   ///     $__lldb_expr, and that function is passed to the passes one by
115   ///     one.
116   ///
117   /// @param[in] interpreter_error
118   ///     An error.  If the expression fails to be interpreted, this error
119   ///     is set to a reason why.
120   ///
121   /// @return
122   ///     True on success; false otherwise
123   //------------------------------------------------------------------
124   bool runOnModule(llvm::Module &llvm_module) override;
125
126   //------------------------------------------------------------------
127   /// Interface stub
128   ///
129   /// Implementation of the llvm::ModulePass::assignPassManager()
130   /// function.
131   //------------------------------------------------------------------
132   void assignPassManager(llvm::PMStack &pass_mgr_stack,
133                          llvm::PassManagerType pass_mgr_type =
134                              llvm::PMT_ModulePassManager) override;
135
136   //------------------------------------------------------------------
137   /// Returns PMT_ModulePassManager
138   ///
139   /// Implementation of the llvm::ModulePass::getPotentialPassManagerType()
140   /// function.
141   //------------------------------------------------------------------
142   llvm::PassManagerType getPotentialPassManagerType() const override;
143
144 private:
145   //------------------------------------------------------------------
146   /// Ensures that the current function's linkage is set to external.
147   /// Otherwise the JIT may not return an address for it.
148   ///
149   /// @param[in] llvm_function
150   ///     The function whose linkage is to be fixed.
151   ///
152   /// @return
153   ///     True on success; false otherwise.
154   //------------------------------------------------------------------
155   bool FixFunctionLinkage(llvm::Function &llvm_function);
156
157   //------------------------------------------------------------------
158   /// A module-level pass to replace all function pointers with their
159   /// integer equivalents.
160   //------------------------------------------------------------------
161
162   //------------------------------------------------------------------
163   /// The top-level pass implementation
164   ///
165   /// @param[in] llvm_module
166   ///     The module currently being processed.
167   ///
168   /// @param[in] llvm_function
169   ///     The function currently being processed.
170   ///
171   /// @return
172   ///     True on success; false otherwise.
173   //------------------------------------------------------------------
174   bool HasSideEffects(llvm::Function &llvm_function);
175
176   //------------------------------------------------------------------
177   /// A function-level pass to check whether the function has side
178   /// effects.
179   //------------------------------------------------------------------
180
181   //------------------------------------------------------------------
182   /// Get the address of a function, and a location to put the complete
183   /// Value of the function if one is available.
184   ///
185   /// @param[in] function
186   ///     The function to find the location of.
187   ///
188   /// @param[out] ptr
189   ///     The location of the function in the target.
190   ///
191   /// @param[out] name
192   ///     The resolved name of the function (matters for intrinsics).
193   ///
194   /// @param[out] value_ptr
195   ///     A variable to put the function's completed Value* in, or NULL
196   ///     if the Value* shouldn't be stored anywhere.
197   ///
198   /// @return
199   ///     The pointer.
200   //------------------------------------------------------------------
201   LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr,
202                                   lldb_private::ConstString &name,
203                                   llvm::Constant **&value_ptr);
204
205   //------------------------------------------------------------------
206   /// A function-level pass to take the generated global value
207   /// $__lldb_expr_result and make it into a persistent variable.
208   /// Also see ASTResultSynthesizer.
209   //------------------------------------------------------------------
210
211   //------------------------------------------------------------------
212   /// Find the NamedDecl corresponding to a Value.  This interface is
213   /// exposed for the IR interpreter.
214   ///
215   /// @param[in] module
216   ///     The module containing metadata to search
217   ///
218   /// @param[in] global
219   ///     The global entity to search for
220   ///
221   /// @return
222   ///     The corresponding variable declaration
223   //------------------------------------------------------------------
224 public:
225   static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val,
226                                          llvm::Module *module);
227
228 private:
229   clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global);
230
231   //------------------------------------------------------------------
232   /// Set the constant result variable m_const_result to the provided
233   /// constant, assuming it can be evaluated.  The result variable
234   /// will be reset to NULL later if the expression has side effects.
235   ///
236   /// @param[in] initializer
237   ///     The constant initializer for the variable.
238   ///
239   /// @param[in] name
240   ///     The name of the result variable.
241   ///
242   /// @param[in] type
243   ///     The Clang type of the result variable.
244   //------------------------------------------------------------------
245   void MaybeSetConstantResult(llvm::Constant *initializer,
246                               const lldb_private::ConstString &name,
247                               lldb_private::TypeFromParser type);
248
249   //------------------------------------------------------------------
250   /// If the IR represents a cast of a variable, set m_const_result
251   /// to the result of the cast.  The result variable will be reset to
252   /// NULL latger if the expression has side effects.
253   ///
254   /// @param[in] type
255   ///     The Clang type of the result variable.
256   //------------------------------------------------------------------
257   void MaybeSetCastResult(lldb_private::TypeFromParser type);
258
259   //------------------------------------------------------------------
260   /// The top-level pass implementation
261   ///
262   /// @param[in] llvm_function
263   ///     The function currently being processed.
264   ///
265   /// @return
266   ///     True on success; false otherwise
267   //------------------------------------------------------------------
268   bool CreateResultVariable(llvm::Function &llvm_function);
269
270   //------------------------------------------------------------------
271   /// A module-level pass to find Objective-C constant strings and
272   /// transform them to calls to CFStringCreateWithBytes.
273   //------------------------------------------------------------------
274
275   //------------------------------------------------------------------
276   /// Rewrite a single Objective-C constant string.
277   ///
278   /// @param[in] NSStr
279   ///     The constant NSString to be transformed
280   ///
281   /// @param[in] CStr
282   ///     The constant C string inside the NSString.  This will be
283   ///     passed as the bytes argument to CFStringCreateWithBytes.
284   ///
285   /// @return
286   ///     True on success; false otherwise
287   //------------------------------------------------------------------
288   bool RewriteObjCConstString(llvm::GlobalVariable *NSStr,
289                               llvm::GlobalVariable *CStr);
290
291   //------------------------------------------------------------------
292   /// The top-level pass implementation
293   ///
294   /// @return
295   ///     True on success; false otherwise
296   //------------------------------------------------------------------
297   bool RewriteObjCConstStrings();
298
299   //------------------------------------------------------------------
300   /// A basic block-level pass to find all Objective-C method calls and
301   /// rewrite them to use sel_registerName instead of statically allocated
302   /// selectors.  The reason is that the selectors are created on the
303   /// assumption that the Objective-C runtime will scan the appropriate
304   /// section and prepare them.  This doesn't happen when code is copied
305   /// into the target, though, and there's no easy way to induce the
306   /// runtime to scan them.  So instead we get our selectors from
307   /// sel_registerName.
308   //------------------------------------------------------------------
309
310   //------------------------------------------------------------------
311   /// Replace a single selector reference
312   ///
313   /// @param[in] selector_load
314   ///     The load of the statically-allocated selector.
315   ///
316   /// @return
317   ///     True on success; false otherwise
318   //------------------------------------------------------------------
319   bool RewriteObjCSelector(llvm::Instruction *selector_load);
320
321   //------------------------------------------------------------------
322   /// The top-level pass implementation
323   ///
324   /// @param[in] basic_block
325   ///     The basic block currently being processed.
326   ///
327   /// @return
328   ///     True on success; false otherwise
329   //------------------------------------------------------------------
330   bool RewriteObjCSelectors(llvm::BasicBlock &basic_block);
331
332   //------------------------------------------------------------------
333   /// A basic block-level pass to find all Objective-C class references that
334   /// use the old-style Objective-C runtime and rewrite them to use
335   /// class_getClass instead of statically allocated class references.
336   //------------------------------------------------------------------
337
338   //------------------------------------------------------------------
339   /// Replace a single old-style class reference
340   ///
341   /// @param[in] selector_load
342   ///     The load of the statically-allocated selector.
343   ///
344   /// @return
345   ///     True on success; false otherwise
346   //------------------------------------------------------------------
347   bool RewriteObjCClassReference(llvm::Instruction *class_load);
348
349   //------------------------------------------------------------------
350   /// The top-level pass implementation
351   ///
352   /// @param[in] basic_block
353   ///     The basic block currently being processed.
354   ///
355   /// @return
356   ///     True on success; false otherwise
357   //------------------------------------------------------------------
358   bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block);
359
360   //------------------------------------------------------------------
361   /// A basic block-level pass to find all newly-declared persistent
362   /// variables and register them with the ClangExprDeclMap.  This
363   /// allows them to be materialized and dematerialized like normal
364   /// external variables.  Before transformation, these persistent
365   /// variables look like normal locals, so they have an allocation.
366   /// This pass excises these allocations and makes references look
367   /// like external references where they will be resolved -- like all
368   /// other external references -- by ResolveExternals().
369   //------------------------------------------------------------------
370
371   //------------------------------------------------------------------
372   /// Handle a single allocation of a persistent variable
373   ///
374   /// @param[in] persistent_alloc
375   ///     The allocation of the persistent variable.
376   ///
377   /// @return
378   ///     True on success; false otherwise
379   //------------------------------------------------------------------
380   bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc);
381
382   //------------------------------------------------------------------
383   /// The top-level pass implementation
384   ///
385   /// @param[in] basic_block
386   ///     The basic block currently being processed.
387   //------------------------------------------------------------------
388   bool RewritePersistentAllocs(llvm::BasicBlock &basic_block);
389
390   //------------------------------------------------------------------
391   /// A function-level pass to find all external variables and functions
392   /// used in the IR.  Each found external variable is added to the
393   /// struct, and each external function is resolved in place, its call
394   /// replaced with a call to a function pointer whose value is the
395   /// address of the function in the target process.
396   //------------------------------------------------------------------
397
398   //------------------------------------------------------------------
399   /// Write an initializer to a memory array of assumed sufficient
400   /// size.
401   ///
402   /// @param[in] data
403   ///     A pointer to the data to write to.
404   ///
405   /// @param[in] initializer
406   ///     The initializer itself.
407   ///
408   /// @return
409   ///     True on success; false otherwise
410   //------------------------------------------------------------------
411   bool MaterializeInitializer(uint8_t *data, llvm::Constant *initializer);
412
413   //------------------------------------------------------------------
414   /// Move an internal variable into the static allocation section.
415   ///
416   /// @param[in] global_variable
417   ///     The variable.
418   ///
419   /// @return
420   ///     True on success; false otherwise
421   //------------------------------------------------------------------
422   bool MaterializeInternalVariable(llvm::GlobalVariable *global_variable);
423
424   //------------------------------------------------------------------
425   /// Handle a single externally-defined variable
426   ///
427   /// @param[in] value
428   ///     The variable.
429   ///
430   /// @return
431   ///     True on success; false otherwise
432   //------------------------------------------------------------------
433   bool MaybeHandleVariable(llvm::Value *value);
434
435   //------------------------------------------------------------------
436   /// Handle a single externally-defined symbol
437   ///
438   /// @param[in] symbol
439   ///     The symbol.
440   ///
441   /// @return
442   ///     True on success; false otherwise
443   //------------------------------------------------------------------
444   bool HandleSymbol(llvm::Value *symbol);
445
446   //------------------------------------------------------------------
447   /// Handle a single externally-defined Objective-C class
448   ///
449   /// @param[in] classlist_reference
450   ///     The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n"
451   ///     where n (if present) is an index.
452   ///
453   /// @return
454   ///     True on success; false otherwise
455   //------------------------------------------------------------------
456   bool HandleObjCClass(llvm::Value *classlist_reference);
457
458   //------------------------------------------------------------------
459   /// Handle all the arguments to a function call
460   ///
461   /// @param[in] C
462   ///     The call instruction.
463   ///
464   /// @return
465   ///     True on success; false otherwise
466   //------------------------------------------------------------------
467   bool MaybeHandleCallArguments(llvm::CallInst *call_inst);
468
469   //------------------------------------------------------------------
470   /// Resolve variable references in calls to external functions
471   ///
472   /// @param[in] basic_block
473   ///     The basic block currently being processed.
474   ///
475   /// @return
476   ///     True on success; false otherwise
477   //------------------------------------------------------------------
478   bool ResolveCalls(llvm::BasicBlock &basic_block);
479
480   //------------------------------------------------------------------
481   /// Remove calls to __cxa_atexit, which should never be generated by
482   /// expressions.
483   ///
484   /// @param[in] call_inst
485   ///     The call instruction.
486   ///
487   /// @return
488   ///     True if the scan was successful; false if some operation
489   ///     failed
490   //------------------------------------------------------------------
491   bool RemoveCXAAtExit(llvm::BasicBlock &basic_block);
492
493   //------------------------------------------------------------------
494   /// The top-level pass implementation
495   ///
496   /// @param[in] basic_block
497   ///     The function currently being processed.
498   ///
499   /// @return
500   ///     True on success; false otherwise
501   //------------------------------------------------------------------
502   bool ResolveExternals(llvm::Function &llvm_function);
503
504   //------------------------------------------------------------------
505   /// A basic block-level pass to excise guard variables from the code.
506   /// The result for the function is passed through Clang as a static
507   /// variable.  Static variables normally have guard variables to
508   /// ensure that they are only initialized once.
509   //------------------------------------------------------------------
510
511   //------------------------------------------------------------------
512   /// Rewrite a load to a guard variable to return constant 0.
513   ///
514   /// @param[in] guard_load
515   ///     The load instruction to zero out.
516   //------------------------------------------------------------------
517   void TurnGuardLoadIntoZero(llvm::Instruction *guard_load);
518
519   //------------------------------------------------------------------
520   /// The top-level pass implementation
521   ///
522   /// @param[in] basic_block
523   ///     The basic block currently being processed.
524   ///
525   /// @return
526   ///     True on success; false otherwise
527   //------------------------------------------------------------------
528   bool RemoveGuards(llvm::BasicBlock &basic_block);
529
530   //------------------------------------------------------------------
531   /// A function-level pass to make all external variable references
532   /// point at the correct offsets from the void* passed into the
533   /// function.  ClangExpressionDeclMap::DoStructLayout() must be called
534   /// beforehand, so that the offsets are valid.
535   //------------------------------------------------------------------
536
537   //------------------------------------------------------------------
538   /// The top-level pass implementation
539   ///
540   /// @param[in] llvm_function
541   ///     The function currently being processed.
542   ///
543   /// @return
544   ///     True on success; false otherwise
545   //------------------------------------------------------------------
546   bool ReplaceVariables(llvm::Function &llvm_function);
547
548   /// Flags
549   bool m_resolve_vars; ///< True if external variable references and persistent
550                        ///variable references should be resolved
551   lldb_private::ConstString
552       m_func_name; ///< The name of the function to translate
553   lldb_private::ConstString
554       m_result_name; ///< The name of the result variable ($0, $1, ...)
555   lldb_private::TypeFromParser
556       m_result_type;      ///< The type of the result variable.
557   llvm::Module *m_module; ///< The module being processed, or NULL if that has
558                           ///not been determined yet.
559   std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the
560                                                    ///module being processed, or
561                                                    ///NULL if there is no
562                                                    ///module.
563   lldb_private::ClangExpressionDeclMap
564       *m_decl_map; ///< The DeclMap containing the Decls
565   llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function
566                                              ///CFStringCreateWithBytes, cast to
567                                              ///the
568   /// appropriate function pointer type
569   llvm::Constant *m_sel_registerName; ///< The address of the function
570                                       ///sel_registerName, cast to the
571                                       ///appropriate
572                                       /// function pointer type
573   llvm::Constant *m_objc_getClass; ///< The address of the function
574                                    ///objc_getClass, cast to the
575                                    ///appropriate
576                                    /// function pointer type
577   llvm::IntegerType
578       *m_intptr_ty; ///< The type of an integer large enough to hold a pointer.
579   lldb_private::Stream
580       &m_error_stream; ///< The stream on which errors should be printed
581   lldb_private::IRExecutionUnit &
582       m_execution_unit; ///< The execution unit containing the IR being created.
583
584   llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that
585                                    ///writes to the result variable.  If
586                                    /// m_has_side_effects is true, this is NULL.
587   bool m_result_is_pointer; ///< True if the function's result in the AST is a
588                             ///pointer (see comments in
589                             /// ASTResultSynthesizer::SynthesizeBodyResult)
590
591   llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be
592                                              ///replaced by a pointer to the
593                                              ///final
594   /// location of the static allocation.
595
596   //------------------------------------------------------------------
597   /// UnfoldConstant operates on a constant [Old] which has just been
598   /// replaced with a value [New].  We assume that new_value has
599   /// been properly placed early in the function, in front of the
600   /// first instruction in the entry basic block
601   /// [FirstEntryInstruction].
602   ///
603   /// UnfoldConstant reads through the uses of Old and replaces Old
604   /// in those uses with New.  Where those uses are constants, the
605   /// function generates new instructions to compute the result of the
606   /// new, non-constant expression and places them before
607   /// FirstEntryInstruction.  These instructions replace the constant
608   /// uses, so UnfoldConstant calls itself recursively for those.
609   ///
610   /// @param[in] llvm_function
611   ///     The function currently being processed.
612   ///
613   /// @return
614   ///     True on success; false otherwise
615   //------------------------------------------------------------------
616
617   class FunctionValueCache {
618   public:
619     typedef std::function<llvm::Value *(llvm::Function *)> Maker;
620
621     FunctionValueCache(Maker const &maker);
622     ~FunctionValueCache();
623     llvm::Value *GetValue(llvm::Function *function);
624
625   private:
626     Maker const m_maker;
627     typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap;
628     FunctionValueMap m_values;
629   };
630
631   FunctionValueCache m_entry_instruction_finder;
632
633   static bool UnfoldConstant(llvm::Constant *old_constant,
634                              llvm::Function *llvm_function,
635                              FunctionValueCache &value_maker,
636                              FunctionValueCache &entry_instruction_finder,
637                              lldb_private::Stream &error_stream);
638
639   //------------------------------------------------------------------
640   /// Construct a reference to m_reloc_placeholder with a given type
641   /// and offset.  This typically happens after inserting data into
642   /// m_data_allocator.
643   ///
644   /// @param[in] type
645   ///     The type of the value being loaded.
646   ///
647   /// @param[in] offset
648   ///     The offset of the value from the base of m_data_allocator.
649   ///
650   /// @return
651   ///     The Constant for the reference, usually a ConstantExpr.
652   //------------------------------------------------------------------
653   llvm::Constant *BuildRelocation(llvm::Type *type, uint64_t offset);
654
655   //------------------------------------------------------------------
656   /// Commit the allocation in m_data_allocator and use its final
657   /// location to replace m_reloc_placeholder.
658   ///
659   /// @param[in] module
660   ///     The module that m_data_allocator resides in
661   ///
662   /// @return
663   ///     True on success; false otherwise
664   //------------------------------------------------------------------
665   bool CompleteDataAllocation();
666 };
667
668 #endif // liblldb_IRForTarget_h_