ucfun.h

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2000 The Exult Team
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifndef INCL_UCFUN
00026 #define INCL_UCFUN
00027 
00028 #include "ucsym.h"
00029 #include "uclabel.h"
00030 
00031 class Uc_location;
00032 class Uc_statement;
00033 #ifndef ALPHA_LINUX_CXX
00034 #  include <iosfwd>
00035 #endif
00036 
00037 /* 
00038  *  This represents a usecode function:
00039  */
00040 class Uc_function
00041   {
00042   static Uc_scope globals;  // For finding intrinsics, funs.
00043           // Intrinsics, indexed by number:
00044   static vector<Uc_intrinsic_symbol *> intrinsics;
00045           // Some intrinsic numbers:
00046   static int add_answer, remove_answer, push_answers, pop_answers,
00047     show_face, remove_face;
00048   static int num_global_statics;
00049   Uc_scope top;     // Top-level scope.
00050   Uc_function_symbol *proto;  // Function declaration.
00051   Uc_scope *cur_scope;    // Current scope.
00052   int num_parms;      // # parameters.
00053   int num_locals;     // Counts locals.
00054   int num_statics;    // Counts local statics.
00055           // Stack of loops (where 'break' can
00056           //   be used.
00057   vector<Uc_statement *> breakables;
00058   vector<int> breaks;   // Offsets of generated breaks in 
00059           //   current loop, with loops separated
00060           //   by -1's.
00061           // Links to called functions:
00062   std::vector<Uc_function_symbol *> links;
00063   std::map<std::string, Uc_label *> labels;
00064   char *text_data;    // All strings.
00065   int text_data_size;
00066           // Map string to its offset.
00067   std::map<std::string, int> text_map;
00068   Uc_statement *statement;  // Statement(s) in function.
00069 
00070   int reloffset; // relative offset of the code being generated
00071 public:
00072   Uc_function(Uc_function_symbol *p);
00073   ~Uc_function();
00074   enum Intrinsic_type
00075     {
00076     bg,     // Black gate.
00077     si      // Serpent isle.
00078     };
00079   static void set_intrinsics(Intrinsic_type ty);
00080   void set_statement(Uc_statement *s)
00081     { statement = s; }
00082   void adjust_reloffset(int diff) { reloffset += diff; }
00083   int get_reloffset() const { return reloffset; }
00084   void push_scope()   // Start a new scope.
00085     { cur_scope = cur_scope->add_scope(); }
00086   void pop_scope()    // End scope.
00087     {
00088     cur_scope = cur_scope->get_parent();
00089     }
00090   Uc_symbol *search(char *nm) // Search current scope.
00091     { return cur_scope->search(nm); }
00092   Uc_symbol *search_up(char *nm)
00093     { 
00094     Uc_symbol *sym = cur_scope->search_up(nm);
00095     return (sym ? sym : globals.search(nm));
00096     }
00097   static Uc_intrinsic_symbol *get_intrinsic(int i)
00098     { return (i >= 0 && i < intrinsics.size())? intrinsics[i] : 0;}
00099   static Uc_intrinsic_symbol *get_add_answer()
00100     { return get_intrinsic(add_answer); }
00101   static Uc_intrinsic_symbol *get_remove_answer()
00102     { return get_intrinsic(remove_answer); }
00103   static Uc_intrinsic_symbol *get_push_answers()
00104     { return get_intrinsic(push_answers); }
00105   static Uc_intrinsic_symbol *get_pop_answers()
00106     { return get_intrinsic(pop_answers); }
00107   static Uc_intrinsic_symbol *get_show_face()
00108     { return get_intrinsic(show_face); }
00109   static Uc_intrinsic_symbol *get_remove_face()
00110     { return get_intrinsic(remove_face); }
00111           // Already declared?
00112   static bool is_dup(Uc_scope *scope, char *nm);
00113   Uc_var_symbol *add_symbol(char *nm);// Add var. to current scope.
00114   void add_static(char *nm);  // Add static var. to current scope.
00115   int add_function_symbol(Uc_function_symbol *fun)
00116     { return cur_scope->add_function_symbol(fun); }
00117   static int add_global_function_symbol(Uc_function_symbol *fun)
00118     { return globals.add_function_symbol(fun); }
00119           // Add string constant.
00120   Uc_symbol *add_string_symbol(char *nm, char *text);
00121           // Add int constant.
00122   Uc_symbol *add_int_const_symbol(char *nm, int value);
00123   static Uc_symbol *add_global_int_const_symbol(char *nm, int val);
00124   static void add_global_static(char *nm);
00125   int add_string(char *text);
00126   int find_string_prefix(Uc_location& loc, const char *text);
00127           // Start/end loop.
00128   void add_label(Uc_label* l) { labels[l->get_name()] = l; }
00129   Uc_label *search_label(char *nm);
00130 
00131   void start_breakable(Uc_statement *s);
00132   void end_breakable(Uc_statement *s, vector<char>& stmt_code);
00133           // Store 'break' location.
00134   void add_break(int op_offset);  // DANGER:  Offset is filled in when
00135           //   end_breakable() is called, so the
00136           //   string this is in better not have
00137           //   been deleted!!!
00138           // Link external function.
00139   int link(Uc_function_symbol *fun);
00140   void link_labels(std::vector<char>& code);
00141   void gen(std::ostream& out);    // Generate Usecode.
00142   };
00143 
00144 #endif

Generated on Mon Jul 9 14:42:51 2007 for ExultEngine by  doxygen 1.5.1