annotate mc-nop-386.c @ 19:b62230ea38f6

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