annotate mc-nop-386.c @ 1:0529f5abe9d0

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