source: to-imperative/trunk/c++/runtime/rf_macros.hh @ 2187

Last change on this file since 2187 was 2187, checked in by orlov, 14 years ago
  • Merged changing_directory_structure changes r2126:2186 into the trunk.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1// $Id: rf_macros.hh 2187 2006-12-15 00:17:56Z orlov $
2
3#ifndef __rf_macros_hh__
4#define __rf_macros_hh__
5
6#include "rf_closure.hh"
7#include "rf_retval.hh"
8
9namespace rfrt
10{
11
12extern Expr* __rf_call_saved_top;
13
14extern rftype::Closure* __rf_closure_to_call;
15
16#ifndef RFRT_STINGY_STACK
17
18extern bool __keep_stack;
19
20#define RF_start_push   stack->start_push_mode()
21#define RF_stop_push    stack->stop_push_mode()
22#define RF_start_pop    stack->start_pop_mode()
23#define RF_stop_pop     stack->stop_pop_mode()
24#define RF_start_args
25#define RF_start_ress   stack->start_ress_mode()
26#define RF_stop_ress(f) stack->stop_ress_mode(f)
27#define RF_stack_up     stack->up()
28
29#else // RFRT_STINGY_STACK
30
31#define RF_start_push   Expr* __rf_call_saved_top = stack->get_top()
32#define RF_stop_push    stack->set_top(__rf_call_saved_top)
33#define RF_start_pop    stack->start_pop_mode(__rf_call_saved_top)
34#define RF_stop_pop     stack->set_top(__rf_call_saved_top); stack->dec_depth()
35#define RF_start_args   Expr* __saved_top = stack->get_top()
36#define RF_start_ress   stack->set_top(__saved_top)
37#define RF_stop_ress(f) stack->inc_depth()
38#define RF_stack_up     stack->set_top(__saved_top); stack->dec_depth()
39
40#endif // RFRT_STINGY_STACK
41
42
43#ifndef RFRT_USE_STATEMENT_EXPR
44
45#ifdef RFRT_STINGY_STACK
46#undef  RF_start_push
47#define RF_start_push   __rf_call_saved_top = stack->get_top()
48#endif
49
50#ifndef RFRT_DISABLE_TAIL_RECURSION
51
52static inline bool __rf_call (rftype::Func const* _f)
53{
54#ifdef RFRT_STINGY_STACK
55  Expr* _top1 = __rf_call_saved_top;
56#endif
57  while (true) {
58    RetVal __retval = (*_f)();
59    if (!__retval.is_tail()) {
60#ifdef RFRT_STINGY_STACK
61      __rf_call_saved_top = _top1;
62#endif
63      return __retval;
64    } else {
65      _f = __retval.get_ptr();
66    }
67  }
68}
69
70#else // RFRT_DISABLE_TAIL_RECURSION
71
72static inline bool __rf_call (rftype::Func const* _f)
73{
74#ifdef RFRT_STINGY_STACK
75  Expr* _top1 = __rf_call_saved_top;
76#endif
77  RetVal __retval = (*_f)();
78#ifdef RFRT_STINGY_STACK
79  __rf_call_saved_top = _top1;
80#endif
81  return __retval;
82}
83
84#endif // RFRT_DISABLE_TAIL_RECURSION
85
86#define RF_CALL(f,args,ress) (\
87  RF_start_push, \
88  stack->push(args), \
89  RF_stop_push, \
90  __rf_call(f) ? \
91    RF_start_pop, \
92    stack->pop(ress), \
93    RF_stop_pop, \
94    true : false \
95)
96
97#else // RFRT_USE_STATEMENT_EXPR
98
99#ifndef RFRT_DISABLE_TAIL_RECURSION
100
101#define RF_CALL(f,args,ress) ({\
102  RF_start_push; \
103  stack->push(args); \
104  RF_stop_push; \
105  RetVal __retval; \
106  rftype::Func* __p = f; \
107  while (true) { \
108    __retval = (*__p)(); \
109    if (!__retval.is_tail()) break; \
110    __p = __retval.get_ptr(); \
111  } \
112  if (__retval) {\
113    RF_start_pop; \
114    stack->pop(ress); \
115    RF_stop_pop; \
116  } \
117  (bool)__retval; \
118})
119
120#else // RFRT_DISABLE_TAIL_RECURSION
121
122#define RF_CALL(f,args,ress) ({\
123  RF_start_push; \
124  stack->push(args); \
125  RF_stop_push; \
126  RetVal __retval = (*f)(); \
127  if (__retval) {\
128    RF_start_pop; \
129    stack->pop(ress); \
130    RF_stop_pop; \
131  } \
132  (bool)__retval; \
133})
134
135#endif // RFRT_DISABLE_TAIL_RECURSION
136
137#endif // RFRT_USE_STATEMENT_EXPR
138
139
140#ifndef RFRT_DISABLE_TAIL_RECURSION
141
142#define RF_TAILCALL(f,args,ress) \
143  RF_stack_up; \
144  RF_start_push; \
145  stack->push(args); \
146  RF_stop_push; \
147  return f;
148
149#else // RFRT_DISABLE_TAIL_RECURSION
150
151#define RF_TAILCALL(f,args,ress) \
152  RF_stack_up; \
153  RF_start_push; \
154  stack->push(args); \
155  RF_stop_push; \
156  return (*f)();
157
158#endif // RFRT_DISABLE_TAIL_RECURSION
159
160
161#define RF_APPLY(f,args,ress) \
162  rfrt::__rf_closure_to_call = f; \
163  RF_CALL(f->get_func(), args, ress);
164
165#define RF_TAILAPPLY(f,args,ress) \
166  rfrt::__rf_closure_to_call = f; \
167  RF_TAILCALL(f->get_func(), args, ress);
168
169
170#define RF_FUNC(f,args,ress) \
171  namespace rffunc { RetVal f##_func_ (void); } \
172  rftype::Func* f = new rftype::Func(rffunc::f##_func_, WSTRPREFIX#f); \
173  RetVal rffunc::f##_func_ (void) { \
174    RF_start_args; \
175    for args { \
176      RF_start_ress; \
177      for ress { \
178        RF_stop_ress(f);
179
180#define RF_END \
181        return true; \
182      } \
183    } \
184  }
185
186#define RF_CLOSURE(f,params,args,ress) \
187  RF_FUNC(f, args, ress) \
188  for params { \
189    rfrt::__rf_closure_to_call->pop_arg();
190
191#define RF_END_CLOSURE \
192    RF_END \
193  }
194
195#if 0
196#define RF_FUNC(f,args,ress) \
197  namespace rffunc { \
198    RetVal f##_func_ (void); \
199    wchar_t const f##_name_[] = L"\0" WSTRPREFIX#f; \
200  } \
201  rftype::StaticObject<rftype::Func>* f = \
202      new rftype::StaticObject<rftype::Func>( \
203            rffunc::f##_func_, rffunc::f##_name_, wcslen(WSTRPREFIX#f) + 1); \
204  RetVal rffunc::f##_func_ (void) { \
205    RF_start_args; \
206    for args { \
207      RF_start_ress; \
208      for ress { \
209        RF_stop_ress(f);
210
211#define RF_END \
212        return true; \
213      } \
214    } \
215  }
216#endif
217
218#if 0
219#define RF_DECL(f) \
220  extern RetVal f (void)
221#endif
222#define RF_DECL(f) \
223  extern rftype::Func* f;
224
225#define RF_ARG rfrt::Arg
226#define RF_RES rfrt::Result
227
228#define RF_VOID (;;)
229
230#define RF_ARG0 RF_VOID
231#define RF_ARG1(a) (RF_ARG a;;)
232#define RF_ARG2(a,b) (RF_ARG a,b;;)
233#define RF_ARG3(a,b,c) (RF_ARG a,b,c;;)
234#define RF_ARG4(a,b,c,d) (RF_ARG a,b,c,d;;)
235#define RF_ARG5(a,b,c,d,e) (RF_ARG a,b,c,d,e;;)
236#define RF_ARG6(a,b,c,d,e,f) (RF_ARG a,b,c,d,e,f;;)
237#define RF_ARG7(a,b,c,d,e,f,g) (RF_ARG a,b,c,d,e,f,g;;)
238#define RF_ARG8(a,b,c,d,e,f,g,h) (RF_ARG a,b,c,d,e,f,g,h;;)
239#define RF_ARG9(a,b,c,d,e,f,g,h,i) (RF_ARG a,b,c,d,e,f,g,h,i;;)
240
241#define RF_RES0 RF_VOID
242#define RF_RES1(a) (RF_RES a;;)
243#define RF_RES2(a,b) (RF_RES a,b;;)
244#define RF_RES3(a,b,c) (RF_RES a,b,c;;)
245#define RF_RES4(a,b,c,d) (RF_RES a,b,c,d;;)
246#define RF_RES5(a,b,c,d,e) (RF_RES a,b,c,d,e;;)
247#define RF_RES6(a,b,c,d,e,f) (RF_RES a,b,c,d,e,f;;)
248#define RF_RES7(a,b,c,d,e,f,g) (RF_RES a,b,c,d,e,f,g;;)
249#define RF_RES8(a,b,c,d,e,f,g,h) (RF_RES a,b,c,d,e,f,g,h;;)
250#define RF_RES9(a,b,c,d,e,f,g,h,i) (RF_RES a,b,c,d,e,f,g,h,i;;)
251
252#ifndef RFRT_STINGY_STACK
253# define RF_RETFAIL \
254    return false
255#else
256# define RF_RETFAIL \
257    return __saved_top
258#endif
259
260#ifndef RFRT_STINGY_STACK
261# define RF_TRAP \
262    size_t __trap_depth   = stack->get_depth(); try
263#else
264# define RF_TRAP \
265    Expr*  __trap_top   = stack->get_top(); \
266    size_t __trap_depth = stack->get_depth(); try
267#endif
268
269#define RF_WITH catch (Expr const& err)
270
271#ifndef RFRT_STINGY_STACK
272# define RF_CLEANUP \
273    do { \
274      __keep_stack = false; \
275      stack->cleanup(__trap_depth); \
276    } while (0)
277#else
278# define RF_CLEANUP stack->destroy_results(__trap_top, __trap_depth)
279#endif
280
281#ifdef RFRT_COMPATIBLE
282# define RF_ERROR(x) \
283    do { \
284      if (trap_stack_on) __keep_stack = true; \
285      Expr y = x; \
286      throw Expr(y, y.get_len() - 2, 2); \
287    } while (0)
288#else
289# define RF_ERROR(x) \
290    do { \
291      if (trap_stack_on) __keep_stack = true; \
292      throw (x); \
293    } while (0)
294#endif
295
296#define RF_LIB_ERROR(msg) RF_FUNC_ERROR(Expr::create<Word>(msg))
297
298#ifndef RFRT_STINGY_STACK
299# define RF_FUNC_ERROR(msg) \
300    do { \
301      if (trap_stack_on) __keep_stack = true; \
302      wchar_t const* fname; \
303      size_t len = stack->get_func(stack->get_depth())->get_name(&fname); \
304      throw Expr::create<Word>(fname, len) + msg; \
305    } while (0)
306#else
307# define RF_FUNC_ERROR(msg) \
308    throw Expr::create<Word>(__FUNCTION__) + msg
309#endif
310
311#define RF_LIB_ERROR_IN_FUNC(func, msg) \
312  throw Word::create_expr(func) + Word::create_expr(msg)
313
314#if 0
315#define RF_LIB_ERROR_IN_FUNC(func, msg) \
316  { \
317    Expr _res; \
318    RF_CALL(rfp__mangle::Cpp2Rfp, \
319           (rftype::Word::Word(func).to_symbol()), \
320           (_res)); \
321    RF_CALL(Convert::To__Chars, (_res), (_res)); \
322    RF_CALL(Convert::To__Upper, (_res), (_res)); \
323    RF_CALL(Convert::To__Word,  (_res), (_res)); \
324    RF_ERROR(_res + rftype::Word::Word(msg).to_symbol()); \
325  }
326#endif
327
328#define RF_NEW_SYMBOL(CppType, TermClass) \
329  template<> \
330  struct SymbolType< CppType > \
331  { \
332    /*enum { type = c };*/ \
333    typedef TermClass Type; \
334  }
335
336#define RF_SYMBOL(CppType) Symbol<CppType, SymbolType< CppType >::Type>
337
338#define RF_REGISTER_SYMBOL(CppType) \
339  template <> \
340  const TypeRegister RF_SYMBOL(CppType)::reg(&RF_SYMBOL(CppType)::funcs)
341
342#define RF_REGISTER_SYMBOL_VARIANT(CppType, InstCppType) \
343  template <> \
344  const TypeRegister RF_SYMBOL(CppType)::reg( \
345      RF_SYMBOL(InstCppType)::get_type(), \
346      &RF_SYMBOL(CppType)::funcs \
347  )
348
349}
350
351#endif // __rf_macros_hh__
Note: See TracBrowser for help on using the repository browser.