| 1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
|---|---|
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_ |
| 6 | #define RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_ |
| 7 | |
| 8 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 9 | #error "AOT runtime should not use compiler sources (including header files)" |
| 10 | #endif // defined(DART_PRECOMPILED_RUNTIME) |
| 11 | |
| 12 | #include "vm/compiler/frontend/kernel_translation_helper.h" |
| 13 | #include "vm/hash_table.h" |
| 14 | #include "vm/object.h" |
| 15 | |
| 16 | namespace dart { |
| 17 | namespace kernel { |
| 18 | |
| 19 | // Reads and caches constants from the kernel constant pool. |
| 20 | class ConstantReader { |
| 21 | public: |
| 22 | ConstantReader(KernelReaderHelper* helper, ActiveClass* active_class); |
| 23 | |
| 24 | virtual ~ConstantReader() {} |
| 25 | |
| 26 | InstancePtr ReadConstantExpression(); |
| 27 | ObjectPtr ReadAnnotations(); |
| 28 | |
| 29 | // Peeks to see if constant at the given offset will evaluate to |
| 30 | // instance of the given clazz. |
| 31 | bool IsInstanceConstant(intptr_t constant_offset, const Class& clazz); |
| 32 | |
| 33 | // Reads a constant at the given offset (possibly by recursing |
| 34 | // into sub-constants). |
| 35 | InstancePtr ReadConstant(intptr_t constant_offset); |
| 36 | |
| 37 | private: |
| 38 | InstancePtr ReadConstantInternal(intptr_t constant_offset); |
| 39 | |
| 40 | KernelReaderHelper* helper_; |
| 41 | Zone* zone_; |
| 42 | TranslationHelper& translation_helper_; |
| 43 | ActiveClass* active_class_; |
| 44 | const Script& script_; |
| 45 | Instance& result_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(ConstantReader); |
| 48 | }; |
| 49 | |
| 50 | class KernelConstMapKeyEqualsTraits : public AllStatic { |
| 51 | public: |
| 52 | static const char* Name() { return "KernelConstMapKeyEqualsTraits"; } |
| 53 | static bool ReportStats() { return false; } |
| 54 | |
| 55 | static bool IsMatch(const Object& a, const Object& b) { |
| 56 | const Smi& key1 = Smi::Cast(a); |
| 57 | const Smi& key2 = Smi::Cast(b); |
| 58 | return (key1.Value() == key2.Value()); |
| 59 | } |
| 60 | static bool IsMatch(const intptr_t key1, const Object& b) { |
| 61 | return KeyAsSmi(key1) == Smi::Cast(b).raw(); |
| 62 | } |
| 63 | static uword Hash(const Object& obj) { |
| 64 | const Smi& key = Smi::Cast(obj); |
| 65 | return HashValue(key.Value()); |
| 66 | } |
| 67 | static uword Hash(const intptr_t key) { |
| 68 | return HashValue(Smi::Value(KeyAsSmi(key))); |
| 69 | } |
| 70 | static ObjectPtr NewKey(const intptr_t key) { return KeyAsSmi(key); } |
| 71 | |
| 72 | private: |
| 73 | static uword HashValue(intptr_t pos) { return pos % (Smi::kMaxValue - 13); } |
| 74 | |
| 75 | static SmiPtr KeyAsSmi(const intptr_t key) { |
| 76 | ASSERT(key >= 0); |
| 77 | return Smi::New(key); |
| 78 | } |
| 79 | }; |
| 80 | typedef UnorderedHashMap<KernelConstMapKeyEqualsTraits> KernelConstantsMap; |
| 81 | |
| 82 | } // namespace kernel |
| 83 | } // namespace dart |
| 84 | |
| 85 | #endif // RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_ |
| 86 |