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  

c_breeze.h

Go to the documentation of this file.
00001 // $Id: c_breeze.h,v 1.20 2003/08/11 17:19:29 abrown 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 #ifndef CBZ_C_BREEZE_H
00039 #define CBZ_C_BREEZE_H
00040 
00041 
00042 // EDB
00043 #ifdef _WIN32
00044 // Disable warnings about long (> 255 chars) identifiers.
00045 #pragma warning(disable:4786)
00046 #pragma warning(disable:4503)
00047 // Disable warnings about int being forced to bool.
00048 #pragma warning(disable:4800)
00049 // Disable warnings about size conversion 
00050 #pragma warning(disable:4267)
00051 #endif // 
00052 
00053 // -- Include the necessary standard library stuff
00054 
00055 // #include <ios>
00056 #include <iostream>
00057 #include <fstream>
00058 #include <sstream>
00059 #include <string>
00060 #include <list>
00061 #include <vector>
00062 #include <map>
00063 #include <algorithm>
00064 #include <functional>
00065 
00066 #include <assert.h>
00067 #include <stdio.h>
00068 #include "cbz_util.h"
00069 #include "hash_set_ex.h"
00070 
00071 // -- Declare all the classes ahead of time...
00072 
00073 class Node;
00074 class unitNode;
00075 class defNode;
00076 class declNode;
00077 class typeNode;
00078 class primNode;
00079 class tdefNode;
00080 class ptrNode;
00081 class arrayNode;
00082 class funcNode;
00083 class sueNode;
00084 class structNode;
00085 class unionNode;
00086 class enumNode;
00087 class suespecNode;
00088 class exprNode;
00089 class indexNode;
00090 class constNode;
00091 class idNode;
00092 class binaryNode;
00093 class unaryNode;
00094 class castNode;
00095 class commaNode;
00096 class ternaryNode;
00097 class callNode;
00098 class initializerNode;
00099 class stmtNode;
00100 class blockNode;
00101 class basicblockNode;
00102 class exprstmtNode;
00103 class targetNode;
00104 class labelNode;
00105 class caseNode;
00106 class selectionNode;
00107 class ifNode;
00108 class switchNode;
00109 class loopNode;
00110 class whileNode;
00111 class doNode;
00112 class forNode;
00113 class jumpNode;
00114 class gotoNode;
00115 class continueNode;
00116 class breakNode;
00117 class returnNode;
00118 class attribNode;
00119 class procNode;
00120 class operandNode;
00121 class conditiongotoNode;
00122 class threeAddrNode;
00123 class textNode;
00124 class CBZ;
00125 class Containers;
00126 class Coord;
00127 class Datatype;
00128 class LirInst;
00129 class LirBlock;
00130 
00131 class Symbols;
00132 class Identifiers_table;
00133 class Tags_table;
00134 class Externals_table;
00135 
00136 class FlowVal;
00137 class FlowProblem;
00138 class Annote;
00139 
00140 // EDB
00141 using namespace std;
00142 
00143 typedef list< Node * > node_list;
00144 typedef node_list::iterator node_list_p;
00145 
00146 typedef list< exprNode * > expr_list;
00147 typedef expr_list::iterator expr_list_p;
00148 
00149 typedef list< declNode * > decl_list;
00150 typedef decl_list::iterator decl_list_p;
00151 
00152 typedef list< targetNode * > target_list;
00153 typedef target_list::iterator target_list_p;
00154 
00155 typedef list< attribNode * > attrib_list;
00156 typedef attrib_list::iterator attrib_list_p;
00157 
00158 typedef list< indexNode *> index_list;
00159 typedef index_list::iterator index_list_p;
00160 
00161 typedef list< idNode * > id_list;
00162 typedef id_list::iterator id_list_p;
00163 
00164 typedef list< stmtNode * > stmt_list;
00165 typedef stmt_list::iterator stmt_list_p;
00166 
00167 typedef list< gotoNode * > goto_list;
00168 typedef goto_list::iterator goto_list_p;
00169 
00170 typedef list< defNode * > def_list;
00171 typedef def_list::iterator def_list_p;
00172 
00173 typedef list< Annote * > annote_list;
00174 typedef annote_list::iterator annote_list_p;
00175 
00176 typedef list< unitNode * > unit_list;
00177 typedef unit_list::iterator unit_list_p;
00178 
00179 typedef vector< string > str_vec;
00180 typedef str_vec::iterator str_vec_p;
00181 
00182 typedef list< string > str_list;
00183 typedef str_list::iterator str_list_p;
00184 
00185 typedef list< textNode * > text_list;
00186 typedef text_list::iterator text_list_p;
00187 
00188 typedef list< suespecNode * > suespec_list;
00189 typedef suespec_list::iterator suespec_list_p;
00190 
00191 typedef list< blockNode * > block_list;
00192 typedef block_list::iterator block_list_p;
00193 
00194 typedef list< basicblockNode * > basicblock_list;
00195 typedef basicblock_list::iterator basicblock_list_p;
00196 
00197 typedef list< operandNode * > operand_list;
00198 typedef operand_list::iterator operand_list_p;
00199 
00200 class Visitor;
00201 class Walker;
00202 class Changer;
00203 
00204 class output_context;
00205 
00206 
00207 // --- Some necessary forward defines
00208 
00209 // a list of instructions (a vector, really)
00210 typedef list<LirInst*> instruction_list;
00211 typedef instruction_list::iterator instruction_list_p;
00212 
00213 // a set of instructions
00214 typedef hash_set_ex<LirInst*> instruction_set;
00215 typedef instruction_set::iterator instruction_set_p;
00216 
00217 // forward define of basic block class and definition of vector
00218 typedef list<LirBlock*> LirBlockList;
00219 typedef LirBlockList::iterator LirBlockList_p;
00220 typedef hash_set_ex<LirBlock*> LirBlockSet;
00221 typedef LirBlockSet::iterator LirBlockSet_p;
00222 
00223 
00224 // --------------------------------------------------------------------
00225 //   Node types
00226 // --------------------------------------------------------------------
00227 
00228 typedef enum {
00229   // expression nodes
00230   Const, Id, Binary, Unary, Cast, Comma, Ternary, Call, Initializer,
00231 
00232   // statement nodes 
00233   Label, Switch, Case, If, While, Do, For, Goto, Continue, 
00234   Break, Return, Block, 
00235 
00236   // type nodes 
00237   Prim, Tdef, Ptr, Array, Func, Struct, Union, Enum, sueSpec,
00238 
00239 
00240   // declaration node 
00241   Decl, 
00242 
00243   // GCC __attribute__ extension 
00244   Attrib, 
00245 
00246   // procedure def node
00247   Proc,
00248 
00249   // random text and preprocessor command node
00250   Text,
00251 
00252   // A statement that just consists of an expression
00253   Expr,
00254 
00255   // Special undeclared node
00256   Undeclared,
00257 
00258   // Translation unit
00259   Unit,
00260 
00261   // Indicates that the node is a meta-variable
00262   Meta,
00263 
00264   // An addressable memory location, assuming base is loaded
00265   Operand,
00266 
00267   // Condition for a dismantled ifNode
00268   Condition,
00269 
00270   // A three address node
00271   ThreeAddr,
00272 
00273 } NodeType;
00274 
00275 // A declarator has several context-dependent scoping options:
00276 
00277 typedef enum {
00278   Redecl,    // may redeclare a typdef name
00279   NoRedecl,  // may not redeclare a typedef name
00280   SU,        // is a structure or union field
00281   Formal     // is a formal parameter, goes into scope of the following body
00282              //  (storage class NOT extern, static, typedef, auto)
00283 } ScopeState;
00284 
00285 // -- Assoc is used in the output phase to control associativity
00286 // for operators and context for types.
00287 
00288 typedef enum { Right, Left } Assoc;
00289 
00290 // -- Inlcude other headers, including the AST
00291 
00292 #include "coord.h"
00293 #include "operators.h"
00294 #include "basic_type.h"
00295 #include "constant.h"
00296 #include "annote.h"
00297 #include "ast.h"
00298 #include "visitor.h"
00299 #include "walker.h"
00300 #include "changer.h"
00301 #include "dataflow.h"
00302 #include "symbol.h"
00303 #include "phase.h"
00304 #include "output_context.h"
00305 #include "arch_info.h"
00306 #include "instruction.h"
00307 
00308 // --- Map for node numberings
00309 
00310   typedef map<Node *,
00311           unsigned int,
00312           less< Node * > > numbering_map;
00313 
00314 // --- The CBZ class holds many of the global configuration values as
00315 // static data members.
00316 
00317 class CBZ {
00318 
00319 public:
00320 
00321   // -- The parsed program
00322   static unit_list Program;
00323 
00324   // -- Global list of all files
00325   static str_vec Files;
00326           
00327   // -- expected suffixes for input and output files 
00328   static string input_suffix ;
00329   static string output_suffix ;
00330 
00331   // -- preprocessor command lines 
00332   static string preproc;
00333   static string default_preproc ;
00334   static string gcc_preproc ;
00335   static string ansi_preproc  ;
00336 
00337   // -- Basic Global Variables 
00338 
00339   static float VersionNumber ;
00340   static string VersionDate ;
00341   static string Executable;
00342   static arch_info ArchInfo;
00343 
00344   // -- default warning level 
00345   static int WarningLevel ;
00346 
00347   // -- CPP flags
00348   static str_list * cpp_flags;
00349 
00350   // -- Parsing state
00351 
00352   static unitNode * current_unit;
00353   static int current_file;
00354   static int current_line;
00355   static int current_offset;
00356 
00357   static text_list pragmas;
00358 
00359   static void set_file(const char *name, int line, bool is_new_file);
00360 
00361   static bool is_file_included();
00362 
00363   static void char_to_text(char * array, unsigned char value);
00364 
00365   // -- Current phase in processing
00366 
00367   static string PhaseName;
00368 
00369   // -- Command Line Flags
00370 
00371   static bool QuietlyIgnore;
00372   static bool DebugLex;
00373   static bool PrintSymTables;
00374   static bool TrackInsertSymbol;
00375   static bool PrintLineOffset;
00376   static bool IgnoreLineDirectives;
00377   static bool ANSIOnly;
00378   static bool FormatReadably;
00379   static bool Preprocess;
00380   static bool ShowPreprocess;
00381   static bool GCCisms;
00382   static bool NoRegAlloc;
00383 
00384   // --- Flag old-style fdcls
00385 
00386   static bool OldStyleFunctionDefinition;
00387 
00388   // --- Errors, warnings, failures...
00389 
00390   static void Fail(const string file, int line, const string msg);
00391   static void SyntaxError(string s);
00392   static void Warning(int level, string s);
00393   static void SyntaxError(Coord c, string s);
00394   static void Warning(int level, Coord c, string s);
00395   static void WarnAboutPrecedence(unsigned int op_id, exprNode * the_expr);
00396   static void Error(Coord c, string s);
00397 
00398   // handy error reporting macro that lets you fail with a formatted message
00399   //
00400   // example usage:
00401   //
00402   //    CBZFAIL(("Don't know how to handle type %s in construct %s", type.c_str(), construct.c_str()));
00403   //
00404 #define CBZFAIL( errfmt ) \
00405   CBZ::Fail( __FILE__, __LINE__, cbz_util::string_format errfmt );
00406 
00407   // -- helper functions for creating temporary variables for various uses
00408 
00409   // just get a new identifier with a unique name
00410   static string get_temp_id_str();
00411 
00412   // get a new identifier node with a unique name
00413   static idNode * get_temp_id();
00414 
00415   // get a new variable with a unique name - its decl will be added to the given block
00416   static idNode * get_temp_var( blockNode * containing_block, typeNode * type );
00417 
00418 private:
00419 
00420   // integer used to generate temporary variable names
00421   static unsigned int next_temp_id_seed;
00422 
00423 };
00424 
00425 // -- Struct to hold parsing information about type qualifiers...
00426 
00427 struct _TQ {
00428   typeNode::Type_qualifiers tq;
00429   declNode::Storage_class sc;
00430   simpleCoord coord;
00431 };
00432 
00433 struct _TYPE {
00434   typeNode * ty;
00435   declNode::Storage_class sc;
00436 };
00437 
00438 
00439 #endif // CBZ_C_BREEZE_H

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