-
Notifications
You must be signed in to change notification settings - Fork 1
/
semantic.h
105 lines (87 loc) · 2.7 KB
/
semantic.h
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
#ifndef SEMANTIC_H
#define SEMANTIC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include "quadruplets.h"
#define MAXSTRING 255
#define MAXCODE 1000
#define true 1
#define false 0
typedef int bool;
struct jump_indices
{
/* data */
int index;
struct jump_indices *next;
};
struct statement
{
/*S: nextlist is a list of all conditional and unconditional jumps
to the instruction following the code for statement S in execution order*/
struct jump_indices *nextlist;
struct jump_indices *breaklist;
struct jump_indices *continuelist;
};
struct boolean_expression
{
/* a list of jump or conditional jump instructions into which
we must insert the label to which control goes if B is true */
struct jump_indices *truelist;
/*a list of jump or conditional jump instructions into which
we must insert the label to which control goes if B is false*/
struct jump_indices *falselist;
};
// union arithmetic_expression_value
// {
// /* Arithmetic expression can be either a value like integers or a temporary variable */
// };
struct arithmetic_expression
{
/* data */
bool is_litteral; // tells weather the synthesized expression is a litteral value
int value; // for simplicity we go for just int values
char sym[255];
};
struct expression
{
/* data */
bool is_string;
bool is_boolean; // tells weather the current expression is boolean or not
bool is_number;
struct arithmetic_expression arithmetic_expression;
struct boolean_expression boolean_expression;
};
struct ifstatement
{
struct jump_indices *breaklist;
struct jump_indices *continuelist;
struct boolean_expression boolean_expression;
struct jump_indices *nextlist;
int m1;
};
// generate temporary names
char *gentemp();
char *genlabel();
// Call with function whenever you want to get a new address
// address might replace char labels
int nextaddress();
// Function used for backaptching
/*
makelist ( i ) creates a new list containing only i , an index into the block of
instructions; makelist returns a pointer to the newly created list.*/
struct jump_indices *makelist(int i);
/*
merge ( q 1 , q 2 ) concatenates the lists pointed to by q 1 and q 2 , and returns
a pointer to the concatenated list.*/
struct jump_indices *merge(struct jump_indices *q1, struct jump_indices *q2);
/*
backpatch ( q; i ) inserts i as the target label for each of the instructions on
the list pointed to by p .
*/
void backpatch(quadruplets_node quads[], int length, struct jump_indices *q, int to);
// swap instruction positions
void migrate(quadruplets_node quads[], int i1, int i2, int j1, int j2);
void scheduled(struct jump_indices *p);
#endif