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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
|
# Location enum
int VLOC_DATA = 2
int VLOC_STCK = 1
int VLOC_LITL = 0
# Should be -2
int32 PTYPE_NONE = 2
# Should be -1
int32 PTYPE_PTR = 1
int32 PTYPE_REF = 0
# 1 Arr is ptr to arr, larger #s are static size arrs
int32 PTYPE_ARR = 1
~uint8 PRIM_CSV_BOO = "bool\0"
~uint8 PRIM_CSV_INT = "int,int8,int16,int32,int64,uint,uint8,uint16,uint32,uint64\0"
~uint8 PRIM_CSV_FLT = "float,float32,float64\0"
# Should dispose of this constructed string
# 1-8 are ax, bx, cx, dx, si, di, sp, bp
# 9-16 are r8, r9, r10, r11, r12, r13, r14, r15
# 17-32 are xmm0, xmm1, xmm2, ..., xmm15
/; reg_string (int r, int size) [~uint8]
utils.Vector out
out.init(1)
uint8 add
/; if (r < 9)
/; if (size == 4)
add = 'e'
out.push(~add)
;; else if (size == 8)
add = 'r'
out.push(~add)
;/
add = 'a'
/; if (r < 5)
add = add + r - 1
;; else if (r == 5 || r == 7)
add = 's'
;; else if (r == 6)
add = 'd'
;; else if (r == 8)
add = 'b'
;/
out.push(~add)
/; if (r == 5 || r == 6)
add = 'i'
out.push(~add)
;; else if (r == 7 || r == 8)
add = 'p'
out.push(~add)
;; else if (size !== 1)
add = 'x'
out.push(~add)
;/
/; if (size == 1)
add = 'l'
out.push(~add)
;/
;; else if (r < 17)
add = 'r'
out.push(~add)
~uint8 num = utils.int_to_str(r - 1)
out.push_cstr(num)
_delete(num)
/; if (size == 1)
add = 'b'
out.push(~add)
;; else if (size == 2)
add = 'w'
out.push(~add)
;; else if (size == 4)
add = 'd'
out.push(~add)
;/
;; else if (r < 33)
out.push_cstr("xmm\0")
~uint8 num = utils.int_to_str(r - 17)
out.push_cstr(num)
_delete(num)
;/
return out.as_cstr()
;/
# Valid value states:
# When loc is DATA value is in the data section and the name is actually the label
# When loc is STCK value is offset from rbp pointer using the offset
# When loc is 0 then offset represents a numeric literal
# When loc is positive then the variable exists in that register*
# Structs and references don't exactly exist in a register and so must
# be combined with the offset to get a resonable approximation
# So when computing the location of a standard type...
# ...just load from the register
# ...Unless it's a ref in which case take into account the offset
# ...Or it's a DATA in which case load from rel label
# ...Or it's on the stack in which case load from
# ...Or it's a literal in which case just use the literal value
# So when computing the location of a struct...
# ...Load as an offset from a register
# ...Unless it's on the stack (offset from rbp)
# ...Or it's in DATA (offset from rel label)
# ...Or it's a ref (lea first ref and then just load direct values from there)
struct Var {
~uint8 name,
~Struct _type,
utils.Vector ptrc,
int loc, offset,
~parse.Node _tn, _id
}
/; method Var
###########################
# Init and copy functions #
###########################
# Dummy init
/; _init (~Struct _type)
self.name = utils.strcpy("dummy\0")
self.ptrc.init(4)
self.loc = 0
self.offset = 0
self._type = _type
;/
# Initial init function, requires type node and
# identifier node
/; init (~parse.Node tn, id)
self.name = utils.strcpy(id`.data)
self.ptrc.init(4)
self.loc = 0
self.offset = 0
self._tn = tn
self._id = id
;/
# Deep copy the variable
/; copy [Var]
Var out = self.shallow_copy()
/; loop (int i = 0; i < self.ptrc.count) [i++]
~int32 p = self.ptrc.get(i)
out.ptrc.push(p)
;/
return out
;/
# A copy without the pointer chain
/; shallow_copy [Var]
Var out
out.init(self._tn, self._id)
out._type = self._type
out.loc = self.loc
out.offset = self.offset
return out
;/
#############################
# Variable inspection funcs #
#############################
# Get a pointer to the top of the pointer chain, returns
# null if the pointer chain is empty
/; top_ptrc [~int32]
# Sanity
/; if (self.ptrc.count < 1)
return NULL
;/
~int32 out = self.ptrc.get(self.ptrc.count - 1)
return out
;/
/; is_ptrc (int idx, int32 ptype) [bool]
/; if (idx !< self.ptrc.count)
return false
;/
idx = self.ptrc.count - idx
idx = idx - 1
~int32 chk = self.ptrc.get(idx)
return chk` == ptype
;/
# Returns true if the variable is a reference
/; is_ref [bool]
return self.is_ptrc(0, 0)
;/
# Returns true if two or more ref layers
/; double_ref [bool]
/; if (self.is_ref() == false)
return false
;/
return self.is_ptrc(1, 0)
;/
# Ref level
/; max_ref [int]
int out = 0
/; loop (int i = 0; i < self.ptrc.count)
/; if (self.is_ptrc(i, 0) == true)
out++
;; else
i = self.ptrc.count
;/
;/
return out
;/
# Returnes true if the underlying type is a signed integer
/; is_signed [bool]
/; if (_is_primitive(self._type`.name) !== 0)
return self._type`.name{0} == 'i'
;/
return false
;/
# Returns true if the variable is a pointer
/; is_ptr [bool]
~int32 p = self.top_ptrc()
/; if (p == NULL)
return false
;/
return p` < 0
;/
# Returns true if the variable is an array
/; is_arr [bool]
~int32 p = self.top_ptrc()
/; if (p == NULL)
return false
;/
return p` > 0
;/
# Whether the variable can be stored within a register
/; regable [bool]
~int p
/; if (self.ptrc.count > 0)
return true
;/
return _is_primitive(self._type`.name) !== 0
;/
/; is_struct [bool]
# Check first if we are a pointer of some sort
~int32 p
/; loop (int i = 0; i < self.ptrc.count) [i++]
p = self.ptrc.get(i)
/; if (p` !== 0)
return false
;/
;/
return _is_primitive(self._type`.name) == 0
;/
# Compute and add the correct pointer chain value for an array
# type prefix
/; _arr_ptr(~parse.Node a)
int32 ptr = 1
/; if (a`.sub.count > 0)
~parse.Node l = a`.sub.get(0)
ptr = utils.cstr_to_int(l`.data)
/; if (ptr < 2)
return
;/
;/
self.ptrc.push(~ptr)
;/
# The "actual size" of the variable (if we were to do a mov on it
# how much space would we need)
/; actual_size [uint]
/; if (self.ptrc.count > 0)
return 8
;; else if (self._type == NULL)
return 0
;/
return self._type`.size
;/
/; type_size [uint]
/; loop (int i = 0; i < self.ptrc.count) [i++]
~int32 p = self.ptrc.get(i)
/; if (p` !== 0)
return 8
;/
;/
return self._type`.size
;/
#####################################
# Variable manipulation (comp time) #
#####################################
# Reverse the pointer chain
/; _reverse_ptrc
int max = self.ptrc.count / 2
~int32 l, r
/; loop (int i = 0; i < max) [i++]
l = self.ptrc.get(i)
r = self.ptrc.get(self.ptrc.count - (i + 1))
int32 tmp = l`
l` = r`
r` = tmp
;/
;/
# Sets up both the ptrc and the _type members, requires
# parent module for resolution of types
/; _resolve_type (~Module parent)
int idx = 0
bool running = true
~parse.Node t, _tn
_tn = self._tn
# Pre-op pointer
/; loop (running == true)
/; if (idx !< _tn`.sub.count)
running = false
;; else
t = _tn`.sub.get(idx)
/; if (t`._type == parse.NTYPE_PRE_OP)
/; if (utils.strcmp(t`.data, "~\0") == true)
int32 ptr = 0
ptr = ptr - PTYPE_PTR
self.ptrc.push(~ptr)
;; else
self._arr_ptr(t)
;/
;; else
running = false
;/
;/
/; if (running == true)
idx++
;/
;/
self._reverse_ptrc()
# After pre-ops comes id
utils.Vector strv
strv.init(8)
running = true
/; loop (running == true)
/; if (idx !< _tn`.sub.count)
running = false
;; else
t = _tn`.sub.get(idx)
/; if (t`._type == parse.NTYPE_ID)
~uint8 str = t`.data
strv.push(~str)
;; else
running = false
;/
;/
/; if (running == true)
idx++
;/
;/
# Main type resolution
# TODO: FUNCTION POINTER
self._type = parent`.find(SEARCH_STRUCT, ~strv)
strv.end()
# Post-op pointer
running = true
/; loop (running == true)
/; if (idx !< _tn`.sub.count)
running = false
;; else
t = _tn`.sub.get(idx)
/; if (t`._type == parse.NTYPE_POST_OP)
int32 ptr = 0
self.ptrc.push(~ptr)
;/
;/
/; if (running == true)
idx++
;/
;/
;/
# Compile the variable into the data section
/; _static_compile (~Module parent, ~CompBuf buf)
# TODO: everything
;/
/; ptr_push (int32 p)
self.ptrc.push(~p)
;/
/; ptr_pop
self.ptrc.pop()
;/
/; end
_delete(self.name)
self.ptrc.end()
;/
####################################
# Variable manipulation (run time) #
####################################
# Returns true if the variable is known to be stored in memory
/; in_mem [bool]
/; if (self.loc < 1)
return true
;/
~int32 ptr = self.top_ptrc()
/; if (ptr !== NULL)
/; if (ptr` == 0)
return true
;; else if (ptr` > 1)
return true
;/
;/
return false
;/
# Typechecking
# Typechecking structs
/; _tc_struct (~Var other) [bool]
/; if (other`.is_struct() == false)
return false
;/
~void a = self._type
~void b = other`._type
return a == b
;/
/; _tc_prim (~Var other) [bool]
# Allow implicit ptr conversions
/; if (self.is_ptr() == true)
return other`.is_ptr()
;/
return false
;/
# Operations
# Helper to gen register when setting a struct
/; _set_struct_r (~CompBuf buf, int reg)
~uint8 r = reg_string(reg, 8)
buf`.add_c(" ; putting struct address into register\n\0")
# Initial deref or move
/; if (self.ptrc.count > 0)
buf`.add_c(" mov \0")
;; else
buf`.add_c(" lea \0")
;/
buf`.add_c(r)
buf`.add_c(", [\0")
# Reg, stack, or data
/; if (self.loc + VLOC_DATA == 0)
buf`.add_c("rel \0")
buf`.add_c(self.name)
;; else if (self.loc + VLOC_STCK == 0)
~uint8 get_reg = reg_string(8, 8)
buf`.add_c(get_reg)
_delete(get_reg)
;; else
~uint8 get_reg = reg_string(self.loc, 8)
buf`.add_c(get_reg)
_delete(get_reg)
;/
# Deal with offset
/; if (self.offset !== 0)
int o = self.offset
/; if (o < 0)
o = 0 - o
buf`.add_c(" - \0")
;; else
buf`.add_c(" + \0")
;/
~uint8 its = utils.int_to_str(o)
buf`.add_c(its)
_delete(its)
;/
buf`.add_c("] ; initial struct addr move\n\0")
# For as many more times as there are references
/; loop (int i = 1; i < self.ptrc.count) [i++]
buf`.add_c(" mov \0")
buf`.add_c(r)
buf`.add_c(", [\0")
buf`.add_c(r)
buf`.add_c("] ; reference chain\n\0")
;/
_delete(r)
;/
# Helper to properly move a struct in memory
/; _set_struct(~CompBuf buf, ~Var other)
# Typecheck
/; if (self._tc_struct(other) == false)
_printf("ERROR: Types do not match when setting struct. [\0")
_printf(self._type`.name)
_printf("] !== [\0")
_printf(other`._type`.name)
_printf("]\n\0")
return
;/
# Have to get struct address (to set) into rdi
self._set_struct_r(buf, 6)
# Have to get struct address (to read) into rsi
other`._set_struct_r(buf, 5)
# Setup move size
~uint8 str
str = utils.int_to_str(self._type`.size)
buf`.add_c(" mov rcx, \0")
buf`.add_c(str)
buf`.add_c(" ; size of struct [\0")
buf`.add_c(self._type`.name)
buf`.add_c("] in bytes\n\0")
_delete(str)
# Move byte (rcx times)
buf`.add_c(" rep movsb ; move struct\n\0")
;/
# Helper to properly get the lhs of a set
/; _set_prim_l (~CompBuf buf) [~uint8]
/; if (self.loc == 0)
_printf("SET WAS CALLED ON A LITERAL! THIS IS A COMPILER ERROR!\0")
return utils.strcpy("ERR SHOULD NOT HAPPEN - TRIED TO SETPRIML ON A LITERAL\0")
;/
# Base of address/register
~uint8 out
/; if (self.loc > 0)
/; if (self.in_mem() == true)
out = reg_string(self.loc, 8)
/; if (self.offset !== 0)
utils.Vector vout
vout.from_cstr(out)
int off = self.offset
/; if (off < 0)
off = 0 - off
vout.push_cstr(" - \0")
;; else if (off > 0)
vout.push_cstr(" + \0")
;/
_delete(out)
out = utils.int_to_str(off)
vout.push_cstr(out)
_delete(out)
out = vout.as_cstr()
;/
;; else
uint sz = self.type_size()
out = reg_string(self.loc, sz)
;/
;; else
utils.Vector vout
/; if (self.loc + 1 == 0)
# Stack
vout.from_cstr("rbp\0")
;; else
vout.from_cstr("rel \0")
vout.push_cstr(self.name)
;/
int off = self.offset
/; if (off < 0)
off = 0 - off
vout.push_cstr(" - \0")
;; else if (off > 0)
vout.push_cstr(" + \0")
;/
/; if (off !== 0)
out = utils.int_to_str(off)
vout.push_cstr(out)
_delete(out)
;/
out = vout.as_cstr()
/; if (self.is_ref() == true)
# Need to move into rdi
buf`.add_c(" mov rdi, [\0")
buf`.add_c(out)
buf`.add_c("]\n\0")
_delete(out)
out = utils.strcpy("rdi\0")
;/
;/
# If in memory we need to wrap in []
/; if (self.in_mem() == true)
utils.Vector vout
vout.from_cstr("[\0")
vout.push_cstr(out)
vout.push_cstr("]\0")
_delete(out)
out = vout.as_cstr()
;/
# Loop and make sure we are dereferencing properly
/; loop (int i = 1; i < self.ptrc.count) [i++]
/; if (self.is_ptrc(i, 0) == true)
buf`.add_c(" mov rdi, [rdi] ; auto deref\n\0")
;; else
i = self.ptrc.count
;/
;/
/; if (self.double_ref() == true)
_delete(out)
out = utils.strcpy("[rdi]\0")
;/
return out
;/
# Helper to properly get the rhs of a set
/; _set_prim_r (~CompBuf buf, ~Var lhs) [~uint8]
/; if (self.loc == 0)
~uint8 o = utils.int_to_str(self.offset)
return o
;/
~uint8 out = self._set_prim_l(buf)
/; if (self.in_mem() == true)
utils.Vector vout
uint ts = self.type_size()
/; if (ts == 1)
vout.from_cstr("byte \0")
;; else if (ts == 2)
vout.from_cstr("word \0")
;; else if (ts == 4)
vout.from_cstr("dword \0")
;; else if (ts == 8)
vout.from_cstr("qword \0")
;/
vout.push_cstr(out)
_delete(out)
out = vout.as_cstr()
;/
# Sign extend if required
bool ext = false
uint R = self.type_size()
uint L = lhs`.type_size()
/; if (R < L)
ext = true
~uint8 vout = reg_string(5, L)
/; if (lhs`.is_signed() == true && self.is_signed() == true)
/; if (R < 4)
buf`.add_c(" movsx \0")
;; else
buf`.add_c(" movsxd \0")
;/
buf`.add_c(vout)
buf`.add_c(", \0")
;; else if (R < 4)
buf`.add_c(" movzx \0")
buf`.add_c(vout)
buf`.add_c(", \0")
;; else
buf`.add_c(" mov esi, \0")
;/
buf`.add_c(out)
buf`.add_c("\n\0")
_delete(out)
out = vout
;/
/; if (ext == false)
/; if (self.in_mem() == true && lhs`.in_mem() == true)
~uint8 vout = reg_string(5, L)
buf`.add_c(" mov \0")
buf`.add_c(vout)
buf`.add_c(", \0")
buf`.add_c(out)
buf`.add_c("\n\0")
_delete(out)
out = vout
;/
;/
return out
;/
# Set this Variable to the value of other
/; set (~CompBuf buf, ~Var other)
# Options:
# - If builtin then move based on size (byte, word, dword, qword)
# - If pointer then move qword
# - If struct then move via rep movsb
/; if (self.is_struct() == true)
# Struct set
self._set_struct(buf, other)
return
;/
# Generate lhs set and rhs set
~uint8 sr = other`._set_prim_r(buf, ~self)
~uint8 sl = self._set_prim_l(buf)
buf`.add_c(" mov \0")
buf`.add_c(sl)
buf`.add_c(", \0")
buf`.add_c(sr)
buf`.add_c("\n\0")
_delete(sl)
_delete(sr)
;/
# Set the address which this reference points to
/; set_ref (~CompBuf buf, ~Var other, int depth)
# count how many refs
int max_depth = self.max_ref()
/; if (depth !< max_depth)
_print_num("ERROR: Unable to set reference, depth %d equals or exceeds max_depth \0", depth)
_print_num("%d\n\0", max_depth)
_printf(" When taking ref of variable \"\0")
_printf(other`.name)
_printf("\" into variable \"\0")
_printf(self.name)
_printf("\"\n\0")
return
;/
int32 set = 0
set = set - 1
max_depth = copy.ptrc.count - max_depth
Var copy = self.copy()
~int32 ptr = copy.ptrc.get(max_depth + depth)
ptr` = set
copy.set(buf, other)
copy.end()
;/
# Copy the pointer to this variable into the output reg (or not if not needed)
/; take_ptr (~CompBuf buf, int reg) [Var]
Var vcpy = self.copy()
/; if (self.in_mem() == false)
_printf("COMPILER ERROR: UNABLE TO GET REFERENCE OF VAR \"\0")
_printf(self.name)
_printf("\" PLEASE REPORT THIS BUG!\n\0")
return vcpy
;/
int32 set = 0
set = set - 1
/; if (vcpy.is_ref() == true)
# If we are a ref, we want to set the first one and that's it
int idx = 0
/; loop (int i = 0; i < vcpy.ptrc.count) [i++]
/; if (vcpy.is_ptrc(i, 0) == false)
i = vcpy.ptrc.count
;; else
set++
;/
;/
idx = vcpy.ptrc.count - idx
~int32 p = vcpy.ptrc.get(idx)
p` = set
;; else
/; if (self.is_struct() == true)
self._set_struct_r(buf, reg)
;; else
~uint8 source = self._set_prim_l(buf)
~uint8 rstr = reg_string(reg, 8)
buf`.add_c(" lea \0")
buf`.add_c(rstr)
buf`.add_c(", \0")
buf`.add_c(source)
buf`.add_c("\n\0")
_delete(source)
_delete(rstr)
;/
vcpy.loc = reg
vcpy.offset = 0
vcpy.ptr_push(set)
;/
return vcpy
;/
#######################
# Standard operations #
#######################
/; standard_op (~CompBuf buf, ~Var other, ~uint8 op_str)
~uint8 from_str = other`._set_prim_r(buf, ~self)
~uint8 to_str = self._set_prim_l(buf)
buf`.add_c(" \0")
buf`.add_c(op_str)
buf`.add_c(" \0")
buf`.add_c(to_str)
buf`.add_c(", \0")
buf`.add_c(from_str)
buf`.add_c("\n\0")
_delete(from_str)
_delete(to_str)
;/
/; add (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset + other`.offset
return
;/
self.standard_op(buf, other, "add\0")
;/
/; sub (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset - other`.offset
return
;/
self.standard_op(buf, other, "sub\0")
;/
/; and (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset & other`.offset
return
;/
self.standard_op(buf, other, "and\0")
;/
/; or (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset | other`.offset
return
;/
self.standard_op(buf, other, "or\0")
;/
/; xor (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset ^ other`.offset
return
;/
self.standard_op(buf, other, "xor\0")
;/
# Product op
/; mul (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset * other`.offset
return
;/
~uint8 from_str = other`._set_prim_r(buf, ~self)
~uint8 to_str = self._set_prim_l(buf)
/; if (self.in_mem() == true)
uint sz = self.type_size()
~uint8 ax = reg_string(1, sz)
# Move to ax register for the mul and move back afterward
buf`.add_c(" mov \0")
buf`.add_c(ax)
buf`.add_c(", \0")
buf`.add_c(to_str)
buf`.add_c("\n\0")
# Do actual product
buf`.add_c(" imul \0")
buf`.add_c(ax)
buf`.add_c(", \0")
buf`.add_c(from_str)
buf`.add_c("\n\0")
# Move back
buf`.add_c(" mov \0")
buf`.add_c(to_str)
buf`.add_c(", \0")
buf`.add_c(ax)
buf`.add_c("\n\0")
_delete(ax)
;; else
buf`.add_c(" imul \0")
buf`.add_c(to_str)
buf`.add_c(", \0")
buf`.add_c(from_str)
buf`.add_c("\n\0")
;/
_delete(from_str)
_delete(to_str)
;/
# Division op
/; _div(~CompBuf buf, ~Var other, int r)
~uint8 from_str = other`._set_prim_r(buf, ~self)
~uint8 to_str = self._set_prim_l(buf)
uint sz = self.type_size()
~uint8 rstr = reg_string(1, sz)
/; if (other`.loc == 0)
# Move literal into register
buf`.add_c(" mov rcx, \0")
buf`.add_c(from_str)
buf`.add_c("\n\0")
_delete(from_str)
from_str = reg_string(3, sz)
;/
# Move dividend into rax
buf`.add_c(" mov \0")
buf`.add_c(rstr)
buf`.add_c(", \0")
buf`.add_c(to_str)
buf`.add_c("\n\0")
/; if (self.is_signed() == true)
# Sign extend
/; if (sz == 1)
buf`.add_c(" movsx ax, al\n\0")
;; else if (sz == 2)
buf`.add_c(" cwd\n\0")
;; else if (sz == 4)
buf`.add_c(" cdq\n\0")
;; else if (sz == 8)
buf`.add_c(" cqo\n\0")
;/
buf`.add_c(" idiv \0")
;; else
# Zero out
buf`.add_c(" xor ")
/; if (sz == 1)
buf`.add_c("ah, ah\n\0")
;; else if (sz == 2)
buf`.add_c("dx, dx\n\0")
;; else if (sz == 4)
buf`.add_c("edx, edx\n\0")
;; else if (sz == 8)
buf`.add_c("rdx, rdx\n\0")
;/
buf`.add_c(" div \0")
;/
buf`.add_c(from_str)
buf`.add_c("\n\0")
# Fix for bytes
_delete(rstr)
/; if (sz == 1 && r == 4)
rstr = utils.strcpy("ah\0")
;; else
rstr = reg_string(r, sz)
;/
buf`.add_c(" mov \0")
buf`.add_c(to_str)
buf`.add_c(", \0")
buf`.add_c(rstr)
buf`.add_c("\n\0")
_delete(from_str)
_delete(to_str)
_delete(rstr)
;/
/; div (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset / other`.offset
return
;/
self._div(buf, other, 1)
;/
/; mod (~CompBuf buf, ~Var other)
/; if (self.loc == VLOC_LITL && other`.loc == VLOC_LITL)
self.offset = self.offset % other`.offset
return
;/
self._div(buf, other, 4)
;/
/; not (~CompBuf buf)
/; if (self.loc == VLOC_LITL)
self.offset = !self.offset
return
;/
~uint8 to_str = self._set_prim_l(buf)
buf`.add_c(" not \0")
/; if (self.in_mem() == true)
uint sz = self.type_size()
/; if (sz == 1)
buf`.add_c("byte \0")
;; else if (sz == 2)
buf`.add_c("word \0")
;; else if (sz == 4)
buf`.add_c("dword \0")
;; else if (sz == 8)
buf`.add_c("qword \0")
;/
;/
buf`.add_c(to_str)
buf`.add_c("\n\0")
_delete(to_str)
;/
/; neg (~CompBuf buf)
/; if (self.loc == VLOC_LITL)
int off = 0
self.offset = off - self.offset
return
;/
~uint8 to_str = self._set_prim_l(buf)
buf`.add_c(" neg \0")
/; if (self.in_mem() == true)
uint sz = self.type_size()
/; if (sz == 1)
buf`.add_c("byte \0")
;; else if (sz == 2)
buf`.add_c("word \0")
;; else if (sz == 4)
buf`.add_c("dword \0")
;; else if (sz == 8)
buf`.add_c("qword \0")
;/
;/
buf`.add_c(to_str)
buf`.add_c("\n\0")
_delete(to_str)
;/
/; member (~CompBuf buf, ~uint8 name) [Var]
/; if (self.is_struct() == false)
_printf("ERROR: Attempted to get a member named \"\0")
_printf(name)
_printf("\" from a primitive type \"\0")
_printf(self._type`.name)
_printf("\"\n\0")
_printf(" (If the previous type was in fact a structure then the variable was probably a pointer)\n\0")
return self.copy()
;/
~Var mbr = self`._type`.get_member(name)
/; if (mbr == NULL)
_printf("ERROR: member \"\0")
_printf(name)
_printf("\" not found in struct \"\0")
_printf(self._type`.name)
_printf("\n\0")
return self.copy()
;/
Var out = mbr`.copy()
/; if (self.is_ref() == true)
self._set_struct_r(buf, 5)
out.loc = 5
;; else
out.loc = self.loc
out.offset = out.offset + self.offset
;/
return out
;/
# Printing
/; _print (int idt)
_indent(idt)
_printf("{ Var : \0")
_printf(self.name)
_printf("\n\0")
_indent(idt + 1)
_printf("type: \0")
/; if (self._type !== NULL)
_printf(self._type`.name)
;; else
_printf("(nil)\0")
;/
_printf("\n\0")
_indent(idt + 1)
_printf("ptrc: \0")
~int32 istr
/; loop (int i = 0; i < self.ptrc.count) [i++]
istr = self.ptrc.get(i)
_print_num("%d \0", istr`)
;/
_printf("\n\0")
_indent(idt + 1)
_print_num("loc: %d\n\0", self.loc)
_indent(idt + 1)
_print_num("off: %d\n\0", self.offset)
_indent(idt)
_printf("}\n\0")
;/
;/
|