annotate mc-nop-386.c @ 2:b3f259ac34a1

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