This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
forked from gek169/SISA64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s64as.c
4364 lines (3970 loc) · 108 KB
/
s64as.c
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
/*
Program written for the GNU99 dialect of C99
to assemble S-ISA-64 assembly language.
Primarily for use in the emulator.
*/
#include "stringutil.h"
#include <stdint.h>
#define LIBMIN_UINT uint64_t
#define LIBMIN_INT int64_t
#define LIBMIN_FLOAT double
#include "libmin.h"
#include "insns.h"
#include "be_encoder.h"
#include <stdio.h>
#include <stdlib.h>
static const char* fail_msg = "\r\n<ASM> Assembler Aborted.\r\n";
static const char* compil_fail_pref = "<ASM COMPILATION ERROR>";
static const char* syntax_fail_pref = "<ASM SYNTAX ERROR>";
static const char* general_fail_pref = "<ASM ERROR>";
static const char* internal_fail_pref = "<ASM INTERNAL ERROR>";
static const char* warn_pref = "<ASM WARNING>";
char* ifilename = NULL;
char* ofilename = "a.bin";
FILE* infile = NULL;
FILE* ofile = NULL;
static char was_macro = 0;
/**/
static char using_asciz = 0;
/*Two C64's worth of memory per buffer*/
#define WKBUF_SZ 0x20000
static char line_copy[WKBUF_SZ] = {0}; /*line_copy*/
static char line[WKBUF_SZ] = {0}; /*line*/
static char buf1[WKBUF_SZ] = {0}; /*buffer for working with strings.*/
static char buf2[WKBUF_SZ] = {0}; /*another buffer for working with strings.*/
static char buf3[WKBUF_SZ] = {0}; /*another buffer for working with strings.*/
static char buf_repl[WKBUF_SZ] = {0}; /*another buffer for working with strings, specifically for the following function:*/
static uint64_t label_generator=0;
static uint64_t label_offset=0; /*used for usermode programs.*/
int perform_inplace_repl( /*returns whether or not it actually did a replacement. You can put it in a while loop.*/
char* workbuf,
const char* replaceme,
const char* replacewith
){
long loc;
loc = strfind((char*)workbuf, (char*)replaceme);
if(loc == -1) return 0;
memcpy(buf_repl, workbuf, loc);
mstrcpy(buf_repl + loc, replacewith);
mstrcpy(
buf_repl + loc + strlen((char*)replacewith),
workbuf + loc + strlen((char*)replaceme)
);
mstrcpy(workbuf, buf_repl);
return 1;
}
static char rut_append_to_me = 0; /*boolean value.*/
static char single_logical_line_mode = 0;
static const char* end_single_logical_line_mode = "$|";
static const unsigned long len_end_single_logical_line_mode = 2;
static char* read_line_from_file(FILE* f, unsigned long* lenout, char terminator){
unsigned char c;
unsigned long blen;
unsigned long i;
blen = 0;
if(rut_append_to_me){
blen = strlen((char*)line);
rut_append_to_me = 0;
}
while(1)
{while_looptop:;
if(feof(f)){goto local_end_1;}
c = fgetc(f);
if(!single_logical_line_mode) if(c == terminator) goto local_end_1;
if(c == '\r') continue; /*Lines read from file should eliminate carriage return.*/
if(c > 0x80) continue; /*Invalid character.*/
if(blen >= (WKBUF_SZ-1)) /*too big*/
{
puts(general_fail_pref);
puts("Oversized line!");
exit(1);
}
line[blen++] = (char)c;
if(single_logical_line_mode){
/*We need to check if the last three characters were the end of a single logical line.*/
if(strlen(line) > len_end_single_logical_line_mode){
for(i = 1; i <= len_end_single_logical_line_mode; i++){
if(
line[blen-i]
!=
end_single_logical_line_mode[len_end_single_logical_line_mode-i]
) goto while_looptop;
}
/*match!*/
single_logical_line_mode = 0;
/*remove it from the string.*/
line[blen - len_end_single_logical_line_mode] = '\0';
goto local_end_1;
}
}
continue;
}
local_end_1:;
line[blen] = '\0';
*lenout = blen;
return line;
}
#ifndef SISA64_MAX_MACROS
#define SISA64_MAX_MACROS 0x10000
#endif
#ifndef SISA64_MAX_GUARDS
#define SISA64_MAX_GUARDS 0x400
#endif
static char* variable_names[SISA64_MAX_MACROS] = {0};
static char* guards[SISA64_MAX_GUARDS] = {0};
static unsigned long nguards = 0;
static char* variable_expansions[SISA64_MAX_MACROS] = {0};
static char variable_is_redefining_flag[SISA64_MAX_MACROS] = {0};
static const unsigned long max_lines_disassembler = 0x1ffFFff;
static unsigned long nmacros = 0;
static unsigned char is_guarded = 0;
//Commenter's note: Remember how include guarding was added to Sisa64? "ASM_GUARD __STRUCTURE_H64"? Yeah, that clears those.
static int reset_guards(){
unsigned long i;
for(i = 0; i < nguards; i++){
if(guards[i]) free(guards[i]);
guards[i] = NULL;
}
return 1;
}
static void remove_trailing_whitespace(){
unsigned long len;
len = strlen(line);
while(1){
if(len == 0) return;
if(isspace(line[len-1])) {
line[len-1] = '\0';
len--;
continue;
}
break;
}
}
/*ASM_GUARD*/
static void decl_guard(){
char* name;
remove_trailing_whitespace();
name = (line + strlen("ASM_GUARD"));
while(isspace(*name))name++;
name = strdup(name);
if(!name){
puts("Malloc Failed.");
}
if(nguards >= SISA64_MAX_GUARDS){
puts(general_fail_pref);
puts("Implementation limit on include guards has been reached.");
exit(1);
}
guards[nguards++] = name;
}
static int guard_is_used(){
unsigned long i;
char* name;
remove_trailing_whitespace();
name = (line + strlen("ASM_GUARD"));
while(isspace(*name))name++;
for(i = 0; i < nguards; i++){
if(streq(guards[i], name)){
return 1;
}
}
return 0;
}
static char int_checker(char* proc){
unsigned long chars_read;
char int_mode;
chars_read = 0;
int_mode = 0;
if(!misdigit(proc[0])) return 1; /*Cannot be valid.*/
if(proc[0] == '0') {
int_mode = 1;
proc++;
chars_read++;
if(proc[0] == ',' || proc[0] == ';' || proc[0] == ' ' || proc[0] == '\0' || proc[0] ==/*(*/ ')') return 0; /*zero.*/
if(proc[0] == 'x') {
int_mode = 2;proc++;
chars_read = 0;
} /*else return 0;*/ /*0 = valid integer */
} /*octal*/
for(;!(proc[0] == ',' || proc[0] == ';'|| proc[0] == ' ' || proc[0] ==/*(*/ ')' || proc[0] == '\0');proc++){
/*Check hex*/
if(int_mode==2 &&
!( mishex(*proc))
){
return 1; /*they have 0x, but no numbers!*/
}
if(int_mode==0 &&
!(misdigit(*proc))){
return 1;
}
if(int_mode==1 &&
!( misoct(*proc) )){
return 1;
}
chars_read++;
}
if(chars_read == 0) return 1; /*There were no characters in the number. Invalid integer.*/
return 0;
}
static uint64_t outputcounter = 0;
static unsigned long nbytes_written = 0;
static unsigned long npasses = 0;
static char printlines = 0;
static unsigned long linesize = 0;
static char region_restriction_mode = 0; /*0 = off, 1 = block, 2 = region*/
static uint8_t memimage[0x40000000] = {0}; /*1 gigabyte memory image.*/
static const uint64_t memimage_mask =
0x3fFFffFF;
static void fputbyte(uint8_t b){
memimage[outputcounter]=b;
outputcounter++;
if(outputcounter > nbytes_written)
nbytes_written = outputcounter;
outputcounter &= memimage_mask;
}
static void putshort(uint16_t sh){
fputbyte(sh/256);
fputbyte(sh);
}
static void putlong(uint32_t sh){
fputbyte(sh/0x1000000);
fputbyte(sh/0x10000);
fputbyte(sh/0x100);
fputbyte(sh);
}
static void putqword(uint64_t sh){
fputbyte(sh/0x100000000000000);
fputbyte(sh/0x1000000000000);
fputbyte(sh/0x10000000000);
fputbyte(sh/0x100000000);
fputbyte(sh/0x1000000);
fputbyte(sh/0x10000);
fputbyte(sh/0x100);
fputbyte(sh);
}
#define ASM_MAX_INCLUDE_LEVEL 20
static FILE* fstack[ASM_MAX_INCLUDE_LEVEL];
char* metaproc = NULL;
unsigned long include_level = 0;
int i_cli_args;
const unsigned long nbuiltin_macros = 4;
/*
Scope code.
*/
#define SCOPE_MAX_VARS 256
enum{
TYPE_U8=0,
TYPE_I8=1,
TYPE_U16=2,
TYPE_I16=3,
TYPE_U32=4,
TYPE_I32=5,
TYPE_F32=6,
TYPE_U64=7,
TYPE_I64=8,
TYPE_F64=9,
TYPE_STRUCT=10,
TYPE_VOID=11
};
#define SCOPE_MAX_VARNAME_LEN 127
#define SCOPE_MAX_GLOBALS 65536
#define SCOPE_MAX_DEPTH 65536
typedef struct{
uint64_t basetype;
uint64_t ptrlevel;
uint64_t arraylen;
uint8_t is_lvalue;
uint64_t structid;
} type;
typedef struct{
char name[SCOPE_MAX_VARNAME_LEN + 1];
char is_stack_allocated; /*For local variables only: Does it use the stack?*/
char is_static_allocated; /*For global variables: always one.*/
unsigned long depth; /*depth?*/
type t;
} scopevar;
unsigned int scope_is_active = 0;
unsigned long scope_depth = 0;
char scope_variables_must_fit_into_register = 0; /*Used for arguments.*/
scopevar scopevars[SCOPE_MAX_VARS];
scopevar gvars[SCOPE_MAX_GLOBALS];
type scope_retval_type = {0};
char scope_has_retval = 0;
unsigned int scope_nvars = 0;
unsigned int scope_gvars = 0;
uint64_t type_get_ptrlvl(type t){
return t.ptrlevel;
}
int type_is_ptr(type t){
return type_get_ptrlvl(t) != 0;
}
char type_is_primitive(type t){
if(t.arraylen) return 0; /*Arrays are never in a register.*/
if(t.ptrlevel) return 1; /*all pointers can fit into registers.*/
if(t.basetype < 10) return 1; /*u8 - double can fit into registers, which are 0 - 9*/
return 0; /*Invalid!*/
}
type parse_type(char* where, char** out){
uint64_t basetype = 7;
uint64_t ptrlevel = 0;
uint64_t arrlen = 0;
long loc_ebrack;
type t = {0};
while(isspace(*where))where++;
if(strprefix("u8",where)){
where += 2;
basetype = TYPE_U8;
goto after_basetype;
}
if(strprefix("byte",where)){
where += 4;
basetype = TYPE_U8;
goto after_basetype;
}
if(strprefix("char",where)){
where += 4;
basetype = TYPE_U8;
goto after_basetype;
}
if(strprefix("uchar",where)){
where += 5;
basetype = TYPE_U8;
goto after_basetype;
}
if(strprefix("ubyte",where)){
where += 5;
basetype = TYPE_U8;
goto after_basetype;
}
if(strprefix("i8",where)){
where += 2;
basetype = TYPE_I8;
goto after_basetype;
}
if(strprefix("sbyte",where)){
where += 5;
basetype = TYPE_I8;
goto after_basetype;
}
if(strprefix("schar",where)){
where += 5;
basetype = TYPE_I8;
goto after_basetype;
}
if(strprefix("u16",where)){
where += 3;
basetype = TYPE_U16;
goto after_basetype;
}
if(strprefix("ushort",where)){
where += 6;
basetype = TYPE_U16;
goto after_basetype;
}
if(strprefix("i16",where)){
where += 3;
basetype = TYPE_I16;
goto after_basetype;
}
if(strprefix("short",where)){
where += 5;
basetype = TYPE_I16;
goto after_basetype;
}
if(strprefix("sshort",where)){
where += 6;
basetype = TYPE_I16;
goto after_basetype;
}
if(strprefix("u32",where)){
where += 3;
basetype = TYPE_U32;
goto after_basetype;
}
if(strprefix("uint",where)){
where += 4;
basetype = TYPE_U32;
goto after_basetype;
}
if(strprefix("ulong",where)){
where += 5;
basetype = TYPE_U32;
goto after_basetype;
}
if(strprefix("unsigned",where)){
where += 8;
basetype = TYPE_U32;
goto after_basetype;
}
if(strprefix("i32",where)){
where += 3;
basetype = TYPE_I32;
goto after_basetype;
}
if(strprefix("int",where)){
where += 3;
basetype = TYPE_I32;
goto after_basetype;
}
if(strprefix("long",where)){
where += 4;
basetype = TYPE_I32;
goto after_basetype;
}
if(strprefix("signed",where)){
where += 6;
basetype = TYPE_I32;
goto after_basetype;
}
if(strprefix("u64",where)){
where += 3;
basetype = TYPE_U64;
goto after_basetype;
}
if(strprefix("qword",where)){
where += 5;
basetype = TYPE_U64;
goto after_basetype;
}
if(strprefix("uqword",where)){
where += 6;
basetype = TYPE_U64;
goto after_basetype;
}
if(strprefix("ullong",where)){
where += 6;
basetype = TYPE_U64;
goto after_basetype;
}
if(strprefix("i64",where)){
where += 3;
basetype = TYPE_I64;
goto after_basetype;
}
if(strprefix("sqword",where)){
where += 6;
basetype = TYPE_I64;
goto after_basetype;
}
if(strprefix("llong",where)){
where += 5;
basetype = TYPE_I64;
goto after_basetype;
}
if(strprefix("sllong",where)){
where += 6;
basetype = TYPE_I64;
goto after_basetype;
}
//floating point types.
if(strprefix("float",where)){
where += 5;
basetype = TYPE_F32;
goto after_basetype;
}
if(strprefix("f32",where)){
where += 3;
basetype = TYPE_F32;
goto after_basetype;
}
if(strprefix("f64",where)){
where += 3;
basetype = TYPE_F64;
goto after_basetype;
}
if(strprefix("double",where)){
where += 6;
basetype = TYPE_F64;
goto after_basetype;
}
if(strprefix("void",where)){
where += 4;
basetype = TYPE_VOID;
goto after_basetype;
}
puts(syntax_fail_pref);
puts("Could not identify base type while parsing type, here:");
puts(where);
puts("Line:");
puts(line_copy);
exit(1);
after_basetype:;
ptrlevel = 0;
while(*where == '*'){
ptrlevel++;
where++;
}
/*TODO: Parse arrays*/
if(*where == '['){
where++; /*consume opening ccb*/
arrlen = matou(where);
loc_ebrack = strfind(where, "]");
if(loc_ebrack == -1){
puts(syntax_fail_pref);
puts("You cannot have an array type without a closing curly brace.");
puts("Line:");
puts(line_copy);
exit(1);
}
if(arrlen == 0){
puts(syntax_fail_pref);
puts("You may not declare a zero-length array type.");
puts("Line:");
puts(line_copy);
exit(1);
}
where += loc_ebrack;
where++; /*skip the ending bracket as well.*/
}
t.basetype = basetype;
t.is_lvalue = 1;
t.ptrlevel = ptrlevel;
t.arraylen = arrlen;
if(out)
*out = where;
return t;
}
static uint64_t type_getsz(type t){
if(t.arraylen){
type q = t;
q.arraylen = 0;
return t.arraylen * type_getsz(q);
}
if(type_is_ptr(t)) return 8;
if(t.basetype == TYPE_U8 || t.basetype == TYPE_I8) return 1;
if(t.basetype == TYPE_U16 || t.basetype == TYPE_I16) return 2;
if(t.basetype == TYPE_U32 || t.basetype == TYPE_I32|| t.basetype == TYPE_F32) return 4;
if(t.basetype == TYPE_U64 || t.basetype == TYPE_I64|| t.basetype == TYPE_F64) return 8;
if(t.basetype == TYPE_VOID) return 0; /*void. */
if(t.basetype == TYPE_STRUCT){
/*TODO*/
puts(internal_fail_pref);
puts("Getting the size of a struct type is currently unimplemented.");
puts("Line:");
puts(line_copy);
exit(1);
}
/*
TODO:
*/
puts(internal_fail_pref);
puts("Tried to get the size of an unknown type!");
puts("Line:");
puts(line_copy);
exit(1);
}
void parse_vardecl(char* where, char** out){
type t;
unsigned long len;
char saved_character;
scopevar* setme;
unsigned long x; /*loop counter variable for checking matching identifiers.*/
unsigned long size_of_type = 0;
char t_is_primitive;
setme = scopevars + scope_nvars;
setme->is_static_allocated = 0;
setme->is_stack_allocated = 0; /*Default.*/
setme->depth = scope_depth; /*Used when determining end-of-scope rules.*/
if(scope_nvars == SCOPE_MAX_VARS){
puts(general_fail_pref);
puts("Too many scope variables are declared.");
puts("Line:");
puts(line_copy);
}
while(isspace(*where))where++;
t = parse_type(where, &where);
t_is_primitive = type_is_primitive(t);
if(scope_variables_must_fit_into_register){
if(!t_is_primitive){
puts(syntax_fail_pref);
puts("Function Arguments may not be arrays or structs. They may be pointers or primitives.");
puts("Line:");
puts(line_copy);
exit(1);
}
}
size_of_type = type_getsz(t);
/*Require at least one space...*/
if(!isspace(*where)){
puts(syntax_fail_pref);
puts("Expected space character during variable declaration parsing. Line:");
puts(line_copy);
exit(1);
}
while(isspace(*where))where++;
/*walk the string until we hit a non-identifier character.*/
for(len = 0;
where[len]!='\0';
len++
){
if(isalnum(where[len]) || where[len] == '_') continue;
break;
}
if(len == 0){
puts(syntax_fail_pref);
puts("Invalid Local Variable Identifier.");
puts("Line:");
puts(line_copy);
exit(1);
}
if(len > SCOPE_MAX_VARNAME_LEN){
puts(syntax_fail_pref);
puts("Variable name has too many characters:");
saved_character = where[len];
where[len] = '\0';
puts(where);
where[len] = saved_character;
puts("Line:");
puts(line_copy);
exit(1);
}
if(isdigit(where[0])){
puts(syntax_fail_pref);
puts("No local variables may start with a number.");
puts("Line:");
puts(line_copy);
exit(1);
}
/*write the variables name and type to scopevars.*/
saved_character = where[len];
where[len] = '\0';
if(
streq("return",where)
||streq("var",where)
||streq("gvar",where)
||streq("pushall",where)
||streq("popall",where)
){
puts(syntax_fail_pref);
puts("This local variable name is reserved:");
puts(where);
puts("Line:");
puts(line_copy);
exit(1);
}
/*check for duplicate identifiers.*/
for(x = 0; x < scope_nvars;x++)
if(streq(scopevars[x].name, where)){
puts("Duplicate identifier declared.");
puts("Identifier:");
puts(where);
puts("Line:");
puts(line_copy);
exit(1);
}
/*CODE GENERATION!
>Allocate space on the stack.
>Assign that stack pointer to a register.
*/
mstrcpy(setme->name, where);
if(!t_is_primitive){
strcat(buf3, "im64 $+0!, q%");
mutoa(buf3 + strlen(buf3), size_of_type);
strcat(buf3, "%;");
/*Assign the pointer...*/
strcat(buf3, "getstp $&");
strcat(buf3, setme->name);
strcat(buf3, ";");
strcat(buf3, "getstp $+1!;iadd $+0!,$+1!;setstp $+0!;");
setme->is_stack_allocated = 1;
}
where[len] = saved_character;
scope_nvars++;
setme->t = t;
/*Skip the identifier...*/
where += len;
if(out)
*out = where;
return;
}
/*appends appropriate code generation to buf2.*/
void parse_gvardecl(char* where, char** out){
type t;
unsigned long len;
unsigned long i; /*loop counter*/
char saved_character;
scopevar* setme;
unsigned long x; /*loop counter variable for checking matching identifiers.*/
unsigned long size_of_type = 0;
setme = gvars + scope_gvars;
setme->is_stack_allocated = 0;
setme->is_static_allocated = 1; /*They are always pointers!*/
setme->depth = 0; /*irrelevant.*/
if(scope_gvars == SCOPE_MAX_VARS){
puts(general_fail_pref);
puts("Too many global variables are declared.");
puts("Line:");
puts(line_copy);
}
while(isspace(*where))where++;
t = parse_type(where, &where);
size_of_type = type_getsz(t);
/*Require at least one space...*/
if(!isspace(*where)){
puts(syntax_fail_pref);
puts("Expected space character during variable declaration parsing.");
puts("Line:");
puts(line_copy);
exit(1);
}
while(isspace(*where))where++;
/*walk the string until we hit a non-identifier character.*/
for(len = 0;
where[len]!='\0';
len++
){
if(isalnum(where[len]) || where[len] == '_') continue;
break;
}
if(len == 0){
puts(syntax_fail_pref);
puts("Invalid Global Variable Identifier.");
puts("Line:");
puts(line_copy);
exit(1);
}
if(len > SCOPE_MAX_VARNAME_LEN){
puts(syntax_fail_pref);
puts("Variable name has too many characters:");
saved_character = where[len];
where[len] = '\0';
puts(where);
where[len] = saved_character;
puts("Line:");
puts(line_copy);
exit(1);
}
if(isdigit(where[0])){
puts(syntax_fail_pref);
puts("No local variables may start with a number.");
puts("Line:");
puts(line_copy);
exit(1);
}
/*write the variables name and type to scopevars.*/
saved_character = where[len];
where[len] = '\0';
if(streq("return",where)){
puts(syntax_fail_pref);
puts("You may not name a variable 'return'.");
puts("Line:");
puts(line_copy);
exit(1);
}
if(streq("var",where)){
puts(syntax_fail_pref);
puts("You may not name a variable 'var'.");
puts("Line:");
puts(line_copy);
exit(1);
}
/*check for duplicate identifiers.*/
for(x = 0; x < scope_gvars;x++)
if(streq(gvars[x].name, where)){
puts("Duplicate global identifier declared.");
puts("Identifier:");
puts(where);
puts("Line:");
puts(line_copy);
exit(1);
}
mstrcpy(setme->name, where);
where[len] = saved_character;
scope_gvars++;
setme->t = t;
/*
CODE GENERATION!
Declare storage. We concatenate onto buf3...
*/
strcat(buf3, "asm_label\\loc_gvar_");
strcat(buf3, setme->name);
strcat(buf3, ";");
(i = size_of_type);
/*Write out storage.*/
while(i >= 8){strcat(buf3, "qwords 0;");i-=8;}
while(i >= 4){strcat(buf3, "longs 0;");i-=4;}
while(i >= 2){strcat(buf3, "shorts 0;");i-=2;}
while(i >= 1){strcat(buf3, "bytes 0;");i-=1;}
/*Skip the identifier...*/
where += len;
if(out)
*out = where;
return;
}
void parse_arglist(char* where, char** out){
while(isspace(*where))where++;
/*Empty argument list?*/
if(*where == /*(*/')'){
where++;
if(out) *out = where;
return;
}
while(1){
while(isspace(*where))where++;
scope_variables_must_fit_into_register = 1;
parse_vardecl(where, &where);
scope_variables_must_fit_into_register = 0;
while(isspace(*where))where++;
if(*where == /*(*/')')
{
where++;
if(out) *out = where;
return;
}
if(*where != ','){
puts(syntax_fail_pref);
puts("Expected comma or closing parentheses in argument list.");
puts("Line:");
puts(line_copy);
exit(1);
}
where++;
}
}
static void handle_dollar_open_ccb(){
/*TODO: Handle previously malloc'd scope variable names.*/
scope_variables_must_fit_into_register = 0;
if(scope_is_active == 0){
scope_depth = 0; /*No scopes.*/
scope_nvars = 0;
}
if(scope_is_active){
line[0] = '\0';
scope_depth++;
return;
}
scope_is_active = 1;
scope_has_retval = 0;
char* s = line + 2;
while(isspace(*s)) s++; /*skip whitespace.*/
if(*s != '(' /*)*/){
puts(syntax_fail_pref);
puts("Initial Scope syntax REQUIRES a function prototype, including OPEN PARENTHESES.");
puts("Line:");
puts(line_copy);
exit(1);
}
s++; /*consume open parentheses.*/
while(isspace(*s)) s++; /*skip whitespace.*/
parse_arglist(s, &s);
while(isspace(*s)) s++; /*skip whitespace.*/
/*Check for the presence of ->*/
if(strprefix("->",s)){
s+=2;
while(isspace(*s)) s++; /*skip whitespace.*/
scope_retval_type = parse_type(s, &s);
if(!type_is_primitive(scope_retval_type)){
puts(syntax_fail_pref);
puts("Functions are never allowed to return a struct or array. They may be pointers or primitives.");
puts("Line:");
puts(line_copy);
exit(1);
}
scope_has_retval = 1;
while(isspace(*s)) s++; /*skip whitespace.*/
}
if(s[0] == '\0'){
goto end; /*Done!*/
}
if(s[0] != ':'){
puts(syntax_fail_pref);
puts("Scope declaration requires colon after the parentheses. Examples:\n${():\n${(int a, int b)->int:"); /*}}*/
puts("Line:");
puts(line_copy);
exit(1);
}
s++; /*consume colon*/
buf3[0] = 0;
/*No longer allow local variable declarations in initial scope.*/
end:;
line[0] = '\0';
return;
}
static void handle_dollar_close_ccb(){
uint64_t stack_usage;
unsigned long stackmanip1;
unsigned long stackmanip2;