-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.h
More file actions
903 lines (838 loc) · 19.1 KB
/
Copy pathcodegen.h
File metadata and controls
903 lines (838 loc) · 19.1 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
/*
* Project: Compiler for imperative programing language IFJ20
*
* File: codegen.h
* Brief: Code generator header file
*
* Authors: Hladký Tomáš xhladk15@stud.fit.vutbr.cz
* Kostolányi Adam xkosto04@stud.fit.vutbr.cz
* Makiš Jozef xmakis00@stud.fit.vutbr.cz
* Bartko Jakub xbartk07@stud.fit.vutbr.cz
*/
#ifndef CODEGEN_H
#define CODEGEN_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "symtable.h"
#include "error.h"
#include "escape_format.h"
#include "codegen_stack.h"
typedef struct instruction instr_t;
// Define intermediate code instructions
typedef enum instr_type {
/*
* Variables are stored in symtable in format <function>:<num_of_call><function>:<variable>
* num_of_call and function can repeat with another function
*
* num_of_call is need for multiple different function calls (different inputs)
*
* Example for variable:
* main:0init:var
* bar:0fun:0hello:var
*
* Example for function:
* main:0some_fun:0another_fun
*
* Example for recursive function:
* main:0hello:1hello:2hello
*
*/
/*
* Symbol types:
*
* symbol var: stores variable of int || float64 || string
* symbol const: stores const variables of int || float64 || string or its reference
* to const in program, eg. 42
* Update: this is not actually symbol, but represent const boolean stored in var
* symbol func: stores function with its parameters and returns (variables or const)
*
*/
// ===================== DECL/DEFINE INSTRUCTIONS =====================
/*
* Variable/variables declaration
*
* elem_dest: symbol var
* elem_1: NULL
* elem_2: NULL
*
* Example:
* var x int
* var y float64
* var z string
* var b bool
*
* ==========================
* Note:
* multiple variables in line are taken one by one
*
* var x, y, z int
*
* is rewritten to this:
*
* var x int
* var y int
* var z int
*/
IC_DECL_VAR,
/*
* Variable/variables definition/assign with another var/vars or/and const/consts
*
* elem_dest: symbol var_list (dest)
* elem_1: symbol var_list (src)
* elem_2: NULL
*
* Example:
* var x = a // variable is also declared here
* var x, y = a, b // variables are also declared here
*
* x := a // variable cannot be already declared
* x, y := a, b // variables may or may not be declared
*
* x = a // variable must be already declared
* x, y = a, b // variables must be already declared
*
* x = 42
*
* ==========================
* Note 1:
*
* If variable/variables were not declared, then IC_DECL_VAR is called first for
* each variable one by one.
*
* ==========================
* Note 2:
*
* symbol var_list is used because in multiple variables it's not clear
* if variable is var or const
*
*/
IC_DEF_VAR,
// ===================== FUNCTION INSTRUCTIONS =====================
/*
* Function definition indicator, instructions after creates separate
* block with label to jump on call
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* Example:
* fun_foo() {
* ...
* }
*
*/
IC_DEF_FUN,
/*
* Function call with or without single/multiple values return
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* Example:
* var x = some_fun() // variable is also declared here
* var x, y, z = some_fun() // variables are also declared here
*
* x := some_fun() // variable cannot be already declared
* x, y, z := some_fun() // variables may or may not be declared
*
* x = some_fun() // variable must be already declared
* x, y, z = some_fun() // variables must be already declared
*
* ==========================
* Note 1:
*
* If variable/variables were not declared, then IC_DECL_VAR is called first for
* each variable one by one.
*
*
* ========================
* Note 2:
*
* in case that some variables are defined and some not
*
* x = defined, y = undefined
*
* var x, y = some_fun()
* Result: x is redeclared = error
*
* x, y := some_fun()
* Result: valid but data type of x must be same as declared, value of x is reassigned.
*
*
* ========================
* Note 3:
*
* Destination variable names are stored in returns
*
*/
IC_CALL_FUN,
/*
* Function return call, only indicates end of current function
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* Example:
* return
* return x
* return x, y, z, 42
*
* ==========================
* Note:
*
* Need symbol func for name of func, for main
* Return parameters are stored in symbol func and they will be set to variables from
* IC_CALL_FUN in case that function has assigned at least 1 return value.
* Otherwise it was called without assigning return value.
*
*/
IC_RET_FUN,
/*
* Helper instruction. Indicates end of function definition.
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* ==========================
* Note:
*
* Need symbol func for name of func, for main
*
*/
IC_END_FUN,
// ===================== ARITHMETIC INSTRUCTIONS =====================
/*
* Variable add operation with var
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a + b
* x = a + 42
* x += c (x = x + c)
*
* ==========================
* Note:
*
* If src elements are not same data type - Error
*
*/
IC_ADD_VAR,
/*
* Variable subtract operation with var
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a - b
* x = a - 42
* x -= c (x = x - c)
*
* ==========================
* Note:
*
* If src elements are not same data type - Error
*
*/
IC_SUB_VAR,
/*
* Variable multiply operation with var
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a * b
* x = a * 42
* x *= c (x = x * c)
*
* ==========================
* Note:
*
* If src elements are not same data type - Error
*
*/
IC_MUL_VAR,
/*
* Variable divide operation with variable. Must be both int or float
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a / b
* x = a / 42
* x /= c (x = x / c)
*
* ==========================
* Note:
*
* If both src elements are float - DIV instruction will be called
* If both src elements are int - IDIV instruction will be called
* If src elements are not same data type - Error
*
*/
IC_DIV_VAR,
// ===================== LOGICAL RELATION INSTRUCTIONS =====================
/*
* Variable/const less than other variable/const
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a < b
* x = 24 < b
* x = a < 42
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src elements are not same data type - Error
*
*/
IC_LT_VAR,
/*
* Variable/const greater than other variable/const
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a > b
* x = 24 > b
* x = a > 42
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src elements are not same data type - Error
*
*/
IC_GT_VAR,
/*
* Variable/const equal other variable/const
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a == b
* x = 24 == b
* x = a == 42
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src elements are not same data type - Error
*
*/
IC_EQ_VAR,
// ===================== LOGICAL BOOLEAN INSTRUCTIONS =====================
/*
* Logical AND upon 2 variables
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a && b
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src elements are not bool - Error
*
*/
IC_AND_VAR,
/*
* Logical OR upon 2 variables
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: symbol var || symbol const (src2)
*
* Example:
* x = a || b
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src elements are not bool - Error
*
*/
IC_OR_VAR,
/*
* Logical NOT upon 1 variable
*
* elem_dest: symbol var (dest)
* elem_1: symbol var || symbol const (src1)
* elem_2: NULL
*
* Example:
* x = !a
*
* ==========================
* Note:
*
* If dest element is not bool - Error
* If src element is not bool - Error
*
*/
IC_NOT_VAR,
// ===================== TYPE CASTING INSTRUCTIONS =====================
/*
* Int to float type cast
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 1: [symbol var || symbol const (src)]
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = int2float(a)
* x = int2float(42)
*
* ==========================
* Note:
*
* If dest element is not float - Error
* If src element is not int - Error
*
*/
IC_INT2FLOAT_VAR,
/*
* Float to int type cast
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 1: [symbol var || symbol const (src)]
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = float2int(a)
* x = float2int(12.761)
*
* ==========================
* Note:
*
* If dest element is not int - Error
* If src element is not float - Error
*
*/
IC_FLOAT2INT_VAR,
/*
* Get character (ascii 0-255) from string on selected index position
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 2: [symbol var || symbol const (src1),
* symbol var || symbol const (src2)]
* returns -> 2: [symbol var (dest), symbol var (err)]
*
* Example:
* x = str2int(a, b)
* x = str2int(a, 2)
*
* ==========================
* Note 1:
*
* If dest element is not int - Error
* If err element is not int - Error
* If src1 element is not string - Error
* If src2 element is not int - Error
* If src2 int value is out of index of src1 string - Error
* If dest int value is out of interval <0,255> - Error
*
* ==========================
* Note 2:
*
* If index out of bounds, err is set to 1 and returned
*
*/
IC_STR2INT_VAR,
// ===================== I/O INSTRUCTIONS =====================
/*
* Read variable from build in functions
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 0: []
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = inputi(22)
*
*
*/
IC_READ_INT,
/*
* Read variable from build in functions
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 0: []
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = inputf(22.2)
*
*
*/
IC_READ_FLOAT,
/*
* Read variable from build in functions
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 0: []
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = inputi("HelloWorld)
*
*
*/
IC_READ_STRING,
/*
* Read variable from build in functions
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 0: []
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = inputi(true)
*
*
*/
IC_READ_BOOL,
/*
* Write variables to output from build in functions
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> n: [symbol var list]
* returns -> 0: []
*
* Example:
* x = print("Hello world!")
* x = print("Result number: ", some_var, "\n")
*
*
*/
IC_WRITE_VAR,
// ===================== STRING INSTRUCTIONS =====================
/*
* Concatenate two strings to one
*
* elem_dest: symbol var (dest)
* elem_1: symbol var (string1)
* elem_2: symbol var (string2)
*
* symbol func: params -> 2: [symbol var || symbol const (src1),
* symbol var || symbol const (src2)]
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = concat("Hello ", "world!")
* x = concat(a, b)
* x = "string1" + "string2"
*
* ==========================
* Note:
*
* If dest element is not string - Error
* If src1 element is not string - Error
* If src2 element is not string - Error
*
*/
IC_CONCAT_STR,
/*
* Get length of string
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 1: [symbol var || symbol const (src)]
* returns -> 1: [symbol var (dest)]
*
* Example:
* x = strlen("Hello")
* x = strlen(a)
*
* ==========================
* Note:
*
* If dest element is not int - Error
* If src1 element is not string - Error
*
*/
IC_STRLEN_STR,
/*
* Get char from string on specific index
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 2: [symbol var || symbol const (src1),
* symbol var || symbol const (src2)]
* returns -> 2: [symbol var (dest), symbol var (err)]
*
* Example:
* x = getc("Hello", 2)
* x = getc(a, 2)
*
* ==========================
* Note 1:
*
* If dest element is not string - Error
* If err element is not int - Error
* If src1 element is not string - Error
* If src2 element is not int - Error
* If src2 int value is out of index of src1 string - Error
*
* ==========================
* Note 2:
*
* If index out of bounds, err is set to 1 and returned
*
*/
IC_GETCHAR_STR,
/*
* Returns substring of some string based on params
*
* elem_dest: symbol func
* elem_1: NULL
* elem_2: NULL
*
* symbol func: params -> 3: [symbol var || symbol const (src1),
* symbol var || symbol const (src2),
* symbol var || symbol const (src3)]
* returns -> 2: [symbol var (dest), symbol var (err)]
*
* Example:
* x = substr("HelloWorld", 2, 5)
* x = substr(a, 0, y)
*
* ==========================
* Note 1:
*
* If dest element is not string - Error
* If err element is not int - Error
* If src1 element is not string - Error
* If src2 element is not int - Error
* If src2 element is not int - Error
* If src2 int value is out of index of dest string - Error
*
* ==========================
* Note 2:
*
* If index out bounds, err is set to 1 and returned
*
* Note 3:
*
* If n > strlen(src1) - src2, only chars to end are returned
*
*
*/
IC_SUBSTR_STR,
// ===================== CONDITION INSTRUCTIONS =====================
/*
* Helper instruction. Indicates incoming condition instructions for if
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_IF_DEF,
/*
* Helper instruction. Indicates start of if perform instructions
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_IF_START,
/*
* Helper instruction. Indicates end of if perform instructions
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_IF_END,
/*
* Helper instruction. Indicates start of else perform instructions
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_ELSE_START,
/*
* Helper instruction. Indicates end of else perform instructions
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_ELSE_END,
// ===================== CYCLE INSTRUCTIONS =====================
/*
* Helper instruction. Indicates incoming instructions for for cycle init
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_FOR_DEF,
/*
* Helper instruction. Indicates incoming instructions for for cycle condition
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_FOR_COND,
/*
* Helper instruction. Indicates incoming instructions for for cycle post iteration expression
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_FOR_STEP,
/*
* Helper instruction. Indicates start of incoming instructions for for cycle body
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_FOR_BODY_START,
/*
* Helper instruction. Indicates end of incoming instructions for for cycle body
*
* elem_dest: NULL
* elem_1: NULL
* elem_2: NULL
*
*/
IC_FOR_BODY_END,
} instr_type_t;
struct instruction {
instr_type_t type;
elem_t *elem_dest_ptr;
elem_t *elem1_ptr;
elem_t *elem2_ptr;
instr_t *next;
};
// Generate program start and prepare required jmp label stacks
void codegen_init();
// Get scope of variable
char *get_frame(sym_var_item_t *sym_var_item);
// Used after condition instruction, check next instruction and prepare label
void try_create_jump(instr_t instr);
bool is_condition_instr(instr_t instr);
// Generators for specific instruction
void declr_var(instr_t instr);
void def_var(instr_t instr);
void call_fun(instr_t instr);
void ret_fun(instr_t instr);
void add_var(instr_t instr);
void sub_var(instr_t instr);
void mul_var(instr_t instr);
void div_var(instr_t instr);
void lt_var(instr_t instr);
void gt_var(instr_t instr);
void eq_var(instr_t instr);
void and_var(instr_t instr);
void or_var(instr_t instr);
void not_var(instr_t instr);
void int2float(instr_t instr);
void float2int(instr_t instr);
void str2int(instr_t instr);
void read_int(instr_t instr);
void read_float(instr_t instr);
void read_string(instr_t instr);
void read_bool(instr_t instr);
void write_var(instr_t instr);
void concat_str(instr_t instr);
void strlen_str(instr_t instr);
void getchar_str(instr_t instr);
void substr_str(instr_t instr);
void substr_str_def();
void getchar_str_def();
void str2int_def();
void read_int_def();
void read_float_def();
void read_string_def();
void read_bool_def();
void if_def();
void if_start(instr_t instr);
void if_end(instr_t instr);
void else_start();
void else_end();
void for_def_codegen();
void for_cond();
void for_step(instr_t instr);
void for_body_start();
void for_body_end();
// Cycle whole ll of instructions, get instruction, resolve and call appropriate function
void codegen_generate_instr();
// Check if build-in functions (those that requires some handle from IFJ20code)
// have been called at least once and if so call generates for each of them
void codegen_post_generate();
// Start codegen generation. Expects filled ll with instructions
void codegen();
#endif //CODEGEN_H