Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 1 | ============================================ |
| 2 | libbcc: A Versatile Bitcode Execution Engine |
| 3 | ============================================ |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 4 | |
| 5 | |
| 6 | Introduction |
| 7 | ------------ |
| 8 | |
| 9 | libbcc is an LLVM bitcode execution engine which compiles the bitcode |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 10 | to an in-memory executable. libbcc provides a *just-in-time bitcode |
| 11 | compiler*, which translates the bitcode into machine code, and a *caching |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 12 | mechanism*, which saves the in-memory executable after the compilation. |
| 13 | Here are some highlights of libbcc: |
| 14 | |
| 15 | * libbcc supports bit code from various language frontends, such as |
| 16 | RenderScript, GLSL. |
| 17 | |
| 18 | * libbcc strives to balance between library size, launch time and |
| 19 | steady-state performance: |
| 20 | |
| 21 | * The size of libbcc is aggressively reduced for a mobile device. |
| 22 | We customize and we don't use Execution Engine. |
| 23 | |
| 24 | * To reduce launch time, we support caching of binaries. |
| 25 | |
| 26 | * For steady-state performance, we enable VFP3 and aggressive |
| 27 | optimizations. |
| 28 | |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 29 | * Currently we disable Lazy JITting. |
| 30 | |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 31 | |
| 32 | |
| 33 | API |
| 34 | --- |
| 35 | |
| 36 | Basic: |
| 37 | |
| 38 | * **bccCreateScript** - Create new bcc script |
| 39 | |
| 40 | * **bccRegisterSymbolCallback** - Register the callback function for external |
| 41 | symbol lookup |
| 42 | |
| 43 | * **bccReadBC** - Set the source bitcode for compilation |
| 44 | |
| 45 | * **bccReadModule** - Set the llvm::Module for compilation |
| 46 | |
| 47 | * **bccLinkBC** - Set the library bitcode for linking |
| 48 | |
| 49 | * **bccPrepareExecutable** Create the in-memory executable by either |
| 50 | just-in-time compilation or cache loading |
| 51 | |
| 52 | * **bccDeleteScript** - Destroy bcc script and release the resources |
| 53 | |
| 54 | * **bccGetError** - Get the error code |
| 55 | |
| 56 | * **bccGetScriptInfoLog** *deprecated* - Don't use this |
| 57 | |
| 58 | |
| 59 | Reflection: |
| 60 | |
| 61 | * **bccGetExportVars** - Get the addresses of exported variables |
| 62 | |
| 63 | * **bccGetExportFuncs** - Get the addresses of exported functions |
| 64 | |
| 65 | * **bccGetPragmas** - Get the pragmas |
| 66 | |
| 67 | |
| 68 | Debug: |
| 69 | |
| 70 | * **bccGetFunctions** - Get the function name list |
| 71 | |
| 72 | * **bccGetFunctionBinary** - Get the address and the size of function binary |
| 73 | |
| 74 | |
| 75 | |
| 76 | Cache File Format |
| 77 | ----------------- |
| 78 | |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 79 | A cache file (denoted as \*.oBCC) for libbcc consists of several sections: |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 80 | header, string pool, dependencies table, relocation table, exported |
| 81 | variable list, exported function list, pragma list, function information |
| 82 | table, and bcc context. Every section should be aligned to a word size. |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 83 | Here is the brief description of each sections: |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 84 | |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 85 | * **Header** (OBCC_Header) - The header of a cache file. It contains the |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 86 | magic word, version, machine integer type information, and the size |
| 87 | and the offset of other sections. The header section is guaranteed |
| 88 | to be at the beginning of the cache file. |
| 89 | |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 90 | * **String Pool** (OBCC_StringPool) - A collection of serialized variable |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 91 | length strings. The strp_index in the other part of the cache file |
| 92 | represents the index of such string in this string pool. |
| 93 | |
| 94 | * **Dependencies Table** (OBCC_DependencyTable) - The dependencies table. |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 95 | This table stores the resource name (or file path), the resouece |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 96 | type (rather in APK or on the file system), and the SHA1 checksum. |
| 97 | |
Shih-wei Liao | 0d4984b | 2011-01-09 04:39:00 -0800 | [diff] [blame^] | 98 | * **Relocation Table** (OBCC_RelocationTable) - *not enabled* |
Logan | 36fafd9 | 2011-01-08 22:55:19 +0800 | [diff] [blame] | 99 | |
| 100 | * **Exported Variable List** (OBCC_ExportVarList), |
| 101 | **Exported Function List** (OBCC_ExportFuncList) - |
| 102 | The list of the addresses of exported variables and exported functions. |
| 103 | |
| 104 | * **Pragma List** (OBCC_PragmaList) - The list of pragma key-value pair. |
| 105 | |
| 106 | * **Function Information Table** (OBCC_FuncTable) - This is a table of |
| 107 | function information, such as function name, function entry address, |
| 108 | and function binary size. Besides, the table should be ordered by |
| 109 | function name. |
| 110 | |
| 111 | * **Context** - The context of the in-memory executable, including |
| 112 | the code and the data. The offset of context should aligned to |
| 113 | a page size, so that we can mmap the context directly into the memory. |
| 114 | |
| 115 | For furthur information, you may read `bcc_cache.h <include/bcc/bcc_cache.h>`_, |
| 116 | `CacheReader.cpp <lib/bcc/CacheReader.cpp>`_, and |
| 117 | `CacheWriter.cpp <lib/bcc/CacheWriter.cpp>`_ for details. |
| 118 | |
| 119 | |
| 120 | |
| 121 | JIT'ed Code Calling Conventions |
| 122 | ------------------------------- |
| 123 | |
| 124 | 1. Calls from Execution Environment or from/to within script: |
| 125 | |
| 126 | On ARM, the first 4 arguments will go into r0, r1, r2, and r3, in that order. |
| 127 | The remaining (if any) will go through stack. |
| 128 | |
| 129 | For ext_vec_types such as float2, a set of registers will be used. In the case |
| 130 | of float2, a register pair will be used. Specifically, if float2 is the first |
| 131 | argument in the function prototype, float2.x will go into r0, and float2.y, |
| 132 | r1. |
| 133 | |
| 134 | Note: stack will be aligned to the coarsest-grained argument. In the case of |
| 135 | float2 above as an argument, parameter stack will be aligned to an 8-byte |
| 136 | boundary (if the sizes of other arguments are no greater than 8.) |
| 137 | |
| 138 | 2. Calls from/to a separate compilation unit: (E.g., calls to Execution |
| 139 | Environment if those runtime library callees are not compiled using LLVM.) |
| 140 | |
| 141 | On ARM, we use hardfp. Note that double will be placed in a register pair. |