libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc-boot / Gvarargs.cc
blob69f5f2c575648d9e05e1826af8b46fe82b10ae97
1 /* do not edit automatically generated by mc from varargs. */
2 /* varargs.mod provides a basic vararg facility for GNU Modula-2.
4 Copyright (C) 2015-2024 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius@glam.ac.uk>.
7 This file is part of GNU Modula-2.
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include <stdbool.h>
26 # if !defined (PROC_D)
27 # define PROC_D
28 typedef void (*PROC_t) (void);
29 typedef struct { PROC_t proc; } PROC;
30 # endif
32 # include "GStorage.h"
33 #if defined(__cplusplus)
34 # undef NULL
35 # define NULL 0
36 #endif
37 #define _varargs_C
39 #include "Gvarargs.h"
40 # include "GStorage.h"
41 # include "Glibc.h"
42 # include "GSYSTEM.h"
43 # include "GM2RTS.h"
45 # define MaxArg 4
46 typedef struct varargs_argDesc_r varargs_argDesc;
48 typedef struct varargs__T6_r varargs__T6;
50 typedef unsigned char *varargs_ptrToByte;
52 typedef struct varargs__T7_a varargs__T7;
54 typedef varargs__T6 *varargs_vararg__opaque;
56 struct varargs_argDesc_r {
57 void * ptr;
58 unsigned int len;
61 struct varargs__T7_a { varargs_argDesc array[MaxArg+1]; };
62 struct varargs__T6_r {
63 unsigned int nArgs;
64 unsigned int i;
65 void * contents;
66 unsigned int size;
67 varargs__T7 arg;
72 nargs - returns the number of arguments wrapped in, v.
75 extern "C" unsigned int varargs_nargs (varargs_vararg v);
78 arg - fills in, a, with the next argument. The size of, a, must be an exact
79 match with the original vararg parameter.
82 extern "C" void varargs_arg (varargs_vararg v, unsigned char *a, unsigned int _a_high);
85 next - assigns the next arg to be collected as, i.
88 extern "C" void varargs_next (varargs_vararg v, unsigned int i);
91 copy - returns a copy of, v.
94 extern "C" varargs_vararg varargs_copy (varargs_vararg v);
97 replace - fills the next argument with, a. The size of, a,
98 must be an exact match with the original vararg
99 parameter.
102 extern "C" void varargs_replace (varargs_vararg v, unsigned char *a, unsigned int _a_high);
105 end - destructor for vararg, v.
108 extern "C" void varargs_end (varargs_vararg *v);
111 start1 - wraps up argument, a, into a vararg.
114 extern "C" varargs_vararg varargs_start1 (const unsigned char *a_, unsigned int _a_high);
117 start2 - wraps up arguments, a, b, into a vararg.
120 extern "C" varargs_vararg varargs_start2 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high);
123 start3 - wraps up arguments, a, b, c, into a vararg.
126 extern "C" varargs_vararg varargs_start3 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high, const unsigned char *c_, unsigned int _c_high);
129 start4 - wraps up arguments, a, b, c, d, into a vararg.
132 extern "C" varargs_vararg varargs_start4 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high, const unsigned char *c_, unsigned int _c_high, const unsigned char *d_, unsigned int _d_high);
136 nargs - returns the number of arguments wrapped in, v.
139 extern "C" unsigned int varargs_nargs (varargs_vararg v)
141 return static_cast<varargs_vararg__opaque> (v)->nArgs;
142 /* static analysis guarentees a RETURN statement will be used before here. */
143 __builtin_unreachable ();
148 arg - fills in, a, with the next argument. The size of, a, must be an exact
149 match with the original vararg parameter.
152 extern "C" void varargs_arg (varargs_vararg v, unsigned char *a, unsigned int _a_high)
154 typedef unsigned char *arg__T1;
156 arg__T1 p;
157 unsigned int j;
159 if (static_cast<varargs_vararg__opaque> (v)->i == static_cast<varargs_vararg__opaque> (v)->nArgs)
161 M2RTS_HALT (-1); /* too many calls to arg. */
162 __builtin_unreachable ();
164 else
166 if ((_a_high+1) == static_cast<varargs_vararg__opaque> (v)->arg.array[static_cast<varargs_vararg__opaque> (v)->i].len)
168 p = static_cast<arg__T1> (static_cast<varargs_vararg__opaque> (v)->arg.array[static_cast<varargs_vararg__opaque> (v)->i].ptr);
169 j = 0;
170 while (j <= _a_high)
172 const_cast<unsigned char *>(a)[j] = (*p);
173 p += 1;
174 j += 1;
177 else
179 M2RTS_HALT (-1); /* parameter mismatch. */
180 __builtin_unreachable ();
182 static_cast<varargs_vararg__opaque> (v)->i += 1;
188 next - assigns the next arg to be collected as, i.
191 extern "C" void varargs_next (varargs_vararg v, unsigned int i)
193 static_cast<varargs_vararg__opaque> (v)->i = i;
198 copy - returns a copy of, v.
201 extern "C" varargs_vararg varargs_copy (varargs_vararg v)
203 varargs_vararg__opaque c;
204 unsigned int j;
205 unsigned int offset;
207 Storage_ALLOCATE ((void **) &c, sizeof (varargs__T6));
208 c->i = static_cast<varargs_vararg__opaque> (v)->i;
209 c->nArgs = static_cast<varargs_vararg__opaque> (v)->nArgs;
210 c->size = static_cast<varargs_vararg__opaque> (v)->size;
211 Storage_ALLOCATE (&c->contents, c->size);
212 c->contents = libc_memcpy (c->contents, static_cast<varargs_vararg__opaque> (v)->contents, static_cast<size_t> (c->size));
213 for (j=0; j<=c->nArgs; j++)
215 offset = (unsigned int ) (((varargs_ptrToByte) (static_cast<varargs_vararg__opaque> (v)->contents))-((varargs_ptrToByte) (static_cast<varargs_vararg__opaque> (v)->arg.array[j].ptr)));
216 c->arg.array[j].ptr = static_cast<void *> ((varargs_ptrToByte) (c->contents));
217 c->arg.array[j].ptr = reinterpret_cast<void *> (reinterpret_cast<char *> (c->arg.array[j].ptr)+offset);
218 c->arg.array[j].len = static_cast<varargs_vararg__opaque> (v)->arg.array[j].len;
220 return static_cast<varargs_vararg> (c);
221 /* static analysis guarentees a RETURN statement will be used before here. */
222 __builtin_unreachable ();
227 replace - fills the next argument with, a. The size of, a,
228 must be an exact match with the original vararg
229 parameter.
232 extern "C" void varargs_replace (varargs_vararg v, unsigned char *a, unsigned int _a_high)
234 typedef unsigned char *replace__T2;
236 replace__T2 p;
237 unsigned int j;
239 if (static_cast<varargs_vararg__opaque> (v)->i == static_cast<varargs_vararg__opaque> (v)->nArgs)
241 M2RTS_HALT (-1); /* too many calls to arg. */
242 __builtin_unreachable ();
244 else
246 if ((_a_high+1) == static_cast<varargs_vararg__opaque> (v)->arg.array[static_cast<varargs_vararg__opaque> (v)->i].len)
248 p = static_cast<replace__T2> (static_cast<varargs_vararg__opaque> (v)->arg.array[static_cast<varargs_vararg__opaque> (v)->i].ptr);
249 j = 0;
250 while (j <= _a_high)
252 (*p) = a[j];
253 p += 1;
254 j += 1;
257 else
259 M2RTS_HALT (-1); /* parameter mismatch. */
260 __builtin_unreachable ();
267 end - destructor for vararg, v.
270 extern "C" void varargs_end (varargs_vararg *v)
272 if ((*v) != NULL)
274 Storage_DEALLOCATE (&static_cast<varargs_vararg__opaque> ((*v))->contents, sizeof (varargs_vararg));
275 Storage_DEALLOCATE ((void **) &(*v), sizeof (varargs__T6));
281 start1 - wraps up argument, a, into a vararg.
284 extern "C" varargs_vararg varargs_start1 (const unsigned char *a_, unsigned int _a_high)
286 varargs_vararg__opaque v;
287 unsigned char a[_a_high+1];
289 /* make a local copy of each unbounded array. */
290 memcpy (a, a_, _a_high+1);
292 Storage_ALLOCATE ((void **) &v, sizeof (varargs__T6));
293 v->i = 0;
294 v->nArgs = 1;
295 v->size = _a_high+1;
296 Storage_ALLOCATE (&v->contents, v->size);
297 v->contents = libc_memcpy (v->contents, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (v->size));
298 v->arg.array[0].ptr = v->contents;
299 v->arg.array[0].len = v->size;
300 return static_cast<varargs_vararg> (v);
301 /* static analysis guarentees a RETURN statement will be used before here. */
302 __builtin_unreachable ();
307 start2 - wraps up arguments, a, b, into a vararg.
310 extern "C" varargs_vararg varargs_start2 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high)
312 typedef unsigned char *start2__T3;
314 varargs_vararg__opaque v;
315 start2__T3 p;
316 unsigned char a[_a_high+1];
317 unsigned char b[_b_high+1];
319 /* make a local copy of each unbounded array. */
320 memcpy (a, a_, _a_high+1);
321 memcpy (b, b_, _b_high+1);
323 Storage_ALLOCATE ((void **) &v, sizeof (varargs__T6));
324 v->i = 0;
325 v->nArgs = 2;
326 v->size = (_a_high+_b_high)+2;
327 Storage_ALLOCATE (&v->contents, v->size);
328 p = static_cast<start2__T3> (libc_memcpy (v->contents, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (_a_high+1)));
329 v->arg.array[0].ptr = static_cast<void *> (p);
330 v->arg.array[0].len = _a_high+1;
331 p += v->arg.array[0].len;
332 p = static_cast<start2__T3> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(b)), static_cast<size_t> (_b_high+1)));
333 v->arg.array[1].ptr = static_cast<void *> (p);
334 v->arg.array[1].len = _b_high+1;
335 return static_cast<varargs_vararg> (v);
336 /* static analysis guarentees a RETURN statement will be used before here. */
337 __builtin_unreachable ();
342 start3 - wraps up arguments, a, b, c, into a vararg.
345 extern "C" varargs_vararg varargs_start3 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high, const unsigned char *c_, unsigned int _c_high)
347 typedef unsigned char *start3__T4;
349 varargs_vararg__opaque v;
350 start3__T4 p;
351 unsigned char a[_a_high+1];
352 unsigned char b[_b_high+1];
353 unsigned char c[_c_high+1];
355 /* make a local copy of each unbounded array. */
356 memcpy (a, a_, _a_high+1);
357 memcpy (b, b_, _b_high+1);
358 memcpy (c, c_, _c_high+1);
360 Storage_ALLOCATE ((void **) &v, sizeof (varargs__T6));
361 v->i = 0;
362 v->nArgs = 3;
363 v->size = ((_a_high+_b_high)+_c_high)+3;
364 Storage_ALLOCATE (&v->contents, v->size);
365 p = static_cast<start3__T4> (libc_memcpy (v->contents, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (_a_high+1)));
366 v->arg.array[0].ptr = static_cast<void *> (p);
367 v->arg.array[0].len = _a_high+1;
368 p += v->arg.array[0].len;
369 p = static_cast<start3__T4> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(b)), static_cast<size_t> (_b_high+1)));
370 v->arg.array[1].ptr = static_cast<void *> (p);
371 v->arg.array[1].len = _b_high+1;
372 p += v->arg.array[1].len;
373 p = static_cast<start3__T4> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(c)), static_cast<size_t> (_c_high+1)));
374 v->arg.array[2].ptr = static_cast<void *> (p);
375 v->arg.array[2].len = _c_high+1;
376 return static_cast<varargs_vararg> (v);
377 /* static analysis guarentees a RETURN statement will be used before here. */
378 __builtin_unreachable ();
383 start4 - wraps up arguments, a, b, c, d, into a vararg.
386 extern "C" varargs_vararg varargs_start4 (const unsigned char *a_, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high, const unsigned char *c_, unsigned int _c_high, const unsigned char *d_, unsigned int _d_high)
388 typedef unsigned char *start4__T5;
390 varargs_vararg__opaque v;
391 start4__T5 p;
392 unsigned char a[_a_high+1];
393 unsigned char b[_b_high+1];
394 unsigned char c[_c_high+1];
395 unsigned char d[_d_high+1];
397 /* make a local copy of each unbounded array. */
398 memcpy (a, a_, _a_high+1);
399 memcpy (b, b_, _b_high+1);
400 memcpy (c, c_, _c_high+1);
401 memcpy (d, d_, _d_high+1);
403 Storage_ALLOCATE ((void **) &v, sizeof (varargs__T6));
404 v->i = 0;
405 v->nArgs = 4;
406 v->size = (((_a_high+_b_high)+_c_high)+_d_high)+4;
407 Storage_ALLOCATE (&v->contents, v->size);
408 p = static_cast<start4__T5> (libc_memcpy (v->contents, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (_a_high+1)));
409 v->arg.array[0].len = _a_high+1;
410 p += v->arg.array[0].len;
411 p = static_cast<start4__T5> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(b)), static_cast<size_t> (_b_high+1)));
412 v->arg.array[1].ptr = static_cast<void *> (p);
413 v->arg.array[1].len = _b_high+1;
414 p += v->arg.array[1].len;
415 p = static_cast<start4__T5> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(c)), static_cast<size_t> (_c_high+1)));
416 v->arg.array[2].ptr = static_cast<void *> (p);
417 v->arg.array[2].len = _c_high+1;
418 p += v->arg.array[2].len;
419 p = static_cast<start4__T5> (libc_memcpy (reinterpret_cast <void *> (p), const_cast<void*> (static_cast<const void*>(c)), static_cast<size_t> (_c_high+1)));
420 v->arg.array[3].ptr = static_cast<void *> (p);
421 v->arg.array[3].len = _c_high+1;
422 return static_cast<varargs_vararg> (v);
423 /* static analysis guarentees a RETURN statement will be used before here. */
424 __builtin_unreachable ();
427 extern "C" void _M2_varargs_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
431 extern "C" void _M2_varargs_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])