libjit-sys 0.2.1

Just-In-Time Compilation in Rust using LibJIT bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * jit-memory.c - Memory copy/set/compare routines.
 *
 * Copyright (C) 2004  Southern Storm Software, Pty Ltd.
 *
 * This file is part of the libjit library.
 *
 * The libjit library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * The libjit library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the libjit library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <jit/jit-util.h>
#include "jit-config.h"

#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if defined(HAVE_STRING_H)
# include <string.h>
#elif defined(HAVE_STRINGS_H)
# include <strings.h>
#endif
#if defined(HAVE_MEMORY_H)
# include <memory.h>
#endif
#if defined(HAVE_STDARG_H)
# include <stdarg.h>
#elif defined(HAVE_VARARGS_H)
# include <varargs.h>
#endif

/*
 * Undefine the macros in "jit-internal.h" so that we
 * can define the real function forms.
 */
#undef jit_memset
#undef jit_memcpy
#undef jit_memmove
#undef jit_memcmp
#undef jit_memchr

/*@
 * @section Memory allocation
 *
 * The @code{libjit} library provides an interface to the traditional
 * system @code{malloc} routines.  All heap allocation in @code{libjit}
 * goes through these functions.  If you need to perform some other kind
 * of memory allocation, you can replace these functions with your
 * own versions.
@*/

/*@
 * @deftypefun {void *} jit_malloc (unsigned int @var{size})
 * Allocate @var{size} bytes of memory from the heap.
 * @end deftypefun
 *
 * @deftypefun {type *} jit_new (@var{type})
 * Allocate @code{sizeof(@var{type})} bytes of memory from the heap and
 * cast the return pointer to @code{@var{type} *}.  This is a macro that
 * wraps up the underlying @code{jit_malloc} function and is less
 * error-prone when allocating structures.
 * @end deftypefun
@*/
void *jit_malloc(unsigned int size)
{
	return malloc(size);
}

/*@
 * @deftypefun {void *} jit_calloc (unsigned int @var{num}, unsigned int @var{size})
 * Allocate @code{@var{num} * @var{size}} bytes of memory from the heap and clear
 * them to zero.
 * @end deftypefun
 *
 * @deftypefun {type *} jit_cnew (@var{type})
 * Allocate @code{sizeof(@var{type})} bytes of memory from the heap and
 * cast the return pointer to @code{@var{type} *}.  The memory is cleared
 * to zero.
 * @end deftypefun
@*/
void *jit_calloc(unsigned int num, unsigned int size)
{
	return calloc(num, size);
}

/*@
 * @deftypefun {void *} jit_realloc (void *@var{ptr}, unsigned int @var{size})
 * Re-allocate the memory at @var{ptr} to be @var{size} bytes in size.
 * The memory block at @var{ptr} must have been allocated by a previous
 * call to @code{jit_malloc}, @code{jit_calloc}, or @code{jit_realloc}.
 * @end deftypefun
@*/
void *jit_realloc(void *ptr, unsigned int size)
{
	return realloc(ptr, size);
}

/*@
 * @deftypefun void jit_free (void *@var{ptr})
 * Free the memory at @var{ptr}.  It is safe to pass a NULL pointer.
 * @end deftypefun
@*/
void jit_free(void *ptr)
{
	if(ptr)
	{
		free(ptr);
	}
}

/*@
 * @section Memory set, copy, compare, etc
 * @cindex Memory operations
 *
 * The following functions are provided to set, copy, compare, and search
 * memory blocks.
@*/

/*@
 * @deftypefun {void *} jit_memset (void *@var{dest}, int @var{ch}, unsigned int @var{len})
 * Set the @var{len} bytes at @var{dest} to the value @var{ch}.
 * Returns @var{dest}.
 * @end deftypefun
@*/
void *jit_memset(void *dest, int ch, unsigned int len)
{
#ifdef HAVE_MEMSET
	return memset(dest, ch, len);
#else
	unsigned char *d = (unsigned char *)dest;
	while(len > 0)
	{
		*d++ = (unsigned char)ch;
		--len;
	}
	return dest;
#endif
}

