Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Logan | a6f41a7 | 2011-02-27 15:24:00 +0800 | [diff] [blame] | 17 | #include "RuntimeStub.h" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 18 | |
Stephen Hines | bbcef8a | 2011-05-04 19:40:10 -0700 | [diff] [blame] | 19 | #include <bcc/bcc_assert.h> |
| 20 | |
| 21 | #include <stdbool.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | typedef struct { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 26 | const char *mName; |
| 27 | void *mPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 28 | } RuntimeFunction; |
| 29 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 30 | #if defined(__arm__) |
| 31 | #define DEF_GENERIC_RUNTIME(func) \ |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 32 | extern void *func; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 33 | #define DEF_VFP_RUNTIME(func) \ |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 34 | extern void *func ## vfp; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 35 | #define DEF_LLVM_RUNTIME(func) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 36 | #define DEF_BCC_RUNTIME(func) |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 37 | #include "Runtime.def" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 38 | #endif |
| 39 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 40 | static const RuntimeFunction gRuntimes[] = { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 41 | #if defined(__arm__) |
| 42 | #define DEF_GENERIC_RUNTIME(func) \ |
| 43 | { #func, (void*) &func }, |
| 44 | // TODO: enable only when target support VFP |
| 45 | #define DEF_VFP_RUNTIME(func) \ |
| 46 | { #func, (void*) &func ## vfp }, |
| 47 | #else |
| 48 | // host compiler library must contain generic runtime |
| 49 | #define DEF_GENERIC_RUNTIME(func) |
| 50 | #define DEF_VFP_RUNTIME(func) |
| 51 | #endif |
| 52 | #define DEF_LLVM_RUNTIME(func) \ |
| 53 | { #func, (void*) &func }, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 54 | #define DEF_BCC_RUNTIME(func) \ |
| 55 | { #func, &func ## _bcc }, |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 56 | #include "Runtime.def" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 59 | static int CompareRuntimeFunction(const void *a, const void *b) { |
| 60 | return strcmp(((const RuntimeFunction*) a)->mName, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 61 | ((const RuntimeFunction*) b)->mName); |
| 62 | } |
| 63 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 64 | void *FindRuntimeFunction(const char *Name) { |
| 65 | // binary search |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 66 | const RuntimeFunction Key = { Name, NULL }; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 67 | const RuntimeFunction *R = |
| 68 | bsearch(&Key, |
| 69 | gRuntimes, |
| 70 | sizeof(gRuntimes) / sizeof(RuntimeFunction), |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 71 | sizeof(RuntimeFunction), |
| 72 | CompareRuntimeFunction); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 73 | |
| 74 | return ((R) ? R->mPtr : NULL); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 75 | } |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 76 | |
| 77 | void VerifyRuntimesTable() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 78 | unsigned N = sizeof(gRuntimes) / sizeof(RuntimeFunction), i; |
| 79 | for(i = 0; i < N; i++) { |
| 80 | const char *Name = gRuntimes[i].mName; |
| 81 | int *Ptr = FindRuntimeFunction(Name); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 82 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 83 | if (Ptr != (int*) gRuntimes[i].mPtr) |
Stephen Hines | bbcef8a | 2011-05-04 19:40:10 -0700 | [diff] [blame] | 84 | bccAssert(false && "Table is corrupted (runtime name should be sorted " |
| 85 | "in Runtime.def)."); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 86 | } |
| 87 | } |