blob: e814361714313816e4723701020abb92595806d4 [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
19#include <string.h>
20#include <stdlib.h>
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070021#include <assert.h>
Shih-wei Liao77ed6142010-04-07 12:21:42 -070022
23typedef struct {
Zonr Chang932648d2010-10-13 22:23:56 +080024 const char *mName;
25 void *mPtr;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070026} RuntimeFunction;
27
Shih-wei Liao77ed6142010-04-07 12:21:42 -070028#if defined(__arm__)
29 #define DEF_GENERIC_RUNTIME(func) \
Zonr Chang932648d2010-10-13 22:23:56 +080030 extern void *func;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070031 #define DEF_VFP_RUNTIME(func) \
Zonr Chang932648d2010-10-13 22:23:56 +080032 extern void *func ## vfp;
Shih-wei Liao77ed6142010-04-07 12:21:42 -070033 #define DEF_LLVM_RUNTIME(func)
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070034 #define DEF_BCC_RUNTIME(func)
Loganc4395232010-11-27 18:54:17 +080035#include "Runtime.def"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070036#endif
37
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070038static const RuntimeFunction gRuntimes[] = {
Shih-wei Liao77ed6142010-04-07 12:21:42 -070039#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 Liaoabd1e3d2010-04-28 01:47:00 -070052#define DEF_BCC_RUNTIME(func) \
53 { #func, &func ## _bcc },
Loganc4395232010-11-27 18:54:17 +080054#include "Runtime.def"
Shih-wei Liao77ed6142010-04-07 12:21:42 -070055};
56
Zonr Chang932648d2010-10-13 22:23:56 +080057static int CompareRuntimeFunction(const void *a, const void *b) {
58 return strcmp(((const RuntimeFunction*) a)->mName,
Shih-wei Liao77ed6142010-04-07 12:21:42 -070059 ((const RuntimeFunction*) b)->mName);
60}
61
Zonr Chang932648d2010-10-13 22:23:56 +080062void *FindRuntimeFunction(const char *Name) {
63 // binary search
Shih-wei Liao77ed6142010-04-07 12:21:42 -070064 const RuntimeFunction Key = { Name, NULL };
Zonr Chang932648d2010-10-13 22:23:56 +080065 const RuntimeFunction *R =
66 bsearch(&Key,
67 gRuntimes,
68 sizeof(gRuntimes) / sizeof(RuntimeFunction),
Shih-wei Liao77ed6142010-04-07 12:21:42 -070069 sizeof(RuntimeFunction),
70 CompareRuntimeFunction);
Zonr Chang932648d2010-10-13 22:23:56 +080071
72 return ((R) ? R->mPtr : NULL);
Shih-wei Liao77ed6142010-04-07 12:21:42 -070073}
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070074
75void VerifyRuntimesTable() {
Zonr Chang932648d2010-10-13 22:23:56 +080076 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 Liaoabd1e3d2010-04-28 01:47:00 -070080
Zonr Chang932648d2010-10-13 22:23:56 +080081 if (Ptr != (int*) gRuntimes[i].mPtr)
82 assert(false && "Table is corrupted (runtime name should be sorted in "
Loganc4395232010-11-27 18:54:17 +080083 "Runtime.def).");
Shih-wei Liaoabd1e3d2010-04-28 01:47:00 -070084 }
85}