00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __DUTABLES_H
00012 #define __DUTABLES_H
00013
00014 #include "common_types.hpp"
00015 #include "literal_storage.hpp"
00016 #include "flat_icblock.hpp"
00017 #include "labeled_icblock.hpp"
00018 #include "uid_gen.hpp"
00019
00020 #include <vector>
00021 #include <map>
00022 #include <limits>
00023
00024 namespace mlc {
00025
00026
00027
00028
00030 typedef mlaskal::lit_storage<int> ls_int_type;
00032 typedef mlaskal::lit_storage<double> ls_real_type;
00034 typedef mlaskal::lit_storage<std::string> ls_str_type;
00036 typedef mlaskal::lit_storage<std::string> ls_id_type;
00037
00039
00041
00043
00045
00046
00047
00048
00050
00051 typedef mlaskal::ICRELNUM stack_address;
00052
00054 typedef ls_int_type::const_iterator ls_int_index;
00056 typedef ls_real_type::const_iterator ls_real_index;
00058 typedef ls_str_type::const_iterator ls_str_index;
00060 typedef ls_id_type::const_iterator ls_id_index;
00061
00062
00063
00065 typedef mlaskal::labeled_icblock::label_type ic_label;
00067 typedef mlaskal::labeled_icblock * icblock_pointer;
00068
00070 inline icblock_pointer icblock_create()
00071 {
00072 return new mlaskal::labeled_icblock;
00073 }
00074
00076
00077 inline void icblock_append_delete( icblock_pointer dicb, icblock_pointer sicb)
00078 {
00079 dicb->append_clear_block( * sicb);
00080 delete sicb;
00081 }
00082
00084 typedef mlaskal::abstract_functions::iterator ic_function_pointer;
00085
00086
00087
00088 class symbol_tables;
00089
00090
00091
00093 enum parameter_mode {
00094 PMODE_UNDEF,
00095 PMODE_BY_VALUE,
00096 PMODE_BY_REFERENCE
00097 };
00098
00099
00100
00101 class abstract_type;
00103 typedef mlaskal::dumb_pointer< abstract_type> type_pointer;
00104
00105
00106
00107 class type_entry;
00109 typedef std::list< type_entry> type_vector;
00110
00111
00112
00114 enum type_category {
00115 TCAT_UNDEF,
00116 TCAT_ARRAY,
00117 TCAT_RECORD,
00118 TCAT_BOOL,
00119 TCAT_RANGE,
00120 TCAT_INT,
00121 TCAT_REAL,
00122 TCAT_STR
00123 };
00124
00126 std::ostream &operator<<(std::ostream &os, type_category typecat);
00127
00128 class array_type;
00129 class record_type;
00130 class int_type;
00131 class range_type;
00132 class bool_type;
00133 class real_type;
00134 class str_type;
00135
00137 typedef mlaskal::dumb_pointer< array_type> array_type_pointer;
00139 typedef mlaskal::dumb_pointer< record_type> record_type_pointer;
00141 typedef mlaskal::dumb_pointer< int_type> int_type_pointer;
00143 typedef mlaskal::dumb_pointer< range_type> range_type_pointer;
00145 typedef mlaskal::dumb_pointer< bool_type> bool_type_pointer;
00147 typedef mlaskal::dumb_pointer< real_type> real_type_pointer;
00149 typedef mlaskal::dumb_pointer< str_type> str_type_pointer;
00150
00151 class DumpUniquizer;
00152
00154 class abstract_type {
00155 public:
00157 virtual ~abstract_type() {}
00159 type_category cat() const { return this ? cat_() : TCAT_UNDEF; }
00161 array_type_pointer access_array() { return this ? arrayType_() : array_type_pointer(); }
00163 record_type_pointer access_record() { return this ? recordType_() : record_type_pointer(); }
00165 int_type_pointer access_int() { return this ? integerType_() : int_type_pointer(); }
00167 range_type_pointer access_range() { return this ? rangeType_() : range_type_pointer(); }
00169 bool_type_pointer access_bool() { return this ? booleanType_() : bool_type_pointer(); }
00171 real_type_pointer access_real() { return this ? realType_() : real_type_pointer(); }
00173 str_type_pointer access_str() { return this ? stringType_() : str_type_pointer(); }
00175
00176 stack_address compute_size() { return this ? compute_size_() : 0; }
00178
00182 mlaskal::physical_type compute_final_ptype() { return this ? compute_final_ptype_() : mlaskal::PTYPE_UNDEF; }
00183 private:
00184 size_t get_idx() const { return this ? get_idx_() : std::numeric_limits< size_t>::max(); }
00185 virtual type_category cat_() const = 0;
00186 virtual array_type_pointer arrayType_() = 0;
00187 virtual record_type_pointer recordType_() = 0;
00188 virtual int_type_pointer integerType_() = 0;
00189 virtual range_type_pointer rangeType_() = 0;
00190 virtual bool_type_pointer booleanType_() = 0;
00191 virtual real_type_pointer realType_() = 0;
00192 virtual str_type_pointer stringType_() = 0;
00193 virtual stack_address compute_size_() = 0;
00194 virtual mlaskal::physical_type compute_final_ptype_() = 0;
00195 virtual size_t get_idx_() const = 0;
00196 virtual const type_entry * access_entry() const = 0;
00197 friend std::ostream & operator<<( std::ostream & o, type_pointer e);
00198 friend bool identical_type( type_pointer a, type_pointer b);
00199 friend class DumpUniquizer;
00200 friend class type_entry;
00201 };
00202
00204 class array_type : public virtual abstract_type {
00205 public:
00206 virtual ~array_type() {}
00208 type_pointer element_type() const { return this ? elementType_() : type_pointer(); }
00210 type_pointer index_type() const { return this ? indexType_() : type_pointer(); }
00211 private:
00212 virtual type_pointer elementType_() const = 0;
00213 virtual type_pointer indexType_() const = 0;
00214 };
00215
00216 class field_entry;
00217
00219 typedef mlaskal::dumb_pointer< const field_entry> field_pointer;
00220
00222 typedef std::vector< field_entry> field_vector;
00223
00225 class record_type : public virtual abstract_type {
00226 public:
00227 virtual ~record_type() {}
00228
00229 field_pointer find( ls_id_index idx) const { return this ? record_find_( idx) : field_pointer(); }
00230
00232 typedef field_vector::const_iterator const_iterator;
00234 const_iterator begin() const { return record_begin_(); }
00236 const_iterator end() const { return record_end_(); }
00237
00238 private:
00239 virtual field_pointer record_find_( ls_id_index idx) const = 0;
00240 virtual field_vector::const_iterator record_begin_() const = 0;
00241 virtual field_vector::const_iterator record_end_() const = 0;
00242 };
00243
00245 class integral_type : public virtual abstract_type {
00246 public:
00247 virtual ~integral_type() {}
00248 };
00249
00251 class int_type : public virtual integral_type {
00252 public:
00253 virtual ~int_type() {}
00254 };
00255
00257 class range_type : public virtual integral_type {
00258 public:
00259 virtual ~range_type() {}
00260
00261 ls_int_index lowerBound() const { return this ? lowerBound_() : ls_int_index(); }
00262
00263 ls_int_index upperBound() const { return this ? upperBound_() : ls_int_index(); }
00264 private:
00265 virtual ls_int_index lowerBound_() const = 0;
00266 virtual ls_int_index upperBound_() const = 0;
00267 };
00268
00270 class bool_type : public virtual abstract_type {
00271 public:
00272 virtual ~bool_type() {}
00273 };
00274
00276 class real_type : public virtual abstract_type {
00277 public:
00278 virtual ~real_type() {}
00279 };
00280
00282 class str_type : public virtual abstract_type {
00283 public:
00284 virtual ~str_type() {}
00285 };
00286
00287 class field_list;
00288
00290 template< typename T>
00291 class ls_index_comparator {
00292 public:
00293 bool operator() (
00294 typename mlaskal::lit_storage< T>::const_iterator a,
00295 typename mlaskal::lit_storage< T>::const_iterator b) const
00296 {
00297 return * a < * b;
00298 }
00299 };
00300
00302 typedef std::map< ls_id_index, const field_entry *, ls_index_comparator< std::string> > record_cache_type;
00303
00305 class type_entry :
00306 public virtual array_type,
00307 public virtual record_type,
00308 public virtual int_type,
00309 public virtual range_type,
00310 public virtual bool_type,
00311 public virtual real_type,
00312 public virtual str_type
00313 {
00314 private:
00315 type_entry();
00316 type_entry( size_t idx, type_category p);
00317 type_entry( size_t idx, type_category p, field_list * fldlist);
00318 type_entry( size_t idx, type_category p, type_pointer t1, type_pointer t2);
00319 type_entry( size_t idx, type_category p, type_pointer t1, type_pointer t2, ls_int_index s1, ls_int_index s2);
00320 size_t idx_;
00321 type_category typecat;
00322 type_pointer ltype1;
00323 type_pointer ltype2;
00324 ls_int_index idx1;
00325 ls_int_index idx2;
00326 field_list * fldlist;
00327 record_cache_type record_cache;
00328
00329 virtual type_category cat_() const;
00330 virtual array_type_pointer arrayType_();
00331 virtual record_type_pointer recordType_();
00332 virtual int_type_pointer integerType_();
00333 virtual range_type_pointer rangeType_();
00334 virtual bool_type_pointer booleanType_();
00335 virtual real_type_pointer realType_();
00336 virtual str_type_pointer stringType_();
00337 virtual type_pointer elementType_() const;
00338 virtual type_pointer indexType_() const;
00339 virtual ls_int_index lowerBound_() const;
00340 virtual ls_int_index upperBound_() const;
00341 virtual size_t get_idx_() const;
00342 virtual const type_entry * access_entry() const;
00343 virtual stack_address compute_size_();
00344 virtual mlaskal::physical_type compute_final_ptype_();
00345 virtual field_pointer record_find_( ls_id_index idx) const;
00346 virtual field_vector::const_iterator record_begin_() const;
00347 virtual field_vector::const_iterator record_end_() const;
00348 virtual void fill_record_cache( int line);
00349
00350 friend class symbol_tables;
00351 friend class DumpUniquizer;
00352 std::string operator()( std::string & name, std::ostream & oo, DumpUniquizer & uniq) const;
00353 friend std::ostream & operator<<( std::ostream & o, const type_entry & e);
00354 };
00355
00357 std::ostream & operator<<( std::ostream & o, const type_entry & e);
00358
00359
00360
00362
00364 class field_entry {
00365 public:
00366 type_pointer type() const
00367 {
00368 return this ? ltype : type_pointer();
00369 }
00370 stack_address offset() const
00371 {
00372 return this ? offset_ : 0;
00373 }
00374 private:
00376 field_entry( ls_id_index i, type_pointer p);
00378 ls_id_index idx;
00380 type_pointer ltype;
00382 stack_address offset_;
00383
00384 std::string operator()( std::ostream & oo, DumpUniquizer & uniq) const;
00385
00386 friend class type_entry;
00387 friend class field_list;
00388 friend struct flftr;
00389
00390 friend std::ostream & operator<<( std::ostream & o, const field_entry & e);
00391 };
00392
00394 std::ostream & operator<<( std::ostream & o, const field_entry & e);
00395
00397 typedef std::vector< field_entry> field_vector;
00398
00400 class field_list {
00401 public:
00403 typedef field_vector::const_iterator const_iterator;
00405 const_iterator begin() const;
00407 const_iterator end() const;
00409 void append_field( ls_id_index idx, type_pointer ltype);
00411
00412 void append_and_kill( field_list * ll2);
00413
00415 field_vector::size_type size() const;
00416
00417 private:
00418 void push_back( const field_entry & v);
00419 void append( const field_list & l2);
00420 void assign_offsets();
00421 std::string operator()( std::ostream & oo, DumpUniquizer & uniq) const;
00422
00423 field_vector v_;
00424 stack_address total_size_;
00425 friend std::ostream & operator<<( std::ostream & o, const field_list & e);
00426 friend class symbol_tables;
00427 friend class type_entry;
00428 };
00429
00430
00431
00433
00437 struct parameter_entry {
00439 parameter_entry( parameter_mode s, ls_id_index i, type_pointer p);
00441 parameter_mode partype;
00443 ls_id_index idx;
00445 type_pointer ltype;
00446
00447 std::string operator()( std::ostream & oo, DumpUniquizer & uniq) const;
00448 };
00449
00451 std::ostream & operator<<( std::ostream & o, const parameter_entry & e);
00452
00454 typedef std::vector< parameter_entry> parameter_vector;
00455
00456 class symbol_entry;
00457
00459 class parameter_list {
00460 public:
00462 typedef parameter_vector::const_iterator const_iterator;
00464 const_iterator begin() const;
00466 const_iterator end() const;
00468 void append_parameter_by_value( ls_id_index idx, type_pointer ltype);
00470 void append_parameter_by_reference( ls_id_index idx, type_pointer ltype);
00472
00473 void append_and_kill( parameter_list * ll2);
00474
00476 parameter_vector::size_type size() const;
00477
00478 private:
00479 void push_back( const parameter_entry & v);
00480 void append( const parameter_list & l2);
00481 std::string operator()( std::ostream & oo, DumpUniquizer & uniq) const;
00482
00483 parameter_vector v_;
00484 friend std::ostream & operator<<( std::ostream & o, const parameter_list & e);
00485 friend class symbol_entry;
00486 };
00487
00489 std::ostream & operator<<( std::ostream & o, const parameter_list & e);
00490
00491
00492
00494 enum symbol_kind {
00495 SKIND_UNDEF,
00496 SKIND_PROCEDURE,
00497 SKIND_FUNCTION,
00498 SKIND_GLOBAL_VARIABLE,
00499 SKIND_LOCAL_VARIABLE,
00500 SKIND_PARAMETER_BY_REFERENCE,
00501 SKIND_TYPE,
00502 SKIND_CONST
00503 };
00504
00505 class typed_symbol;
00506 class variable_symbol;
00507 class subprogram_symbol;
00508 class procedure_symbol;
00509 class function_symbol;
00510 class global_variable_symbol;
00511 class local_variable_symbol;
00512 class parameter_by_reference_symbol;
00513 class type_symbol;
00514 class const_symbol;
00515
00517 typedef mlaskal::dumb_pointer< typed_symbol> typed_symbol_pointer;
00519 typedef mlaskal::dumb_pointer< variable_symbol> variable_symbol_pointer;
00521 typedef mlaskal::dumb_pointer< subprogram_symbol> subprogram_symbol_pointer;
00523 typedef mlaskal::dumb_pointer< procedure_symbol> procedure_symbol_pointer;
00525 typedef mlaskal::dumb_pointer< function_symbol> function_symbol_pointer;
00527 typedef mlaskal::dumb_pointer< global_variable_symbol> global_variable_symbol_pointer;
00529 typedef mlaskal::dumb_pointer< local_variable_symbol> local_variable_symbol_pointer;
00531 typedef mlaskal::dumb_pointer< parameter_by_reference_symbol> parameter_by_reference_symbol_pointer;
00533 typedef mlaskal::dumb_pointer< type_symbol> type_symbol_reference;
00535 typedef mlaskal::dumb_pointer< const_symbol> const_symbol_reference;
00536
00538 class abstract_symbol
00539 {
00540 public:
00541 virtual ~abstract_symbol() {}
00543 symbol_kind kind() const { return this ? kind_() : SKIND_UNDEF; }
00545 typed_symbol_pointer access_typed() { return this ? typedSymbol_() : typed_symbol_pointer(); }
00547 variable_symbol_pointer access_variable() { return this ? variableSymbol_() : variable_symbol_pointer(); }
00549 subprogram_symbol_pointer access_subprogram() { return this ? subprogramSymbol_() : subprogram_symbol_pointer(); }
00551 procedure_symbol_pointer access_procedure() { return this ? procSymbol_() : procedure_symbol_pointer(); }
00553 function_symbol_pointer access_function() { return this ? fncSymbol_() : function_symbol_pointer(); }
00555 global_variable_symbol_pointer access_global_variable() { return this ? gVarSymbol_() : global_variable_symbol_pointer(); }
00557 local_variable_symbol_pointer access_local_variable() { return this ? lVarSymbol_() : local_variable_symbol_pointer(); }
00559 parameter_by_reference_symbol_pointer access_parameter_by_reference() { return this ? varParSymbol_() : parameter_by_reference_symbol_pointer(); }
00561 type_symbol_reference access_type() { return this ? typeSymbol_() : type_symbol_reference(); }
00563 const_symbol_reference access_const() { return this ? constSymbol_() : const_symbol_reference(); }
00564 private:
00565 virtual symbol_kind kind_() const = 0;
00566 virtual typed_symbol_pointer typedSymbol_() = 0;
00567 virtual variable_symbol_pointer variableSymbol_() = 0;
00568 virtual subprogram_symbol_pointer subprogramSymbol_() = 0;
00569 virtual procedure_symbol_pointer procSymbol_() = 0;
00570 virtual function_symbol_pointer fncSymbol_() = 0;
00571 virtual global_variable_symbol_pointer gVarSymbol_() = 0;
00572 virtual local_variable_symbol_pointer lVarSymbol_() = 0;
00573 virtual parameter_by_reference_symbol_pointer varParSymbol_() = 0;
00574 virtual type_symbol_reference typeSymbol_() = 0;
00575 virtual const_symbol_reference constSymbol_() = 0;
00576 };
00577
00579 typedef mlaskal::dumb_pointer< abstract_symbol> symbol_pointer;
00580
00582 class subprogram_symbol
00583 : public virtual abstract_symbol
00584 {
00585 public:
00586 virtual ~subprogram_symbol() {}
00588 const parameter_list * parameters() const { return this ? parList_() : 0; }
00590 ic_function_pointer code() const { return this ? label_() : ic_function_pointer(); }
00591 private:
00592 virtual const parameter_list * parList_() const = 0;
00593 virtual ic_function_pointer label_() const = 0;
00594 };
00595
00597 class typed_symbol
00598 : public virtual abstract_symbol
00599 {
00600 public:
00601 virtual ~typed_symbol() {}
00603 type_pointer type() const { return this ? type_() : type_pointer(); }
00604 private:
00605 virtual type_pointer type_() const = 0;
00606 };
00607
00609 class procedure_symbol
00610 : public virtual subprogram_symbol
00611 {
00612 public:
00613 virtual ~procedure_symbol() {}
00614 };
00615
00617 class function_symbol
00618 : public virtual subprogram_symbol,
00619 public virtual typed_symbol
00620 {
00621 public:
00622 virtual ~function_symbol() {}
00623 };
00624
00626 class variable_symbol
00627 : public virtual typed_symbol
00628 {
00629 public:
00630 virtual ~variable_symbol() {}
00632 virtual stack_address address() const { return this ? varOffset_() : 0; }
00633 private:
00634 virtual stack_address varOffset_() const = 0;
00635 };
00636
00638 class global_variable_symbol
00639 : public virtual variable_symbol
00640 {
00641 public:
00642 virtual ~global_variable_symbol() {}
00643 };
00644
00646
00647 class local_variable_symbol
00648 : public virtual variable_symbol
00649 {
00650 public:
00651 virtual ~local_variable_symbol() {}
00652 };
00653
00655 class parameter_by_reference_symbol
00656 : public virtual variable_symbol
00657 {
00658 public:
00659 virtual ~parameter_by_reference_symbol() {}
00660 };
00661
00663 class type_symbol
00664 : public virtual typed_symbol
00665 {
00666 public:
00667 virtual ~type_symbol() {}
00668 };
00669
00670 class bool_const_symbol;
00671 class int_const_symbol;
00672 class real_const_symbol;
00673 class str_const_symbol;
00674
00676 typedef mlaskal::dumb_pointer< bool_const_symbol> bool_const_symbol_pointer;
00678 typedef mlaskal::dumb_pointer< int_const_symbol> int_const_symbol_pointer;
00680 typedef mlaskal::dumb_pointer< real_const_symbol> real_const_symbol_pointer;
00682 typedef mlaskal::dumb_pointer< str_const_symbol> str_const_symbol_pointer;
00683
00685 class const_symbol
00686 : public virtual typed_symbol
00687 {
00688 public:
00689 virtual ~const_symbol() {}
00690
00692 bool_const_symbol_pointer access_bool_const() { return this ? boolConstSymbol_() : bool_const_symbol_pointer(); }
00694 int_const_symbol_pointer access_int_const() { return this ? integerConstSymbol_() : int_const_symbol_pointer(); }
00696 real_const_symbol_pointer access_real_const() { return this ? realConstSymbol_() : real_const_symbol_pointer(); }
00698 str_const_symbol_pointer access_str_const() { return this ? stringConstSymbol_() : str_const_symbol_pointer(); }
00699 private:
00700 virtual bool_const_symbol_pointer boolConstSymbol_() = 0;
00701 virtual int_const_symbol_pointer integerConstSymbol_() = 0;
00702 virtual real_const_symbol_pointer realConstSymbol_() = 0;
00703 virtual str_const_symbol_pointer stringConstSymbol_() = 0;
00704 };
00705
00707
00708 class bool_const_symbol
00709 : public virtual const_symbol
00710 {
00711 public:
00712 virtual ~bool_const_symbol() {}
00713 bool bool_value() const { return this ? boolValue_() : false; }
00714 private:
00715 virtual bool boolValue_() const = 0;
00716 };
00717
00719 class int_const_symbol
00720 : public virtual const_symbol
00721 {
00722 public:
00723 virtual ~int_const_symbol() {}
00724 ls_int_index int_value() const { return this ? integerValue_() : ls_int_index(); }
00725 private:
00726 virtual ls_int_index integerValue_() const = 0;
00727 };
00728
00730 class real_const_symbol
00731 : public virtual const_symbol
00732 {
00733 public:
00734 virtual ~real_const_symbol() {}
00735 ls_real_index real_value() const { return this ? realValue_() : ls_real_index(); }
00736 private:
00737 virtual ls_real_index realValue_() const = 0;
00738 };
00739
00741 class str_const_symbol
00742 : public virtual const_symbol
00743 {
00744 public:
00745 virtual ~str_const_symbol() {}
00746 ls_str_index str_value() const { return this ? stringValue_() : ls_str_index(); }
00747 private:
00748 virtual ls_str_index stringValue_() const = 0;
00749 };
00750
00751
00752
00753 class local_symbol_tables;
00754 class symbol_entry;
00755 class label_entry;
00756
00758 typedef std::map< ls_id_index, symbol_entry, ls_index_comparator< std::string> > symbol_map;
00760 typedef std::map< ls_int_index, label_entry, ls_index_comparator< int> > label_map;
00761
00763 class symbol_entry
00764 : public virtual procedure_symbol,
00765 public virtual function_symbol,
00766 public virtual global_variable_symbol,
00767 public virtual local_variable_symbol,
00768 public virtual parameter_by_reference_symbol,
00769 public virtual type_symbol,
00770 public virtual bool_const_symbol,
00771 public virtual int_const_symbol,
00772 public virtual real_const_symbol,
00773 public virtual str_const_symbol
00774 {
00775 private:
00776 symbol_entry( symbol_kind s, stack_address i, type_pointer p, const ic_function_pointer & m, local_symbol_tables * lt)
00777 : symtype( s), value( false), relnum_( i), ltype( p), parlist( 0), magic( m), epilogue_( icblock_create()), local_tables_( lt)
00778 {
00779 }
00780 symbol_entry( symbol_kind s, stack_address i, type_pointer p, ic_label bl, ic_label el)
00781 : symtype( s), value( false), relnum_( i), ltype( p), parlist( 0), magic(), epilogue_( 0), local_tables_( 0),
00782 icip_begin_label( bl), icip_end_label( el)
00783 {
00784 }
00785 symbol_entry( symbol_kind s, bool i, type_pointer p)
00786 : symtype( s), value( i), relnum_( -1), ltype( p), parlist( 0), magic(), epilogue_( 0), local_tables_( 0)
00787 {
00788 }
00789 symbol_entry( symbol_kind s, ls_int_index i, type_pointer p)
00790 : symtype( s), value( false), intidx( i), relnum_( -1), ltype( p), parlist( 0), magic(), epilogue_( 0), local_tables_( 0)
00791 {
00792 }
00793 symbol_entry( symbol_kind s, ls_real_index i, type_pointer p)
00794 : symtype( s), value( false), realidx( i), relnum_( -1), ltype( p), parlist( 0), magic(), epilogue_( 0), local_tables_( 0)
00795 {
00796 }
00797 symbol_entry( symbol_kind s, ls_str_index i, type_pointer p)
00798 : symtype( s), value( false), stridx( i), relnum_( -1), ltype( p), parlist( 0), magic(), epilogue_( 0), local_tables_( 0)
00799 {
00800 }
00801 symbol_kind symtype;
00802 bool value;
00803 ls_int_index intidx;
00804 ls_real_index realidx;
00805 ls_str_index stridx;
00806 stack_address relnum_;
00807 type_pointer ltype;
00808 parameter_list * parlist;
00809 ic_function_pointer magic;
00810 mlaskal::abstract_function_vars local_vars_;
00811 icblock_pointer epilogue_;
00812 local_symbol_tables * local_tables_;
00813 ic_label icip_begin_label;
00814 ic_label icip_end_label;
00815 virtual symbol_kind kind_() const;
00816 virtual typed_symbol_pointer typedSymbol_();
00817 virtual variable_symbol_pointer variableSymbol_();
00818 virtual subprogram_symbol_pointer subprogramSymbol_();
00819 virtual procedure_symbol_pointer procSymbol_();
00820 virtual function_symbol_pointer fncSymbol_();
00821 virtual global_variable_symbol_pointer gVarSymbol_();
00822 virtual local_variable_symbol_pointer lVarSymbol_();
00823 virtual parameter_by_reference_symbol_pointer varParSymbol_();
00824 virtual type_symbol_reference typeSymbol_();
00825 virtual const_symbol_reference constSymbol_();
00826 virtual const parameter_list * parList_() const;
00827 virtual type_pointer type_() const;
00828 virtual stack_address varOffset_() const;
00829 virtual bool_const_symbol_pointer boolConstSymbol_();
00830 virtual int_const_symbol_pointer integerConstSymbol_();
00831 virtual real_const_symbol_pointer realConstSymbol_();
00832 virtual str_const_symbol_pointer stringConstSymbol_();
00833 virtual bool boolValue_() const;
00834 virtual ls_int_index integerValue_() const;
00835 virtual ls_real_index realValue_() const;
00836 virtual ls_str_index stringValue_() const;
00837 virtual ic_function_pointer label_() const;
00838 std::string operator()( const std::string & indent, const std::string & name, std::ostream & o, DumpUniquizer & uniq) const;
00839
00840 void kill();
00841
00842 friend void kill_symbol( symbol_map::value_type & e);
00843 friend class symbol_tables;
00844 friend class local_symbol_tables;
00845 friend std::ostream & operator<<( std::ostream & o, const symbol_entry & e);
00846 };
00847
00848
00849
00851 class label_symbol {
00852 public:
00853 virtual ~label_symbol() {}
00855 virtual ic_label label() const = 0;
00857 virtual void goto_found( int line) = 0;
00859 virtual bool label_found( int line) = 0;
00860 };
00861
00863 typedef mlaskal::dumb_pointer< label_symbol> label_symbol_pointer;
00864
00865
00866
00867 struct check_label;
00868
00870 class label_entry
00871 : public virtual label_symbol
00872 {
00873 private:
00874 label_entry( int ln, const ls_int_index & i, const ic_label & l)
00875 : id( i), magic( l), have_goto( false), have_label( false), decl_line( ln), goto_line( 0), label_line( 0) {}
00876 ls_int_index id;
00877 ic_label magic;
00878 bool have_goto, have_label;
00879 int decl_line, goto_line, label_line;
00880 virtual ic_label label() const;
00881 virtual void goto_found( int line);
00882 virtual bool label_found( int line);
00883 void check();
00884
00885 friend class symbol_tables;
00886 friend struct check_label;
00887 };
00888
00889
00890
00891 class local_symbol_tables {
00892 private:
00893 local_symbol_tables();
00894 ~local_symbol_tables();
00895 std::string dump_locals( std::ostream & oo, DumpUniquizer & uniq) const;
00896 std::ostream & dump_llabel( std::ostream & o, const label_map::const_iterator & lit, std::ostream & oo, DumpUniquizer & uniq) const;
00897 std::ostream & dump_local( std::ostream & o, const symbol_map::const_iterator & sit, std::ostream & oo, DumpUniquizer & uniq) const;
00898
00899 label_map llabels;
00900 symbol_map locals;
00901 stack_address lvarsize;
00902 stack_address retval_;
00903
00904 friend class symbol_tables;
00905 friend class symbol_entry;
00906 };
00907
00908
00909
00910 struct MlaskalCtx;
00911
00913 class symbol_tables {
00914 public:
00915
00916
00917
00919 ls_int_type & ls_int() { return aic_->get_ls_int(); }
00921 ls_real_type & ls_real() { return aic_->get_ls_real(); }
00923 ls_str_type & ls_str() { return aic_->get_ls_string(); }
00925 ls_id_type & ls_id() { return aic_->get_ls_id(); }
00926
00927
00929
00930 type_pointer create_array_type( type_pointer index_type, type_pointer element_type);
00931
00933
00934 type_pointer create_record_type( field_list * fldlist, int line = 0);
00935
00937
00938 type_pointer create_range_type( ls_int_index i1, ls_int_index i2);
00939
00941
00942 label_symbol_pointer find_label( ls_int_index idx);
00943
00945
00949 variable_symbol_pointer add_var( int line, ls_id_index idx, type_pointer ltype);
00950
00952
00956 bool_const_symbol_pointer add_const_bool( int line, ls_id_index idx, bool val);
00957
00959
00963 int_const_symbol_pointer add_const_int( int line, ls_id_index idx, ls_int_index symidx);
00964
00966
00970 real_const_symbol_pointer add_const_real( int line, ls_id_index idx, ls_real_index symidx);
00971
00973
00977 str_const_symbol_pointer add_const_str( int line, ls_id_index idx, ls_str_index symidx);
00978
00980
00984 type_symbol_reference add_type( int line, ls_id_index idx, type_pointer ltype);
00985
00987
00995 label_symbol_pointer add_label_entry( int line, ls_int_index idx, ic_label lbl);
00996
00998
01002 function_symbol_pointer add_fnc( int line, ls_id_index idx, type_pointer ltype, parameter_list * parlist);
01003
01005
01009 procedure_symbol_pointer add_proc( int line, ls_id_index idx, parameter_list * parlist);
01010
01012
01017 bool set_subprogram_code( ls_id_index idx, icblock_pointer icb);
01018
01020
01025 bool set_main_code( ls_id_index idx, icblock_pointer icb);
01026
01028
01035 bool enter( int line, ls_id_index idx);
01036
01038
01043 void leave( int line);
01044
01046
01048 ls_id_index my_function_name() const;
01049
01051
01053 mlc::ls_int_type::const_iterator one();
01054
01056
01058 stack_address my_return_address();
01059
01061
01063 type_pointer logical_undef();
01064
01066
01068 type_pointer logical_bool();
01069
01071
01073 type_pointer logical_integer();
01074
01076
01078 type_pointer logical_real();
01079
01081
01083 type_pointer logical_string();
01084
01086
01088 bool nested() const;
01089
01091
01097 symbol_pointer find_symbol( ls_id_index symidx);
01098
01099 private:
01100 label_map & llabels() { if ( nested_ ) return my_function_entry()->local_tables_->llabels; return glabels; }
01101 const label_map & llabels() const { if ( nested_ ) return my_function_entry()->local_tables_->llabels; return glabels; }
01102 symbol_map & locals() { if ( nested_ ) return my_function_entry()->local_tables_->locals; return globals; }
01103 const symbol_map & locals() const { if ( nested_ ) return my_function_entry()->local_tables_->locals; return globals; }
01104 stack_address & lvarsize() { if ( nested_ ) return my_function_entry()->local_tables_->lvarsize; return gvarsize; }
01105 const stack_address & lvarsize() const { if ( nested_ ) return my_function_entry()->local_tables_->lvarsize; return gvarsize; }
01106 stack_address & retval_() { return my_function_entry()->local_tables_->retval_; }
01107 const stack_address & retval_() const { return my_function_entry()->local_tables_->retval_; }
01108
01109 const symbol_entry * my_function_entry() const;
01110 symbol_entry * my_function_entry();
01111
01112 type_vector types;
01113 label_map glabels;
01114 symbol_map globals;
01115 bool nested_;
01116 stack_address gvarsize;
01117 ls_id_index myfncname_;
01118 mlc::ls_int_type::const_iterator one_;
01119 type_pointer bool_;
01120 type_pointer integer_;
01121 type_pointer real_;
01122 type_pointer string_;
01123 type_pointer undef_;
01124 bool debug_;
01125 mlaskal::abstract_ic * aic_;
01126 mlaskal::abstract_function_vars global_vars_;
01127 mlaskal::tr1::shared_ptr< mlaskal::labeled_icblock> global_block_;
01128 icblock_pointer global_epilogue_;
01129
01130 type_pointer create_atomic_type( type_category ptype);
01131
01132 ic_function_pointer my_function_code();
01133
01134 symbol_tables( bool debug, mlaskal::abstract_ic * aic);
01135
01136 void dump_enter( ls_id_index idx) const;
01137
01138 void dump_leave() const;
01139
01140 void dump_builtins() const;
01141
01142 void dump_main() const;
01143
01144 void dump_type( const type_vector::const_iterator & tit) const;
01145
01146 void dump_glabel( const label_map::const_iterator & lit) const;
01147
01148 void dump_global( const symbol_map::const_iterator & sit) const;
01149
01150 void dump_llabel( const label_map::const_iterator & lit) const;
01151
01152 void dump_local( const symbol_map::const_iterator & sit) const;
01153
01154 std::ostream & dump_type( std::ostream & o, const type_vector::const_iterator & tit) const;
01155
01156 std::ostream & dump_glabel( std::ostream & o, const label_map::const_iterator & lit) const;
01157
01158 std::ostream & dump_glabel( std::ostream & o, const label_map::const_iterator & lit, std::ostream & oo, DumpUniquizer & uniq) const;
01159
01160 std::ostream & dump_global( std::ostream & o, const symbol_map::const_iterator & sit) const;
01161
01162 std::ostream & dump_global( std::ostream & o, const symbol_map::const_iterator & sit, std::ostream & oo, DumpUniquizer & uniq) const;
01163
01164 std::ostream & dump_llabel( std::ostream & o, const label_map::const_iterator & lit) const;
01165
01166 std::ostream & dump_local( std::ostream & o, const symbol_map::const_iterator & sit) const;
01167
01168 std::ostream & dump( std::ostream & o) const;
01169
01170 void init_builtins();
01171
01172 ~symbol_tables();
01173
01174 friend int symtab_preprocess( MlaskalCtx *ctx, bool debug);
01175 friend int symtab_postprocess(struct MlaskalCtx *ctx, const std::string & dump_fname);
01176 };
01177
01178
01179
01181 parameter_list * create_parameter_list();
01182
01184 field_list * create_field_list();
01185
01187
01188 bool identical_type( type_pointer a, type_pointer b);
01189
01190
01191
01192 };
01193
01194 #endif