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