comparison mc-codegen.c @ 138:e6e77af048a1 float-double

float/dobule configuration
author kono
date Tue, 06 May 2003 02:31:46 +0900
parents 9fb09db54436
children 742224f4da02
comparison
equal deleted inserted replaced
137:9fb09db54436 138:e6e77af048a1
5 #include "mc.h" 5 #include "mc.h"
6 #include "mc-codegen.h" 6 #include "mc-codegen.h"
7 #include "mc-code.h" 7 #include "mc-code.h"
8 8
9 int creg; /* current register */ 9 int creg; /* current register */
10 int reg_sp; /* REGister Stack-Pointer */
11 int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */
12
13 /* floating point registers */
14
15 int freg; /* current floating point register */ 10 int freg; /* current floating point register */
16 int freg_sp; /* floating point REGister Stack-Pointer */
17 int freg_stack[MAX_MAX]; /* 実際のレジスタの領域 */
18 11
19 int use; /* generated value will be used */ 12 int use; /* generated value will be used */
20 13
21 /* 14 /*
22 creg currrent virtual register 15 creg currrent virtual register
23 regs[] virtual register usage
24 regv[] value in virtual register flag
25 freg current floating point register 16 freg current floating point register
26 fregv calue in floating point register 17 fregv calue in floating point register
27 */ 18 */
28 19
29 static void remove0(int *parent,int e) ; 20 static void remove0(int *parent,int e) ;
105 return ADDRESS; 96 return ADDRESS;
106 case REGISTER: 97 case REGISTER:
107 code_register(e2,creg); 98 code_register(e2,creg);
108 return INT; 99 return INT;
109 case DREGISTER: 100 case DREGISTER:
110 code_fregister(e2,freg); 101 code_dregister(e2,freg,1);
111 return DOUBLE; 102 return DOUBLE;
112 case FREGISTER: 103 case FREGISTER:
113 code_fregister(e2,freg); 104 code_dregister(e2,freg,0);
114 return FLOAT; 105 return FLOAT;
115 case RLVAR: 106 case RLVAR:
116 code_rlvar(e2,creg); 107 code_rlvar(e2,creg);
117 return INT; 108 return INT;
118 case CRLVAR: 109 case CRLVAR:
135 return ADDRESS; 126 return ADDRESS;
136 case CONST: /* 代入する値が0でも特別な処理はしない */ 127 case CONST: /* 代入する値が0でも特別な処理はしない */
137 code_const(e2,creg); 128 code_const(e2,creg);
138 return INT; 129 return INT;
139 case DCONST: 130 case DCONST:
140 code_dconst(e1,freg); 131 code_dconst(e1,freg,1);
141 return DOUBLE; 132 return DOUBLE;
142 case FCONST: 133 case FCONST:
143 code_fconst(e1,freg); 134 code_dconst(e1,freg,0);
144 return FLOAT; 135 return FLOAT;
145 case STRING: 136 case STRING:
146 code_string(e1,creg); 137 code_string(e1,creg);
147 return ADDRESS; 138 return ADDRESS;
148 case FUNCTION: 139 case FUNCTION:
155 return g_expr0(e2); 146 return g_expr0(e2);
156 case RINDIRECT: case CRINDIRECT: 147 case RINDIRECT: case CRINDIRECT:
157 case DRINDIRECT: case FRINDIRECT: 148 case DRINDIRECT: case FRINDIRECT:
158 return rindirect(e1); 149 return rindirect(e1);
159 case ADDRESS: 150 case ADDRESS:
160 if (car(e2)==REGISTER||car(e2)==DREGISTER) 151 if (car(e2)==REGISTER||car(e2)==DREGISTER||car(e2)==FREGISTER)
161 return register_to_lvar(e2); 152 return register_to_lvar(e2); /* too late? */
162 else 153 else
163 return g_expr0(e2); 154 return g_expr0(e2);
164 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */ 155 case MINUS: /* レジスタに対し、neglを実行すれば実現可能 */
165 g_expr0(e2); code_neg(creg); 156 g_expr0(e2); code_neg(creg);
166 return INT; 157 return INT;
167 case DMINUS: 158 case DMINUS:
168 g_expr0(e2); code_dneg(freg); 159 g_expr0(e2); code_dneg(freg,1);
169 return DOUBLE; 160 return DOUBLE;
170 case FMINUS: 161 case FMINUS:
171 g_expr0(e2); code_fneg(freg); 162 g_expr0(e2); code_dneg(freg,0);
172 return FLOAT; 163 return FLOAT;
173 case CONV: 164 case CONV:
174 g_expr0(e2); 165 g_expr0(e2);
175 switch(caddr(e1)) { 166 switch(caddr(e1)) {
176 case I2D: code_i2d(creg,freg); return DOUBLE; 167 case I2D: code_i2d(creg,freg); return DOUBLE;
253 code_set_fixed_creg(1,t); 244 code_set_fixed_creg(1,t);
254 fwddef(e3); 245 fwddef(e3);
255 return t; 246 return t;
256 case DCOND: 247 case DCOND:
257 case FCOND: 248 case FCOND:
258 d = car(e1)==DCOND?DOUBLE:FLOAT; 249 d = (car(e1)==DCOND?DOUBLE:FLOAT);
259 e2=fwdlabel(); 250 e2=fwdlabel();
260 b_expr(cadr(e1),0,e2,0); 251 b_expr(cadr(e1),0,e2,0);
261 code_set_fixed_creg(0,d); 252 code_set_fixed_creg(0,d);
262 t = g_expr0(caddr(e1)); 253 t = g_expr0(caddr(e1));
263 code_set_fixed_creg(1,t); 254 code_set_fixed_creg(1,t);
452 case RLVAR: 443 case RLVAR:
453 code_cmp_rlvar(e2); 444 code_cmp_rlvar(e2);
454 jcond(l1,cond); 445 jcond(l1,cond);
455 return; 446 return;
456 case DRLVAR: 447 case DRLVAR:
457 code_cmp_drlvar(e2); 448 code_cmp_drlvar(e2,1);
458 jcond(l1,cond); 449 jcond(l1,cond);
459 return; 450 return;
451 case FRLVAR:
452 code_cmp_drlvar(e2,0);
453 jcond(l1,cond);
454 return;
460 case DRGVAR: 455 case DRGVAR:
461 code_cmp_drgvar(e2); 456 code_cmp_drgvar(e2,1);
457 jcond(l1,cond);
458 return;
459 case FRGVAR:
460 code_cmp_drgvar(e2,0);
462 jcond(l1,cond); 461 jcond(l1,cond);
463 return; 462 return;
464 case REGISTER: 463 case REGISTER:
465 code_cmp_register(e2); 464 code_cmp_register(e2);
466 jcond(l1,cond); 465 jcond(l1,cond);
467 return; 466 return;
467 case FREGISTER:
468 code_cmp_dregister(e2,0);
469 jcond(l1,cond);
470 return;
468 case DREGISTER: 471 case DREGISTER:
469 code_cmp_fregister(e2); 472 code_cmp_dregister(e2,1);
470 jcond(l1,cond); 473 jcond(l1,cond);
471 return; 474 return;
472 case CONST: 475 case CONST:
473 if(control&&((cond&&e2)||(!cond&&!e2))) jmp(l1); 476 if(control&&((cond&&e2)||(!cond&&!e2))) jmp(l1);
474 return; 477 return;
475 case DCONST: 478 case DCONST:
479 case FCONST:
476 if(control&&((dcadr(e2)!=0.0)^cond)) jmp(l1); 480 if(control&&((dcadr(e2)!=0.0)^cond)) jmp(l1);
477 return; 481 return;
478 default: 482 default:
479 if(err) { 483 if(err) {
480 error(-1); return; /* recursive g_expr/b_expr */ 484 error(-1); return; /* recursive g_expr/b_expr */
481 } 485 }
482 t=g_expr(e1); 486 t=g_expr(e1);
483 if(t==FLOAT||t==DOUBLE) 487 if(t==FLOAT)
484 code_cmp_fregister(freg); 488 code_cmp_dregister(freg,0);
489 else if(t==DOUBLE)
490 code_cmp_dregister(freg,1);
485 else 491 else
486 code_cmp_register(creg); 492 code_cmp_register(creg);
487 jcond(l1,cond); 493 jcond(l1,cond);
488 return; 494 return;
489 } 495 }
517 int tag = car(e); 523 int tag = car(e);
518 int lvar; 524 int lvar;
519 int t; 525 int t;
520 if (!n||n==&null_nptr) error(REG_ERR); 526 if (!n||n==&null_nptr) error(REG_ERR);
521 if (tag==REGISTER) { 527 if (tag==REGISTER) {
522 /* regs[reg]==INPUT_REG case should be considered */
523 n->dsp = new_lvar(size_of_int); 528 n->dsp = new_lvar(size_of_int);
524 t = INT; 529 t = INT;
525 } else if (tag==DREGISTER) { 530 } else if (tag==DREGISTER) {
526 /* fregs[reg]==INPUT_REG case should be considered */
527 n->dsp = new_lvar(size_of_double); 531 n->dsp = new_lvar(size_of_double);
532 t = DOUBLE;
533 } else if (tag==FREGISTER) {
534 n->dsp = new_lvar(size_of_float);
528 t = DOUBLE; 535 t = DOUBLE;
529 } else error(-1); 536 } else error(-1);
530 n->sc = LVAR; 537 n->sc = LVAR;
531 lvar = list2(LVAR,n->dsp); 538 lvar = list2(LVAR,n->dsp);
532 g_expr_u(assign_expr0(list2(LVAR,n->dsp),list3(tag,reg,(int)n),t,t)); 539 g_expr_u(assign_expr0(list2(LVAR,n->dsp),list3(tag,reg,(int)n),t,t));
533 if (tag==REGISTER) { 540 if (tag==REGISTER) {
534 free_register(reg); 541 free_register(reg);
535 } else if (tag==DREGISTER) { 542 } else if (tag==DREGISTER) {
536 free_fregister(reg); 543 free_dregister(reg,1);
544 } else if (tag==FREGISTER) {
545 free_dregister(reg,0);
537 } 546 }
538 return g_expr0(lvar); 547 return g_expr0(lvar);
539 #endif 548 #endif
540 } 549 }
541 550
576 int use0=*use; 585 int use0=*use;
577 while(use0) { 586 while(use0) {
578 if (car(use0)==t) { 587 if (car(use0)==t) {
579 if (car(caddr(use0))==REGISTER) 588 if (car(caddr(use0))==REGISTER)
580 free_register(cadr(caddr(use0))); 589 free_register(cadr(caddr(use0)));
590 else if (car(caddr(use0))==FREGISTER)
591 free_dregister(cadr(caddr(use0)),0);
581 else if (car(caddr(use0))==DREGISTER) 592 else if (car(caddr(use0))==DREGISTER)
582 free_fregister(cadr(caddr(use0))); 593 free_dregister(cadr(caddr(use0)),1);
583 break; 594 break;
584 } 595 }
585 use0 = cadr(use0); 596 use0 = cadr(use0);
586 } 597 }
587 remove0(target,t); 598 remove0(target,t);
595 if (sz==size_of_int && (e1=get_register())!=-1) { 606 if (sz==size_of_int && (e1=get_register())!=-1) {
596 e1=list3(REGISTER,e1,0); 607 e1=list3(REGISTER,e1,0);
597 *use=list3(t,*use,e1); 608 *use=list3(t,*use,e1);
598 g_expr_u(assign_expr0(e1,s,ty,ty)); 609 g_expr_u(assign_expr0(e1,s,ty,ty));
599 *target = append4(*target,t,ty,e1); 610 *target = append4(*target,t,ty,e1);
600 } else if (sz==size_of_double && (e1=get_fregister())!=-1) { 611 } else if (sz==size_of_double && (e1=get_dregister(1))!=-1) {
601 e1=list3(DREGISTER,e1,0); 612 e1=list3(DREGISTER,e1,0);
613 *use=list3(t,*use,e1);
614 g_expr_u(assign_expr0(e1,s,ty,ty));
615 *target = append4(*target,t,ty,e1);
616 } else if (sz==size_of_float && (e1=get_dregister(0))!=-1) {
617 e1=list3(FREGISTER,e1,0);
602 *use=list3(t,*use,e1); 618 *use=list3(t,*use,e1);
603 g_expr_u(assign_expr0(e1,s,ty,ty)); 619 g_expr_u(assign_expr0(e1,s,ty,ty));
604 *target = append4(*target,t,ty,e1); 620 *target = append4(*target,t,ty,e1);
605 } else { 621 } else {
606 g_expr_u(assign_expr0((e1=list2(LVAR,new_lvar(sz))),s,ty,ty)); 622 g_expr_u(assign_expr0((e1=list2(LVAR,new_lvar(sz))),s,ty,ty));
703 int 719 int
704 is_simple(int e1) 720 is_simple(int e1)
705 { 721 {
706 return ( 722 return (
707 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER ||e1==DREGISTER || 723 e1==CONST || e1==FNAME || e1==LVAR || e1==REGISTER ||e1==DREGISTER ||
724 e1==FREGISTER ||
708 e1==GVAR || e1==RGVAR || e1==RLVAR || e1==CRLVAR || e1==CRGVAR || 725 e1==GVAR || e1==RGVAR || e1==RLVAR || e1==CRLVAR || e1==CRGVAR ||
709 e1==DRLVAR || e1==FRLVAR 726 e1==DRLVAR || e1==FRLVAR
710 ); 727 );
711 } 728 }
712 729
728 { 745 {
729 int ce1=car(e1); 746 int ce1=car(e1);
730 return ( 747 return (
731 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || ce1==DRLVAR || 748 ce1==LVAR ||ce1==RLVAR||ce1==CRLVAR || ce1==DRLVAR ||
732 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || ce1==DRGVAR || 749 ce1==GVAR ||ce1==RGVAR||ce1==CRGVAR || ce1==DRGVAR ||
733 ce1==REGISTER|| ce1==DREGISTER 750 ce1==REGISTER|| ce1==DREGISTER || ce1==FREGISTER
734 ); 751 );
735 } 752 }
736 753
737 754
738 void 755 void
739 jump(int e1, int env) 756 jump(int e1, int env)
740 { 757 {
741 int e2,e3,e4,sz,arg_size,ty,regs,fregs; 758 int e2,e3,e4,sz,arg_size,ty,regs,fregs;
742 int t0,s0; 759 int t0,s0,r;
743 NMTBL *code0 = 0; 760 NMTBL *code0 = 0;
744 int target = 0; 761 int target = 0;
745 int source = 0; 762 int source = 0;
746 int processing = 0; 763 int processing = 0;
747 int use = 0; 764 int use = 0;
750 767
751 arg_size = 0; regs = 0; 768 arg_size = 0; regs = 0;
752 fregs = 0; 769 fregs = 0;
753 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) { 770 for (e3 = reverse0(caddr(e1)); e3; e3 = cadr(e3)) {
754 e2 = car(e3); sz = size(ty=caddr(e3)); 771 e2 = car(e3); sz = size(ty=caddr(e3));
755 if (scalar(ty) && 772 if (scalar(ty) && (r = get_input_register_var(regs,0,1))) {
756 regs < MAX_CODE_INPUT_REGISTER_VAR) { 773 target=list4(r,target,ty,e2); regs++;
757 target=list4(get_input_register_var(regs++,0,1), 774 } else if (ty==FLOAT && (r = get_input_dregister_var(fregs,0,1,0))) {
758 target,ty,e2); 775 target=list4(r, target,ty,e2); fregs++;
759 } else if ((ty==DOUBLE||ty==FLOAT) && 776 } else if (ty==DOUBLE && (r = get_input_dregister_var(fregs,0,1,1))) {
760 fregs < MAX_CODE_INPUT_DREGISTER_VAR) { 777 target=list4(r, target,ty,e2); fregs++;
761 target=list4(get_input_fregister_var(fregs++,0,1),
762 target,ty,e2);
763 } else { 778 } else {
764 target=list4(list2(LVAR,0), target,ty,e2); 779 target=list4(list2(LVAR,0), target,ty,e2);
765 } 780 }
766 /* keep arg space for register variables */ 781 /* keep arg space for register variables */
767 arg_size += sz; 782 arg_size += sz;
831 /* 並列代入を実行 */ 846 /* 並列代入を実行 */
832 parallel_assign(&target,&source,&processing,&use); 847 parallel_assign(&target,&source,&processing,&use);
833 while (use) { 848 while (use) {
834 if (car(caddr(use))==REGISTER) 849 if (car(caddr(use))==REGISTER)
835 free_register(cadr(caddr(use))); 850 free_register(cadr(caddr(use)));
851 else if (car(caddr(use))==FREGISTER)
852 free_dregister(cadr(caddr(use)),0);
836 else if (car(caddr(use))==DREGISTER) 853 else if (car(caddr(use))==DREGISTER)
837 free_fregister(cadr(caddr(use))); 854 free_dregister(cadr(caddr(use)),1);
838 else if (car(caddr(use))==LVAR) 855 else if (car(caddr(use))==LVAR)
839 free_lvar(cadr(caddr(use))); 856 free_lvar(cadr(caddr(use)));
840 use=cadr(use); 857 use=cadr(use);
841 } 858 }
842 if(target) error(-1); 859 if(target) error(-1);
893 e3 = caddr(e1); 910 e3 = caddr(e1);
894 g_expr(e3); 911 g_expr(e3);
895 emit_dpush(d); 912 emit_dpush(d);
896 g_expr(e2); 913 g_expr(e2);
897 dtosop(car(e1),(e2=emit_dpop(d))); 914 dtosop(car(e1),(e2=emit_dpop(d)));
898 emit_dpop_free(e2); 915 emit_dpop_free(e2,d);
899 fregv[freg]=1; 916 fregv[freg]=1;
900 return; 917 return;
901 } 918 }
902 919
903 void 920 void
1013 void 1030 void
1014 dassign_opt(int e5,int e2,int e4,int d) 1031 dassign_opt(int e5,int e2,int e4,int d)
1015 { 1032 {
1016 int reg; 1033 int reg;
1017 /* e2=e4 */ 1034 /* e2=e4 */
1018 if (e5==DREGISTER) { 1035 if (e5==DREGISTER||e5==FREGISTER) {
1019 reg = cadr(e4); 1036 reg = cadr(e4);
1020 switch(car(e2)) { 1037 switch(car(e2)) {
1021 case GVAR: /* i=3 */ 1038 case GVAR: /* i=3 */
1022 code_dassign_gvar(e2,reg,d); 1039 code_dassign_gvar(e2,reg,d);
1023 return; 1040 return;
1024 case LVAR: 1041 case LVAR:
1025 code_dassign_lvar(cadr(e2),reg,d); 1042 code_dassign_lvar(cadr(e2),reg,d);
1026 return; 1043 return;
1027 case DREGISTER: 1044 case DREGISTER:
1045 case FREGISTER:
1028 if (reg!=cadr(e2)) 1046 if (reg!=cadr(e2))
1029 code_dassign_fregister(cadr(e2),d,reg); 1047 code_dassign_dregister(cadr(e2),d,reg);
1030 return; 1048 return;
1031 default: 1049 default:
1032 error(-1); 1050 error(-1);
1033 } 1051 }
1034 g_expr(e2); 1052 g_expr(e2);
1035 code_dassign(e2,reg,d); 1053 code_dassign(e2,reg,d);
1036 return; 1054 return;
1037 } 1055 }
1038 /* e2 is register now */ 1056 /* e2 is register now */
1039 if (car(e2)!=DREGISTER) error(-1); 1057 if (car(e2)!=DREGISTER && car(e2)!=FREGISTER) error(-1);
1040 reg = cadr(e2); 1058 reg = cadr(e2);
1041 switch(e5) { 1059 switch(e5) {
1042 case FRGVAR: 1060 case FRGVAR:
1043 case DRGVAR: code_drgvar(e4,d,reg); return; 1061 case DRGVAR: code_drgvar(e4,d,reg); return;
1044 case FRLVAR: 1062 case FRLVAR:
1045 case DRLVAR: code_drlvar(cadr(e4),d,reg); return; 1063 case DRLVAR: code_drlvar(cadr(e4),d,reg); return;
1046 case FCONST: 1064 case FCONST:
1047 case DCONST: code_dconst(e4,reg); return; 1065 case DCONST: code_dconst(e4,reg,1); return;
1048 default: 1066 default:
1049 error(-1); 1067 error(-1);
1050 } 1068 }
1051 } 1069 }
1052 1070
1059 e2 = cadr(e1); 1077 e2 = cadr(e1);
1060 e3 = cadr(e2); 1078 e3 = cadr(e2);
1061 e4 = caddr(e1); e5=car(e4); 1079 e4 = caddr(e1); e5=car(e4);
1062 d = (car(e1)==LASS)?2:(car(e1)==DASS)?1:0; 1080 d = (car(e1)==LASS)?2:(car(e1)==DASS)?1:0;
1063 if (!use && ( 1081 if (!use && (
1064 (e5==DREGISTER) || 1082 (e5==DREGISTER) || (e5==FREGISTER) ||
1065 (car(e2)==DREGISTER&&(e5==DRGVAR||e5==DRLVAR||e5==DCONST))|| 1083 (car(e2)==DREGISTER&&(e5==DRGVAR||e5==DRLVAR||e5==DCONST))||
1066 (car(e2)==DREGISTER&&(e5==FRGVAR||e5==FRLVAR||e5==FCONST)) 1084 (car(e2)==DREGISTER&&(e5==FRGVAR||e5==FRLVAR||e5==FCONST))
1067 )) { 1085 )) {
1068 dassign_opt(e5,e2,e4,d); 1086 dassign_opt(e5,e2,e4,d);
1069 return; 1087 return;
1076 case LVAR: 1094 case LVAR:
1077 g_expr(e4); 1095 g_expr(e4);
1078 code_dassign_lvar(cadr(e2),freg,d); 1096 code_dassign_lvar(cadr(e2),freg,d);
1079 return; 1097 return;
1080 case DREGISTER: 1098 case DREGISTER:
1099 case FREGISTER:
1081 g_expr(e4); 1100 g_expr(e4);
1082 if (freg!=cadr(e2)) 1101 if (freg!=cadr(e2))
1083 code_dassign_fregister(cadr(e2),d,freg); 1102 code_dassign_dregister(cadr(e2),d,freg);
1084 return; 1103 return;
1085 } 1104 }
1086 g_expr(e2); 1105 g_expr(e2);
1087 emit_push(); 1106 emit_push();
1088 g_expr(e4); 1107 g_expr(e4);
1273 if (contains(caddr(e),type)) return 1; 1292 if (contains(caddr(e),type)) return 1;
1274 e = cadddr(e); 1293 e = cadddr(e);
1275 continue; 1294 continue;
1276 /* nullary operators */ 1295 /* nullary operators */
1277 case GVAR: case RGVAR: case CRGVAR: case LVAR: 1296 case GVAR: case RGVAR: case CRGVAR: case LVAR:
1278 case REGISTER: case DREGISTER: 1297 case REGISTER: case DREGISTER: case FREGISTER:
1279 case RLVAR: case CRLVAR: case FRLVAR: case FRGVAR: 1298 case RLVAR: case CRLVAR: case FRLVAR: case FRGVAR:
1280 case DRLVAR: case DRGVAR: 1299 case DRLVAR: case DRGVAR:
1281 case FNAME: case CONST: case DCONST: case FCONST: case STRING: 1300 case FNAME: case CONST: case DCONST: case FCONST: case STRING:
1282 case RETURN: case ENVIRONMENT: 1301 case RETURN: case ENVIRONMENT:
1283 return 0; 1302 return 0;