1 /*
2 * Creation Date: <2003/12/19 23:09:56 samuel>
3 * Time-stamp: <2004/01/07 19:36:42 samuel>
4 *
5 * <bindings.h>
6 *
7 * Forth bindings
8 *
9 * Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2
14 *
15 */
16
17 #ifndef _H_BINDINGS
18 #define _H_BINDINGS
19
20 #include "kernel/stack.h"
21 #include "kernel/kernel.h"
22
23 #define PUSH3(a,b,c) do { PUSH((a)); PUSH((b)); PUSH((c)); } while(0)
24 #define PUSH2(a,b) do { PUSH((a)); PUSH((b)); } while(0)
25 #define RET( v ) do { PUSH(v); return; } while(0)
26
27 /* initialization */
28 extern int initialize_forth( void );
29
30 /* panic */
31 extern int forth_segv_handler( char *segv_addr );
32
33 /* active package */
34 extern phandle_t find_dev( const char *path );
35 extern phandle_t get_cur_dev( void );
36 extern phandle_t activate_device( const char *str );
37 extern void device_end( void );
38 extern void activate_dev( phandle_t ph );
39
40
41 /* ihandle related */
42 extern phandle_t ih_to_phandle( ihandle_t ih );
43 extern ihandle_t my_parent( void );
44 extern ihandle_t my_self( void );
45 extern char *my_args_copy( void );
46
47 extern xt_t find_package_method( const char *meth, phandle_t ph );
48 extern xt_t find_ih_method( const char *method, ihandle_t ih );
49 extern xt_t find_parent_method( const char *method );
50 extern void call_package( xt_t xt, ihandle_t ihandle );
51 extern void call_parent( xt_t xt );
52 extern void call_parent_method( const char *method );
53
54 /* package */
55 extern ihandle_t open_package( const char *argstr, phandle_t ph );
56 extern ihandle_t open_dev( const char *spec );
57 extern void close_package( ihandle_t ih );
58 extern void close_dev( ihandle_t ih );
59 extern char *get_path_from_ph( phandle_t ph );
60
61 /* property access */
62 extern void set_property( phandle_t ph, const char *name,
63 const char *buf, int len );
64 extern void set_int_property( phandle_t ph, const char *name,
65 u32 val );
66 extern u32 get_int_property( phandle_t ph, const char *name,
67 int *retlen );
68 extern char *get_property( phandle_t ph, const char *name,
69 int *retlen );
70
71 /* device tree iteration */
72 extern phandle_t dt_iter_begin( void );
73 extern phandle_t dt_iterate( phandle_t last_tree );
74 extern phandle_t dt_iterate_type( phandle_t last_tree,
75 const char *type );
dt_find_type(const char * type)76 static inline phandle_t dt_find_type( const char *type ) {
77 return dt_iterate_type( 0, type );
78 }
79
80 /* forth bindings */
81 extern cell feval( const char *str );
82 extern void bind_xtfunc( const char *name, xt_t xt,
83 ucell arg, void (*func)(void) );
84 extern void bind_func( const char *name, void (*func)(void) );
85 extern xt_t bind_noname_func( void (*func)(void) );
86 extern void push_str( const char *str );
87 extern char *pop_fstr_copy( void );
88
89 extern int _fword( const char *word, xt_t *cache_xt );
90 extern cell _eword( const char *word, xt_t *cache_xt, int nargs );
91 extern int _selfword( const char *method, xt_t *cache_xt );
92 extern int _parword( const char *method, xt_t *cache_xt );
93
94 #define fword(w) ({ static xt_t cache_xt = 0; _fword(w, &cache_xt); })
95 #define eword(w, nargs) ({ static xt_t cache_xt = 0; _eword(w, &cache_xt, nargs); })
96 #define selfword(w) ({ static xt_t cache_xt = 0; _selfword(w, &cache_xt); })
97 #define parword(w) ({ static xt_t cache_xt = 0; _parword(w, &cache_xt); })
98
99 extern void throw( int error );
100
101
102 /* node bindings */
103 extern void make_openable( int only_parents );
104
105
106 typedef struct {
107 const char *name;
108 void *func;
109 } method_t;
110
111 #define REGISTER_NODE_METHODS( name, path ) do { \
112 const char *paths[1]; \
113 \
114 paths[0] = path; \
115 bind_node( name##_flags_, name##_size_, \
116 paths, 1, name##_m, sizeof(name##_m)/sizeof(method_t)); \
117 } while(0)
118
119 #define BIND_NODE_METHODS(ph, name) do { \
120 bind_node_methods(ph, name##_flags_, name##_size_, \
121 name##_m, sizeof(name##_m)/sizeof(method_t)); \
122 } while(0)
123
124 #define DECLARE_UNNAMED_NODE( name, flags, size ) \
125 static const int name##_flags_ = flags; \
126 static const int name##_size_ = size;
127
128 #define DECLARE_NODE( name, flags, size, paths... ) \
129 static const char * const name##_p[] = { paths }; \
130 DECLARE_UNNAMED_NODE(name, flags, size)
131
132 #define NODE_METHODS( name ) \
133 static const method_t name##_m[]
134
135 #define REGISTER_NODE( name ) do { \
136 bind_node( name##_flags_, name##_size_, \
137 name##_p, sizeof(name##_p)/sizeof(char*), \
138 name##_m, sizeof(name##_m)/sizeof(method_t) ); \
139 } while(0)
140
141 extern void
142 bind_node_methods(phandle_t ph, int flags, int size, const method_t *methods, int nmet);
143
144 extern void bind_node( int flags, int size, const char * const *paths, int npaths,
145 const method_t *methods, int nmethods );
146
147 extern phandle_t bind_new_node( int flags, int size, const char *name,
148 const method_t *methods, int nmethods );
149
150 #define INSTALL_OPEN 1 /* install trivial open and close methods */
151
152
153
154 #endif /* _H_BINDINGS */
155