blob: 83d96bfebe2dd44f7b2b4e48cae7892dcc88d35a [file] [log] [blame]
Matt Waladabd2462015-07-15 18:39:41 -07001/*
2 * Copyright 2015, 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 "bcinfo/BitcodeWrapper.h"
18
19#include "llvm/Support/raw_ostream.h"
20
21#include "BitWriter_2_9/ReaderWriter_2_9.h"
22#include "BitWriter_2_9_func/ReaderWriter_2_9_func.h"
23#include "BitWriter_3_2/ReaderWriter_3_2.h"
24
25#include "slang_assert.h"
26#include "slang_bitcode_gen.h"
27#include "slang_version.h"
28
29namespace slang {
30
31void writeBitcode(llvm::raw_ostream &Out,
32 const llvm::Module &M,
33 uint32_t TargetAPI,
34 uint32_t OptimizationLevel) {
35 std::string BitcodeStr;
36 llvm::raw_string_ostream Bitcode(BitcodeStr);
37
38 // Create the bitcode.
39 switch (TargetAPI) {
40 case SLANG_HC_TARGET_API:
41 case SLANG_HC_MR1_TARGET_API:
42 case SLANG_HC_MR2_TARGET_API: {
43 // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter
44 llvm_2_9::WriteBitcodeToFile(&M, Bitcode);
45 break;
46 }
47 case SLANG_ICS_TARGET_API:
48 case SLANG_ICS_MR1_TARGET_API: {
49 // ICS targets must use the LLVM 2.9_func BitcodeWriter
50 llvm_2_9_func::WriteBitcodeToFile(&M, Bitcode);
51 break;
52 }
53 default: {
54 if (TargetAPI != SLANG_DEVELOPMENT_TARGET_API &&
55 (TargetAPI < SLANG_MINIMUM_TARGET_API ||
56 TargetAPI > SLANG_MAXIMUM_TARGET_API)) {
57 slangAssert(false && "Invalid target API value");
58 }
59 // Switch to the 3.2 BitcodeWriter by default, and don't use
60 // LLVM's included BitcodeWriter at all (for now).
61 llvm_3_2::WriteBitcodeToFile(&M, Bitcode);
62 break;
63 }
64 }
65
66 const uint32_t CompilerVersion = SlangVersion::CURRENT;
67
68 // Create the bitcode wrapper.
69 bcinfo::AndroidBitcodeWrapper Wrapper;
70 size_t ActualWrapperLen = bcinfo::writeAndroidBitcodeWrapper(
71 &Wrapper, Bitcode.str().length(), TargetAPI,
72 CompilerVersion, OptimizationLevel);
73
74 slangAssert(ActualWrapperLen > 0);
75
76 // Write out the file.
77 Out.write(reinterpret_cast<char*>(&Wrapper), ActualWrapperLen);
78 Out << Bitcode.str();
79}
80
81} // namespace slang