forked from bkaradzic/bx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrtnone.cpp
700 lines (570 loc) · 11.8 KB
/
crtnone.cpp
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Copyright 2010-2022 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx/blob/master/LICENSE
*/
#include <bx/debug.h>
#include <bx/file.h>
#include <bx/math.h>
#include <bx/sort.h>
#include <bx/timer.h>
#if BX_CRT_NONE
#include "crt0.h"
#define NOT_IMPLEMENTED() \
{ bx::debugPrintf("crtnone: %s not implemented\n", BX_FUNCTION); abort(); }
extern "C" void* memcpy(void* _dst, const void* _src, size_t _numBytes)
{
bx::memCopy(_dst, _src, _numBytes);
return _dst;
}
extern "C" void* memmove(void* _dst, const void* _src, size_t _numBytes)
{
bx::memMove(_dst, _src, _numBytes);
return _dst;
}
extern "C" void* memset(void* _dst, int _ch, size_t _numBytes)
{
bx::memSet(_dst, uint8_t(_ch), _numBytes);
return _dst;
}
#if !BX_PLATFORM_NONE
typedef int64_t off64_t;
typedef int32_t pid_t;
extern "C" int32_t memcmp(const void* _lhs, const void* _rhs, size_t _numBytes)
{
return bx::memCmp(_lhs, _rhs, _numBytes);
}
extern "C" size_t strlen(const char* _str)
{
return bx::strLen(_str);
}
extern "C" size_t strnlen(const char* _str, size_t _max)
{
return bx::strLen(_str, _max);
}
extern "C" void* strcpy(char* _dst, const char* _src)
{
bx::strCopy(_dst, INT32_MAX, _src, INT32_MAX);
return _dst;
}
extern "C" void* strncpy(char* _dst, const char* _src, size_t _num)
{
bx::strCopy(_dst, INT32_MAX, _src, _num);
return _dst;
}
extern "C" char* strcat(char* _dst, const char* _src)
{
bx::strCat(_dst, INT32_MAX, _src, INT32_MAX);
return _dst;
}
extern "C" const char* strchr(const char* _str, int _ch)
{
return bx::strFind(_str, _ch).getPtr();
}
extern "C" int32_t strcmp(const char* _lhs, const char* _rhs)
{
return bx::strCmp(_lhs, _rhs);
}
extern "C" int32_t strncmp(const char* _lhs, const char* _rhs, size_t _max)
{
return bx::strCmp(_lhs, _rhs, _max);
}
extern "C" int32_t strcasecmp(const char* _lhs, const char* _rhs)
{
return bx::strCmpI(_lhs, _rhs);
}
extern "C" const char* strstr(const char* _str, const char* _find)
{
return bx::strFind(_str, _find).getPtr();
}
extern "C" void qsort(void* _base, size_t _num, size_t _size, bx::ComparisonFn _fn)
{
BX_ASSERT(_num <= UINT32_MAX && _size <= UINT32_MAX, "");
return bx::quickSort(_base, _num, _size, _fn);
}
extern "C" int isprint(int _ch)
{
return bx::isPrint(_ch);
}
extern "C" int toupper(int _ch)
{
return bx::toUpper(_ch);
}
extern "C" size_t mbstowcs(wchar_t* _dst, const char* _src, size_t _max)
{
BX_UNUSED(_dst, _src, _max);
return 0;
}
extern "C" char* strdup(const char* _src)
{
uint32_t size = bx::strLen(_src)+1;
char* dup = (char*)malloc(size);
bx::strCopy(dup, size, _src);
return dup;
}
extern "C" long int strtol(const char* _str, char** _end, int _base)
{
BX_UNUSED(_str, _end, _base);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int abs(int _value)
{
return _value >= 0 ? _value : -_value;
}
extern "C" float fabsf(float _x)
{
return bx::abs(_x);
}
extern "C" double fabs(double _x)
{
return bx::abs(_x);
}
extern "C" double ldexp(double _x, int _exp)
{
return bx::ldexp(float(_x), _exp);
}
extern "C" float expf(float _x)
{
return bx::exp(_x);
}
extern "C" float logf(float _x)
{
return bx::log(_x);
}
extern "C" float log10f(float _x)
{
BX_UNUSED(_x);
return 0.0f;
}
extern "C" float powf(float _x, float _y)
{
return bx::pow(_x, _y);
}
extern "C" double pow(double _x, float _y)
{
return bx::pow(_x, _y);
}
extern "C" float sinf(float _x)
{
return bx::sin(_x);
}
extern "C" float cosf(float _x)
{
return bx::cos(_x);
}
extern "C" float tanf(float _x)
{
return bx::tan(_x);
}
extern "C" float atan2f(float _y, float _x)
{
return bx::atan2(_y, _x);
}
extern "C" float sqrtf(float _x)
{
return bx::sqrt(_x);
}
extern "C" double sqrt(double _x)
{
return bx::sqrt(_x);
}
extern "C" float ceilf(float _x)
{
return bx::ceil(_x);
}
extern "C" double ceil(double _x)
{
return bx::ceil(_x);
}
extern "C" float floorf(float _x)
{
return bx::floor(_x);
}
extern "C" double floor(double _x)
{
return bx::floor(_x);
}
extern "C" float acosf(float _x)
{
return bx::acos(_x);
}
extern "C" float fmodf(float _numer, float _denom)
{
return bx::mod(_numer, _denom);
}
extern "C" int atoi(const char* _str)
{
int32_t result = 0;
bx::fromString(&result, _str);
return result;
}
extern "C" double atof(const char* _str)
{
double result = 0.0;
bx::fromString(&result, _str);
return result;
}
extern "C" struct DIR* opendir(const char* _dirname)
{
BX_UNUSED(_dirname);
// NOT_IMPLEMENTED();
return NULL;
}
extern "C" struct dirent* readdir(struct DIR* _dirp)
{
BX_UNUSED(_dirp);
NOT_IMPLEMENTED();
return NULL;
}
extern "C" int closedir(struct DIR* _dirp)
{
BX_UNUSED(_dirp);
NOT_IMPLEMENTED();
return 0;
}
extern "C" int vsnprintf(char* _out, size_t _max, const char* _format, va_list _argList)
{
return bx::vsnprintf(_out, _max, _format, _argList);
}
extern "C" int sprintf(char* _out, const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
int32_t len = bx::vsnprintf(_out, INT32_MAX, _format, argList);
va_end(argList);
return len;
}
extern "C" int snprintf(char* _out, size_t _max, const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
int32_t len = bx::vsnprintf(_out, _max, _format, argList);
va_end(argList);
return len;
}
extern "C" int printf(const char* _format, ...)
{
va_list argList;
va_start(argList, _format);
bx::WriterI* writer = bx::getStdOut();
bx::Error err;
int32_t len = bx::write(writer, &err, _format, argList);
va_end(argList);
return len;
}
struct FILE
{
};
extern "C" int fprintf(FILE* _stream, const char* _format, ...)
{
BX_UNUSED(_stream, _format);
return -1;
}
extern "C" int vfprintf(FILE* _stream, const char* _format, va_list _argList)
{
BX_UNUSED(_stream, _format, _argList);
return -1;
}
extern "C" int sscanf(const char* _str, const char* _format, ...)
{
BX_UNUSED(_str, _format);
return -1;
}
extern "C" int fscanf(FILE* _stream, const char* _format, ...)
{
BX_UNUSED(_stream, _format);
return -1;
}
FILE * stdout;
extern "C" FILE* fopen(const char* _filename, const char* _mode)
{
BX_UNUSED(_filename, _mode);
bx::debugPrintf("fopen(\"%s\", \"%s\");\n", _filename, _mode);
// NOT_IMPLEMENTED();
return NULL;
}
extern "C" int fclose(FILE* _stream)
{
BX_UNUSED(_stream);
bx::debugPrintf("fclose(%p);\n", _stream);
// NOT_IMPLEMENTED();
return 0;
}
extern "C" size_t fread(void* _ptr, size_t _size, size_t _count, FILE* _stream)
{
BX_UNUSED(_ptr, _size, _count, _stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" size_t fwrite(const void* _ptr, size_t _size, size_t _count, FILE* _stream)
{
BX_UNUSED(_ptr, _size, _count, _stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int fseek(FILE* _stream, long int _offset, int _origin)
{
BX_UNUSED(_stream, _offset, _origin);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int fseeko64(FILE* _stream, off64_t _offset, int _whence)
{
BX_UNUSED(_stream, _offset, _whence);
NOT_IMPLEMENTED();
return -1;
}
extern "C" long int ftell(FILE* _stream)
{
BX_UNUSED(_stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" off64_t ftello64(FILE* _stream)
{
BX_UNUSED(_stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int feof(FILE* _stream)
{
BX_UNUSED(_stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int ferror(FILE* _stream)
{
BX_UNUSED(_stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" FILE* popen(const char* _command, const char* _type)
{
BX_UNUSED(_command, _type);
NOT_IMPLEMENTED();
return NULL;
}
extern "C" int pclose(FILE* _stream)
{
BX_UNUSED(_stream);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int execvp(const char* _file, char* const _argv[])
{
BX_UNUSED(_file, _argv);
NOT_IMPLEMENTED();
return -1;
}
typedef int32_t clockid_t;
inline void toTimespecNs(timespec& _ts, int64_t _nsecs)
{
_ts.tv_sec = _nsecs/INT64_C(1000000000);
_ts.tv_nsec = _nsecs%INT64_C(1000000000);
}
extern "C" int clock_gettime(clockid_t _clock, struct timespec* _ts)
{
BX_UNUSED(_clock);
int64_t now = crt0::getHPCounter();
toTimespecNs(*_ts, now);
return 0;
}
extern "C" long syscall(long _num, ...)
{
va_list argList;
va_start(argList, _num);
long result = -1;
switch (_num)
{
case 39:
result = crt0::processGetId();
break;
case 228:
{
clockid_t arg0 = va_arg(argList, clockid_t);
timespec* arg1 = va_arg(argList, timespec*);
result = clock_gettime(arg0, arg1);
}
break;
default:
bx::debugPrintf("? syscall %d\n", _num);
break;
}
va_end(argList);
return result;
}
extern "C" long sysconf(int name)
{
BX_UNUSED(name);
NOT_IMPLEMENTED();
return -1;
}
extern "C" pid_t fork(void)
{
NOT_IMPLEMENTED();
return -1;
}
extern "C" int sched_yield(void)
{
NOT_IMPLEMENTED();
return -1;
}
extern "C" int prctl(int _option, unsigned long _arg2, unsigned long _arg3, unsigned long _arg4, unsigned long _arg5)
{
BX_UNUSED(_option, _arg2, _arg3, _arg4, _arg5);
NOT_IMPLEMENTED();
return -1;
}
extern "C" int chdir(const char* _path)
{
BX_UNUSED(_path);
bx::debugPrintf("chdir(%s) not implemented!\n", _path);
return -1;
}
extern "C" char* getcwd(char* _buf, size_t _size)
{
BX_UNUSED(_buf, _size);
NOT_IMPLEMENTED();
return NULL;
}
extern "C" char* getenv(const char* _name)
{
return const_cast<char*>(crt0::getEnv(_name) );
}
extern "C" int setenv(const char* _name, const char* _value, int _overwrite)
{
BX_UNUSED(_name, _value, _overwrite);
bx::debugPrintf("setenv(%s, %s, %d) not implemented!\n", _name, _value, _overwrite);
return -1;
}
extern "C" int unsetenv(const char* _name)
{
BX_UNUSED(_name);
bx::debugPrintf("unsetenv(%s) not implemented!\n", _name);
return -1;
}
#if 0
struct timeval
{
time_t tv_sec;
suseconds_t tv_usec;
};
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
#endif //
extern "C" int gettimeofday(struct timeval* _tv, struct timezone* _tz)
{
BX_UNUSED(_tz);
timespec ts;
if (NULL == _tv)
{
return 0;
}
clock_gettime(0 /*CLOCK_REALTIME*/, &ts);
_tv->tv_sec = ts.tv_sec;
_tv->tv_usec = (int)ts.tv_nsec / 1000;
return 0;
}
typedef int64_t time_t;
extern "C" time_t time(time_t* _arg)
{
timespec ts;
clock_gettime(0 /*CLOCK_REALTIME*/, &ts);
time_t result = ts.tv_sec;
if (NULL != _arg)
{
*_arg = result;
}
return result;
}
extern "C" void* realloc(void* _ptr, size_t _size)
{
return crt0::realloc(_ptr, _size);
}
extern "C" void* malloc(size_t _size)
{
return crt0::realloc(NULL, _size);
}
extern "C" void free(void* _ptr)
{
crt0::realloc(_ptr, 0);
}
#endif // BX_PLATFORM_*
extern "C" void abort(void)
{
bx::debugPrintf("crtnone: abort called!\n");
crt0::exit(bx::kExitFailure);
}
extern "C" void __assert_fail(const char* _assertion, const char* _file, uint32_t _line, const char* _function)
{
BX_UNUSED(_assertion, _file, _line, _function);
abort();
}
void* __dso_handle = (void*)&__dso_handle;
void operator delete(void*)
{
}
extern "C" void __cxa_pure_virtual(void)
{
}
extern "C" int __cxa_atexit(void (*_dtorFn)(void*), void* _arg, void* _dsoHandle)
{
BX_UNUSED(_dtorFn, _arg, _dsoHandle);
return 0;
}
extern "C" void __gxx_personality_v0(void)
{
}
extern "C" void _Unwind_Resume(void*)
{
}
extern "C" int __gcc_personality_v0(int _version, ...)
{
BX_UNUSED(_version);
return 0;
}
namespace __cxxabiv1
{
class __class_type_info
{
public:
virtual ~__class_type_info();
const char* m_name;
};
__class_type_info::~__class_type_info()
{
}
class __si_class_type_info : public __class_type_info
{
public:
virtual ~__si_class_type_info();
};
__si_class_type_info::~__si_class_type_info()
{
}
class __vmi_class_type_info : public __class_type_info
{
public:
virtual ~__vmi_class_type_info();
};
__vmi_class_type_info::~__vmi_class_type_info()
{
}
__extension__ typedef int __guard __attribute__( (mode(__DI__) ) );
extern "C" int __cxa_guard_acquire(__guard* _g)
{
return !*(char*)(_g);
}
extern "C" void __cxa_guard_release(__guard* _g)
{
*(char*)_g = 1;
}
extern "C" void __cxa_guard_abort(__guard* _g)
{
BX_UNUSED(_g);
}
} // namespace __cxxabiv1
#endif // BX_CRT_NONE