Debian Codegraph @

src/c/cmpaux.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * cmpaux.d - auxiliaries used in compiled Lisp code
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <limits.h>
16 #include <ecl/ecl.h>
17 #include <ecl/ecl-inl.h>
18
19 cl_object
20 si_specialp(cl_object sym)
21 {
22 @(return ((ecl_symbol_type(sym) & ecl_stp_special)? ECL_T : ECL_NIL))
23 }
24
25 cl_object
26 si_constp(cl_object sym)
27 {
28 @(return ((ecl_symbol_type(sym) & ecl_stp_constant)? ECL_T : ECL_NIL))
29 }
30
31 cl_fixnum
32 ecl_ifloor(cl_fixnum x, cl_fixnum y)
33 {
34 if (y == 0)
35 FEerror("Zero divizor", 0);
36 else if (y > 0)
37 if (x >= 0)
38 return(x/y);
39 else
40 return(-((-x+y-1))/y);
41 else
42 if (x >= 0)
43 return(-((x-y-1)/(-y)));
44 else
45 return((-x)/(-y));
46 }
47
48 cl_fixnum
49 ecl_imod(cl_fixnum x, cl_fixnum y)
50 {
51 return(x - ecl_ifloor(x, y)*y);
52 }
53
54 /*
55 * ----------------------------------------------------------------------
56 * Conversions to C
57 * ----------------------------------------------------------------------
58 */
59
60 char
61 ecl_to_char(cl_object x)
62 {
63 switch (ecl_t_of(x)) {
64 case t_fixnum:
65 return ecl_fixnum(x);
66 case t_character:
67 return ECL_CHAR_CODE(x);
68 default:
69 FEerror("~S cannot be coerced to a C char.", 1, x);
70 }
71 }
72
73 cl_fixnum
74 ecl_to_fixnum(cl_object x)
75 {
76 switch (ecl_t_of(x)) {
77 case t_fixnum:
78 case t_bignum:
79 return fixint(x);
80 /* case t_character: return (cl_fixnum)ECL_CHAR_CODE(x); */
81 case t_ratio:
82 return (cl_fixnum)ecl_to_double(x);
83 case t_singlefloat:
84 return (cl_fixnum)ecl_single_float(x);
85 case t_doublefloat:
86 return (cl_fixnum)ecl_double_float(x);
87 case t_longfloat:
88 return (cl_fixnum)ecl_long_float(x);
89 default:
90 FEerror("~S cannot be coerced to a C int.", 1, x);
91 }
92 }
93
94 cl_index
95 ecl_to_unsigned_integer(cl_object x)
96 {
97 switch (ecl_t_of(x)) {
98 case t_fixnum:
99 case t_bignum:
100 return fixnnint(x);
101 case t_ratio:
102 return (cl_index)ecl_to_double(x);
103 case t_singlefloat:
104 return (cl_index)ecl_single_float(x);
105 case t_doublefloat:
106 return (cl_index)ecl_double_float(x);
107 case t_longfloat:
108 return (cl_index)ecl_long_float(x);
109 default:
110 FEerror("~S cannot be coerced to a C unsigned int.", 1, x);
111 }
112 }
113
114 int
115 ecl_aref_bv(cl_object x, cl_index index)
116 {
117 index += x->vector.offset;
118 return ((x->vector.self.bit[index/CHAR_BIT] & (0200>>index%CHAR_BIT)) != 0);
119 }
120
121 int
122 ecl_aset_bv(cl_object x, cl_index index, int value)
123 {
124 index += x->vector.offset;
125 if (value == 0)
126 x->vector.self.bit[index/CHAR_BIT] &= ~(0200>>index%CHAR_BIT);
127 else
128 x->vector.self.bit[index/CHAR_BIT] |= 0200>>index%CHAR_BIT;
129 return value;
130 }
131
132 void
133 cl_throw(cl_object tag)
134 {
135 ecl_frame_ptr fr = frs_sch(tag);
136 if (fr == NULL)
137 FEcontrol_error("THROW: The catch ~S is undefined.", 1, tag);
138 ecl_unwind(ecl_process_env(), fr);
139 }
140
141 void
142 cl_return_from(cl_object block_id, cl_object block_name)
143 {
144 ecl_frame_ptr fr = frs_sch(block_id);
145 if (fr == NULL)
146 FEcontrol_error("RETURN-FROM: The block ~S with id ~S is missing.",
147 2, block_name, block_id);
148 ecl_unwind(ecl_process_env(), fr);
149 }
150
151 void
152 cl_go(cl_object tag_id, cl_object label)
153 {
154 const cl_env_ptr the_env = ecl_process_env();
155 ecl_frame_ptr fr = frs_sch(tag_id);
156 if (fr == NULL)
157 FEcontrol_error("GO: The tagbody ~S is missing.", 1, tag_id);
158 the_env->values[0] = label;
159 the_env->nvalues = 1;
160 ecl_unwind(the_env, fr);
161 }
162
163 cl_object
164 cl_grab_rest_args(ecl_va_list args)
165 {
166 cl_object rest = ECL_NIL;
167 cl_object *r = &rest;
168 while (args[0].narg) {
169 *r = ecl_list1(ecl_va_arg(args));
170 r = &ECL_CONS_CDR(*r);
171 }
172 return rest;
173 }
174
175 void
176 cl_parse_key(
177 ecl_va_list args, /* actual args */
178 int nkey, /* number of keywords */
179 cl_object *keys, /* keywords for the function */
180 cl_object *vars, /* where to put values (vars[0..nkey-1])
181 and suppliedp (vars[nkey..2*nkey-1]) */
182 cl_object *rest, /* if rest != NULL, where to collect rest values */
183 bool allow_other_keys) /* whether other key are allowed */
184 {
185 int i;
186 cl_object supplied_allow_other_keys = OBJNULL;
187 cl_object unknown_keyword = OBJNULL;
188
189 if (rest != NULL) *rest = ECL_NIL;
190
191 for (i = 0; i < 2*nkey; i++)
192 vars[i] = ECL_NIL; /* default values: NIL, supplied: NIL */
193 if (args[0].narg <= 0) return;
194
195 for (; args[0].narg > 1; ) {
196 cl_object keyword = ecl_va_arg(args);
197 cl_object value = ecl_va_arg(args);
198 if (ecl_unlikely(!ECL_SYMBOLP(keyword)))
199 FEprogram_error("LAMBDA: Keyword expected, got ~S.", 1, keyword);
200 if (rest != NULL) {
201 rest = &ECL_CONS_CDR(*rest = ecl_list1(keyword));
202 rest = &ECL_CONS_CDR(*rest = ecl_list1(value));
203 }
204 for (i = 0; i < nkey; i++) {
205 if (keys[i] == keyword) {
206 if (vars[nkey+i] == ECL_NIL) {
207 vars[i] = value;
208 vars[nkey+i] = ECL_T;
209 }
210 goto goon;
211 }
212 }
213 /* the key is a new one */
214 if (keyword == @':allow-other-keys') {
215 if (supplied_allow_other_keys == OBJNULL)
216 supplied_allow_other_keys = value;
217 } else if (unknown_keyword == OBJNULL)
218 unknown_keyword = keyword;
219 goon:;
220 }
221 if (ecl_unlikely(args[0].narg != 0))
222 FEprogram_error("Odd number of keys", 0);
223 if (ecl_unlikely(unknown_keyword != OBJNULL && !allow_other_keys &&
224 (supplied_allow_other_keys == ECL_NIL ||
225 supplied_allow_other_keys == OBJNULL))) {
226 for (i = 0; i < nkey; i++) {
227 if (keys[i] == @':allow-other-keys' && vars[nkey+i] == ECL_T && !Null(vars[i])) {
228 return;
229 }
230 }
231 FEprogram_error("Unknown keyword ~S", 1, unknown_keyword);
232 }
233 }