Llvm Study Notes-code instance howtousejit analysis (1)

Source: Internet
Author: User
Document directory
  • I. initializenativetarget

 

37 # include "llvm/llvmcontext. H "<br/> 38 # include" llvm/module. H "<br/> 39 # include" llvm/constants. H "<br/> 40 # include" llvm/derivedtypes. H "<br/> 41 # include" llvm/instructions. H "<br/> 42 # include" llvm/executionengine/JIT. H "<br/> 43 # include" llvm/executionengine/interpreter. H "<br/> 44 # include" llvm/executionengine/genericvalue. H "<br/> 45 # include" llvm/target/targetselect. H "<br/> 46 # include" llvm/support/managedstatic. H "<br/> 47 # include" llvm/support/raw_ostream.h "<br/> 48 using namespace llvm; <br/> 49 <br/> 50 int main () {<br/> 51 <br/> 52 initializenativetarget (); <br/> 53 <br/> 54 llvmcontext context; <br/> 55 <br/> 56 // create some module to put our function into it. <br/> 57 module * m = new module ("test", context ); <br/> 58 <br/> 59 // create the Add1 function entry and insert this entry into module M. the <br/> 60 // function will have a return type of "int" and take an argument of "int ". <br/> 61 // the '0' terminates the list of argument types. <br/> 62 function * add1f = <br/> 63 cast <function> (m-> getorinsertfunction ("Add1", type: getint32ty (context ), <br/> 64 type: getint32ty (context), <br/> 65 (type *) 0); <br/>

 

The example of howtousejit demonstrates how to use llvm to create and execute two functions. The C language prototype of the function is as follows:

Int Add1 (int x ){
Return x + 1;
}

Int Foo (){
Return Add1 (10 );
}

I. initializenativetarget

The initializenativetarget function is called in row 52 of the preceding example function. This function is mainly used to initialize the target machine corresponding to the host.

Function call to ensure that the corresponding library of the target machine has been linked to the program. The function implementation is as follows:

101 inline bool initializenativetarget () {<br/> 102 // if we have a native target, initialize it to ensure it is linked in. <br/> 103 # ifdef llvm_native_target <br/> 104 llvm_native_targetinfo (); <br/> 105 llvm_native_target (); <br/> 106 return false; <br/> 107 # else <br/> 108 return true; <br/> 109 # endif <br/> 110}

 

Ii. llvmcontext

Line 54 of the howtousejit Code declares an instance context of the llvmcontext type. This class is defined in the File Include/llvm/llvmcontext. h. Note the following in the Code:

 

/// This is an important class for using llvm in a threaded context. It
/// (Opaquely) owns and manages the core "Global" data of llvm's Core
/// Infrastructure, including the type and constant uniquing tables.
/// Llvmcontext itself provides no locking guarantees, so you shoshould be careful
/// To have one context per Thread.

I understand that this llvmcontext records the local variables of the thread for each thread, that is, every llvm thread corresponds to such a context instance.

 

Iii. Module

Then, a module variable is initialized in line 57 of the Code. The module description in the code comment is as follows:

/// A module instance is used to store all the information related to
/// Llvm module. modules are the top level container of all other llvm
/// Intermediate representation (IR) objects. Each module directly contains
/// List of globals variables, a list of functions, a list of libraries (or
/// Other modules) This module depends on, a symbol table, and various data
/// About the target's characteristics.

An instance of one module is used to store all information in one module. It is the container that represents the object among all other llvms. Each target directly contains a list of global variables,

A list of functions, a list of function libraries that this module depends on, a symbol table, and some data about this target feature.

The module class Declaration contains the following member variables:

Public:

Typedef iplist <globalvariable> globallisttype;
/// The type for the list of functions.
Typedef iplist <function> functionlisttype;
/// The type for the list of aliases.
Typedef iplist <globalalias> aliaslisttype;
/// The type for the list of named metadata.
Typedef ilist <namedmdnode> namedmdlisttype;

/// The type for the list of dependent libraries.
Typedef STD: vector <STD: String> librarylisttype;

PRIVATE:
Llvmcontext & context; // <The llvmcontext from which types and
/// <Constants are allocated.
Public:

/// Get the target endian information.
/// @ Returns endianess-an enumeration for the endianess of the target
Endianness getendianness () const;

/// Get the target pointer size.
/// @ Returns pointersize-an enumeration for the size of the target's pointer
Pointersize getpointersize () const;

/// Get the global data context.
/// @ Returns llvmcontext-a container for llvm's global information
Llvmcontext & getcontext () const {return context ;}

 

IV,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.