/*@
 * @deftypefun {void *} jit_memcpy (void *@var{dest}, const void *@var{src}, unsigned int @var{len})
 * Copy the @var{len} bytes at @var{src} to @var{dest}.  Returns
 * @var{dest}.  The behavior is undefined if the blocks overlap
 * (use @var{jit_memmove} instead for that case).
 * @end deftypefun
@*/
void *jit_memcpy(void *dest, const void *src, unsigned int len)
{
#if defined(HAVE_MEMCPY)
	return memcpy(dest, src, len);
#elif defined(HAVE_BCOPY)
	bcopy(src, dest, len);
	return dest;
#else
	unsigned char *d = (unsigned char *)dest;
	const unsigned char *s = (const unsigned char *)src;
	while(len > 0)
	{
		*d++ = *s++;
		--len;
	}
	return dest;
#endif
}

/*@
 * @deftypefun {void *} jit_memmove (void *@var{dest}, const void *@var{src}, unsigned int @var{len})
 * Copy the @var{len} bytes at @var{src} to @var{dest} and handle
 * overlapping blocks correctly.  Returns @var{dest}.
 * @end deftypefun
@*/
void *jit_memmove(void *dest, const void *src, unsigned int len)
{
#ifdef HAVE_MEMMOVE
	return memmove(dest, src, len);
#else
	unsigned char *d = (unsigned char *)dest;
	const unsigned char *s = (const unsigned char *)src;
	if(((const unsigned char *)d) < s)
	{
		while(len > 0)
		{
			*d++ = *s++;
			--len;
		}
	}
	else
	{
		d += len;
		s += len;
		while(len > 0)
		{
			*(--d) = *(--s);
			--len;
		}
	}
	return dest;
#endif
}

/*@
 * @deftypefun int jit_memcmp (const void *@var{s1}, const void *@var{s2}, unsigned int @var{len})
 * Compare @var{len} bytes at @var{s1} and @var{s2}, returning a negative,
 * zero, or positive result depending upon their relationship.  It is
 * system-specific as to whether this function uses signed or unsigned
 * byte comparisons.
 * @end deftypefun
@*/
int jit_memcmp(const void *s1, const void *s2, unsigned int len)
{
#if defined(HAVE_MEMCMP)
	return memcmp(s1, s2, len);
#elif defined(HAVE_BCMP)
	return bcmp(s1, s2, len);
#else
	const unsigned char *str1 = (const unsigned char *)s1;
	const unsigned char *str2 = (const unsigned char *)s2;
	while(len > 0)
	{
		if(*str1 < *str2)
			return -1;
		else if(*str1 > *str2)
			return 1;
		++str1;
		++str2;
		--len;
	}
	return 0;
#endif
}

/*@
 * @deftypefun {void *} jit_memchr (void *@var{str}, int @var{ch}, unsigned int @var{len})
 * Search the @var{len} bytes at @var{str} for the first instance of
 * the value @var{ch}.  Returns the location of @var{ch} if it was found,
 * or NULL if it was not found.
 * @end deftypefun
@*/
void *jit_memchr(const void *str, int ch, unsigned int len)
{
#ifdef HAVE_MEMCHR
	return memchr(str, ch, len);
#else
	const unsigned char *s = (const unsigned char *)str;
	while(len > 0)
	{
		if(*s == (unsigned char)ch)
		{
			return (void *)s;
		}
		++s;
		--len;
	}
	return (void *)0;
#endif
}

/*@
 * @section String operations
 * @cindex String operations
 *
 * The following functions are provided to manipulate NULL-terminated
 * strings.  It is highly recommended that you use these functions in
 * preference to system functions, because the corresponding system
 * functions are extremely non-portable.
@*/

/*@
 * @deftypefun {unsigned int} jit_strlen (const char *@var{str})
 * Returns the length of @var{str}.
 * @end deftypefun
@*/
unsigned int jit_strlen(const char *str)
{
#ifdef HAVE_STRLEN
	return (unsigned int)(strlen(str));
#else
	unsigned int len = 0;
	while(*str++ != '\0')
	{
		++len;
	}
	return len;
#endif
}

/*@
 * @deftypefun {char *} jit_strcpy (char *@var{dest}, const char *@var{src})
 * Copy the string at @var{src} to @var{dest}.  Returns @var{dest}.
 * @end deftypefun
@*/
char *jit_strcpy(char *dest, const char *src)
{
#ifdef HAVE_STRCPY
	return strcpy(dest, src);
#else
	char ch;
	char *d = dest;
	while((ch = *src++) != '\0')
	{
		*d++ = ch;
	}
	*d = '\0';
	return dest;
#endif
}

