-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.h
95 lines (85 loc) · 2.22 KB
/
ir.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
/*
* File: ir.h
* Project: lab3
* File Created: 2021/11/21
* Author: Tianjie Shen ([email protected])
* -----
* Copyright 2021 NJU, Tianjie Shen
*/
#ifndef LAB3_IR_H
#define LAB3_IR_H
#include "symbol.h"
#include <stdio.h>
typedef enum OP_TYPE_ {
OP_NULL,
OP_TEMP,
OP_LABEL,
OP_VAR,
OP_ADDR,
OP_SIZE,
OP_CONST,
OP_RELOP,
OP_FUNC,
} OP_TYPE;
typedef struct Operand_* Operand;
struct Operand_ {
OP_TYPE kind;
bool is_addr;
union {
int value; // OP_CONSTANT
int temp_no,label_no; // OP_TEMP,OP_LABEL
char func_name[64],var_name[64],addr_name[64]; // OP_FUNC,OP_VARIABLE
char relop[64];
} u;
};
Operand new_label();
Operand new_temp();
Operand new_var(char* sval);
Operand new_const(char* val);
Operand new_int(int val);
Operand new_func(char* val);
Operand new_relop(char* relop);
Operand new_size(int val);
Operand new_addr(char* val);
typedef enum IR_TYPE_ {
IR_LABEL,
IR_FUNC,
IR_ASSIGN,
IR_ADD,
IR_SUB,
IR_MUL,
IR_DIV,
IR_LOAD,
IR_SAVE,
IR_GOTO,
IR_JUMP_COND,
IR_RET,
IR_DEC,
IR_ARG,
IR_CALL,
IR_PARAM,
IR_READ,
IR_WRITE
} IR_TYPE;
typedef struct InterCode_* InterCode;
struct InterCode_ {
IR_TYPE kind;
union {
struct { Operand label;} label;
struct { Operand function;} function;
struct { Operand right, left; } assign, addr, load, save, call, dec;
struct { Operand result, op1, op2; } binop;
struct { Operand dest;} jump;
struct { Operand op1, relop, op2, dest;} jump_cond;
struct { Operand var;} ret, read, write, arg, param;
} u;
};
typedef struct InterCodes_* InterCodes;
struct InterCodes_ { InterCode code; struct InterCodes_ *prev, *next; bool dead;};
void insertInterCode(InterCode ic);
void outputInterCodes(FILE* fp);
void gen_ir_1(IR_TYPE type, Operand op1);
void gen_ir_2(IR_TYPE type, Operand op1, Operand op2);
void gen_ir_3(IR_TYPE type, Operand op1, Operand op2, Operand op3);
void gen_ir_if(char* relop, Operand op1, Operand op2, Operand op3);
#endif //LAB3_IR_H