-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirvm.h
231 lines (189 loc) · 3.79 KB
/
irvm.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
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
/* This file is part of IRVM.
Copyright (C) 2014 Pablo de Oliveira
IRVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
IRVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with IRVM. If not, see <http://www.gnu.org/licenses/>. */
#ifndef IRVM_IRVM_H
#define IRVM_IRVM_H
#include <stdint.h>
#include <ccan/list/list.h>
#include "id.h"
/* a location in the user's input program */
struct location
{
int first_line;
int first_column;
int last_line;
int last_column;
};
/* current location in the tree execution:
used when reporting errors */
extern struct location *current_loc;
/* one IRVM runtime primitive handler */
typedef int32_t (*primitive) (void);
/* IR nodes */
enum NODE
{
T_CONST,
T_TEMP,
T_BINOP,
T_MEM,
T_CALL,
T_ESEQ,
T_MOVE_MEM,
T_MOVE_TEMP,
T_SXP,
T_JUMP,
T_CJUMP,
T_SEQ,
T_LABEL,
T_NAME
};
/* IR arithmetic operators */
enum OPER
{
ADD,
SUB,
MUL,
DIV,
MOD
};
/* IR relative operators */
enum RELOP
{
LT,
LE,
GT,
GE,
NE,
EQ
};
/* The generic IR Node structure */
struct Node
{
/* the kind of node */
enum NODE kind;
/* list element so it can be embedded in lists */
struct list_node list;
/* next node that should be executed after this one */
struct Node *next;
/* node location in the input program */
struct location loc;
};
/* The root of the parsed IR tree */
extern struct Node *irtree;
/* Each kind of node has a specific structure
that captures its peculiarities.
Therefore each IR node has:
* a specific structure that depends on its kind,
for example struct Const for a CONST node.
* a generic Node structure
Usually when moving through the IR tree, we will use the outside generic
Node structure.
It is trivial to get the generic Node structure from the Specific structure
through the node pointer.
To get the Specific structure from the Generic structure is a bit harder.
We use the ccan/container_of macros, that retrieve the outer specific
structure by computing the memory offset of the node field. For example
the following code gets the specific structure of a node:
struct Const * get(struct Node * n)
{
assert(n.kind == T_CONST);
return container_of(n, struct Const, node);
}
*/
struct Const
{
struct Node node;
int32_t value;
};
struct Temp
{
struct Node node;
id name;
};
struct Binop
{
struct Node node;
enum OPER opr;
struct Node *left;
struct Node *right;
};
struct Mem
{
struct Node node;
struct Node *exp;
};
struct Call
{
struct Node node;
id name;
struct Node *target;
primitive primitive;
struct list_head *arguments;
};
struct Eseq
{
struct Node node;
struct Node *stm;
struct Node *exp;
};
struct MoveTemp
{
struct Node node;
id dest;
struct Node *orig;
};
struct MoveMem
{
struct Node node;
struct Node *dest;
struct Node *orig;
};
struct Sxp
{
struct Node node;
struct Node *exp;
};
struct Jump
{
struct Node node;
id name;
struct Node *target;
};
struct Cjump
{
struct Node node;
enum RELOP ropr;
struct Node *left;
struct Node *right;
id bthen;
id belse;
struct Node *bthen_target;
struct Node *belse_target;
};
struct Seq
{
struct Node node;
struct list_head *stms;
};
struct Label
{
struct Node node;
id name;
int32_t lit;
};
struct Name
{
struct Node node;
id name;
int32_t lit;
};
#endif /* IRVM_IRVM_H */