1f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang/*
2f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * Copyright 2012, The Android Open Source Project
3f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang *
4f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
5f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * you may not use this file except in compliance with the License.
6f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * You may obtain a copy of the License at
7f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang *
8f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
9f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang *
10f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * Unless required by applicable law or agreed to in writing, software
11f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
12f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * See the License for the specific language governing permissions and
14f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang * limitations under the License.
15f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang */
16f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
17c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Support/Initialization.h"
18f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
19f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#include <cstdlib>
20f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
21f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#include <llvm/Support/ErrorHandling.h>
22f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#include <llvm/Support/TargetSelect.h>
23f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
24c72c4ddfcd79c74f70713da91a69569451b5c19eZonr Chang#include "bcc/Config/Config.h"
25ef73a242762bcd8113b9b65ceccbe7d909b5acbcZonr Chang#include "bcc/Support/Log.h"
26f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
27f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Changnamespace {
28f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
2986e4506416f974d3c717deaca2b17b0792cc2d49Stephen Hinesvoid llvm_error_handler(void *pUserData, const std::string &pMessage,
3086e4506416f974d3c717deaca2b17b0792cc2d49Stephen Hines                        bool pGenCrashDiag) {
31f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  ALOGE("%s", pMessage.c_str());
32f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  ::exit(1);
33f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang}
34f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
35f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang} // end anonymous namespace
36f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
37f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Changvoid bcc::init::Initialize() {
38f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  static bool is_initialized = false;
39f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
40f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  if (is_initialized) {
41f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang    return;
42f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  }
43f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
44f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  // Setup error handler for LLVM.
45f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  llvm::remove_fatal_error_handler();
46f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  llvm::install_fatal_error_handler(llvm_error_handler, NULL);
47f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
48f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#if defined(PROVIDE_ARM_CODEGEN)
49f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeARMAsmPrinter();
50d793ca93d75fe8f1d29ceab8f9bf0432f0f63565Stephen Hines  LLVMInitializeARMAsmParser();
51388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# if USE_DISASSEMBLER
52388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao  LLVMInitializeARMDisassembler();
53388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# endif
54f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeARMTargetMC();
55f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeARMTargetInfo();
56f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeARMTarget();
57f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#endif
58f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
59f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#if defined(PROVIDE_MIPS_CODEGEN)
60f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeMipsAsmPrinter();
61d793ca93d75fe8f1d29ceab8f9bf0432f0f63565Stephen Hines  LLVMInitializeMipsAsmParser();
62388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# if USE_DISASSEMBLER
63388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao  LLVMInitializeMipsDisassembler();
64388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# endif
65f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeMipsTargetMC();
66f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeMipsTargetInfo();
67f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeMipsTarget();
68f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#endif
69f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
70f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#if defined(PROVIDE_X86_CODEGEN)
71f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeX86AsmPrinter();
72d793ca93d75fe8f1d29ceab8f9bf0432f0f63565Stephen Hines  LLVMInitializeX86AsmParser();
73388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# if USE_DISASSEMBLER
74388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao  LLVMInitializeX86Disassembler();
75388fec12e6bbf5798281d266cac63630bcaba333Shih-wei Liao# endif
76f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeX86TargetMC();
77f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeX86TargetInfo();
78f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  LLVMInitializeX86Target();
79f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang#endif
80f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
81c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray#if defined(PROVIDE_ARM64_CODEGEN)
82c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64AsmPrinter();
83c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64AsmParser();
84c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray# if USE_DISASSEMBLER
85c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64Disassembler();
86c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray# endif
87c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64TargetMC();
88c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64TargetInfo();
89c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  LLVMInitializeAArch64Target();
90c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray#endif
91c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray
92f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  is_initialized = true;
93f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang
94f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang  return;
95f74ee1940ac8e01c862e90de49eb10982648f6a1Zonr Chang}
96