Changeset 4346 for icGREP


Ignore:
Timestamp:
Dec 18, 2014, 10:49:33 AM (5 years ago)
Author:
cameron
Message:

Pablo printer uses ostream, python Pablo syntax

Location:
icGREP/icgrep-devel/icgrep
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/compiler.cpp

    r4337 r4346  
    2525#include <pablo/analysis/useanalysis.h>
    2626
    27 //#define DEBUG_PRINT_RE_AST
    28 //#define DEBUG_PRINT_PBIX_AST
     27#define DEBUG_PRINT_RE_AST
     28#define DEBUG_PRINT_PBIX_AST
    2929
    3030#ifdef DEBUG_PRINT_RE_AST
     
    113113    #ifdef DEBUG_PRINT_PBIX_AST
    114114    //Print to the terminal the AST that was generated by the character class compiler.
    115     std::cerr << "CC AST:" << std::endl << PabloPrinter::print(main.statements()) << std::endl;
     115    std::cerr << "CC AST:" << std::endl;
     116    PabloPrinter::print(main.statements(), std::cerr);
    116117    #endif
    117118
     
    124125    #ifdef DEBUG_PRINT_PBIX_AST
    125126    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
    126     std::cerr << "Initial Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
     127    std::cerr << "Initial Pablo AST:\n";
     128    PabloPrinter::print(main.statements(), std::cerr);
    127129    #endif
    128130
     
    132134    #ifdef DEBUG_PRINT_PBIX_AST
    133135    //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
    134     std::cerr << "Final Pablo AST:" << PabloPrinter::print(main.statements()) << ")" << std::endl;
     136    std::cerr << "Final Pablo AST:\n";
     137    PabloPrinter::print(main.statements(), std::cerr);
    135138    #endif
    136139
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4344 r4346  
    849849        auto f = mMarkerMap.find(var->getName());
    850850        if (LLVM_UNLIKELY(f == mMarkerMap.end())) {
    851             throw std::runtime_error(PabloPrinter::print(var) + " used before creation.");
     851            throw std::runtime_error((var->getName()->str()) + " used before creation.");
    852852        }
    853853        retVal = f->second;
  • icGREP/icgrep-devel/icgrep/pablo/printer_pablos.cpp

    r4280 r4346  
    66
    77#include "printer_pablos.h"
    8 
     8#include <iostream>
     9#include <ostream>
    910
    1011//Regular Expressions
     
    3839using namespace pablo;
    3940
    40 std::string PabloPrinter::print(const PabloBlock & block)
     41void PabloPrinter::print(const PabloBlock & block, std::ostream & strm)
    4142{
    42     std::string strOut = "[";
    43 
    44     strOut = strOut.substr(0, strOut.length() - 1);
    45     strOut += "],[";
    46 
    47     strOut += print(block.statements());
    48 
    49     strOut = strOut.substr(0, strOut.length() - 1);
    50     strOut += "]";
    51 
    52     return strOut;
     43    print(block.statements(), "", strm);
    5344}
    5445
    55 std::string PabloPrinter::print(const StatementList & stmts) {
    56     std::string strOut = "";
    57     for (const Statement * stmt : stmts) {
    58         strOut += print(stmt) + "\n";
    59     }
    60     return strOut;
     46void PabloPrinter::print(const StatementList & stmts, std::ostream & strm) {
     47    print(stmts, "", strm);
    6148}
    6249
    63 std::string PabloPrinter::print(const Statement * stmt) {
     50void PabloPrinter::print(const StatementList & stmts, std::string indent, std::ostream & strm) {
     51    for (const Statement * stmt : stmts) {
     52        print(stmt, indent, strm);
     53    }
     54}
     55
     56void PabloPrinter::print(const Statement * stmt, std::string indent, std::ostream & strm) {
    6457    if (stmt == nullptr) {
    65         return "<null>";
     58        strm << "<null-stmt>" << std::endl;
    6659    }
    6760    else if (const Assign * an = dyn_cast<const Assign>(stmt)) {
    68         std::string result = "Assign('" + an->getName()->str() + "'," + print(an->getExpr());
     61        strm << indent;
    6962        if (an->isOutputAssignment()) {
    70             result += ",Output=" + std::to_string(an->getOutputIndex());
     63            strm << "Output[" << std::to_string(an->getOutputIndex()) << "].";
    7164        }
    72         return result + ")";
     65        strm << an->getName()->str() << " = ";
     66        print(an->getExpr(), strm);
     67        strm << std::endl;
    7368    }
    7469    else if (const Next * next = dyn_cast<const Next>(stmt)) {
    75         return "Next(" + next->getName()->str() + "," + print(next->getExpr()) + ")";
     70        strm << indent << "Next(" << next->getName()->str() << ") = ";
     71        print(next->getExpr(), strm);
     72        strm << std::endl;
    7673    }
    7774    else if (const If * ifstmt = dyn_cast<const If>(stmt)) {
    78         return "If(" + print(ifstmt->getCondition()) + "," + print(ifstmt->getBody()) + ")";
     75        strm << indent << "if ";
     76        print(ifstmt->getCondition(), strm);
     77        strm << ":\n";
     78        print(ifstmt->getBody(), indent + "  ", strm);
    7979    }
    8080    else if (const While * whl = dyn_cast<const While>(stmt)) {
    81         return "While(" + print(whl->getCondition()) + "," + print(whl->getBody()) + ")";
     81        strm << indent << "while";
     82        print(whl->getCondition(), strm);
     83        strm << ":\n";
     84        print(whl->getBody(), indent + "  ", strm);
    8285    }
    83     return "???";
     86    else {
     87        strm << indent << "**UNKNOWN Pablo Statement type **" << std::endl;
     88    }
    8489}
    8590
    86 std::string PabloPrinter::print(const PabloAST *expr) {
     91void PabloPrinter::print(const PabloAST *expr, std::ostream & strm) {
    8792    if (expr == nullptr) {
    88         return "<null>";
     93        strm << "<null-expr>";
    8994    }
    9095    else if (isa<const Zeroes>(expr)) {
    91         return "Zeroes";
     96        strm << "0";
    9297    }
    9398    else if (isa<const Ones>(expr)) {
    94         return "Ones";
     99        strm << "1";
    95100    }
    96101    else if (const Call * pablo_call = dyn_cast<const Call>(expr)) {
    97         return "Call '" + pablo_call->getCallee()->str() + "'";
     102        strm << pablo_call->getCallee()->str() << "()";
    98103    }
    99104    else if (const Var * pablo_var = dyn_cast<const Var>(expr)) {
    100         return "Var '" + pablo_var->getName()->str() + "' ";
     105        strm << pablo_var->getName()->str();
    101106    }
    102107    else if (const And * pablo_and = dyn_cast<const And>(expr)) {
    103         return "And(" + print(pablo_and->getExpr1()) +"," + print(pablo_and->getExpr2()) + ")";
     108        strm << "(";
     109        print(pablo_and->getExpr1(), strm);
     110        strm << " & ";
     111        print(pablo_and->getExpr2(), strm);
     112        strm << ")";
    104113    }
    105114    else if (const Or * pablo_or = dyn_cast<const Or>(expr)) {
    106         return "Or(" + print(pablo_or->getExpr1()) + "," + print(pablo_or->getExpr2()) + ")";
     115        strm << "(";
     116        print(pablo_or->getExpr1(), strm);
     117        strm << " | ";
     118        print(pablo_or->getExpr2(), strm);
     119        strm << ")";
    107120    }
    108121    else if (const Sel * pablo_sel = dyn_cast<const Sel>(expr)) {
    109         return "(" + print(pablo_sel->getCondition()) + " ? " + print(pablo_sel->getTrueExpr()) + " : " + print(pablo_sel->getFalseExpr()) + ")";
     122        strm << "(";
     123        print(pablo_sel->getCondition(), strm);
     124        strm << " ? ";
     125        print(pablo_sel->getTrueExpr(), strm);
     126        strm << " : ";
     127        print(pablo_sel->getFalseExpr(), strm);
     128        strm << ")";
    110129    }
    111130    else if (const Not * pablo_not = dyn_cast<const Not>(expr)) {
    112         return "Not (" + print(pablo_not->getExpr()) + ")";
     131        strm << "(~";
     132        print(pablo_not->getExpr(), strm);
     133        strm << ")";
    113134    }
    114135    else if (const Advance * adv = dyn_cast<const Advance>(expr)) {
    115         return "Advance(" + print(adv->getExpr()) + ", " + std::to_string(adv->getAdvanceAmount()) + ")";
     136        strm << "pablo.Advance(";
     137        print(adv->getExpr(), strm);
     138        strm << ", " << std::to_string(adv->getAdvanceAmount()) << ")";
    116139    }
    117140    else if (const MatchStar * mstar = dyn_cast<const MatchStar>(expr)) {
    118         return "MatchStar(" + print(mstar->getMarker()) + ", " + print(mstar->getCharClass()) + ")";
     141        strm << "pablo.MatchStar(";
     142        print(mstar->getMarker(), strm);
     143        strm << ", ";
     144        print(mstar->getCharClass(), strm);
     145        strm << ")";
    119146    }
    120147    else if (const ScanThru * sthru = dyn_cast<const ScanThru>(expr)) {
    121         return "ScanThru(" + print(sthru->getScanFrom()) + ", " + print(sthru->getScanThru()) + ")";
     148        strm << "pablo.ScanThru(";
     149        print(sthru->getScanFrom(), strm);
     150        strm << ", ";
     151        print(sthru->getScanThru(), strm);
     152        strm << ")";
    122153    }
    123154    else if (isa<Statement>(expr)) {
    124         return print(cast<Statement>(expr));
     155        print(cast<Statement>(expr), strm);
    125156    }
    126     return "???";
     157    else {
     158        strm << "**UNKNOWN Pablo Expression type **\n" << std::endl;
     159    }
    127160}
    128161
  • icGREP/icgrep-devel/icgrep/pablo/printer_pablos.h

    r4276 r4346  
    1717class PabloPrinter {
    1818public:
    19     static std::string print(const pablo::PabloBlock & block);
    20     static std::string print(const pablo::StatementList & stmts);
    21     static std::string print(const pablo::PabloAST * expr);
    22     static std::string print(const pablo::Statement *stmt);
     19    static void print(const pablo::PabloBlock & block, std::ostream & strm);
     20    static void print(const pablo::StatementList & stmts, std::ostream & strm);
     21    static void print(const pablo::StatementList & stmts, std::string indent, std::ostream & strm);
     22    static void print(const pablo::PabloAST * expr, std::ostream & strm);
     23    static void print(const pablo::Statement *stmt, std::string indent, std::ostream & strm);
    2324};
    2425
Note: See TracChangeset for help on using the changeset viewer.