C-Breeze
C Compiler Infrastructure

[ Project home page]

c_breeze.h

Go to the documentation of this file.
00001 // $Id: c_breeze.h,v 1.24 2003/11/11 19:23:14 benh 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 
00129 class Symbols;
00130 class Identifiers_table;
00131 class Tags_table;
00132 class Externals_table;
00133 
00134 class FlowVal;
00135 class FlowProblem;
00136 class Annote;
00137 
00138 // EDB
00139 using namespace std;
00140 
00141 typedef list< Node * > node_list;
00142 typedef node_list::iterator node_list_p;
00143 
00144 typedef list< exprNode * > expr_list;
00145 typedef expr_list::iterator expr_list_p;
00146 
00147 typedef list< declNode * > decl_list;
00148 typedef decl_list::iterator decl_list_p;
00149 
00150 typedef list< targetNode * > target_list;
00151 typedef target_list::iterator target_list_p;
00152 
00153 typedef list< attribNode * > attrib_list;
00154 typedef attrib_list::iterator attrib_list_p;
00155 
00156 typedef list< indexNode *> index_list;
00157 typedef index_list::iterator index_list_p;
00158 
00159 typedef list< idNode * > id_list;
00160 typedef id_list::iterator id_list_p;
00161 
00162 typedef list< stmtNode * > stmt_list;
00163 typedef stmt_list::iterator stmt_list_p;
00164 
00165 typedef list< gotoNode * > goto_list;
00166 typedef goto_list::iterator goto_list_p;
00167 
00168 typedef list< defNode * > def_list;
00169 typedef def_list::iterator def_list_p;
00170 
00171 typedef list< Annote * > annote_list;
00172 typedef annote_list::iterator annote_list_p;
00173 
00174 typedef list< unitNode * > unit_list;
00175 typedef unit_list::iterator unit_list_p;
00176 
00177 typedef vector< string > str_vec;
00178 typedef str_vec::iterator str_vec_p;
00179 
00180 typedef list< string > str_list;
00181 typedef str_list::iterator str_list_p;
00182 
00183 typedef list< textNode * > text_list;
00184 typedef text_list::iterator text_list_p;
00185 
00186 typedef list< suespecNode * > suespec_list;
00187 typedef suespec_list::iterator suespec_list_p;
00188 
00189 typedef list< blockNode * > block_list;
00190 typedef block_list::iterator block_list_p;
00191 
00192 typedef list< basicblockNode * > basicblock_list;
00193 typedef basicblock_list::iterator basicblock_list_p;
00194 
00195 typedef list< operandNode * > operand_list;
00196 typedef operand_list::iterator operand_list_p;
00197 
00198 class Visitor;
00199 class Walker;
00200 class Changer;
00201 
00202 class output_context;
00203 
00204 // --------------------------------------------------------------------
00205 //   Node types
00206 // --------------------------------------------------------------------
00207 
00208 typedef enum {
00209   // expression nodes
00210   Const, Id, Binary, Unary, Cast, Comma, Ternary, Call, Initializer,
00211 
00212   // statement nodes 
00213   Label, Switch, Case, If, While, Do, For, Goto, Continue, 
00214   Break, Return, Block, 
00215 
00216   // type nodes 
00217   Prim, Tdef, Ptr, Array, Func, Struct, Union, Enum, sueSpec,
00218 
00219 
00220   // declaration node 
00221   Decl, 
00222 
00223   // GCC __attribute__ extension 
00224   Attrib, 
00225 
00226   // procedure def node
00227   Proc,
00228 
00229   // random text and preprocessor command node
00230   Text,
00231 
00232   // A statement that just consists of an expression
00233   Expr,
00234 
00235   // Special undeclared node
00236   Undeclared,
00237 
00238   // Translation unit
00239   Unit,
00240 
00241   // Indicates that the node is a meta-variable
00242   Meta,
00243 
00244   // An addressable memory location, assuming base is loaded
00245   Operand,
00246 
00247   // Condition for a dismantled ifNode
00248   Condition,
00249 
00250   // A three address node
00251   ThreeAddr,
00252 
00253 } NodeType;
00254 
00255 // A declarator has several context-dependent scoping options:
00256 
00257 typedef enum {
00258   Redecl,    // may redeclare a typdef name
00259   NoRedecl,  // may not redeclare a typedef name
00260   SU,        // is a structure or union field
00261   Formal     // is a formal parameter, goes into scope of the following body
00262              //  (storage class NOT extern, static, typedef, auto)
00263 } ScopeState;
00264 
00265 // -- Assoc is used in the output phase to control associativity
00266 // for operators and context for types.
00267 
00268 typedef enum { Right, Left } Assoc;
00269 
00270 // -- Inlcude other headers, including the AST
00271 
00272 #include "coord.h"
00273 #include "operators.h"
00274 #include "basic_type.h"
00275 #include "constant.h"
00276 #include "annote.h"
00277 #include "ast.h"
00278 #include "visitor.h"
00279 #include "walker.h"
00280 #include "changer.h"
00281 #include "dataflow.h"
00282 #include "symbol.h"
00283 #include "phase.h"
00284 #include "output_context.h"
00285 
00286 // --- Map for node numberings
00287 
00288   typedef map<Node *,
00289           unsigned int,
00290           less< Node * > > numbering_map;
00291 
00292 // --- The CBZ class holds many of the global configuration values as
00293 // static data members.
00294 
00295 class CBZ {
00296 
00297 public:
00298 
00299   // -- The parsed program
00300   static unit_list Program;
00301 
00302   // -- Global list of all files
00303   static str_vec Files;
00304           
00305   // -- expected suffixes for input and output files 
00306   static string input_suffix ;
00307   static string output_suffix ;
00308   static string obj_suffix ;
00309 
00310   // -- preprocessor command lines 
00311   static string cc_cmd;
00312   static string preproc_flags;
00313   static string gcc_preproc_flags;
00314   static string ansi_preproc_flags;
00315 
00316   // -- Basic Global Variables 
00317 
00318   static float VersionNumber ;
00319   static string VersionDate ;
00320   static string Executable;
00321 
00322   // -- default warning level 
00323   static int WarningLevel ;
00324 
00325   // -- CPP flags
00326   static str_list * cpp_flags;
00327 
00328   // -- Parsing state
00329 
00330   static unitNode * current_unit;
00331   static int current_file;
00332   static int current_line;
00333   static int current_offset;
00334 
00335   static text_list pragmas;
00336 
00337   static void set_file(const char *name, int line, bool is_new_file);
00338 
00339   static bool is_file_included();
00340 
00341   static void char_to_text(char * array, unsigned char value);
00342 
00343   // -- Current phase in processing
00344 
00345   static string PhaseName;
00346 
00347   // -- Command Line Flags
00348 
00349   static bool QuietlyIgnore;
00350   static bool DebugLex;
00351   static bool PrintSymTables;
00352   static bool TrackInsertSymbol;
00353   static bool PrintLineOffset;
00354   static bool IgnoreLineDirectives;
00355   static bool ANSIOnly;
00356   static bool FormatReadably;
00357   static bool Preprocess;
00358   static bool ShowPreprocess;
00359   static bool GCCisms;
00360   static bool NoRegAlloc;
00361   static bool LeaveIncluded;
00362 
00363   // --- Flag old-style fdcls
00364 
00365   static bool OldStyleFunctionDefinition;
00366 
00367   // --- Errors, warnings, failures...
00368 
00369   static void Fail(const string file, int line, const string msg);
00370   static void SyntaxError(string s);
00371   static void Warning(int level, string s);
00372   static void SyntaxError(Coord c, string s);
00373   static void Warning(int level, Coord c, string s);
00374   static void WarnAboutPrecedence(unsigned int op_id, exprNode * the_expr);
00375   static void Error(Coord c, string s);
00376 
00377   // handy error reporting macro that lets you fail with a formatted message
00378   //
00379   // example usage:
00380   //
00381   //    CBZFAIL(("Don't know how to handle type %s in construct %s", type.c_str(), construct.c_str()));
00382   //
00383 #define CBZFAIL( errfmt ) \
00384   CBZ::Fail( __FILE__, __LINE__, cbz_util::string_format errfmt );
00385 
00386   // -- helper functions for creating temporary variables for various uses
00387 
00388   // just get a new identifier with a unique name
00389   static string get_temp_id_str();
00390 
00391   // get a new identifier node with a unique name
00392   static idNode * get_temp_id();
00393 
00394   // get a new variable with a unique name - its decl will be added to the given block
00395   static idNode * get_temp_var( blockNode * containing_block, typeNode * type );
00396 
00397 private:
00398 
00399   // integer used to generate temporary variable names
00400   static unsigned int next_temp_id_seed;
00401 
00402 };
00403 
00404 // -- Struct to hold parsing information about type qualifiers...
00405 
00406 struct _TQ {
00407   typeNode::Type_qualifiers tq;
00408   declNode::Storage_class sc;
00409   simpleCoord coord;
00410 };
00411 
00412 struct _TYPE {
00413   typeNode * ty;
00414   declNode::Storage_class sc;
00415 };
00416 
00417 
00418 #endif // CBZ_C_BREEZE_H

Generated on February 1, 2006
Back to the C-Breeze home page