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