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 | |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 21 | #include <assert.h> |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 22 | |
| 23 | typedef struct { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 24 | const char *mName; |
| 25 | void *mPtr; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 26 | } RuntimeFunction; |
| 27 | |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 28 | #if defined(__arm__) |
| 29 | #define DEF_GENERIC_RUNTIME(func) \ |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 30 | extern void *func; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 31 | #define DEF_VFP_RUNTIME(func) \ |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 32 | extern void *func ## vfp; |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 33 | #define DEF_LLVM_RUNTIME(func) |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 34 | #define DEF_BCC_RUNTIME(func) |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 35 | #include "Runtime.def" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 38 | static const RuntimeFunction gRuntimes[] = { |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 39 | #if defined(__arm__) |
| 40 | #define DEF_GENERIC_RUNTIME(func) \ |
| 41 | { #func, (void*) &func }, |
| 42 | // TODO: enable only when target support VFP |
| 43 | #define DEF_VFP_RUNTIME(func) \ |
| 44 | { #func, (void*) &func ## vfp }, |
| 45 | #else |
| 46 | // host compiler library must contain generic runtime |
| 47 | #define DEF_GENERIC_RUNTIME(func) |
| 48 | #define DEF_VFP_RUNTIME(func) |
| 49 | #endif |
| 50 | #define DEF_LLVM_RUNTIME(func) \ |
| 51 | { #func, (void*) &func }, |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 52 | #define DEF_BCC_RUNTIME(func) \ |
| 53 | { #func, &func ## _bcc }, |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 54 | #include "Runtime.def" |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 57 | static int CompareRuntimeFunction(const void *a, const void *b) { |
| 58 | return strcmp(((const RuntimeFunction*) a)->mName, |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 59 | ((const RuntimeFunction*) b)->mName); |
| 60 | } |
| 61 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 62 | void *FindRuntimeFunction(const char *Name) { |
| 63 | // binary search |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 64 | const RuntimeFunction Key = { Name, NULL }; |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 65 | const RuntimeFunction *R = |
| 66 | bsearch(&Key, |
| 67 | gRuntimes, |
| 68 | sizeof(gRuntimes) / sizeof(RuntimeFunction), |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 69 | sizeof(RuntimeFunction), |
| 70 | CompareRuntimeFunction); |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 71 | |
| 72 | return ((R) ? R->mPtr : NULL); |
Shih-wei Liao | 77ed614 | 2010-04-07 12:21:42 -0700 | [diff] [blame] | 73 | } |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 74 | |
| 75 | void VerifyRuntimesTable() { |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 76 | unsigned N = sizeof(gRuntimes) / sizeof(RuntimeFunction), i; |
| 77 | for(i = 0; i < N; i++) { |
| 78 | const char *Name = gRuntimes[i].mName; |
| 79 | int *Ptr = FindRuntimeFunction(Name); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 80 | |
Zonr Chang | 932648d | 2010-10-13 22:23:56 +0800 | [diff] [blame] | 81 | if (Ptr != (int*) gRuntimes[i].mPtr) |
| 82 | assert(false && "Table is corrupted (runtime name should be sorted in " |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 83 | "Runtime.def)."); |
Shih-wei Liao | abd1e3d | 2010-04-28 01:47:00 -0700 | [diff] [blame] | 84 | } |
| 85 | } |