source: to-imperative/trunk/runtime/rf_term.hh @ 723

Last change on this file since 723 was 723, checked in by orlov, 18 years ago
  • Reworked Char::to_chars() method.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1//-----------------------------------------------------------------------------
2/// @file rf_term.hh
3///
4/// Refal+ term interface
5//
6//  $Source$
7//  $Revision: 723 $
8//  $Date: 2003-05-05 12:04:11 +0000 (Mon, 05 May 2003) $
9//-----------------------------------------------------------------------------
10
11#ifndef __rf_term_hh__
12#define __rf_term_hh__
13
14#include "rf_memory_chunk.hh"
15#include "rf_common.hh"
16#include "rf_types.hh"
17#include "rf_object.hh"
18#include "pxx_string.hh"
19
20///
21/// A namespace for Refal runtime
22namespace rfrt
23{
24
25using namespace rftype ;
26
27//
28// In previous runtime version we keeped the order of memory block in each
29// reference term. This was needed to compute a pointer of a memory block.
30// Now an allocator is able to get this pointer for us, so we don't need all
31// stuff related to this. However, we still keep old code, because in a future
32// we may return to the old scheme if it can lead to faster program execution.
33#if !defined(RF_STORE_CHUNK_ORDER_IN_TERM)
34
35typedef enum {
36  term_sym,
37  term_ref,
38  term_obj,
39  term_class_num
40} term_class_t ;
41
42///
43/// "Flat" bit. This bit, if set for reference term, means that referenced
44/// expression is "flat", i.e. it contains only short symbols, so we should
45/// accurately handle this when destroying the expression. This bit was
46/// introduced because in a case of a "flat" expression we don't need to walk
47/// through and dereference childs.
48/// This bit is kept as invariant during all expression operations.
49#define FLAT_BIT (uintptr_t(0x04))
50///
51/// Pointer mask. Used to obtain a clear pointer from term data.
52#define PTR_MASK ~(uintptr_t((sizeof(uintptr_t) << 1) - 1))
53///
54/// Type mask. Used to distinguish between reference term and other terms.
55/// This is the maximum number of internal "short" types (i.e. types that can
56/// be placed in place in term data.
57#define TYPE_MASK (uintptr_t(0xFFFF0))
58#define CLASS_MASK (uintptr_t(0x03))
59#define TYPE_SHIFT 4
60
61#else
62#if UINTPTR_MAX == UINT32_MAX
63#define ORDER_SHIFT 3
64#define REF_BIT (uintptr_t(0x04))
65#define PTR_MASK (~(uintptr_t(0x07)))
66#define ORDER1_MASK (uintptr_t(0x03))
67#define ORDER2_MASK (uintptr_t(0x07))
68#elif UINTPTR_MAX == UINT64_MAX
69#define ORDER_SHIFT 4
70#define REF_BIT (uintptr_t(0x08))
71#define PTR_MASK (~(uintptr_t(0x0F)))
72#define ORDER1_MASK (uintptr_t(0x03))
73#define ORDER2_MASK (uintptr_t(0x0F))
74#else
75#error "Unsupported pointer size"
76#endif
77//#define COMPOUND_BIT (uintptr_t(0x8000))
78//#define CHAR_SHIFT ((sizeof(uintptr_t) - sizeof (uint16_t)) * 8)
79#define ORDER1(x) ((uintptr_t)((x) >> ORDER_SHIFT))
80#define ORDER2(x) ((uintptr_t)((x) & ORDER2_MASK))
81#define ORDER(x,y) (((x) << ORDER_SHIFT) | (y))
82
83#endif // RF_STORE_CHUNK_ORDER_IN_TERM
84
85//-----------------------------------------------------------------------------
86/// Refal term. This is one of two basic runtime classes, which describes
87/// Refal expression elements.
88///
89/// FIXME: To follow all invariants it is necessary to define copy ctor and
90/// assignment operator, which should increment memory block reference counter
91/// in a case of reference term. Destructor should decrement this reference
92/// counter appropriately. This is not done for optimization purposes.
93//-----------------------------------------------------------------------------
94class Term
95{
96///
97/// We can access all term fields from expression class and some methods
98friend class Expr ;
99friend inline Expr operator + (Term const&, Expr const&) ;
100friend inline Expr operator + (Term const&, Term const&) ;
101friend inline bool flat_eq (
102  Expr const& _e1, uintptr_t _pos1,
103  Expr const& _e2, uintptr_t _pos2, uintptr_t _len );
104
105protected:
106  ///
107  /// The first term field containing symbol type or (in a case of reference
108  /// term) pointer to the first term of referenced expresion
109  uintptr_t data1 ;
110  ///
111  /// The second term field containing actual data (for short type), pointer
112  /// to object (for object extensions) or pointer to the right margin of
113  /// referenced expression
114
115  union {
116    uintptr_t uint_data2 ;
117    intptr_t int_data2 ;
118    void* ptr_data2 ;
119  };
120
121  ///
122  /// Constructor for empty term with given class and type
123  inline Term (term_class_t _class, unsigned _type) ;
124  ///
125  /// Create expression with specified length
126  static inline Expr create_expr (size_t _len, int _align = 0) ;
127private:
128  ///
129  /// Constructor for a character term. All constructors are private because
130  /// they should be called only from expression implementation.
131  /// FIXME: Originally it was planned that all strings in Refal will be
132  /// unicode-based (there will be no difference in expression size, because
133  /// a term can contain either usual character or wide character). However it
134  /// was decided later to have both character types, so probably instead of
135  /// this we should have Term (char _c) and Term (wchar_t _c).
136  inline Term (uint16_t _c) ;
137  ///
138  /// Constructor for a reference term. It is is needed for Expr::operator()
139  /// implementation.
140  /// FIXME: The definition of this method is in rf_expr.hh now. It should be
141  /// moved into rf_term.ih, when expression interface and implementation will
142  /// be separated.
143  inline Term (Expr const& _expr) ;
144  inline Term (Object* _obj) ;
145
146  inline Term (Term const& _t) ;
147  inline Term& operator = (Term const& _t) ;
148
149  typedef void (Term::*ctor_func_t)(Term const& _t) ;
150  typedef void (Term::*dtor_func_t)() ;
151  typedef bool (Term::*eq_func_t)(Term const& _t) const ;
152  typedef pxx::WString (Term::*to_string_func_t)() const ;
153  typedef size_t (Term::*get_char_len_func_t)() const ;
154  typedef Term* (Term::*to_chars_func_t)(Term* _p) const ;
155
156  static ctor_func_t ctor_funcs[term_class_num] ;
157  static dtor_func_t dtor_funcs[term_class_num] ;
158  static eq_func_t eq_funcs[term_class_num] ;
159  static to_string_func_t to_string_funcs[term_class_num] ;
160
161  void ctor_sym (Term const& _t) ;
162  void ctor_ref (Term const& _t) ;
163  void ctor_obj (Term const& _t) ;
164  void dtor_sym () ;
165  void dtor_ref () ;
166  void dtor_obj () ;
167  bool eq_sym (Term const& _t) const ;
168  bool eq_ref (Term const& _t) const ;
169  bool eq_obj (Term const& _t) const ;
170  pxx::WString to_string_sym () const ;
171  pxx::WString to_string_ref () const ;
172  pxx::WString to_string_obj () const ;
173
174public:
175  ///
176  /// Destructor.
177  inline ~Term () ;
178  ///
179  /// Check whether two terms are equal
180  inline bool operator == (Term const& _t) const ;
181  ///
182  /// Check whether two terms are not equal
183  inline bool operator != (Term const& _t) const ;
184  ///
185  /// Check whether two term sequences are equal
186  static inline bool eq (
187    Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2
188  ) ;
189  ///
190  /// Check whether a term is a reference term
191  inline bool is_ref () const ;
192  ///
193  /// Check whether a term is a symbol
194  inline bool is_sym () const ;
195  ///
196  /// Check whether a term is a border symbol
197  inline bool is_border () const ;
198  ///
199  /// Get a pointer to the first term of referenced expression for a reference
200  /// term. Note that this method doesn't check whether a term is a reference
201  /// term.
202  inline Term* get_first () const ;
203  ///
204  /// Get a pointer to the right margin of referenced expression for
205  /// a reference term. Note that this method doesn't checkwhether a term is
206  /// a reference term.
207  inline Term* get_last () const ;
208  ///
209  /// Get a type of "short" symbol.
210  inline unsigned get_type () const ;
211  ///
212  /// Set a type of "short" symbol.
213  inline void set_type (unsigned _type) ;
214  inline term_class_t get_class () const ;
215  inline void set_class (term_class_t _class) ;
216  inline Object& get_object () ;
217  ///
218  /// Get a pointer to memory block containing referenced expression. Note
219  /// that this method doesn't check whether a term is a reference term.
220  inline MemoryChunk* get_mem_chunk() const ;
221
222  inline operator pxx::WString () const ;
223
224#if defined(RF_STORE_CHUNK_ORDER_IN_TERM)
225  inline unsigned get_order () const
226  {
227    return ORDER(data1 & ORDER1_MASK, data2 & ORDER2_MASK);
228  }
229#endif
230
231};
232
233}
234
235#endif // __rf_term_hh__
Note: See TracBrowser for help on using the repository browser.