source: to-imperative/trunk/runtime/rf_macros.hh @ 185

Last change on this file since 185 was 185, checked in by orlov, 18 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.9 KB
Line 
1#ifndef __rf_macros_hh__
2#define __rf_macros_hh__
3
4namespace rfrt
5{
6
7#define ID(x) x
8#define CID(x) x const
9#define CRID(x) x const&
10#define push_args(args...) (CRID(Stack))stack,##args
11#define decl_args(args...) CID(Arg) ##args
12#define decl_ress(args...) ID(Result) ,##args
13#define pop_ress(args...) stack,##args
14#define RM_PAREN(x...) x
15
16#if 1
17#define RF_CALL(f,args,ress) ({\
18  Expr* __saved_top1 = stack.get_top(); \
19  push_args args; \
20  stack.set_top(__saved_top1); \
21  RetVal __retval; \
22  rf_func_t __p = (rf_func_t)(&rf_##f); \
23  while (true) { \
24    __retval = (*__p)(); \
25    if (!__retval.is_tail()) break; \
26    __p = __retval.get_ptr(); \
27  } \
28  if (__retval) {\
29    stack.set_top(__saved_top1); \
30    pop_ress ress; \
31    stack.set_top(__saved_top1); \
32  } \
33  (bool)__retval; \
34})
35
36#else // Disable tail recursion
37
38#define RF_CALL(f,args,ress) ({\
39  Expr* __saved_top1 = stack.get_top(); \
40  push_args args; \
41  stack.set_top(__saved_top1); \
42  RetVal __retval = rf_##f(); \
43  if (__retval) {\
44    stack.set_top(__saved_top1); \
45    pop_ress ress; \
46    stack.set_top(__saved_top1); \
47  } \
48  (bool)__retval; \
49})
50
51#endif
52
53#define RF_TAILCALL(f,args,ress) \
54  stack.set_top(__saved_top); \
55  push_args args; \
56  stack.set_top(__saved_top); \
57  return (rf_func_t)rf_##f;
58
59#define RF_FUNC(f,args,ress) \
60  extern "C" RetVal rf_##f (void) { \
61    Expr* __saved_top = stack.get_top(); \
62    RM_PAREN args; \
63    stack.set_top(__saved_top); \
64    RM_PAREN ress;
65
66#define RF_DECL(f) \
67  extern "C" RetVal rf_##f (void)
68
69#define RF_ARG Arg
70#define RF_RES Result
71
72#define RF_END \
73    return true; \
74  }
75
76#define retfail \
77  return __saved_top
78
79#define func_decl(f) \
80  extern "C" RetVal rf_##f ()
81
82#define trap \
83  Expr* __trap_top = stack.get_top(); try
84
85#define with catch (Expr const& err)
86
87#define cleanup stack.destroy_results(__trap_top)
88
89#define error(x) throw Expr(x)
90
91}
92
93#endif // __rf_macros_hh__
Note: See TracBrowser for help on using the repository browser.