chllcode.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002, Trent Waddington
00003  *
00004  * See the file "LICENSE.TERMS" for information on usage and
00005  * redistribution of this file, and for a DISCLAIMER OF ALL
00006  * WARRANTIES.
00007  *
00008  */
00009 
00010 /*
00011  * $Revision: 1.28 $    // 1.24.2.8
00012  *
00013  * 22 Nov 02 - Mike: Re-ordered initialisations to keep gcc quiet
00014  */
00015 
00016 /*==============================================================================
00017  * FILE:       chllcode.h
00018  * OVERVIEW:   Concrete class for the "C" high level language
00019  *             This class provides methods which are specific for the C language binding.
00020  *             I guess this will be the most popular output language unless we do C++.
00021  *============================================================================*/
00022 
00023 #ifndef _CHLLCODE_H_
00024 #define _CHLLCODE_H_
00025 
00026 #include <string>
00027 #include <sstream>
00028 
00029 class BasicBlock;
00030 class Exp;
00031 class Proc;
00032 class Assign;
00033 class LocationSet;
00034 
00035 // Operator precedence
00036 /*
00037 Operator Name               Associativity   Operators
00038 Primary scope resolution    left to right   ::
00039 Primary                     left to right   ()  [ ]  .  -> dynamic_cast typeid
00040 Unary                       right to left   ++  --  +  -  !  ~  &  *
00041                                             (type_name)  sizeof new delete
00042 C++ Pointer to Member       left to right   .* ->*
00043 Multiplicative              left to right   *  /  %
00044 Additive                    left to right   +  -
00045 Bitwise Shift               left to right   <<  >>
00046 Relational                  left to right   <  >  <=  >=
00047 Equality                    left to right   ==  !=
00048 Bitwise AND                 left to right   &
00049 Bitwise Exclusive OR        left to right   ^
00050 Bitwise Inclusive OR        left to right   |
00051 Logical AND                 left to right   &&
00052 Logical OR                  left to right   ||
00053 Conditional                 right to left   ? :
00054 Assignment                  right to left   =  +=  -=  *=   /=  <<=  >>=  %=
00055                                             &=  ^=  |=
00056 Comma                       left to right   ,
00057 */
00058 
00059 /// Operator precedence
00060 enum PREC {
00061     PREC_NONE=0,            ///< Outer level (no parens required)
00062     PREC_COMMA,             ///< Comma
00063     PREC_ASSIGN,            ///< Assignment
00064     PREC_COND,              ///< Conditional
00065     PREC_LOG_OR,            ///< Logical OR
00066     PREC_LOG_AND,           ///< Logical AND
00067     PREC_BIT_IOR,           ///< Bitwise Inclusive OR
00068     PREC_BIT_XOR,           ///< Bitwise Exclusive OR
00069     PREC_BIT_AND,           ///< Bitwise AND
00070     PREC_EQUAL,             ///< Equality
00071     PREC_REL,               ///< Relational
00072     PREC_BIT_SHIFT,         ///< Bitwise Shift
00073     PREC_ADD,               ///< Additive
00074     PREC_MULT,              ///< Multiplicative
00075     PREC_PTR_MEM,           ///< C++ Pointer to Member
00076     PREC_UNARY,             ///< Unary
00077     PREC_PRIM,              ///< Primary
00078     PREC_SCOPE              ///< Primary scope resolution
00079 };
00080 
00081 
00082 
00083 /// Outputs C code.
00084 class CHLLCode : public HLLCode {
00085 private:
00086         /// The generated code.
00087         std::list<char *> lines;
00088 
00089         void indent(std::ostringstream& str, int indLevel);
00090         void appendExp(std::ostringstream& str, Exp *exp, PREC curPrec, bool uns = false);
00091         void appendType(std::ostringstream& str, Type *typ);
00092         void appendTypeIdent(std::ostringstream& str, Type *typ, const char *ident);
00093         /// Adds: (
00094         void openParen(std::ostringstream& str, PREC outer, PREC inner) {
00095             if (inner < outer) str << "("; }
00096         /// Adds: )
00097         void closeParen(std::ostringstream& str, PREC outer, PREC inner) {
00098             if (inner < outer) str << ")"; }
00099 
00100         void appendLine(const std::ostringstream& ostr);
00101         void appendLine(const std::string& s);
00102 
00103         /// All locals in a Proc
00104         std::map<std::string, Type*> locals;
00105 
00106         /// All used goto labels.
00107         std::set<int> usedLabels;
00108 
00109 public:
00110         // constructor
00111                 CHLLCode();
00112                 CHLLCode(UserProc *p);
00113 
00114         // destructor
00115 virtual         ~CHLLCode();
00116 
00117         // clear this class, calls the base
00118 virtual void    reset();
00119 
00120         /*
00121          * Functions to add new code
00122          */
00123 
00124         // pretested loops (cond is optional because it is in the bb [somewhere])
00125 virtual void    AddPretestedLoopHeader(int indLevel, Exp *cond);
00126 virtual void    AddPretestedLoopEnd(int indLevel);
00127 
00128         // endless loops
00129 virtual void    AddEndlessLoopHeader(int indLevel);
00130 virtual void    AddEndlessLoopEnd(int indLevel);
00131 
00132         // posttested loops
00133 virtual void    AddPosttestedLoopHeader(int indLevel);
00134 virtual void    AddPosttestedLoopEnd(int indLevel, Exp *cond);
00135 
00136         // case conditionals "nways"
00137 virtual void    AddCaseCondHeader(int indLevel, Exp *cond);
00138 virtual void    AddCaseCondOption(int indLevel, Exp *opt);
00139 virtual void    AddCaseCondOptionEnd(int indLevel);
00140 virtual void    AddCaseCondElse(int indLevel);
00141 virtual void    AddCaseCondEnd(int indLevel);
00142 
00143         // if conditions
00144 virtual void    AddIfCondHeader(int indLevel, Exp *cond);
00145 virtual void    AddIfCondEnd(int indLevel);
00146 
00147         // if else conditions
00148 virtual void    AddIfElseCondHeader(int indLevel, Exp *cond);
00149 virtual void    AddIfElseCondOption(int indLevel);
00150 virtual void    AddIfElseCondEnd(int indLevel);
00151 
00152         // goto, break, continue, etc
00153 virtual void    AddGoto(int indLevel, int ord);
00154 virtual void    AddContinue(int indLevel);
00155 virtual void    AddBreak(int indLevel);
00156 
00157         // labels
00158 virtual void    AddLabel(int indLevel, int ord);
00159 virtual void    RemoveLabel(int ord);
00160 virtual void    RemoveUnusedLabels(int maxOrd);
00161 
00162         // sequential statements
00163 virtual void    AddAssignmentStatement(int indLevel, Assign *asgn);
00164 virtual void    AddCallStatement(int indLevel, Proc *proc, const char *name, StatementList& args,
00165                     StatementList* results);
00166 virtual void    AddIndCallStatement(int indLevel, Exp *exp, StatementList &args, StatementList* results);
00167 virtual void    AddReturnStatement(int indLevel, StatementList* rets);
00168 
00169         // proc related
00170 virtual void    AddProcStart(UserProc* proc);
00171 virtual void    AddProcEnd();
00172 virtual void    AddLocal(const char *name, Type *type, bool last = false);
00173 virtual void    AddGlobal(const char *name, Type *type, Exp *init = NULL);
00174 virtual void    AddPrototype(UserProc* proc);
00175 private:
00176         void    AddProcDec(UserProc* proc, bool open);  // Implement AddProcStart and AddPrototype
00177 public:
00178 
00179         // comments
00180 virtual void    AddLineComment(char* cmt);
00181 
00182         /*
00183          * output functions
00184          */
00185 virtual void    print(std::ostream &os);
00186 };
00187 
00188 #endif

Generated on Tue Sep 19 21:18:15 2006 for Boomerang by  doxygen 1.4.6