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