comparison mc-nop-386.c @ 13:a8a812dace23

struct local initialization
author kono
date Mon, 17 Jan 2000 00:43:06 +0900
parents f928bd330351
children 77c0710a8729
comparison
equal deleted inserted replaced
12:f928bd330351 13:a8a812dace23
120 120
121 #define DATA_REG 0 121 #define DATA_REG 0
122 #define POINTER_REG 3 122 #define POINTER_REG 3
123 static char *reg_name[8]; 123 static char *reg_name[8];
124 static char *reg_name_l[4]; 124 static char *reg_name_l[4];
125 static char *reg_name_w[4];
125 126
126 /* 127 /*
127 creg currrent virtual register 128 creg currrent virtual register
128 lreg operand virtual register 129 lreg operand virtual register
129 dreg spare virtual register 130 dreg spare virtual register
176 reg_name[REG_ESP] = "%esp"; 177 reg_name[REG_ESP] = "%esp";
177 reg_name_l[REG_EAX] = "%al"; 178 reg_name_l[REG_EAX] = "%al";
178 reg_name_l[REG_EBX] = "%bl"; 179 reg_name_l[REG_EBX] = "%bl";
179 reg_name_l[REG_ECX] = "%cl"; 180 reg_name_l[REG_ECX] = "%cl";
180 reg_name_l[REG_EDX] = "%dl"; 181 reg_name_l[REG_EDX] = "%dl";
182 reg_name_w[REG_EAX] = "%ax";
183 reg_name_w[REG_EBX] = "%bx";
184 reg_name_w[REG_ECX] = "%cx";
185 reg_name_w[REG_EDX] = "%dx";
181 186
182 } 187 }
183 188
184 char * 189 char *
185 register_name(int i) 190 register_name(int i)
728 } 733 }
729 printf("\tlea _%d,%s\n",lb,crn); 734 printf("\tlea _%d,%s\n",lb,crn);
730 } 735 }
731 } 736 }
732 737
738 #define MAX_COPY_LEN 20
739
740 void
741 emit_copy(char *from,char *to,int length,int offset)
742 {
743 if (length<0) return;
744 switch (length) {
745 case 0: break;
746 case 1:
747 printf("\tmovb %d(%s),%s\n",offset,from, reg_name_l[rname[dreg]] );
748 printf("\tmovb %s,%d(%s)\n",reg_name_l[rname[dreg]] ,offset,to);
749 break;
750 case 2:
751 printf("\tmovw %d(%s),%s\n",offset,from, reg_name_w[rname[dreg]] );
752 printf("\tmovw %s,%d(%s)\n",reg_name_w[rname[dreg]] ,offset,to);
753 break;
754 case 4:
755 printf("\tmovl %d(%s),%s\n",offset,from, drn);
756 printf("\tmovl %s,%d(%s)\n",drn, offset,to);
757 break;
758 default:
759 if (length <=MAX_COPY_LEN) {
760 for(;length>4;length-=4,offset+=4);
761 emit_copy(from,to,4,offset);
762 for(;length>2;length-=2,offset+=2);
763 emit_copy(from,to,2,offset);
764 if(length>0)
765 emit_copy(from,to,length,offset);
766 return;
767 }
768 printf("\texch %%edi,%s\n",from);
769 printf("\texch %%esi,%s\n",to);
770 printf("\texch %%ecx,%s\n",drn);
771 printf("\tmovl $%d,%%ecx\n",length);
772 printf("\tcld\n\trep\n\tmovsl\n");
773 printf("\texch %%ecx,%s\n",drn);
774 printf("\texch %%esi,%s\n",to);
775 printf("\texch %%edi,%s\n",from);
776 }
777 }
778
779 int
780 struct_push(int e4,int t)
781 {
782 int e5;
783 g_expr(e4);
784 e5=size(t);
785 if(e5%size_of_int) {
786 e5 += size_of_int - (e5%size_of_int);
787 }
788 printf("\tsubl $%d,%%esp\n",e5);
789 if (e5<=MAX_COPY_LEN)
790 emit_copy(crn,"%esp",e5,0);
791 else {
792 printf("\tpushl %%edi\n");
793 printf("\tpushl %%esi\n");
794 printf("\tpushl %%ecx\n");
795 printf("\tleal 12(%%esp),%%di\n");
796 if (rname[creg]!=REG_ESI)
797 printf("\tmovl %s,%%esi\n",crn);
798 printf("\tmovl $%d,%%ecx\n",e5);
799 printf("\tcld\n\trep\n\tmovsl\n");
800 printf("\tpopl %%ecx\n");
801 printf("\tpopl %%esi\n");
802 printf("\tpopl %%edi\n");
803 }
804 return e5/size_of_int;
805 }
806
733 void 807 void
734 function(int e1) 808 function(int e1)
735 { 809 {
736 int e2,e3,e4,e5,nargs; 810 int e2,e3,e4,e5,nargs,t;
737 NMTBL *n; 811 NMTBL *n;
738 812
739 e2 = cadr(e1); 813 e2 = cadr(e1);
740 nargs = 0; 814 nargs = 0;
741 for (e3 = caddr(e1); e3; e3 = cadr(e3)) { 815 for (e3 = caddr(e1); e3; e3 = cadr(e3)) {
816 t=caddr(e3);
742 n=(NMTBL *)(e5=(cadr(e4 = car(e3)))); 817 n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
743 switch(car(e4)) { 818 switch(car(e4)) {
744 case FNAME: 819 case FNAME:
745 printf("\tlea %s,%s\n",n->nm,crn); 820 printf("\tlea %s,%s\n",n->nm,crn);
746 printf("\tpushl %s\n",crn); 821 printf("\tpushl %s\n",crn);
747 break; 822 break;
748 case ADDRESS: 823 case ADDRESS:
749 g_expr(e5); 824 g_expr(e5);
750 printf("\tpushl %s\n",crn); 825 printf("\tpushl %s\n",crn);
751 break; 826 break;
752 default:g_expr(e4); 827 default:
753 printf("\tpushl %s\n",crn); 828 if(scalar(t)) {
829 g_expr(e4);
830 printf("\tpushl %s\n",crn);
831 } else if (car(t)==STRUCT||car(t)==UNION) {
832 nargs += struct_push(e4,t);
833 continue;
834 } else {
835 error(TYERR);
836 }
754 } 837 }
755 ++nargs; 838 ++nargs;
756 } 839 }
757 if (car(e2) == FNAME) { 840 if (car(e2) == FNAME) {
758 n=(NMTBL *)cadr(e2); 841 n=(NMTBL *)cadr(e2);
952 sz = size(cadr(e2)); /* cld rep movsb */ 1035 sz = size(cadr(e2)); /* cld rep movsb */
953 g_expr(e4); 1036 g_expr(e4);
954 emit_push(); 1037 emit_push();
955 g_expr(e2); 1038 g_expr(e2);
956 xrn = emit_pop(0); 1039 xrn = emit_pop(0);
1040 emit_copy(crn,xrn,sz,0);
957 return; 1041 return;
958 } 1042 }
959 1043
960 void 1044 void
961 assign(int e1) 1045 assign(int e1)
1404 emit_data(int e, int t, NMTBL *n) 1488 emit_data(int e, int t, NMTBL *n)
1405 { 1489 {
1406 int l; 1490 int l;
1407 char *name; 1491 char *name;
1408 name = n->nm; 1492 name = n->nm;
1409 if(mode==GDECL) { /* global */ 1493 if(mode!=GDECL) {
1410 if (n->dsp != -1) { 1494 error(-1); return;
1411 n->dsp = -1; /* initiallized flag */ 1495 }
1412 printf(".globl\t%s\n",name); 1496 if (n->dsp != -1) {
1413 data_mode(name); 1497 n->dsp = -1; /* initiallized flag */
1414 align(t); 1498 printf(".globl\t%s\n",name);
1415 printf("%s:\n",name); 1499 data_mode(name);
1500 align(t);
1501 printf("%s:\n",name);
1502 } else {
1503 data_mode(0);
1504 }
1505 if(car(e)==CONST) {
1506 if (t==CHAR) {
1507 printf("\t.byte %d\n",cadr(e));
1508 if (data_alignment>0)
1509 data_alignment++;
1510 gpc += 1;
1416 } else { 1511 } else {
1417 data_mode(0); 1512 printf("\t.long %d\n",cadr(e));
1513 gpc += size_of_int;
1418 } 1514 }
1419 if(car(e)==CONST) { 1515 } else if(t!=CHAR) {
1420 if (t==CHAR) { 1516 gpc += size_of_int;
1421 printf("\t.byte %d\n",cadr(e)); 1517 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) {
1422 if (data_alignment>0) 1518 printf("\t.long %s\n",caddr(cadr(e)));
1423 data_alignment++; 1519 } else if(car(e)==FNAME) {
1424 gpc += 1; 1520 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm);
1425 } else { 1521 } else if(car(e)==STRING) {
1426 printf("\t.long %d\n",cadr(e)); 1522 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
1427 gpc += size_of_int; 1523 l = fwdlabel();
1524 printf("\t.long _%d\n",l);
1525 printf(".section\t.rodata\n");
1526 printf("_%d:\n",l);
1527 output_mode = RODATA_EMIT_MODE;
1428 } 1528 }
1429 } else if(t!=CHAR) { 1529 ascii((char *)cadr(e));
1430 gpc += size_of_int; 1530 } else error(TYERR);
1431 if(car(e)==ADDRESS&&car(cadr(e))==GVAR) {
1432 printf("\t.long %s\n",caddr(cadr(e)));
1433 } else if(car(e)==FNAME) {
1434 printf("\t.long %s\n",((NMTBL *)cadr(e))->nm);
1435 } else if(car(e)==STRING) {
1436 if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
1437 l = fwdlabel();
1438 printf("\t.long _%d\n",l);
1439 printf(".section\t.rodata\n");
1440 printf("_%d:\n",l);
1441 output_mode = RODATA_EMIT_MODE;
1442 }
1443 ascii((char *)cadr(e));
1444 } else error(TYERR);
1445 }
1446 } else {
1447 /* if (n->sc!=LVAR) { error(TYERR); return; } */
1448 if(car(e)==CONST) {
1449 printf("\tmovl $%d,%s\n",cadr(e),crn);
1450 printf("\t%s %s,%d(%%ebp)\n",t==CHAR?"movsbl":"movl",crn,n->dsp);
1451 } else if(t!=CHAR) {
1452 if(car(e)==ADDRESS&&car(cadr(e))==GVAR)
1453 printf("\tlea %s,%s\n",caddr(e),crn);
1454 else if(car(e)==FNAME)
1455 printf("\tlea %s,%s\n",((NMTBL *)cadr(e))->nm,crn);
1456 else if(car(e)==STRING) {
1457 string(e);
1458 } else error(TYERR);
1459 printf("\tmovl %s,%d(%%ebp)\n",crn,n->dsp);
1460 }
1461 } 1531 }
1462 } 1532 }
1463 1533
1464 void 1534 void
1465 emit_data_closing(NMTBL *n) 1535 emit_data_closing(NMTBL *n)