C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

print_walker.cc

Go to the documentation of this file.
00001 // $Id: print_walker.cc,v 1.9 2003/08/07 23:14:05 pnav Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2000 University of Texas at Austin
00008 // 
00009 //  Samuel Z. Guyer
00010 //  Daniel A. Jimenez
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #include "c_breeze.h"
00039 #include "print_walker.h"
00040 #include "df_number_walker.h"
00041 
00042 // ------------------------------------------------------------
00043 // Entry point
00044 // ------------------------------------------------------------
00045 
00046 void print_walker::print(Node * n, ostream & out)
00047 {
00048   numbering_map * num = df_number_walker::number(n, Preorder);
00049   print_walker p(out, num);
00050 
00051   n->walk(p);
00052 
00053   delete num;
00054 }
00055 
00056 // ------------------------------------------------------------
00057 // Internal methods
00058 // ------------------------------------------------------------
00059 
00060 print_walker::print_walker(ostream & out, numbering_map * num)
00061   : Walker(Both, Subtree), 
00062     _out(out),
00063     _indent(0),
00064     _num(num)
00065 {}
00066 
00067 void print_walker::indent(Node * n) const
00068 {
00069   _out.width(4);
00070   _out << (*_num)[n];
00071 
00072   _out.width(_indent+1);
00073   _out.fill('.');
00074   // _out.setf(ios_base::left, ios_base::adjustfield);
00075 
00076   _out << ".";
00077 
00078   _out.width(0);
00079   _out.fill(' ');
00080   // _out.setf(ios_base::right, ios_base::adjustfield);
00081 }
00082 
00083 void print_walker::at_unit(unitNode * the_unit, Order ord)
00084 {
00085   if (ord == Preorder) {
00086     indent(the_unit);
00087     _out << "unit" << endl;
00088 
00089     in();
00090   }
00091 
00092   if (ord == Postorder)
00093     out();
00094 }
00095 
00096 void print_walker::at_decl(declNode * the_decl, Order ord)
00097 {
00098   if (ord == Preorder) {
00099     indent(the_decl);
00100 
00101     _out << "decl: ";
00102 
00103     if (the_decl->name().empty())
00104       _out << "(abstract)";
00105     else
00106       _out << "\"" << the_decl->name() << "\"";
00107 
00108     _out << " ";
00109     _out << declNode::decl_location_name(the_decl->decl_location());
00110     _out << " ";
00111     _out << declNode::storage_class_name(the_decl->storage_class());
00112 
00113     _out << " ids=(";
00114     id_list & i = the_decl->ref_list();
00115     for ( id_list_p p = i.begin(); p != i.end(); p++ ) {
00116       if ( p != i.begin() )
00117         _out << ", ";
00118       _out << "#" << (*_num)[*p];
00119     }
00120     _out << ")" << endl;
00121 
00122     in();
00123   }
00124 
00125   if (ord == Postorder)
00126     out();
00127 }
00128 
00129 void print_walker::at_proc(procNode * the_proc, Order ord)
00130 {
00131   if (ord == Preorder) {
00132     indent(the_proc);
00133     _out << "proc: ";
00134 
00135     if (! the_proc->decl())
00136       _out << "(no func decl) ";
00137 
00138     if (! the_proc->body())
00139       _out << "(no body) ";
00140 
00141     _out << endl;
00142 
00143     in();
00144   }
00145 
00146   if (ord == Postorder)
00147     out();
00148 }
00149 
00150 void print_walker::at_prim(primNode * the_prim, Order ord)
00151 {
00152   if (ord == Preorder) {
00153     indent(the_prim);
00154     _out << "prim: ";
00155 
00156     _out << the_prim->basic().to_string() << " ";
00157     _out << the_prim->type_qualifiers_name() << " ";
00158     _out << endl;
00159 
00160     in();
00161   }
00162 
00163   if (ord == Postorder)
00164     out();
00165 }
00166 
00167 void print_walker::at_tdef(tdefNode * the_tdef, Order ord)
00168 {
00169   if (ord == Preorder) {
00170     indent(the_tdef);
00171 
00172     _out << "tdef: ";
00173 
00174     if (the_tdef->name().empty())
00175       _out << "(no name)";
00176     else
00177       _out << "\"" << the_tdef->name() << "\"";
00178 
00179     _out << " ";
00180     _out << "def=#" << (*_num)[the_tdef->def()] << " ";
00181     _out << the_tdef->type_qualifiers_name() << endl;
00182 
00183     in();
00184   }
00185 
00186   if (ord == Postorder)
00187     out();
00188 }
00189 
00190 void print_walker::at_ptr(ptrNode * the_ptr, Order ord)
00191 {
00192   if (ord == Preorder) {
00193     indent(the_ptr);
00194     _out << "ptr: " << the_ptr->type_qualifiers_name() << endl;
00195 
00196     in();
00197   }
00198 
00199   if (ord == Postorder)
00200     out();
00201 }
00202 
00203 void print_walker::at_array(arrayNode * the_array, Order ord)
00204 {
00205   if (ord == Preorder) {
00206     indent(the_array);
00207     _out << "array: size=" << the_array->size() << " ";
00208 
00209     if (! the_array->dim())
00210       _out << "(no dimension) ";
00211 
00212     _out << the_array->type_qualifiers_name() << endl;
00213 
00214     in();
00215   }
00216 
00217   if (ord == Postorder)
00218     out();
00219 }
00220 
00221 void print_walker::at_func(funcNode * the_func, Order ord)
00222 {
00223   if (ord == Preorder) {
00224     indent(the_func);
00225     _out << "func: " << the_func->type_qualifiers_name() << endl;
00226 
00227     in();
00228   }
00229 
00230   if (ord == Postorder)
00231     out();
00232 }
00233 
00234 void print_walker::at_sue(sueNode * the_sue, Order ord)
00235 {
00236   if (ord == Preorder) {
00237     indent(the_sue);
00238 
00239     switch (the_sue->typ()) {
00240     case Struct:
00241       _out << "struct: ";
00242       break;
00243     case Union:
00244       _out << "union: ";
00245       break;
00246     case Enum:
00247       _out << "enum: ";
00248       break;
00249     default:
00250       _out << "ERROR ";
00251     }
00252 
00253     if (the_sue->spec()->name().empty())
00254       _out << "(no name) ";
00255     else
00256       _out << "\"" << the_sue->spec()->name() << "\" ";
00257 
00258     _out << "spec=#" << (*_num)[the_sue->spec()];
00259 
00260     _out << endl;
00261 
00262     in();
00263   }
00264 
00265   if (ord == Postorder)
00266     out();
00267 }
00268 
00269 void print_walker::at_suespec(suespecNode * the_suespec, Order ord)
00270 {
00271   if (ord == Preorder) {
00272     indent(the_suespec);
00273     _out << "suespec: ";
00274 
00275     if (the_suespec->name().empty())
00276       _out << "(no name) ";
00277     else
00278       _out << "\"" << the_suespec->name() << "\"";
00279 
00280     if (the_suespec->complete())
00281       _out << "complete ";
00282 
00283     if (the_suespec->visited())
00284       _out << "visited ";
00285 
00286     _out << "size=" << the_suespec->size() << " align=" << the_suespec->align();
00287     _out << " " << the_suespec->type_qualifiers_name() << endl;
00288 
00289     in();
00290   }
00291 
00292   if (ord == Postorder)
00293     out();
00294 }
00295 
00296 void print_walker::at_const(constNode * the_const, Order ord)
00297 {
00298   if (ord == Preorder) {
00299     indent(the_const);
00300     _out << "const: val=\"" << the_const->text() << "\"" << endl;
00301 
00302     in();
00303   }
00304 
00305   if (ord == Postorder)
00306     out();
00307 }
00308 
00309 void print_walker::at_id(idNode * the_id, Order ord)
00310 {
00311   if (ord == Preorder) {
00312     indent(the_id);
00313     _out << "id: ";
00314     _out << "\"" << the_id->name() << "\" ";
00315     _out << "decl=#" << (*_num)[the_id->decl()];
00316     _out << endl;
00317 
00318     in();
00319   }
00320 
00321   if (ord == Postorder)
00322     out();
00323 }
00324 
00325 void print_walker::at_binary(binaryNode * the_binary, Order ord)
00326 {
00327   if (ord == Preorder) {
00328     indent(the_binary);
00329     _out << "binary: op='" << the_binary->op()->print() << "' ";
00330 
00331     if (! the_binary->left())
00332       _out << "(no LHS) ";
00333 
00334     if (! the_binary->right())
00335       _out << "(no RHS) ";
00336     
00337     _out << endl;
00338 
00339     in();
00340   }
00341 
00342   if (ord == Postorder)
00343     out();
00344 }
00345 
00346 void print_walker::at_unary(unaryNode * the_unary, Order ord)
00347 {
00348   if (ord == Preorder) {
00349     indent(the_unary);
00350     _out << "unary: op='" << the_unary->op()->print() << "'";
00351 
00352     if (! the_unary->expr())
00353       _out << "(no expr) ";
00354 
00355     _out << endl;
00356 
00357     in();
00358   }
00359 
00360   if (ord == Postorder)
00361     out();
00362 }
00363 
00364 void print_walker::at_cast(castNode * the_cast, Order ord)
00365 {
00366   if (ord == Preorder) {
00367     indent(the_cast);
00368     if (the_cast->is_implicit())
00369       _out << "implicit cast: ";
00370     else
00371       _out << "cast: ";
00372 
00373     if (! the_cast->expr())
00374       _out << "(no type) ";
00375 
00376     _out << endl;
00377 
00378     in();
00379   }
00380 
00381   if (ord == Postorder)
00382     out();
00383 }
00384 
00385 void print_walker::at_comma(commaNode * the_comma, Order ord)
00386 {
00387   if (ord == Preorder) {
00388     indent(the_comma);
00389     _out << "comma: " << endl;
00390 
00391     in();
00392   }
00393 
00394   if (ord == Postorder)
00395     out();
00396 }
00397 
00398 void print_walker::at_ternary(ternaryNode * the_ternary, Order ord)
00399 {
00400   if (ord == Preorder) {
00401     indent(the_ternary);
00402     _out << "ternary: ";
00403 
00404     if (! the_ternary->cond())
00405       _out << "(no cond) ";
00406 
00407     if (! the_ternary->true_br())
00408       _out << "(no true branch) ";
00409 
00410     if (! the_ternary->false_br())
00411       _out << "(no false branch) ";
00412 
00413     _out << endl;
00414 
00415     in();
00416   }
00417 
00418   if (ord == Postorder)
00419     out();
00420 }
00421 
00422 void print_walker::at_call(callNode * the_call, Order ord)
00423 {
00424   if (ord == Preorder) {
00425     indent(the_call);
00426     _out << "call: ";
00427 
00428     if (! the_call->name())
00429       _out << "(no object) ";
00430 
00431     if (the_call->args().empty())
00432       _out << "(no args) ";
00433 
00434     _out << "proc=#" << (*_num)[the_call->proc()];
00435 
00436     _out << endl;
00437 
00438     in();
00439   }
00440 
00441   if (ord == Postorder)
00442     out();
00443 }
00444 
00445 void print_walker::at_initializer(initializerNode * the_initializer, Order ord)
00446 {
00447   if (ord == Preorder) {
00448     indent(the_initializer);
00449     _out << "initializer: ";
00450 
00451     if (the_initializer->exprs().empty())
00452       _out << "(no exprs) ";
00453 
00454     _out << endl;
00455 
00456     in();
00457   }
00458 
00459   if (ord == Postorder)
00460     out();
00461 }
00462 
00463 void print_walker::at_block(blockNode * the_block, Order ord)
00464 {
00465   if (ord == Preorder) {
00466     indent(the_block);
00467     _out << "block: ";
00468 
00469     if (the_block->decls().empty())
00470       _out << "(no decls) ";
00471 
00472     if (the_block->stmts().empty())
00473       _out << "(no stmts) ";
00474 
00475     _out << endl;
00476 
00477     in();
00478   }
00479 
00480   if (ord == Postorder)
00481     out();
00482 }
00483 
00484 void print_walker::at_basicblock(basicblockNode * the_bb, Order ord)
00485 {
00486   if (ord == Preorder) {
00487     indent(the_bb);
00488     _out << "basicblock: ";
00489 
00490     if (the_bb->decls().empty())
00491       _out << "(no decls) ";
00492 
00493     if (the_bb->stmts().empty())
00494       _out << "(no stmts) ";
00495 
00496     _out << endl;
00497 
00498     in();
00499   }
00500 
00501   if (ord == Postorder)
00502     out();
00503 }
00504 
00505 void print_walker::at_exprstmt(exprstmtNode * the_exprstmt, Order ord)
00506 {
00507   if (ord == Preorder) {
00508     indent(the_exprstmt);
00509     _out << "exprstmt: ";
00510 
00511     if (! the_exprstmt->expr())
00512       _out << "(no expr) ";
00513 
00514     _out << endl;
00515 
00516     in();
00517   }
00518 
00519   if (ord == Postorder)
00520     out();
00521 }
00522 
00523 void print_walker::at_label(labelNode * the_label, Order ord)
00524 {
00525   if (ord == Preorder) {
00526     indent(the_label);
00527     _out << "label: ";
00528 
00529     if (the_label->name().empty())
00530       _out << "(no name)";
00531     else
00532       _out << "\"" << the_label->name() << "\" ";
00533 
00534     if (! the_label->stmt())
00535       _out << "(no stmt) ";
00536 
00537     _out << " gotos=(";
00538 
00539     goto_list & g = the_label->references();
00540     for (goto_list_p p = g.begin();
00541          p != g.end();
00542          ++p) {
00543       if (p != g.begin())
00544         _out << ", ";
00545       _out << "#" << (*_num)[*p];
00546     }
00547 
00548     _out << ")";
00549     _out << endl;
00550 
00551     in();
00552   }
00553 
00554   if (ord == Postorder)
00555     out();
00556 }
00557 
00558 void print_walker::at_case(caseNode * the_case, Order ord)
00559 {
00560   if (ord == Preorder) {
00561     indent(the_case);
00562 
00563     if (! the_case->expr())
00564       _out << "default: ";
00565     else
00566       _out << "case: ";
00567 
00568     if (! the_case->stmt())
00569       _out << "(no stmt) ";
00570 
00571     _out << "switch=#" << (*_num)[the_case->container()];
00572 
00573     _out << endl;
00574 
00575     in();
00576   }
00577 
00578   if (ord == Postorder)
00579     out();
00580 }
00581 
00582 void print_walker::at_if(ifNode * the_if, Order ord)
00583 {
00584   if (ord == Preorder) {
00585     indent(the_if);
00586     _out << "if: ";
00587 
00588     if (! the_if->expr())
00589       _out << "(no expr) ";
00590 
00591     if (! the_if->true_br())
00592       _out << "(no true branch) ";
00593 
00594     if (! the_if->false_br())
00595       _out << "(no false branch) ";
00596 
00597     _out << endl;
00598 
00599     in();
00600   }
00601 
00602   if (ord == Postorder)
00603     out();
00604 }
00605 
00606 void print_walker::at_switch(switchNode * the_switch, Order ord)
00607 {
00608   if (ord == Preorder) {
00609     indent(the_switch);
00610     _out << "switch: ";
00611 
00612     if (! the_switch->expr())
00613       _out << "(no expr) ";
00614 
00615     if (! the_switch->stmt())
00616       _out << "(no body) ";
00617 
00618     _out << "cases=(";
00619 
00620     target_list & g = the_switch->cases();
00621     for (target_list_p p = g.begin();
00622          p != g.end();
00623          ++p) {
00624       if (p != g.begin())
00625         _out << ", ";
00626       _out << "#" << (*_num)[*p];
00627     }
00628 
00629     _out << ")";
00630     _out << endl;
00631 
00632     in();
00633   }
00634 
00635   if (ord == Postorder)
00636     out();
00637 }
00638 
00639 void print_walker::at_while(whileNode * the_while, Order ord)
00640 {
00641   if (ord == Preorder) {
00642     indent(the_while);
00643     _out << "while: ";
00644 
00645     if (! the_while->cond())
00646       _out << "(no cond) ";
00647 
00648     if (! the_while->body())
00649       _out << "(no body) ";
00650 
00651     _out << endl;
00652 
00653     in();
00654   }
00655 
00656   if (ord == Postorder)
00657     out();
00658 }
00659 
00660 void print_walker::at_do(doNode * the_do, Order ord)
00661 {
00662   if (ord == Preorder) {
00663     indent(the_do);
00664     _out << "do: ";
00665 
00666     if (! the_do->cond())
00667       _out << "(no cond) ";
00668 
00669     if (! the_do->body())
00670       _out << "(no body) ";
00671 
00672     _out << endl;
00673 
00674     in();
00675   }
00676 
00677   if (ord == Postorder)
00678     out();
00679 }
00680 
00681 void print_walker::at_for(forNode * the_for, Order ord)
00682 {
00683   if (ord == Preorder) {
00684     indent(the_for);
00685     _out << "for: ";
00686 
00687     if (! the_for->init())
00688       _out << "(no init) ";
00689 
00690     if (! the_for->cond())
00691       _out << "(no cond) ";
00692 
00693     if (! the_for->next())
00694       _out << "(no next) ";
00695 
00696     if (! the_for->body())
00697       _out << "(no body) ";
00698 
00699     _out << endl;
00700 
00701     in();
00702   }
00703 
00704   if (ord == Postorder)
00705     out();
00706 }
00707 
00708 void print_walker::at_goto(gotoNode * the_goto, Order ord)
00709 {
00710   if (ord == Preorder) {
00711     indent(the_goto);
00712 
00713     _out << "goto: ";
00714     _out << "\"" << the_goto->name() << "\" ";
00715     _out << "label=#" << (*_num)[the_goto->label()];
00716     _out << endl;
00717 
00718     in();
00719   }
00720 
00721   if (ord == Postorder)
00722     out();
00723 }
00724 
00725 void print_walker::at_continue(continueNode * the_continue, Order ord)
00726 {
00727   if (ord == Preorder) {
00728     indent(the_continue);
00729 
00730     _out << "continue: ";
00731     _out << "loop=#" << (*_num)[the_continue->container()];
00732     _out << endl;
00733 
00734     in();
00735   }
00736 
00737   if (ord == Postorder)
00738     out();
00739 }
00740 
00741 void print_walker::at_break(breakNode * the_break, Order ord)
00742 {
00743   if (ord == Preorder) {
00744     indent(the_break);
00745 
00746     _out << "break: ";
00747     _out << "container=#" << (*_num)[the_break->container()];
00748     _out << endl;
00749 
00750     in();
00751   }
00752 
00753   if (ord == Postorder)
00754     out();
00755 }
00756 
00757 void print_walker::at_return(returnNode * the_return, Order ord)
00758 {
00759   if (ord == Preorder) {
00760     indent(the_return);
00761 
00762     _out << "return: ";
00763 
00764     if (! the_return->expr())
00765       _out << "(no expr) ";
00766 
00767     _out << "proc=#" << (*_num)[the_return->proc()];
00768     _out << endl;
00769 
00770     in();
00771   }
00772 
00773   if (ord == Postorder)
00774     out();
00775 }
00776 
00777 void print_walker::at_attrib(attribNode * the_attrib, Order ord)
00778 {
00779   if (ord == Preorder) {
00780     indent(the_attrib);
00781     _out << "attrib: " << the_attrib->name() << endl;
00782 
00783     in();
00784   }
00785 
00786   if (ord == Postorder)
00787     out();
00788 }
00789 
00790 void print_walker::at_operand(operandNode * the_oper, Order ord)
00791 {
00792   if ( ord == Preorder ) {
00793     indent(the_oper);
00794     _out << "operand: addr='" << (the_oper->addr() ? "true" : "false") << "' "
00795          << "star='" << (the_oper->star() ? "true" : "false") << "' ";
00796     if ( ! the_oper->index() )
00797       _out << "(no Index)";
00798 
00799     _out << endl;
00800 
00801     in();
00802   }
00803 
00804   if ( ord == Postorder )
00805     out();
00806 }
00807 
00808 void print_walker::at_conditiongoto(conditiongotoNode * the_condgoto, 
00809                                     Order ord)
00810 {
00811   if ( ord == Preorder ) {
00812     indent(the_condgoto);
00813     _out << "conditiongoto: relop='" << the_condgoto->op()->print() << "' ";
00814     _out << "\"" << the_condgoto->name() << "\" ";
00815     _out << "label=#" << (*_num)[the_condgoto->label()];
00816     _out << endl;
00817 
00818     in();
00819   }
00820   
00821   if ( ord == Postorder )
00822     out();
00823 }
00824 
00825 void print_walker::at_threeAddr(threeAddrNode * the_3addr, Order ord)
00826 {
00827   if ( ord == Preorder ) {
00828     indent(the_3addr);
00829     _out << "threeAddr:";
00830     if ( the_3addr->op() ) 
00831       _out << " op='" << the_3addr->op()->print() << "' ";
00832     _out << endl;
00833 
00834     in();
00835   }
00836 
00837   if ( ord == Postorder )
00838     out();
00839 }
00840 
00841 void print_walker::at_text(textNode * the_text, Order ord)
00842 {
00843   if (ord == Preorder) {
00844     indent(the_text);
00845     _out << "text: " << the_text->text() << endl;
00846 
00847     in();
00848   }
00849 
00850   if (ord == Postorder)
00851     out();
00852 }
00853 
00854 

Generated on August 27, 2003
Back to the C-Breeze home page