IRForTarget.h revision c7ca466fa99aea0e4e81530157bc8927682cae00
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-public.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 ConstantInt; 24 class Function; 25 class GlobalValue; 26 class GlobalVariable; 27 class Instruction; 28 class Module; 29 class StoreInst; 30 class TargetData; 31 class Type; 32 class Value; 33} 34 35namespace lldb_private { 36 class ClangExpressionDeclMap; 37} 38 39//---------------------------------------------------------------------- 40/// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h" 41/// @brief Transforms the IR for a function to run in the target 42/// 43/// Once an expression has been parsed and converted to IR, it can run 44/// in two contexts: interpreted by LLDB as a DWARF location expression, 45/// or compiled by the JIT and inserted into the target process for 46/// execution. 47/// 48/// IRForTarget makes the second possible, by applying a series of 49/// transformations to the IR which make it relocatable. These 50/// transformations are discussed in more detail next to their relevant 51/// functions. 52//---------------------------------------------------------------------- 53class IRForTarget : public llvm::ModulePass 54{ 55public: 56 class StaticDataAllocator { 57 public: 58 StaticDataAllocator(); 59 virtual ~StaticDataAllocator(); 60 virtual lldb_private::StreamString &GetStream() = 0; 61 virtual lldb::addr_t Allocate() = 0; 62 }; 63 64 //------------------------------------------------------------------ 65 /// Constructor 66 /// 67 /// @param[in] decl_map 68 /// The list of externally-referenced variables for the expression, 69 /// for use in looking up globals and allocating the argument 70 /// struct. See the documentation for ClangExpressionDeclMap. 71 /// 72 /// @param[in] resolve_vars 73 /// True if the external variable references (including persistent 74 /// variables) should be resolved. If not, only external functions 75 /// are resolved. 76 /// 77 /// @param[in] execution_policy 78 /// Determines whether an IR interpreter can be used to statically 79 /// evaluate the expression. 80 /// 81 /// @param[in] const_result 82 /// This variable is populated with the statically-computed result 83 /// of the function, if it has no side-effects and the result can 84 /// be computed statically. 85 /// 86 /// @param[in] data_allocator 87 /// If non-NULL, the static data allocator to use for literal strings. 88 /// 89 /// @param[in] error_stream 90 /// If non-NULL, a stream on which errors can be printed. 91 /// 92 /// @param[in] func_name 93 /// The name of the function to prepare for execution in the target. 94 //------------------------------------------------------------------ 95 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, 96 bool resolve_vars, 97 lldb_private::ExecutionPolicy execution_policy, 98 lldb::ClangExpressionVariableSP &const_result, 99 StaticDataAllocator *data_allocator, 100 lldb_private::Stream *error_stream, 101 const char* func_name = "$__lldb_expr"); 102 103 //------------------------------------------------------------------ 104 /// Destructor 105 //------------------------------------------------------------------ 106 virtual ~IRForTarget(); 107 108 //------------------------------------------------------------------ 109 /// Run this IR transformer on a single module 110 /// 111 /// Implementation of the llvm::ModulePass::runOnModule() function. 112 /// 113 /// @param[in] llvm_module 114 /// The module to run on. This module is searched for the function 115 /// $__lldb_expr, and that function is passed to the passes one by 116 /// one. 117 /// 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 bool 150 interpretSuccess () 151 { 152 return m_interpret_success; 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 //------------------------------------------------------------------ 237 /// The top-level pass implementation 238 /// 239 /// @param[in] llvm_function 240 /// The function currently being processed. 241 /// 242 /// @return 243 /// True if the function has side effects (or if this cannot 244 /// be determined); false otherwise. 245 //------------------------------------------------------------------ 246 bool 247 ResolveFunctionPointers (llvm::Module &llvm_module, 248 llvm::Function &llvm_function); 249 250 //------------------------------------------------------------------ 251 /// A function-level pass to take the generated global value 252 /// $__lldb_expr_result and make it into a persistent variable. 253 /// Also see ASTResultSynthesizer. 254 //------------------------------------------------------------------ 255 256 //------------------------------------------------------------------ 257 /// Find the NamedDecl corresponding to a Value. This interface is 258 /// exposed for the IR interpreter. 259 /// 260 /// @param[in] module 261 /// The module containing metadata to search 262 /// 263 /// @param[in] global 264 /// The global entity to search for 265 /// 266 /// @return 267 /// The corresponding variable declaration 268 //------------------------------------------------------------------ 269public: 270 static clang::NamedDecl * 271 DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module); 272private: 273 clang::NamedDecl * 274 DeclForGlobal (llvm::GlobalValue *global); 275 276 //------------------------------------------------------------------ 277 /// Set the constant result variable m_const_result to the provided 278 /// constant, assuming it can be evaluated. The result variable 279 /// will be reset to NULL later if the expression has side effects. 280 /// 281 /// @param[in] initializer 282 /// The constant initializer for the variable. 283 /// 284 /// @param[in] name 285 /// The name of the result variable. 286 /// 287 /// @param[in] type 288 /// The Clang type of the result variable. 289 //------------------------------------------------------------------ 290 void 291 MaybeSetConstantResult (llvm::Constant *initializer, 292 const lldb_private::ConstString &name, 293 lldb_private::TypeFromParser type); 294 295 //------------------------------------------------------------------ 296 /// If the IR represents a cast of a variable, set m_const_result 297 /// to the result of the cast. The result variable will be reset to 298 /// NULL latger if the expression has side effects. 299 /// 300 /// @param[in] type 301 /// The Clang type of the result variable. 302 //------------------------------------------------------------------ 303 void 304 MaybeSetCastResult (lldb_private::TypeFromParser type); 305 306 //------------------------------------------------------------------ 307 /// The top-level pass implementation 308 /// 309 /// @param[in] llvm_function 310 /// The function currently being processed. 311 /// 312 /// @return 313 /// True on success; false otherwise 314 //------------------------------------------------------------------ 315 bool 316 CreateResultVariable (llvm::Function &llvm_function); 317 318 //------------------------------------------------------------------ 319 /// A function-level pass to find Objective-C constant strings and 320 /// transform them to calls to CFStringCreateWithBytes. 321 //------------------------------------------------------------------ 322 323 //------------------------------------------------------------------ 324 /// Rewrite a single Objective-C constant string. 325 /// 326 /// @param[in] NSStr 327 /// The constant NSString to be transformed 328 /// 329 /// @param[in] CStr 330 /// The constant C string inside the NSString. This will be 331 /// passed as the bytes argument to CFStringCreateWithBytes. 332 /// 333 /// @param[in] FirstEntryInstruction 334 /// An instruction early in the execution of the function. 335 /// When this function synthesizes a call to 336 /// CFStringCreateWithBytes, it places the call before this 337 /// instruction. The instruction should come before all 338 /// uses of the NSString. 339 /// 340 /// @return 341 /// True on success; false otherwise 342 //------------------------------------------------------------------ 343 bool 344 RewriteObjCConstString (llvm::GlobalVariable *NSStr, 345 llvm::GlobalVariable *CStr, 346 llvm::Instruction *FirstEntryInstruction); 347 348 //------------------------------------------------------------------ 349 /// The top-level pass implementation 350 /// 351 /// @param[in] llvm_function 352 /// The function currently being processed. 353 /// 354 /// @return 355 /// True on success; false otherwise 356 //------------------------------------------------------------------ 357 bool 358 RewriteObjCConstStrings (llvm::Function &llvm_function); 359 360 //------------------------------------------------------------------ 361 /// A basic block-level pass to find all Objective-C method calls and 362 /// rewrite them to use sel_registerName instead of statically allocated 363 /// selectors. The reason is that the selectors are created on the 364 /// assumption that the Objective-C runtime will scan the appropriate 365 /// section and prepare them. This doesn't happen when code is copied 366 /// into the target, though, and there's no easy way to induce the 367 /// runtime to scan them. So instead we get our selectors from 368 /// sel_registerName. 369 //------------------------------------------------------------------ 370 371 //------------------------------------------------------------------ 372 /// Replace a single selector reference 373 /// 374 /// @param[in] selector_load 375 /// The load of the statically-allocated selector. 376 /// 377 /// @return 378 /// True on success; false otherwise 379 //------------------------------------------------------------------ 380 bool 381 RewriteObjCSelector (llvm::Instruction* selector_load); 382 383 //------------------------------------------------------------------ 384 /// The top-level pass implementation 385 /// 386 /// @param[in] basic_block 387 /// The basic block currently being processed. 388 /// 389 /// @return 390 /// True on success; false otherwise 391 //------------------------------------------------------------------ 392 bool 393 RewriteObjCSelectors (llvm::BasicBlock &basic_block); 394 395 //------------------------------------------------------------------ 396 /// A basic block-level pass to find all newly-declared persistent 397 /// variables and register them with the ClangExprDeclMap. This 398 /// allows them to be materialized and dematerialized like normal 399 /// external variables. Before transformation, these persistent 400 /// variables look like normal locals, so they have an allocation. 401 /// This pass excises these allocations and makes references look 402 /// like external references where they will be resolved -- like all 403 /// other external references -- by ResolveExternals(). 404 //------------------------------------------------------------------ 405 406 //------------------------------------------------------------------ 407 /// Handle a single allocation of a persistent variable 408 /// 409 /// @param[in] persistent_alloc 410 /// The allocation of the persistent variable. 411 /// 412 /// @return 413 /// True on success; false otherwise 414 //------------------------------------------------------------------ 415 bool 416 RewritePersistentAlloc (llvm::Instruction *persistent_alloc); 417 418 //------------------------------------------------------------------ 419 /// The top-level pass implementation 420 /// 421 /// @param[in] basic_block 422 /// The basic block currently being processed. 423 //------------------------------------------------------------------ 424 bool 425 RewritePersistentAllocs (llvm::BasicBlock &basic_block); 426 427 //------------------------------------------------------------------ 428 /// A function-level pass to find all external variables and functions 429 /// used in the IR. Each found external variable is added to the 430 /// struct, and each external function is resolved in place, its call 431 /// replaced with a call to a function pointer whose value is the 432 /// address of the function in the target process. 433 //------------------------------------------------------------------ 434 435 //------------------------------------------------------------------ 436 /// Write an initializer to a memory array of assumed sufficient 437 /// size. 438 /// 439 /// @param[in] data 440 /// A pointer to the data to write to. 441 /// 442 /// @param[in] initializer 443 /// The initializer itself. 444 /// 445 /// @return 446 /// True on success; false otherwise 447 //------------------------------------------------------------------ 448 bool 449 MaterializeInitializer (uint8_t *data, llvm::Constant *initializer); 450 451 //------------------------------------------------------------------ 452 /// Move an internal variable into the static allocation section. 453 /// 454 /// @param[in] global_variable 455 /// The variable. 456 /// 457 /// @return 458 /// True on success; false otherwise 459 //------------------------------------------------------------------ 460 bool 461 MaterializeInternalVariable (llvm::GlobalVariable *global_variable); 462 463 //------------------------------------------------------------------ 464 /// Handle a single externally-defined variable 465 /// 466 /// @param[in] value 467 /// The variable. 468 /// 469 /// @return 470 /// True on success; false otherwise 471 //------------------------------------------------------------------ 472 bool 473 MaybeHandleVariable (llvm::Value *value); 474 475 //------------------------------------------------------------------ 476 /// Handle a single externally-defined symbol 477 /// 478 /// @param[in] symbol 479 /// The symbol. 480 /// 481 /// @return 482 /// True on success; false otherwise 483 //------------------------------------------------------------------ 484 bool 485 HandleSymbol (llvm::Value *symbol); 486 487 //------------------------------------------------------------------ 488 /// Handle all the arguments to a function call 489 /// 490 /// @param[in] C 491 /// The call instruction. 492 /// 493 /// @return 494 /// True on success; false otherwise 495 //------------------------------------------------------------------ 496 bool 497 MaybeHandleCallArguments (llvm::CallInst *call_inst); 498 499 //------------------------------------------------------------------ 500 /// Resolve variable references in calls to external functions 501 /// 502 /// @param[in] basic_block 503 /// The basic block currently being processed. 504 /// 505 /// @return 506 /// True on success; false otherwise 507 //------------------------------------------------------------------ 508 bool 509 ResolveCalls (llvm::BasicBlock &basic_block); 510 511 //------------------------------------------------------------------ 512 /// The top-level pass implementation 513 /// 514 /// @param[in] basic_block 515 /// The function currently being processed. 516 /// 517 /// @return 518 /// True on success; false otherwise 519 //------------------------------------------------------------------ 520 bool 521 ResolveExternals (llvm::Function &llvm_function); 522 523 //------------------------------------------------------------------ 524 /// A basic block-level pass to excise guard variables from the code. 525 /// The result for the function is passed through Clang as a static 526 /// variable. Static variables normally have guard variables to 527 /// ensure that they are only initialized once. 528 //------------------------------------------------------------------ 529 530 //------------------------------------------------------------------ 531 /// Rewrite a load to a guard variable to return constant 0. 532 /// 533 /// @param[in] guard_load 534 /// The load instruction to zero out. 535 //------------------------------------------------------------------ 536 void 537 TurnGuardLoadIntoZero(llvm::Instruction* guard_load); 538 539 //------------------------------------------------------------------ 540 /// The top-level pass implementation 541 /// 542 /// @param[in] basic_block 543 /// The basic block currently being processed. 544 /// 545 /// @return 546 /// True on success; false otherwise 547 //------------------------------------------------------------------ 548 bool 549 RemoveGuards (llvm::BasicBlock &basic_block); 550 551 //------------------------------------------------------------------ 552 /// A module-level pass to allocate all string literals in a separate 553 /// allocation and redirect references to them. 554 //------------------------------------------------------------------ 555 556 //------------------------------------------------------------------ 557 /// The top-level pass implementation 558 /// 559 /// @return 560 /// True on success; false otherwise 561 //------------------------------------------------------------------ 562 bool 563 ReplaceStrings (); 564 565 //------------------------------------------------------------------ 566 /// A basick block-level pass to find all literals that will be 567 /// allocated as statics by the JIT (in contrast to the Strings, 568 /// which already are statics) and synthesize loads for them. 569 //------------------------------------------------------------------ 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 ReplaceStaticLiterals (llvm::BasicBlock &basic_block); 582 583 //------------------------------------------------------------------ 584 /// A function-level pass to make all external variable references 585 /// point at the correct offsets from the void* passed into the 586 /// function. ClangExpressionDeclMap::DoStructLayout() must be called 587 /// beforehand, so that the offsets are valid. 588 //------------------------------------------------------------------ 589 590 //------------------------------------------------------------------ 591 /// The top-level pass implementation 592 /// 593 /// @param[in] llvm_function 594 /// The function currently being processed. 595 /// 596 /// @return 597 /// True on success; false otherwise 598 //------------------------------------------------------------------ 599 bool 600 ReplaceVariables (llvm::Function &llvm_function); 601 602 /// Flags 603 bool m_resolve_vars; ///< True if external variable references and persistent variable references should be resolved 604 lldb_private::ExecutionPolicy m_execution_policy; ///< True if the interpreter should be used to attempt to get a static result 605 bool m_interpret_success; ///< True if the interpreter successfully handled the whole expression 606 std::string m_func_name; ///< The name of the function to translate 607 lldb_private::ConstString m_result_name; ///< The name of the result variable ($0, $1, ...) 608 lldb_private::TypeFromParser m_result_type; ///< The type of the result variable. 609 llvm::Module *m_module; ///< The module being processed, or NULL if that has not been determined yet. 610 std::auto_ptr<llvm::TargetData> m_target_data; ///< The target data for the module being processed, or NULL if there is no module. 611 lldb_private::ClangExpressionDeclMap *m_decl_map; ///< The DeclMap containing the Decls 612 StaticDataAllocator *m_data_allocator; ///< If non-NULL, the allocator to use for constant strings 613 llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type 614 llvm::Constant *m_sel_registerName; ///< The address of the function sel_registerName, cast to the appropriate function pointer type 615 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 616 lldb_private::Stream *m_error_stream; ///< If non-NULL, the stream on which errors should be printed 617 618 bool m_has_side_effects; ///< True if the function's result cannot be simply determined statically 619 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. 620 bool m_result_is_pointer; ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult) 621 622 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be replaced by a pointer to the final location of the static allocation. 623 624 //------------------------------------------------------------------ 625 /// UnfoldConstant operates on a constant [Old] which has just been 626 /// replaced with a value [New]. We assume that new_value has 627 /// been properly placed early in the function, in front of the 628 /// first instruction in the entry basic block 629 /// [FirstEntryInstruction]. 630 /// 631 /// UnfoldConstant reads through the uses of Old and replaces Old 632 /// in those uses with New. Where those uses are constants, the 633 /// function generates new instructions to compute the result of the 634 /// new, non-constant expression and places them before 635 /// FirstEntryInstruction. These instructions replace the constant 636 /// uses, so UnfoldConstant calls itself recursively for those. 637 /// 638 /// @param[in] llvm_function 639 /// The function currently being processed. 640 /// 641 /// @return 642 /// True on success; false otherwise 643 //------------------------------------------------------------------ 644 static bool 645 UnfoldConstant (llvm::Constant *old_constant, 646 llvm::Value *new_constant, 647 llvm::Instruction *first_entry_inst); 648 649 //------------------------------------------------------------------ 650 /// Construct a reference to m_reloc_placeholder with a given type 651 /// and offset. This typically happens after inserting data into 652 /// m_data_allocator. 653 /// 654 /// @param[in] type 655 /// The type of the value being loaded. 656 /// 657 /// @param[in] offset 658 /// The offset of the value from the base of m_data_allocator. 659 /// 660 /// @return 661 /// The Constant for the reference, usually a ConstantExpr. 662 //------------------------------------------------------------------ 663 llvm::Constant * 664 BuildRelocation(llvm::Type *type, 665 uint64_t offset); 666 667 //------------------------------------------------------------------ 668 /// Commit the allocation in m_data_allocator and use its final 669 /// location to replace m_reloc_placeholder. 670 /// 671 /// @param[in] module 672 /// The module that m_data_allocator resides in 673 /// 674 /// @return 675 /// True on success; false otherwise 676 //------------------------------------------------------------------ 677 bool 678 CompleteDataAllocation (); 679 680}; 681 682#endif 683