type_inference_visitor.h revision 7b89bc0d1e73ae5a4265f93bb5497019b1a9bf17
16447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea/*
26447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * Copyright (C) 2011 The Android Open Source Project
36447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea *
46447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * Licensed under the Apache License, Version 2.0 (the "License");
56447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * you may not use this file except in compliance with the License.
66447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * You may obtain a copy of the License at
76447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea *
86447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea *      http://www.apache.org/licenses/LICENSE-2.0
96447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea *
106447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * Unless required by applicable law or agreed to in writing, software
116447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * distributed under the License is distributed on an "AS IS" BASIS,
126447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * See the License for the specific language governing permissions and
146447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea * limitations under the License.
156447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea */
166447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
176447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#ifndef ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
186447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#define ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
196447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
206447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
216447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#include "dex_file-inl.h"
226447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#include "sea_ir/visitor.h"
236447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#include "sea_ir/types/types.h"
246447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
256447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirleanamespace sea_ir {
266447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
276447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea// The TypeInferenceVisitor visits each instruction and computes its type taking into account
286447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   the current type of the operands. The type is stored in the visitor.
296447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea// We may be better off by using a separate visitor type hierarchy that has return values
306447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   or that passes data as parameters, than to use fields to store information that should
316447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   in fact be returned after visiting each element. Ideally, I would prefer to use templates
326447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   to specify the returned value type, but I am not aware of a possible implementation
336447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   that does not horribly duplicate the visitor infrastructure code (version 1: no return value,
346447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea//   version 2: with template return value).
356447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirleaclass TypeInferenceVisitor: public IRVisitor {
366447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea public:
376447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  TypeInferenceVisitor(SeaGraph* graph, art::verifier::RegTypeCache* types):
386447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea    graph_(graph), type_cache_(types), crt_type_() {
396447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  }
406447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  // There are no type related actions to be performed on these classes.
416447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Initialize(SeaGraph* graph) { }
426447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(SeaGraph* graph) { }
436447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(Region* region) { }
446447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
456447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(PhiInstructionNode* instruction) { }
467b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea  void Visit(SignatureNode* parameter);
476447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(InstructionNode* instruction) { }
486447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(ConstInstructionNode* instruction) { }
496447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(ReturnInstructionNode* instruction) { }
506447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(IfNeInstructionNode* instruction) { }
516447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(MoveResultInstructionNode* instruction) { }
526447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(InvokeStaticInstructionNode* instruction) { }
536447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(AddIntInstructionNode* instruction) { }
546447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(GotoInstructionNode* instruction) { }
556447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  void Visit(IfEqzInstructionNode* instruction) { }
566447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
577b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea  const Type* GetType() {
586447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea    // TODO: Currently multiple defined types are not supported.
597b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea    if (crt_type_.size()>0) {
607b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea      const Type* single_type = crt_type_.at(0);
617b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea      crt_type_.clear();
627b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea      return single_type;
637b89bc0d1e73ae5a4265f93bb5497019b1a9bf17Dragos Sbirlea    }
646447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea    return NULL;
656447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  }
666447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
676447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea protected:
686447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  const SeaGraph* const graph_;
696447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  art::verifier::RegTypeCache* type_cache_;
706447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea  std::vector<const Type*> crt_type_;             // Stored temporarily between two calls to Visit.
716447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea};
726447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
736447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea}  // namespace sea_ir
746447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea
756447919b5ee8d34c4767f908c7cd7223c224544cDragos Sbirlea#endif  // ART_COMPILER_SEA_IR_TYPES_TYPE_INFERENCE_VISITOR_H_
76