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