-
Notifications
You must be signed in to change notification settings - Fork 0
/
myCodeGen.h
169 lines (161 loc) · 4.39 KB
/
myCodeGen.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
#ifndef __MY_CODE_GEN_H
#define __MY_CODE_GEN_H
#include "compiler_hw_common.h"
#include "myType.h"
#include "mySymbolTable.h"
static int g_indent_cnt = 0;
static FILE *fout = NULL;
#define CODEGEN(...) \
do { \
for (int i = 0; i < g_indent_cnt; i++) { \
fprintf(fout, "\t"); \
} \
fprintf(fout, __VA_ARGS__); \
} while (0)
static int label_cnt=0;
void LABELGEN(int l){
g_indent_cnt--;
CODEGEN("Label%d:\n",l);
g_indent_cnt++;
}
void code_begin(){
CODEGEN(".source hw3.j\n");
CODEGEN(".class public Main\n");
CODEGEN(".super java/lang/Object\n");
}
void method_begin(char* name,char *sigs,unsigned char rtype){
CODEGEN(".method public static %s(%s)%c\n",name,sigs,get_type(rtype));
CODEGEN(".limit stack 100\n");
CODEGEN(".limit locals 100\n");
next_addr=0;
insert_func(name,sigs,rtype);
g_indent_cnt++;
}
void main_method_begin(){
CODEGEN(".method public static main([Ljava/lang/String;)V\n");
CODEGEN(".limit stack 100\n");
CODEGEN(".limit locals 100\n");
next_addr=1;
insert_func("main","",type_void);
g_indent_cnt++;
}
void method_end(){
CODEGEN("return\n");
g_indent_cnt--;
CODEGEN(".end method\n");
}
void println_post(unsigned char ptype){
int l1,l2;
switch(ptype){
case type_b:
l1=label_cnt++;
l2=label_cnt++;
CODEGEN("ifeq Label%d\n",l1);
CODEGEN("ldc \"true\"\n");
CODEGEN("goto Label%d\n",l2);
LABELGEN(l1);
CODEGEN("ldc \"false\"\n");
LABELGEN(l2);
case type_s:
CODEGEN("getstatic java/lang/System/out Ljava/io/PrintStream;\n");
CODEGEN("swap\n");
CODEGEN("invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V\n");
break;
default:
CODEGEN("getstatic java/lang/System/out Ljava/io/PrintStream;\n");
CODEGEN("swap\n");
CODEGEN("invokevirtual java/io/PrintStream/println(%c)V\n",get_type(ptype));
}
}
void print_post(unsigned char ptype){
int l1,l2;
switch(ptype){
case type_b:
l1=label_cnt++;
l2=label_cnt++;
CODEGEN("ifeq Label%d\n",l1);
CODEGEN("ldc \"true\"\n");
CODEGEN("goto Label%d\n",l2);
LABELGEN(l1);
CODEGEN("ldc \"false\"\n");
LABELGEN(l2);
case type_s:
CODEGEN("getstatic java/lang/System/out Ljava/io/PrintStream;\n");
CODEGEN("swap\n");
CODEGEN("invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V\n");
break;
default:
CODEGEN("getstatic java/lang/System/out Ljava/io/PrintStream;\n");
CODEGEN("swap\n");
CODEGEN("invokevirtual java/io/PrintStream/print(%c)V\n",get_type(ptype));
}
}
unsigned char myload(char* name){
int ad=symbol_find(name);
if(ad==-1){
printf("error:%d: undefined: %s\n",reduce_line,name);
return type_error;
}
struct symbol_node* s=symbol_table+ad;
switch(s->type){
case type_f:
CODEGEN("fload %d\n",s->addr);
break;
case type_i:
case type_b:
CODEGEN("iload %d\n",s->addr);
break;
case type_s:
CODEGEN("aload %d\n",s->addr);
break;
}
return s->type;
}
void mystore(char* name){
int ad=symbol_find(name);
if(ad==-1){
printf("error:%d: undefined: %s\n",reduce_line,name);
return;
}
struct symbol_node* s=symbol_table+ad;
switch(s->type){
case type_f:
CODEGEN("fstore %d\n",s->addr);
break;
case type_i:
case type_b:
CODEGEN("istore %d\n",s->addr);
break;
case type_s:
CODEGEN("astore %d\n",s->addr);
break;
}
}
void my_icmp(char* op){
int l1=label_cnt++;
int l2=label_cnt++;
CODEGEN("isub\n");
CODEGEN("if%s Label%d\n",op,l1);
CODEGEN("iconst_0\n");
CODEGEN("goto Label%d\n",l2);
LABELGEN(l1);
CODEGEN("iconst_1\n");
LABELGEN(l2);
}
void my_fcmp(char* op){
int l1=label_cnt++;
int l2=label_cnt++;
CODEGEN("fcmpl\n");
CODEGEN("if%s Label%d\n",op,l1);
CODEGEN("iconst_0\n");
CODEGEN("goto Label%d\n",l2);
LABELGEN(l1);
CODEGEN("iconst_1\n");
LABELGEN(l2);
}
int if_begin(){
int l1=label_cnt++;
CODEGEN("ifeq Label%d\n",l1);
return l1;
}
#endif