annotate mc-nop-386.c @ 15:6667dbd4f718 struct-copy

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