-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.cpp
919 lines (761 loc) · 26.5 KB
/
backend.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
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
#include "backend.h"
#include "asm_instructions.h"
#include "elfhdr.h"
const int TRASH = 228;
const int BREAK = 0;
const int CONITINUE = 1;
#define $ND_CHR node->data.ch
#define $ND_DBL node->data.dbl
#define $ND_STR node->data.str
#define $ND_LNG node->data_lng
#define $ND_TYP node->data_type
#define $CUR_VAR_DEF_FLAG vr_lists->var[cur_var_num].def_flag
#define $CUR_VAR_PARAM_FLAG vr_lists->var[cur_var_num].param_flag
#define $CUR_VAR_HASH vr_lists->var[cur_var_num].var_hash
#define $CUR_VAR_VAL vr_lists->var[cur_var_num].var_value
#define $CUR_FUNC jtable->func_jmp[jtable->tmp_func]
#define $FREE_VAR vr_lists->var[vr_lists->free]
#define $FREE vr_lists->free
#define $R node->right
#define $L node->left
#define $VISIT(node) \
if (node) \
VisitWriteCommands(node, vr_lists, fdata, jtable)
#define $VISIT_NEW_LIST(node, newlist) \
if (node) \
VisitWriteCommands(node, newlist, fdata, jtable)
#define $VISIT_CALC(node) \
(VisitCalcVal(node, vr_lists))
//==========================================================================
static int VisitCheckDefinedVal(Node* node, var_lists* vr_lists, int* def_value);
static double VisitCalcVal(Node* node, var_lists* vr_lists);
//==========================================================================
static int std_funcs_add (ELFfile* fdata, JMPtable* jtable);
static int pop_param_back (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int WriteCode (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int RelOpHash (Node* node);
//==========================================================================
static void visit_check_var_const_use_rbx (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static void visit_check_var_const_use_rax (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
//==========================================================================
static int VWC_stmt_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_op_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_op_case_equ (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_var_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_const_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_def_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_func_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_param_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_call_param_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_call_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_call_case_std_func (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable, int func_hash);
static int VWC_ret_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_if_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_desis_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_rel_op_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
static int VWC_while_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable);
//==========================================================================
int ELFCtor (ELFfile* fdata)
{
assert(fdata);
fdata->exe_file = fopen("run.me", "w");
if (fdata->exe_file == NULL)
assert(0 && "EXECUTABLE FILE PTR IS NULL !!");
ELFhdrCtor(&fdata->ehdr);
PhdrCtor(&fdata->phdr);
return 0;
}
//==========================================================================
int GenerateELFFile (Node* node)
{
assert(node);
var_lists* vr_lists = (var_lists*) calloc(1, sizeof(var_lists));
CheckPtr(vr_lists, "vr_lists callocation error!");
$FREE = 1;
vr_lists->var = (var_list*) calloc(VAR_MAX_CUNT, sizeof(var_list));
CheckPtr(vr_lists->var, "vr_lists->var callocation error!");
FILE* com_file = fopen("asm_file.txt", "w");
ELFfile* fdata = (ELFfile*) calloc(1, sizeof(ELFfile));
CheckPtr(fdata, "fdata callocation error!");
ELFCtor(fdata);
JMPtable* jtable = (JMPtable*) calloc(1, sizeof(JMPtable));
CheckPtr(jtable, "jtable callocation error!");
WriteCode(node, vr_lists, fdata, jtable);
system("chmod +x run.me");
free(vr_lists->var);
free(vr_lists);
free(jtable);
fclose(com_file);
fclose(fdata->exe_file);
//system("./run.me");
return 0;
}
//==========================================================================
int WriteCode(Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
$WRITE_CONST(0, double); // fill code buffer with trash for memory alignment
std_funcs_add(fdata, jtable);
$WRITE_OPCDE(push_rbp);
$WRITE_OPCDE(mov_rbp_rsp);
$WRITE_OPCDE(sub_rsp);
int sub_rsp_ip = fdata->ip;
$WRITE_CONST(TRASH, int); // substract from rsp the number of local variables to be able to use stack
VisitWriteCommands(node, vr_lists, fdata, jtable);
$WRITE_CONST_IP(($FREE - 1) * SIZE_OF_REG, int, sub_rsp_ip); // $FREE - 1 = number of local variables
$WRITE_OPCDE(my_exit);
FixPhdr(fdata);
fwrite((char*)(&fdata->ehdr), sizeof(char), sizeof(Elf64_Ehdr), fdata->exe_file);
fwrite((char*)(&fdata->phdr), sizeof(char), sizeof(Elf64_Phdr), fdata->exe_file);
fwrite((char*)(&fdata->code), sizeof(char), fdata->ip, fdata->exe_file);
}
//==========================================================================
int VisitWriteCommands (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
switch($ND_TYP)
{
case STATEMENT://rework doesnt needed
{
return VWC_stmt_case(node, vr_lists, fdata, jtable);
}
case OPERATOR://DONE!!!!!
{
return VWC_op_case(node, vr_lists, fdata, jtable);
}
case VARIABLE://reworked
{
return VWC_var_case(node, vr_lists, fdata, jtable);
}
case CONSTANT://reworked
{
return VWC_const_case(node, vr_lists, fdata, jtable);
}
case DEFINE://done
{
return VWC_def_case(node, vr_lists, fdata, jtable);
}
case FUNCTION://reworked
{
return VWC_func_case(node, vr_lists, fdata, jtable);
}
case PARAMETER://norm vrodeby
{
return VWC_param_case(node, vr_lists, fdata, jtable);
}
case PARAMETER_CALL://done
{
return VWC_call_param_case(node, vr_lists, fdata, jtable);
}
case CALL://done
{
return VWC_call_case(node, vr_lists, fdata, jtable);
}
case RETURN://done
{
return VWC_ret_case(node, vr_lists, fdata, jtable);
}
case IF://done
{
return VWC_if_case(node, vr_lists, fdata, jtable);
}
case DESISION://done
{
return VWC_desis_case(node, vr_lists, fdata, jtable);
}
case REL_OPERATOR:
{
return VWC_rel_op_case(node, vr_lists, fdata, jtable);
}
case WHILE://done
{
return VWC_while_case(node, vr_lists, fdata, jtable);
}
defaul:
{
assert(0 && "Shit happen(s) (undefined case in VisitWriteCommands switch)");
}
}// Эссэ "Ночной кодинг))"
return 0;
}
//==========================================================================
int VWC_stmt_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
$VISIT($R);
$VISIT($L);
return BREAK;
}
//==========================================================================
int VWC_op_case_equ (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if ($L->data_type != VARIABLE)
assert(0 && "INCORRECT INPUT! YOU CAN ONLY ASSIGN A VALUE TO VARIABLE!\n");
int cur_var_hash = murmurHash($L->data.str, $L->data_lng);
int cur_var_num = FindVariable(vr_lists, cur_var_hash);
int def_val = DEFINED;
VisitCheckDefinedVal($R, vr_lists, &def_val);
if (def_val == DEFINED && vr_lists->while_flag == NOT_IN_WHILE)
{
$CUR_VAR_DEF_FLAG = DEFINED;
double var_val = $VISIT_CALC($R);
$CUR_VAR_VAL = var_val;
$WRITE_OPCDE(mov_rax_const);
$WRITE_CONST((int)(var_val * 100), int);
}
else
{
$CUR_VAR_DEF_FLAG = UNDEFINED;
visit_check_var_const_use_rax($R, vr_lists, fdata, jtable);
}
$WRITE_OPCDE(mov_mem_rbp_rax);
$WRITE_CONST(cur_var_num * (-SIZE_OF_REG), int);
return BREAK;
}
//==========================================================================
int VWC_op_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if ($ND_CHR == EQL)
return VWC_op_case_equ(node, vr_lists, fdata, jtable);
visit_check_var_const_use_rax($L, vr_lists, fdata, jtable);
visit_check_var_const_use_rbx($R, vr_lists, fdata, jtable);
switch ($ND_CHR)
{
case MUL:
{
$WRITE_OPCDE(imul_rax_rbx);
$WRITE_OPCDE(idiv_rax_100);
break;
}
case SUB:
{
$WRITE_OPCDE(sub_rax_rbx);
break;
}
case ADD:
{
$WRITE_OPCDE(add_rax_rbx);
break;
}
case DIV:
{
$WRITE_OPCDE(imul_rax_100);
$WRITE_OPCDE(xor_rdx_rdx);
$WRITE_OPCDE(div_rbx);
break;
}
case DEG:
{
assert(ERRORR && "Sorry, but exponentiation is not allowed))");
}
default:
{
if ($ND_CHR == '=')
return 0;
assert(ERRORR && "case operator incorrect input");
}
}
return BREAK;
}
//==========================================================================
int VWC_var_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
int cur_var_hash = murmurHash($ND_STR, node->data_lng);
int cur_var_num = FindVariable(vr_lists, cur_var_hash);
if ($CUR_VAR_PARAM_FLAG == FUNC_PARAM)
{
$WRITE_CONST((cur_var_num + 1) * SIZE_OF_REG, int); // +1 is used, to deal with "push rbp"
}
else
{
$WRITE_CONST(cur_var_num * (-SIZE_OF_REG), int);
}
return BREAK;
}
//==========================================================================
int VWC_const_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
int temp = $ND_DBL;
temp *= 100;
$WRITE_CONST(temp, int);
return BREAK;
}
//==========================================================================
int VWC_def_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
$WRITE_OPCDE(jmp); // function skip jump
int func_skip_jmp_ip = fdata->ip;
$WRITE_CONST(fdata->ip, int);
var_lists vr_lists_new = {};
vr_lists_new.var = (var_list*) calloc(VAR_MAX_CUNT, sizeof(var_list));
CheckPtr(vr_lists_new.var, "vr_lists_new.var callocation error!");
vr_lists_new.free = 1;
$VISIT_NEW_LIST($L, &vr_lists_new);
$WRITE_OPCDE(push_rbp);
$WRITE_OPCDE(mov_rbp_rsp);
$WRITE_OPCDE(sub_rsp);
int ip_sub_rsp = fdata->ip; // save current ip to rewrite it lately
$WRITE_CONST(TRASH, int);
$VISIT_NEW_LIST($R, &vr_lists_new);
$WRITE_CONST_IP((vr_lists_new.free -1) * (SIZE_OF_REG), int, ip_sub_rsp);
$WRITE_OPCDE(mov_rsp_rbp);
$WRITE_OPCDE(pop_rbp);
$WRITE_OPCDE(ret);
$WRITE_CONST_IP(fdata->ip - func_skip_jmp_ip - sizeof(int), int, func_skip_jmp_ip);
free(vr_lists_new.var);
return BREAK;
}
//==========================================================================
int VWC_func_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
$CUR_FUNC.func_hash = murmurHash($L->data.str, $L->data_lng);
$CUR_FUNC.func_ip = fdata->ip;
jtable->tmp_func += 1;
$VISIT($R);
return BREAK;
}
//==========================================================================
int VWC_param_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
int tmp_hash = murmurHash(node->right->data.str, node->right->data_lng);
$FREE_VAR.var_hash = tmp_hash;
$FREE_VAR.param_flag = FUNC_PARAM;
$FREE += 1;
$VISIT($L);
return BREAK;
}
//==========================================================================
int VWC_call_param_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if ($R->data_type == CONSTANT)
{
$WRITE_OPCDE(push_constant);
$VISIT($R);
$VISIT($L);
}
if($R->data_type == VARIABLE)
{
$WRITE_OPCDE(push_mem_rbp);
$VISIT($R);
$VISIT($L);
}
else
{
$VISIT($R);
$WRITE_OPCDE(push_rax);
$VISIT($L);
}
return BREAK;
}
//==========================================================================
int VWC_call_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
int func_hash = murmurHash($L->data.str, $L->data_lng);
int std_func_flag = VWC_call_case_std_func(node, vr_lists, fdata, jtable, func_hash); // check if std func called
if (std_func_flag == STD_FUNC_DETECTED)
return BREAK;
$VISIT($R);
int hash = murmurHash($L->data.str, $L->data_lng);
int func_ip = FindFunction(jtable, hash);
if (func_ip == -1)
assert(0 && "YOU CAN'T USE FUNCTIONS BEFORE THEY'RE DEFINED!!!");
$WRITE_OPCDE(call);
$WRITE_CONST(func_ip - fdata->ip - sizeof(int), int);
pop_param_back($R, vr_lists, fdata, jtable);
return BREAK;
}
//==========================================================================
int pop_param_back (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if ($R == NULL)
return CONITINUE;
$WRITE_OPCDE(pop_r12);
if ($L)
pop_param_back($R, vr_lists, fdata, jtable);
return CONITINUE;
}
//==========================================================================
int VWC_call_case_std_func (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable, int func_hash)
{
if (func_hash == SCAN_HASH)
{
int hash = murmurHash($L->data.str, $L->data_lng);
int func_ip = FindFunction(jtable, hash);
$WRITE_OPCDE(call);
$WRITE_CONST(func_ip - fdata->ip - sizeof(int), int);
if ($R->right->data_type != VARIABLE)
assert(0 && "INCORRECT INPUT, SCAN MUST HAVE VARIABLE AS A PARAMETER!");
$WRITE_OPCDE(imul_rax_100);
$WRITE_OPCDE(mov_mem_rbp_rax);
$VISIT($R->right);
int cur_var_hash = murmurHash($R->right->data.str, $R->right->data_lng);
int cur_var_num = FindVariable(vr_lists, cur_var_hash);
$CUR_VAR_DEF_FLAG = UNDEFINED;
return STD_FUNC_DETECTED;
}
else if (func_hash == PRINT_HASH)
{
int hash = murmurHash($L->data.str, $L->data_lng);
int func_ip = FindFunction(jtable, hash);
visit_check_var_const_use_rax($R->right, vr_lists, fdata, jtable);
$WRITE_OPCDE(call);
$WRITE_CONST(func_ip - fdata->ip - sizeof(int), int);
return STD_FUNC_DETECTED;
}
else if (func_hash == SQRT_HASH)
{
int hash = murmurHash($L->data.str, $L->data_lng);
int func_ip = FindFunction(jtable, hash);
visit_check_var_const_use_rax($R->right, vr_lists, fdata, jtable);
$WRITE_OPCDE(imul_rax_100);
$WRITE_OPCDE(call);
$WRITE_CONST(func_ip - fdata->ip - sizeof(int), int);
return STD_FUNC_DETECTED;
}
return STD_FUNC_NOT_DETECTED;
}
//==========================================================================
int VWC_ret_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if ($L->data_type == CONSTANT)
{
$WRITE_OPCDE(push_constant);
$VISIT($L);
$WRITE_OPCDE(pop_rax);
}
else if ($L->data_type == VARIABLE)
{
$WRITE_OPCDE(push_mem_rbp);
$VISIT($L);
$WRITE_OPCDE(pop_rax);
}
else
$VISIT($L);
$WRITE_OPCDE(mov_rsp_rbp);
$WRITE_OPCDE(pop_rbp);
$WRITE_OPCDE(ret);
return BREAK;
}
//==========================================================================
int VWC_if_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
$VISIT($L);
$VISIT($R);
return BREAK;
}
//==========================================================================
int VWC_desis_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
int rel_jmp_ip = fdata->ip;
$WRITE_CONST(TRASH, int);
$VISIT($L);
$WRITE_OPCDE(jmp); //jmp на скип else после тру ифа
int else_skip_jmp = fdata->ip;
$WRITE_CONST(TRASH, int);
$WRITE_CONST_IP(fdata->ip - rel_jmp_ip - sizeof(int), int, rel_jmp_ip);
$VISIT($R);
$WRITE_CONST_IP(fdata->ip - else_skip_jmp - sizeof(int), int, else_skip_jmp);
return BREAK;
}
//==========================================================================
int VWC_rel_op_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
visit_check_var_const_use_rbx($R, vr_lists, fdata, jtable);
visit_check_var_const_use_rax($L, vr_lists, fdata, jtable);
$WRITE_OPCDE(cmp_rax_rbx);
int rel_hash = RelOpHash(node);
switch(rel_hash)// Here I use jumps with the opposite condition !!!
{
case '==':
{
$WRITE_OPCDE(jne);
break;
}
case '!=':
{
$WRITE_OPCDE(je);
break;
}
case '>=':
{
$WRITE_OPCDE(jl);
break;
}
case '<=':
{
$WRITE_OPCDE(jg);
break;
}
case '>':
{
$WRITE_OPCDE(jle);
break;
}
case '<':
{
$WRITE_OPCDE(jge);
break;
}
default:
{
assert(0 && "OOooops incorrect relative operator !");
}
}
return BREAK;
}
//==========================================================================
int VWC_while_case (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
vr_lists->while_flag = IN_WHILE;
int while_strt_ip = fdata->ip;
$VISIT($L);
int while_end_ip = fdata->ip;
$WRITE_CONST(TRASH, int);// trash jmp at the end
$VISIT($R); // while body
$WRITE_OPCDE(jmp); // jmp at the start
$WRITE_CONST(while_strt_ip - fdata->ip - sizeof(int), int);
$WRITE_CONST_IP(fdata->ip - while_end_ip - sizeof(int), int, while_end_ip);
vr_lists->while_flag = NOT_IN_WHILE;
return BREAK;
}
//==========================================================================
void visit_check_var_const_use_rbx (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if (node->data_type == CONSTANT)
{
$WRITE_OPCDE(mov_rbx_const);
$VISIT(node);
}
else if(node->data_type == VARIABLE)
{
$WRITE_OPCDE(mov_rbx_mem_rbp);
$VISIT(node);
}
else
{
$WRITE_OPCDE(push_rax);
$VISIT(node);
$WRITE_OPCDE(mov_rbx_rax);
$WRITE_OPCDE(pop_rax);
}
}
//==========================================================================
void visit_check_var_const_use_rax (Node* node, var_lists* vr_lists, ELFfile* fdata, JMPtable* jtable)
{
if (node->data_type == CONSTANT)
{
$WRITE_OPCDE(mov_rax_const);
$VISIT(node);
}
else if(node->data_type == VARIABLE)
{
$WRITE_OPCDE(mov_rax_mem_rbp);
$VISIT(node);
}
else
{
$VISIT(node);
}
}
//==========================================================================
int FindVariable (var_lists* vr_lists, int hash)
{
for (int i = 0 ; i < VAR_MAX_CUNT ; i++)
{
if (vr_lists->var[i].var_hash == hash)
return i;
}
$FREE_VAR.var_hash = hash;
int var_num = $FREE;
$FREE += 1;
return var_num;
}
//==========================================================================
int FindFunction (JMPtable* jtable, int hash)
{
for(int i = 0 ; i < jtable->tmp_func ; i++)
{
if (jtable->func_jmp[i].func_hash == hash)
return jtable->func_jmp[i].func_ip;
}
assert(ERRORR && "YOU CAN'T USE FUNCTIONS BEFORE THEY'RE DEFINED!!!");
}
//==========================================================================
#define $add_one_std_func(fname) \
do \
{ \
$WRITE_OPCDE(jmp); \
jmp_ip = fdata->ip; \
$WRITE_CONST(fdata->ip, int); \
\
$CUR_FUNC.func_hash = murmurHash(fname, strlen(fname)); \
$CUR_FUNC.func_ip = fdata->ip; \
jtable->tmp_func += 1; \
\
$WRITE_OPCDE(my_##fname); \
$WRITE_CONST_IP(fdata->ip - jmp_ip - sizeof(int), int, jmp_ip); \
} while (0);
//==========================================================================
int std_funcs_add(ELFfile* fdata, JMPtable* jtable)
{
char scan[] = "scan";
char print[] = "print";
char sqrt[] = "sqrt";
int jmp_ip = 0;
$add_one_std_func(scan);
$add_one_std_func(print);
$add_one_std_func(sqrt);
return 0;
}
#undef $add_one_std_func
//==========================================================================
void FixPhdr(ELFfile* fdata)
{
int memsz = fdata->ip;
fdata->phdr.p_filesz = memsz;
fdata->phdr.p_memsz = memsz;
}
//==========================================================================
// backend optimisation funcs
//==========================================================================
#define $VISIT_CHECK_DEF(node) \
if (node) \
VisitCheckDefinedVal(node, vr_lists, def_value)
//==========================================================================
int VisitCheckDefinedVal(Node* node, var_lists* vr_lists, int* def_value)
{
switch($ND_TYP)
{
case OPERATOR:
{
$VISIT_CHECK_DEF($L);
$VISIT_CHECK_DEF($R);
break;
}
case VARIABLE:
{
int cur_var_hash = murmurHash($ND_STR, node->data_lng);
int cur_var_num = FindVariable(vr_lists, cur_var_hash);
if ($CUR_VAR_DEF_FLAG == UNDEFINED)
*def_value = UNDEFINED;
break;
}
case CALL:
{
*def_value = UNDEFINED;
break;
}
case CONSTANT:
{
break;
}
default:
{
assert(ERRORR && "Something went wrond! Default case in VisitCheckDefinedVal!");
}
}
}
#undef $VISIT_CHECK_DEF
//==========================================================================
double VisitCalcVal(Node* node, var_lists* vr_lists)
{
switch($ND_TYP)
{
case OPERATOR:
{
switch($ND_CHR)
{
case MUL:
{
return $VISIT_CALC($L) * $VISIT_CALC($R);
}
case DIV:
{
return $VISIT_CALC($L) / $VISIT_CALC($R);
}
case ADD:
{
return $VISIT_CALC($L) + $VISIT_CALC($R);
}
case SUB:
{
return $VISIT_CALC($L) - $VISIT_CALC($R);
}
}
}
case VARIABLE:
{
int cur_var_hash = murmurHash($ND_STR, node->data_lng);
int cur_var_num = FindVariable(vr_lists, cur_var_hash);
return $CUR_VAR_VAL;
}
case CONSTANT:
{
return $ND_DBL;
}
default:
{
assert(ERRORR && "Something went wrond! Default case in VisitCalcVal!");
}
}
}
//==========================================================================
int RelOpHash(Node* node)
{
int hash = 0;
for (int i = 0 ; i < $ND_LNG ; i++)
{
hash *= 256;
hash += $ND_STR[i];
}
return hash;
}
//==========================================================================
int murmurHash (char * key, unsigned int len)
{
const unsigned int m = 0x5bd1e995;
const unsigned int seed = 0;
const int r = 24;
unsigned int h = seed ^ len;
const unsigned char* data = (const unsigned char*) key;
unsigned int k;
while (len >= 4)
{
k = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch (len)
{
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
//==========================================================================
#undef $ND_CHR
#undef $ND_DBL
#undef $ND_STR
#undef $ND_LNG
#undef $ND_TYP
#undef $CUR_VAR_DEF_FLAG
#undef $CUR_VAR_HASH
#undef $CUR_VAR_VAL
#undef $CUR_FUNC
#undef $FREE_VAR
#undef $FREE
#undef $R
#undef $L