1/*
2 * Copyright (C) 2011 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_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
18#define ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
19
20
21#include "dex_file-inl.h"
22#include "sea_ir/ir/visitor.h"
23#include "sea_ir/types/types.h"
24
25namespace sea_ir {
26
27// The TypeInferenceVisitor visits each instruction and computes its type taking into account
28//   the current type of the operands. The type is stored in the visitor.
29// We may be better off by using a separate visitor type hierarchy that has return values
30//   or that passes data as parameters, than to use fields to store information that should
31//   in fact be returned after visiting each element. Ideally, I would prefer to use templates
32//   to specify the returned value type, but I am not aware of a possible implementation
33//   that does not horribly duplicate the visitor infrastructure code (version 1: no return value,
34//   version 2: with template return value).
35class TypeInferenceVisitor: public IRVisitor {
36 public:
37  TypeInferenceVisitor(SeaGraph* graph, TypeData* type_data,
38      art::verifier::RegTypeCache* types):
39    graph_(graph), type_data_(type_data), type_cache_(types), crt_type_() {
40  }
41  // There are no type related actions to be performed on these classes.
42  void Initialize(SeaGraph* graph) { }
43  void Visit(SeaGraph* graph);
44  void Visit(Region* region) { }
45
46  void Visit(PhiInstructionNode* instruction);
47  void Visit(SignatureNode* parameter);
48  void Visit(InstructionNode* instruction) { }
49  void Visit(UnnamedConstInstructionNode* instruction);
50  void Visit(ConstInstructionNode* instruction) { }
51  void Visit(ReturnInstructionNode* instruction) { }
52  void Visit(IfNeInstructionNode* instruction) { }
53  void Visit(MoveResultInstructionNode* instruction);
54  void Visit(InvokeStaticInstructionNode* instruction);
55  void Visit(AddIntInstructionNode* instruction);
56  void Visit(GotoInstructionNode* instruction) { }
57  void Visit(IfEqzInstructionNode* instruction) { }
58
59  const Type* MergeTypes(std::vector<const Type*>& types) const;
60  const Type* MergeTypes(const Type* t1, const Type* t2) const;
61  std::vector<const Type*> GetOperandTypes(InstructionNode* instruction) const;
62  const Type* GetType() {
63    // TODO: Currently multiple defined types are not supported.
64    if (!crt_type_.empty()) {
65      const Type* single_type = crt_type_.at(0);
66      crt_type_.clear();
67      return single_type;
68    }
69    return NULL;
70  }
71
72 protected:
73  const SeaGraph* const graph_;
74  TypeData* type_data_;
75  art::verifier::RegTypeCache* type_cache_;
76  std::vector<const Type*> crt_type_;             // Stored temporarily between two calls to Visit.
77};
78
79}  // namespace sea_ir
80
81#endif  // ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
82