-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathneo-c-libc.h
More file actions
2843 lines (2525 loc) · 72.8 KB
/
Copy pathneo-c-libc.h
File metadata and controls
2843 lines (2525 loc) · 72.8 KB
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef NEO_C_LIBC_H
#define NEO_C_LIBC_H
#if defined(__BAREMETAL__)
typedef __builtin_va_list va_list;
#if defined(__NEO_MICRO16__) || defined(__NEO_MICRO8__)
typedef unsigned int size_t;
typedef int ptrdiff_t;
typedef unsigned int uintptr_t;
typedef int intptr_t;
#elif defined(__NEO_MICRO32__)
typedef unsigned int size_t;
typedef int ptrdiff_t;
typedef unsigned int uintptr_t;
typedef int intptr_t;
#else
typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef unsigned long uintptr_t;
typedef long intptr_t;
#endif
typedef char int8_t;
typedef short int16_t;
#if defined(__NEO_MICRO16__) || defined(__NEO_MICRO8__)
typedef long int32_t;
#else
typedef int int32_t;
#endif
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
#if defined(__NEO_MICRO16__) || defined(__NEO_MICRO8__)
typedef unsigned long uint32_t;
#else
typedef unsigned int uint32_t;
#endif
typedef unsigned long long uint64_t;
typedef int wchar_t;
#ifndef bool
typedef _Bool bool;
#endif
#ifndef true
#define true 1
#define false 0
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_end(ap) __builtin_va_end(ap)
#define va_copy(dst, src) __builtin_va_copy(dst, src)
#else
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#endif
#ifndef BUFSIZ
#define BUFSIZ 8192
#endif
#ifndef EOF
#define EOF (-1)
#endif
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#endif
#ifndef LC_ALL
#define LC_ALL 6
#endif
#ifndef NEO_VASPRINTF_STACK_SIZE
#if defined(__NEO_MICRO_RAM_8K__)
#define NEO_VASPRINTF_STACK_SIZE 128
#elif defined(__NEO_MICRO8__)
#define NEO_VASPRINTF_STACK_SIZE 96
#elif defined(__NEO_MICRO16__)
#define NEO_VASPRINTF_STACK_SIZE 128
#elif defined(__BAREMETAL__) || defined(__MINUX__)
#define NEO_VASPRINTF_STACK_SIZE 512
#elif defined(__APPLE__) || defined(__NEO_DARWIN_BARE__) || defined(__x86_64__) || defined(__i386__)
#define NEO_VASPRINTF_STACK_SIZE 262144
#else
#define NEO_VASPRINTF_STACK_SIZE 512
#endif
#endif
#ifndef O_RDONLY
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#if defined(__MINUX__)
#define O_CREAT (1<<9)
#define O_TRUNC (1<<10)
#define O_APPEND (1<<11)
#elif defined(__linux__) || defined(__ANDROID__)
#define O_CREAT 64
#define O_TRUNC 512
#define O_APPEND 1024
#elif defined(__APPLE__)
#define O_CREAT 512
#define O_TRUNC 1024
#define O_APPEND 8
#elif defined(__NEO_DARWIN_BARE__)
#define O_CREAT 512
#define O_TRUNC 1024
#define O_APPEND 8
#else
#define O_CREAT 64
#define O_TRUNC 512
#define O_APPEND 1024
#endif
#endif
#undef va_start
#define va_start(ap, last) __builtin_va_start(ap, last)
using neo-c;
typedef struct {
size_t gl_pathc; // count of paths matched
char **gl_pathv; // NULL-terminated vector of strings
} glob_t;
// Minimal ctype classification table required by newlib-style ctype macros.
// Values mirror newlib's _ctype_ so that isalpha/isdigit macros work without
// pulling in the full libc archive.
uniq const char _ctype_[1 + 256] = {
0,
32, 32, 32, 32, 32, 32, 32, 32, 32, 40, 40, 40, 40, 40, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32,-120, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 16, 16, 16, 16, 16,
16, 16, 65, 65, 65, 65, 65, 65, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 16, 16, 16, 16,
16, 16, 66, 66, 66, 66, 66, 66, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 16, 16, 16, 16,
32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0
};
uniq int __append_char(char **p, unsigned long *rem, char c) {
if (*rem > 1) { **p = c; (*p)++; (*rem)--; return 1; }
return 0;
}
uniq void __append_str(char **p, unsigned long *rem, const char *s) {
while (*s && *rem > 1) { **p = *s++; (*p)++; (*rem)--; }
}
uniq int errno;
#ifdef __NEO_MICRO__
extern unsigned long brk(unsigned long addr);
extern void putchar(char c);
extern void exit(int status);
#endif
#if defined(__NEO_MICRO__)
c_include {
extern int errno;
extern void putchar(char c);
#ifndef NEO_MICRO_HEAP_SIZE
#ifdef __NEO_MICRO_RAM_8K__
#define NEO_MICRO_HEAP_SIZE 6144U
#elif defined(__NEO_MICRO8__)
#define NEO_MICRO_HEAP_SIZE 512U
#elif defined(__NEO_MICRO16__)
#define NEO_MICRO_HEAP_SIZE 1024U
#else
#define NEO_MICRO_HEAP_SIZE (64 * 1024)
#endif
#endif
static unsigned char __neo_micro_heap[NEO_MICRO_HEAP_SIZE];
static unsigned long __neo_micro_brk_offset = 0;
unsigned long brk(unsigned long addr)
{
unsigned long base = (unsigned long)__neo_micro_heap;
unsigned long limit = base + (unsigned long)NEO_MICRO_HEAP_SIZE;
unsigned long request = (unsigned long)addr;
if(addr == 0) {
return base + __neo_micro_brk_offset;
}
if(request < base || request > limit) {
errno = 12;
return (unsigned long)-1;
}
__neo_micro_brk_offset = request - base;
return addr;
}
void exit(int status)
{
(void)status;
for(;;) {
}
}
}
#elif defined(__MINUX__) && defined(__riscv)
c_include {
extern int errno;
#define __NEO_WEAK __attribute__((weak))
#define SYS_write 64
#define SYS_read 65
#define SYS_open 66
#define SYS_close 67
#define SYS_exit 70
#define SYS_brk 74
#define SYS_getcwd 79
#define SYS_chdir 80
#define SYS_mkdir 81
#define SYS_rmdir 82
#define SYS_unlink 83
#define SYS_rename 84
#define SYS_stat 87
#define SYS_lstat 89
#define SYS_lseek 192
#define SYS_fstat 210
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
static long __neo_minux_syscall1(long n, long a0)
{
register long _a0 asm("a0") = a0;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a7) : "memory");
return _a0;
}
static long __neo_minux_syscall2(long n, long a0, long a1)
{
register long _a0 asm("a0") = a0;
register long _a1 asm("a1") = a1;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a1), "r"(_a7) : "memory");
return _a0;
}
static long __neo_minux_syscall3(long n, long a0, long a1, long a2)
{
register long _a0 asm("a0") = a0;
register long _a1 asm("a1") = a1;
register long _a2 asm("a2") = a2;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a1), "r"(_a2), "r"(_a7) : "memory");
return _a0;
}
__NEO_WEAK int* __errno_location(void)
{
return &errno;
}
__NEO_WEAK int bcmp(const void* s1, const void* s2, unsigned long n)
{
const unsigned char* p1 = (const unsigned char*)s1;
const unsigned char* p2 = (const unsigned char*)s2;
unsigned long i;
for(i = 0; i < n; i++) {
if(p1[i] != p2[i]) {
return (int)p1[i] - (int)p2[i];
}
}
return 0;
}
__NEO_WEAK long read(int fd, void* buf, unsigned long count)
{
return __neo_minux_syscall3(SYS_read, fd, (long)buf, (long)count);
}
__NEO_WEAK long write(int fd, const void* buf, unsigned long count)
{
return __neo_minux_syscall3(SYS_write, fd, (long)buf, (long)count);
}
__NEO_WEAK int open(const char* path, int flags, int mode)
{
return (int)__neo_minux_syscall3(SYS_open, (long)path, flags, mode);
}
__NEO_WEAK int close(int fd)
{
return (int)__neo_minux_syscall1(SYS_close, fd);
}
__NEO_WEAK int unlink(const char* path)
{
return (int)__neo_minux_syscall1(SYS_unlink, (long)path);
}
__NEO_WEAK int rename(const char* oldpath, const char* newpath)
{
return (int)__neo_minux_syscall2(SYS_rename, (long)oldpath, (long)newpath);
}
__NEO_WEAK long lseek(int fd, long offset, int whence)
{
return __neo_minux_syscall3(SYS_lseek, fd, offset, whence);
}
struct stat {
unsigned short type;
unsigned short nlink;
unsigned int size;
unsigned int inum;
unsigned int mode;
unsigned short uid;
unsigned short gid;
unsigned int atime;
unsigned int mtime;
unsigned int ctime;
};
__NEO_WEAK int stat(const char* path, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_stat, (long)path, (long)st);
}
__NEO_WEAK int lstat(const char* path, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_lstat, (long)path, (long)st);
}
__NEO_WEAK int fstat(int fd, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_fstat, fd, (long)st);
}
__NEO_WEAK char* getcwd(char* buf, unsigned long size)
{
long n = __neo_minux_syscall2(SYS_getcwd, (long)buf, (long)size);
return n < 0 ? (char*)0 : buf;
}
__NEO_WEAK int chdir(const char* path)
{
return (int)__neo_minux_syscall1(SYS_chdir, (long)path);
}
__NEO_WEAK int mkdir(const char* path, int mode)
{
return (int)__neo_minux_syscall2(SYS_mkdir, (long)path, mode);
}
__NEO_WEAK int rmdir(const char* path)
{
return (int)__neo_minux_syscall1(SYS_rmdir, (long)path);
}
__attribute__((used, weak)) void exit(int status)
{
__neo_minux_syscall1(SYS_exit, status);
for(;;) {
}
}
__NEO_WEAK void putchar(char c)
{
write(1, &c, 1);
}
__NEO_WEAK unsigned long brk(unsigned long size)
{
return (unsigned long)__neo_minux_syscall1(SYS_brk, (long)size);
}
struct utsname {
char sysname[65];
char nodename[65];
char release[65];
char version[65];
char machine[65];
char domainname[65];
};
static void __neo_copy_cstr(char* dst, const char* src, unsigned long n)
{
unsigned long i = 0;
if(n == 0) {
return;
}
while(i + 1 < n && src[i]) {
dst[i] = src[i];
i++;
}
dst[i] = 0;
}
__NEO_WEAK int uname(struct utsname* buf)
{
if(!buf) {
errno = 22;
return -1;
}
__neo_copy_cstr(buf->sysname, "Minux", sizeof(buf->sysname));
__neo_copy_cstr(buf->nodename, "minux9", sizeof(buf->nodename));
__neo_copy_cstr(buf->release, "", sizeof(buf->release));
__neo_copy_cstr(buf->version, "", sizeof(buf->version));
__neo_copy_cstr(buf->machine, "riscv64", sizeof(buf->machine));
__neo_copy_cstr(buf->domainname, "", sizeof(buf->domainname));
return 0;
}
typedef long time_t;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
long tm_gmtoff;
const char* tm_zone;
};
__NEO_WEAK time_t time(time_t* t)
{
time_t result = 0;
if(t) {
*t = result;
}
return result;
}
__NEO_WEAK struct tm* localtime(const time_t* t)
{
static struct tm tm_;
(void)t;
tm_.tm_sec = 0;
tm_.tm_min = 0;
tm_.tm_hour = 0;
tm_.tm_mday = 1;
tm_.tm_mon = 0;
tm_.tm_year = 70;
tm_.tm_wday = 4;
tm_.tm_yday = 0;
tm_.tm_isdst = 0;
tm_.tm_gmtoff = 0;
tm_.tm_zone = "UTC";
return &tm_;
}
extern int main(int argc, char** argv) __attribute__((used));
__attribute__((used, weak, naked, noreturn)) void _start(void)
{
__asm__ volatile(
"call main\n"
"li a7, 70\n"
"ecall\n"
"1: j 1b\n"
);
}
}
extern size_t brk(size_t size);
extern long read(int fd, void* buf, size_t count);
extern long write(int fd, const void* buf, size_t count);
extern int open(const char* path, int flags, int mode);
extern int close(int fd);
extern int unlink(const char* path);
extern int rename(const char* oldpath, const char* newpath);
extern long lseek(int fd, long offset, int whence);
extern void exit(int status);
extern void putchar(char c);
#elif defined(__linux__) && defined(__x86_64__)
c_include {
extern int errno;
#define __NEO_WEAK __attribute__((weak))
#if defined(__MINUX__) && defined(__riscv)
#define SYS_write 64
#define SYS_read 65
#define SYS_open 66
#define SYS_close 67
#define SYS_exit 70
#define SYS_brk 74
#define SYS_getcwd 79
#define SYS_chdir 80
#define SYS_mkdir 81
#define SYS_rmdir 82
#define SYS_unlink 83
#define SYS_rename 84
#define SYS_stat 87
#define SYS_lstat 89
#define SYS_lseek 192
#define SYS_fstat 210
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
static long __neo_minux_syscall1(long n, long a0)
{
register long _a0 asm("a0") = a0;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a7) : "memory");
return _a0;
}
static long __neo_minux_syscall2(long n, long a0, long a1)
{
register long _a0 asm("a0") = a0;
register long _a1 asm("a1") = a1;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a1), "r"(_a7) : "memory");
return _a0;
}
static long __neo_minux_syscall3(long n, long a0, long a1, long a2)
{
register long _a0 asm("a0") = a0;
register long _a1 asm("a1") = a1;
register long _a2 asm("a2") = a2;
register long _a7 asm("a7") = n;
asm volatile("ecall" : "+r"(_a0) : "r"(_a1), "r"(_a2), "r"(_a7) : "memory");
return _a0;
}
__NEO_WEAK int* __errno_location(void)
{
return &errno;
}
__NEO_WEAK int bcmp(const void* s1, const void* s2, unsigned long n)
{
const unsigned char* p1 = (const unsigned char*)s1;
const unsigned char* p2 = (const unsigned char*)s2;
unsigned long i;
for(i = 0; i < n; i++) {
if(p1[i] != p2[i]) {
return (int)p1[i] - (int)p2[i];
}
}
return 0;
}
__NEO_WEAK long read(int fd, void* buf, unsigned long count)
{
return __neo_minux_syscall3(SYS_read, fd, (long)buf, (long)count);
}
__NEO_WEAK long write(int fd, const void* buf, unsigned long count)
{
return __neo_minux_syscall3(SYS_write, fd, (long)buf, (long)count);
}
__NEO_WEAK int open(const char* path, int flags, int mode)
{
return (int)__neo_minux_syscall3(SYS_open, (long)path, flags, mode);
}
__NEO_WEAK int close(int fd)
{
return (int)__neo_minux_syscall1(SYS_close, fd);
}
__NEO_WEAK int unlink(const char* path)
{
return (int)__neo_minux_syscall1(SYS_unlink, (long)path);
}
__NEO_WEAK int rename(const char* oldpath, const char* newpath)
{
return (int)__neo_minux_syscall2(SYS_rename, (long)oldpath, (long)newpath);
}
__NEO_WEAK long lseek(int fd, long offset, int whence)
{
return __neo_minux_syscall3(SYS_lseek, fd, offset, whence);
}
struct stat {
unsigned short type;
unsigned short nlink;
unsigned int size;
unsigned int inum;
unsigned int mode;
unsigned short uid;
unsigned short gid;
unsigned int atime;
unsigned int mtime;
unsigned int ctime;
};
__NEO_WEAK int stat(const char* path, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_stat, (long)path, (long)st);
}
__NEO_WEAK int lstat(const char* path, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_lstat, (long)path, (long)st);
}
__NEO_WEAK int fstat(int fd, struct stat* st)
{
return (int)__neo_minux_syscall2(SYS_fstat, fd, (long)st);
}
__NEO_WEAK char* getcwd(char* buf, unsigned long size)
{
long n = __neo_minux_syscall2(SYS_getcwd, (long)buf, (long)size);
return n < 0 ? (char*)0 : buf;
}
__NEO_WEAK int chdir(const char* path)
{
return (int)__neo_minux_syscall1(SYS_chdir, (long)path);
}
__NEO_WEAK int mkdir(const char* path, int mode)
{
return (int)__neo_minux_syscall2(SYS_mkdir, (long)path, mode);
}
__NEO_WEAK int rmdir(const char* path)
{
return (int)__neo_minux_syscall1(SYS_rmdir, (long)path);
}
__attribute__((used, weak)) void exit(int status)
{
__neo_minux_syscall1(SYS_exit, status);
for(;;) {
}
}
__NEO_WEAK void putchar(char c)
{
write(1, &c, 1);
}
__NEO_WEAK unsigned long brk(unsigned long size)
{
return (unsigned long)__neo_minux_syscall1(SYS_brk, (long)size);
}
struct utsname {
char sysname[65];
char nodename[65];
char release[65];
char version[65];
char machine[65];
char domainname[65];
};
static void __neo_copy_cstr(char* dst, const char* src, unsigned long n)
{
unsigned long i = 0;
if(n == 0) {
return;
}
while(i + 1 < n && src[i]) {
dst[i] = src[i];
i++;
}
dst[i] = 0;
}
__NEO_WEAK int uname(struct utsname* buf)
{
if(!buf) {
errno = 22;
return -1;
}
__neo_copy_cstr(buf->sysname, "Minux", sizeof(buf->sysname));
__neo_copy_cstr(buf->nodename, "minux9", sizeof(buf->nodename));
__neo_copy_cstr(buf->release, "", sizeof(buf->release));
__neo_copy_cstr(buf->version, "", sizeof(buf->version));
__neo_copy_cstr(buf->machine, "riscv64", sizeof(buf->machine));
__neo_copy_cstr(buf->domainname, "", sizeof(buf->domainname));
return 0;
}
typedef long time_t;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
long tm_gmtoff;
const char* tm_zone;
};
__NEO_WEAK time_t time(time_t* t)
{
time_t result = 0;
if(t) {
*t = result;
}
return result;
}
__NEO_WEAK struct tm* localtime(const time_t* t)
{
static struct tm tm_;
(void)t;
tm_.tm_sec = 0;
tm_.tm_min = 0;
tm_.tm_hour = 0;
tm_.tm_mday = 1;
tm_.tm_mon = 0;
tm_.tm_year = 70;
tm_.tm_wday = 4;
tm_.tm_yday = 0;
tm_.tm_isdst = 0;
tm_.tm_gmtoff = 0;
tm_.tm_zone = "UTC";
return &tm_;
}
extern int main(int argc, char** argv) __attribute__((used));
__attribute__((used, weak, naked, noreturn)) void _start(void)
{
__asm__ volatile(
"call main\n"
"li a7, 70\n"
"ecall\n"
"1: j 1b\n"
);
}
#else
static long __neo_linux_syscall1(long n, long a1)
{
long ret;
__asm__ volatile(
"syscall"
: "=a"(ret)
: "a"(n), "D"(a1)
: "rcx", "r11", "memory"
);
return ret;
}
static long __neo_linux_syscall3(long n, long a1, long a2, long a3)
{
long ret;
__asm__ volatile(
"syscall"
: "=a"(ret)
: "a"(n), "D"(a1), "S"(a2), "d"(a3)
: "rcx", "r11", "memory"
);
return ret;
}
static long __neo_linux_errno_result(long ret)
{
if(ret < 0 && ret >= -4095) {
errno = (int)-ret;
return -1;
}
return ret;
}
__NEO_WEAK int* __errno_location(void)
{
return &errno;
}
__NEO_WEAK int bcmp(const void* s1, const void* s2, unsigned long n)
{
const unsigned char* p1 = (const unsigned char*)s1;
const unsigned char* p2 = (const unsigned char*)s2;
unsigned long i;
for(i = 0; i < n; i++) {
if(p1[i] != p2[i]) {
return (int)p1[i] - (int)p2[i];
}
}
return 0;
}
__NEO_WEAK long read(int fd, void* buf, unsigned long count)
{
return __neo_linux_errno_result(__neo_linux_syscall3(0, fd, (long)buf, (long)count));
}
__NEO_WEAK long write(int fd, const void* buf, unsigned long count)
{
return __neo_linux_errno_result(__neo_linux_syscall3(1, fd, (long)buf, (long)count));
}
__NEO_WEAK int open(const char* path, int flags, int mode)
{
return (int)__neo_linux_errno_result(__neo_linux_syscall3(2, (long)path, flags, mode));
}
__NEO_WEAK int close(int fd)
{
return (int)__neo_linux_errno_result(__neo_linux_syscall1(3, fd));
}
__NEO_WEAK int unlink(const char* path)
{
return (int)__neo_linux_errno_result(__neo_linux_syscall1(87, (long)path));
}
__attribute__((used, weak)) void exit(int status)
{
__neo_linux_syscall1(60, status);
for(;;) {
}
}
__NEO_WEAK void putchar(char c)
{
write(1, &c, 1);
}
__NEO_WEAK unsigned long brk(unsigned long size)
{
return (unsigned long)__neo_linux_syscall1(12, (long)size);
}
struct utsname {
char sysname[65];
char nodename[65];
char release[65];
char version[65];
char machine[65];
char domainname[65];
};
static void __neo_copy_cstr(char* dst, const char* src, unsigned long n)
{
unsigned long i = 0;
if(n == 0) {
return;
}
while(i + 1 < n && src[i]) {
dst[i] = src[i];
i++;
}
dst[i] = 0;
}
__NEO_WEAK int uname(struct utsname* buf)
{
if(!buf) {
errno = 22;
return -1;
}
__neo_copy_cstr(buf->sysname, "Linux", sizeof(buf->sysname));
__neo_copy_cstr(buf->nodename, "neo-c-bare", sizeof(buf->nodename));
__neo_copy_cstr(buf->release, "", sizeof(buf->release));
__neo_copy_cstr(buf->version, "", sizeof(buf->version));
__neo_copy_cstr(buf->machine, "x86_64", sizeof(buf->machine));
__neo_copy_cstr(buf->domainname, "", sizeof(buf->domainname));
return 0;
}
typedef long time_t;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
long tm_gmtoff;
const char* tm_zone;
};
__NEO_WEAK time_t time(time_t* t)
{
time_t result = (time_t)__neo_linux_syscall1(201, 0);
if(result < 0) {
result = 0;
}
if(t) {
*t = result;
}
return result;
}
__NEO_WEAK struct tm* localtime(const time_t* t)
{
static struct tm tm_;
(void)t;
tm_.tm_sec = 0;
tm_.tm_min = 0;
tm_.tm_hour = 0;
tm_.tm_mday = 1;
tm_.tm_mon = 0;
tm_.tm_year = 70;
tm_.tm_wday = 4;
tm_.tm_yday = 0;
tm_.tm_isdst = 0;
tm_.tm_gmtoff = 0;
tm_.tm_zone = "UTC";
return &tm_;
}
extern int main(int argc, char** argv) __attribute__((used));
__attribute__((used, weak, naked, noreturn)) void _start(void)
{
__asm__ volatile(
"mov (%rsp), %rdi\n"
"lea 8(%rsp), %rsi\n"
"andq $-16, %rsp\n"
"call main\n"
"mov %eax, %edi\n"
"call exit\n"
);
}
#endif
}
extern size_t brk(size_t size);
extern long read(int fd, void* buf, size_t count);
extern long write(int fd, const void* buf, size_t count);
extern int open(const char* path, int flags, int mode);
extern int close(int fd);
extern int unlink(const char* path);
extern void exit(int status);
extern void putchar(char c);
#elif defined(__APPLE__) || defined(__NEO_DARWIN_BARE__)
c_include {
extern int errno;
#define __NEO_WEAK __attribute__((weak))
__NEO_WEAK int* __errno_location(void)
{
return &errno;
}
#ifndef NEO_DARWIN_BARE_HEAP_SIZE
#define NEO_DARWIN_BARE_HEAP_SIZE (1024UL * 1024UL * 1024UL)
#endif
static unsigned char __neo_darwin_heap[NEO_DARWIN_BARE_HEAP_SIZE];
static unsigned long __neo_darwin_brk_offset;
unsigned long brk(unsigned long addr)
{
unsigned long base = (unsigned long)__neo_darwin_heap;
unsigned long limit = base + NEO_DARWIN_BARE_HEAP_SIZE;
if(addr == 0) {
return base + __neo_darwin_brk_offset;
}
if(addr < base || addr > limit) {
errno = 12;
return (unsigned long)-1;
}