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
17#include "RuntimeStub.h"
18
19#include <bcc/bcc_assert.h>
20
21#include <stdbool.h>
22#include <string.h>
23#include <stdlib.h>
24
25typedef struct {
26  const char *mName;
27  void *mPtr;
28} RuntimeFunction;
29
30#if defined(__arm__) || defined(__mips__)
31  #define DEF_GENERIC_RUNTIME(func)   \
32    extern void *func;
33  #define DEF_VFP_RUNTIME(func) \
34    extern void *func ## vfp;
35  #define DEF_LLVM_RUNTIME(func)
36  #define DEF_BCC_RUNTIME(func)
37#include "Runtime.def"
38#endif
39
40static const RuntimeFunction gRuntimes[] = {
41#if defined(__arm__) || defined(__mips__)
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 },
54#define DEF_BCC_RUNTIME(func) \
55  { #func, &func ## _bcc },
56#include "Runtime.def"
57};
58
59static int CompareRuntimeFunction(const void *a, const void *b) {
60  return strcmp(((const RuntimeFunction*) a)->mName,
61               ((const RuntimeFunction*) b)->mName);
62}
63
64void *FindRuntimeFunction(const char *Name) {
65  // binary search
66  const RuntimeFunction Key = { Name, NULL };
67  const RuntimeFunction *R =
68      bsearch(&Key,
69              gRuntimes,
70              sizeof(gRuntimes) / sizeof(RuntimeFunction),
71              sizeof(RuntimeFunction),
72              CompareRuntimeFunction);
73
74  return ((R) ? R->mPtr : NULL);
75}
76
77void VerifyRuntimesTable() {
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);
82
83    if (Ptr != (int*) gRuntimes[i].mPtr)
84      bccAssert(false && "Table is corrupted (runtime name should be sorted "
85                         "in Runtime.def).");
86  }
87}
88