annotate mc-nop-386.c @ 44:c9293a6e40d2

*** empty log message ***
author kono
date Thu, 13 Feb 2003 18:16:19 +0900
parents a792cf69d698
children b9266c88495c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1 /* Micro-C Code Generatation Part for intel386 */
1
0529f5abe9d0 *** empty log message ***
kono
parents: 0
diff changeset
2 /* $Id$ */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
3
d35df41eac69 Initial revision
kono
parents:
diff changeset
4 #define EXTERN extern
d35df41eac69 Initial revision
kono
parents:
diff changeset
5 #include "mc.h"
d35df41eac69 Initial revision
kono
parents:
diff changeset
6
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
7 extern int get_register_var(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
8 extern void bexpr(int e1, char cond, int l1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
9 extern void closing(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
10 extern void cmpdimm(int e, int csreg);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
11 extern void code_enter(char *name) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
12 extern void code_enter1(int disp0,int args);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
13 extern void code_init(void);
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
14 extern void code_leave(char *name,int disp) ;
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
15 extern void leave(int control,char *name,int disp0) ;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
16 extern void def_label(int cslabel, int dlabel) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
17 extern void emit_data(int e, int t, NMTBL *n);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
18 extern void emit_init(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
19 extern void enter(char *name) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
20 extern void enter1(int disp) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
21 extern void g_expr(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
22 extern void gen_comment(char *s);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
23 extern void gen_gdecl(char *n, int gpc) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
24 extern void gen_source(char *s);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
25 extern void gexpr(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
26 extern void jcond(int l, char cond);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
27 extern void jmp(int l);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
28 extern void opening(char *filename);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
29 extern void ret(void);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
30
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
31 static int edx_setup() ;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
32 static int lvar(int l);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
33 static void jump(int e1, int env);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
34 static void data_mode(char *name);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
35 static void text_mode(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
36 static void assign(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
37 static void assop(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
38 static void b_expr(int e1, char cond, int l1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
39 static void emit_push(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
40 static void free_register(int i);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
41 static void function(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
42 static void global_table(void) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
43 /* static void indexy(char *op, char *gvar); */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
44 /* static void jmp_eq_label(int l); */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
45 /* static void jmp_label(int l); */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
46 static void ld_indexx(int byte, int n, int xreg);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
47 static void local_table(void) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
48 static void machinop(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
49 static void rexpr(int e1, int l1, char *s);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
50 static void rindirect(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
51 static void sassign(int e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
52 static void shift(char *op, int xreg) ;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
53 /* static void st_indexx(int byte, int n, int xreg); */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
54 static void string(int e1);
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
55 static void tosop(int op,int reg);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
56 static void edx_cleanup();
26
85b8648de04f *** empty log message ***
kono
parents: 24
diff changeset
57 static void use_register(int virt, int real, int move);
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
58 static void emit_copy(int from,int to,int length,int offset);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
59
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
60 extern int error(int n);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
61 extern int fwdlabel(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
62 extern void fwddef(int l);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
63 extern int backdef(void);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
64 extern int size(int t);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
65 extern int list3(int e1, int e2, int e3);
2
kono
parents: 1
diff changeset
66 extern int scalar(int);
kono
parents: 1
diff changeset
67 extern int reverse0(int);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
68
d35df41eac69 Initial revision
kono
parents:
diff changeset
69 #define TEXT_EMIT_MODE 0
d35df41eac69 Initial revision
kono
parents:
diff changeset
70 #define DATA_EMIT_MODE 1
d35df41eac69 Initial revision
kono
parents:
diff changeset
71 #define RODATA_EMIT_MODE 2
d35df41eac69 Initial revision
kono
parents:
diff changeset
72
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
73 static int output_mode = TEXT_EMIT_MODE;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
74 static int data_alignment = 0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
75
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
76 static int code_disp_label;
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
77 static int func_disp_label;
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
78
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
79 /*
d35df41eac69 Initial revision
kono
parents:
diff changeset
80 local1 <----24 local variable
d35df41eac69 Initial revision
kono
parents:
diff changeset
81 %esi -20 <- disp_offset
d35df41eac69 Initial revision
kono
parents:
diff changeset
82 %edi -16
d35df41eac69 Initial revision
kono
parents:
diff changeset
83 %edx -12
d35df41eac69 Initial revision
kono
parents:
diff changeset
84 %ecx -8
d35df41eac69 Initial revision
kono
parents:
diff changeset
85 %ebx -4
d35df41eac69 Initial revision
kono
parents:
diff changeset
86 %ebp = %esp 0
d35df41eac69 Initial revision
kono
parents:
diff changeset
87 %eip 4 <- arg_offset
d35df41eac69 Initial revision
kono
parents:
diff changeset
88 arg1 8
d35df41eac69 Initial revision
kono
parents:
diff changeset
89 see enter/enter1/leave
d35df41eac69 Initial revision
kono
parents:
diff changeset
90 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
91 int arg_offset = 8;
6
bf602558130d continuation
kono
parents: 5
diff changeset
92 int code_arg_offset = -4;
3
kono
parents: 2
diff changeset
93 int disp_offset = -12;
kono
parents: 2
diff changeset
94 int func_disp_offset = -12;
kono
parents: 2
diff changeset
95 int code_disp_offset = -4;
kono
parents: 2
diff changeset
96 int size_of_int = 4;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
97 int endian = 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
98 int MAX_REGISTER=6; /* intel386のレジスタを4つまで使う*/
d35df41eac69 Initial revision
kono
parents:
diff changeset
99 int REAL_MAX_REGISTER=8; /* intel386のレジスタが8つということ*/
d35df41eac69 Initial revision
kono
parents:
diff changeset
100 int MAX_DATA_REG=4;
d35df41eac69 Initial revision
kono
parents:
diff changeset
101 int MAX_POINTER=3;
d35df41eac69 Initial revision
kono
parents:
diff changeset
102 int MAX_REGISTGER_VAR=2;
d35df41eac69 Initial revision
kono
parents:
diff changeset
103
d35df41eac69 Initial revision
kono
parents:
diff changeset
104 static int creg; /* current register */
d35df41eac69 Initial revision
kono
parents:
diff changeset
105 static int dreg; /* temporary register */
d35df41eac69 Initial revision
kono
parents:
diff changeset
106 static int reg_sp; /* REGister Stack-Pointer */
d35df41eac69 Initial revision
kono
parents:
diff changeset
107
d35df41eac69 Initial revision
kono
parents:
diff changeset
108
d35df41eac69 Initial revision
kono
parents:
diff changeset
109 #define REG_EAX 0
d35df41eac69 Initial revision
kono
parents:
diff changeset
110 #define REG_EBX 1
d35df41eac69 Initial revision
kono
parents:
diff changeset
111 #define REG_ECX 2
d35df41eac69 Initial revision
kono
parents:
diff changeset
112 #define REG_EDX 3
d35df41eac69 Initial revision
kono
parents:
diff changeset
113 #define REG_ESI 4
d35df41eac69 Initial revision
kono
parents:
diff changeset
114 #define REG_EDI 5
d35df41eac69 Initial revision
kono
parents:
diff changeset
115 #define REG_EBP 6
d35df41eac69 Initial revision
kono
parents:
diff changeset
116 #define REG_ESP 7
d35df41eac69 Initial revision
kono
parents:
diff changeset
117
d35df41eac69 Initial revision
kono
parents:
diff changeset
118
d35df41eac69 Initial revision
kono
parents:
diff changeset
119 #define DATA_REG 0
d35df41eac69 Initial revision
kono
parents:
diff changeset
120 #define POINTER_REG 3
d35df41eac69 Initial revision
kono
parents:
diff changeset
121 static char *reg_name[8];
d35df41eac69 Initial revision
kono
parents:
diff changeset
122 static char *reg_name_l[4];
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
123 static char *reg_name_w[4];
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
124
d35df41eac69 Initial revision
kono
parents:
diff changeset
125 /*
d35df41eac69 Initial revision
kono
parents:
diff changeset
126 creg currrent virtual register
d35df41eac69 Initial revision
kono
parents:
diff changeset
127 dreg spare virtual register
d35df41eac69 Initial revision
kono
parents:
diff changeset
128
d35df41eac69 Initial revision
kono
parents:
diff changeset
129 rname[creg] currrent real register
d35df41eac69 Initial revision
kono
parents:
diff changeset
130 rname[dreg] spare real register
d35df41eac69 Initial revision
kono
parents:
diff changeset
131
d35df41eac69 Initial revision
kono
parents:
diff changeset
132 regs[] virtual register usage
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
133 regv[] value in virtual register flag
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
134
d35df41eac69 Initial revision
kono
parents:
diff changeset
135 reg_name[rname[creg]]
d35df41eac69 Initial revision
kono
parents:
diff changeset
136 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
137
d35df41eac69 Initial revision
kono
parents:
diff changeset
138 #define MAX_MAX 10
d35df41eac69 Initial revision
kono
parents:
diff changeset
139 static int rname[MAX_MAX];
d35df41eac69 Initial revision
kono
parents:
diff changeset
140 static int regs[MAX_MAX]; /* 使われているレジスタを示すフラグ */
d35df41eac69 Initial revision
kono
parents:
diff changeset
141 static int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
142 static int regv[MAX_MAX]; /* 値が入っているかどうか */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
143
d35df41eac69 Initial revision
kono
parents:
diff changeset
144 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
145 code_init(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
146 {
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
147 arg_offset = 8;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
148 func_disp_offset = -12;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
149 disp_offset = -12;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
150 size_of_int = 4;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
151 endian = 0;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
152 MAX_REGISTER=6;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
153 MAX_DATA_REG=4;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
154 MAX_POINTER=3;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
155 MAX_REGISTER_VAR=2;
d35df41eac69 Initial revision
kono
parents:
diff changeset
156
d35df41eac69 Initial revision
kono
parents:
diff changeset
157 reg_name[REG_EAX] = "%eax";
d35df41eac69 Initial revision
kono
parents:
diff changeset
158 reg_name[REG_EBX] = "%ebx";
d35df41eac69 Initial revision
kono
parents:
diff changeset
159 reg_name[REG_ECX] = "%ecx";
d35df41eac69 Initial revision
kono
parents:
diff changeset
160 reg_name[REG_EDX] = "%edx";
d35df41eac69 Initial revision
kono
parents:
diff changeset
161 reg_name[REG_ESI] = "%esi";
d35df41eac69 Initial revision
kono
parents:
diff changeset
162 reg_name[REG_EDI] = "%edi";
d35df41eac69 Initial revision
kono
parents:
diff changeset
163 reg_name[REG_EBP] = "%ebp";
d35df41eac69 Initial revision
kono
parents:
diff changeset
164 reg_name[REG_ESP] = "%esp";
d35df41eac69 Initial revision
kono
parents:
diff changeset
165 reg_name_l[REG_EAX] = "%al";
d35df41eac69 Initial revision
kono
parents:
diff changeset
166 reg_name_l[REG_EBX] = "%bl";
d35df41eac69 Initial revision
kono
parents:
diff changeset
167 reg_name_l[REG_ECX] = "%cl";
d35df41eac69 Initial revision
kono
parents:
diff changeset
168 reg_name_l[REG_EDX] = "%dl";
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
169 reg_name_w[REG_EAX] = "%ax";
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
170 reg_name_w[REG_EBX] = "%bx";
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
171 reg_name_w[REG_ECX] = "%cx";
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
172 reg_name_w[REG_EDX] = "%dx";
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
173
d35df41eac69 Initial revision
kono
parents:
diff changeset
174 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
175
d35df41eac69 Initial revision
kono
parents:
diff changeset
176 char *
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
177 register_name(int i,int byte)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
178 {
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
179 if (i<0) {
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
180 error(REG_ERR);
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
181 return "%eax";
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
182 }
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
183 if (byte && rname[i] <= REG_EDX) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
184 return reg_name_l[rname[i]];
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
185 } else {
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
186 return reg_name[rname[i]]; /* should be error */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
187 }
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
188 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
189
d35df41eac69 Initial revision
kono
parents:
diff changeset
190 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
191 get_register(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
192 { /* 使われていないレジスタを調べる */
d35df41eac69 Initial revision
kono
parents:
diff changeset
193 int i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
194 for(i=0;i<MAX_REGISTER;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
195 if (! regs[i]) { /* 使われていないなら */
d35df41eac69 Initial revision
kono
parents:
diff changeset
196 regs[i]=1; /* そのレジスタを使うことを宣言し */
d35df41eac69 Initial revision
kono
parents:
diff changeset
197 return i; /* その場所を表す番号を返す */
d35df41eac69 Initial revision
kono
parents:
diff changeset
198 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
199 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
200 return -1; /* 空いている場所がないなら、それを表す -1 を返す */
d35df41eac69 Initial revision
kono
parents:
diff changeset
201 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
202
d35df41eac69 Initial revision
kono
parents:
diff changeset
203 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
204 free_register(int i) { /* いらなくなったレジスタを開放 */
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
205 regv[i]=regs[i]=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
206 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
207
d35df41eac69 Initial revision
kono
parents:
diff changeset
208 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
209 register_full(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
210 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
211 int i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
212 for(i=0;i<MAX_REGISTER;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
213 if (! regs[i]) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
214 return 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
215 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
216 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
217 return 1;
d35df41eac69 Initial revision
kono
parents:
diff changeset
218 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
219
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
220 static
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
221 int
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
222 free_register_count(void)
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
223 {
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
224 int i,count;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
225 count = 0;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
226 for(i=0;i<MAX_REGISTER;i++) {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
227 if (! regs[i] && ! regv[i]) count++;
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
228 }
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
229 return count;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
230 }
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
231
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
232 static void
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
233 free_all_register(void)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
234 {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
235 int i;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
236 for(i=0;i<MAX_REGISTER;i++) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
237 regs[i]=regv[i]=0;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
238 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
239 creg = get_register();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
240 dreg = get_register();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
241 return;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
242 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
243
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
244 void
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
245 use_register_var(int i) {
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
246 regv[i]=1;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
247 }
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
248
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
249 static int creg_regvar = -1;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
250 static int creg_regvar_back;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
251 static int creg_back;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
252
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
253 void
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
254 creg_destroy() {
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
255 creg_back = creg; creg_regvar_back = creg_regvar;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
256 if (creg_regvar>=0)
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
257 creg = creg_regvar;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
258 creg_regvar=-1;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
259 }
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
260
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
261 void
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
262 creg_un_destroy() {
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
263 creg = creg_back; creg_regvar = creg_regvar_back;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
264 }
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
265
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
266 void
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
267 register_usage(char *s)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
268 {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
269 int i;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
270 printf("# %d: %s:",lineno,s);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
271 printf(" creg=%s dreg=%s ",register_name(creg,0),register_name(dreg,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
272 for(i=0;i<MAX_REGISTER;i++) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
273 printf("%d",regs[i]);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
274 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
275 printf(":");
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
276 for(i=0;i<MAX_REGISTER;i++) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
277 printf("%d",regv[i]);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
278 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
279 #if 0
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
280 printf(" regs_stack",register_name(creg,0),register_name(dreg,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
281 for(i=reg_sp;i>=0;i--) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
282 if(reg_stack[i]>=0)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
283 printf(" %s",register_name(reg_stack[i],0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
284 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
285 #endif
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
286 printf("\n");
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
287 }
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
288
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
289 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
290 gexpr_init(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
291 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
292 while(reg_sp > 0) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
293 free_register(reg_stack[--reg_sp]);
d35df41eac69 Initial revision
kono
parents:
diff changeset
294 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
295 text_mode();
26
85b8648de04f *** empty log message ***
kono
parents: 24
diff changeset
296 use_register(creg,REG_EAX,0);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
297 regv[dreg]=0;
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
298 creg_regvar = -1;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
299 register_usage("gexpr_init");
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
300 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
301
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
302
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
303 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
304 emit_init(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
305 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
306 int i;
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
307 for(i=0;i<REAL_MAX_REGISTER;i++) { regs[i]=0; regv[i]=0;rname[i]=i;}
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
308 free_all_register();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
309 reg_sp = 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
310 text_mode();
d35df41eac69 Initial revision
kono
parents:
diff changeset
311 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
312
d35df41eac69 Initial revision
kono
parents:
diff changeset
313 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
314 virtual(int real)
d35df41eac69 Initial revision
kono
parents:
diff changeset
315 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
316 int real_v,i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
317 real_v = -1;
d35df41eac69 Initial revision
kono
parents:
diff changeset
318 for(i=0;i<MAX_REGISTER;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
319 if (rname[i]==real) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
320 real_v=i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
321 break;
d35df41eac69 Initial revision
kono
parents:
diff changeset
322 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
323 }
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
324 /* if (real_v == -1)
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
325 error(-1); */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
326 return real_v;
d35df41eac69 Initial revision
kono
parents:
diff changeset
327 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
328
d35df41eac69 Initial revision
kono
parents:
diff changeset
329 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
330 use_register(int virt, int real, int move)
d35df41eac69 Initial revision
kono
parents:
diff changeset
331 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
332 int real_v;
d35df41eac69 Initial revision
kono
parents:
diff changeset
333 char *move_op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
334 if (rname[virt]==real)
d35df41eac69 Initial revision
kono
parents:
diff changeset
335 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
336 real_v = virtual(real);
d35df41eac69 Initial revision
kono
parents:
diff changeset
337 move_op = regs[real_v]?"\txchg %s,%s\n":"\tmovl %s,%s\n";
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
338 #define REGFLOW
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
339 #ifdef REGFLOW
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
340 if (move || (regv[real_v])) {
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
341 printf(move_op,reg_name[rname[virt]],reg_name[real]);
d35df41eac69 Initial revision
kono
parents:
diff changeset
342 }
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
343 #else
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
344 if (move || (regs[real_v])) {
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
345 printf(move_op,reg_name[rname[virt]],reg_name[real]);
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
346 }
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
347 #endif
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
348 rname[real_v] = rname[virt];
d35df41eac69 Initial revision
kono
parents:
diff changeset
349 rname[virt] = real;
d35df41eac69 Initial revision
kono
parents:
diff changeset
350 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
351
d35df41eac69 Initial revision
kono
parents:
diff changeset
352 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
353 use_pointer(int virt, int move)
d35df41eac69 Initial revision
kono
parents:
diff changeset
354 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
355 int i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
356 if (rname[virt]>=POINTER_REG)
d35df41eac69 Initial revision
kono
parents:
diff changeset
357 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
358 for(i=POINTER_REG;i<MAX_REGISTER;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
359 if (!regs[virtual(i)]) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
360 use_register(virt,i,move);
d35df41eac69 Initial revision
kono
parents:
diff changeset
361 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
362 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
363 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
364 /* we prefer EBX */
d35df41eac69 Initial revision
kono
parents:
diff changeset
365 use_register(virt,REG_EBX,move);
d35df41eac69 Initial revision
kono
parents:
diff changeset
366 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
367
d35df41eac69 Initial revision
kono
parents:
diff changeset
368 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
369 use_data_reg(int virt, int move)
d35df41eac69 Initial revision
kono
parents:
diff changeset
370 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
371 int i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
372 if (rname[virt]<MAX_DATA_REG)
d35df41eac69 Initial revision
kono
parents:
diff changeset
373 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
374 for(i=0;i<MAX_DATA_REG;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
375 if (!regs[virtual(i)]) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
376 use_register(virt,i,move);
d35df41eac69 Initial revision
kono
parents:
diff changeset
377 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
378 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
379 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
380 /* we prefer EBX */
d35df41eac69 Initial revision
kono
parents:
diff changeset
381 use_register(virt,REG_EBX,move);
d35df41eac69 Initial revision
kono
parents:
diff changeset
382 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
383
d35df41eac69 Initial revision
kono
parents:
diff changeset
384 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
385 pop_register(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
386 { /* レジスタから値を取り出す */
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
387 return reg_stack[--reg_sp];
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
388 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
389
d35df41eac69 Initial revision
kono
parents:
diff changeset
390 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
391 stack_used(void) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
392 return reg_stack[--reg_sp]<0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
393 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
394
d35df41eac69 Initial revision
kono
parents:
diff changeset
395 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
396 emit_push(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
397 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
398 int new_reg;
d35df41eac69 Initial revision
kono
parents:
diff changeset
399 new_reg = get_register();
d35df41eac69 Initial revision
kono
parents:
diff changeset
400 if(new_reg<0) { /* もうレジスタがない */
d35df41eac69 Initial revision
kono
parents:
diff changeset
401 reg_stack[reg_sp++] = -1;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
402 printf("\tpushl %s\n",register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
403 /* creg is used soon, don't regv[creg]=0 */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
404 } else {
d35df41eac69 Initial revision
kono
parents:
diff changeset
405 reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
406 creg = new_reg;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
407 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
408 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
409 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
410
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
411 void
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
412 emit_push_x(int xreg)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
413 {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
414 int new_reg;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
415 new_reg = get_register();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
416 if(new_reg<0) { /* もうレジスタがない */
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
417 reg_stack[reg_sp++] = -1;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
418 printf("\tpushl %s\n",register_name(xreg,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
419 /* creg is used soon, don't regv[xreg]=0 */
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
420 } else {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
421 reg_stack[reg_sp++] = xreg; /* push するかわりにレジスタを使う */
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
422 xreg = new_reg;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
423 regv[xreg]=1;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
424 }
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
425 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
426
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
427 int
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
428 emit_pop(int type)
d35df41eac69 Initial revision
kono
parents:
diff changeset
429 {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
430 int xreg;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
431 if ((xreg=pop_register())==-1) {
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
432 if (type==POINTER_REG)
d35df41eac69 Initial revision
kono
parents:
diff changeset
433 use_pointer(dreg,0);
d35df41eac69 Initial revision
kono
parents:
diff changeset
434 else if (type==DATA_REG)
d35df41eac69 Initial revision
kono
parents:
diff changeset
435 use_data_reg(dreg,0);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
436 if (regv[dreg]) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
437 printf("# emit_pop dreg conflict\n");
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
438 }
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
439 printf("\tpopl %s\n",register_name(dreg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
440 xreg = dreg;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
441 regv[xreg]=1;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
442 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
443 return xreg;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
444 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
445
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
446 static void
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
447 emit_pop_free(int xreg)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
448 {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
449 if (xreg==dreg) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
450 regv[dreg]=0;
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
451 } else if (xreg!=-1) {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
452 free_register(xreg);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
453 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
454 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
455
d35df41eac69 Initial revision
kono
parents:
diff changeset
456 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
457 get_register_var(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
458 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
459 int i;
d35df41eac69 Initial revision
kono
parents:
diff changeset
460 for(i=REG_ESI;i<REG_ESP;i++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
461 if (! regs[i]) { /* 使われていないなら */
d35df41eac69 Initial revision
kono
parents:
diff changeset
462 regs[i]=1; /* そのレジスタを使うことを宣言し */
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
463 regv[i]=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
464 return i; /* その場所を表す番号を返す */
d35df41eac69 Initial revision
kono
parents:
diff changeset
465 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
466 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
467 return -1;
d35df41eac69 Initial revision
kono
parents:
diff changeset
468 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
469
d35df41eac69 Initial revision
kono
parents:
diff changeset
470 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
471 gexpr(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
472 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
473 gexpr_init();
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
474 #if 0
32
b35787d8a442 macro initialization
kono
parents: 27
diff changeset
475 if(lineno==2862) {
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
476 g_expr(e1); /*break here*/
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
477 return;
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
478 }
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
479 #endif
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
480 g_expr(e1);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
481 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
482
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
483 int
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
484 csvalue()
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
485 {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
486 return rname[creg]; /* for siwtch value */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
487 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
488
d35df41eac69 Initial revision
kono
parents:
diff changeset
489 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
490 g_expr(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
491 {
22
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
492 int e2,e3/*,e4*/;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
493 char *xrn;
d35df41eac69 Initial revision
kono
parents:
diff changeset
494 NMTBL *n;
d35df41eac69 Initial revision
kono
parents:
diff changeset
495
d35df41eac69 Initial revision
kono
parents:
diff changeset
496 if (chk) return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
497 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
498 switch (car(e1)){
d35df41eac69 Initial revision
kono
parents:
diff changeset
499 case GVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
500 /* use_pointer(creg,0); */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
501 printf("\tmovl $%s,%s\n",(char *)caddr(e1),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
502 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
503 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
504 case RGVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
505 /* use_pointer(creg,0); */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
506 printf("\tmovl %s,%s\n",(char *)caddr(e1),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
507 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
508 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
509 case CRGVAR:
27
af0c69eaa433 *** empty log message ***
kono
parents: 26
diff changeset
510 printf("\tmovsbl %s,%s\n",(char *)caddr(e1),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
511 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
512 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
513 case LVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
514 /* use_pointer(creg,0); */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
515 printf("\tlea %d(%%ebp),%s\n",lvar(e2),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
516 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
517 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
518 case REGISTER:
d35df41eac69 Initial revision
kono
parents:
diff changeset
519 /* this is of course redundant... */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
520 /* we can use rname for this? */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
521 /* or why not creg=e2? */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
522 printf("\tmovl %s,%s\n",register_name(e2,0),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
523 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
524 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
525 case RLVAR:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
526 printf("\tmovl %d(%%ebp),%s\n",lvar(e2),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
527 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
528 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
529 case CRLVAR:
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
530 printf("\tmovsbl %d(%%ebp),%s\n",lvar(e2),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
531 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
532 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
533 case FNAME:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
534 printf("\tmovl $%s,%s\n",((NMTBL *)e2)->nm,register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
535 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
536 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
537 case CONST: /* 代入する値が0でも特別な処理はしない */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
538 printf("\tmovl $%d,%s\n",e2,register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
539 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
540 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
541 case STRING:
d35df41eac69 Initial revision
kono
parents:
diff changeset
542 string(e1);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
543 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
544 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
545 case FUNCTION:
d35df41eac69 Initial revision
kono
parents:
diff changeset
546 function(e1);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
547 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
548 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
549 case CODE:
d35df41eac69 Initial revision
kono
parents:
diff changeset
550 jump(e2,caddr(e1));
d35df41eac69 Initial revision
kono
parents:
diff changeset
551 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
552 case INDIRECT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
553 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
554 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
555 case RINDIRECT: case CRINDIRECT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
556 rindirect(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
557 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
558 case ADDRESS:
d35df41eac69 Initial revision
kono
parents:
diff changeset
559 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
560 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
561 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
562 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
563 /* use_data_reg(creg,1); */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
564 printf("\tnegl %s\n", register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
565 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
566 case BNOT: /* ~ */
d35df41eac69 Initial revision
kono
parents:
diff changeset
567 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
568 /* use_data_reg(creg,1); */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
569 printf("\tnotl %s\n", register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
570 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
571 case LNOT: /* ! */
d35df41eac69 Initial revision
kono
parents:
diff changeset
572 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
573 use_data_reg(creg,1);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
574 xrn = register_name(creg,1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
575 printf("\tcmpl $0,%s\n", register_name(creg,0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
576 printf("\tsete %s\n", xrn);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
577 printf("\tmovzbl %s,%s\n", xrn,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
578 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
579 case PREINC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
580 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
581 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
582 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
583 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
584 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
585 g_expr(e2);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
586 xrn = register_name(creg,0);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
587 printf("\taddl $%d,(%s)\n",caddr(e1),xrn);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
588 printf("\tmovl (%s),%s\n",xrn,xrn);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
589 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
590 case POSTINC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
591 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
592 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
593 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
594 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
595 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
596 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
597 emit_push();
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
598 xrn = register_name((e2=emit_pop(0)),0);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
599 printf("\tmovl (%s),%s\n",xrn,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
600 printf("\taddl $%d,(%s)\n",caddr(e1),xrn);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
601 emit_pop_free(e2);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
602 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
603 case CPOSTINC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
604 /* char *p; *p++ */
d35df41eac69 Initial revision
kono
parents:
diff changeset
605 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
606 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
607 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
608 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
609 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
610 g_expr(e2);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
611 emit_push();
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
612 xrn = register_name((e2=emit_pop(0)),1);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
613 printf("\tmovsbl (%s),%s\n",xrn,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
614 printf("\tincl (%s)\n",xrn);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
615 emit_pop_free(e2);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
616 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
617 case CPREINC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
618 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
619 printf("\taddl $%d,%s\n",caddr(e1),register_name(cadr(e2),0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
620 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
621 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
622 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
623 g_expr(e2);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
624 printf("\tincl (%s)\n",register_name(creg,0));
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
625 printf("\tmovsbl (%s),%s\n",register_name(creg,0),register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
626 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
627 case CPOSTDEC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
628 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
629 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
630 printf("\tdecl %s\n",register_name(cadr(e2),0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
631 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
632 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
633 g_expr(e2);
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
634 printf("\tmovsbl (%s),%s\n",register_name(creg,0),register_name(creg,0));
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
635 printf("\tdecl (%s)\n",register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
636 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
637 case CPREDEC:
d35df41eac69 Initial revision
kono
parents:
diff changeset
638 if (car(e2)==REGISTER) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
639 printf("\tdecl %s\n",register_name(cadr(e2),0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
640 printf("\tmovl %s,%s\n",register_name(cadr(e2),0),register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
641 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
642 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
643 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
644 emit_push();
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
645 e2 = emit_pop(0);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
646 printf("\tdecl (%s)\n",register_name(e2,0));
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
647 printf("\tmovsbl (%s),%s\n",register_name(e2,0),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
648 emit_pop_free(e2);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
649 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
650 case MUL: case UMUL:
d35df41eac69 Initial revision
kono
parents:
diff changeset
651 case DIV: case UDIV:
d35df41eac69 Initial revision
kono
parents:
diff changeset
652 case MOD: case UMOD:
d35df41eac69 Initial revision
kono
parents:
diff changeset
653 case LSHIFT: case ULSHIFT: case RSHIFT: case URSHIFT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
654 case ADD: case SUB: case BAND: case EOR: case BOR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
655 machinop(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
656 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
657 case COND:
d35df41eac69 Initial revision
kono
parents:
diff changeset
658 e2=fwdlabel();
d35df41eac69 Initial revision
kono
parents:
diff changeset
659 b_expr(cadr(e1),0,e2);
22
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
660 use_register(creg,REG_EAX,0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
661 g_expr(caddr(e1));
22
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
662 /* e4 = rname[creg]; this is a bad idea */
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
663 use_register(creg,REG_EAX,1);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
664 jmp(e3=fwdlabel());
d35df41eac69 Initial revision
kono
parents:
diff changeset
665 fwddef(e2);
22
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
666 use_register(creg,REG_EAX,0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
667 g_expr(cadddr(e1));
22
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
668 /* use_register(creg,e4,1); */
3fa41d00f658 *** empty log message ***
kono
parents: 21
diff changeset
669 use_register(creg,REG_EAX,1);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
670 fwddef(e3);
d35df41eac69 Initial revision
kono
parents:
diff changeset
671 return;
37
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
672 case RSTRUCT:
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
673 g_expr(e2);
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
674 return;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
675 case SASS:
d35df41eac69 Initial revision
kono
parents:
diff changeset
676 sassign(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
677 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
678 case ASS: case CASS:
d35df41eac69 Initial revision
kono
parents:
diff changeset
679 assign(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
680 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
681 case ASSOP: case CASSOP:
d35df41eac69 Initial revision
kono
parents:
diff changeset
682 assop(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
683 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
684 case COMMA:
d35df41eac69 Initial revision
kono
parents:
diff changeset
685 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
686 g_expr(caddr(e1));
d35df41eac69 Initial revision
kono
parents:
diff changeset
687 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
688 case RETURN:
d35df41eac69 Initial revision
kono
parents:
diff changeset
689 n = (NMTBL *)e2;
d35df41eac69 Initial revision
kono
parents:
diff changeset
690 if (retcont==0)
d35df41eac69 Initial revision
kono
parents:
diff changeset
691 retcont=fwdlabel();
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
692 printf("\tleal _%d,%s\n",retcont,register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
693 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
694 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
695 case ENVIRONMENT:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
696 printf("\tmovl %%ebp,%s\n",register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
697 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
698 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
699 default:
d35df41eac69 Initial revision
kono
parents:
diff changeset
700 b_expr(e1,1,e2=fwdlabel()); /* including > < ... */
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
701 xrn = register_name(creg,0);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
702 printf("\txorl %s,%s\n",xrn,xrn);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
703 jmp(e3=fwdlabel());
d35df41eac69 Initial revision
kono
parents:
diff changeset
704 fwddef(e2);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
705 printf("\tmovl $1,%s\n",xrn);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
706 fwddef(e3);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
707 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
708 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
709 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
710
d35df41eac69 Initial revision
kono
parents:
diff changeset
711 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
712 bexpr(int e1, char cond, int l1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
713 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
714 gexpr_init();
d35df41eac69 Initial revision
kono
parents:
diff changeset
715 b_expr(e1,cond,l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
716 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
717
d35df41eac69 Initial revision
kono
parents:
diff changeset
718 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
719 b_expr(int e1, char cond, int l1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
720 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
721 int e2,l2;
d35df41eac69 Initial revision
kono
parents:
diff changeset
722 if (chk) return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
723 e2=cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
724 switch(car(e1)) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
725 case LNOT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
726 b_expr(e2,!cond,l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
727 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
728 case GT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
729 rexpr(e1,l1,cond?"g":"le");
d35df41eac69 Initial revision
kono
parents:
diff changeset
730 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
731 case UGT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
732 rexpr(e1,l1,cond?"a":"be");
d35df41eac69 Initial revision
kono
parents:
diff changeset
733 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
734 case GE:
d35df41eac69 Initial revision
kono
parents:
diff changeset
735 rexpr(e1,l1,cond?"ge":"l");
d35df41eac69 Initial revision
kono
parents:
diff changeset
736 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
737 case UGE:
d35df41eac69 Initial revision
kono
parents:
diff changeset
738 rexpr(e1,l1,cond?"ae":"b");
d35df41eac69 Initial revision
kono
parents:
diff changeset
739 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
740 case LT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
741 rexpr(e1,l1,cond?"l":"ge");
d35df41eac69 Initial revision
kono
parents:
diff changeset
742 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
743 case ULT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
744 rexpr(e1,l1,cond?"b":"ae");
d35df41eac69 Initial revision
kono
parents:
diff changeset
745 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
746 case LE:
d35df41eac69 Initial revision
kono
parents:
diff changeset
747 rexpr(e1,l1,cond?"le":"g");
d35df41eac69 Initial revision
kono
parents:
diff changeset
748 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
749 case ULE:
d35df41eac69 Initial revision
kono
parents:
diff changeset
750 rexpr(e1,l1,cond?"be":"a");
d35df41eac69 Initial revision
kono
parents:
diff changeset
751 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
752 case EQ:
d35df41eac69 Initial revision
kono
parents:
diff changeset
753 rexpr(e1,l1,cond?"e":"ne");
d35df41eac69 Initial revision
kono
parents:
diff changeset
754 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
755 case NEQ:
d35df41eac69 Initial revision
kono
parents:
diff changeset
756 rexpr(e1,l1,cond?"ne":"e");
d35df41eac69 Initial revision
kono
parents:
diff changeset
757 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
758 case LAND:
d35df41eac69 Initial revision
kono
parents:
diff changeset
759 b_expr(e2,0,cond?(l2=fwdlabel()):l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
760 b_expr(caddr(e1),cond,l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
761 if(cond) fwddef(l2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
762 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
763 case LOR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
764 b_expr(e2,1,cond?l1:(l2=fwdlabel()));
d35df41eac69 Initial revision
kono
parents:
diff changeset
765 b_expr(caddr(e1),cond,l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
766 if(!cond) fwddef(l2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
767 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
768 case CRGVAR:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
769 printf("\tcmpb $0,%s\n",(char *)caddr(e1));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
770 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
771 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
772 case CRLVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
773 printf("\tcmpb $0,%d(%%ebp)\n",lvar(e2));
d35df41eac69 Initial revision
kono
parents:
diff changeset
774 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
775 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
776 case RGVAR:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
777 printf("\tcmpl $0,%s\n",(char *)caddr(e1));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
778 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
779 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
780 case RLVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
781 printf("\tcmpl $0,%d(%%ebp)\n",lvar(e2));
d35df41eac69 Initial revision
kono
parents:
diff changeset
782 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
783 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
784 case REGISTER:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
785 printf("\tcmpl $0,%s\n",register_name(e2,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
786 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
787 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
788 case CONST:
d35df41eac69 Initial revision
kono
parents:
diff changeset
789 if((cond&&e2)||(!cond&&!e2)) jmp(l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
790 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
791 default:
d35df41eac69 Initial revision
kono
parents:
diff changeset
792 g_expr(e1);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
793 printf("\tcmpl $0,%s\n",register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
794 jcond(l1,cond);
d35df41eac69 Initial revision
kono
parents:
diff changeset
795 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
796 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
797 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
798
d35df41eac69 Initial revision
kono
parents:
diff changeset
799 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
800 ascii(char *s)
d35df41eac69 Initial revision
kono
parents:
diff changeset
801 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
802 printf("\t.string \"");
d35df41eac69 Initial revision
kono
parents:
diff changeset
803 while(*s) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
804 if (*s=='\n')
d35df41eac69 Initial revision
kono
parents:
diff changeset
805 printf("%cn",92);
d35df41eac69 Initial revision
kono
parents:
diff changeset
806 else if (*s<' ')
d35df41eac69 Initial revision
kono
parents:
diff changeset
807 printf("%c%03o",92,*s);
d35df41eac69 Initial revision
kono
parents:
diff changeset
808 else if (*s==34)
d35df41eac69 Initial revision
kono
parents:
diff changeset
809 printf("%c%c",92,34);
d35df41eac69 Initial revision
kono
parents:
diff changeset
810 else
d35df41eac69 Initial revision
kono
parents:
diff changeset
811 printf("%c",*s);
d35df41eac69 Initial revision
kono
parents:
diff changeset
812 s++;
d35df41eac69 Initial revision
kono
parents:
diff changeset
813 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
814 printf("%c\n",34);
d35df41eac69 Initial revision
kono
parents:
diff changeset
815 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
816
d35df41eac69 Initial revision
kono
parents:
diff changeset
817 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
818 string(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
819 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
820 char *s;
d35df41eac69 Initial revision
kono
parents:
diff changeset
821 int i,lb;
d35df41eac69 Initial revision
kono
parents:
diff changeset
822
d35df41eac69 Initial revision
kono
parents:
diff changeset
823 if (0) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
824 s=(char *)cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
825 lb=fwdlabel();
d35df41eac69 Initial revision
kono
parents:
diff changeset
826 printf("\tjmp _%d\n",lb);
d35df41eac69 Initial revision
kono
parents:
diff changeset
827 i=backdef();
d35df41eac69 Initial revision
kono
parents:
diff changeset
828 ascii(s);
d35df41eac69 Initial revision
kono
parents:
diff changeset
829 printf("\t.align 2\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
830 fwddef(lb);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
831 printf("\tlea _%d,%s\n",i,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
832 } else {
d35df41eac69 Initial revision
kono
parents:
diff changeset
833 s=(char *)cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
834 printf(".section\t.rodata\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
835 lb=fwdlabel();
d35df41eac69 Initial revision
kono
parents:
diff changeset
836 printf("_%d:\n",lb);
d35df41eac69 Initial revision
kono
parents:
diff changeset
837 ascii(s);
d35df41eac69 Initial revision
kono
parents:
diff changeset
838 if (output_mode==TEXT_EMIT_MODE) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
839 printf(".text\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
840 } else {
d35df41eac69 Initial revision
kono
parents:
diff changeset
841 text_mode();
d35df41eac69 Initial revision
kono
parents:
diff changeset
842 }
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
843 printf("\tlea _%d,%s\n",lb,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
844 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
845 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
846
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
847 #define MAX_COPY_LEN 20
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
848
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
849 void
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
850 emit_copy(int from,int to,int length,int offset)
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
851 {
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
852 int fix = 0;
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
853 if (length<=0) return;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
854 switch (length) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
855 case 0: break;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
856 case 1:
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
857 printf("\tmovb %d(%s),%s\n",offset,register_name(from,0), reg_name_l[rname[dreg]] );
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
858 printf("\tmovb %s,%d(%s)\n",reg_name_l[rname[dreg]] ,offset,register_name(to,0));
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
859 break;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
860 case 2:
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
861 printf("\tmovw %d(%s),%s\n",offset,register_name(from,0), reg_name_w[rname[dreg]] );
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
862 printf("\tmovw %s,%d(%s)\n",reg_name_w[rname[dreg]] ,offset,register_name(to,0));
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
863 break;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
864 case 4:
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
865 printf("\tmovl %d(%s),%s\n",offset,register_name(from,0), register_name(dreg,0));
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
866 printf("\tmovl %s,%d(%s)\n",register_name(dreg,0), offset,register_name(to,0));
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
867 break;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
868 default:
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
869 if (length <=MAX_COPY_LEN) {
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
870 for(;length>=4;length-=4,offset+=4)
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
871 emit_copy(from,to,4,offset);
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
872 for(;length>=2;length-=2,offset+=2)
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
873 emit_copy(from,to,2,offset);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
874 if(length>0)
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
875 emit_copy(from,to,length,offset);
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
876 break;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
877 }
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
878 use_register(from,REG_ESI,1);
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
879 use_register(to, REG_EDI,1);
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
880 use_register(dreg,REG_ECX,0);
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
881 printf("\tmovl $%d,%%ecx\n",length/4);
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
882 fix = (length/4)*4;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
883 printf("\tcld\n\trep\n\tmovsl\n");
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
884 if(length%4) {
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
885 emit_copy(from,to,length,offset+length/4);
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
886 }
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
887 }
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
888 /* this code is necessary for the value of assignment or function call */
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
889 /* otherwise we don't need this */
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
890 if (fix) printf("\tsubl $%d,%s\n",fix,register_name(to,0));
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
891 if(creg!=to) {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
892 if (to==dreg) error(-1);
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
893 free_register(creg); creg=to;
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
894 }
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
895 regv[from]=regv[to]=regv[dreg]=0;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
896 regv[creg]=1;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
897 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
898
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
899 int
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
900 struct_push(int e4,int t)
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
901 {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
902 int length,xreg,save,lreg;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
903 g_expr(e4);
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
904 length=size(t);
77c0710a8729 struct copy
kono
parents: 13
diff changeset
905 if(length%size_of_int) {
77c0710a8729 struct copy
kono
parents: 13
diff changeset
906 length += size_of_int - (length%size_of_int);
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
907 }
40
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
908 if (length==4) {
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
909 printf("\tpushl (%s)\n",register_name(creg,0)); return 1;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
910 }
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
911 if (length==8) {
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
912 printf("\tpushl 4(%s)\n",register_name(creg,0));
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
913 printf("\tpushl (%s)\n",register_name(creg,0)); return 2;
060d1e549fec *** empty log message ***
kono
parents: 39
diff changeset
914 }
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
915 /* I think, after g_expr, at least two registers are free */
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
916 printf("\tsubl $%d,%%esp\n",length);
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
917 if (register_full()) {
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
918 /* this is wrong assumption */
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
919 save = 1;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
920 for(lreg=0;lreg!=creg&&lreg!=dreg;lreg++);
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
921 printf("\tpushl %s\n",register_name(lreg,0));
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
922 xreg = lreg;
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
923 } else {
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
924 save=0;
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
925 xreg = get_register();
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
926 }
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
927 printf("\tmovl %%esp,%s\n",register_name(xreg,0));
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
928 regv[xreg]=1;
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
929 emit_copy(creg,xreg,length,0);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
930 /* we have value in creg */
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
931 if (save) {
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
932 printf("\tpopl %s\n",register_name(lreg,0));
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
933 } else
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
934 free_register(xreg);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
935 regv[xreg]=0;
14
77c0710a8729 struct copy
kono
parents: 13
diff changeset
936 return length/size_of_int;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
937 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
938
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
939 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
940 function(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
941 {
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
942 int e2,e3,e4,e5,nargs,t;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
943 NMTBL *n;
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
944 int save,saved;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
945 if (free_register_count()<1) {
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
946 for(save = 0;save==dreg||save==creg;save++);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
947 printf("\tpushl %s\n",register_name(save,0));
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
948 saved = 1;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
949 } else {
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
950 save = get_register();
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
951 saved = 0;
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
952 }
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
953 regv[save]=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
954 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
955 nargs = 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
956 for (e3 = caddr(e1); e3; e3 = cadr(e3)) {
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
957 t=caddr(e3);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
958 n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
d35df41eac69 Initial revision
kono
parents:
diff changeset
959 switch(car(e4)) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
960 case FNAME:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
961 printf("\tlea %s,%s\n",n->nm,register_name(creg,0));
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
962 printf("\tpushl %s\n",register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
963 break;
d35df41eac69 Initial revision
kono
parents:
diff changeset
964 case ADDRESS:
d35df41eac69 Initial revision
kono
parents:
diff changeset
965 g_expr(e5);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
966 printf("\tpushl %s\n",register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
967 break;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
968 default:
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
969 if(scalar(t)) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
970 g_expr(e4);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
971 printf("\tpushl %s\n",register_name(creg,0));
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
972 } else if (car(t)==STRUCT||car(t)==UNION) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
973 nargs += struct_push(e4,t);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
974 continue;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
975 } else {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
976 error(TYERR);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
977 }
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
978 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
979 ++nargs;
d35df41eac69 Initial revision
kono
parents:
diff changeset
980 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
981 if (car(e2) == FNAME) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
982 n=(NMTBL *)cadr(e2);
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
983 } else {
44
c9293a6e40d2 *** empty log message ***
kono
parents: 43
diff changeset
984 use_register(creg,REG_EAX,0);
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
985 g_expr(e2);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
986 }
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
987
26
85b8648de04f *** empty log message ***
kono
parents: 24
diff changeset
988 /* we don't have to save creg nor dreg */
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
989 regs[creg]=0; regs[dreg]=0;
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
990 regv[creg]= regv[dreg]= regv[save]= 0;
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
991 use_register(creg,REG_EAX,0);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
992 use_register(dreg,REG_EDX,0); /* will be destroyed */
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
993 use_register(save,REG_ECX,0); /* will be destroyed */
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
994 regs[creg]=1; regs[dreg]=1;
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
995
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
996 if (car(e2) == FNAME) {
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
997 printf("\tcall\t%s\n",n->nm);
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
998 } else {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
999 printf("\tcall\t*%s\n",register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1000 }
6
bf602558130d continuation
kono
parents: 5
diff changeset
1001 if (nargs) printf("\taddl $%d,%%esp\n",size_of_int*nargs);
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1002 if (saved) {
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1003 printf("\tpopl %s\n",register_name(save,0));
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1004 } else {
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1005 free_register(save);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1006 }
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1007 regv[save]=0;
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1008 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1009 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1010
2
kono
parents: 1
diff changeset
1011 int
3
kono
parents: 2
diff changeset
1012 arg_size(int e3,int *nargs0)
2
kono
parents: 1
diff changeset
1013 {
kono
parents: 1
diff changeset
1014 int i,nargs,offset_list,e,t;
kono
parents: 1
diff changeset
1015
kono
parents: 1
diff changeset
1016 offset_list = 0;
kono
parents: 1
diff changeset
1017 /* we should use prototypes's type */
kono
parents: 1
diff changeset
1018 for (i = nargs = 0; e3;e3 =cadr(e3)) {
kono
parents: 1
diff changeset
1019 e = car(e3); t = caddr(e3);
3
kono
parents: 2
diff changeset
1020 if (i < MAX_REGISTER_VAR && scalar(t)) {
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1021 offset_list = list3(-(REG_ESI+i++),offset_list,e);
2
kono
parents: 1
diff changeset
1022 } else {
kono
parents: 1
diff changeset
1023 offset_list =
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1024 list3(nargs,offset_list,e);
3
kono
parents: 2
diff changeset
1025 nargs += (car(e3)==CHAR?size_of_int:size(t));
2
kono
parents: 1
diff changeset
1026 }
kono
parents: 1
diff changeset
1027 }
3
kono
parents: 2
diff changeset
1028 *nargs0 = -nargs;
2
kono
parents: 1
diff changeset
1029 return offset_list;
kono
parents: 1
diff changeset
1030 }
kono
parents: 1
diff changeset
1031
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1032 int
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1033 is_simple(int e1)
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1034 {
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1035 int e2 = car(e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1036 return (
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1037 e2==FNAME || e2==LVAR || e2==CRLVAR || e2==REGISTER
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1038 );
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1039 }
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1040
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1041 void
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1042 jump_new(int e1, int env)
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1043 {
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1044 int e2;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1045 NMTBL *code0;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1046 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1047 jump アドレスを計算する
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1048 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1049 /* compute jump address */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1050 e2 = cadr(e1);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1051 if (car(e2) == FNAME) {
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1052 code0=(NMTBL *)cadr(e2);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1053 if (code0->sc!=CODE) {
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1054 error(STERR); return;
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1055 }
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1056 } else { /* indirect */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1057 g_expr(e2);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1058 emit_push();
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1059 }
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1060 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1061 まず、複雑な式を前もって単純な式に落す。
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1062 必要なら局所変数を用いる。
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1063 局所変数へのオフセットを覚えておく
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1064 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1065 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1066 スタックの位置を修正する
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1067 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1068 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1069 オフセットを用いて、
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1070 並べ変えを行う
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1071 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1072 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1073 スタックの位置を修正する
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1074 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1075 /*
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1076 jump code を生成する
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1077 */
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1078 }
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1079
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1080 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1081 jump(int e1, int env)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1082 {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1083 int i,args,e2,e3,e4,e5,nargs,regs;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1084 NMTBL *n,*code0;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1085 int new_disp,scode,disp1,xreg;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1086 char *xrn;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1087
2
kono
parents: 1
diff changeset
1088 /* We need three passes. Compute Stack size, Compute Arg, Copy it. */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1089 /* count number of args */
2
kono
parents: 1
diff changeset
1090 args = caddr(e1);
3
kono
parents: 2
diff changeset
1091 args = reverse0(args);
kono
parents: 2
diff changeset
1092 nargs = arg_size(args,&new_disp); /* compute in normal order */
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1093 disp1 = (fnptr->sc==CODE)?0:-size_of_int;
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1094 if (new_disp+disp1 < disp) { /* have to extend stack */
6
bf602558130d continuation
kono
parents: 5
diff changeset
1095 if (fnptr->sc==CODE)
bf602558130d continuation
kono
parents: 5
diff changeset
1096 printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int);
bf602558130d continuation
kono
parents: 5
diff changeset
1097 else
bf602558130d continuation
kono
parents: 5
diff changeset
1098 printf("\tleal %d(%%ebp),%%esp\n",new_disp+disp_offset);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1099 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1100 /* compute jump address */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1101 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1102 if (car(e2) == FNAME) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1103 code0=(NMTBL *)cadr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1104 if (code0->sc!=CODE) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1105 error(STERR); return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1106 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1107 } else { /* indirect */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1108 g_expr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1109 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1110 }
2
kono
parents: 1
diff changeset
1111 /* compute arguments in reverse order */
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1112 /* printf("## jump code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */
2
kono
parents: 1
diff changeset
1113 regs = 0;
4
535bf528d68f copy free jump
kono
parents: 3
diff changeset
1114 i=MAX_REGISTER_VAR;
2
kono
parents: 1
diff changeset
1115 for (e3=nargs; e3;e3 =cadr(e3)) {
kono
parents: 1
diff changeset
1116 n=(NMTBL *)(e5=(cadr(e4 = caddr(e3))));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1117 switch(car(e4)) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1118 case FNAME:
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1119 printf("\tlea %s,%s\n",n->nm,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1120 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1121 break;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1122 case ADDRESS:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1123 g_expr(e5);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1124 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1125 break;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1126 case RLVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1127 case CRLVAR:
4
535bf528d68f copy free jump
kono
parents: 3
diff changeset
1128 if (env==0 && fnptr->sc==CODE) {
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1129 /* printf("## e5=%d car(e3)=%d\n",e5,car(e3)); */
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1130 if (e5>=0 && e5==car(e3)) {
2
kono
parents: 1
diff changeset
1131 /* The same positioned local variable. No need to copy */
kono
parents: 1
diff changeset
1132 reg_stack[reg_sp++] = -2;
kono
parents: 1
diff changeset
1133 }
4
535bf528d68f copy free jump
kono
parents: 3
diff changeset
1134 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1135 }
3
kono
parents: 2
diff changeset
1136 g_expr(e4);
kono
parents: 2
diff changeset
1137 emit_push();
kono
parents: 2
diff changeset
1138 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1139 case REGISTER:
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1140 /* printf("## i=%d rname[e5]=%d\n",i,rname[e5]); */
4
535bf528d68f copy free jump
kono
parents: 3
diff changeset
1141 if (i>0 && rname[e5]==REG_ESI+ --i) {
3
kono
parents: 2
diff changeset
1142 /* The same register variable. No need to copy */
2
kono
parents: 1
diff changeset
1143 reg_stack[reg_sp++] = e5;
4
535bf528d68f copy free jump
kono
parents: 3
diff changeset
1144 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1145 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1146 default:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1147 g_expr(e4);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1148 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1149 }
2
kono
parents: 1
diff changeset
1150 regs++;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1151 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1152 if (env) {
2
kono
parents: 1
diff changeset
1153 /* change the frame pointer */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1154 g_expr(env);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1155 printf("\tmovl %s,%%ebp\n",register_name(creg,0));
3
kono
parents: 2
diff changeset
1156 } else if (fnptr->sc==FUNCTION) {
kono
parents: 2
diff changeset
1157 printf("\tlea %d(%%ebp),%%ebp\n",disp_offset);
kono
parents: 2
diff changeset
1158 }
kono
parents: 2
diff changeset
1159 /* force lvar offset mode to CODE */
kono
parents: 2
diff changeset
1160 scode = fnptr->sc; fnptr->sc = CODE;
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1161 /* printf("## jump2 code_arg_offset=%d code_disp_offset=%d\n",code_arg_offset,code_disp_offset); */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1162 /* copy arguments to destination environment if necessary */
3
kono
parents: 2
diff changeset
1163 nargs = reverse0(nargs); /* pop in normal order */
kono
parents: 2
diff changeset
1164 i=0;
kono
parents: 2
diff changeset
1165 for (e3=nargs; e3;e3 =cadr(e3)) {
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1166 if ((e4=car(e3))<0) {
2
kono
parents: 1
diff changeset
1167 /* register case */
kono
parents: 1
diff changeset
1168 if (reg_stack[--reg_sp]>=REG_ESI) {
kono
parents: 1
diff changeset
1169 /* the same registger */
kono
parents: 1
diff changeset
1170 } else {
kono
parents: 1
diff changeset
1171 if(reg_stack[reg_sp]<0) {
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1172 printf("\tpopl %s\n",reg_name[rname[REG_ESI+i]]); /* e4? */
2
kono
parents: 1
diff changeset
1173 } else {
kono
parents: 1
diff changeset
1174 printf("\tmovl %s,%s\n",
kono
parents: 1
diff changeset
1175 reg_name[rname[reg_stack[reg_sp]]],
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1176 reg_name[rname[REG_ESI+i]]); /* e4? */
2
kono
parents: 1
diff changeset
1177 free_register(reg_stack[reg_sp]);
kono
parents: 1
diff changeset
1178 }
3
kono
parents: 2
diff changeset
1179 i++;
2
kono
parents: 1
diff changeset
1180 }
kono
parents: 1
diff changeset
1181 } else {
kono
parents: 1
diff changeset
1182 /* local variable case */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1183 if (reg_stack[reg_sp-1]== -2) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1184 /* same positioned variable */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1185 reg_sp--;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1186 } else {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1187 printf("\tmovl %s,%d(%%ebp)\n",register_name((xreg=emit_pop(0)),0), lvar(e4));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1188 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1189 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1190 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1191 free_register(xreg);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1192 if (car(e2) != FNAME) {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1193 xrn=register_name((xreg=emit_pop(0)),0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1194 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1195 free_register(xreg);
6
bf602558130d continuation
kono
parents: 5
diff changeset
1196 if (!env && new_disp+disp1>disp) {
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1197 /* shrink stack if necessary */
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1198 printf("\tleal %d(%%ebp),%%esp\n",new_disp-size_of_int);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1199 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1200 if (car(e2) == FNAME) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1201 printf("\tjmp %s\n",code0->nm);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1202 } else {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1203 printf("\tjmp *%s\n",xrn);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1204 }
3
kono
parents: 2
diff changeset
1205 fnptr->sc = scode;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1206 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1207
d35df41eac69 Initial revision
kono
parents:
diff changeset
1208
d35df41eac69 Initial revision
kono
parents:
diff changeset
1209 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1210 machinop(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1211 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1212 int e2,e3,op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1213
d35df41eac69 Initial revision
kono
parents:
diff changeset
1214 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1215 op = car(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1216 e3 = caddr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1217 g_expr(e3);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1218 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1219 g_expr(e2);
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1220 tosop(car(e1),(e2=pop_register()));
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1221 emit_pop_free(e2);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1222 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1223 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1224 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1225
d35df41eac69 Initial revision
kono
parents:
diff changeset
1226
d35df41eac69 Initial revision
kono
parents:
diff changeset
1227 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1228 rindirect(int e1) /* *(p +5 ) */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1229
d35df41eac69 Initial revision
kono
parents:
diff changeset
1230 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1231 char *op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1232 int e2,e3,byte;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1233
d35df41eac69 Initial revision
kono
parents:
diff changeset
1234 op = ((byte = (car(e1) == CRINDIRECT)) ? "movsbl" : "movl");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1235 e3 = cadr(e2 = cadr(e1));
d35df41eac69 Initial revision
kono
parents:
diff changeset
1236 g_expr(e2);
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1237 printf("\t%s (%s),%s\n",op,register_name(creg,0),register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1238 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1239
d35df41eac69 Initial revision
kono
parents:
diff changeset
1240 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1241 sassign(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1242 {
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
1243 int e2,e3,e4,sz,xreg;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1244
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1245 /* structure assignment */
37
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
1246 e2 = cadr(e1); /* pointer variable to the struct */
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
1247 e3 = cadr(e2); /* offset of the variable (distination) */
39
c63c4fdeb9a7 struct done.
kono
parents: 38
diff changeset
1248 e4 = caddr(e1); /* right value (source) */
37
412ad2e6c2a2 struct copy
kono
parents: 36
diff changeset
1249 sz = cadddr(e1); /* size of struct or union */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1250 g_expr(e4);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1251 emit_push();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1252 g_expr(e2);
38
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
1253 xreg = emit_pop(0);
d48d952da354 struct copy continue
kono
parents: 37
diff changeset
1254 emit_copy(xreg,creg,sz,0);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1255 emit_pop_free(xreg);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1256 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1257 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1258
d35df41eac69 Initial revision
kono
parents:
diff changeset
1259 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1260 assign(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1261 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1262 char *op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1263 int e2,e3,e4,byte;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1264
d35df41eac69 Initial revision
kono
parents:
diff changeset
1265 op = ((byte=(car(e1) == CASS))? "movb" : "movl");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1266 /* e2=e4 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1267 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1268 e3 = cadr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1269 e4 = caddr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1270 switch(car(e2)) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1271 case GVAR: /* i=3 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1272 g_expr(e4);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1273 if (byte) use_data_reg(creg,1);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1274 printf("\t%s %s,%s\n",op,register_name(creg,byte),(char *)caddr(e2));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1275 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1276 case LVAR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1277 g_expr(e4);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1278 if (byte) use_data_reg(creg,1);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1279 printf("\t%s %s,%d(%%ebp)\n",op,register_name(creg,byte),lvar(cadr(e2)));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1280 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1281 case REGISTER:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1282 g_expr(e4);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1283 if (creg!=cadr(e2))
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1284 printf("\tmovl %s,%s\n",register_name(creg,0),register_name(cadr(e2),0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1285 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1286 }
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1287 g_expr(e2);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1288 emit_push();
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1289 use_data_reg(creg,0);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1290 g_expr(e4);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1291 if (byte) use_data_reg(creg,1);
23
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1292 e2 = emit_pop(0);
e605560e6146 Fix breaking edx,ecx on function call
kono
parents: 22
diff changeset
1293 printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(e2,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1294 emit_pop_free(e2);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1295 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1296 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1297 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1298
d35df41eac69 Initial revision
kono
parents:
diff changeset
1299 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1300 assop(int e1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1301 {
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1302 int e2,e3,byte,op,reg;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1303 char *xrn;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1304 int xreg,edx;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1305
d35df41eac69 Initial revision
kono
parents:
diff changeset
1306 /* e2 op= e3 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1307 byte = (car(e1) == CASSOP);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1308 e2 = cadr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1309 if (car(e2)==INDIRECT) e2=cadr(e2);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1310 e3 = caddr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1311 op = cadddr(e1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1312
d35df41eac69 Initial revision
kono
parents:
diff changeset
1313 g_expr(e3);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1314 if (car(e2)==REGISTER) {
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1315 xreg = creg;
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1316 creg = reg = cadr(e2);
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1317 tosop(op,xreg);
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1318 creg = xreg;
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1319 printf("\tmovl %s,%s\n",register_name(reg,0),register_name(creg,0));
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1320 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1321 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1322 }
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1323 emit_push();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1324 g_expr(e2);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1325 edx = edx_setup();
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1326 xrn = register_name(xreg = emit_pop(0),0); /* pop e3 value */
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1327 regv[xreg]=regs[xreg]=1;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1328 printf("\tmovl %s,%s # assop \n",register_name(creg,0),register_name(edx,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1329 regv[edx]=1;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1330 ld_indexx(byte,0,edx);
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1331 tosop(op,xreg);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1332 printf("\t%s %s,(%s)\n",byte ? "movb" : "movl",register_name(creg,byte),register_name(edx,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1333 edx_cleanup();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1334 emit_pop_free(xreg);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1335 regv[creg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1336 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1337 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1338
d35df41eac69 Initial revision
kono
parents:
diff changeset
1339
d35df41eac69 Initial revision
kono
parents:
diff changeset
1340 void
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1341 tosop(int op,int oreg)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1342 {
43
a792cf69d698 *** empty log message ***
kono
parents: 42
diff changeset
1343 int dx;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1344 char *orn,*crn;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1345
d35df41eac69 Initial revision
kono
parents:
diff changeset
1346 switch(op) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1347 case LSHIFT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1348 case ULSHIFT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1349 shift("sall",oreg);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1350 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1351 case RSHIFT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1352 shift("sarl",oreg);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1353 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1354 case URSHIFT:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1355 shift("shrl",oreg);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1356 return;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1357 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1358 if(oreg==-1) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1359 printf("\tpopl %s\n",register_name(dreg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1360 oreg = dreg;
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1361 regv[dreg]=1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1362 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1363 regv[oreg]=1; regs[oreg]=1;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1364 orn = register_name(oreg,0);
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1365 crn = register_name(creg,0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1366 switch(op) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1367 case ADD:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1368 printf("\taddl %s,%s\n",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1369 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1370 case SUB:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1371 printf("\tsubl %s,%s\n",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1372 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1373 case BAND:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1374 printf("\tandl %s,%s\n",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1375 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1376 case EOR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1377 printf("\txorl %s,%s\n",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1378 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1379 case BOR:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1380 printf("\torl %s,%s\n",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1381 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1382 case MUL:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1383 case UMUL:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1384 printf("\t%s %s,%s\n","imull",orn,crn);
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1385 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1386 case DIV:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1387 case UDIV:
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1388 use_register(creg,REG_EAX,1);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1389 edx_setup();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1390 orn = register_name(oreg,0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1391 if (op==DIV)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1392 printf("\tcltd\n\tdivl %s\n",orn);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1393 else
d35df41eac69 Initial revision
kono
parents:
diff changeset
1394 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1395 edx_cleanup();
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1396 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1397 case MOD:
d35df41eac69 Initial revision
kono
parents:
diff changeset
1398 case UMOD:
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1399 use_register(creg,REG_EAX,1);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1400 edx_setup();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1401 orn = register_name(oreg,0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1402 if (op==DIV)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1403 printf("\tcltd\n\tdivl %s\n",orn);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1404 else
d35df41eac69 Initial revision
kono
parents:
diff changeset
1405 printf("\txor %%edx,%%edx\n\tidivl %s\n",orn);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1406 dx = virtual(REG_EDX);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1407 if (dx!=creg) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1408 rname[dx]=rname[creg];
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1409 rname[creg]=REG_EDX;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1410 }
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1411 edx_cleanup();
41
886ca1f2cf15 *** empty log message ***
kono
parents: 40
diff changeset
1412 break;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1413 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1414 if (oreg!=dreg&&oreg>=0)
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1415 free_register(oreg);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1416 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1417
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1418 static int edx_stack=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1419
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1420 int
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1421 edx_setup()
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1422 {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1423 int edx_save;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1424 /* make real EDX register empty */
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1425 if (free_register_count()<1) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1426 for(edx_save = 0;edx_save==dreg||edx_save==creg;edx_save++);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1427 printf("\tpushl %s\n",register_name(edx_save,0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1428 edx_stack = list3(edx_save,edx_stack,0);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1429 } else {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1430 edx_save = get_register();
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1431 edx_stack = list3(edx_save,edx_stack,1);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1432 }
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1433 regv[edx_save]=0;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1434 use_register(edx_save,REG_EDX,0);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1435 return edx_save;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1436 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1437
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1438
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1439 void
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1440 edx_cleanup()
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1441 {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1442 if (caddr(edx_stack)==0) {
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1443 printf("\tpopl %s\n",register_name(car(edx_stack),0));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1444 } else
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1445 free_register(car(edx_stack));
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1446 edx_stack = cadr(edx_stack);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1447 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1448
d35df41eac69 Initial revision
kono
parents:
diff changeset
1449 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1450 shift(char *op, int reg)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1451 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1452 if (reg>=0) {
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1453 use_register(reg,REG_ECX,1);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1454 } else {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1455 use_register(dreg,REG_ECX,0);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1456 printf("\tpopl %%ecx\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1457 }
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1458 printf("\t%s %%cl,%s\n",op,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1459 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1460
d35df41eac69 Initial revision
kono
parents:
diff changeset
1461 void
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1462 ld_indexx(int byte, int n, int xreg)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1463 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1464 char *op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1465
d35df41eac69 Initial revision
kono
parents:
diff changeset
1466 op = byte ? "movsbl" : "movl";
d35df41eac69 Initial revision
kono
parents:
diff changeset
1467 if (n)
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1468 printf("\t%s %d(%s),%s\n",op,n,register_name(xreg,0),register_name(creg,byte));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1469 else
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1470 printf("\t%s (%s),%s\n",op,register_name(xreg,0),register_name(creg,byte));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1471 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1472
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1473 /*
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1474 void
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1475 st_indexx(int byte, int n, int xreg)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1476 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1477 char *op;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1478
d35df41eac69 Initial revision
kono
parents:
diff changeset
1479 op = byte ? "movb" : "movl";
d35df41eac69 Initial revision
kono
parents:
diff changeset
1480 if (n)
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1481 printf("\t%s %s,%d(%s)\n",op,register_name(creg,byte),n,register_name(xreg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1482 else
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1483 printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(xreg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1484 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1485
d35df41eac69 Initial revision
kono
parents:
diff changeset
1486 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1487 indexy(char *op, char *gvar)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1488 {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1489 printf("\t%s %s,%s\n",op,register_name(creg,0),gvar);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1490 }
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1491 */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1492
d35df41eac69 Initial revision
kono
parents:
diff changeset
1493 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1494 cmpdimm(int e, int csreg)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1495 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1496 /* used in dosiwtch() */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1497 use_register(creg,csreg,0);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1498 printf("\tcmpl $%d,%s\n",e,register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1499 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1500
d35df41eac69 Initial revision
kono
parents:
diff changeset
1501 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1502 opening(char *filename)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1503 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1504 printf("\t.file \"%s\"\n",filename);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1505 printf("\t.version\t\"01.01\"\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1506 printf("gcc2_compiled.:\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1507 printf(".text\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1508 emit_init();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1509 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1510
d35df41eac69 Initial revision
kono
parents:
diff changeset
1511 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1512 closing(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1513 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1514 global_table();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1515 printf("\t.ident \"Micro-C compiled\"\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1516 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1517
d35df41eac69 Initial revision
kono
parents:
diff changeset
1518 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1519 rexpr(int e1, int l1, char *s)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1520 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1521 g_expr(list3(SUB,cadr(e1),caddr(e1)));
d35df41eac69 Initial revision
kono
parents:
diff changeset
1522 printf("\tj%s\t_%d\n",s,l1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1523 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1524
d35df41eac69 Initial revision
kono
parents:
diff changeset
1525 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1526 jcond(int l, char cond)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1527 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1528 printf("\tj%s\t_%d\n",cond?"ne":"e",l);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1529 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1530
d35df41eac69 Initial revision
kono
parents:
diff changeset
1531 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1532 jmp(int l)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1533 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1534 control=0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1535 printf("\tjmp\t_%d\n",l);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1536 /* align? */
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1537 /*
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1538 this is not allowed because of ? operator
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1539 regv[creg]=regv[dreg]=0;
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1540 use_register(creg,REG_EAX,0);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1541 use_register(dreg,REG_EBX,0);
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1542 */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1543 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1544
d35df41eac69 Initial revision
kono
parents:
diff changeset
1545 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
1546 fwdlabel(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1547 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1548 return labelno++;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1549 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1550
d35df41eac69 Initial revision
kono
parents:
diff changeset
1551 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1552 fwddef(int l)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1553 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1554 control=1;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1555 printf("_%d:\n",l);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1556 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1557
d35df41eac69 Initial revision
kono
parents:
diff changeset
1558 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
1559 backdef(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1560 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1561 control=1;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1562 printf("_%d:\n",labelno);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1563 return labelno++;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1564 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1565
d35df41eac69 Initial revision
kono
parents:
diff changeset
1566 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1567 def_label(int cslabel, int dlabel)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1568 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1569 int fl;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1570
d35df41eac69 Initial revision
kono
parents:
diff changeset
1571 fl = 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1572 if (control) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1573 jmp(fl=fwdlabel());
d35df41eac69 Initial revision
kono
parents:
diff changeset
1574 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1575 fwddef(cslabel);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1576 if (dlabel)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1577 jmp(dlabel);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1578 if (fl) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1579 fwddef(fl);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1580 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1581 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1582
d35df41eac69 Initial revision
kono
parents:
diff changeset
1583 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1584 gen_comment(char *s)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1585 {
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1586 printf("## %s",s);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1587 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1588
d35df41eac69 Initial revision
kono
parents:
diff changeset
1589 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1590 gen_source(char *s)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1591 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1592 printf("%s",s);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1593 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1594
d35df41eac69 Initial revision
kono
parents:
diff changeset
1595 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1596 code_enter(char *name)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1597 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1598 printf("\t.align 4\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1599 if (stmode!=STATIC)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1600 printf(".globl %s\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1601 printf("\t.type\t%s,@function\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1602 printf("%s:\n",name);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1603 free_all_register();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1604 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1605
d35df41eac69 Initial revision
kono
parents:
diff changeset
1606 void
6
bf602558130d continuation
kono
parents: 5
diff changeset
1607 code_enter1(int disp0,int args)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1608 {
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1609 code_disp_label=fwdlabel();
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1610 printf("\tsubl $_%d,%%esp\n",code_disp_label);
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1611 /*
6
bf602558130d continuation
kono
parents: 5
diff changeset
1612 if(disp0) {
bf602558130d continuation
kono
parents: 5
diff changeset
1613 printf("\tsubl $%d,%%esp\n",-disp0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1614 }
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1615 */
21
f9f789aa7587 Linux version (Redhat 7.2)
kono
parents: 18
diff changeset
1616 printf("## args %d disp %d code_arg_offset=%d code_disp_offset=%d\n",args,disp,code_arg_offset,code_disp_offset);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1617 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1618
d35df41eac69 Initial revision
kono
parents:
diff changeset
1619 void
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1620 code_leave(char *name,int disp0)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1621 {
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1622 printf("\t.set _%d,%d\n",code_disp_label,-disp0);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1623 printf("_%d:\n",labelno);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1624 printf("\t.size\t%s,_%d-%s\n",name,labelno,name);
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1625 local_table();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1626 labelno++;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1627 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1628
d35df41eac69 Initial revision
kono
parents:
diff changeset
1629 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1630 enter(char *name)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1631 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1632 printf("\t.align 2\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1633 if (stmode!=STATIC)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1634 printf(".globl %s\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1635 printf("%s:\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1636 printf("\t.type\t%s,@function\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1637 printf("\tpushl %%ebp\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1638 printf("\tmovl %%esp,%%ebp\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1639 printf("\tpushl %%ebx\n");
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
1640 printf("\tpushl %%esi\n");
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
1641 printf("\tpushl %%edi\n");
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1642 free_all_register();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1643 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1644
d35df41eac69 Initial revision
kono
parents:
diff changeset
1645 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1646 enter1(int disp)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1647 {
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1648 func_disp_label=fwdlabel();
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1649 printf("\tsubl $_%d,%%esp\n",func_disp_label);
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1650 /* if(disp) printf("\tsubl $%d,%%esp\n",-disp); */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1651 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1652
d35df41eac69 Initial revision
kono
parents:
diff changeset
1653 void
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1654 leave(int control, char *name,int disp)
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1655 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1656 if (control)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1657 use_register(creg,REG_EAX,1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1658 if (retcont) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1659 if (control)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1660 jmp(retlabel);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1661 fwddef(retcont);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1662 use_register(creg,REG_EAX,0);
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1663 printf("\tmovl %s,%s\n",reg_name[REG_ESI],register_name(creg,0));
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1664 /* printf("\tleave\n"); */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1665 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1666 fwddef(retlabel);
42
a89cf0d6904f regv assop
kono
parents: 41
diff changeset
1667 /* use_register(creg,REG_EAX,0); too late */
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1668 /* if(disp) printf("\taddl $%d,%%esp\n",-disp); */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1669 printf("\tlea %d(%%ebp),%%esp\n",disp_offset);
24
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
1670 printf("\tpopl %%edi\n");
1df0bf7eb5da add esi/edi register. fix virtual register bug.
kono
parents: 23
diff changeset
1671 printf("\tpopl %%esi\n");
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1672 printf("\tpopl %%ebx\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1673 printf("\tleave\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1674 printf("\tret\n");
36
9f89cef85b41 *** empty log message ***
kono
parents: 33
diff changeset
1675 printf("\t.set _%d,%d\n",func_disp_label,-disp);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1676 printf("_%d:\n",labelno);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1677 printf("\t.size\t%s,_%d-%s\n",name,labelno,name);
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1678 local_table();
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1679 labelno++;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1680 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1681
d35df41eac69 Initial revision
kono
parents:
diff changeset
1682
d35df41eac69 Initial revision
kono
parents:
diff changeset
1683 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1684 ret(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1685 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1686 use_register(creg,REG_EAX,1);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1687 jmp(retlabel);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1688 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1689
d35df41eac69 Initial revision
kono
parents:
diff changeset
1690 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1691 gen_gdecl(char *n, int gpc)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1692 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1693 /*
d35df41eac69 Initial revision
kono
parents:
diff changeset
1694 if (stmode!=STATIC)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1695 printf(".globl %s\n",n);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1696 */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1697 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1698
d35df41eac69 Initial revision
kono
parents:
diff changeset
1699 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1700 align(int t)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1701 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1702 if (t!=CHAR) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1703 if (data_alignment & 1)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1704 printf("\t.align 2\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1705 data_alignment = 0;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1706 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1707 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1708
d35df41eac69 Initial revision
kono
parents:
diff changeset
1709 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1710 emit_data(int e, int t, NMTBL *n)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1711 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1712 int l;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1713 char *name;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1714 name = n->nm;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1715 if(mode!=GDECL) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1716 error(-1); return;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1717 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1718 if (n->dsp != -1) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1719 n->dsp = -1; /* initiallized flag */
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1720 printf(".globl\t%s\n",name);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1721 data_mode(name);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1722 align(t);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1723 printf("%s:\n",name);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1724 } else {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1725 data_mode(0);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1726 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1727 if(car(e)==CONST) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1728 if (t==CHAR) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1729 printf("\t.byte %d\n",cadr(e));
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1730 if (data_alignment>0)
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1731 data_alignment++;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1732 gpc += 1;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1733 } else {
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1734 printf("\t.long %d\n",cadr(e));
3
kono
parents: 2
diff changeset
1735 gpc += size_of_int;
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1736 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1737 } else if(t!=CHAR) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1738 gpc += size_of_int;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1739 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) {
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1740 printf("\t.long %s\n",(char *)caddr(cadr(e)));
13
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1741 } else if(car(e)==FNAME) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1742 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1743 } else if(car(e)==STRING) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1744 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1745 l = fwdlabel();
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1746 printf("\t.long _%d\n",l);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1747 printf(".section\t.rodata\n");
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1748 printf("_%d:\n",l);
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1749 output_mode = RODATA_EMIT_MODE;
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1750 }
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1751 ascii((char *)cadr(e));
a8a812dace23 struct local initialization
kono
parents: 12
diff changeset
1752 } else error(TYERR);
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1753 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1754 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1755
d35df41eac69 Initial revision
kono
parents:
diff changeset
1756 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1757 emit_data_closing(NMTBL *n)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1758 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1759 int lb;
18
df7fa8cee67b pass -Wall
kono
parents: 15
diff changeset
1760 if (mode==GDECL) {
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1761 data_mode(0);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1762 lb=fwdlabel();
d35df41eac69 Initial revision
kono
parents:
diff changeset
1763 printf("_%d:\n",lb);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1764 printf("\t.size\t%s,_%d-%s\n",n->nm,lb,n->nm);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1765 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1766 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1767
d35df41eac69 Initial revision
kono
parents:
diff changeset
1768 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1769 global_table(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1770 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1771 NMTBL *n;
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1772 int init;
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1773 init=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1774 for(n=ntable;n < &ntable[GSYMS];n++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1775 if (n->sc == GVAR && n->dsp != -1) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1776 /* n->dsp = -1 means initialized global */
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1777 if (init==0) {
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1778 data_mode(0);
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1779 init=1;
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1780 }
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1781 printf(".comm %s,%d\n",n->nm,size(n->ty));
d35df41eac69 Initial revision
kono
parents:
diff changeset
1782 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1783 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1784 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1785
d35df41eac69 Initial revision
kono
parents:
diff changeset
1786 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1787 local_table(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1788 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1789 NMTBL *n;
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1790 int init;
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1791 init=0;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1792 /* static local variables */
d35df41eac69 Initial revision
kono
parents:
diff changeset
1793 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1794 if (n->sc == GVAR) {
12
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1795 if (init==0) {
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1796 data_mode(0);
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1797 init=1;
f928bd330351 *** empty log message ***
kono
parents: 7
diff changeset
1798 }
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1799 printf(".lcomm %s,%d\n",n->nm,size(n->ty));
d35df41eac69 Initial revision
kono
parents:
diff changeset
1800 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1801 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1802 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1803
d35df41eac69 Initial revision
kono
parents:
diff changeset
1804 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1805 text_mode(void)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1806 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1807 if (output_mode!=TEXT_EMIT_MODE) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1808 printf(".text\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1809 printf("\t.align 2\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1810 output_mode = TEXT_EMIT_MODE;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1811 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1812 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1813
d35df41eac69 Initial revision
kono
parents:
diff changeset
1814 void
d35df41eac69 Initial revision
kono
parents:
diff changeset
1815 data_mode(char *name)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1816 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1817 if (output_mode!=DATA_EMIT_MODE) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1818 printf(".data\n");
d35df41eac69 Initial revision
kono
parents:
diff changeset
1819 output_mode = DATA_EMIT_MODE;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1820 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1821 if (name)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1822 printf("\t.type\t%s,@object\n",name);
d35df41eac69 Initial revision
kono
parents:
diff changeset
1823 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1824
d35df41eac69 Initial revision
kono
parents:
diff changeset
1825 int
d35df41eac69 Initial revision
kono
parents:
diff changeset
1826 lvar(int l)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1827 {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1828 if (l<0) {
d35df41eac69 Initial revision
kono
parents:
diff changeset
1829 if (fnptr->sc==CODE)
d35df41eac69 Initial revision
kono
parents:
diff changeset
1830 return l+code_disp_offset;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1831 else
d35df41eac69 Initial revision
kono
parents:
diff changeset
1832 return l+disp_offset;
d35df41eac69 Initial revision
kono
parents:
diff changeset
1833 } else {
3
kono
parents: 2
diff changeset
1834 if (fnptr->sc==CODE)
5
3db1b6d78412 *** empty log message ***
kono
parents: 4
diff changeset
1835 return -l+code_arg_offset;
3
kono
parents: 2
diff changeset
1836 else
kono
parents: 2
diff changeset
1837 return l+arg_offset;
0
d35df41eac69 Initial revision
kono
parents:
diff changeset
1838 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1839 }
d35df41eac69 Initial revision
kono
parents:
diff changeset
1840
d35df41eac69 Initial revision
kono
parents:
diff changeset
1841 /* end */