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