Mercurial > hg > CbC > old > device
annotate mc-code-powerpc.c @ 203:28baf6cd9ad1
*** empty log message ***
author | kono |
---|---|
date | Tue, 13 Apr 2004 10:28:21 +0900 |
parents | 601301152d9c |
children | 4c614334f3d0 |
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 |
202 | 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; |
202 | 1427 } else if (t==LONGLONG||t==ULONGLONG) { |
1428 continue; | |
89 | 1429 } else if (t==DOUBLE||t==FLOAT) { |
137 | 1430 if (reg_arg<MAX_INPUT_REGISTER_VAR) { |
119 | 1431 /* sigh... |
1432 printf requies floating value in integer registers | |
1433 */ | |
137 | 1434 if (dots) { |
1435 if (car(e4)==DRLVAR) { | |
1436 special_lvar = cadr(e4); | |
1437 e5 = list2(LVAR,special_lvar); | |
1438 } else { | |
1439 special_lvar = new_lvar(size_of_double); | |
1440 g_expr(assign_expr0( | |
1441 (e5=list2(LVAR,special_lvar)),e4,DOUBLE,t)); | |
1442 reg_arg_list = list2(e5,reg_arg_list); | |
1443 e4 = list2(DREGISTER,freg); | |
1444 /* freg should not change until XXX */ | |
1445 } | |
1446 r0=get_input_register_var(reg_arg,0,0); | |
1447 r1 = reg_arg+1+MIN_TMP_REG; | |
1448 if (regs[r1]==PTRC_REG) clear_ptr_cache_reg(r1); | |
1449 /* else if (regs[r1]) error(-1); */ | |
1450 r1=get_input_register_var_1(reg_arg+1,0,0); | |
1451 | |
1452 use_var(r0); /* protect from input register free */ | |
1453 use_var(r1); /* protect from input register free */ | |
1454 reg_arg_list = list2(r0,reg_arg_list); | |
1455 reg_arg_list = list2(r1,reg_arg_list); | |
1456 arg_assign = list2( assign_expr0(r0,e5,INT,INT), arg_assign); | |
1457 arg_assign = list2( assign_expr0(r1, | |
1458 list2(LVAR,special_lvar+size_of_int), | |
1459 INT,INT), arg_assign); | |
111 | 1460 } |
1461 reg_arg += 2; | |
1462 } | |
136
069960078249
remove redundant funcall argument for prototyped code in PowerPC case.
kono
parents:
135
diff
changeset
|
1463 if (dots && freg_arg>=4 && freg_arg<MAX_INPUT_DREGISTER_VAR) { |
119 | 1464 /* oh my god! |
1465 it requies integer register and floating register and | |
1466 stack value. You are crazy. | |
1467 */ | |
1468 arg_assign = list2( | |
1469 assign_expr0(list2(LVAR,caller_arg_offset_v(nargs)), | |
138 | 1470 get_input_dregister_var(freg_arg,0,0,1),t,t), |
119 | 1471 arg_assign); |
1472 } | |
122 | 1473 if (freg_arg>=MAX_INPUT_DREGISTER_VAR) { |
119 | 1474 arg = list2(LVAR,caller_arg_offset_v(nargs)); |
137 | 1475 } else if (!simple_args(e3) && cadr(e3)) { |
138 | 1476 arg = get_dregister_var(0,1); |
104 | 1477 arg_assign = list2( |
138 | 1478 assign_expr0(get_input_dregister_var(freg_arg,0,0,1), |
137 | 1479 arg,t,t), |
104 | 1480 arg_assign); |
99 | 1481 } else { |
138 | 1482 arg = get_input_dregister_var(freg_arg,0,0,1); |
99 | 1483 } |
107 | 1484 use_var(arg); /* protect from input register free */ |
99 | 1485 reg_arg_list = list2(arg,reg_arg_list); |
118 | 1486 g_expr_u(assign_expr0(arg,e4,t,t)); /* XXX */ |
99 | 1487 freg_arg++; |
122 | 1488 nargs += size(t)/size_of_int; |
89 | 1489 continue; |
203 | 1490 } else if (t>=0&&(car(t)==STRUCT||car(t)==UNION)) { |
119 | 1491 arg = list2(LVAR,caller_arg_offset_v(nargs)); |
1492 nargs += struct_push(e4,t,arg); | |
89 | 1493 continue; |
1494 } else { | |
1495 error(TYERR); | |
1496 } | |
1497 ++nargs; | |
1498 } | |
116 | 1499 if (max_func_args<nargs) max_func_args=nargs; |
104 | 1500 for(;arg_assign;arg_assign=cadr(arg_assign)) { |
1501 g_expr_u(car(arg_assign)); | |
1502 } | |
96 | 1503 clear_ptr_cache(); |
89 | 1504 if (car(e2) == FNAME) { |
108 | 1505 printf("\tbl\tL_%s$stub\n",fn->nm); |
89 | 1506 } else { |
108 | 1507 jrn = register_name(cadr(jmp)); |
89 | 1508 printf("\tmtctr %s\n",jrn); |
1509 printf("\tbctrl\n"); | |
1510 } | |
99 | 1511 for(;reg_arg_list;reg_arg_list=cadr(reg_arg_list)) { |
108 | 1512 arg = car(reg_arg_list); |
147 | 1513 if (car(arg)==REGISTER||car(arg)==DREGISTER||car(arg)==FREGISTER) |
1514 free_register(cadr(arg)); | |
119 | 1515 else if (car(arg)==LVAR&&cadr(arg)<0) free_lvar(cadr(arg)); |
99 | 1516 } |
105 | 1517 if (ret_type==DOUBLE||ret_type==FLOAT) { |
104 | 1518 set_freg(RET_FREGISTER,0); |
145 | 1519 regv[freg]=1; regv[creg]=0; |
105 | 1520 } else if (ret_type==VOID) { |
145 | 1521 regv[freg]=0; regv[creg]=0; |
96 | 1522 } else { |
104 | 1523 set_creg(RET_REGISTER,0); |
145 | 1524 regv[freg]=0; regv[creg]=1; |
96 | 1525 } |
105 | 1526 return ret_type; |
89 | 1527 } |
1528 | |
1529 void | |
1530 code_frame_pointer(int e3) { | |
128 | 1531 #if R1SAVE |
1532 printf("\tmr r1,%s\n",register_name(e3)); | |
1533 #else | |
126 | 1534 printf("\tmr r30,%s\n",register_name(e3)); |
128 | 1535 #endif |
89 | 1536 } |
1537 | |
1538 | |
1539 void | |
1540 code_fix_frame_pointer(int disp_offset) { | |
128 | 1541 int l = 0; |
1542 printf("\tla r30,"); | |
1543 printf("lo16(%d+L_%d)(r30)\n",FUNC_LVAR,lvar_offset_label); | |
89 | 1544 } |
1545 | |
1546 void | |
1547 code_jmp(char *s) { | |
128 | 1548 max_reg_var = REG_VAR_BASE-REG_VAR_MIN; |
1549 max_freg_var = FREG_VAR_BASE-FREG_VAR_MIN; | |
89 | 1550 printf("\tb L_%s$stub\n",s); |
1551 } | |
1552 | |
1553 | |
1554 void | |
1555 code_indirect_jmp(int e2) { | |
128 | 1556 max_reg_var = REG_VAR_BASE-REG_VAR_MIN; |
1557 max_freg_var = FREG_VAR_BASE-FREG_VAR_MIN; | |
89 | 1558 printf("\tmtctr %s\n",register_name(e2)); |
1559 printf("\tbctr\n"); | |
1560 } | |
1561 | |
95 | 1562 int |
196 | 1563 code_rindirect(int e1, int offset, int us) |
89 | 1564 { |
95 | 1565 char *crn; |
196 | 1566 g_expr(e1); |
90 | 1567 crn=register_name(creg); |
196 | 1568 printf("\tlwz %s,%d(%s)\n",crn,offset,crn); |
1569 return INT; | |
1570 } | |
1571 | |
1572 int | |
1573 code_crindirect(int e1, int offset, int us) | |
1574 { | |
1575 char *crn; | |
1576 g_expr(e1); | |
1577 crn=register_name(creg); | |
1578 if (us) { | |
1579 printf("\tlbz %s,%d(%s)\n",crn,offset,crn); | |
1580 return UCHAR; | |
1581 } else { | |
119 | 1582 printf("\tlbz %s,%d(%s)\n",crn,offset,crn); |
90 | 1583 printf("\textsb %s,%s\n",crn,crn); |
95 | 1584 return CHAR; |
196 | 1585 } |
1586 } | |
1587 | |
1588 int | |
1589 code_srindirect(int e1, int offset, int us) | |
1590 { | |
1591 char *crn; | |
1592 g_expr(e1); | |
1593 crn=register_name(creg); | |
1594 if (us) { | |
1595 printf("\tlhz %s,%d(%s)\n",crn,offset,crn); | |
1596 return USHORT; | |
1597 } else { | |
165 | 1598 printf("\tlhz %s,%d(%s)\n",crn,offset,crn); |
1599 printf("\textsh %s,%s\n",crn,crn); | |
1600 return SHORT; | |
89 | 1601 } |
1602 } | |
1603 | |
196 | 1604 #if FLOAT_CODE |
1605 int | |
1606 code_drindirect(int e1, int offset, int d) | |
1607 { | |
1608 char *crn; | |
1609 g_expr(e1); | |
1610 crn=register_name(creg); | |
1611 printf("\t%s %s,%d(%s)\n",fload(d), | |
1612 fregister_name(freg),offset,crn); | |
1613 regv[creg]=0; regv[freg]=1; | |
1614 creg = freg; | |
1615 return DOUBLE; | |
1616 } | |
1617 #endif | |
1618 | |
1619 #if LONGLONG_CODE | |
1620 int | |
1621 code_lrindirect(int e1, int offset, int us) | |
1622 { | |
1623 char *crn; | |
1624 g_expr(e1); | |
1625 crn=register_name(creg); | |
1626 return LONGLONG; | |
1627 } | |
1628 #endif | |
1629 | |
1630 | |
89 | 1631 void |
103 | 1632 code_assign_gvar(int e2,int creg,int byte) { |
90 | 1633 int r; |
1634 char *crn,*rrn; | |
109 | 1635 r = get_ptr_cache((NMTBL*)cadr(e2)); |
90 | 1636 rrn=register_name(r); |
1637 crn=register_name(creg); | |
165 | 1638 if (byte==1) { |
108 | 1639 printf("\tstb %s,0(%s)\n",crn,rrn); |
165 | 1640 } else if (byte==size_of_short) { |
1641 printf("\tsth %s,0(%s)\n",crn,rrn); | |
90 | 1642 } else { |
108 | 1643 printf("\tstw %s,0(%s)\n",crn,rrn); |
90 | 1644 } |
89 | 1645 } |
1646 | |
1647 void | |
103 | 1648 code_assign_lvar(int e2,int creg,int byte) { |
90 | 1649 char *crn; |
1650 crn=register_name(creg); | |
119 | 1651 lvar_intro(e2); |
165 | 1652 if (byte==1) { |
1653 printf("\tstb %s,",crn); | |
1654 } else if (byte==size_of_short) { | |
1655 printf("\tsth %s,",crn); | |
90 | 1656 } else { |
165 | 1657 printf("\tstw %s,",crn); |
90 | 1658 } |
165 | 1659 lvar(e2); |
89 | 1660 } |
1661 | |
1662 void | |
111 | 1663 code_assign_register(int e2,int byte,int creg) { |
104 | 1664 if (e2!=creg) |
1665 printf("\tmr %s,%s\n",register_name(e2),register_name(creg)); | |
89 | 1666 } |
1667 | |
1668 void | |
118 | 1669 code_assign(int e2,int byte,int creg) { |
95 | 1670 char *drn=register_name(e2); |
1671 char *crn=register_name(creg); | |
1672 | |
165 | 1673 if (byte==1) { |
108 | 1674 printf("\tstb %s,0(%s)\n",crn,drn); |
165 | 1675 } else if (byte==size_of_short) { |
1676 printf("\tsth %s,0(%s)\n",crn,drn); | |
90 | 1677 } else { |
108 | 1678 printf("\tstw %s,0(%s)\n",crn,drn); |
90 | 1679 } |
144 | 1680 regv[creg]=1; |
89 | 1681 } |
1682 | |
1683 | |
1684 void | |
1685 code_register_assop(int e2,int op,int byte) { | |
1686 int reg; | |
1687 int xreg = creg; | |
1688 creg = reg = e2; | |
1689 tosop(op,xreg); | |
1690 creg = xreg; | |
104 | 1691 if (creg!=reg) |
1692 printf("\tmr %s,%s\n",register_name(creg),register_name(reg)); | |
144 | 1693 regv[creg]=1; |
89 | 1694 } |
1695 | |
1696 | |
1697 void | |
162 | 1698 code_assop(int op,int byte,int sign) { |
90 | 1699 char *xrn,*crn,*drn; |
89 | 1700 int xreg; |
90 | 1701 int edx = get_register(); if(!edx) error(-1); |
93 | 1702 xrn = register_name(xreg = emit_pop(0)); /* pop e3 value */ |
89 | 1703 regv[xreg]=regs[xreg]=1; |
113 | 1704 printf("# assop\n\tmr %s,%s\n",register_name(edx),register_name(creg)); |
89 | 1705 regv[edx]=1; |
162 | 1706 ld_indexx(byte,0,edx,sign); |
89 | 1707 tosop(op,xreg); |
90 | 1708 crn = register_name(creg); |
1709 drn = register_name(edx); | |
165 | 1710 if (byte==1) { |
108 | 1711 printf("\tstb %s,0(%s)\n",crn,drn); |
165 | 1712 } else if (byte==size_of_short) { |
1713 printf("\tsth %s,0(%s)\n",crn,drn); | |
90 | 1714 } else { |
108 | 1715 printf("\tstw %s,0(%s)\n",crn,drn); |
90 | 1716 } |
1717 free_register(edx); | |
89 | 1718 emit_pop_free(xreg); |
144 | 1719 regv[creg]=1; |
89 | 1720 } |
1721 | |
1722 | |
1723 void | |
1724 tosop(int op,int oreg) | |
1725 { | |
1726 int dx; | |
95 | 1727 char *orn,*crn,*drn; |
89 | 1728 |
103 | 1729 if(oreg==-1) { |
1730 error(-1); | |
1731 } else if (oreg<= -REG_LVAR_OFFSET) { | |
104 | 1732 dx = get_register(); if (dx<0) error(-1); |
119 | 1733 code_rlvar(oreg+REG_LVAR_OFFSET,dx); |
104 | 1734 oreg = dx; |
103 | 1735 regv[oreg]=1; |
1736 } | |
1737 | |
89 | 1738 switch(op) { |
1739 case LSHIFT: | |
1740 case ULSHIFT: | |
90 | 1741 shift("slw",oreg); |
144 | 1742 regv[creg]=1; |
89 | 1743 return; |
1744 case RSHIFT: | |
186 | 1745 shift("sraw",oreg); |
144 | 1746 regv[creg]=1; |
89 | 1747 return; |
1748 case URSHIFT: | |
186 | 1749 shift("srw",oreg); |
144 | 1750 regv[creg]=1; |
89 | 1751 return; |
1752 } | |
93 | 1753 orn = register_name(oreg); |
89 | 1754 crn = register_name(creg); |
1755 switch(op) { | |
1756 case ADD: | |
114 | 1757 printf("\tadd %s,%s,%s\n",crn,crn,orn); |
89 | 1758 break; |
1759 case SUB: | |
114 | 1760 printf("\tsub %s,%s,%s\n",crn,crn,orn); |
89 | 1761 break; |
112 | 1762 case CMP: |
113 | 1763 printf("\tcmpw cr0,%s,%s\n",crn,orn); |
112 | 1764 break; |
89 | 1765 case BAND: |
114 | 1766 printf("\tand %s,%s,%s\n",crn,crn,orn); |
89 | 1767 break; |
1768 case EOR: | |
114 | 1769 printf("\txor %s,%s,%s\n",crn,crn,orn); |
89 | 1770 break; |
1771 case BOR: | |
114 | 1772 printf("\tor %s,%s,%s\n",crn,crn,orn); |
89 | 1773 break; |
1774 case MUL: | |
116 | 1775 printf("\tmullw %s,%s,%s\n",crn,crn,orn); |
1776 break; | |
89 | 1777 case UMUL: |
114 | 1778 printf("\tmullw %s,%s,%s\n",crn,crn,orn); |
89 | 1779 break; |
1780 case DIV: | |
114 | 1781 printf("\tdivw %s,%s,%s\n",crn,crn,orn); |
90 | 1782 break; |
89 | 1783 case UDIV: |
114 | 1784 printf("\tdivwu %s,%s,%s\n",crn,crn,orn); |
89 | 1785 break; |
1786 case MOD: | |
90 | 1787 dx=get_register(); |
95 | 1788 drn = register_name(dx); |
116 | 1789 printf("\tdivw %s,%s,%s\n",drn,crn,orn); |
1790 printf("\tmullw %s,%s,%s\n",drn,drn,orn); | |
1791 printf("\tsubf %s,%s,%s\n",crn,drn,crn); | |
114 | 1792 free_register(dx); |
90 | 1793 break; |
89 | 1794 case UMOD: |
90 | 1795 dx=get_register(); |
95 | 1796 drn = register_name(dx); |
114 | 1797 printf("\tdivwu %s,%s,%s\n",drn,crn,orn); |
116 | 1798 printf("\tmullw %s,%s,%s\n",drn,drn,orn); |
1799 printf("\tsubf %s,%s,%s\n",crn,drn,crn); | |
114 | 1800 free_register(dx); |
89 | 1801 break; |
112 | 1802 default: |
1803 error(-1); | |
89 | 1804 } |
95 | 1805 if(oreg!=creg) free_register(oreg); |
144 | 1806 regv[creg]=1; |
89 | 1807 } |
1808 | |
189 | 1809 int |
1810 code_const_op_p(int op,int v) | |
1811 { | |
1812 if (op==BAND||op==DIV||op==UDIV||op==MOD||op==UMOD) return 0; | |
1813 return (-127<v&&v<128); | |
1814 } | |
1815 | |
1816 void | |
1817 oprtc(int op,int v) | |
1818 { | |
1819 char *crn = register_name(creg); | |
1820 | |
1821 switch(op) { | |
1822 case LSHIFT: | |
1823 case ULSHIFT: | |
1824 printf("\tslwi %s,%s,%d\n",crn,crn,v); | |
1825 return; | |
1826 case RSHIFT: | |
1827 printf("\tsrawi %s,%s,%d\n",crn,crn,v); | |
1828 return; | |
1829 case URSHIFT: | |
1830 printf("\tsrwi %s,%s,%d\n",crn,crn,v); | |
1831 return; | |
1832 case ADD: | |
1833 printf("\taddi %s,%s,lo16(%d)\n",crn,crn,v); | |
1834 break; | |
1835 case SUB: | |
1836 printf("\tsubi %s,%s,lo16(%d)\n",crn,crn,v); | |
1837 break; | |
1838 case CMP: | |
1839 printf("\tcmpwi cr0,%s,lo16(%d)\n",crn,v); | |
1840 break; | |
1841 case EOR: | |
1842 printf("\txori %s,%s,lo16(%d)\n",crn,crn,v); | |
1843 break; | |
1844 case BOR: | |
1845 printf("\tori %s,%s,lo16(%d)\n",crn,crn,v); | |
1846 break; | |
1847 case MUL: | |
1848 printf("\tmulli %s,%s,lo16(%d)\n",crn,crn,v); | |
1849 break; | |
1850 case UMUL: | |
1851 printf("\tmulli %s,%s,lo16(%d)\n",crn,crn,v); | |
1852 break; | |
1853 default: | |
1854 error(-1); | |
1855 } | |
1856 } | |
89 | 1857 |
1858 void | |
1859 shift(char *op, int reg) | |
1860 { | |
93 | 1861 char *crn = register_name(creg); |
1862 char *rrn = register_name(reg); | |
186 | 1863 printf("\t%s %s,%s,%s\n",op,crn,crn,rrn); |
89 | 1864 } |
1865 | |
1866 void | |
162 | 1867 ld_indexx(int byte, int n, int xreg,int sign) |
89 | 1868 { |
95 | 1869 char *crn = register_name(creg); |
165 | 1870 if (byte==1) { |
95 | 1871 printf("\tlbz %s,%d(%s)\n",register_name(creg),n, |
90 | 1872 register_name(xreg)); |
162 | 1873 if (sign) |
1874 printf("\textsb %s,%s\n",crn,crn); | |
165 | 1875 } else if (byte==size_of_short) { |
1876 printf("\tlhz %s,%d(%s)\n",register_name(creg),n, | |
1877 register_name(xreg)); | |
1878 if (sign) | |
1879 printf("\textsh %s,%s\n",crn,crn); | |
90 | 1880 } else |
95 | 1881 printf("\tlwz %s,%d(%s)\n",register_name(creg),n, |
90 | 1882 register_name(xreg)); |
89 | 1883 } |
1884 | |
95 | 1885 int |
1886 code_csvalue() | |
1887 { | |
1888 return creg; | |
1889 } | |
1890 | |
89 | 1891 void |
95 | 1892 code_cmpdimm(int e, int csreg) |
89 | 1893 { |
1894 /* used in dosiwtch() */ | |
1895 if(chk) return; | |
108 | 1896 printf("\tcmpwi cr0,%s,%d\n",register_name(csreg),e); |
89 | 1897 } |
1898 | |
1899 void | |
1900 code_opening(char *filename) | |
1901 { | |
1902 printf("\t.file \"%s\"\n",filename); | |
108 | 1903 /* printf("\t.version\t\"01.01\"\n"); */ |
89 | 1904 /* printf("gcc2_compiled.:\n"); */ |
1905 printf(".text\n"); | |
1906 } | |
1907 | |
1908 void | |
95 | 1909 rexpr(int e1, int l1, char *s,int t) |
89 | 1910 { |
90 | 1911 g_expr(list3(CMP,cadr(e1),caddr(e1))); |
102 | 1912 printf("\tb%s cr0,L_%d\n",s,l1); |
89 | 1913 } |
1914 | |
1915 | |
1916 void | |
1917 jcond(int l, char cond) | |
1918 { | |
1919 if (chk) return; | |
102 | 1920 printf("\tb%s cr0,L_%d\n",cond?"ne":"eq",l); |
89 | 1921 } |
1922 | |
1923 void | |
1924 jmp(int l) | |
1925 { | |
1926 control=0; | |
1927 if (chk) return; | |
102 | 1928 printf("\tb\tL_%d\n",l); |
89 | 1929 } |
1930 | |
1931 void | |
1932 gen_comment(char *s) | |
1933 { | |
1934 if (chk) return; | |
1935 printf("## %s",s); | |
1936 } | |
1937 | |
1938 void | |
1939 code_enter(char *name) | |
1940 { | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1941 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
|
1942 text_mode(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1943 else |
108 | 1944 printf("\t.align 2\n"); |
89 | 1945 if (stmode!=STATIC) |
102 | 1946 printf(".globl _%s\n",name); |
126 | 1947 #ifdef DOT_SIZE |
89 | 1948 printf("\t.type\t%s,@function\n",name); |
126 | 1949 #endif |
101 | 1950 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
|
1951 code_disp_label=fwdlabel(); |
128 | 1952 printf("\tla r1,lo16(L_%d)(r30)\n",code_disp_label); |
126 | 1953 printf("\tbcl 20,31,L_%d\n",code_base = fwdlabel()); |
1954 fwddef(code_base); | |
1955 printf("\tmflr r31\n"); | |
119 | 1956 max_func_args = 0; |
89 | 1957 } |
1958 | |
1959 | |
1960 void | |
1961 code_enter1(int args) | |
1962 { | |
106 | 1963 set_creg(CREG_REGISTER,0); |
1964 set_freg(FREG_FREGISTER,0); | |
89 | 1965 } |
1966 | |
1967 void | |
1968 code_leave(char *name) | |
1969 { | |
128 | 1970 int r1_offsetv; |
89 | 1971 disp&= -size_of_int; |
128 | 1972 r1_offsetv = -disp+max_func_args*size_of_int+code_disp_offset; |
1973 | |
1974 printf(".set L_%d,%d\n",code_disp_label,-r1_offsetv); | |
89 | 1975 local_table(); |
1976 free_all_register(); | |
1977 } | |
1978 | |
1979 void | |
1980 enter(char *name) | |
1981 { | |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1982 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
|
1983 text_mode(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1984 else |
108 | 1985 printf("\t.align 2\n"); |
89 | 1986 if (stmode!=STATIC) |
101 | 1987 printf(".globl _%s\n",name); |
108 | 1988 /* |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1989 printf("\t.type\t%s,@function\n",name); |
108 | 1990 */ |
101 | 1991 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
|
1992 code_setup=fwdlabel(); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
1993 printf("\tmflr r0\n"); |
102 | 1994 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
|
1995 code_base=fwdlabel(); |
111 | 1996 fwddef(code_base); |
116 | 1997 r1_offset_label = fwdlabel(); |
1998 lvar_offset_label = fwdlabel(); | |
1999 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
|
2000 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
|
2001 printf("\tmflr r31\n"); |
116 | 2002 max_func_args = 0; |
89 | 2003 } |
2004 | |
2005 void | |
2006 enter1() | |
2007 { | |
182 | 2008 text_mode(); |
106 | 2009 set_creg(CREG_REGISTER,0); |
2010 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
|
2011 } |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2012 |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2013 int |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2014 reg_save_offset() |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2015 { |
108 | 2016 return -( |
116 | 2017 (REAL_MAX_REGISTER-(REG_VAR_BASE-max_reg_var))*size_of_int+ |
108 | 2018 (REAL_MAX_FREGISTER-(FREG_VAR_BASE-max_freg_var))*size_of_double |
2019 ); | |
89 | 2020 } |
2021 | |
2022 void | |
2023 leave(int control, char *name) | |
2024 { | |
130 | 2025 int retcont1=0,sz; |
128 | 2026 |
2027 if (max_freg_var>=0 && max_freg_var<=3) max_freg_var=3; | |
2028 reg_save = reg_save_offset(); | |
111 | 2029 |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2030 if (control) { |
108 | 2031 code_set_return_register(1); |
89 | 2032 } |
126 | 2033 if (retcont) { |
2034 if (control) jmp(retlabel); | |
128 | 2035 retcont1 = fwdlabel(); |
126 | 2036 fwddef(retcont); |
2037 if (cadr(fnptr->ty)==FLOAT||cadr(fnptr->ty)==DOUBLE) { | |
2038 printf("\tfmr f1,f31\n"); | |
128 | 2039 } else if (cadr(fnptr->ty)>0&&( |
2040 car(cadr(fnptr->ty))==STRUCT || | |
2041 car(cadr(fnptr->ty))==UNION)) { | |
2042 sz = size(cadr(fnptr->ty)); | |
2043 printf("\tli r7,%d\n",sz); | |
2044 printf("\tsubl r6,r7,r30\n"); | |
2045 printf("\tlwz r3,lo16(%d)(r30)\n",(my_func_args-1)*size_of_int); | |
2046 emit_copy(6,3,sz,0,1,1); | |
126 | 2047 } else if (cadr(fnptr->ty)!=VOID) { |
2048 printf("\tmr r3,r29\n"); | |
2049 } | |
128 | 2050 #if !R1SAVE |
2051 printf("\tla r1,lo16(%d)(r30)\n", | |
2052 -reg_save+my_func_args*size_of_int); | |
2053 #endif | |
2054 printf("\tb L_%d\n",retcont1); | |
126 | 2055 } |
89 | 2056 fwddef(retlabel); |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2057 printf("\tlwz r1,0(r1)\n"); |
128 | 2058 if (retcont) { |
2059 fwddef(retcont1); | |
2060 } | |
110 | 2061 if (max_freg_var>=0) { |
2062 printf("\tlmw r%d,%d(r1)\n", | |
111 | 2063 REG_VAR_BASE-max_reg_var,reg_save); |
2064 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
|
2065 printf("\tb restFP+%d ; restore f%d-f31\n", |
111 | 2066 freg_save, |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2067 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
|
2068 } else { |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2069 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
|
2070 printf("\tmtlr r0\n"); |
110 | 2071 printf("\tlmw r%d,%d(r1)\n", |
116 | 2072 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
|
2073 printf("\tblr\n"); |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2074 } |
89 | 2075 |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2076 disp &= -size_of_int; |
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2077 fwddef(code_setup); |
110 | 2078 printf("\tstmw r%d,%d(r1)\n", |
116 | 2079 REG_VAR_BASE-max_reg_var,reg_save); |
110 | 2080 printf("\tstw r0,8(r1)\n"); |
2081 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
|
2082 printf("\tb saveFP+%d ; save f%d-f31\n", |
111 | 2083 freg_save, |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2084 FREG_VAR_BASE-max_freg_var); |
110 | 2085 else { |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2086 printf("\tblr\n"); |
110 | 2087 } |
100
a9e6f2a2946f
Wrote all code for PowerPC and no compile error. Let's fix all bugs...
kono
parents:
99
diff
changeset
|
2088 |
116 | 2089 code_offset_set(); |
89 | 2090 local_table(); |
2091 labelno++; | |
2092 free_all_register(); | |
2093 } | |
2094 | |
2095 | |
2096 void | |
107 | 2097 code_set_return_register(int mode) { |
111 | 2098 if (cadr(fnptr->ty)==DOUBLE||cadr(fnptr->ty)==FLOAT) { |
107 | 2099 set_freg(RET_FREGISTER,mode); |
147 | 2100 } else if (cadr(fnptr->ty)==VOID) { |
103 | 2101 } else { |
107 | 2102 set_creg(RET_REGISTER,mode); |
103 | 2103 } |
89 | 2104 } |
2105 | |
187 | 2106 int |
2107 code_get_fixed_creg(int reg,int type) { | |
2108 return creg; | |
2109 } | |
2110 | |
89 | 2111 void |
187 | 2112 code_set_fixed_creg(int reg,int mode,int type) { |
108 | 2113 if (type==FLOAT||type==DOUBLE) { |
187 | 2114 set_freg(reg,mode); |
195 | 2115 } else if (type==LONGLONG) { |
2116 set_lreg(reg,mode); | |
108 | 2117 } else { |
187 | 2118 set_creg(reg,mode); |
108 | 2119 } |
107 | 2120 } |
2121 | |
2122 void | |
89 | 2123 gen_gdecl(char *n, int gpc) |
2124 { | |
102 | 2125 /* |
89 | 2126 if (stmode!=STATIC) |
99 | 2127 printf(".globl _%s\n",n); |
102 | 2128 */ |
89 | 2129 } |
2130 | |
2131 void | |
2132 align(int t) | |
2133 { | |
2134 if (t!=CHAR) { | |
2135 if (data_alignment & 1) | |
2136 printf("\t.align 2\n"); | |
2137 data_alignment = 0; | |
2138 } | |
2139 } | |
2140 | |
2141 void | |
2142 emit_data(int e, int t, NMTBL *n) | |
2143 { | |
2144 int l; | |
195 | 2145 #if FLOAT_CODE |
89 | 2146 double d; |
2147 float f; | |
195 | 2148 #endif |
89 | 2149 char *name; |
2150 name = n->nm; | |
156 | 2151 if(mode!=GDECL && mode!=STADECL) { |
89 | 2152 error(-1); return; |
2153 } | |
2154 if (chk) return; | |
2155 if (n->dsp != -1) { | |
2156 n->dsp = -1; /* initiallized flag */ | |
102 | 2157 printf(".globl\t_%s\n",name); |
89 | 2158 data_mode(name); |
2159 align(t); | |
102 | 2160 printf("_%s:\n",name); |
89 | 2161 } else { |
2162 data_mode(0); | |
2163 } | |
2164 if(car(e)==CONST) { | |
168 | 2165 if (t==CHAR||t==UCHAR) { |
89 | 2166 printf("\t.byte %d\n",cadr(e)); |
2167 if (data_alignment>0) | |
2168 data_alignment++; | |
2169 gpc += 1; | |
168 | 2170 } else if (t==SHORT||t==USHORT) { |
2171 printf("\t.short %d\n",cadr(e)); | |
89 | 2172 if (data_alignment>0) data_alignment++; |
168 | 2173 gpc += size_of_short; |
89 | 2174 } else { |
2175 printf("\t.long %d\n",cadr(e)); | |
2176 gpc += size_of_int; | |
2177 } | |
195 | 2178 #if LONGLONG_CODE |
2179 #endif | |
2180 #if FLOAT_CODE | |
89 | 2181 } else if(t==DOUBLE) { |
2182 d = dcadr(e); | |
99 | 2183 printf("\t.long\t0x%x,0x%x\n",code_d2(d),code_d1(d)); |
89 | 2184 } else if(t==FLOAT) { |
2185 f = dcadr(e); | |
2186 printf("\t.long\t0x%x\n",*(int *)&f); | |
195 | 2187 #endif |
89 | 2188 } else if(t!=CHAR) { |
2189 gpc += size_of_int; | |
2190 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) { | |
116 | 2191 printf("\t.long _%s\n",((NMTBL *)cadr(cadr(e)))->nm); |
89 | 2192 } else if(car(e)==FNAME) { |
116 | 2193 printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); |
109 | 2194 } else if(car(e)==GVAR) { |
116 | 2195 printf("\t.long _%s\n",((NMTBL *)cadr(e))->nm); |
89 | 2196 } else if(car(e)==STRING) { |
2197 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) { | |
2198 l = fwdlabel(); | |
102 | 2199 printf("\t.long L_%d\n",l); |
108 | 2200 printf(".cstring\n\t.align 2\n"); |
102 | 2201 printf("L_%d:\n",l); |
89 | 2202 output_mode = RODATA_EMIT_MODE; |
2203 } | |
2204 ascii((char *)cadr(e)); | |
2205 } else error(TYERR); | |
168 | 2206 } else error(TYERR); |
89 | 2207 } |
2208 | |
2209 void | |
2210 emit_data_closing(NMTBL *n) | |
2211 { | |
126 | 2212 #ifdef DOT_SIZE |
89 | 2213 int lb; |
126 | 2214 #endif |
89 | 2215 if (chk) return; |
2216 if (mode==GDECL) { | |
2217 data_mode(0); | |
126 | 2218 #ifdef DOT_SIZE |
89 | 2219 lb=fwdlabel(); |
102 | 2220 printf("L_%d:\n",lb); |
2221 printf("\t.size\t%s,L_%d-%s\n",n->nm,lb,n->nm); | |
126 | 2222 #endif |
89 | 2223 } |
2224 } | |
2225 | |
2226 void | |
2227 global_table(void) | |
2228 { | |
2229 NMTBL *n; | |
98 | 2230 int init; char *extrn; |
89 | 2231 init=0; |
2232 for(n=ntable;n < &ntable[GSYMS];n++) { | |
109 | 2233 if ((n->sc == GVAR) && n->dsp != -1) { |
89 | 2234 /* n->dsp = -1 means initialized global */ |
2235 if (init==0) { | |
2236 data_mode(0); | |
2237 init=1; | |
2238 } | |
97 | 2239 printf(".comm _%s,%d\n",n->nm,size(n->ty)); |
109 | 2240 } else if ((n->sc==STATIC) && n->dsp != -1) { |
2241 /* n->dsp = -1 means initialized global */ | |
2242 if (init==0) { | |
2243 data_mode(0); | |
2244 init=1; | |
2245 } | |
2246 printf(".lcomm _%s,%d\n",n->nm,size(n->ty)); | |
97 | 2247 } |
2248 } | |
2249 for(n=ntable;n < &ntable[GSYMS];n++) { | |
103 | 2250 if (is_code(n)||is_function(n)) { |
97 | 2251 extrn = n->nm; |
103 | 2252 if (n->sc==EXTRN1) { |
2253 data_mode(0); | |
2254 printf(".picsymbol_stub\n"); | |
97 | 2255 printf("L_%s$stub:\n",extrn); |
2256 printf("\t.indirect_symbol _%s\n",extrn); | |
2257 printf("\tmflr r0\n"); | |
2258 printf("\tbcl 20,31,L0$_%s\n",extrn); | |
2259 printf("L0$_%s:\n",extrn); | |
2260 printf("\tmflr r11\n"); | |
2261 printf("\taddis r11,r11,ha16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); | |
2262 printf("\tmtlr r0\n"); | |
2263 printf("\tlwz r12,lo16(L_%s$lazy_ptr-L0$_%s)(r11)\n",extrn,extrn); | |
2264 printf("\tmtctr r12\n"); | |
2265 printf("\taddi r11,r11,lo16(L_%s$lazy_ptr-L0$_%s)\n",extrn,extrn); | |
2266 printf("\tbctr\n"); | |
2267 printf(".data\n"); | |
2268 printf(".lazy_symbol_pointer\n"); | |
2269 printf("L_%s$lazy_ptr:\n",extrn); | |
2270 printf("\t.indirect_symbol _%s\n",extrn); | |
2271 printf("\t.long dyld_stub_binding_helper\n"); | |
103 | 2272 } else if (n->sc==FUNCTION||n->sc==CODE) { |
2273 text_mode(); | |
2274 printf("\t.set L_%s$stub,_%s\n",extrn,extrn); | |
109 | 2275 data_mode(0); |
111 | 2276 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",extrn,extrn); |
108 | 2277 } |
97 | 2278 } |
2279 } | |
109 | 2280 init=0; |
2281 for(n=ntable;n < &ntable[GSYMS];n++) { | |
2282 if (n->sc == GVAR) { | |
2283 if (init==0) { | |
119 | 2284 printf(".data\n"); |
109 | 2285 init=1; |
2286 } | |
116 | 2287 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",n->nm,n->nm); |
109 | 2288 } |
2289 } | |
97 | 2290 init = 0; |
2291 for(n=ntable;n < &ntable[GSYMS];n++) { | |
116 | 2292 if (is_code(n)||is_function(n)) continue; |
109 | 2293 if (n->sc==EXTRN1) { |
97 | 2294 if(init==0) { |
119 | 2295 printf(".data\n"); |
97 | 2296 printf(".non_lazy_symbol_pointer\n"); |
2297 init=1; | |
2298 } | |
2299 printf("L_%s$non_lazy_ptr:\n",n->nm); | |
2300 printf("\t.indirect_symbol _%s\n",n->nm); | |
2301 printf("\t.long\t0\n"); | |
89 | 2302 } |
2303 } | |
2304 } | |
2305 | |
2306 void | |
2307 local_table(void) | |
2308 { | |
2309 NMTBL *n; | |
2310 int init; | |
2311 init=0; | |
2312 /* static local variables */ | |
2313 for(n=ntable+GSYMS;n < &ntable[GSYMS+LSYMS];n++) { | |
2314 if (n->sc == GVAR) { | |
2315 if (init==0) { | |
2316 data_mode(0); | |
2317 init=1; | |
2318 } | |
156 | 2319 if (n->dsp != -1) /* initialized static */ |
2320 printf(".lcomm _%s,%d\n",n->nm,size(n->ty)); | |
2321 printf("L_%s$non_lazy_ptr:\n\t.long\t_%s\n",n->nm,n->nm); | |
89 | 2322 } |
2323 } | |
2324 } | |
2325 | |
2326 void | |
2327 text_mode(void) | |
2328 { | |
2329 if (output_mode!=TEXT_EMIT_MODE) { | |
2330 printf(".text\n"); | |
2331 printf("\t.align 2\n"); | |
2332 output_mode = TEXT_EMIT_MODE; | |
2333 } | |
2334 } | |
2335 | |
2336 void | |
2337 data_mode(char *name) | |
2338 { | |
2339 if (output_mode!=DATA_EMIT_MODE) { | |
2340 printf(".data\n"); | |
2341 output_mode = DATA_EMIT_MODE; | |
2342 } | |
108 | 2343 /* |
89 | 2344 if (name) |
2345 printf("\t.type\t%s,@object\n",name); | |
108 | 2346 */ |
89 | 2347 } |
2348 | |
195 | 2349 #if FLOAT_CODE |
2350 | |
89 | 2351 /* floating point */ |
2352 | |
96 | 2353 static int float_one_lib_used=0; |
2354 static char *float_one_lib[] = { | |
2355 ".data", | |
108 | 2356 /* ".literal8", */ |
96 | 2357 " .align 3", |
2358 "__float_one:", | |
2359 " .long 1065353216", | |
2360 ".text", | |
109 | 2361 /* ".set L__float_one$non_lazy_ptr,__float_one", */ |
96 | 2362 0 |
2363 }; | |
97 | 2364 static int float_zero_lib_used=0; |
2365 static char *float_zero_lib[] = { | |
2366 ".data", | |
108 | 2367 /* ".literal8", */ |
97 | 2368 " .align 3", |
2369 "__float_zero:", | |
2370 " .long 0", | |
2371 ".text", | |
109 | 2372 /* ".set L__float_zero$non_lazy_ptr,__float_zero", */ |
97 | 2373 0 |
2374 }; | |
2375 | |
89 | 2376 char * |
2377 fstore(int d) | |
2378 { | |
90 | 2379 return (d?"stfd":"stfs"); |
89 | 2380 } |
2381 | |
2382 char * | |
2383 fload(int d) | |
2384 { | |
90 | 2385 return d?"lfd":"lfs"; |
89 | 2386 } |
2387 | |
95 | 2388 void |
138 | 2389 code_cmp_dregister(int e2,int d) |
95 | 2390 { |
99 | 2391 char *frn,*rrn,*grn; |
2392 int greg,r; | |
138 | 2393 grn = register_name(greg = get_dregister(d)); |
132 | 2394 frn = register_name(e2); |
99 | 2395 float_zero_lib_used=1; |
109 | 2396 r = get_ptr_cache(&float_zero); |
99 | 2397 rrn = register_name(r); |
108 | 2398 printf("\tlfs %s,0(%s)\n",grn,rrn); |
99 | 2399 printf("\tfcmpu cr0,%s,%s\n",grn,frn); |
147 | 2400 free_register(greg); |
99 | 2401 return; |
95 | 2402 } |
2403 | |
2404 void | |
138 | 2405 code_dregister(int e2,int freg,int d) |
95 | 2406 { |
147 | 2407 if (freg!=e2) { |
2408 if (is_int_reg(e2)) error(-1); | |
108 | 2409 printf("\tfmr %s,%s\n",fregister_name(freg),fregister_name(e2)); |
147 | 2410 } |
145 | 2411 regv[freg]=1; |
95 | 2412 } |
2413 | |
103 | 2414 void code_dassign_gvar(int e2,int freg,int d) |
89 | 2415 { |
90 | 2416 int r; |
109 | 2417 r = get_ptr_cache((NMTBL*)cadr(e2)); |
147 | 2418 if (!is_float_reg(freg)) error(-1); |
108 | 2419 printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(r)); |
145 | 2420 regv[freg]=1; |
89 | 2421 } |
2422 | |
103 | 2423 void code_dassign_lvar(int e2,int freg,int d) |
89 | 2424 { |
119 | 2425 lvar_intro(e2); |
147 | 2426 if (!is_float_reg(freg)) error(-1); |
119 | 2427 printf("\t%s %s,",fstore(d),fregister_name(freg)); |
2428 lvar(e2); | |
145 | 2429 regv[freg]=1; |
89 | 2430 } |
2431 | |
103 | 2432 void code_dassign(int e2,int freg,int d) |
89 | 2433 { |
147 | 2434 if (!is_float_reg(freg)) error(-1); |
108 | 2435 printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(e2)); |
145 | 2436 regv[freg]=1; |
89 | 2437 } |
2438 | |
111 | 2439 void |
138 | 2440 code_dassign_dregister(int e2,int d,int freg) { |
147 | 2441 if (e2!=freg) { |
2442 if (is_int_reg(freg)) error(-1); | |
111 | 2443 printf("\tfmr %s,%s\n",fregister_name(e2),fregister_name(freg)); |
147 | 2444 } |
111 | 2445 } |
2446 | |
89 | 2447 static double d0 = 1.0; |
2448 | |
2449 int | |
2450 code_d1(double d) | |
2451 { | |
2452 int *i = (int *)&d0; int *j = (int *)&d; | |
99 | 2453 return (i[1] == 0x3ff00000)?j[0]:j[1]; |
89 | 2454 } |
2455 | |
2456 int | |
2457 code_d2(double d) | |
2458 { | |
2459 int *i = (int *)&d0; int *j = (int *)&d; | |
99 | 2460 return (i[1] == 0x3ff00000)?j[1]:j[0]; |
89 | 2461 } |
2462 | |
133 | 2463 int |
2464 code_f(double d) | |
2465 { | |
2466 float f = d; | |
2467 int *j = (int *)&f; | |
2468 return *j; | |
2469 } | |
2470 | |
138 | 2471 void |
2472 code_dconst(int e2,int freg,int d) | |
89 | 2473 { |
2474 int lb; | |
138 | 2475 double value = dcadr(e2); |
90 | 2476 int r; |
97 | 2477 char *rrn,*frn; |
2478 frn = fregister_name(freg); | |
138 | 2479 if (value==0.0) { |
97 | 2480 float_zero_lib_used=1; |
109 | 2481 r = get_ptr_cache(&float_zero); |
98 | 2482 rrn = register_name(r); |
108 | 2483 printf("\tlfs %s,0(%s)\n",frn,rrn); |
97 | 2484 return; |
89 | 2485 } |
138 | 2486 if (value==1.0) { |
97 | 2487 float_one_lib_used=1; |
109 | 2488 r = get_ptr_cache(&float_one); |
98 | 2489 rrn = register_name(r); |
108 | 2490 printf("\tlfs %s,0(%s)\n",frn,rrn); |
97 | 2491 return; |
89 | 2492 } |
97 | 2493 rrn = register_name((r=get_register())); |
108 | 2494 printf(" \t.data\n\t.align 3\n"); |
89 | 2495 lb=fwdlabel(); |
102 | 2496 printf("L_%d:\n",lb); |
138 | 2497 if (d) { |
2498 printf("\t.long\t0x%x,0x%x\n",code_d2(value),code_d1(value)); | |
133 | 2499 } else { |
138 | 2500 printf("\t.long\t0x%x\n",code_f(value)); |
133 | 2501 } |
89 | 2502 if (output_mode==TEXT_EMIT_MODE) { |
2503 printf(".text\n"); | |
2504 } else { | |
2505 text_mode(); | |
2506 } | |
102 | 2507 printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",rrn,lb,code_base); |
2508 printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",rrn,lb,code_base,rrn); | |
138 | 2509 if (d) { |
133 | 2510 printf("\tlfd %s,0(%s)\n",frn,rrn); |
2511 } else { | |
2512 printf("\tlfs %s,0(%s)\n",frn,rrn); | |
2513 } | |
90 | 2514 free_register(r); |
145 | 2515 regv[freg]=1; |
89 | 2516 } |
2517 | |
133 | 2518 |
138 | 2519 void code_dneg(int freg,int d) |
89 | 2520 { |
97 | 2521 char *frn = fregister_name(freg); |
147 | 2522 if (is_int_reg(freg)) error(-1); |
95 | 2523 printf("\tfneg %s,%s\n",frn,frn); |
89 | 2524 } |
2525 | |
147 | 2526 void code_d2i(int freg0) |
89 | 2527 { |
147 | 2528 char *frn; |
2529 char *crn; | |
119 | 2530 int e2 = new_lvar(size_of_double); |
147 | 2531 |
2532 freg0 = use_double(freg0); | |
2533 frn = fregister_name(freg0); | |
2534 creg = use_int(creg); | |
2535 crn = register_name(creg); | |
2536 | |
2537 freg = freg0; | |
119 | 2538 free_lvar(e2); |
93 | 2539 printf("\tfctiwz %s,%s\n",frn,frn); |
119 | 2540 lvar_intro(e2); |
2541 printf("\tstfd %s,",frn); lvar(e2); | |
2542 lvar_intro(e2+size_of_double-size_of_int); | |
2543 printf("\tlwz %s,",crn); lvar(e2+size_of_double-size_of_int); | |
147 | 2544 regv[creg]=1; |
89 | 2545 } |
2546 | |
91 | 2547 static int i2d_lib_used=0; |
2548 static char *i2d_lib[] = { | |
2549 ".data", | |
108 | 2550 /* ".literal8", */ |
91 | 2551 " .align 3", |
2552 "__i2dLC0:", | |
2553 " .long 1127219200", | |
2554 " .long -2147483648", | |
2555 ".text", | |
2556 " .align 2", | |
111 | 2557 "i2d_:", |
91 | 2558 " mflr r0", |
2559 " bcl 20,31,__i2dL1$pb", | |
2560 "__i2dL1$pb:", | |
2561 " mflr r10", | |
2562 " mtlr r0", | |
2563 " xoris r3,r3,0x8000", | |
116 | 2564 " stw r3,-28(r30)", |
91 | 2565 " lis r0,0x4330", |
116 | 2566 " stw r0,-32(r30)", |
2567 " lfd f0,-32(r30)", | |
91 | 2568 " addis r9,r10,ha16(__i2dLC0-__i2dL1$pb)", |
2569 " lfd f1,lo16(__i2dLC0-__i2dL1$pb)(r9)", | |
2570 " fsub f1,f0,f1", | |
2571 " blr", | |
2572 0 | |
2573 }; | |
2574 | |
147 | 2575 void code_i2d(int creg0) |
89 | 2576 { |
95 | 2577 i2d_lib_used = 1; |
113 | 2578 clear_ptr_cache(); |
2579 code_save_stacks(); | |
104 | 2580 set_creg(RET_REGISTER,1); |
91 | 2581 printf("\tbl i2d_\n"); |
104 | 2582 set_freg(RET_FREGISTER,0); |
147 | 2583 regv[freg]=1; |
89 | 2584 } |
2585 | |
91 | 2586 static int d2u_lib_used=0; |
2587 static char *d2u_lib[] = { | |
108 | 2588 /* ".literal8", */ |
91 | 2589 " .align 3", |
2590 "__d2uLC0:", | |
2591 " .long 1105199104", | |
2592 " .long 0", | |
2593 ".text", | |
2594 " .align 2", | |
111 | 2595 "d2u_:", |
91 | 2596 " mflr r0", |
2597 " bcl 20,31,__d2uL1$pb", | |
2598 "__d2uL1$pb:", | |
2599 " mflr r10", | |
2600 " mtlr r0", | |
2601 " addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)", | |
2602 " lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)", | |
2603 " fcmpu cr0,f1,f0", | |
2604 " cror 2,1,2", | |
2605 " beq- cr0,__d2uL2", | |
2606 " fctiwz f0,f1", | |
116 | 2607 " stfd f0,-32(r30)", |
2608 " lwz r3,-28(r30)", | |
91 | 2609 " blr", |
2610 "__d2uL2:", | |
2611 " addis r9,r10,ha16(__d2uLC0-__d2uL1$pb)", | |
2612 " lfd f0,lo16(__d2uLC0-__d2uL1$pb)(r9)", | |
2613 " fsub f0,f1,f0", | |
2614 " fctiwz f0,f0", | |
116 | 2615 " stfd f0,-24(r30)", |
2616 " lwz r3,-20(r30)", | |
91 | 2617 " xoris r3,r3,0x8000", |
2618 " blr", | |
2619 0 | |
2620 }; | |
2621 | |
147 | 2622 void code_d2u(int freg0) |
89 | 2623 { |
113 | 2624 code_save_stacks(); |
2625 clear_ptr_cache(); | |
91 | 2626 d2u_lib_used=1; |
104 | 2627 set_freg(RET_FREGISTER,1); |
111 | 2628 printf("\tbl d2u_\n"); |
104 | 2629 set_creg(RET_REGISTER,0); |
147 | 2630 regv[freg]=1; |
91 | 2631 } |
90 | 2632 |
91 | 2633 static int u2d_lib_used=0; |
2634 static char *u2d_lib[] = { | |
2635 ".data", | |
108 | 2636 /* ".literal8", */ |
91 | 2637 " .align 3", |
2638 "__u2dLC1:", | |
2639 " .long 1127219200", | |
2640 " .long 0", | |
2641 ".text", | |
2642 " .align 2", | |
111 | 2643 "u2d_:", |
91 | 2644 " mflr r0", |
2645 " bcl 20,31,__u2dL2$pb", | |
2646 "__u2dL2$pb:", | |
2647 " mflr r10", | |
2648 " mtlr r0", | |
116 | 2649 " stw r3,-28(r30)", |
91 | 2650 " lis r0,0x4330", |
116 | 2651 " stw r0,-32(r30)", |
2652 " lfd f0,-32(r30)", | |
91 | 2653 " addis r9,r10,ha16(__u2dLC1-__u2dL2$pb)", |
2654 " lfd f1,lo16(__u2dLC1-__u2dL2$pb)(r9)", | |
2655 " fsub f1,f0,f1", | |
2656 " blr", | |
2657 0 | |
2658 }; | |
89 | 2659 |
147 | 2660 void code_u2d(int creg0) |
89 | 2661 { |
113 | 2662 code_save_stacks(); |
2663 clear_ptr_cache(); | |
147 | 2664 u2d_lib_used = 1; |
2665 set_creg(RET_REGISTER,1); | |
111 | 2666 printf("\tbl u2d_\n"); |
147 | 2667 set_freg(FREG_FREGISTER,0); |
2668 regv[freg]=1; | |
89 | 2669 } |
2670 | |
133 | 2671 void code_d2f(int freg) { } |
2672 void code_f2d(int freg) { } | |
147 | 2673 void code_f2i(int freg) { code_d2i(freg); } |
2674 void code_f2u(int freg) { code_d2u(freg); } | |
2675 void code_i2f(int creg) { code_i2d(creg); } | |
2676 void code_u2f(int creg) { code_u2d(creg); } | |
133 | 2677 |
107 | 2678 void code_drgvar(int e2,int d,int freg) |
89 | 2679 { |
91 | 2680 int r; |
109 | 2681 r = get_ptr_cache((NMTBL*)cadr(e2)); |
108 | 2682 printf("\t%s %s,0(%s)\n",fload(d),fregister_name(freg),register_name(r)); |
145 | 2683 regv[freg]=1; |
89 | 2684 } |
2685 | |
2686 | |
107 | 2687 void code_drlvar(int e2,int d,int freg) |
89 | 2688 { |
119 | 2689 lvar_intro(e2); |
2690 printf("\t%s %s,",fload(d),fregister_name(freg)); lvar(e2); | |
145 | 2691 regv[freg]=1; |
89 | 2692 } |
2693 | |
138 | 2694 void code_cmp_drgvar(int e2,int d) |
89 | 2695 { |
91 | 2696 int r; |
97 | 2697 char *frn=fregister_name(freg); |
138 | 2698 int g=get_dregister(d); |
97 | 2699 char *grn=fregister_name(g); |
109 | 2700 r = get_ptr_cache((NMTBL*)cadr(e2)); |
108 | 2701 printf("\t%s %s,0(%s)\n",fload(1),grn,register_name(r)); |
91 | 2702 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2703 free_register(g); |
145 | 2704 regv[freg]=0; |
89 | 2705 } |
2706 | |
138 | 2707 void code_cmp_drlvar(int e2,int d) |
89 | 2708 { |
97 | 2709 char *frn=fregister_name(freg); |
138 | 2710 int g=get_dregister(d); |
97 | 2711 char *grn=fregister_name(g); |
108 | 2712 |
119 | 2713 lvar_intro(e2); |
2714 printf("\t%s %s,",fload(1),grn); lvar(e2); | |
91 | 2715 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2716 free_register(g); |
145 | 2717 regv[freg]=0; |
89 | 2718 } |
2719 | |
2720 void dtosop(int op,int e1) | |
2721 { | |
127 | 2722 char *opn=""; |
97 | 2723 char *frn=fregister_name(freg); |
2724 char *grn=fregister_name(e1); | |
145 | 2725 regv[freg]=1; |
89 | 2726 switch(op) { |
133 | 2727 case FADD: |
91 | 2728 case DADD: opn="fadd"; break; |
133 | 2729 case FSUB: |
101 | 2730 case DSUB: opn="fsub"; break; |
133 | 2731 case FDIV: |
101 | 2732 case DDIV: opn="fdiv"; break; |
133 | 2733 case FMUL: |
101 | 2734 case DMUL: opn="fmul"; break; |
133 | 2735 case FCMP: |
91 | 2736 case DCMP: |
108 | 2737 printf("\tfcmpu cr0,%s,%s\n",frn,grn); |
147 | 2738 free_register(e1); |
101 | 2739 return; |
133 | 2740 case FCMPGE: |
91 | 2741 case DCMPGE: |
108 | 2742 printf("\tfcmpu cr7,%s,%s\n",frn,grn); |
147 | 2743 free_register(e1); |
101 | 2744 return; |
133 | 2745 default: |
2746 error(-1); return; | |
89 | 2747 } |
91 | 2748 printf("\t%s %s,%s,%s\n",opn,frn,frn,grn); |
145 | 2749 regv[freg]=1; |
147 | 2750 free_register(e1); |
89 | 2751 } |
2752 | |
2753 void | |
2754 code_dassop(int op,int d) { | |
91 | 2755 /* we have lvalue in creg, applied floating value is in freg */ |
97 | 2756 char *frn=fregister_name(freg); |
138 | 2757 int xreg=emit_dpop(d); |
91 | 2758 char *crn=register_name(creg); |
2759 | |
147 | 2760 if (!is_float_reg(freg)) error(-1); |
108 | 2761 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
102 | 2762 dtosop(op,xreg); |
108 | 2763 printf("\t%s %s,0(%s)\n",fstore(d),frn,crn); |
138 | 2764 emit_dpop_free(xreg,d); |
145 | 2765 regv[freg]=1; |
89 | 2766 } |
2767 | |
91 | 2768 |
89 | 2769 void |
107 | 2770 code_dpreinc(int e1,int e2,int d,int reg) { |
91 | 2771 char *frn; |
2772 char *crn; | |
2773 int g; | |
2774 char *grn,*drn; | |
2775 int r; | |
109 | 2776 r = get_ptr_cache(&float_one); |
96 | 2777 float_one_lib_used=1; |
91 | 2778 |
144 | 2779 if (car(e2)==DREGISTER||car(e2)==FREGISTER) { |
2780 error(-1); /* unspported now */ | |
2781 } | |
89 | 2782 g_expr(e2); |
91 | 2783 |
2784 crn=register_name(creg); | |
97 | 2785 frn=fregister_name(freg); |
91 | 2786 drn=register_name(r); |
138 | 2787 grn=fregister_name(g=get_dregister(d)); |
91 | 2788 |
108 | 2789 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
2790 printf("\tlfs %s,0(%s)\n",grn,drn); | |
89 | 2791 if (caddr(e1)>0) |
91 | 2792 printf("\tfadd %s,%s,%s\n",frn,frn,grn); |
89 | 2793 else |
91 | 2794 printf("\tfsub %s,%s,%s\n",frn,frn,grn); |
147 | 2795 if (!is_float_reg(freg)) error(-1); |
108 | 2796 printf("\t%s %s,0(%s)\n",fstore(d),frn,crn); |
147 | 2797 free_register(g); |
145 | 2798 regv[freg]=1; |
147 | 2799 regv[ireg]=0; |
2800 creg = freg; | |
89 | 2801 } |
2802 | |
2803 void | |
107 | 2804 code_dpostinc(int e1,int e2,int d,int reg) { |
91 | 2805 char *frn; |
2806 char *crn; | |
2807 int g; | |
2808 char *grn,*drn; | |
2809 int r; | |
109 | 2810 r = get_ptr_cache(&float_one); |
96 | 2811 float_one_lib_used=1; |
91 | 2812 |
144 | 2813 if (car(e2)==DREGISTER||car(e2)==FREGISTER) { |
2814 error(-1); /* unspported now */ | |
2815 } | |
89 | 2816 g_expr(e2); |
91 | 2817 |
2818 crn=register_name(creg); | |
97 | 2819 frn=fregister_name(freg); |
91 | 2820 drn=register_name(r); |
138 | 2821 grn=fregister_name(g=get_dregister(d)); |
91 | 2822 |
108 | 2823 printf("\t%s %s,0(%s)\n",fload(d),frn,crn); |
2824 printf("\tlfs %s,0(%s)\n",grn,drn); | |
89 | 2825 if (caddr(e1)>0) |
91 | 2826 printf("\tfadd %s,%s,%s\n",grn,frn,grn); |
89 | 2827 else |
91 | 2828 printf("\tfsub %s,%s,%s\n",grn,frn,grn); |
147 | 2829 if (!is_float_reg(freg)) error(-1); |
108 | 2830 printf("\t%s %s,0(%s)\n",fstore(d),grn,crn); |
147 | 2831 free_register(g); |
145 | 2832 regv[freg]=1; |
147 | 2833 regv[ireg]=0; |
2834 creg = freg; | |
89 | 2835 } |
2836 | |
2837 void | |
2838 drexpr(int e1, int e2,int l1, int op) | |
2839 { | |
91 | 2840 g_expr(list3(((op==DOP+GE)?DCMPGE:DCMP),e1,e2)); |
89 | 2841 switch(op) { |
2842 case DOP+GE: | |
133 | 2843 case FOP+GE: |
91 | 2844 printf("\tcror 2,29,30\n"); |
111 | 2845 printf("\tbne\tcr0,L_%d\n",l1); |
89 | 2846 break; |
2847 case DOP+GT: | |
133 | 2848 case FOP+GT: |
111 | 2849 printf("\tble\tcr0,L_%d\n",l1); |
89 | 2850 break; |
2851 case DOP+EQ: | |
133 | 2852 case FOP+EQ: |
111 | 2853 printf("\tbne\tcr0,L_%d\n",l1); |
89 | 2854 break; |
2855 case DOP+NEQ: | |
133 | 2856 case FOP+NEQ: |
111 | 2857 printf("\tbeq\tcr0,L_%d\n",l1); |
89 | 2858 break; |
2859 } | |
2860 } | |
2861 | |
138 | 2862 int emit_dpop(int d) |
89 | 2863 { |
114 | 2864 int xreg,reg; |
97 | 2865 xreg=pop_fregister(); |
2866 if (xreg<= -REG_LVAR_OFFSET) { | |
138 | 2867 reg = get_dregister(d); |
119 | 2868 code_drlvar(REG_LVAR_OFFSET+xreg,1,reg); |
117 | 2869 free_lvar(REG_LVAR_OFFSET+xreg); |
145 | 2870 regv[reg]=1; xreg=reg; |
96 | 2871 } |
2872 return xreg; | |
89 | 2873 } |
2874 | |
138 | 2875 void emit_dpop_free(int e1,int d) |
89 | 2876 { |
147 | 2877 free_register(e1); |
89 | 2878 } |
2879 | |
138 | 2880 void emit_dpush(int d) |
89 | 2881 { |
97 | 2882 int new_reg; |
102 | 2883 if (freg_sp>MAX_MAX) error(-1); |
138 | 2884 new_reg = get_dregister(1); |
97 | 2885 freg_stack[freg_sp++] = freg; /* push するかわりにレジスタを使う */ |
147 | 2886 creg = freg = new_reg; |
145 | 2887 regv[freg]=1; |
89 | 2888 } |
2889 | |
195 | 2890 #endif |
2891 | |
2892 #if LONGLONG_CODE | |
2893 | |
2894 | |
2895 /* 64bit int part */ | |
2896 | |
2897 void lrexpr(int e1, int e2,int l1, int op) | |
2898 { | |
2899 } | |
2900 | |
2901 int lpop_register() | |
2902 { | |
2903 return 0; | |
2904 } | |
2905 | |
2906 int emit_lpop() | |
2907 { | |
2908 return 0; | |
2909 } | |
2910 | |
2911 void code_lregister(int e2,int reg) | |
2912 { | |
2913 | |
2914 } | |
2915 | |
2916 void code_cmp_lregister(int reg) | |
2917 { | |
2918 | |
2919 } | |
2920 | |
2921 void code_cmp_lrgvar(int e1,int e2) | |
2922 { | |
2923 | |
2924 } | |
2925 | |
2926 void code_cmp_lrlvar(int e1,int e2) | |
2927 { | |
2928 | |
2929 } | |
2930 | |
2931 void code_lassign(int e1,int e2) | |
2932 { | |
2933 | |
2934 } | |
2935 | |
2936 void code_lassign_gvar(int e1,int e2) | |
2937 { | |
2938 | |
2939 } | |
2940 | |
2941 void code_lassign_lvar(int e1,int e2) | |
2942 { | |
2943 | |
2944 } | |
2945 | |
2946 void code_lassign_lregister(int e2,int reg) | |
2947 { | |
2948 | |
2949 } | |
2950 | |
2951 void code_lconst(int e1,int e2) | |
2952 { | |
2953 | |
2954 } | |
2955 | |
2956 void code_lneg(int e1,int e2) | |
2957 { | |
2958 | |
2959 } | |
2960 | |
2961 void code_lrgvar(int e1,int e2) | |
2962 { | |
2963 | |
2964 } | |
2965 | |
2966 void code_lrlvar(int e1,int e2) | |
2967 { | |
2968 | |
2969 } | |
2970 | |
2971 void ltosop(int e1,int e2) | |
2972 { | |
2973 | |
2974 } | |
2975 | |
2976 void emit_lpop_free(int e1) | |
2977 { | |
2978 | |
2979 } | |
2980 | |
2981 void emit_lpush() | |
2982 { | |
2983 | |
2984 } | |
2985 | |
2986 void code_i2ll(int creg) | |
2987 { | |
2988 | |
2989 } | |
2990 | |
2991 void code_i2ull(int creg) | |
2992 { | |
2993 | |
2994 } | |
2995 | |
2996 void code_u2ll(int creg) | |
2997 { | |
2998 | |
2999 } | |
3000 | |
3001 void code_u2ull(int creg) | |
3002 { | |
3003 | |
3004 } | |
3005 | |
3006 void code_ll2i(int creg) | |
3007 { | |
3008 | |
3009 } | |
3010 | |
3011 void code_ll2u(int creg) | |
3012 { | |
3013 | |
3014 } | |
3015 | |
3016 void code_ull2i(int creg) | |
3017 { | |
3018 | |
3019 } | |
3020 | |
3021 void code_ull2u(int creg) | |
3022 { | |
3023 | |
3024 } | |
3025 | |
3026 #if FLOAT_CODE | |
3027 void code_d2ll(int creg) | |
3028 { | |
3029 | |
3030 } | |
3031 | |
3032 void code_d2ull(int creg) | |
3033 { | |
3034 | |
3035 } | |
3036 | |
3037 void code_f2ll(int creg) | |
3038 { | |
3039 | |
3040 } | |
3041 | |
3042 void code_f2ull(int creg) | |
3043 { | |
3044 | |
3045 } | |
3046 | |
3047 void code_ll2d(int creg) | |
3048 { | |
3049 | |
3050 } | |
3051 | |
3052 void code_ll2f(int creg) | |
3053 { | |
3054 | |
3055 } | |
3056 | |
3057 void code_ull2d(int creg) | |
3058 { | |
3059 | |
3060 } | |
3061 | |
3062 void code_ull2f(int creg) | |
3063 { | |
3064 | |
3065 } | |
3066 | |
3067 void code_ull2ll(int creg) | |
3068 { | |
3069 | |
3070 } | |
3071 | |
3072 void code_ull2ull(int creg) | |
3073 { | |
3074 | |
3075 } | |
3076 | |
3077 #endif | |
3078 | |
3079 | |
3080 void code_lpreinc(int e1,int e2,int reg) | |
3081 { | |
3082 | |
3083 } | |
3084 | |
3085 void code_lpostinc(int e1,int e2,int reg) | |
3086 { | |
3087 | |
3088 } | |
3089 | |
3090 void code_lassop(int op) | |
3091 { | |
3092 | |
3093 } | |
3094 | |
3095 | |
3096 #endif | |
3097 | |
95 | 3098 void |
3099 code_save_stacks() | |
3100 { | |
103 | 3101 int i,reg; |
101 | 3102 for(i=0;i<reg_sp;i++) { |
103 | 3103 if ((reg=reg_stack[i])>=0) { |
101 | 3104 code_assign_lvar( |
119 | 3105 (reg_stack[i]=new_lvar(size_of_int)),reg,0); |
101 | 3106 reg_stack[i]= reg_stack[i]-REG_LVAR_OFFSET; |
3107 } | |
3108 } | |
195 | 3109 #if FLOAT_CODE |
101 | 3110 for(i=0;i<freg_sp;i++) { |
103 | 3111 if ((reg=freg_stack[i])>=0) { |
101 | 3112 code_dassign_lvar( |
119 | 3113 (freg_stack[i]=new_lvar(size_of_double)),reg,1); |
101 | 3114 freg_stack[i]= freg_stack[i]-REG_LVAR_OFFSET; |
3115 } | |
3116 } | |
195 | 3117 #endif |
96 | 3118 } |
3119 | |
3120 void | |
3121 emit_lib(char *p[]) | |
95 | 3122 { |
98 | 3123 while(*p) { |
3124 printf("%s\n",*p++); | |
96 | 3125 } |
3126 } | |
3127 | |
3128 void | |
3129 code_closing() | |
3130 { | |
195 | 3131 #if FLOAT_CODE |
96 | 3132 if (d2u_lib_used) emit_lib(d2u_lib); |
3133 if (u2d_lib_used) emit_lib(u2d_lib); | |
3134 if (float_one_lib_used) emit_lib(float_one_lib); | |
98 | 3135 if (float_zero_lib_used) emit_lib(float_zero_lib); |
96 | 3136 if (i2d_lib_used) emit_lib(i2d_lib); |
195 | 3137 #endif |
97 | 3138 global_table(); |
108 | 3139 /* printf("\t.ident \"Micro-C compiled\"\n"); */ |
95 | 3140 } |
3141 | |
89 | 3142 /* end */ |
3143 |