/*@
 * @deftypefun {char *} jit_strcat (char *@var{dest}, const char *@var{src})
 * Copy the string at @var{src} to the end of the string at @var{dest}.
 * Returns @var{dest}.
 * @end deftypefun
@*/
char *jit_strcat(char *dest, const char *src)
{
#ifdef HAVE_STRCAT
	return strcat(dest, src);
#else
	char ch;
	char *d = dest + jit_strlen(dest);
	while((ch = *src++) != '\0')
	{
		*d++ = ch;
	}
	*d = '\0';
	return dest;
#endif
}

/*@
 * @deftypefun {char *} jit_strncpy (char *@var{dest}, const char *@var{src}, unsigned int @var{len})
 * Copy at most @var{len} characters from the string at @var{src} to
 * @var{dest}.  Returns @var{dest}.
 * @end deftypefun
@*/
char *jit_strncpy(char *dest, const char *src, unsigned int len)
{
#ifdef HAVE_STRNCPY
	return strncpy(dest, src, len);
#else
	char ch;
	char *d = dest;
	while(len > 0 && (ch = *src++) != '\0')
	{
		*d++ = ch;
		--len;
	}
	while(len > 0)
	{
		*d++ = '\0';
		--len;
	}
	return dest;
#endif
}

/*@
 * @deftypefun {char *} jit_strdup (const char *@var{str})
 * Allocate a block of memory using @code{jit_malloc} and copy
 * @var{str} into it.  Returns NULL if @var{str} is NULL or there
 * is insufficient memory to perform the @code{jit_malloc} operation.
 * @end deftypefun
@*/
char *jit_strdup(const char *str)
{
	char *new_str;
	if(!str)
	{
		return 0;
	}
	new_str = jit_malloc(strlen(str) + 1);
	if(!new_str)
	{
		return 0;
	}
	strcpy(new_str, str);
	return new_str;
}

/*@
 * @deftypefun {char *} jit_strndup (const char *@var{str}, unsigned int @var{len})
 * Allocate a block of memory using @code{jit_malloc} and copy at most
 * @var{len} characters of @var{str} into it.  The copied string is then
 * NULL-terminated.  Returns NULL if @var{str} is NULL or there
 * is insufficient memory to perform the @code{jit_malloc} operation.
 * @end deftypefun
@*/
char *jit_strndup(const char *str, unsigned int len)
{
	char *new_str;
	if(!str)
	{
		return 0;
	}
	new_str = jit_malloc(len + 1);
	if(!new_str)
	{
		return 0;
	}
	jit_memcpy(new_str, str, len);
	new_str[len] = '\0';
	return new_str;
}

/*@
 * @deftypefun int jit_strcmp (const char *@var{str1}, const char *@var{str2})
 * Compare the two strings @var{str1} and @var{str2}, returning
 * a negative, zero, or positive value depending upon their relationship.
 * @end deftypefun
@*/
int jit_strcmp(const char *str1, const char *str2)
{
#ifdef HAVE_STRCMP
	return strcmp(str1, str2);
#else
	int ch1, ch2;
	for(;;)
	{
		ch1 = *str1++;
		ch2 = *str2++;
		if(ch1 != ch2 || !ch1 || !ch2)
		{
			break;
		}
	}
	return (ch1 - ch2);
#endif
}

/*@
 * @deftypefun int jit_strncmp (const char *@var{str1}, const char *@var{str2}, unsigned int @var{len})
 * Compare the two strings @var{str1} and @var{str2}, returning
 * a negative, zero, or positive value depending upon their relationship.
 * At most @var{len} characters are compared.
 * @end deftypefun
@*/
int jit_strncmp(const char *str1, const char *str2, unsigned int len)
{
#ifdef HAVE_STRNCMP
	return strncmp(str1, str2, len);
#else
	int ch1, ch2;
	while(len > 0)
	{
		ch1 = *str1++;
		ch2 = *str2++;
		if(ch1 != ch2 || !ch1 || !ch2)
		{
			return (ch1 - ch2);
		}
		--len;
	}
	return 0;
#endif
}

