Mercurial > hg > CbC > old > device
annotate mc-code-powerpc.c @ 173:3b33c7daae95
*** empty log message ***
author | kono |
---|---|
date | Wed, 26 Nov 2003 18:55:37 +0900 |
parents | 096559f07a70 |
children | 352feeae4b0a |
rev | line source |
---|---|
89 | 1 /* Micro-C Code Generatation Part for Power PC (Mac OS X) */ |
2 /* $Id$ */ | |
3 | |
4 #define EXTERN extern | |
5 #include "mc.h" | |
93 | 6 #include "mc-code.h" |
89 | 7 #include "mc-codegen.h" |
8 | |
173 | 9 char *l_include_path[] = { |
10 "/usr/include/", | |
11 0 | |
12 }; | |
13 | |
89 | 14 #define TEXT_EMIT_MODE 0 |
15 #define DATA_EMIT_MODE 1 | |
16 #define RODATA_EMIT_MODE 2 | |
17 | |
93 | 18 static void data_mode(char *name); |
19 static void init_ptr_cache(); | |
162 | 20 static void ld_indexx(int byte, int n, int xreg,int sign); |
93 | 21 static void local_table(void); |
22 static void shift(char *op, int reg); | |
119 | 23 static int struct_push(int e4,int t,int arg); |
93 | 24 |
89 | 25 static int output_mode = TEXT_EMIT_MODE; |
26 static int data_alignment = 0; | |
27 | |
28 static int code_disp_label; | |
111 | 29 static int code_setup; |
116 | 30 static int r1_offset_label; |
31 static int lvar_offset_label; | |
32 | |
33 static int reg_save; | |
34 static int freg_save; | |
35 | |
147 | 36 static int freg,ireg; |
37 | |
116 | 38 int size_of_int = 4; |
165 | 39 int size_of_short = 2; |
116 | 40 int size_of_float = 4; |
41 int size_of_double = 8; | |
42 int size_of_longlong = 8; | |
43 int endian = 1; | |
111 | 44 |
138 | 45 int reg_sp; /* REGister Stack-Pointer */ |
46 int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ | |
47 | |
48 /* floating point registers */ | |
49 | |
50 int freg_sp; /* floating point REGister Stack-Pointer */ | |
51 int freg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ | |
89 | 52 |
119 | 53 #define REG_fp 1 |
54 #define REG_sp 30 | |
55 #define REG_VAR_BASE 29 | |
130 | 56 #define REG_VAR_MIN 18 |
119 | 57 #define MIN_TMP_REG 3 |
130 | 58 #define MAX_TMP_REG 11 |
119 | 59 |
60 #define PTRC_REG 3 | |
61 | |
62 #define FREG_VAR_BASE 31 | |
130 | 63 #define FREG_VAR_MIN 20 |
119 | 64 #define MIN_TMP_FREG 1 |
130 | 65 #define MAX_TMP_FREG 14 |
119 | 66 |
67 #define RET_REGISTER 3 | |
145 | 68 #define RET_FREGISTER (1+FREG_OFFSET) |
119 | 69 |
70 int MAX_REGISTER=30; /* PowerPCのレジスタを10個まで使う*/ | |
122 | 71 int MAX_FREGISTER=31; |
119 | 72 #define REAL_MAX_REGISTER 32 /* PowerPCのレジスタが32ということ*/ |
73 #define REAL_MAX_FREGISTER 32 /* PowerPCのレジスタが32ということ*/ | |
74 | |
145 | 75 #define FREG_OFFSET REAL_MAX_REGISTER |
76 | |
122 | 77 int MAX_INPUT_REGISTER_VAR = 11-MIN_TMP_REG; |
78 int MAX_CODE_INPUT_REGISTER_VAR = 11-MIN_TMP_REG; | |
79 int MAX_INPUT_DREGISTER_VAR = 14-MIN_TMP_FREG; | |
138 | 80 int MAX_INPUT_FREGISTER_VAR = 14-MIN_TMP_FREG; |
122 | 81 int MAX_CODE_INPUT_DREGISTER_VAR = 14-MIN_TMP_FREG; |
138 | 82 int MAX_CODE_INPUT_FREGISTER_VAR = 14-MIN_TMP_FREG; |
119 | 83 |
84 #define CREG_REGISTER MAX_TMP_REG | |
145 | 85 #define FREG_FREGISTER (MAX_TMP_FREG+FREG_OFFSET) |
119 | 86 |
145 | 87 int powerpc_regs[REAL_MAX_REGISTER+REAL_MAX_FREGISTER]; |
88 int powerpc_regv[REAL_MAX_REGISTER+REAL_MAX_FREGISTER]; | |
119 | 89 |
90 int *regv = powerpc_regv; | |
91 int *regs = powerpc_regs; | |
92 | |
93 static int max_reg_var, max_freg_var; | |
94 static int cond_reg=-1,cond_freg=-1; | |
95 | |
96 static char *reg_name[] = { | |
97 "r0","r1","r2","r3","r4","r5","r6","r7","r8","r9", | |
98 "r10","r11","r12","r13","r14","r15","r16","r17","r18","r19", | |
99 "r20","r21","r22","r23","r24","r25","r26","r27","r28","r29", | |
145 | 100 "r30","r31", |
119 | 101 "f0","f1","f2","f3","f4","f5","f6","f7","f8","f9", |
102 "f10","f11","f12","f13","f14","f15","f16","f17","f18","f19", | |
103 "f20","f21","f22","f23","f24","f25","f26","f27","f28","f29", | |
104 "f30","f31" | |
105 }; | |
106 | |
107 #define register_name(i) reg_name[i] | |
145 | 108 #define fregister_name(i) reg_name[i] |
119 | 109 |
147 | 110 #define is_int_reg(i) (0<=i&&i<REAL_MAX_REGISTER) |
111 #define is_float_reg(i) (REAL_MAX_REGISTER<=i&&i<REAL_MAX_FREGISTER+REAL_MAX_REGISTER) | |
112 | |
113 | |
114 int use_int(int i) { | |
115 if (!is_int_reg(i)) i = ireg; | |
116 if (!regs[i]) regs[i]=1; | |
117 return i; | |
118 } | |
119 | |
120 int use_float(int i) { | |
121 if (!is_float_reg(i)) i = freg; | |
122 if (!regs[i]) regs[i]=1; | |
123 return i; | |
124 } | |
125 | |
126 int use_double(int i) { | |
127 if (!is_float_reg(i)) i = freg; | |
128 if (!regs[i]) regs[i]=1; | |
129 return i; | |
130 } | |
131 | |
119 | 132 static |
133 NMTBL float_zero = {"_float_zero",STATIC,FLOAT,0}; | |
134 static | |
135 NMTBL float_one = {"_float_one",STATIC,FLOAT,0}; | |
136 | |
137 | |
138 static char * fload(int d); | |
139 static int code_d1(double d); | |
140 static int code_d2(double d); | |
141 static void code_save_stacks(); | |
142 static void code_save_input_registers(); | |
143 static void clear_ptr_cache_reg(int r); | |
144 static void set_creg(int,int); | |
145 static void set_freg(int,int); | |
146 | |
153 | 147 static int max_func_args; |
148 static int my_func_args; | |
119 | 149 #define ARG_LVAR_OFFSET 0x10000000 |
150 | |
126 | 151 /* |
89 | 152 |
97 | 153 r0 return value etc. |
154 r3-r10 input register | |
126 | 155 r22-r29 saved register variable (input register for code segement) |
89 | 156 r30 stack pointer |
157 r31 0 | |
158 r1 frame pointer | |
97 | 159 |
160 f0 return value etc. | |
161 f1-r8 input register | |
162 f24-f31 saved register variable | |
116 | 163 |
127 | 164 function call stack frame |
116 | 165 <------r1_offset------------------------------> |
166 <------------lvar_offset-------> | |
167 r+ +------------+---+---------------+----------+--------------+----+ - | |
122 | 168 callee arg xx register save local caller arg xx |
116 | 169 reg_save disp max_func_args*size_of_int |
119 | 170 lvar>0 lvar<0 lvar>0x1000 0000 |
127 | 171 |
172 code segment stack frame | |
173 | |
174 * gotoを呼び出した関数のr1 ! r1(goto前のr1) | |
128 | 175 # * r30 <---r1_offset---------> r1 |
127 | 176 r+ +----------+--+----------+----------------+-----------+----------+----+ |
177 cousin arg xx reg save !callee arg !code local caller arg xx | |
178 r20-r29 lvar>0 lvar<0 lvar>0x1000 000 | |
179 f20-f31 <-my_func_args--><--disp-----><-max_func_arg-> | |
180 *size_of_int *size_of_int | |
181 | |
89 | 182 */ |
122 | 183 int arg_offset = 24; int arg_offset1 = 24; int disp_offset = -12; |
184 #define func_disp_offset 60 | |
185 #define r1_offset func_disp_offset+12 | |
121 | 186 int code_disp_offset = 0; int jump_offset = 0; |
187 #define CODE_LVAR l+code_disp_offset | |
129 | 188 #define CODE_CALLER_ARG (l-ARG_LVAR_OFFSET)+arg_offset1 |
121 | 189 #define FUNC_LVAR l+disp_offset |
190 #define CALLER_ARG (l-ARG_LVAR_OFFSET)+arg_offset1 | |
191 #define CALLEE_ARG l+arg_offset | |
89 | 192 |
116 | 193 void |
194 code_offset_set() | |
195 { | |
126 | 196 #if 0 |
121 | 197 int l; |
126 | 198 #endif |
121 | 199 int lvar_offsetv = -disp+max_func_args*size_of_int+func_disp_offset; |
200 int r1_offsetv = -disp+max_func_args*size_of_int-reg_save+r1_offset; | |
201 printf(".set L_%d,%d\n",lvar_offset_label,lvar_offsetv); | |
202 printf(".set L_%d,%d\n",r1_offset_label,r1_offsetv); | |
126 | 203 #if 0 |
121 | 204 printf("# function %s\n",fnptr->nm); |
205 l = ARG_LVAR_OFFSET; | |
206 printf("# offset call0\t%d\n",CALLER_ARG); | |
207 l = ARG_LVAR_OFFSET+max_func_args*size_of_int; | |
122 | 208 printf("# offset calln\t%d %d\n",CALLER_ARG,max_func_args*size_of_int); |
121 | 209 l = disp; |
122 | 210 printf("# offset lvarn\t%d %d\n",FUNC_LVAR+lvar_offsetv,disp); |
121 | 211 l = 0; |
212 printf("# offset lvar0\t%d\n",FUNC_LVAR+lvar_offsetv); | |
213 l = -reg_save; | |
214 printf("# offset regs\t%d\n",FUNC_LVAR+lvar_offsetv); | |
215 printf("# offset r1off\t%d\n",r1_offsetv); | |
216 l = 0; | |
217 printf("# offset carg0\t%d\n",CALLEE_ARG+r1_offsetv); | |
218 l = my_func_args; | |
122 | 219 printf("# offset cargn\t%d %d\n",CALLEE_ARG+r1_offsetv,my_func_args); |
126 | 220 #endif |
116 | 221 } |
111 | 222 |
119 | 223 |
224 static void | |
225 lvar8(int l) | |
116 | 226 { |
227 if (fnptr->sc==CODE) { | |
129 | 228 if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
229 printf("lo16(%d)(r1)\n",CODE_CALLER_ARG); | |
230 } else | |
231 printf("lo16(%d)(r30)\n",CODE_LVAR); | |
119 | 232 } else if (l<0) { /* local variable */ |
233 printf("lo16(%d+L_%d)(r30)\n",FUNC_LVAR,lvar_offset_label); | |
125 | 234 } else if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
119 | 235 printf("lo16(%d)(r30)\n",CALLER_ARG); |
236 } else { /* callee's arguments */ | |
121 | 237 printf("lo16(%d+L_%d)(r30)\n",CALLEE_ARG,r1_offset_label); |
116 | 238 } |
239 } | |
111 | 240 |
119 | 241 /* if size of local variables / input variables is more then 64k, |
242 lo16 does not work. We have to use ha16 also. But we can't know | |
243 the exact size in one path compile. We may safely use lvar16ha | |
244 if disp or max_func_args > 32k. Of course this is reduantant for | |
245 smaller offset. But who cares who use very large local variables? | |
246 */ | |
96 | 247 |
119 | 248 #define LARGE_LVAR (disp<-32765||max_func_args>32765) |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
249 |
119 | 250 static void |
251 lvar16ha(int l) | |
252 { | |
253 if (fnptr->sc==CODE) { | |
129 | 254 if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
255 printf("la r0,ha16(%d)(r1)\n",CODE_CALLER_ARG); | |
256 } else | |
257 printf("la r0,ha16(%d)(r30)\n",CODE_LVAR); | |
119 | 258 } else if (l<0) { /* local variable */ |
259 printf("la r0,ha16(%d+L_%d)(r30)\n",FUNC_LVAR,lvar_offset_label); | |
125 | 260 } else if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
119 | 261 if (CALLER_ARG>32765) |
262 printf("la r0,ha16(%d)(r30)\n",CALLER_ARG); | |
263 } else { /* callee's arguments */ | |
121 | 264 printf("la r0,ha16(%d+L_%d)(r30)\n",CALLEE_ARG,r1_offset_label); |
119 | 265 } |
266 } | |
95 | 267 |
119 | 268 static void |
269 lvar16lo(int l) | |
270 { | |
271 if (fnptr->sc==CODE) { | |
129 | 272 if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
273 printf("lo16(%d)(r0)\n",CODE_CALLER_ARG); | |
274 } else | |
275 printf("lo16(%d)(r0)\n",CODE_LVAR); | |
119 | 276 } else if (l<0) { /* local variable */ |
277 printf("lo16(%d+L_%d)(r0)\n",FUNC_LVAR,lvar_offset_label); | |
125 | 278 } else if (l>=ARG_LVAR_OFFSET) { /* caller's arguments */ |
119 | 279 if (CALLER_ARG>32765) |
280 printf("lo16(%d)(r0)\n",CALLER_ARG); | |
281 else | |
282 printf("lo16(%d)(r30)\n",CALLER_ARG); | |
283 } else { /* callee's arguments */ | |
121 | 284 printf("lo16(%d+L_%d)(r0)\n",CALLEE_ARG,r1_offset_label); |
119 | 285 } |
286 } | |
89 | 287 |
119 | 288 #define lvar_intro(i) if (LARGE_LVAR) lvar16ha(i) |
289 | |
290 #define lvar(i) if (LARGE_LVAR) lvar16lo(i); else lvar8(i) | |
89 | 291 |
119 | 292 void |
293 code_lvar(int e2,int creg) { | |
294 lvar_intro(e2); | |
295 printf("\tla %s,",register_name(creg)); | |
296 lvar(e2); | |
297 regv[creg]=1; | |
298 } | |
95 | 299 |
89 | 300 void |
301 code_init(void) | |
302 { | |
173 | 303 macro_define("__ppc__ 1\n"); |
304 | |
102 | 305 init_ptr_cache(); |
89 | 306 } |
307 | |
308 void | |
309 gexpr_code_init(void){ | |
172 | 310 #if 0 |
311 int new_reg,old_reg,dm; | |
312 /* register shuffling */ | |
313 if (is_int_reg(creg)) { | |
314 dm = get_register(); | |
315 new_reg = get_register(); | |
316 old_reg = creg; | |
317 ireg = creg = new_reg; | |
318 } else if (is_float_reg(creg)) { | |
319 dm = get_dregister(1); | |
320 new_reg = get_dregister(1); | |
321 old_reg = creg; | |
322 freg = creg = new_reg; | |
323 } | |
324 free_register(old_reg); | |
325 free_register(dm); | |
326 #endif | |
89 | 327 regv[creg]=0; |
145 | 328 regv[freg]=0; |
96 | 329 } |
330 | |
147 | 331 void |
332 code_gexpr(int e){ | |
333 if (is_int_reg(creg) && creg!=ireg) error(-1); | |
334 } | |
335 | |
89 | 336 |
109 | 337 void |
137 | 338 code_arg_register(NMTBL *fnptr) |
109 | 339 { |
137 | 340 int args = fnptr->dsp; |
341 NMTBL *n; | |
342 int reg_var = 0; | |
343 int freg_var = 0; | |
344 int type; | |
345 int reg; | |
346 int is_code0 = is_code(fnptr); | |
347 | |
348 while (args) { | |
349 /* process in reverse order */ | |
350 n = (NMTBL*)caddr(args); | |
351 type = n->ty; | |
352 if (scalar(type)) { | |
353 if ((reg = get_input_register_var(reg_var,n,is_code0))) { | |
354 n->sc = REGISTER; | |
355 n->dsp = cadr(reg); | |
356 regv[n->dsp]= 1; | |
357 regs[n->dsp]= INPUT_REG; | |
358 reg_var++; | |
359 cadddr(args)=size_of_int; /* why we need this? */ | |
360 } | |
138 | 361 } else if (type==FLOAT) { |
362 if ((reg = get_input_dregister_var(freg_var,n,is_code0,0))) { | |
363 n->sc = DREGISTER; | |
364 n->dsp = cadr(reg); | |
145 | 365 regv[n->dsp]= 1; |
366 regs[n->dsp]= INPUT_REG; | |
138 | 367 freg_var++; |
368 cadddr(args)=size(type); /* why we need this? */ | |
369 } | |
370 } else if (type==DOUBLE) { | |
371 if ((reg = get_input_dregister_var(freg_var,n,is_code0,1))) { | |
137 | 372 n->sc = DREGISTER; |
373 n->dsp = cadr(reg); | |
145 | 374 regv[n->dsp]= 1; |
375 regs[n->dsp]= INPUT_REG; | |
137 | 376 freg_var++; |
377 cadddr(args)=size(type); /* why we need this? */ | |
378 } | |
379 } | |
380 args = cadr(args); | |
381 } | |
126 | 382 if (is_function(fnptr)) |
383 code_save_input_registers(); | |
109 | 384 } |
385 | |
89 | 386 int |
387 get_register(void) | |
388 { /* 使われていないレジスタを調べる */ | |
120 | 389 int i,reg; |
96 | 390 for(i=MAX_TMP_REG;i>MIN_TMP_REG;i--) { |
102 | 391 if (regs[i]) continue; /* 使われている */ |
392 regs[i]=USING_REG; /* そのレジスタを使うことを宣言し */ | |
393 return i; /* その場所を表す番号を返す */ | |
394 } | |
135 | 395 /* PTR_CACHE をつぶす */ |
102 | 396 for(i=MAX_TMP_REG;i>MIN_TMP_REG;i--) { |
119 | 397 if (regs[i]==PTRC_REG) { |
98 | 398 clear_ptr_cache_reg(i); |
96 | 399 } else |
400 continue; | |
401 regs[i]=USING_REG; /* そのレジスタを使うことを宣言し */ | |
402 return i; /* その場所を表す番号を返す */ | |
403 } | |
404 /* search register stack */ | |
405 for(i=0;i<reg_sp;i++) { | |
120 | 406 if ((reg=reg_stack[i])>=0) { |
96 | 407 code_assign_lvar( |
120 | 408 (reg_stack[i]=new_lvar(size_of_int)),reg,0); |
96 | 409 reg_stack[i]= reg_stack[i]-REG_LVAR_OFFSET; |
120 | 410 return reg; |
89 | 411 } |
412 } | |
130 | 413 for(i=0;i<REG_VAR_BASE-REG_VAR_MIN;i++) { |
414 reg =REG_VAR_BASE-i; | |
415 if (! regs[reg]) { /* 使われていないなら */ | |
416 regs[reg]=USING_REG; /* そのレジスタを使うことを宣言し */ | |
417 regv[reg]=0; | |
418 if (i>max_reg_var) max_reg_var=i; | |
419 return reg; | |
420 } | |
421 } | |
96 | 422 /* 空いている場所がないなら、エラー (いったい誰が使ってるの?) */ |
136
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
423 error(HPERR); return creg; |
89 | 424 } |
425 | |
93 | 426 int |
427 pop_register(void) | |
428 { /* レジスタから値を取り出す */ | |
429 return reg_stack[--reg_sp]; | |
430 } | |
431 | |
96 | 432 int |
138 | 433 get_dregister(int d) |
96 | 434 { /* 使われていないレジスタを調べる */ |
103 | 435 int i,reg; |
145 | 436 for(i=MAX_TMP_FREG+FREG_OFFSET;i>MIN_TMP_FREG+FREG_OFFSET;i--) { |
437 if (regs[i]) continue; /* 使われている */ | |
438 regs[i]=USING_REG; /* そのレジスタを使うことを宣言し */ | |
102 | 439 return i; /* その場所を表す番号を返す */ |
440 } | |
96 | 441 /* search register stack */ |
442 for(i=0;i<freg_sp;i++) { | |
103 | 443 if ((reg=freg_stack[i])>=0) { |
96 | 444 code_dassign_lvar( |
119 | 445 (freg_stack[i]=new_lvar(size_of_double)),reg,1); |
96 | 446 freg_stack[i]= freg_stack[i]-REG_LVAR_OFFSET; |
120 | 447 return reg; |
96 | 448 } |
449 } | |
130 | 450 for(i=0;i<FREG_VAR_BASE-REG_VAR_MIN;i++) { |
145 | 451 reg =FREG_VAR_BASE-i+FREG_OFFSET; |
452 if (! regs[reg]) { /* 使われていないなら */ | |
453 regs[reg]=USING_REG; /* そのレジスタを使うことを宣言し */ | |
454 regv[reg]=0; | |
130 | 455 if (i>max_freg_var) max_freg_var=i; |
456 return reg; | |
457 } | |
458 } | |
96 | 459 /* 空いている場所がないなら、エラー (いったい誰が使ってるの?) */ |
98 | 460 error(REG_ERR); return freg; |
96 | 461 } |
462 | |
463 int | |
97 | 464 pop_fregister(void) |
96 | 465 { /* レジスタから値を取り出す */ |
466 return freg_stack[--freg_sp]; | |
467 } | |
468 | |
93 | 469 void |
470 emit_pop_free(int xreg) | |
471 { | |
103 | 472 if (xreg>=0) |
473 free_register(xreg); | |
93 | 474 } |
475 | |
89 | 476 void |
477 free_register(int i) { /* いらなくなったレジスタを開放 */ | |
147 | 478 if (i<0||MAX_FREGISTER+FREG_OFFSET<i) error(-1); |
145 | 479 regv[i]=regs[i]=0; |
96 | 480 } |
481 | |
89 | 482 int |
138 | 483 get_input_dregister_var(int i,NMTBL *n,int is_code,int d) |
99 | 484 { |
126 | 485 if (is_code) { |
148 | 486 if(!(i<FREG_VAR_BASE-FREG_VAR_MIN)) return 0; |
487 i = FREG_VAR_BASE-i+FREG_OFFSET; | |
126 | 488 } else { |
135 | 489 if (i<0||i>=MAX_INPUT_DREGISTER_VAR) return 0; |
145 | 490 i = i+MIN_TMP_FREG+FREG_OFFSET; |
126 | 491 } |
492 return list3(DREGISTER,i,(int)n); | |
99 | 493 } |
494 | |
495 int | |
126 | 496 get_input_register_var(int i,NMTBL *n,int is_code) |
99 | 497 { |
126 | 498 if (is_code) { |
131 | 499 if(!(i<REG_VAR_BASE-REG_VAR_MIN)) return 0; |
126 | 500 i = REG_VAR_BASE-i; |
501 } else { | |
135 | 502 if (i<0||i>=MAX_INPUT_REGISTER_VAR) return 0; |
503 i = i+MIN_TMP_REG; | |
504 } | |
505 return list3(REGISTER,i,(int)n); | |
506 } | |
507 | |
145 | 508 /* double register case? */ |
509 | |
135 | 510 int |
511 get_input_register_var_1(int i,NMTBL *n,int is_code) | |
512 { | |
513 if (is_code) { | |
514 if(!(i<REG_VAR_BASE-REG_VAR_MIN)) return 0; | |
515 i = REG_VAR_BASE-i; | |
516 } else { | |
517 if (i<0||i>=MAX_INPUT_REGISTER_VAR+1) return 0; | |
126 | 518 i = i+MIN_TMP_REG; |
519 } | |
520 return list3(REGISTER,i,(int)n); | |
99 | 521 } |
522 | |
523 int | |
137 | 524 free_register_count(int d) |
103 | 525 { |
526 int i,count,fcount; | |
527 fcount = count = 0; | |
528 for(i=0;i<MAX_REGISTER;i++) { | |
529 if (! regs[i] && ! regv[i]) count++; | |
530 } | |
531 for(i=0;i<MAX_FREGISTER;i++) { | |
145 | 532 if (! regs[i+FREG_OFFSET] && ! regv[i+FREG_OFFSET]) fcount++; |
103 | 533 } |
137 | 534 printf("# free reg %d freg %d\n",count,fcount); |
535 return d?fcount:count; | |
103 | 536 } |
537 | |
538 int | |
89 | 539 register_full(void) |
540 { | |
541 int i; | |
542 for(i=0;i<MAX_REGISTER;i++) { | |
543 if (! regs[i]) { | |
544 return 0; | |
545 } | |
546 } | |
547 return 1; | |
548 } | |
549 | |
550 void | |
551 free_all_register(void) | |
552 { | |
553 int i; | |
97 | 554 for(i=0;i<MAX_REGISTER;i++) { regs[i]=0; regv[i]=0; } |
145 | 555 for(i=0;i<MAX_FREGISTER;i++) { regs[i+FREG_OFFSET]=0; regv[i+FREG_OFFSET]=0; } |
89 | 556 creg = get_register(); |
138 | 557 freg = get_dregister(1); |
107 | 558 set_creg(CREG_REGISTER,0); |
559 set_freg(FREG_FREGISTER,0); | |
89 | 560 return; |
561 } | |
562 | |
563 void | |
564 register_usage(char *s) | |
565 { | |
141 | 566 #if 0 |
89 | 567 int i; |
126 | 568 #endif |
89 | 569 if (chk) return; |
109 | 570 if (!lsrc) return; |
89 | 571 printf("# %d: %s:",lineno,s); |
126 | 572 printf(" creg=%s fgreg=%s",register_name(creg),fregister_name(freg)); |
141 | 573 #if 0 |
126 | 574 printf("\n# regs:"); |
575 for(i=0;i<MAX_REGISTER;i++) { printf("%d",regv[i]); } | |
101 | 576 printf(":"); |
126 | 577 for(i=0;i<MAX_REGISTER;i++) { printf("%d",regs[i]); } |
101 | 578 printf("\n# freg:"); |
145 | 579 for(i=0;i<MAX_FREGISTER;i++) { printf("%d",regs[i+FREG_OFFSET]); } |
89 | 580 for(i=reg_sp;i>=0;i--) { |
581 if(reg_stack[i]>=0) | |
134 | 582 printf(" %s",register_name(reg_stack[i])); |
89 | 583 } |
584 #endif | |
585 printf("\n"); | |
586 } | |
587 | |
588 void | |
589 gexpr_init(void) | |
590 { | |
591 while(reg_sp > 0) { | |
592 free_register(reg_stack[--reg_sp]); | |
593 } | |
148 | 594 while(freg_sp > 0) { |
595 free_register(freg_stack[--freg_sp]); | |
596 } | |
108 | 597 if (cond_freg!=-1) { |
147 | 598 if(car(cond_freg)==DREGISTER) free_register(cadr(cond_freg)); |
599 else if(car(cond_freg)==FREGISTER) free_register(cadr(cond_freg)); | |
108 | 600 cond_freg=-1; |
601 } | |
602 if (cond_reg!=-1) { | |
603 if(car(cond_reg)==REGISTER) free_register(cadr(cond_reg)); | |
604 cond_reg=-1; | |
605 } | |
89 | 606 text_mode(); |
607 gexpr_code_init(); | |
608 register_usage("gexpr_init"); | |
609 } | |
610 | |
611 | |
612 void | |
613 emit_init(void) | |
614 { | |
615 free_all_register(); | |
110 | 616 max_reg_var=-1; max_freg_var=-1; |
89 | 617 reg_sp = 0; |
123 | 618 freg_sp = 0; |
89 | 619 text_mode(); |
620 } | |
621 | |
622 int | |
623 register_var(int r) { | |
99 | 624 return r; |
89 | 625 } |
626 | |
97 | 627 |
89 | 628 int |
105 | 629 get_register_var(NMTBL *n) |
89 | 630 { |
631 int i; | |
99 | 632 for(i=0;i<REG_VAR_BASE-REG_VAR_MIN;i++) { |
137 | 633 if (! regs[REG_VAR_BASE-i]) { /* 使われていないなら */ |
634 /* そのレジスタを使うことを宣言し */ | |
635 regs[REG_VAR_BASE-i]=USING_REG; | |
636 regv[REG_VAR_BASE-i]=0; | |
97 | 637 if (i>max_reg_var) max_reg_var=i; |
122 | 638 /* その場所を表す番号を返す */ |
639 return list3(REGISTER,REG_VAR_BASE-i,(int)n); | |
137 | 640 } |
96 | 641 } |
104 | 642 return list2(LVAR,new_lvar(size_of_int)); |
96 | 643 } |
644 | |
645 int | |
97 | 646 fregister_var(int r) { |
99 | 647 return r; |
96 | 648 } |
649 | |
650 int | |
138 | 651 get_dregister_var(NMTBL *n,int d) |
96 | 652 { |
653 int i; | |
148 | 654 for(i=0;i<FREG_VAR_BASE-FREG_VAR_MIN;i++) { |
145 | 655 if (! regs[FREG_VAR_BASE-i+FREG_OFFSET]) { /* 使われていないなら */ |
656 regs[FREG_VAR_BASE-i+FREG_OFFSET]=USING_REG; /*そのレジスタを使うことを宣言し*/ | |
657 regv[FREG_VAR_BASE-i+FREG_OFFSET]=0; | |
97 | 658 if (i>max_freg_var) max_freg_var=i; |
122 | 659 /* その場所を表す番号を返す */ |
145 | 660 return list3(DREGISTER,FREG_VAR_BASE-i+FREG_OFFSET,(int)n); |
89 | 661 } |
662 } | |
104 | 663 return list2(LVAR,new_lvar(size_of_double)); |
89 | 664 } |
665 | |
666 void | |
107 | 667 emit_push() |
89 | 668 { |
669 int new_reg; | |
102 | 670 if (reg_sp>MAX_MAX) error(-1); |
89 | 671 new_reg = get_register(); |
97 | 672 reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ |
147 | 673 ireg = creg = new_reg; |
97 | 674 regv[creg]=1; |
89 | 675 } |
676 | |
677 int | |
93 | 678 emit_pop(int type) |
89 | 679 { |
112 | 680 int xreg,reg; |
97 | 681 xreg=pop_register(); |
682 if (xreg<= -REG_LVAR_OFFSET) { | |
112 | 683 reg = get_register(); |
119 | 684 code_rlvar(REG_LVAR_OFFSET+xreg,reg); |
117 | 685 free_lvar(REG_LVAR_OFFSET+xreg); |
112 | 686 xreg = reg; |
147 | 687 regv[creg]=1; |
97 | 688 } |
89 | 689 return xreg; |
690 } | |
691 | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
692 static int code_base; |
89 | 693 |
694 #define MAX_PTR_CACHE 10 | |
695 | |
696 int ptr_cache=0; | |
697 | |
93 | 698 void |
90 | 699 init_ptr_cache() |
700 { | |
701 int i; | |
702 for(i=0;i<MAX_PTR_CACHE;i++) { | |
703 ptr_cache=glist3(0,ptr_cache,0); | |
704 } | |
705 } | |
706 | |
95 | 707 void |
96 | 708 clear_ptr_cache_reg(int r) |
709 { | |
710 int ptcptr=ptr_cache; | |
711 while(ptcptr) { | |
712 if(car(ptcptr)&&caddr(ptcptr)==r) { | |
713 car(ptcptr)=0; | |
714 caddr(ptcptr)=0; | |
102 | 715 free_register(r); |
98 | 716 return; |
96 | 717 } |
718 ptcptr=cadr(ptcptr); | |
719 } | |
720 } | |
721 | |
722 void | |
89 | 723 clear_ptr_cache() |
724 { | |
725 int ptcptr=ptr_cache; | |
726 while(ptcptr) { | |
727 if(car(ptcptr)) | |
728 free_register(caddr(ptcptr)); | |
729 car(ptcptr)=0; | |
730 caddr(ptcptr)=0; | |
731 ptcptr=cadr(ptcptr); | |
732 } | |
733 } | |
734 | |
735 | |
736 int | |
109 | 737 get_ptr_cache(NMTBL *nptr) |
89 | 738 { |
739 int r; | |
740 int ptcptr=ptr_cache; | |
109 | 741 int g = (int)nptr; |
89 | 742 int p,p1; |
95 | 743 char *rrn; |
89 | 744 |
127 | 745 p1 = ptcptr; p = cadr(p1); /* unnecesary, if ptcptr is initialized */ |
89 | 746 while(ptcptr) { |
747 if(car(ptcptr)==g) return caddr(ptcptr); | |
748 p1=p; p=ptcptr; | |
749 ptcptr=cadr(ptcptr); | |
750 } | |
751 cadr(p1)=0; /* remove the last one */ | |
752 cadr(p) = ptr_cache; /* connect current queue to the last one */ | |
753 ptr_cache = p; /* now the last one is the top */ | |
754 if (!caddr(p)) { | |
96 | 755 if((r=get_register())) { |
756 caddr(p)=r; regs[r]=PTRC_REG; | |
127 | 757 } else { |
758 error(-1); | |
96 | 759 r=creg; /* this can't happen */ |
127 | 760 } |
102 | 761 car(p)=g; |
127 | 762 } else { |
763 r = caddr(p); | |
89 | 764 } |
765 rrn = register_name(r); | |
109 | 766 if (nptr->sc==STATIC) { |
767 printf("\taddis %s,r31,ha16(_%s-L_%d)\n", | |
768 rrn,nptr->nm,code_base); | |
769 printf("\tla %s,lo16(_%s-L_%d)(%s)\n", | |
770 rrn,nptr->nm,code_base,rrn); | |
771 } else { | |
772 printf("\taddis %s,r31,ha16(L_%s$non_lazy_ptr-L_%d)\n", | |
773 rrn,nptr->nm,code_base); | |
774 printf("\tlwz %s,lo16(L_%s$non_lazy_ptr-L_%d)(%s)\n", | |
775 rrn,nptr->nm,code_base,rrn); | |
776 } | |
89 | 777 return r; |
778 } | |
779 | |
168 | 780 static char *cload(int sz) { return sz==1?"lbz":sz==size_of_short?"lhz":"lwz"; } |
781 static char *cstore(int sz) { return sz==1?"stb":sz==size_of_short?"sth":"stw"; } | |
782 static char *cext(int sz) { return sz==size_of_short?"extsh":"extsb"; } | |
783 | |
89 | 784 void |
92 | 785 code_label(int labelno) |
786 { | |
787 clear_ptr_cache(); | |
102 | 788 printf("L_%d:\n",labelno); |
92 | 789 } |
790 | |
791 void | |
107 | 792 code_gvar(int e1,int creg) { |
89 | 793 int r; |
109 | 794 r = get_ptr_cache((NMTBL*)cadr(e1)); |
89 | 795 if(r!=creg) |
796 printf("\tmr %s,%s\n",register_name(creg),register_name(r)); | |
107 | 797 regv[creg]=1; |
89 | 798 return; |
799 } | |
800 | |
801 void | |
107 | 802 code_rgvar(int e1,int creg) { |
147 | 803 if (!is_int_reg(creg)) error(-1); |
108 | 804 printf("\tlwz %s,0(%s)\n",register_name(creg), |
109 | 805 register_name(get_ptr_cache((NMTBL*)cadr(e1)))); |
107 | 806 regv[creg]=1; |
89 | 807 } |
808 | |
809 void | |
165 | 810 code_crgvar(int e1,int creg,int sign,int sz){ |
107 | 811 char *crn = register_name(creg); |
168 | 812 printf("\t%s %s,0(%s)\n",cload(sz),crn, |
109 | 813 register_name(get_ptr_cache((NMTBL*)cadr(e1)))); |
162 | 814 if (sign) |
168 | 815 printf("\t%s %s,%s\n",cext(sz),crn,crn); |
107 | 816 regv[creg]=1; |
89 | 817 } |
818 | |
165 | 819 |
820 | |
89 | 821 void |
107 | 822 code_register(int e2,int creg) { |
90 | 823 if (creg!=e2) |
824 printf("\tmr %s,%s\n",register_name(creg),register_name(e2)); | |
107 | 825 regv[creg]=1; |
89 | 826 } |
827 | |
828 | |
829 void | |
95 | 830 code_rlvar(int e2,int reg) { |
119 | 831 lvar_intro(e2); |
147 | 832 if (!is_int_reg(reg)) error(-1); |
119 | 833 printf("\tlwz %s,",register_name(reg)); |
834 lvar(e2); | |
107 | 835 regv[creg]=1; |
89 | 836 } |
837 | |
838 void | |
165 | 839 code_crlvar(int e2,int reg,int sign,int sz) { |
119 | 840 lvar_intro(e2); |
165 | 841 printf("\t%s %s,",cload(sz),register_name(reg)); |
119 | 842 lvar(e2); |
162 | 843 if (sign) |
165 | 844 printf("\t%s %s,%s\n",cext(sz),register_name(reg),register_name(reg)); |
147 | 845 regv[creg]=1; |
89 | 846 } |
847 | |
165 | 848 |
849 | |
160 | 850 |
89 | 851 |
852 void | |
109 | 853 code_fname(NMTBL *n,int creg) { |
89 | 854 int r; |
109 | 855 r = get_ptr_cache(n); |
89 | 856 if(r!=creg) |
95 | 857 printf("\tmr %s,%s\n",register_name(creg),register_name(r)); |
107 | 858 regv[creg]=1; |
89 | 859 return; |
860 } | |
861 | |
862 | |
863 void | |
107 | 864 code_const(int e2,int creg) { |
89 | 865 char *crn = register_name(creg); |
866 if (-32768<e2&&e2<32768) | |
867 printf("\tli %s,%d\n",crn,e2); | |
868 else { | |
869 printf("\tlis %s,ha16(%d)\n",crn,e2); | |
870 printf("\taddi %s,%s,lo16(%d)\n",crn,crn,e2); | |
871 } | |
107 | 872 regv[creg]=1; |
89 | 873 } |
874 | |
875 | |
876 void | |
107 | 877 code_neg(int creg) { |
93 | 878 printf("\tneg %s,%s\n", register_name(creg), register_name(creg)); |
89 | 879 } |
880 | |
881 | |
882 void | |
107 | 883 code_not(int creg) { |
89 | 884 printf("\tnor %s,%s,%s\n", |
885 register_name(creg), register_name(creg),register_name(creg)); | |
886 } | |
887 | |
888 | |
889 void | |
107 | 890 code_lnot(int creg) { |
89 | 891 printf("\tsubfic r0,%s,0\n", register_name(creg)); |
892 printf("\tadde %s,r0,%s\n", register_name(creg),register_name(creg)); | |
893 } | |
894 | |
895 void | |
168 | 896 code_preinc(int e1,int e2,int dir,int sign,int sz,int reg) { |
89 | 897 char *xrn,*drn; |
898 int i,dreg; | |
899 if (car(e2)==REGISTER) { | |
95 | 900 printf("\taddi %s,%s,%d\n", |
168 | 901 register_name(cadr(e2)),register_name(cadr(e2)), dir); |
118 | 902 if (cadr(reg)!=e2) |
903 printf("\tmr %s,%s\n",register_name(cadr(reg)),register_name(e2)); | |
107 | 904 regv[reg]=1; |
89 | 905 return; |
906 } | |
907 g_expr(e2); | |
908 xrn = register_name(creg); | |
909 dreg=get_register(); if (!dreg) error(-1); | |
910 drn = register_name(dreg); | |
168 | 911 printf("\t%s %s,0(%s)\n",cload(sz),drn,xrn); |
172 | 912 if (use && sign && sz!=size_of_int) |
168 | 913 printf("\t%s %s,%s\n",cext(sz),drn,drn); |
914 printf("\taddi %s,%s,%d\n",drn,drn,dir); | |
915 printf("\t%s %s,0(%s)\n",cstore(sz),drn,xrn); | |
89 | 916 i=creg;creg=dreg;dreg=i; |
147 | 917 regv[creg]=1; ireg=creg; |
89 | 918 free_register(dreg); |
919 } | |
920 | |
921 | |
922 void | |
168 | 923 code_postinc(int e1,int e2,int dir,int sign,int sz,int reg) { |
89 | 924 char *xrn,*crn,*nrn; |
95 | 925 int dreg,nreg,i; |
89 | 926 if (car(e2)==REGISTER) { |
107 | 927 printf("\tmr %s,%s\n",register_name(reg),register_name(cadr(e2))); |
89 | 928 printf("\taddi %s,%s,%d\n", |
168 | 929 register_name(cadr(e2)),register_name(cadr(e2)),dir); |
107 | 930 regv[reg]=1; |
89 | 931 return; |
932 } | |
933 g_expr(e2); | |
934 crn = register_name(creg); | |
935 dreg=get_register(); if (!dreg) error(-1); | |
936 xrn = register_name(dreg); | |
937 nreg=get_register(); if (!nreg) error(-1); | |
938 nrn = register_name(nreg); | |
168 | 939 printf("\t%s %s,0(%s)\n",cload(sz),xrn,crn); |
172 | 940 if (use && sign && sz!=size_of_int) |
168 | 941 printf("\t%s %s,%s\n",cext(sz),xrn,xrn); |
942 printf("\taddi %s,%s,%d\n",nrn,xrn,dir); | |
943 printf("\t%s %s,0(%s)\n",cstore(sz),nrn,crn); | |
944 i=creg;creg=dreg;dreg=i; | |
89 | 945 free_register(nreg); |
946 free_register(dreg); | |
147 | 947 regv[creg]=1; ireg=creg; |
89 | 948 } |
949 | |
950 | |
951 void | |
107 | 952 code_return(int creg) { |
95 | 953 char *crn = register_name(creg); |
102 | 954 printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",crn,retcont,code_base); |
955 printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",crn,retcont,code_base,crn); | |
89 | 956 } |
957 | |
128 | 958 #define R1SAVE 1 |
89 | 959 |
960 void | |
107 | 961 code_environment(int creg) { |
126 | 962 /* save frame pointer */ |
128 | 963 #if R1SAVE |
964 printf("\tlwz %s,0(r1)\n",register_name(creg)); | |
965 #else | |
966 int l = 0; | |
967 printf("\tla %s,",register_name(creg)); | |
968 printf("lo16(%d+L_%d)(r30)\n",FUNC_LVAR,lvar_offset_label); | |
969 #endif | |
89 | 970 } |
971 | |
972 void | |
116 | 973 code_bool(int e1) { |
89 | 974 char *xrn; |
975 int e2,e3; | |
976 b_expr(e1,1,e2=fwdlabel(),1); /* including > < ... */ | |
147 | 977 creg = use_int(creg); |
89 | 978 xrn = register_name(creg); |
979 printf("\tli %s,0\n",xrn); | |
980 jmp(e3=fwdlabel()); | |
981 fwddef(e2); | |
982 printf("\tli %s,1\n",xrn); | |
983 fwddef(e3); | |
984 } | |
985 | |
986 char * | |
987 code_gt(int cond) { | |
113 | 988 return (cond?"gt":"le"); |
89 | 989 } |
990 | |
991 char * | |
992 code_ugt(int cond) { | |
113 | 993 return (cond?"gt":"le"); |
89 | 994 } |
995 | |
996 char * | |
997 code_ge(int cond) { | |
113 | 998 return (cond?"ge":"lt"); |
89 | 999 } |
1000 | |
1001 char * | |
1002 code_uge(int cond) { | |
113 | 1003 return (cond?"ge":"lt"); |
89 | 1004 } |
1005 | |
1006 char * | |
1007 code_eq(int cond) { | |
113 | 1008 return (cond?"eq":"ne"); |
89 | 1009 } |
1010 | |
1011 void | |
167 | 1012 code_cmp_crgvar(int e1,int sz) { |
89 | 1013 int r; |
1014 char *crn = register_name(creg); | |
109 | 1015 r = get_ptr_cache((NMTBL*)cadr(e1)); |
167 | 1016 printf("\t%s %s,0(%s)\n",cload(sz),crn,register_name(r)); |
89 | 1017 printf("\tcmpwi cr0,%s,0\n",crn); |
107 | 1018 regv[creg]=0; |
89 | 1019 } |
1020 | |
1021 | |
1022 void | |
167 | 1023 code_cmp_crlvar(int e2,int sz) { |
89 | 1024 char *crn = register_name(creg); |
119 | 1025 lvar_intro(e2); |
167 | 1026 printf("\t%s %s,",cload(sz),crn); |
160 | 1027 lvar(e2); |
1028 code_cmp_register(creg); | |
1029 regv[creg]=0; | |
1030 } | |
1031 | |
89 | 1032 |
1033 void | |
1034 code_cmp_rgvar(int e1) { | |
1035 int r; | |
1036 char *crn = register_name(creg); | |
147 | 1037 if (!is_int_reg(creg)) error(-1); |
109 | 1038 r = get_ptr_cache((NMTBL*)cadr(e1)); |
108 | 1039 printf("\tlwz %s,0(%s)\n",crn,register_name(r)); |
89 | 1040 code_cmp_register(creg); |
107 | 1041 regv[creg]=0; |
89 | 1042 } |
1043 | |
1044 | |
1045 void | |
119 | 1046 code_cmp_rlvar(int e2) { |
89 | 1047 char *crn = register_name(creg); |
119 | 1048 lvar_intro(e2); |
1049 printf("\tlwz %s,",crn); | |
1050 lvar(e2); | |
89 | 1051 code_cmp_register(creg); |
107 | 1052 regv[creg]=0; |
89 | 1053 } |
1054 | |
1055 | |
1056 void | |
1057 code_cmp_register(int e2) { | |
1058 printf("\tcmpwi cr0,%s,0\n",register_name(e2)); | |
1059 } | |
1060 | |
1061 | |
1062 void | |
1063 ascii(char *s) | |
1064 { | |
1065 printf("\t.ascii \""); | |
1066 while(*s) { | |
1067 if (*s=='\n') | |
1068 printf("%cn",92); | |
1069 else if (*s<' ') | |
1070 printf("%c%03o",92,*s); | |
116 | 1071 else if (*s=='\\') |
1072 printf("\\\\"); | |
89 | 1073 else if (*s==34) |
1074 printf("%c%c",92,34); | |
1075 else | |
1076 printf("%c",*s); | |
1077 s++; | |
1078 } | |
108 | 1079 printf("\\0%c\n\t.align 2\n",34); |
89 | 1080 } |
1081 | |
1082 void | |
107 | 1083 code_string(int e1,int creg) |
89 | 1084 { |
95 | 1085 char *s,*crn; |
1086 int lb; | |
1087 crn=register_name(creg); | |
89 | 1088 |
1089 s=(char *)cadr(e1); | |
109 | 1090 printf(".data\t\n.cstring\n\t.align 2\n"); |
89 | 1091 lb=fwdlabel(); |
102 | 1092 printf("L_%d:\n",lb); |
89 | 1093 ascii(s); |
1094 if (output_mode==TEXT_EMIT_MODE) { | |
1095 printf(".text\n"); | |
1096 } else { | |
1097 text_mode(); | |
1098 } | |
102 | 1099 printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",crn,lb,code_base); |
1100 printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",crn,lb,code_base,crn); | |
89 | 1101 } |
1102 | |
1103 #define MAX_COPY_LEN 20 | |
1104 | |
1105 void | |
1106 emit_copy(int from,int to,int length,int offset,int value,int det) | |
1107 { | |
1108 char *frn = register_name(from); | |
1109 char *trn = register_name(to); | |
1110 char *drn; | |
1111 int fix = 0; | |
125 | 1112 char *memmove = "memmove"; |
95 | 1113 int dreg = get_register(); if (!dreg) error(-1); |
89 | 1114 drn = register_name(dreg); |
1115 | |
1116 /* length <0 means upward direction copy */ | |
1117 switch (length) { | |
1118 case 0: break; | |
1119 case 1: case -1: | |
1120 printf("\tlbz %s,%d(%s)\n",drn,offset,frn); | |
1121 printf("\tstb %s,%d(%s)\n",drn,offset,trn); | |
1122 break; | |
1123 case 2: case -2: | |
1124 printf("\tlhz %s,%d(%s)\n",drn,offset,frn); | |
1125 printf("\tsth %s,%d(%s)\n",drn,offset,trn); | |
1126 break; | |
1127 case 4: case -4: | |
1128 printf("\tlwz %s,%d(%s)\n",drn,offset,frn); | |
1129 printf("\tstw %s,%d(%s)\n",drn,offset,trn); | |
1130 break; | |
1131 default: | |
1132 if (-MAX_COPY_LEN<length && length <0) { | |
1133 for(;length<=4;length+=4,offset-=4) | |
1134 emit_copy(from,to,4,offset,0,det); | |
1135 for(;length<=2;length+=2,offset-=2) | |
1136 emit_copy(from,to,2,offset,0,det); | |
1137 if(length>0) | |
1138 emit_copy(from,to,length,offset,0,det); | |
1139 break; | |
1140 } else if (length <=MAX_COPY_LEN) { | |
1141 for(;length>=4;length-=4,offset+=4) | |
1142 emit_copy(from,to,4,offset,0,det); | |
1143 for(;length>=2;length-=2,offset+=2) | |
1144 emit_copy(from,to,2,offset,0,det); | |
1145 if(length>0) | |
1146 emit_copy(from,to,length,offset,0,det); | |
1147 break; | |
1148 } | |
125 | 1149 clear_ptr_cache(); |
1150 code_save_stacks(); | |
1151 printf("\tli r5,%d\n",length); | |
1152 printf("\tmr r4,%s\n",frn); | |
1153 printf("\tmr r3,%s\n",trn); | |
1154 /* overrap must be allowed */ | |
1155 printf("\tbl L_%s$stub\n",memmove); | |
1156 extern_define(memmove,0,FUNCTION,1); | |
1157 fix=0; | |
1158 set_creg(RET_REGISTER,0); | |
1159 if (creg!=to) { | |
1160 free_register(to); to = creg; | |
89 | 1161 } |
125 | 1162 break; |
89 | 1163 } |
1164 if (value) { | |
1165 /* creg must point top of the destination data */ | |
1166 /* this code is necessary for the value of assignment or function call */ | |
1167 /* otherwise we don't need this */ | |
1168 if (fix) printf("\taddi %s,%s,%d\n",trn,trn,fix); | |
1169 if(creg!=to) { | |
1170 free_register(creg); creg=to; | |
1171 } | |
1172 } | |
1173 free_register(dreg); | |
1174 regv[from]=regv[to]=regv[dreg]=0; | |
1175 regv[creg]=1; | |
1176 } | |
1177 | |
1178 int | |
119 | 1179 struct_push(int e4,int t,int arg) |
89 | 1180 { |
95 | 1181 int length,count; |
119 | 1182 int dreg,sreg; char *drn,*crn,*srn; |
89 | 1183 g_expr(e4); |
1184 length=size(t); | |
1185 if(length%size_of_int) { | |
1186 length += size_of_int - (length%size_of_int); | |
1187 } | |
95 | 1188 dreg = get_register(); if (!dreg) error(-1); |
89 | 1189 drn = register_name(dreg); |
119 | 1190 crn = register_name(creg); |
125 | 1191 if (length<MAX_COPY_LEN) { |
1192 sreg = get_register(); if (!sreg) error(-1); | |
1193 srn = register_name(sreg); | |
1194 code_lvar(cadr(arg),sreg); | |
1195 for(count=0;length<MAX_COPY_LEN;count++,length-=size_of_int) { | |
1196 if (length==0) { | |
1197 free_register(sreg); | |
1198 free_register(dreg); | |
1199 return count; | |
1200 } else { | |
1201 printf("\tlwz %s,%d(%s)\n",drn,length-size_of_int,crn); | |
1202 printf("\tstwu %s,%d(%s)\n",drn,-size_of_int,srn); | |
1203 } | |
89 | 1204 } |
1205 } | |
125 | 1206 code_lvar(cadr(arg),dreg); |
89 | 1207 /* downward direction copy */ |
1208 emit_copy(creg,dreg,length,0,0,1); | |
119 | 1209 if (dreg) free_register(dreg); |
89 | 1210 return length/size_of_int; |
1211 } | |
1212 | |
104 | 1213 void |
1214 set_creg(int reg,int mode) | |
1215 { | |
1216 if (reg!=creg) { | |
111 | 1217 clear_ptr_cache_reg(reg); |
147 | 1218 if (reg!=ireg && mode) |
1219 printf("\tmr %s,%s\n",register_name(reg),register_name(ireg)); | |
104 | 1220 free_register(creg); |
147 | 1221 creg = ireg = reg; |
104 | 1222 regs[creg]=1; |
1223 } | |
1224 } | |
1225 | |
1226 void | |
1227 set_freg(int reg,int mode) | |
1228 { | |
147 | 1229 if (reg!=creg) { |
1230 if (reg!=freg && mode) { | |
104 | 1231 printf("\tfmr %s,%s\n",fregister_name(reg),fregister_name(freg)); |
147 | 1232 } |
1233 free_register(creg); | |
1234 creg = freg = reg; | |
145 | 1235 regs[freg]=1; |
104 | 1236 } |
1237 } | |
1238 | |
1239 void | |
1240 use_var(int arg) | |
1241 { | |
1242 if (car(arg)==REGISTER) | |
116 | 1243 regs[cadr(arg)]=USING_REG; |
104 | 1244 else if (car(arg)==DREGISTER) |
145 | 1245 regs[cadr(arg)]=USING_REG; |
104 | 1246 } |
1247 | |
105 | 1248 void |
1249 code_save_input_registers() | |
1250 { | |
119 | 1251 int args; |
1252 NMTBL *n; | |
1253 int reg; | |
1254 int tag; | |
1255 int lvar; | |
1256 int t; | |
1257 /* fnptr->dsp=list4(type,fnptr->dsp,(int)n,0); */ | |
1258 int reg_offset = 0; | |
1259 int offset = 0; | |
1260 | |
1261 for(args = fnptr->dsp;args;args = cadr(args)) { | |
1262 n = (NMTBL *)caddr(args); | |
1263 tag = n->sc; | |
1264 reg = n->dsp; | |
1265 if (!n||n==&null_nptr) error(REG_ERR); | |
1266 if (tag==REGISTER) { | |
1267 /* regs[reg]==INPUT_REG case should be considered */ | |
1268 n->dsp = offset; | |
1269 offset+=size_of_int; | |
1270 t = INT; | |
1271 reg += reg_offset; /* for duplicated floating point argument */ | |
1272 } else if (tag==DREGISTER) { | |
145 | 1273 /* regs[reg]==INPUT_REG case should be considered */ |
119 | 1274 n->dsp = offset; |
120 | 1275 t = n->ty; |
121 | 1276 if(t==FLOAT) { offset+=size_of_float; reg_offset+=1; } |
1277 else if(t==DOUBLE) { offset+=size_of_double; reg_offset+=2; } | |
120 | 1278 else error(-1); |
122 | 1279 } else { |
1280 offset += size(n->ty); | |
119 | 1281 continue; |
122 | 1282 } |
119 | 1283 n->sc = LVAR; |
1284 lvar = list2(LVAR,n->dsp); | |
1285 g_expr_u(assign_expr0(list2(LVAR,n->dsp),list3(tag,reg,(int)n),n->ty,t)); | |
147 | 1286 if (tag==REGISTER||tag==DREGISTER||tag==FREGISTER) { |
119 | 1287 free_register(reg); |
1288 } | |
1289 } | |
121 | 1290 my_func_args = offset; |
105 | 1291 } |
1292 | |
95 | 1293 int |
108 | 1294 simple_args(int e3) |
1295 { | |
1296 return | |
1297 !contains_in_list(e3,FUNCTION) && | |
1298 !contains_in_list(e3,CONV) && | |
125 | 1299 !contains_in_list(e3,RSTRUCT) && |
164 | 1300 !contains_in_list(e3,STASS) |
108 | 1301 ; |
1302 } | |
1303 | |
119 | 1304 int |
1305 caller_arg_offset_v(int arg) | |
1306 { | |
1307 return ARG_LVAR_OFFSET+arg*size_of_int; | |
1308 } | |
111 | 1309 |
108 | 1310 int |
89 | 1311 function(int e1) |
1312 { | |
119 | 1313 int e2,e3,e4,e5,nargs,t,r0,r1; |
104 | 1314 int arg,reg_arg,freg_arg,arg_assign; |
136
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1315 int dots; |
111 | 1316 int reg_arg_list=0,ret_type,special_lvar; |
127 | 1317 NMTBL *fn = 0; |
108 | 1318 int jmp = 0; |
1319 char *jrn; | |
95 | 1320 |
111 | 1321 special_lvar = -1; |
136
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1322 ret_type = cadr(cadddr(e1)); |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1323 if (ret_type==CHAR) ret_type=INT; |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1324 |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1325 /* check argments type is DOTS? */ |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1326 t = caddr(cadddr(e1)); |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1327 if (t==0 || t==DOTS) dots = 1; |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1328 else { |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1329 dots = 0; |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1330 for(t = caddr(cadddr(e1));t;t = cadr(t)) { |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1331 if (car(t)==DOTS) dots = 1; |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1332 } |
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1333 } |
108 | 1334 |
1335 e2 = cadr(e1); | |
1336 if (car(e2) == FNAME) { | |
1337 fn=(NMTBL *)cadr(e2); | |
1338 } else { | |
1339 jmp = get_register_var(0); | |
1340 if (car(jmp)!=REGISTER) error(-1); | |
1341 reg_arg_list = list2(jmp,reg_arg_list); | |
116 | 1342 g_expr(e2); |
1343 code_register(creg,cadr(jmp)); | |
1344 /* g_expr(assign_expr0(jmp,e2,INT,INT)); functions are lvalue */ | |
108 | 1345 } |
1346 | |
99 | 1347 /* now all input register vars are free */ |
108 | 1348 code_save_stacks(); |
107 | 1349 set_creg(CREG_REGISTER,0); |
1350 set_freg(FREG_FREGISTER,0); | |
108 | 1351 |
1352 nargs = reg_arg = freg_arg = arg_assign = 0; | |
105 | 1353 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) { |
89 | 1354 t=caddr(e3); |
111 | 1355 e4 = car(e3); |
89 | 1356 if(scalar(t)) { |
122 | 1357 if (reg_arg>=MAX_INPUT_REGISTER_VAR) { |
119 | 1358 arg = list2(LVAR,caller_arg_offset_v(nargs)); |
116 | 1359 } else if (!simple_args(e3) && cadr(e3)) { |
105 | 1360 arg = get_register_var(0); |
104 | 1361 arg_assign = list2( |
137 | 1362 assign_expr0(get_input_register_var(reg_arg,0,0), |
1363 arg,t,t), | |
104 | 1364 arg_assign); |
96 | 1365 } else { |
126 | 1366 arg = get_input_register_var(reg_arg,0,0); |
89 | 1367 } |
107 | 1368 use_var(arg); /* protect from input register free */ |
99 | 1369 reg_arg_list = list2(arg,reg_arg_list); |
1370 g_expr_u(assign_expr0(arg,e4,t,t)); | |
89 | 1371 nargs ++ ; reg_arg++; |
99 | 1372 continue; |
89 | 1373 } else if (t==DOUBLE||t==FLOAT) { |
137 | 1374 if (reg_arg<MAX_INPUT_REGISTER_VAR) { |
119 | 1375 /* sigh... |
1376 printf requies floating value in integer registers | |
1377 */ | |
137 | 1378 if (dots) { |
1379 if (car(e4)==DRLVAR) { | |
1380 special_lvar = cadr(e4); | |
1381 e5 = list2(LVAR,special_lvar); | |
1382 } else { | |
1383 special_lvar = new_lvar(size_of_double); | |
1384 g_expr(assign_expr0( | |
1385 (e5=list2(LVAR,special_lvar)),e4,DOUBLE,t)); | |
1386 reg_arg_list = list2(e5,reg_arg_list); | |
1387 e4 = list2(DREGISTER,freg); | |
1388 /* freg should not change until XXX */ | |
1389 } | |
1390 r0=get_input_register_var(reg_arg,0,0); | |
1391 r1 = reg_arg+1+MIN_TMP_REG; | |
1392 if (regs[r1]==PTRC_REG) clear_ptr_cache_reg(r1); | |
1393 /* else if (regs[r1]) error(-1); */ | |
1394 r1=get_input_register_var_1(reg_arg+1,0,0); | |
1395 | |
1396 use_var(r0); /* protect from input register free */ | |
1397 use_var(r1); /* protect from input register free */ | |
1398 reg_arg_list = list2(r0,reg_arg_list); | |
1399 reg_arg_list = list2(r1,reg_arg_list); | |
1400 arg_assign = list2( assign_expr0(r0,e5,INT,INT), arg_assign); | |
1401 arg_assign = list2( assign_expr0(r1, | |
1402 list2(LVAR,special_lvar+size_of_int), | |
1403 INT,INT), arg_assign); | |
111 | 1404 } |
1405 reg_arg += 2; | |
1406 } | |
136
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1407 if (dots && freg_arg>=4 && freg_arg<MAX_INPUT_DREGISTER_VAR) { |
119 | 1408 /* oh my god! |
1409 it requies integer register and floating register and | |
1410 stack value. You are crazy. | |
1411 */ | |
1412 arg_assign = list2( | |
1413 assign_expr0(list2(LVAR,caller_arg_offset_v(nargs)), | |
138 | 1414 get_input_dregister_var(freg_arg,0,0,1),t,t), |
119 | 1415 arg_assign); |
1416 } | |
122 | 1417 if (freg_arg>=MAX_INPUT_DREGISTER_VAR) { |
119 | 1418 arg = list2(LVAR,caller_arg_offset_v(nargs)); |
137 | 1419 } else if (!simple_args(e3) && cadr(e3)) { |
138 | 1420 arg = get_dregister_var(0,1); |
104 | 1421 arg_assign = list2( |
138 | 1422 assign_expr0(get_input_dregister_var(freg_arg,0,0,1), |
137 | 1423 arg,t,t), |
104 | 1424 arg_assign); |
99 | 1425 } else { |
138 | 1426 arg = get_input_dregister_var(freg_arg,0,0,1); |
99 | 1427 } |
107 | 1428 use_var(arg); /* protect from input register free */ |
99 | 1429 reg_arg_list = list2(arg,reg_arg_list); |
118 | 1430 g_expr_u(assign_expr0(arg,e4,t,t)); /* XXX */ |
99 | 1431 freg_arg++; |
122 | 1432 nargs += size(t)/size_of_int; |
89 | 1433 continue; |
1434 } else if (car(t)==STRUCT||car(t)==UNION) { | |
119 | 1435 arg = list2(LVAR,caller_arg_offset_v(nargs)); |
1436 nargs += struct_push(e4,t,arg); | |
89 | 1437 continue; |
1438 } else { | |
1439 error(TYERR); | |
1440 } | |
1441 ++nargs; | |
1442 } | |
116 | 1443 if (max_func_args<nargs) max_func_args=nargs; |
104 | 1444 for(;arg_assign;arg_assign=cadr(arg_assign)) { |
1445 g_expr_u(car(arg_assign)); | |
1446 } | |
96 | 1447 clear_ptr_cache(); |
89 | 1448 if (car(e2) == FNAME) { |
108 | 1449 printf("\tbl\tL_%s$stub\n",fn->nm); |
89 | 1450 } else { |
108 | 1451 jrn = register_name(cadr(jmp)); |
89 | 1452 printf("\tmtctr %s\n",jrn); |
1453 printf("\tbctrl\n"); | |
1454 } | |
99 | 1455 for(;reg_arg_list;reg_arg_list=cadr(reg_arg_list)) { |
108 | 1456 arg = car(reg_arg_list); |
147 | 1457 if (car(arg)==REGISTER||car(arg)==DREGISTER||car(arg)==FREGISTER) |
1458 free_register(cadr(arg)); | |
119 | 1459 else if (car(arg)==LVAR&&cadr(arg)<0) free_lvar(cadr(arg)); |
99 | 1460 } |
105 | 1461 if (ret_type==DOUBLE||ret_type==FLOAT) { |
104 | 1462 set_freg(RET_FREGISTER,0); |
145 | 1463 regv[freg]=1; regv[creg]=0; |
105 | 1464 } else if (ret_type==VOID) { |
145 | 1465 regv[freg]=0; regv[creg]=0; |
96 | 1466 } else { |
104 | 1467 set_creg(RET_REGISTER,0); |
145 | 1468 regv[freg]=0; regv[creg]=1; |
96 | 1469 } |
105 | 1470 return ret_type; |
89 | 1471 } |
1472 | |
1473 void | |
1474 code_frame_pointer(int e3) { | |
128 | 1475 #if R1SAVE |
1476 printf("\tmr r1,%s\n",register_name(e3)); | |
1477 #else | |
126 | 1478 printf("\tmr r30,%s\n",register_name(e3)); |
128 | 1479 #endif |
89 | 1480 } |
1481 | |
1482 | |
1483 void | |
1484 code_fix_frame_pointer(int disp_offset) { | |
128 | 1485 int l = 0; |
1486 printf("\tla r30,"); | |
1487 printf("lo16(%d+L_%d)(r30)\n",FUNC_LVAR,lvar_offset_label); | |
89 | 1488 } |
1489 | |
1490 void | |
1491 code_jmp(char *s) { | |
128 | 1492 max_reg_var = REG_VAR_BASE-REG_VAR_MIN; |
1493 max_freg_var = FREG_VAR_BASE-FREG_VAR_MIN; | |
89 | 1494 printf("\tb L_%s$stub\n",s); |
1495 } | |
1496 | |
1497 | |
1498 void | |
1499 code_indirect_jmp(int e2) { | |
128 | 1500 max_reg_var = REG_VAR_BASE-REG_VAR_MIN; |
1501 max_freg_var = FREG_VAR_BASE-FREG_VAR_MIN; | |
89 | 1502 printf("\tmtctr %s\n",register_name(e2)); |
1503 printf("\tbctr\n"); | |
1504 } | |
1505 | |
95 | 1506 int |
89 | 1507 rindirect(int e1) /* *(p +5 ) */ |
1508 { | |
95 | 1509 char *crn; |
119 | 1510 int e2,e3,e4,offset; |
1511 | |
1512 offset=0; | |
89 | 1513 e3 = cadr(e2 = cadr(e1)); |
119 | 1514 if (car(e2)==ADD) { |
1515 e4=caddr(e2); | |
1516 if (car(e4)==CONST) { | |
1517 offset=cadr(e4); | |
1518 e2=e3; | |
1519 } | |
1520 } | |
89 | 1521 g_expr(e2); |
90 | 1522 crn=register_name(creg); |
89 | 1523 switch (car(e1)) { |
1524 case FRINDIRECT: case DRINDIRECT: | |
119 | 1525 printf("\t%s %s,%d(%s)\n",fload(car(e1)==DRINDIRECT), |
1526 fregister_name(freg),offset,crn); | |
90 | 1527 regv[creg]=0; regv[freg]=1; |
147 | 1528 creg = freg; |
95 | 1529 return DOUBLE; |
90 | 1530 case CRINDIRECT: |
119 | 1531 printf("\tlbz %s,%d(%s)\n",crn,offset,crn); |
90 | 1532 printf("\textsb %s,%s\n",crn,crn); |
95 | 1533 return CHAR; |
162 | 1534 case CURINDIRECT: |
1535 printf("\tlbz %s,%d(%s)\n",crn,offset,crn); | |
1536 return UCHAR; | |
165 | 1537 case SRINDIRECT: |
1538 printf("\tlhz %s,%d(%s)\n",crn,offset,crn); | |
1539 printf("\textsh %s,%s\n",crn,crn); | |
1540 return SHORT; | |
1541 case SURINDIRECT: | |
1542 printf("\tlhz %s,%d(%s)\n",crn,offset,crn); | |
1543 return USHORT; | |
90 | 1544 case RINDIRECT: |
119 | 1545 printf("\tlwz %s,%d(%s)\n",crn,offset,crn); |
95 | 1546 return INT; |
89 | 1547 } |
95 | 1548 error(-1); return INT; |
89 | 1549 } |
1550 | |
1551 void | |
103 | 1552 code_assign_gvar(int e2,int creg,int byte) { |
90 | 1553 int r; |
1554 char *crn,*rrn; | |
109 | 1555 r = get_ptr_cache((NMTBL*)cadr(e2)); |
90 | 1556 rrn=register_name(r); |
1557 crn=register_name(creg); | |
165 | 1558 if (byte==1) { |
108 | 1559 printf("\tstb %s,0(%s)\n",crn,rrn); |
165 | 1560 } else if (byte==size_of_short) { |
1561 printf("\tsth %s,0(%s)\n",crn,rrn); | |
90 | 1562 } else { |
108 | 1563 printf("\tstw %s,0(%s)\n",crn,rrn); |
90 | 1564 } |
89 | 1565 } |
1566 | |
1567 void | |
103 | 1568 code_assign_lvar(int e2,int creg,int byte) { |
90 | 1569 char *crn; |
1570 crn=register_name(creg); | |
119 | 1571 lvar_intro(e2); |
165 | 1572 if (byte==1) { |
1573 printf("\tstb %s,",crn); | |
1574 } else if (byte==size_of_short) { | |
1575 printf("\tsth %s,",crn); | |
90 | 1576 } else { |
165 | 1577 printf("\tstw %s,",crn); |
90 | 1578 } |
165 | 1579 lvar(e2); |
89 | 1580 } |
1581 | |
1582 void | |
111 | 1583 code_assign_register(int e2,int byte,int creg) { |
104 | 1584 if (e2!=creg) |
1585 printf("\tmr %s,%s\n",register_name(e2),register_name(creg)); | |
89 | 1586 } |
1587 | |
1588 void | |
118 | 1589 code_assign(int e2,int byte,int creg) { |
95 | 1590 char *drn=register_name(e2); |
1591 char *crn=register_name(creg); | |
1592 | |
165 | 1593 if (byte==1) { |
108 | 1594 printf("\tstb %s,0(%s)\n",crn,drn); |
165 | 1595 } else if (byte==size_of_short) { |
1596 printf("\tsth %s,0(%s)\n",crn,drn); | |
90 | 1597 } else { |
108 | 1598 printf("\tstw %s,0(%s)\n",crn,drn); |
90 | 1599 } |
144 | 1600 regv[creg]=1; |
89 | 1601 } |
1602 | |
1603 | |
1604 void | |
1605 code_register_assop(int e2,int op,int byte) { | |
1606 int reg; | |
1607 int xreg = creg; | |
1608 creg = reg = e2; | |
1609 tosop(op,xreg); | |
1610 creg = xreg; | |
104 | 1611 if (creg!=reg) |
1612 printf("\tmr %s,%s\n",register_name(creg),register_name(reg)); | |
144 | 1613 regv[creg]=1; |
89 | 1614 } |
1615 | |
1616 | |
1617 void | |
162 | 1618 code_assop(int op,int byte,int sign) { |
90 | 1619 char *xrn,*crn,*drn; |
89 | 1620 int xreg; |
90 | 1621 int edx = get_register(); if(!edx) error(-1); |
93 | 1622 xrn = register_name(xreg = emit_pop(0)); /* pop e3 value */ |
89 | 1623 regv[xreg]=regs[xreg]=1; |
113 | 1624 printf("# assop\n\tmr %s,%s\n",register_name(edx),register_name(creg)); |
89 | 1625 regv[edx]=1; |
162 | 1626 ld_indexx(byte,0,edx,sign); |
89 | 1627 tosop(op,xreg); |
90 | 1628 crn = register_name(creg); |
1629 drn = register_name(edx); | |
165 | 1630 if (byte==1) { |
108 | 1631 printf("\tstb %s,0(%s)\n",crn,drn); |
165 | 1632 } else if (byte==size_of_short) { |
1633 printf("\tsth %s,0(%s)\n",crn,drn); | |
90 | 1634 } else { |
108 | 1635 printf("\tstw %s,0(%s)\n",crn,drn); |
90 | 1636 } |
1637 free_register(edx); | |
89 | 1638 emit_pop_free(xreg); |
144 | 1639 regv[creg]=1; |
89 | 1640 } |
1641 | |
1642 | |
1643 void | |
1644 tosop(int op,int oreg) | |
1645 { | |
1646 int dx; | |
95 | 1647 char *orn,*crn,*drn; |
89 | 1648 |
103 | 1649 if(oreg==-1) { |
1650 error(-1); | |
1651 } else if (oreg<= -REG_LVAR_OFFSET) { | |
104 | 1652 dx = get_register(); if (dx<0) error(-1); |
119 | 1653 code_rlvar(oreg+REG_LVAR_OFFSET,dx); |
104 | 1654 oreg = dx; |
103 | 1655 regv[oreg]=1; |
1656 } | |
1657 | |
89 | 1658 switch(op) { |
1659 case LSHIFT: | |
1660 case ULSHIFT: | |
90 | 1661 shift("slw",oreg); |
144 | 1662 regv[creg]=1; |
89 | 1663 return; |
1664 case RSHIFT: | |
90 | 1665 shift("srw",oreg); |
144 | 1666 regv[creg]=1; |
89 | 1667 return; |
1668 case URSHIFT: | |
90 | 1669 shift("sraw",oreg); |
144 | 1670 regv[creg]=1; |
89 | 1671 return; |
1672 } | |
93 | 1673 orn = register_name(oreg); |
89 | 1674 crn = register_name(creg); |
1675 switch(op) { | |
1676 case ADD: | |
114 | 1677 printf("\tadd %s,%s,%s\n",crn,crn,orn); |
89 | 1678 break; |
1679 case SUB: | |
114 | 1680 printf("\tsub %s,%s,%s\n",crn,crn,orn); |
89 | 1681 break; |
112 | 1682 case CMP: |
113 | 1683 printf("\tcmpw cr0,%s,%s\n",crn,orn); |
112 | 1684 break; |
89 | 1685 case BAND: |
114 | 1686 printf("\tand %s,%s,%s\n",crn,crn,orn); |
89 | 1687 break; |
1688 case EOR: | |
114 | 1689 printf("\txor %s,%s,%s\n",crn,crn,orn); |
89 | 1690 break; |
1691 case BOR: | |
114 | 1692 printf("\tor %s,%s,%s\n",crn,crn,orn); |
89 | 1693 break; |
1694 case MUL: | |
116 | 1695 printf("\tmullw %s,%s,%s\n",crn,crn,orn); |
1696 break; | |
89 | 1697 case UMUL: |
114 | 1698 printf("\tmullw %s,%s,%s\n",crn,crn,orn); |
89 | 1699 break; |
1700 case DIV: | |
114 | 1701 printf("\tdivw %s,%s,%s\n",crn,crn,orn); |
90 | 1702 break; |
89 | 1703 case UDIV: |
114 | 1704 printf("\tdivwu %s,%s,%s\n",crn,crn,orn); |
89 | 1705 break; |
1706 case MOD: | |
90 | 1707 dx=get_register(); |
95 | 1708 drn = register_name(dx); |
116 | 1709 printf("\tdivw %s,%s,%s\n",drn,crn,orn); |
1710 printf("\tmullw %s,%s,%s\n",drn,drn,orn); | |
1711 printf("\tsubf %s,%s,%s\n",crn,drn,crn); | |
114 | 1712 free_register(dx); |
90 | 1713 break; |
89 | 1714 case UMOD: |
90 | 1715 dx=get_register(); |
95 | 1716 drn = register_name(dx); |
114 | 1717 printf("\tdivwu %s,%s,%s\n",drn,crn,orn); |
116 | 1718 printf("\tmullw %s,%s,%s\n",drn,drn,orn); |
1719 printf("\tsubf %s,%s,%s\n",crn,drn,crn); | |
114 | 1720 free_register(dx); |
89 | 1721 break; |
112 | 1722 default: |
1723 error(-1); | |
89 | 1724 } |
95 | 1725 if(oreg!=creg) free_register(oreg); |
144 | 1726 regv[creg]=1; |
89 | 1727 } |
1728 | |
1729 | |
1730 void | |
1731 shift(char *op, int reg) | |
1732 { | |
93 | 1733 char *crn = register_name(creg); |
1734 char *rrn = register_name(reg); | |
90 | 1735 printf("\t%s %s,%s,%s\n",op,crn,rrn,crn); |
89 | 1736 } |
1737 | |
1738 void | |
162 | 1739 ld_indexx(int byte, int n, int xreg,int sign) |
89 | 1740 { |
95 | 1741 char *crn = register_name(creg); |
165 | 1742 if (byte==1) { |
95 | 1743 printf("\tlbz %s,%d(%s)\n",register_name(creg),n, |
90 | 1744 register_name(xreg)); |
162 | 1745 if (sign) |
1746 printf("\textsb %s,%s\n",crn,crn); | |
165 | 1747 } else if (byte==size_of_short) { |
1748 printf("\tlhz %s,%d(%s)\n",register_name(creg),n, | |
1749 register_name(xreg)); | |
1750 if (sign) | |
1751 printf("\textsh %s,%s\n",crn,crn); | |
90 | 1752 } else |
95 | 1753 printf("\tlwz %s,%d(%s)\n",register_name(creg),n, |
90 | 1754 register_name(xreg)); |
89 | 1755 } |
1756 | |
95 | 1757 int |
1758 code_csvalue() | |
1759 { | |
1760 return creg; | |
1761 } | |
1762 | |
89 | 1763 void |
95 | 1764 code_cmpdimm(int e, int csreg) |
89 | 1765 { |
1766 /* used in dosiwtch() */ | |
1767 if(chk) return; | |
108 | 1768 printf("\tcmpwi cr0,%s,%d\n",register_name(csreg),e); |
89 | 1769 } |
1770 | |
1771 void | |
1772 code_opening(char *filename) | |
1773 { | |
1774 printf("\t.file \"%s\"\n",filename); | |
108 | 1775 /* printf("\t.version\t\"01.01\"\n"); */ |
89 | 1776 /* printf("gcc2_compiled.:\n"); */ |
1777 printf(".text\n"); | |
1778 } | |
1779 | |
1780 void | |
95 | 1781 rexpr(int e1, int l1, char *s,int t) |
89 | 1782 { |
90 | 1783 g_expr(list3(CMP,cadr(e1),caddr(e1))); |
102 | 1784 printf("\tb%s cr0,L_%d\n",s,l1); |
89 | 1785 } |
1786 | |
1787 | |
1788 void | |
1789 jcond(int l, char cond) | |
1790 { | |
1791 if (chk) return; | |
102 | 1792 printf("\tb%s cr0,L_%d\n",cond?"ne":"eq",l); |
89 | 1793 } |
1794 | |
1795 void | |
1796 jmp(int l) | |
1797 { | |
1798 control=0; | |
1799 if (chk) return; | |
102 | 1800 printf("\tb\tL_%d\n",l); |
89 | 1801 } |
1802 | |
1803 void | |
1804 gen_comment(char *s) | |
1805 { | |
1806 if (chk) return; | |
1807 printf("## %s",s); | |
1808 } | |
1809 | |
1810 void | |
1811 code_enter(char *name) | |
1812 { | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1813 if (output_mode!=TEXT_EMIT_MODE) |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1814 text_mode(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1815 else |
108 | 1816 printf("\t.align 2\n"); |
89 | 1817 if (stmode!=STATIC) |
102 | 1818 printf(".globl _%s\n",name); |
126 | 1819 #ifdef DOT_SIZE |
89 | 1820 printf("\t.type\t%s,@function\n",name); |
126 | 1821 #endif |
101 | 1822 printf("_%s:\n",name); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1823 code_disp_label=fwdlabel(); |
128 | 1824 printf("\tla r1,lo16(L_%d)(r30)\n",code_disp_label); |
126 | 1825 printf("\tbcl 20,31,L_%d\n",code_base = fwdlabel()); |
1826 fwddef(code_base); | |
1827 printf("\tmflr r31\n"); | |
119 | 1828 max_func_args = 0; |
89 | 1829 } |
1830 | |
1831 | |
1832 void | |
1833 code_enter1(int args) | |
1834 { | |
106 | 1835 set_creg(CREG_REGISTER,0); |
1836 set_freg(FREG_FREGISTER,0); | |
89 | 1837 } |
1838 | |
1839 void | |
1840 code_leave(char *name) | |
1841 { | |
128 | 1842 int r1_offsetv; |
89 | 1843 disp&= -size_of_int; |
128 | 1844 r1_offsetv = -disp+max_func_args*size_of_int+code_disp_offset; |
1845 | |
1846 printf(".set L_%d,%d\n",code_disp_label,-r1_offsetv); | |
89 | 1847 local_table(); |
1848 free_all_register(); | |
1849 } | |
1850 | |
1851 void | |
1852 enter(char *name) | |
1853 { | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1854 if (output_mode!=TEXT_EMIT_MODE) |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1855 text_mode(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1856 else |
108 | 1857 printf("\t.align 2\n"); |
89 | 1858 if (stmode!=STATIC) |
101 | 1859 printf(".globl _%s\n",name); |
108 | 1860 /* |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1861 printf("\t.type\t%s,@function\n",name); |
108 | 1862 */ |
101 | 1863 printf("_%s:\n",name); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1864 code_setup=fwdlabel(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1865 printf("\tmflr r0\n"); |
102 | 1866 printf("\tbl L_%d\n",code_setup); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1867 code_base=fwdlabel(); |
111 | 1868 fwddef(code_base); |
116 | 1869 r1_offset_label = fwdlabel(); |
1870 lvar_offset_label = fwdlabel(); | |
1871 printf("\tstwu r1,lo16(-L_%d)(r1)\n",r1_offset_label); | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1872 printf("\tmr r30,r1\n"); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1873 printf("\tmflr r31\n"); |
116 | 1874 max_func_args = 0; |
89 | 1875 } |
1876 | |
1877 void | |
1878 enter1() | |
1879 { | |
106 | 1880 set_creg(CREG_REGISTER,0); |
1881 set_freg(FREG_FREGISTER,0); | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1882 } |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1883 |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1884 int |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1885 reg_save_offset() |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1886 { |
108 | 1887 return -( |
116 | 1888 (REAL_MAX_REGISTER-(REG_VAR_BASE-max_reg_var))*size_of_int+ |
108 | 1889 (REAL_MAX_FREGISTER-(FREG_VAR_BASE-max_freg_var))*size_of_double |
1890 ); | |
89 | 1891 } |
1892 | |
1893 void | |
1894 leave(int control, char *name) | |
1895 { | |
130 | 1896 int retcont1=0,sz; |
128 | 1897 |
1898 if (max_freg_var>=0 && max_freg_var<=3) max_freg_var=3; | |
1899 reg_save = reg_save_offset(); | |
111 | 1900 |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1901 if (control) { |
108 | 1902 code_set_return_register(1); |
89 | 1903 } |
126 | 1904 if (retcont) { |
1905 if (control) jmp(retlabel); | |
128 | 1906 retcont1 = fwdlabel(); |
126 | 1907 fwddef(retcont); |
1908 if (cadr(fnptr->ty)==FLOAT||cadr(fnptr->ty)==DOUBLE) { | |
1909 printf("\tfmr f1,f31\n"); | |
128 | 1910 } else if (cadr(fnptr->ty)>0&&( |
1911 car(cadr(fnptr->ty))==STRUCT || | |
1912 car(cadr(fnptr->ty))==UNION)) { | |
1913 sz = size(cadr(fnptr->ty)); | |
1914 printf("\tli r7,%d\n",sz); | |
1915 printf("\tsubl r6,r7,r30\n"); | |
1916 printf("\tlwz r3,lo16(%d)(r30)\n",(my_func_args-1)*size_of_int); | |
1917 emit_copy(6,3,sz,0,1,1); | |
126 | 1918 } else if (cadr(fnptr->ty)!=VOID) { |
1919 printf("\tmr r3,r29\n"); | |
1920 } | |
128 | 1921 #if !R1SAVE |
1922 printf("\tla r1,lo16(%d)(r30)\n", | |
1923 -reg_save+my_func_args*size_of_int); | |
1924 #endif | |
1925 printf("\tb L_%d\n",retcont1); | |
126 | 1926 } |
89 | 1927 fwddef(retlabel); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1928 printf("\tlwz r1,0(r1)\n"); |
128 | 1929 if (retcont) { |
1930 fwddef(retcont1); | |
1931 } | |
110 | 1932 if (max_freg_var>=0) { |
1933 printf("\tlmw r%d,%d(r1)\n", | |
111 | 1934 REG_VAR_BASE-max_reg_var,reg_save); |
1935 freg_save = 72-(REAL_MAX_FREGISTER-(FREG_VAR_BASE-max_freg_var))*4; | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1936 printf("\tb restFP+%d ; restore f%d-f31\n", |
111 | 1937 freg_save, |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1938 FREG_VAR_BASE-max_freg_var); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1939 } else { |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1940 printf("\tlwz r0,8(r1)\n"); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1941 printf("\tmtlr r0\n"); |
110 | 1942 printf("\tlmw r%d,%d(r1)\n", |
116 | 1943 REG_VAR_BASE-max_reg_var,reg_save); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1944 printf("\tblr\n"); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1945 } |
89 | 1946 |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1947 disp &= -size_of_int; |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1948 fwddef(code_setup); |
110 | 1949 printf("\tstmw r%d,%d(r1)\n", |
116 | 1950 REG_VAR_BASE-max_reg_var,reg_save); |
110 | 1951 printf("\tstw r0,8(r1)\n"); |
1952 if (max_freg_var>=0) | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1953 printf("\tb saveFP+%d ; save f%d-f31\n", |
111 | 1954 freg_save, |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1955 FREG_VAR_BASE-max_freg_var); |
110 | 1956 else { |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1957 printf("\tblr\n"); |
110 | 1958 } |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1959 |
116 | 1960 code_offset_set(); |
89 | 1961 local_table(); |
1962 labelno++; | |
1963 free_all_register(); | |
1964 } | |
1965 | |
1966 | |
1967 void | |
107 | 1968 code_set_return_register(int mode) { |
111 | 1969 if (cadr(fnptr->ty)==DOUBLE||cadr(fnptr->ty)==FLOAT) { |
107 | 1970 set_freg(RET_FREGISTER,mode); |
147 | 1971 } else if (cadr(fnptr->ty)==VOID) { |
103 | 1972 } else { |
107 | 1973 set_creg(RET_REGISTER,mode); |
103 | 1974 } |
89 | 1975 } |
1976 | |
1977 void | |
108 | 1978 code_set_fixed_creg(int mode,int type) { |
1979 if (type==FLOAT||type==DOUBLE) { | |
1980 if (cond_freg== -1) { | |
138 | 1981 cond_freg = get_dregister_var(0,1); |
108 | 1982 if(car(cond_freg)!=DREGISTER) error(-1); |
1983 } | |
1984 set_freg(cadr(cond_freg),mode); | |
1985 } else { | |
1986 if (cond_reg== -1) { | |
1987 cond_reg = get_register_var(0); | |
1988 if(car(cond_reg)!=REGISTER) error(-1); | |
1989 } | |
1990 set_creg(cadr(cond_reg),mode); | |
1991 } | |
107 | 1992 } |
1993 | |
1994 void | |
89 | 1995 gen_gdecl(char *n, int gpc) |
1996 { | |
102 | 1997 /* |
89 | 1998 if (stmode!=STATIC) |
99 | 1999 printf(".globl _%s\n",n); |
102 | 2000 */ |
89 | 2001 } |
2002 | |
2003 void | |
2004 align(int t) | |
2005 { | |
2006 if (t!=CHAR) { | |
2007 if (data_alignment & 1) | |
2008 printf("\t.align 2\n"); | |
2009 data_alignment = 0; | |
2010 } | |
2011 } | |
2012 | |
2013 void | |
2014 emit_data(int e, int t, NMTBL *n) | |
2015 { | |
2016 int l; | |
2017 double d; | |
2018 float f; | |
2019 char *name; | |
2020 name = n->nm; | |
156 | 2021 if(mode!=GDECL && mode!=STADECL) { |
89 | 2022 error(-1); return; |
2023 } | |
2024 if (chk) return; | |
2025 if (n->dsp != -1) { | |
2026 n->dsp = -1; /* initiallized flag */ | |
102 | 2027 printf(".globl\t_%s\n",name); |
89 | 2028 data_mode(name); |
2029 align(t); | |
102 | 2030 printf("_%s:\n",name); |
89 | 2031 } else { |
2032 data_mode(0); | |
2033 } | |
2034 if(car(e)==CONST) { | |
168 | 2035 if (t==CHAR||t==UCHAR) { |
89 | 2036 printf("\t.byte %d\n",cadr(e)); |
2037 if (data_alignment>0) | |
2038 data_alignment++; | |
2039 gpc += 1; | |
168 | 2040 } else if (t==SHORT||t==USHORT) { |
2041 printf("\t.short %d\n",cadr(e)); | |
89 | 2042 if (data_alignment>0) data_alignment++; |
168 | 2043 gpc += size_of_short; |
89 | 2044 } else { |
2045 printf("\t.long %d\n",cadr(e)); | |
2046 gpc += size_of_int; | |
2047 } | |
2048 } else if(t==DOUBLE) { | |
2049 d = dcadr(e); | |
99 | 2050 printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); |
89 | 2051 } else if(t==FLOAT) { |
2052 f = dcadr(e); | |
2053 printf("\t.long\t0x%x\n",*(int *)&f); | |
2054 } else if(t!=CHAR) { | |
2055 gpc += size_of_int; | |
2056 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { | |
116 | 2057 printf("\t.long _%s\n",((NMTBL *)cadr(cadr(e)))->nm); |
89 | 2058 } else if(car(e)==FNAME) { |
116 | 2059 printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); |
109 | 2060 } else if(car(e)==GVAR) { |
116 | 2061 printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); |
89 | 2062 } else if(car(e)==STRING) { |
2063 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { | |
2064 l = fwdlabel(); | |
102 | 2065 printf("\t.long L_%d\n",l); |
108 | 2066 printf(".cstring\n\t.align 2\n"); |
102 | 2067 printf("L_%d:\n",l); |
89 | 2068 output_mode = RODATA_EMIT_MODE; |
2069 } | |
2070 ascii((char *)cadr(e)); | |
2071 } else error(TYERR); | |
168 | 2072 } else error(TYERR); |
89 | 2073 } |
2074 | |
2075 void | |
2076 emit_data_closing(NMTBL *n) | |
2077 { | |
126 | 2078 #ifdef DOT_SIZE |
89 | 2079 int lb; |
126 | 2080 #endif |
89 | 2081 if (chk) return; |
2082 if (mode==GDECL) { | |
2083 data_mode(0); | |
126 | 2084 #ifdef DOT_SIZE |
89 | 2085 lb=fwdlabel(); |
102 | 2086 printf("L_%d:\n",lb); |
2087 printf("\t.size\t%s,L_%d-%s\n",n->nm,lb,n->nm); | |
126 | 2088 #endif |
89 | 2089 } |
2090 } | |
2091 | |
2092 void | |
2093 global_table(void) | |
2094 { | |
2095 NMTBL *n; | |
98 | 2096 int init; char *extrn; |
89 | 2097 init=0; |
2098 for(n=ntable;n < &ntable[GSYMS];n++) { | |
109 | 2099 if ((n->sc == GVAR) && n->dsp != -1) { |
89 | 2100 /* n->dsp = -1 means initialized global */ |
2101 if (init==0) { | |
2102 data_mode(0); | |
2103 init=1; | |
2104 } | |
97 | 2105 printf(".comm _%s,%d\n",n->nm,size(n->ty)); |
109 | 2106 } else if ((n->sc==STATIC) && n->dsp != -1) { |
2107 /* n->dsp = -1 means initialized global */ | |
2108 if (init==0) { | |
2109 data_mode(0); | |
2110 init=1; | |
2111 } | |
2112 printf(".lcomm _%s,%d\n",n->nm,size(n->ty)); | |
97 | 2113 } |
2114 } | |
2115 for(n=ntable;n < &ntable[GSYMS];n++) { | |
103 | 2116 if (is_code(n)||is_function(n)) { |
97 | 2117 extrn = n->nm; |
103 | 2118 if (n->sc==EXTRN1) { |
2119 data_mode(0); | |
2120 printf(".picsymbol_stub\n"); | |
97 | 2121 printf("L_%s$stub:\n",extrn); |
2122 printf("\t.indirect_symbol _%s\n",extrn); | |
2123 printf("\tmflr r0\n"); | |
2124 printf("\tbcl 20,31,L0$_%s\n",extrn); | |
2125 printf("L0$_%s:\n",extrn); | |
2126 printf("\tmflr r11\n"); | |
2127 printf("\taddis r11,r11,ha16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); | |
2128 printf("\tmtlr r0\n"); | |
2129 printf("\tlwz r12,lo16(L_%s$lazy_ptr-L0$_%s)(r11)\n",extrn,extrn); | |
2130 printf("\tmtctr r12\n"); | |
2131 printf("\taddi r11,r11,lo16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); | |
2132 printf("\tbctr\n"); | |
2133 printf(".data\n"); | |
2134 printf(".lazy_symbol_pointer\n"); | |
2135 printf("L_%s$lazy_ptr:\n",extrn); | |
2136 printf("\t.indirect_symbol _%s\n",extrn); | |
2137 printf("\t.long dyld_stub_binding_helper\n"); | |
103 | 2138 } else if (n->sc==FUNCTION||n->sc==CODE) { |
2139 text_mode(); | |
2140 printf("\t.set L_%s$stub,_%s\n",extrn,extrn); | |
109 | 2141 data_mode(0); |
111 | 2142 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",extrn,extrn); |
108 | 2143 } |
97 | 2144 } |
2145 } | |
109 | 2146 init=0; |
2147 for(n=ntable;n < &ntable[GSYMS];n++) { | |
2148 if (n->sc == GVAR) { | |
2149 if (init==0) { | |
119 | 2150 printf(".data\n"); |
109 | 2151 init=1; |
2152 } | |
116 | 2153 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",n->nm,n->nm); |
109 | 2154 } |
2155 } | |
97 | 2156 init = 0; |
2157 for(n=ntable;n < &ntable[GSYMS];n++) { | |
116 | 2158 if (is_code(n)||is_function(n)) continue; |
109 | 2159 if (n->sc==EXTRN1) { |
97 | 2160 if(init==0) { |
119 | 2161 printf(".data\n"); |
97 | 2162 printf(".non_lazy_symbol_pointer\n"); |
2163 init=1; | |
2164 } | |
2165 printf("L_%s$non_lazy_ptr:\n",n->nm); | |
2166 printf("\t.indirect_symbol _%s\n",n->nm); | |
2167 printf("\t.long\t0\n"); | |
89 | 2168 } |
2169 } | |
2170 } | |
2171 | |
2172 void | |
2173 local_table(void) | |
2174 { | |
2175 NMTBL *n; | |
2176 int init; | |
2177 init=0; | |
2178 /* static local variables */ | |
2179 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { | |
2180 if (n->sc == GVAR) { | |
2181 if (init==0) { | |
2182 data_mode(0); | |
2183 init=1; | |
2184 } | |
156 | 2185 if (n->dsp != -1) /* initialized static */ |
2186 printf(".lcomm _%s,%d\n",n->nm,size(n->ty)); | |
2187 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",n->nm,n->nm); | |
89 | 2188 } |
2189 } | |
2190 } | |
2191 | |
2192 void | |
2193 text_mode(void) | |
2194 { | |
2195 if (output_mode!=TEXT_EMIT_MODE) { | |
2196 printf(".text\n"); | |
2197 printf("\t.align 2\n"); | |
2198 output_mode = TEXT_EMIT_MODE; | |
2199 } | |
2200 } | |
2201 | |
2202 void | |
2203 data_mode(char *name) | |
2204 { | |
2205 if (output_mode!=DATA_EMIT_MODE) { | |
2206 printf(".data\n"); | |
2207 output_mode = DATA_EMIT_MODE; | |
2208 } | |
108 | 2209 /* |
89 | 2210 if (name) |
2211 printf("\t.type\t%s,@object\n",name); | |
108 | 2212 */ |
89 | 2213 } |
2214 | |
2215 /* floating point */ | |
2216 | |
96 | 2217 static int float_one_lib_used=0; |
2218 static char *float_one_lib[] = { | |
2219 ".data", | |
108 | 2220 /* ".literal8", */ |
96 | 2221 " .align 3", |
2222 "__float_one:", | |
2223 " .long 1065353216", | |
2224 ".text", | |
109 | 2225 /* ".set L__float_one$non_lazy_ptr,__float_one", */ |
96 | 2226 0 |
2227 }; | |
97 | 2228 static int float_zero_lib_used=0; |
2229 static char *float_zero_lib[] = { | |
2230 ".data", | |
108 | 2231 /* ".literal8", */ |
97 | 2232 " .align 3", |
2233 "__float_zero:", | |
2234 " .long 0", | |
2235 ".text", | |
109 | 2236 /* ".set L__float_zero$non_lazy_ptr,__float_zero", */ |
97 | 2237 0 |
2238 }; | |
2239 | |
96 | 2240 |
89 | 2241 |
2242 char * | |
2243 fstore(int d) | |
2244 { | |
90 | 2245 return (d?"stfd":"stfs"); |
89 | 2246 } |
2247 | |
2248 char * | |
2249 fload(int d) | |
2250 { | |
90 | 2251 return d?"lfd":"lfs"; |
89 | 2252 } |
2253 | |
95 | 2254 void |
138 | 2255 code_cmp_dregister(int e2,int d) |
95 | 2256 { |
99 | 2257 char *frn,*rrn,*grn; |
2258 int greg,r; | |
138 | 2259 grn = register_name(greg = get_dregister(d)); |
132 | 2260 frn = register_name(e2); |
99 | 2261 float_zero_lib_used=1; |
109 | 2262 r = get_ptr_cache(&float_zero); |
99 | 2263 rrn = register_name(r); |
108 | 2264 printf("\tlfs %s,0(%s)\n",grn,rrn); |
99 | 2265 printf("\tfcmpu cr0,%s,%s\n",grn,frn); |
147 | 2266 free_register(greg); |
99 | 2267 return; |
95 | 2268 } |
2269 | |
2270 void | |
138 | 2271 code_dregister(int e2,int freg,int d) |
95 | 2272 { |
147 | 2273 if (freg!=e2) { |
2274 if (is_int_reg(e2)) error(-1); | |
108 | 2275 printf("\tfmr %s,%s\n",fregister_name(freg),fregister_name(e2)); |
147 | 2276 } |
145 | 2277 regv[freg]=1; |
95 | 2278 } |
2279 | |
103 | 2280 void code_dassign_gvar(int e2,int freg,int d) |
89 | 2281 { |
90 | 2282 int r; |
109 | 2283 r = get_ptr_cache((NMTBL*)cadr(e2)); |
147 | 2284 if (!is_float_reg(freg)) error(-1); |
108 | 2285 printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(r)); |
145 | 2286 regv[freg]=1; |
89 | 2287 } |
2288 | |
103 | 2289 void code_dassign_lvar(int e2,int freg,int d) |
89 | 2290 { |
119 | 2291 lvar_intro(e2); |
147 | 2292 if (!is_float_reg(freg)) error(-1); |
119 | 2293 printf("\t%s %s,",fstore(d),fregister_name(freg)); |
2294 lvar(e2); | |
145 | 2295 regv[freg]=1; |
89 | 2296 } |
2297 | |
103 | 2298 void code_dassign(int e2,int freg,int d) |
89 | 2299 { |
147 | 2300 if (!is_float_reg(freg)) error(-1); |
108 | 2301 printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(e2)); |
145 | 2302 regv[freg]=1; |
89 | 2303 } |
2304 | |
111 | 2305 void |
138 | 2306 code_dassign_dregister(int e2,int d,int freg) { |
147 | 2307 if (e2!=freg) { |
2308 if (is_int_reg(freg)) error(-1); | |
111 | 2309 printf("\tfmr %s,%s\n",fregister_name(e2),fregister_name(freg)); |
147 | 2310 } |
111 | 2311 } |
2312 | |
89 | 2313 static double d0 = 1.0; |
2314 | |
2315 int | |
2316 code_d1(double d) | |
2317 { | |
2318 int *i = (int *)&d0; int *j = (int *)&d; | |
99 | 2319 return (i[1] == 0x3ff00000)?j[0]:j[1]; |
89 | 2320 } |
2321 | |
2322 int | |
2323 code_d2(double d) | |
2324 { | |
2325 int *i = (int *)&d0; int *j = (int *)&d; | |
99 | 2326 return (i[1] == 0x3ff00000)?j[1]:j[0]; |
89 | 2327 } |
2328 | |
133 | 2329 int |
2330 code_f(double d) | |
2331 { | |
2332 float f = d; | |
2333 int *j = (int *)&f; | |
2334 return *j; | |
2335 } | |
2336 | |
138 | 2337 void |
2338 code_dconst(int e2,int freg,int d) | |
89 | 2339 { |
2340 int lb; | |
138 | 2341 double value = dcadr(e2); |
90 | 2342 int r; |
97 | 2343 char *rrn,*frn; |
2344 frn = fregister_name(freg); | |
138 | 2345 if (value==0.0) { |
97 | 2346 float_zero_lib_used=1; |
109 | 2347 r = get_ptr_cache(&float_zero); |
98 | 2348 rrn = register_name(r); |
108 | 2349 printf("\tlfs %s,0(%s)\n",frn,rrn); |
97 | 2350 return; |
89 | 2351 } |
138 | 2352 if (value==1.0) { |
97 | 2353 float_one_lib_used=1; |
109 | 2354 r = get_ptr_cache(&float_one); |
98 | 2355 rrn = register_name(r); |
108 | 2356 printf("\tlfs %s,0(%s)\n",frn,rrn); |
97 | 2357 return; |
89 | 2358 } |
97 | 2359 rrn = register_name((r=get_register())); |
108 | 2360 printf(" \t.data\n\t.align 3\n"); |
89 | 2361 lb=fwdlabel(); |
102 | 2362 printf("L_%d:\n",lb); |
138 | 2363 if (d) { |
2364 printf("\t.long\t0x%x,0x%x\n",code_d2(value),code_d1(value)); | |
133 | 2365 } else { |
138 | 2366 printf("\t.long\t0x%x\n",code_f(value)); |
133 | 2367 } |
89 | 2368 if (output_mode==TEXT_EMIT_MODE) { |
2369 printf(".text\n"); | |
2370 } else { | |
2371 text_mode(); | |
2372 } | |
102 | 2373 printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",rrn,lb,code_base); |
2374 printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",rrn,lb,code_base,rrn); | |
138 | 2375 if (d) { |
133 | 2376 printf("\tlfd %s,0(%s)\n",frn,rrn); |
2377 } else { | |
2378 printf("\tlfs %s,0(%s)\n",frn,rrn); | |
2379 } | |
90 | 2380 free_register(r); |
145 | 2381 regv[freg]=1; |
89 | 2382 } |
2383 | |
133 | 2384 |
138 | 2385 void code_dneg(int freg,int d) |
89 | 2386 { |
97 | 2387 char *frn = fregister_name(freg); |
147 | 2388 if (is_int_reg(freg)) error(-1); |
95 | 2389 printf("\tfneg %s,%s\n",frn,frn); |
89 | 2390 } |
2391 | |
147 | 2392 void code_d2i(int freg0) |
89 | 2393 { |
147 | 2394 char *frn; |
2395 char *crn; | |
119 | 2396 int e2 = new_lvar(size_of_double); |
147 | 2397 |
2398 freg0 = use_double(freg0); | |
2399 frn = fregister_name(freg0); | |
2400 creg = use_int(creg); | |
2401 crn = register_name(creg); | |
2402 | |
2403 freg = freg0; | |
119 | 2404 free_lvar(e2); |
93 | 2405 printf("\tfctiwz %s,%s\n",frn,frn); |
119 | 2406 lvar_intro(e2); |
2407 printf("\tstfd %s,",frn); lvar(e2); | |
2408 lvar_intro(e2+size_of_double-size_of_int); | |
2409 printf("\tlwz %s,",crn); lvar(e2+size_of_double-size_of_int); | |
147 | 2410 regv[creg]=1; |
89 | 2411 } |
2412 | |
91 | 2413 static int i2d_lib_used=0; |
2414 static char *i2d_lib[] = { | |
2415 ".data", | |
108 | 2416 /* ".literal8", */ |
91 | 2417 " .align 3", |
2418 "__i2dLC0:", | |
2419 " .long 1127219200", | |
2420 " .long -2147483648", | |
2421 ".text", | |
2422 " .align 2", | |
111 | 2423 "i2d_:", |
91 | 2424 " mflr r0", |
2425 " bcl 20,31,__i2dL1$pb", | |
2426 "__i2dL1$pb:", | |
2427 " mflr r10", | |
2428 " mtlr r0", | |
2429 " xoris r3,r3,0x8000", | |
116 | 2430 " stw r3,-28(r30)", |
91 | 2431 " lis r0,0x4330", |
116 | 2432 " stw r0,-32(r30)", |
2433 " lfd f0,-32(r30)", | |
91 | 2434 " addis r9,r10,ha16(__i2dLC0-__i2dL1$pb)", |
2435 " lfd f1,lo16(__i2dLC0-__i2dL1$pb)(r9)", | |
2436 " fsub f1,f0,f1", | |
2437 " blr", | |
2438 0 | |
2439 }; | |
2440 | |
147 | 2441 void code_i2d(int creg0) |
89 | 2442 { |
95 | 2443 i2d_lib_used = 1; |
113 | 2444 clear_ptr_cache(); |
2445 code_save_stacks(); | |
104 | 2446 set_creg(RET_REGISTER,1); |
91 | 2447 printf("\tbl i2d_\n"); |
104 | 2448 set_freg(RET_FREGISTER,0); |
147 | 2449 regv[freg]=1; |
89 | 2450 } |
2451 | |
91 | 2452 static int d2u_lib_used=0; |
2453 static char *d2u_lib[] = { | |
108 | 2454 /* ".literal8", */ |
91 | 2455 " .align 3", |
2456 "__d2uLC0:", | |
2457 " .long 1105199104", | |
2458 " .long 0", | |
2459 ".text", | |
2460 " .align 2", | |
111 | 2461 "d2u_:", |
91 | 2462 " mflr r0", |
2463 " bcl 20,31,__d2uL1$pb", | |
2464 "__d2uL1$pb:", | |
2465 " mflr r10", | |
2466 " mtlr r0", | |
2467 " addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)", | |
2468 " lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)", | |
2469 " fcmpu cr0,f1,f0", | |
2470 " cror 2,1,2", | |
2471 " beq- cr0,__d2uL2", | |
2472 " fctiwz f0,f1", | |
116 | 2473 " stfd f0,-32(r30)", |
2474 " lwz r3,-28(r30)", | |
91 | 2475 " blr", |
2476 "__d2uL2:", | |
2477 " addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)", | |
2478 " lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)", | |
2479 " fsub f0,f1,f0", | |
2480 " fctiwz f0,f0", | |
116 | 2481 " stfd f0,-24(r30)", |
2482 " lwz r3,-20(r30)", | |
91 | 2483 " xoris r3,r3,0x8000", |
2484 " blr", | |
2485 0 | |
2486 }; | |
2487 | |
147 | 2488 void code_d2u(int freg0) |
89 | 2489 { |
113 | 2490 code_save_stacks(); |
2491 clear_ptr_cache(); | |
91 | 2492 d2u_lib_used=1; |
104 | 2493 set_freg(RET_FREGISTER,1); |
111 | 2494 printf("\tbl d2u_\n"); |
104 | 2495 set_creg(RET_REGISTER,0); |
147 | 2496 regv[freg]=1; |
91 | 2497 } |
90 | 2498 |
91 | 2499 static int u2d_lib_used=0; |
2500 static char *u2d_lib[] = { | |
2501 ".data", | |
108 | 2502 /* ".literal8", */ |
91 | 2503 " .align 3", |
2504 "__u2dLC1:", | |
2505 " .long 1127219200", | |
2506 " .long 0", | |
2507 ".text", | |
2508 " .align 2", | |
111 | 2509 "u2d_:", |
91 | 2510 " mflr r0", |
2511 " bcl 20,31,__u2dL2$pb", | |
2512 "__u2dL2$pb:", | |
2513 " mflr r10", | |
2514 " mtlr r0", | |
116 | 2515 " stw r3,-28(r30)", |
91 | 2516 " lis r0,0x4330", |
116 | 2517 " stw r0,-32(r30)", |
2518 " lfd f0,-32(r30)", | |
91 | 2519 " addis r9,r10,ha16(__u2dLC1-__u2dL2$pb)", |
2520 " lfd f1,lo16(__u2dLC1-__u2dL2$pb)(r9)", | |
2521 " fsub f1,f0,f1", | |
2522 " blr", | |
2523 0 | |
2524 }; | |
89 | 2525 |
147 | 2526 void code_u2d(int creg0) |
89 | 2527 { |
113 | 2528 code_save_stacks(); |
2529 clear_ptr_cache(); | |
147 | 2530 u2d_lib_used = 1; |
2531 set_creg(RET_REGISTER,1); | |
111 | 2532 printf("\tbl u2d_\n"); |
147 | 2533 set_freg(FREG_FREGISTER,0); |
2534 regv[freg]=1; | |
89 | 2535 } |
2536 | |
133 | 2537 void code_d2f(int freg) { } |
2538 void code_f2d(int freg) { } | |
147 | 2539 void code_f2i(int freg) { code_d2i(freg); } |
2540 void code_f2u(int freg) { code_d2u(freg); } | |
2541 void code_i2f(int creg) { code_i2d(creg); } | |
2542 void code_u2f(int creg) { code_u2d(creg); } | |
133 | 2543 |
107 | 2544 void code_drgvar(int e2,int d,int freg) |
89 | 2545 { |
91 | 2546 int r; |
109 | 2547 r = get_ptr_cache((NMTBL*)cadr(e2)); |
108 | 2548 printf("\t%s %s,0(%s)\n",fload(d),fregister_name(freg),register_name(r)); |
145 | 2549 regv[freg]=1; |
89 | 2550 } |
2551 | |
2552 | |
107 | 2553 void code_drlvar(int e2,int d,int freg) |
89 | 2554 { |
119 | 2555 lvar_intro(e2); |
2556 printf("\t%s %s,",fload(d),fregister_name(freg)); lvar(e2); | |
145 | 2557 regv[freg]=1; |
89 | 2558 } |
2559 | |
138 | 2560 void code_cmp_drgvar(int e2,int d) |
89 | 2561 { |
91 | 2562 int r; |
97 | 2563 char *frn=fregister_name(freg); |
138 | 2564 int g=get_dregister(d); |
97 | 2565 char *grn=fregister_name(g); |
109 | 2566 r = get_ptr_cache((NMTBL*)cadr(e2)); |
108 | 2567 printf("\t%s %s,0(%s)\n",fload(1),grn,register_name(r)); |
91 | 2568 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2569 free_register(g); |
145 | 2570 regv[freg]=0; |
89 | 2571 } |
2572 | |
138 | 2573 void code_cmp_drlvar(int e2,int d) |
89 | 2574 { |
97 | 2575 char *frn=fregister_name(freg); |
138 | 2576 int g=get_dregister(d); |
97 | 2577 char *grn=fregister_name(g); |
108 | 2578 |
119 | 2579 lvar_intro(e2); |
2580 printf("\t%s %s,",fload(1),grn); lvar(e2); | |
91 | 2581 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2582 free_register(g); |
145 | 2583 regv[freg]=0; |
89 | 2584 } |
2585 | |
2586 void dtosop(int op,int e1) | |
2587 { | |
127 | 2588 char *opn=""; |
97 | 2589 char *frn=fregister_name(freg); |
2590 char *grn=fregister_name(e1); | |
145 | 2591 regv[freg]=1; |
89 | 2592 switch(op) { |
133 | 2593 case FADD: |
91 | 2594 case DADD: opn="fadd"; break; |
133 | 2595 case FSUB: |
101 | 2596 case DSUB: opn="fsub"; break; |
133 | 2597 case FDIV: |
101 | 2598 case DDIV: opn="fdiv"; break; |
133 | 2599 case FMUL: |
101 | 2600 case DMUL: opn="fmul"; break; |
133 | 2601 case FCMP: |
91 | 2602 case DCMP: |
108 | 2603 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2604 free_register(e1); |
101 | 2605 return; |
133 | 2606 case FCMPGE: |
91 | 2607 case DCMPGE: |
108 | 2608 printf("\tfcmpu cr7,%s,%s\n",frn,grn); |
147 | 2609 free_register(e1); |
101 | 2610 return; |
133 | 2611 default: |
2612 error(-1); return; | |
89 | 2613 } |
91 | 2614 printf("\t%s %s,%s,%s\n",opn,frn,frn,grn); |
145 | 2615 regv[freg]=1; |
147 | 2616 free_register(e1); |
89 | 2617 } |
2618 | |
2619 void | |
2620 code_dassop(int op,int d) { | |
91 | 2621 /* we have lvalue in creg, applied floating value is in freg */ |
97 | 2622 char *frn=fregister_name(freg); |
138 | 2623 int xreg=emit_dpop(d); |
91 | 2624 char *crn=register_name(creg); |
2625 | |
147 | 2626 if (!is_float_reg(freg)) error(-1); |
108 | 2627 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
102 | 2628 dtosop(op,xreg); |
108 | 2629 printf("\t%s %s,0(%s)\n",fstore(d),frn,crn); |
138 | 2630 emit_dpop_free(xreg,d); |
145 | 2631 regv[freg]=1; |
89 | 2632 } |
2633 | |
91 | 2634 |
89 | 2635 void |
107 | 2636 code_dpreinc(int e1,int e2,int d,int reg) { |
91 | 2637 char *frn; |
2638 char *crn; | |
2639 int g; | |
2640 char *grn,*drn; | |
2641 int r; | |
109 | 2642 r = get_ptr_cache(&float_one); |
96 | 2643 float_one_lib_used=1; |
91 | 2644 |
144 | 2645 if (car(e2)==DREGISTER||car(e2)==FREGISTER) { |
2646 error(-1); /* unspported now */ | |
2647 } | |
89 | 2648 g_expr(e2); |
91 | 2649 |
2650 crn=register_name(creg); | |
97 | 2651 frn=fregister_name(freg); |
91 | 2652 drn=register_name(r); |
138 | 2653 grn=fregister_name(g=get_dregister(d)); |
91 | 2654 |
108 | 2655 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
2656 printf("\tlfs %s,0(%s)\n",grn,drn); | |
89 | 2657 if (caddr(e1)>0) |
91 | 2658 printf("\tfadd %s,%s,%s\n",frn,frn,grn); |
89 | 2659 else |
91 | 2660 printf("\tfsub %s,%s,%s\n",frn,frn,grn); |
147 | 2661 if (!is_float_reg(freg)) error(-1); |
108 | 2662 printf("\t%s %s,0(%s)\n",fstore(d),frn,crn); |
147 | 2663 free_register(g); |
145 | 2664 regv[freg]=1; |
147 | 2665 regv[ireg]=0; |
2666 creg = freg; | |
89 | 2667 } |
2668 | |
2669 void | |
107 | 2670 code_dpostinc(int e1,int e2,int d,int reg) { |
91 | 2671 char *frn; |
2672 char *crn; | |
2673 int g; | |
2674 char *grn,*drn; | |
2675 int r; | |
109 | 2676 r = get_ptr_cache(&float_one); |
96 | 2677 float_one_lib_used=1; |
91 | 2678 |
144 | 2679 if (car(e2)==DREGISTER||car(e2)==FREGISTER) { |
2680 error(-1); /* unspported now */ | |
2681 } | |
89 | 2682 g_expr(e2); |
91 | 2683 |
2684 crn=register_name(creg); | |
97 | 2685 frn=fregister_name(freg); |
91 | 2686 drn=register_name(r); |
138 | 2687 grn=fregister_name(g=get_dregister(d)); |
91 | 2688 |
108 | 2689 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
2690 printf("\tlfs %s,0(%s)\n",grn,drn); | |
89 | 2691 if (caddr(e1)>0) |
91 | 2692 printf("\tfadd %s,%s,%s\n",grn,frn,grn); |
89 | 2693 else |
91 | 2694 printf("\tfsub %s,%s,%s\n",grn,frn,grn); |
147 | 2695 if (!is_float_reg(freg)) error(-1); |
108 | 2696 printf("\t%s %s,0(%s)\n",fstore(d),grn,crn); |
147 | 2697 free_register(g); |
145 | 2698 regv[freg]=1; |
147 | 2699 regv[ireg]=0; |
2700 creg = freg; | |
89 | 2701 } |
2702 | |
2703 void | |
2704 drexpr(int e1, int e2,int l1, int op) | |
2705 { | |
91 | 2706 g_expr(list3(((op==DOP+GE)?DCMPGE:DCMP),e1,e2)); |
89 | 2707 switch(op) { |
2708 case DOP+GE: | |
133 | 2709 case FOP+GE: |
91 | 2710 printf("\tcror 2,29,30\n"); |
111 | 2711 printf("\tbne\tcr0,L_%d\n",l1); |
89 | 2712 break; |
2713 case DOP+GT: | |
133 | 2714 case FOP+GT: |
111 | 2715 printf("\tble\tcr0,L_%d\n",l1); |
89 | 2716 break; |
2717 case DOP+EQ: | |
133 | 2718 case FOP+EQ: |
111 | 2719 printf("\tbne\tcr0,L_%d\n",l1); |
89 | 2720 break; |
2721 case DOP+NEQ: | |
133 | 2722 case FOP+NEQ: |
111 | 2723 printf("\tbeq\tcr0,L_%d\n",l1); |
89 | 2724 break; |
2725 } | |
2726 } | |
2727 | |
138 | 2728 int emit_dpop(int d) |
89 | 2729 { |
114 | 2730 int xreg,reg; |
97 | 2731 xreg=pop_fregister(); |
2732 if (xreg<= -REG_LVAR_OFFSET) { | |
138 | 2733 reg = get_dregister(d); |
119 | 2734 code_drlvar(REG_LVAR_OFFSET+xreg,1,reg); |
117 | 2735 free_lvar(REG_LVAR_OFFSET+xreg); |
145 | 2736 regv[reg]=1; xreg=reg; |
96 | 2737 } |
2738 return xreg; | |
89 | 2739 } |
2740 | |
138 | 2741 void emit_dpop_free(int e1,int d) |
89 | 2742 { |
147 | 2743 free_register(e1); |
89 | 2744 } |
2745 | |
138 | 2746 void emit_dpush(int d) |
89 | 2747 { |
97 | 2748 int new_reg; |
102 | 2749 if (freg_sp>MAX_MAX) error(-1); |
138 | 2750 new_reg = get_dregister(1); |
97 | 2751 freg_stack[freg_sp++] = freg; /* push するかわりにレジスタを使う */ |
147 | 2752 creg = freg = new_reg; |
145 | 2753 regv[freg]=1; |
89 | 2754 } |
2755 | |
95 | 2756 void |
2757 code_save_stacks() | |
2758 { | |
103 | 2759 int i,reg; |
101 | 2760 for(i=0;i<reg_sp;i++) { |
103 | 2761 if ((reg=reg_stack[i])>=0) { |
101 | 2762 code_assign_lvar( |
119 | 2763 (reg_stack[i]=new_lvar(size_of_int)),reg,0); |
101 | 2764 reg_stack[i]= reg_stack[i]-REG_LVAR_OFFSET; |
2765 } | |
2766 } | |
2767 for(i=0;i<freg_sp;i++) { | |
103 | 2768 if ((reg=freg_stack[i])>=0) { |
101 | 2769 code_dassign_lvar( |
119 | 2770 (freg_stack[i]=new_lvar(size_of_double)),reg,1); |
101 | 2771 freg_stack[i]= freg_stack[i]-REG_LVAR_OFFSET; |
2772 } | |
2773 } | |
96 | 2774 } |
2775 | |
2776 void | |
2777 emit_lib(char *p[]) | |
95 | 2778 { |
98 | 2779 while(*p) { |
2780 printf("%s\n",*p++); | |
96 | 2781 } |
2782 } | |
2783 | |
2784 void | |
2785 code_closing() | |
2786 { | |
2787 if (d2u_lib_used) emit_lib(d2u_lib); | |
2788 if (u2d_lib_used) emit_lib(u2d_lib); | |
2789 if (float_one_lib_used) emit_lib(float_one_lib); | |
98 | 2790 if (float_zero_lib_used) emit_lib(float_zero_lib); |
96 | 2791 if (i2d_lib_used) emit_lib(i2d_lib); |
97 | 2792 global_table(); |
108 | 2793 /* printf("\t.ident \"Micro-C compiled\"\n"); */ |
95 | 2794 } |
2795 | |
89 | 2796 /* end */ |
2797 |