annotate mc-nop-386.c @ 0:d35df41eac69

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