/*@
 * @deftypefun int jit_stricmp (const char *@var{str1}, const char *@var{str2})
 * Compare the two strings @var{str1} and @var{str2}, returning
 * a negative, zero, or positive value depending upon their relationship.
 * Instances of the English letters A to Z are converted into their
 * lower case counterparts before comparison.
 *
 * Note: this function is guaranteed to use English case comparison rules,
 * no matter what the current locale is set to, making it suitable for
 * comparing token tags and simple programming language identifiers.
 *
 * Locale-sensitive string comparison is complicated and usually specific
 * to the front end language or its supporting runtime library.  We
 * deliberately chose not to handle this in @code{libjit}.
 * @end deftypefun
@*/
int jit_stricmp(const char *str1, const char *str2)
{
	int ch1, ch2;
	for(;;)
	{
		ch1 = *str1++;
		ch2 = *str2++;
		if(ch1 >= 'A' && ch1 <= 'Z')
		{
			ch1 = ch1 - 'A' + 'a';
		}
		if(ch2 >= 'A' && ch2 <= 'Z')
		{
			ch2 = ch2 - 'A' + 'a';
		}
		if(ch1 != ch2 || !ch1 || !ch2)
		{
			break;
		}
	}
	return (ch1 - ch2);
}

/*@
 * @deftypefun int jit_strnicmp (const char *@var{str1}, const char *@var{str2}, unsigned int @var{len})
 * Compare the two strings @var{str1} and @var{str2}, returning
 * a negative, zero, or positive value depending upon their relationship.
 * At most @var{len} characters are compared.  Instances of the English
 * letters A to Z are converted into their lower case counterparts
 * before comparison.
 * @end deftypefun
@*/
int jit_strnicmp(const char *str1, const char *str2, unsigned int len)
{
	int ch1, ch2;
	while(len > 0)
	{
		ch1 = *str1++;
		ch2 = *str2++;
		if(ch1 >= 'A' && ch1 <= 'Z')
		{
			ch1 = ch1 - 'A' + 'a';
		}
		if(ch2 >= 'A' && ch2 <= 'Z')
		{
			ch2 = ch2 - 'A' + 'a';
		}
		if(ch1 != ch2 || !ch1 || !ch2)
		{
			return (ch1 - ch2);
		}
		--len;
	}
	return 0;
}

/*@
 * @deftypefun {char *} jit_strchr (const char *@var{str}, int @var{ch})
 * Search @var{str} for the first occurrence of @var{ch}.  Returns
 * the address where @var{ch} was found, or NULL if not found.
 * @end deftypefun
@*/
char *jit_strchr(const char *str, int ch)
{
#ifdef HAVE_STRCHR
	return strchr(str, ch);
#else
	char *s = (char *)str;
	for(;;)
	{
		if(*s == (char)ch)
		{
			return s;
		}
		else if(*s == '\0')
		{
			break;
		}
		++s;
	}
	return 0;
#endif
}

/*@
 * @deftypefun {char *} jit_strrchr (const char *@var{str}, int @var{ch})
 * Search @var{str} for the first occurrence of @var{ch}, starting
 * at the end of the string.  Returns the address where @var{ch}
 * was found, or NULL if not found.
 * @end deftypefun
@*/
char *jit_strrchr(const char *str, int ch)
{
#ifdef HAVE_STRRCHR
	return strrchr(str, ch);
#else
	unsigned int len = jit_strlen(str);
	char *s = (char *)(str + len);
	while(len > 0)
	{
		--s;
		if(*s == (char)ch)
		{
			return s;
		}
		--len;
	}
	return 0;
#endif
}

int jit_sprintf(char *str, const char *format, ...)
{
	va_list va;
	int result;
#ifdef HAVE_STDARG_H
	va_start(va, format);
#else
	va_start(va);
#endif
#ifdef VSPRINTF
	result = vsprintf(str, format, va);
#else
	*str = '\0';
	result = 0;
#endif
	va_end(va);
	return result;
}

int jit_snprintf(char *str, unsigned int len, const char *format, ...)
{
	va_list va;
	int result;
#ifdef HAVE_STDARG_H
	va_start(va, format);
#else
	va_start(va);
#endif
#if defined(HAVE_VSNPRINTF)
	result = vsnprintf(str, len, format, va);
#elif defined(HAVE__VSNPRINTF)
	result = _vsnprintf(str, len, format, va);
#else
	*str = '\0';
	result = 0;
#endif
	va_end(va);
	return result;
}