blob: f940c351572879a04d172df1830995b9a880bd87 [file] [log] [blame]
Zonr Chang932648d2010-10-13 22:23:56 +08001/*
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
Logana6f41a72011-02-27 15:24:00 +080017#include "RuntimeStub.h"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070018
Stephen Hinesbbcef8a2011-05-04 19:40:10 -070019#include <bcc/bcc_assert.h>
20
21#include <stdbool.h>
Shih-wei Liao77ed6142010-04-07 12:21:42 -070022#include <string.h>
23#include <stdlib.h>
24
25typedef struct {
Zonr Chang932648d2010-10-13 22:23:56 +080026 const char *mName;
27 void *mPtr;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070028} RuntimeFunction;
29
Shih-wei Liao77ed6142010-04-07 12:21:42 -070030#if defined(__arm__)
31 #define DEF_GENERIC_RUNTIME(func) \
Zonr Chang932648d2010-10-13 22:23:56 +080032 extern void *func;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070033 #define DEF_VFP_RUNTIME(func) \
Zonr Chang932648d2010-10-13 22:23:56 +080034 extern void *func ## vfp;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070035 #define DEF_LLVM_RUNTIME(func)
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070036 #define DEF_BCC_RUNTIME(func)
Loganc4395232010-11-27 18:54:17 +080037#include "Runtime.def"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070038#endif
39
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070040static const RuntimeFunction gRuntimes[] = {
Shih-wei Liao77ed6142010-04-07 12:21:42 -070041#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 Liaoabd1e3d2010-04-28 01:47:00 -070054#define DEF_BCC_RUNTIME(func) \
55 { #func, &func ## _bcc },
Loganc4395232010-11-27 18:54:17 +080056#include "Runtime.def"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070057};
58
Zonr Chang932648d2010-10-13 22:23:56 +080059static int CompareRuntimeFunction(const void *a, const void *b) {
60 return strcmp(((const RuntimeFunction*) a)->mName,
Shih-wei Liao77ed6142010-04-07 12:21:42 -070061 ((const RuntimeFunction*) b)->mName);
62}
63
Zonr Chang932648d2010-10-13 22:23:56 +080064void *FindRuntimeFunction(const char *Name) {
65 // binary search
Shih-wei Liao77ed6142010-04-07 12:21:42 -070066 const RuntimeFunction Key = { Name, NULL };
Zonr Chang932648d2010-10-13 22:23:56 +080067 const RuntimeFunction *R =
68 bsearch(&Key,
69 gRuntimes,
70 sizeof(gRuntimes) / sizeof(RuntimeFunction),
Shih-wei Liao77ed6142010-04-07 12:21:42 -070071 sizeof(RuntimeFunction),
72 CompareRuntimeFunction);
Zonr Chang932648d2010-10-13 22:23:56 +080073
74 return ((R) ? R->mPtr : NULL);
Shih-wei Liao77ed6142010-04-07 12:21:42 -070075}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070076
77void VerifyRuntimesTable() {
Zonr Chang932648d2010-10-13 22:23:56 +080078 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 Liaoabd1e3d2010-04-28 01:47:00 -070082
Zonr Chang932648d2010-10-13 22:23:56 +080083 if (Ptr != (int*) gRuntimes[i].mPtr)
Stephen Hinesbbcef8a2011-05-04 19:40:10 -070084 bccAssert(false && "Table is corrupted (runtime name should be sorted "
85 "in Runtime.def).");
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070086 }
87}