blob: b9ff0ac5abeed072cc059074f7fd665ae1e4b754 [file] [log] [blame]
Andreas Gampec87d27b2014-06-26 16:11:07 -07001/*
2 * Copyright (C) 2014 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#ifndef ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_
18#define ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_
19
20#include "gc/heap.h"
21#include "gc/space/image_space.h"
22#include "instruction_set.h"
23#include "runtime.h"
24
25#include <string>
26
27namespace art {
28
29class ImplicitCheckOptions {
30 public:
31 static constexpr const char* kImplicitChecksOatHeaderKey = "implicit-checks";
32
33 static std::string Serialize(bool explicit_null_checks, bool explicit_stack_overflow_checks,
34 bool explicit_suspend_checks) {
35 char tmp[4];
36 tmp[0] = explicit_null_checks ? 'N' : 'n';
37 tmp[1] = explicit_stack_overflow_checks ? 'O' : 'o';
38 tmp[2] = explicit_suspend_checks ? 'S' : 's';
39 tmp[3] = 0;
40 return std::string(tmp);
41 }
42
43 static bool Parse(const char* str, bool* explicit_null_checks,
44 bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) {
45 if (str != nullptr && str[0] != 0 && str[1] != 0 && str[2] != 0 &&
46 (str[0] == 'n' || str[0] == 'N') &&
47 (str[1] == 'o' || str[1] == 'O') &&
48 (str[2] == 's' || str[2] == 'S')) {
49 *explicit_null_checks = str[0] == 'N';
50 *explicit_stack_overflow_checks = str[1] == 'O';
51 *explicit_suspend_checks = str[2] == 'S';
52 return true;
53 } else {
54 return false;
55 }
56 }
57
58 static void Check(InstructionSet isa, bool* explicit_null_checks,
59 bool* explicit_stack_overflow_checks, bool* explicit_suspend_checks) {
60 switch (isa) {
61 case kArm:
62 case kThumb2:
63 break; // All checks implemented, leave as is.
64
65 default: // No checks implemented, reset all to explicit checks.
66 *explicit_null_checks = true;
67 *explicit_stack_overflow_checks = true;
68 *explicit_suspend_checks = true;
69 }
70 }
71
72 static bool CheckForCompiling(InstructionSet host, InstructionSet target,
73 bool* explicit_null_checks, bool* explicit_stack_overflow_checks,
74 bool* explicit_suspend_checks) {
75 // Check the boot image settings.
76 Runtime* runtime = Runtime::Current();
77 if (runtime != nullptr) {
78 gc::space::ImageSpace* ispace = runtime->GetHeap()->GetImageSpace();
79 if (ispace != nullptr) {
80 const OatFile* oat_file = ispace->GetOatFile();
81 if (oat_file != nullptr) {
82 const char* v = oat_file->GetOatHeader().GetStoreValueByKey(kImplicitChecksOatHeaderKey);
83 if (!Parse(v, explicit_null_checks, explicit_stack_overflow_checks,
84 explicit_suspend_checks)) {
85 LOG(FATAL) << "Should have been able to parse boot image implicit check values";
86 }
87 return true;
88 }
89 }
90 }
91
92 // Check the current runtime.
93 bool cross_compiling = true;
94 switch (host) {
95 case kArm:
96 case kThumb2:
97 cross_compiling = target != kArm && target != kThumb2;
98 break;
99 default:
100 cross_compiling = host != target;
101 break;
102 }
103 if (!cross_compiling) {
104 Runtime* runtime = Runtime::Current();
105 *explicit_null_checks = runtime->ExplicitNullChecks();
106 *explicit_stack_overflow_checks = runtime->ExplicitStackOverflowChecks();
107 *explicit_suspend_checks = runtime->ExplicitSuspendChecks();
108 return true;
109 }
110
111 // Give up.
112 return false;
113 }
114};
115
116} // namespace art
117
118#endif // ART_RUNTIME_IMPLICIT_CHECK_OPTIONS_H_