Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: runtime/vm/flow_graph_builder.cc

Issue 9453014: Implement a simple InstructionVisitor class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/flow_graph_builder.h ('k') | runtime/vm/intermediate_language.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/flow_graph_builder.h" 5 #include "vm/flow_graph_builder.h"
6 6
7 #include "vm/flags.h" 7 #include "vm/flags.h"
8 #include "vm/intermediate_language.h" 8 #include "vm/intermediate_language.h"
9 #include "vm/os.h" 9 #include "vm/os.h"
10 #include "vm/parser.h" 10 #include "vm/parser.h"
(...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 858
859 void FlowGraphBuilder::TraceBailout() const { 859 void FlowGraphBuilder::TraceBailout() const {
860 if (FLAG_trace_bailout && HasBailedOut()) { 860 if (FLAG_trace_bailout && HasBailedOut()) {
861 OS::Print("Failed: %s in %s\n", 861 OS::Print("Failed: %s in %s\n",
862 bailout_reason_, 862 bailout_reason_,
863 parsed_function().function().ToFullyQualifiedCString()); 863 parsed_function().function().ToFullyQualifiedCString());
864 } 864 }
865 } 865 }
866 866
867 867
868 void FlowGraphBuilder::PrintGraph() const { 868 // Graph printing.
869 if (!FLAG_print_flow_graph || HasBailedOut()) return; 869 class FlowGraphPrinter : public InstructionVisitor {
870 public:
871 explicit FlowGraphPrinter(const Function& function) : function_(function) { }
870 872
871 OS::Print("==== %s\n", 873 virtual ~FlowGraphPrinter() {}
872 parsed_function().function().ToFullyQualifiedCString());
873 874
874 for (intptr_t i = postorder_block_entries_.length() - 1; i >= 0; --i) { 875 // Print the instructions in a block terminated by newlines. Add "goto N"
876 // to the end of the block if it ends with an unconditional jump to
877 // another block and that block is not next in reverse postorder.
878 void VisitBlocks(const GrowableArray<BlockEntryInstr*>& block_order);
879
880 // Each visit function prints an instruction with a four space
881 // indent and no trailing newline. Basic block entries are labeled
882 // with their block number.
883 #define DECLARE_VISIT(type) \
884 virtual void Visit##type(type##Instr* instr);
885 FOR_EACH_INSTRUCTION(DECLARE_VISIT)
886 #undef DECLARE_VISIT
887
888 private:
889 const Function& function_;
890
891 DISALLOW_COPY_AND_ASSIGN(FlowGraphPrinter);
892 };
893
894
895 void FlowGraphPrinter::VisitBlocks(
896 const GrowableArray<BlockEntryInstr*>& block_order) {
897 OS::Print("==== %s\n", function_.ToFullyQualifiedCString());
898
899 for (intptr_t i = block_order.length() - 1; i >= 0; --i) {
875 // Print the block entry. 900 // Print the block entry.
876 Instruction* current = postorder_block_entries_[i]->Print(); 901 Instruction* current = block_order[i]->Accept(this);
877 // And all the successors until an exit, branch, or a block entry. 902 // And all the successors until an exit, branch, or a block entry.
878 while ((current != NULL) && !current->IsBlockEntry()) { 903 while ((current != NULL) && !current->IsBlockEntry()) {
879 OS::Print("\n"); 904 OS::Print("\n");
880 current = current->Print(); 905 current = current->Accept(this);
881 } 906 }
882 if (current != NULL && current->IsBlockEntry()) { 907 if ((current != NULL) && current->IsBlockEntry()) {
883 OS::Print(" goto %d", current->GetBlockNumber()); 908 OS::Print(" goto %d", BlockEntryInstr::cast(current)->block_number());
884 } 909 }
885 OS::Print("\n"); 910 OS::Print("\n");
886 } 911 }
887 } 912 }
888 913
889 914
915 void FlowGraphPrinter::VisitJoinEntry(JoinEntryInstr* instr) {
916 OS::Print("%2d: [join]", instr->block_number());
917 }
918
919
920 void FlowGraphPrinter::VisitTargetEntry(TargetEntryInstr* instr) {
921 OS::Print("%2d: [target]", instr->block_number());
922 }
923
924
925 void FlowGraphPrinter::VisitDo(DoInstr* instr) {
926 OS::Print(" ");
927 instr->computation()->Print();
928 }
929
930
931 void FlowGraphPrinter::VisitBind(BindInstr* instr) {
932 OS::Print(" t%d <-", instr->temp_index());
933 instr->computation()->Print();
934 }
935
936
937 void FlowGraphPrinter::VisitReturn(ReturnInstr* instr) {
938 OS::Print(" return ");
939 instr->value()->Print();
940 }
941
942
943 void FlowGraphPrinter::VisitBranch(BranchInstr* instr) {
944 OS::Print(" if ");
945 instr->value()->Print();
946 OS::Print(" goto(%d, %d)", instr->true_successor()->block_number(),
947 instr->false_successor()->block_number());
948 }
949
950
890 void FlowGraphBuilder::BuildGraph() { 951 void FlowGraphBuilder::BuildGraph() {
891 EffectGraphVisitor for_effect(this, 0); 952 EffectGraphVisitor for_effect(this, 0);
892 for_effect.AddInstruction(new TargetEntryInstr()); 953 for_effect.AddInstruction(new TargetEntryInstr());
893 parsed_function().node_sequence()->Visit(&for_effect); 954 parsed_function().node_sequence()->Visit(&for_effect);
894 TraceBailout(); 955 TraceBailout();
895 if (!HasBailedOut() && (for_effect.entry() != NULL)) { 956 if (!HasBailedOut() && (for_effect.entry() != NULL)) {
896 // Accumulate basic block entries via postorder traversal. 957 // Accumulate basic block entries via postorder traversal.
897 for_effect.entry()->Postorder(&postorder_block_entries_); 958 for_effect.entry()->Postorder(&postorder_block_entries_);
898 // Number the blocks in reverse postorder starting with 0. 959 // Number the blocks in reverse postorder starting with 0.
899 intptr_t last_index = postorder_block_entries_.length() - 1; 960 intptr_t last_index = postorder_block_entries_.length() - 1;
900 for (intptr_t i = last_index; i >= 0; --i) { 961 for (intptr_t i = last_index; i >= 0; --i) {
901 postorder_block_entries_[i]->SetBlockNumber(last_index - i); 962 postorder_block_entries_[i]->set_block_number(last_index - i);
902 } 963 }
903 } 964 }
904 PrintGraph(); 965 if (FLAG_print_flow_graph) {
966 FlowGraphPrinter printer(parsed_function().function());
967 printer.VisitBlocks(postorder_block_entries_);
968 }
905 } 969 }
906 970
907 } // namespace dart 971 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/flow_graph_builder.h ('k') | runtime/vm/intermediate_language.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698