• R/O
  • SSH

CbC: Commit

micro-C base Continuation based C


Commit MetaInfo

Revision8106ea9cf185974b9500553d724e421f5f6304b2 (tree)
Zeit2006-10-11 18:45:41
Autorshinji_kono
Commitershinji_kono

Log Message

Intel Mac support

Ändern Zusammenfassung

Diff

diff -r ce2364964f1e -r 8106ea9cf185 .gdbinit.i386mac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.gdbinit.i386mac Wed Oct 11 18:45:41 2006 +0900
@@ -0,0 +1,17 @@
1+tb main
2+define regs
3+call (void)printf("eax=%08x ebx=%08x ecx=%08x edx=%08x\nesi=%08x edi=%08x ebp=%08x esp=%08x eip=%08x\n",$eax,$ebx,$ecx,$edx,$esi,$edi,$ebp,$esp,$eip)
4+end
5+define si
6+stepi
7+regs
8+x/1i $eip
9+end
10+define ni
11+nexti
12+regs
13+x/1i $eip
14+end
15+b errmsg
16+r -s test/code-gen-all.c
17+# r -s mc-code-powerpc.c
diff -r ce2364964f1e -r 8106ea9cf185 README
--- a/README Wed Sep 27 16:28:22 2006 +0900
+++ b/README Wed Oct 11 18:45:41 2006 +0900
@@ -124,6 +124,7 @@
124124 // Long is equal to an int and a pointer (32bit).
125125
126126 Only Mac OS X and Red hat Linux is supported.
127+Intel Mac is supported.
127128
128129 Inline directive is ignored and gives normal function definition.
129130
diff -r ce2364964f1e -r 8106ea9cf185 README.jp
--- a/README.jp Wed Sep 27 16:28:22 2006 +0900
+++ b/README.jp Wed Oct 11 18:45:41 2006 +0900
@@ -132,6 +132,7 @@
132132 // long ¤Ï32bit. int ¤âpointer¤âƱ¤¸¡£
133133
134134 Mac OS X ¤È Red hat Linux ¤À¤±¤Ç¥Æ¥¹¥È¤·¤Æ¤¢¤ê¤Þ¤¹¡£
135+Intel Mac ¤Ç¤âÆ°ºî¤·¤Þ¤¹¡£
135136
136137 // Inline ¤Ï̵»ë¤µ¤ì¡¢ÉáÄ̤Îstatic´Ø¿ô¤Ë¥³¥ó¥Ñ¥¤¥ë¤µ¤ì¤Þ¤¹¡£
137138
diff -r ce2364964f1e -r 8106ea9cf185 mc-code-ia32.c
--- a/mc-code-ia32.c Wed Sep 27 16:28:22 2006 +0900
+++ b/mc-code-ia32.c Wed Oct 11 18:45:41 2006 +0900
@@ -29,6 +29,11 @@
2929 #include "mc-codegen.h"
3030 #include "mc-code.h"
3131
32+#ifdef __APPLE__
33+#define USE_SSE2
34+#define USE_PIC
35+#endif
36+
3237 #if defined(__GNUC__) && __GNUC__ >= 4
3338
3439 #include "mc-include.c"
@@ -125,6 +130,7 @@
125130 #ifdef __APPLE__
126131 "#define __APPLE__ 1\n"
127132 "#define __GNUC__ 4\n"
133+"#define __BIG_ENDIAN__ 1\n"
128134 #endif
129135 ;
130136
@@ -266,6 +272,7 @@
266272 see enter/enter1/leave see code_enter
267273 */
268274 static int arg_offset;
275+
269276 int disp_offset = -12;
270277 #define func_disp_offset (-12)
271278 #define code_disp_offset (-12)
@@ -273,7 +280,9 @@
273280 static int code_disp_label;
274281 static int func_disp_label;
275282
276-// static int goffset_label;
283+#ifdef __APPLE__
284+static int goffset_label;
285+#endif
277286
278287 static int
279288 lvar(int l)
@@ -383,7 +392,7 @@
383392 return i;
384393 }
385394
386-#define is_data_reg(reg) (1<=reg&&reg<=4)
395+#define is_data_reg(reg) (REG_EAX<=reg&&reg<=REG_EDX)
387396 #define is_pointer_reg(reg) (REG_ESI<=reg&&reg<=REG_EBP)
388397
389398 static int
@@ -405,17 +414,26 @@
405414 }
406415
407416 #define use_data_reg(reg,keep) \
408- if (reg==-1||!is_data_reg(reg)) reg=use_data_reg0(keep)
417+ if (reg==-1||!is_data_reg(reg)) reg=use_data_reg0(keep,reg)
409418
410419 int
411-use_data_reg0(int keep)
420+use_data_reg0(int keep,int reg)
412421 {
413- int i = creg;
414422 int ptreg =0;
423+ int i;
415424 if (is_pointer_reg(creg)) {
416- free_register(ptreg=creg); creg = 0;
425+ free_register(ptreg=creg);
426+ ireg = creg = 0;
417427 }
418- if (!i||!ireg||!is_int_reg(i)) {
428+ if (is_pointer_reg(ireg)) {
429+ free_register(ireg);
430+ ireg = 0;
431+ }
432+ i = reg==USING_REG?creg:reg;
433+#ifdef __APPLE__
434+ if (regs[i]==PTRC_REG) clear_ptr_cache_reg(i);
435+#endif
436+ if (!i||!ireg||!is_data_reg(i)) {
419437 if (lreg) { if (regs[lreg]) free_register(lreg); lreg = 0; }
420438 if (!ireg) {
421439 ireg = get_data_register();
@@ -423,7 +441,7 @@
423441 i = ireg;
424442 }
425443 if (!regs[i]) regs[i]=USING_REG;
426- creg = i;
444+ creg = ireg = i;
427445 if (ptreg && keep) {
428446 printf("\tmovl %s,%s\n",reg_name[ptreg],reg_name[creg]);
429447 }
@@ -434,8 +452,11 @@
434452 set_ireg(int reg,int mode)
435453 {
436454 if (!is_int_reg(reg)) error(-1);
437- if (reg!=creg) {
438- // clear_ptr_cache_reg(reg);
455+ if (creg > 0 && reg!=creg) {
456+#ifdef __APPLE__
457+ if (regs[reg]==PTRC_REG)
458+ clear_ptr_cache_reg(reg);
459+#endif
439460 if (ireg && reg!=ireg ) {
440461 if (regs[ireg]!=REG_VAR) free_register(ireg);
441462 if (mode) {
@@ -461,6 +482,9 @@
461482 if (!lreg||!regs[lreg]) {
462483 // long long mode use all registers
463484 code_save_stacks();
485+#ifdef __APPLE__
486+ clear_ptr_cache();
487+#endif
464488 }
465489 i = lreg = (reg==USE_CREG)?REG_LCREG:reg;
466490 if (!regs[i]) regs[i]=USING_REG;
@@ -529,6 +553,9 @@
529553 {
530554 /* called for each file */
531555 output_mode = -1;
556+#ifdef __APPLE__
557+ init_ptr_cache();
558+#endif
532559 }
533560
534561
@@ -572,6 +599,14 @@
572599 return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */
573600 }
574601 }
602+#ifdef __APPLE__
603+ /* PTR_CACHE ¤ò¤Ä¤Ö¤¹ */
604+ if ((i=last_ptr_cache())) {
605+ clear_ptr_cache_reg(i);
606+ regs[i]=USING_REG; /* ¤½¤Î¥ì¥¸¥¹¥¿¤ò»È¤¦¤³¤È¤òÀë¸À¤· */
607+ return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */
608+ }
609+#endif
575610 /* search register stack */
576611 for(i=0;i<reg_sp;i++) {
577612 if ((reg=reg_stack[i])>=0) {
@@ -589,12 +624,22 @@
589624 get_data_register(void)
590625 { /* »È¤ï¤ì¤Æ¤¤¤Ê¤¤¥ì¥¸¥¹¥¿¤òÄ´¤Ù¤ë */
591626 int i,reg,j;
592- for(i=1;i<=REG_EDX;i++) {
627+ for(i=REG_EAX;i<=REG_EDX;i++) {
593628 if (! regs[i]) { /* »È¤ï¤ì¤Æ¤¤¤Ê¤¤¤Ê¤é */
594629 regs[i]=1; /* ¤½¤Î¥ì¥¸¥¹¥¿¤ò»È¤¦¤³¤È¤òÀë¸À¤· */
595630 return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */
596631 }
597632 }
633+#ifdef __APPLE__
634+ /* PTR_CACHE ¤ò¤Ä¤Ö¤¹ */
635+ while ((i=last_ptr_cache())) {
636+ clear_ptr_cache_reg(i);
637+ if (is_data_reg(i)) {
638+ regs[i]=USING_REG; /* ¤½¤Î¥ì¥¸¥¹¥¿¤ò»È¤¦¤³¤È¤òÀë¸À¤· */
639+ return i; /* ¤½¤Î¾ì½ê¤òɽ¤¹ÈÖ¹æ¤òÊÖ¤¹ */
640+ }
641+ }
642+#endif
598643 /* search register stack */
599644 for(i=0;i<reg_sp;i++) {
600645 if (is_data_reg(i) && (reg=reg_stack[i])>=0) {
@@ -623,13 +668,29 @@
623668 extern void
624669 use_ptr_cache(int r)
625670 {
671+#ifdef __APPLE__
672+ regs[r]=PTRC_REG;
673+#else
626674 error(-1);
675+#endif
627676 }
628677
629678 extern void
630679 code_ptr_cache_def(int r,NMTBL *nptr)
631680 {
681+#ifdef __APPLE__
682+ char *rrn = register_name(r,0);
683+ if (nptr->sc==STATIC && !(is_code(nptr)||is_function(nptr))) {
684+ printf("\tleal _%s-_%d(%%ebx),%s\n",nptr->nm,
685+ goffset_label,rrn);
686+ } else {
687+ printf("\tmovl L_%s$non_lazy_ptr-_%d(%%ebx),%s\n",
688+ nptr->nm,
689+ goffset_label,rrn);
690+ }
691+#else
632692 error(-1);
693+#endif
633694 }
634695
635696 /*
@@ -900,6 +961,9 @@
900961 void
901962 code_label(int labelno)
902963 {
964+#ifdef __APPLE__
965+ clear_ptr_cache();
966+#endif
903967 printf("_%d:\n",labelno);
904968 }
905969
@@ -907,11 +971,12 @@
907971 code_gvar(int e1,int creg) {
908972 use_int(creg);
909973 #ifdef __APPLE__
974+ int r = get_ptr_cache((NMTBL*)caddr(e1));
910975 if (cadr(e1)) {
911- printf("\tmovl $_%s+%d,%s\n",((NMTBL*)caddr(e1))->nm,cadr(e1),
976+ printf("\tleal %d(%s),%s\n", cadr(e1),register_name(r,0),
912977 register_name(creg,0));
913978 } else {
914- printf("\tmovl $_%s,%s\n",((NMTBL*)caddr(e1))->nm,register_name(creg,0));
979+ printf("\tmovl %s,%s\n", register_name(r,0), register_name(creg,0));
915980 }
916981 #else
917982 if (cadr(e1)) {
@@ -928,11 +993,13 @@
928993 code_rgvar(int e1,int creg) {
929994 use_int(creg);
930995 #ifdef __APPLE__
996+ int r = get_ptr_cache((NMTBL*)caddr(e1));
931997 if (cadr(e1)) {
932- printf("\tmovl _%s+%d,%s\n",((NMTBL*)caddr(e1))->nm,cadr(e1),
933- register_name(creg,0));
934- } else
935- printf("\tmovl _%s,%s\n",((NMTBL*)caddr(e1))->nm,register_name(creg,0));
998+ printf("\tmovl %d(%s),%s\n", cadr(e1),register_name(r,0),
999+ register_name(creg,0));
1000+ } else {
1001+ printf("\tmovl (%s),%s\n", register_name(r,0), register_name(creg,0));
1002+ }
9361003 #else
9371004 if (cadr(e1)) {
9381005 printf("\tmovl %s+%d,%s\n",((NMTBL*)caddr(e1))->nm,cadr(e1),
@@ -953,12 +1020,14 @@
9531020 code_crgvar(int e1,int creg,int sign,int sz){
9541021 use_int(creg);
9551022 #ifdef __APPLE__
1023+ int r = get_ptr_cache((NMTBL*)caddr(e1));
9561024 if (cadr(e1)) {
957- printf("\t%s _%s+%d,%s\n",cload(sign,sz),
958- ((NMTBL*)caddr(e1))->nm,cadr(e1),register_name(creg,0));
959- } else
960- printf("\t%s _%s,%s\n",cload(sign,sz),
961- ((NMTBL*)caddr(e1))->nm,register_name(creg,0));
1025+ printf("\t%s %d(%s),%s\n", cload(sign,sz),cadr(e1),register_name(r,0),
1026+ register_name(creg,0));
1027+ } else {
1028+ printf("\t%s (%s),%s\n", cload(sign,sz),
1029+ register_name(r,0), register_name(creg,0));
1030+ }
9621031 #else
9631032 if (cadr(e1)) {
9641033 printf("\t%s %s+%d,%s\n",cload(sign,sz),
@@ -1036,7 +1105,13 @@
10361105 code_fname(NMTBL *n,int creg) {
10371106 use_int(creg);
10381107 #ifdef __APPLE__
1039- printf("\tmovl $_%s,%s\n",n->nm,register_name(creg,0));
1108+ if (n->sc==STATIC) {
1109+ printf("\tleal _%s-_%d(%%ebx),%s\n", n->nm, goffset_label,
1110+ register_name(creg,0));
1111+ return;
1112+ }
1113+ int r = get_ptr_cache(n);
1114+ printf("\tmovl %s,%s\n", register_name(r,0), register_name(creg,0));
10401115 #else
10411116 printf("\tmovl $%s,%s\n",n->nm,register_name(creg,0));
10421117 #endif
@@ -1045,7 +1120,12 @@
10451120 void
10461121 code_label_value(int label,int reg) {
10471122 use_int(reg);
1123+#ifdef __APPLE__
1124+ printf("\tleal _%d-_%d(%%ebx),%s\n",
1125+ label,goffset_label,register_name(reg,0));
1126+#else
10481127 printf("\tleal _%d,%s\n",label,register_name(reg,0));
1128+#endif
10491129 }
10501130
10511131 void
@@ -1190,16 +1270,17 @@
11901270 code_cmp_crgvar(int e1,int reg,int sz,int label,int cond) {
11911271 use_int(reg);
11921272 #ifdef __APPLE__
1273+ int r = get_ptr_cache((NMTBL*)caddr(e1));
11931274 if (cadr(e1)) {
11941275 if (sz==1)
1195- printf("\tcmpb $0,_%s+%d\n",((NMTBL*)caddr(e1))->nm,cadr(e1));
1276+ printf("\tcmpb $0,%d(%s)\n",cadr(e1),register_name(r,0));
11961277 else if (sz==SIZE_OF_SHORT)
1197- printf("\tcmpw $0,_%s+%d\n",((NMTBL*)caddr(e1))->nm,cadr(e1));
1278+ printf("\tcmpw $0,%d(%s)\n",cadr(e1),register_name(r,0));
11981279 } else {
11991280 if (sz==1)
1200- printf("\tcmpb $0,_%s\n",((NMTBL*)caddr(e1))->nm);
1281+ printf("\tcmpb $0,(%s)\n",register_name(r,0));
12011282 else if (sz==SIZE_OF_SHORT)
1202- printf("\tcmpw $0,_%s\n",((NMTBL*)caddr(e1))->nm);
1283+ printf("\tcmpw $0,(%s)\n",register_name(r,0));
12031284 }
12041285 #else
12051286 if (cadr(e1)) {
@@ -1232,11 +1313,12 @@
12321313 void
12331314 code_cmp_rgvar(int e1,int reg,int label,int cond) {
12341315 use_int(reg);
1235-#ifdef __APPLE
1316+#ifdef __APPLE__
1317+ int r = get_ptr_cache((NMTBL*)caddr(e1));
12361318 if (cadr(e1))
1237- printf("\tcmpl $0,_%s+%d\n",((NMTBL*)caddr(e1))->nm,cadr(e1));
1319+ printf("\tcmpl $0,%d(%s)\n",cadr(e1),register_name(r,0));
12381320 else
1239- printf("\tcmpl $0,_%s\n",((NMTBL*)caddr(e1))->nm);
1321+ printf("\tcmpl $0,(%s)\n",register_name(r,0));
12401322 #else
12411323 if (cadr(e1))
12421324 printf("\tcmpl $0,%s+%d\n",((NMTBL*)caddr(e1))->nm,cadr(e1));
@@ -1283,7 +1365,13 @@
12831365 } else {
12841366 text_mode(0);
12851367 }
1368+#ifdef __APPLE__
1369+ printf("\tleal _%d-_%d(%%ebx),%s\n",lb,
1370+ goffset_label,
1371+ register_name(creg,0));
1372+#else
12861373 printf("\tlea _%d,%s\n",lb,register_name(creg,0));
1374+#endif
12871375 set_attr(n,LABEL,lb);
12881376 }
12891377
@@ -1342,6 +1430,8 @@
13421430 break;
13431431 }
13441432 // should be parallel_rassign....
1433+ // clear_ptr_cache();
1434+ // code_save_stacks();
13451435
13461436 printf("\tpushl %%esi\n");
13471437 printf("\tpushl %%edi\n");
@@ -1405,6 +1495,7 @@
14051495 else {
14061496 printf("\tpushl %d(%s)\n",
14071497 length-SIZE_OF_INT,register_name(creg,0));
1498+ stack_depth += SIZE_OF_INT;
14081499 }
14091500 }
14101501 // alignment may remain
@@ -1412,7 +1503,7 @@
14121503 /* downward direction copy */
14131504 emit_copy(creg,REG_ESP,length,0,0,1);
14141505 /* we have value in creg, it may be changed */
1415- stack_depth += length*SIZE_OF_INT;
1506+ stack_depth += length;
14161507 return length/SIZE_OF_INT;
14171508 }
14181509
@@ -1433,6 +1524,39 @@
14331524 #endif
14341525
14351526 e2 = cadr(e1);
1527+#ifdef __APPLE__
1528+ // esp must align 16byte for mmx operation
1529+ nargs = 0;
1530+ int length;
1531+ for (e3 = caddr(e1); e3; e3 = cadr(e3)) {
1532+ t=caddr(e3);
1533+ if(scalar(t)) {
1534+ } else if (t==LONGLONG||t==ULONGLONG) {
1535+ ++nargs;
1536+ } else if (t==DOUBLE) {
1537+ nargs += SIZE_OF_DOUBLE/SIZE_OF_INT;
1538+ continue;
1539+ } else if (t==FLOAT) {
1540+ nargs += SIZE_OF_FLOAT/SIZE_OF_INT;
1541+ continue;
1542+ } else if (car(t)==STRUCT||car(t)==UNION) {
1543+ // struct must align 16 (but how?)
1544+ length = size(t);
1545+ if (length%SIZE_OF_INT)
1546+ length += SIZE_OF_INT - (length%SIZE_OF_INT);
1547+ nargs += length/SIZE_OF_INT;
1548+ continue;
1549+ } else {
1550+ error(TYERR);
1551+ }
1552+ ++nargs;
1553+ }
1554+ length = 16-((nargs*4+stack_depth)%16);
1555+ if (length) {
1556+ stack_depth += length;
1557+ printf("\tleal %d(%%esp),%%esp\n",-length);
1558+ }
1559+#endif
14361560 nargs = 0;
14371561 for (e3 = caddr(e1); e3; e3 = cadr(e3)) {
14381562 t=caddr(e3);
@@ -1489,7 +1613,9 @@
14891613 g_expr(e2);
14901614 use_register(creg,REG_EAX,1); /* will be destroyed */
14911615 }
1492-
1616+#ifdef __APPLE__
1617+ clear_ptr_cache();
1618+#endif
14931619 if (car(e2) == FNAME) {
14941620 #ifdef __APPLE__
14951621 printf("\tcall\tL_%s$stub\n",n->nm);
@@ -1499,7 +1625,8 @@
14991625 } else {
15001626 printf("\tcall\t*%s\n",register_name(REG_EAX,0));
15011627 }
1502- if (nargs) printf("\taddl $%d,%%esp\n",SIZE_OF_INT*nargs);
1628+ if (stack_depth!=stack_depth_save)
1629+ printf("\taddl $%d,%%esp\n",stack_depth-stack_depth_save);
15031630 if (ret_type==DOUBLE||ret_type==FLOAT) {
15041631 } else if (ret_type==LONGLONG||ret_type==ULONGLONG) {
15051632 use_longlong0(USE_CREG);
@@ -1529,8 +1656,8 @@
15291656 printf("\tmovl %%esp,%s\n",drn);
15301657 printf("\tsubl %s,%%esp\n",crn);
15311658 printf("\tmovl %%esp,%s\n",crn);
1532- free_register(edx);
15331659 emit_copy(edx,creg,stack_depth,0,1,1);
1660+ free_register(edx); // should be free before emit copy but...
15341661 printf("\taddl $%d,%s\n",stack_depth,register_name(creg,0));
15351662
15361663 } else {
@@ -1633,10 +1760,13 @@
16331760 if (byte) { use_data_reg(creg,1);
16341761 } else { use_int(creg); }
16351762 #ifdef __APPLE__
1763+ int r = get_ptr_cache((NMTBL*)caddr(e2));
16361764 if (cadr(e2))
1637- printf("\t%s %s,_%s+%d\n",move(byte),register_name(creg,byte),((NMTBL*)caddr(e2))->nm,cadr(e2));
1765+ printf("\t%s %s,%d(%s)\n",move(byte),register_name(creg,byte),
1766+ cadr(e2),register_name(r,0));
16381767 else
1639- printf("\t%s %s,_%s\n",move(byte),register_name(creg,byte),((NMTBL*)caddr(e2))->nm);
1768+ printf("\t%s %s,(%s)\n",move(byte),register_name(creg,byte),
1769+ register_name(r,0));
16401770 #else
16411771 if (cadr(e2))
16421772 printf("\t%s %s,%s+%d\n",move(byte),register_name(creg,byte),((NMTBL*)caddr(e2))->nm,cadr(e2));
@@ -1769,6 +1899,10 @@
17691899 case UDIV:
17701900 case MOD:
17711901 case UMOD:
1902+#ifdef __APPLE__
1903+ if (regs[REG_EDX]==PTRC_REG)
1904+ clear_ptr_cache_reg(REG_EDX);
1905+#endif
17721906 use_register(reg,REG_EAX,1);
17731907 if (oreg==REG_EAX) oreg=reg;
17741908 if (oreg==REG_EDX) {
@@ -1993,6 +2127,7 @@
19932127 if (stmode!=STATIC)
19942128 printf(".globl _%s\n",name);
19952129 printf("_%s:\n",name);
2130+ clear_ptr_cache();
19962131 #endif
19972132 }
19982133
@@ -2004,12 +2139,24 @@
20042139 printf("\tlea _%d(%%ebp),%%esp\n",code_disp_label);
20052140
20062141 printf("## args %d disp %d code_disp_offset=%d\n",args,disp,code_disp_offset);
2142+#ifdef __APPLE__
2143+ printf("\tcall\t___i686.get_pc_thunk.bx\n");
2144+ printf("_%d:\n",labelno);
2145+ goffset_label = labelno;
2146+ labelno++;
2147+ regs[REG_EBX] = 1;
2148+#endif
20072149 }
20082150
20092151 void
20102152 code_leave(char *name)
20112153 {
2154+#ifdef __APPLE__
2155+ disp &= -(SIZE_OF_INT*4); // 16byte alignment
2156+ disp -= 4;
2157+#else
20122158 disp &= -SIZE_OF_INT;
2159+#endif
20132160 printf("\t.set _%d,%d\n",code_disp_label,disp+code_disp_offset-8);
20142161 #ifndef __APPLE__
20152162 printf("_%d:\n",labelno);
@@ -2045,12 +2192,12 @@
20452192 func_disp_label=fwdlabel();
20462193 printf("\tlea _%d(%%ebp),%%esp\n",func_disp_label);
20472194 #ifdef __APPLE__
2048-#if 0
2049- printf("\tcalli\t___i686.get_pc_thunk.cx\n");
2195+ printf("\tcall\t___i686.get_pc_thunk.bx\n");
20502196 printf("_%d:\n",labelno);
20512197 goffset_label = labelno;
20522198 labelno++;
2053-#endif
2199+ regs[REG_EBX] = 1;
2200+ clear_ptr_cache();
20542201 #endif
20552202
20562203 control=1;
@@ -2080,7 +2227,12 @@
20802227 {
20812228 int sz;
20822229
2230+#ifdef __APPLE__
2231+ disp &= -(SIZE_OF_INT*4); // 16byte alignment
2232+ disp -= 12;
2233+#else
20832234 disp &= -SIZE_OF_INT;
2235+#endif
20842236 if (control)
20852237 code_set_return_register(1);
20862238 if (retcont) {
@@ -2382,6 +2534,7 @@
23822534 }
23832535 #endif
23842536
2537+#ifndef __APPLE__
23852538 void
23862539 global_table(void)
23872540 {
@@ -2398,30 +2551,78 @@
23982551 data_mode(0);
23992552 init=1;
24002553 }
2401-#ifdef __APPLE__
2402- printf(".comm _%s,%d\n",n->nm,size(n->ty));
2403-#else
24042554 printf(".comm %s,%d\n",n->nm,size(n->ty));
2405-#endif
24062555 // .lcomm?
24072556 }
24082557 }
2409-#ifdef __APPLE__
2558+}
2559+
2560+#else
2561+
2562+void
2563+global_table(void)
2564+{
2565+ NMTBL *n;
2566+ int init;
2567+ init=0;
2568+ for(n=global_list;n;n = n->next) {
2569+ if ((n->sc == GVAR) && n->dsp != -1) {
2570+ if (is_code(n)||is_function(n)) continue;
2571+ /* n->dsp = -1 means initialized global */
2572+ if (init==0) {
2573+ data_mode(0);
2574+ init=1;
2575+ }
2576+ printf(".comm _%s,%d\n",n->nm,size(n->ty));
2577+ } else if ((n->sc==STATIC) && n->dsp != -1) {
2578+ /* n->dsp = -1 means initialized global */
2579+ if (is_code(n)||is_function(n)) {
2580+ printf("\t.set L_%s$stub,_%s\n",n->nm,n->nm);
2581+ continue;
2582+ }
2583+ if (init==0) {
2584+ data_mode(0);
2585+ init=1;
2586+ }
2587+ printf(".lcomm _%s,%d\n",n->nm,size(n->ty));
2588+ }
2589+ }
2590+ init=0;
24102591 for(n = global_list;n!=&null_nptr;n = n->next) {
2411- if (is_code(n)||is_function(n)) {
2412- if (n->sc==EXTRN1) {
2413- data_mode(0);
2592+ if (n->sc == GVAR ||
2593+ ((is_code(n) || is_function(n)) &&has_attr(n,FNAME)) ) {
2594+ if (init==0) {
2595+ printf("\t.section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
2596+ init=1;
2597+ }
2598+ printf("L_%s$non_lazy_ptr:\n\t.indirect_symbol _%s\n\t.long\t0\n",
2599+ n->nm,n->nm);
2600+ }
2601+ }
2602+ for(n = global_list;n!=&null_nptr;n = n->next) {
2603+ if (n->sc==EXTRN1) {
2604+ if (!(is_code(n) || is_function(n))) {
2605+ printf("\t.section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
2606+ printf("L_%s$non_lazy_ptr:\n\t.indirect_symbol _%s\n\t.long\t0\n",
2607+ n->nm,n->nm);
24142608 }
24152609 printf("\t.section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5\n");
24162610 printf("L_%s$stub:\n",n->nm);
24172611 printf("\t.indirect_symbol _%s\n",n->nm);
24182612 printf("\thlt ; hlt ; hlt ; hlt ; hlt\n");
2419- }
2613+ }
24202614 }
2615+ printf(" .subsections_via_symbols\n");
2616+ printf(" .section __TEXT,__textcoal_nt,coalesced,pure_instructions\n");
2617+ printf(".weak_definition ___i686.get_pc_thunk.bx\n");
2618+ printf(".private_extern ___i686.get_pc_thunk.bx\n");
2619+ printf("___i686.get_pc_thunk.bx:\n");
2620+ printf(" movl (%%esp), %%ebx\n");
2621+ printf(" ret\n");
2622+}
2623+
24212624 #endif
24222625
2423-}
2424-
24252626 void
24262627 local_table(void)
24272628 {
@@ -2512,10 +2713,11 @@
25122713 void code_dassign_gvar(int e2,int freg,int d)
25132714 {
25142715 #ifdef __APPLE__
2716+ int r = get_ptr_cache((NMTBL*)caddr(e2));
25152717 if (cadr(e2))
2516- printf("\t%s _%s+%d\n",fstore(d),((NMTBL*)caddr(e2))->nm,cadr(e2));
2718+ printf("\t%s %d(%s)\n",fstore(d),cadr(e2),register_name(r,0));
25172719 else
2518- printf("\t%s _%s\n",fstore(d),((NMTBL*)caddr(e2))->nm);
2720+ printf("\t%s (%s)\n",fstore(d),register_name(r,0));
25192721 #else
25202722 if (cadr(e2))
25212723 printf("\t%s %s+%d\n",fstore(d),((NMTBL*)caddr(e2))->nm,cadr(e2));
@@ -2581,7 +2783,11 @@
25812783 } else {
25822784 text_mode(0);
25832785 }
2786+#ifdef __APPLE__
2787+ printf("\tfldl _%d-_%d(%%ebx)\n",lb,goffset_label);
2788+#else
25842789 printf("\tfldl _%d\n",lb);
2790+#endif
25852791 }
25862792
25872793 void
@@ -2662,10 +2868,11 @@
26622868 void code_drgvar(int e2,int d,int freg)
26632869 {
26642870 #ifdef __APPLE__
2871+ int r = get_ptr_cache((NMTBL*)caddr(e2));
26652872 if (cadr(e2))
2666- printf("\t%s _%s+%d\n",fload(d),((NMTBL*)caddr(e2))->nm,cadr(e2));
2873+ printf("\t%s %d(%s)\n",fload(d),cadr(e2),register_name(r,0));
26672874 else
2668- printf("\t%s _%s\n",fload(d),((NMTBL*)caddr(e2))->nm);
2875+ printf("\t%s (%s)\n",fload(d),register_name(r,0));
26692876 #else
26702877 if (cadr(e2))
26712878 printf("\t%s %s+%d\n",fload(d),((NMTBL*)caddr(e2))->nm,cadr(e2));
@@ -2683,10 +2890,11 @@
26832890 void code_cmp_drgvar(int e2,int reg,int d,int label,int cond)
26842891 {
26852892 #ifdef __APPLE__
2893+ int r = get_ptr_cache((NMTBL*)caddr(e2));
26862894 if (cadr(e2))
2687- printf("\tfcomp _%s+%d\n",((NMTBL*)caddr(e2))->nm,cadr(e2));
2895+ printf("\tfcomp %d(%s)\n",cadr(e2),register_name(r,0));
26882896 else
2689- printf("\tfcomp _%s\n",((NMTBL*)caddr(e2))->nm);
2897+ printf("\tfcomp (%s)\n",register_name(r,0));
26902898 #else
26912899 if (cadr(e2))
26922900 printf("\tfcomp %s+%d\n",((NMTBL*)caddr(e2))->nm,cadr(e2));
@@ -2717,6 +2925,10 @@
27172925 case DCMP:
27182926 printf("\tfucompp\n");
27192927 printf("\tfnstsw\t%%ax\n");
2928+#ifdef __APPLE__
2929+ if (regs[REG_EAX]==PTRC_REG)
2930+ clear_ptr_cache_reg(REG_EAX);
2931+#endif
27202932 break;
27212933 }
27222934 }
@@ -2855,6 +3067,10 @@
28553067 {
28563068 if (e2!=USE_CREG)
28573069 error(-1);
3070+#ifdef __APPLE__
3071+ if (regs[REG_EAX]==PTRC_REG)
3072+ clear_ptr_cache_reg(REG_EAX);
3073+#endif
28583074 printf("\tfldz\n");
28593075 printf("\tfucompp\n");
28603076 printf("\tfnstsw\t%%ax\n");
@@ -2921,6 +3137,9 @@
29213137 /* specified registers stacks are saved in local variable */
29223138 /* temporal registers are saved in local variable */
29233139 int i,reg;
3140+ if (regs[reg1]==PTRC_REG)
3141+ clear_ptr_cache_reg(reg1);
3142+
29243143 for(i=0;i<reg_sp;i++) {
29253144 if ((reg=reg_stack[i])>=0 && reg==reg1) {
29263145 code_assign_lvar(
@@ -2971,10 +3190,12 @@
29713190 g_expr(e1);
29723191 emit_lpush();
29733192 g_expr(e2);
2974- // we are sure %ecx,%ebx is free
3193+ // we are sure %ecx is free
3194+ // %ebx is used in Intel Mac
3195+ stack_depth -= SIZE_OF_INT * 2;
29753196 printf("\tpopl %%ecx\n"); // LSW
2976- printf("\tpopl %%ebx\n"); // MSW
2977- printf("\tsubl %%edx,%%ebx\n");
3197+ printf("\tcmpl %%edx,(%%esp)\n"); // MSW
3198+ printf("\tpopl %%edx\n");
29783199 l2 = fwdlabel();
29793200 // cond==0 jump on false condtion ( if(x) => rexpr(.. cond=0 ...) )
29803201 switch(op) {
@@ -3038,18 +3259,20 @@
30383259 void code_cmp_lrgvar(int e1,int e2,int label,int cond)
30393260 {
30403261 char *n,*crn;
3041- n = ((NMTBL*)caddr(e1))->nm;
30423262 use_int(e2);
30433263 crn = register_name(e2,0);
30443264 #ifdef __APPLE__
3265+ int r = get_ptr_cache((NMTBL*)caddr(e1));
3266+ n = register_name(r,0);
30453267 if (cadr(e1)) {
3046- printf("\tmovl _%s+%d,%s\n",n,cadr(e1),crn);
3047- printf("\torl _%s+%d,%s\n",n,cadr(e1)+4,crn);
3268+ printf("\tmovl %d(%s),%s\n",cadr(e1),n,crn);
3269+ printf("\torl %d(%s),%s\n",cadr(e1)+4,n,crn);
30483270 } else {
3049- printf("\tmovl _%s,%s\n",n,crn);
3050- printf("\torl _%s+4,%s\n",n,crn);
3271+ printf("\tmovl (%s),%s\n",n,crn);
3272+ printf("\torl 4(%s),%s\n",n,crn);
30513273 }
30523274 #else
3275+ n = ((NMTBL*)caddr(e1))->nm;
30533276 if (cadr(e1)) {
30543277 printf("\tmovl %s+%d,%s\n",n,cadr(e1),crn);
30553278 printf("\torl %s+%d,%s\n",n,cadr(e1)+4,crn);
@@ -3088,18 +3311,20 @@
30883311 void code_lassign_gvar(int e1,int e2)
30893312 {
30903313 char *n;
3091- n = ((NMTBL*)caddr(e1))->nm;
30923314 use_longlong(e2);
30933315 #if ENDIAN_L==0
30943316 #ifdef __APPLE__
3317+ int r = get_ptr_cache((NMTBL*)caddr(e1));
3318+ n = register_name(r,0);
30953319 if (cadr(e1)) {
3096- printf("\tmovl %s,_%s+%d\n",l_eax(e2),n,cadr(e1));
3097- printf("\tmovl %s,_%s+%d\n",l_edx(e2),n,cadr(e1)+4);
3320+ printf("\tmovl %s,%d(%s)\n",l_eax(e2),cadr(e1),n);
3321+ printf("\tmovl %s,%d(%s)\n",l_edx(e2),cadr(e1)+4,n);
30983322 } else {
3099- printf("\tmovl %s,_%s\n",l_eax(e2),n);
3100- printf("\tmovl %s,_%s+4\n",l_edx(e2),n);
3323+ printf("\tmovl %s,(%s)\n",l_eax(e2),n);
3324+ printf("\tmovl %s,4(%s)\n",l_edx(e2),n);
31013325 }
31023326 #else
3327+ n = ((NMTBL*)caddr(e1))->nm;
31033328 if (cadr(e1)) {
31043329 printf("\tmovl %s,%s+%d\n",l_eax(e2),n,cadr(e1));
31053330 printf("\tmovl %s,%s+%d\n",l_edx(e2),n,cadr(e1)+4);
@@ -3151,18 +3376,20 @@
31513376 void code_lrgvar(int e1,int e2)
31523377 {
31533378 char *n;
3154- n = ((NMTBL*)caddr(e1))->nm;
31553379 use_longlong(e2);
31563380 #if ENDIAN_L==0
31573381 #ifdef __APPLE__
3382+ int r = get_ptr_cache((NMTBL*)caddr(e1));
3383+ n = register_name(r,0);
31583384 if (cadr(e1)) {
3159- printf("\tmovl _%s+%d,%s\n",n,cadr(e1),l_eax(e2));
3160- printf("\tmovl _%s+%d,%s\n",n,cadr(e1)+4,l_edx(e2));
3385+ printf("\tmovl %d(%s),%s\n",cadr(e1),n,l_eax(e2));
3386+ printf("\tmovl %d(%s),%s\n",cadr(e1)+4,n,l_edx(e2));
31613387 } else {
3162- printf("\tmovl _%s,%s\n",n,l_eax(e2));
3163- printf("\tmovl _%s+4,%s\n",n,l_edx(e2));
3388+ printf("\tmovl (%s),%s\n",n,l_eax(e2));
3389+ printf("\tmovl 4(%s),%s\n",n,l_edx(e2));
31643390 }
31653391 #else
3392+ n = ((NMTBL*)caddr(e1))->nm;
31663393 if (cadr(e1)) {
31673394 printf("\tmovl %s+%d,%s\n",n,cadr(e1),l_eax(e2));
31683395 printf("\tmovl %s+%d,%s\n",n,cadr(e1)+4,l_edx(e2));
@@ -3265,10 +3492,21 @@
32653492 // printf("\taddl $8,%%esp\n");
32663493 printf("\tlea 16(%%esp),%%esp\n");
32673494 return;
3495+#ifdef __APPLE__
3496+ case LDIV: call="L___divdi3$stub";
3497+ extern_define("__divdi3",0,FUNCTION,1); break;
3498+ case LUDIV: call="L___udivdi3$stub";
3499+ extern_define("__udivdi3",0,FUNCTION,1); break;
3500+ case LMOD: call="L___moddi3$stub";
3501+ extern_define("__moddi3",0,FUNCTION,1); break;
3502+ case LUMOD: call="L___umoddi3$stub";
3503+ extern_define("__umoddi3",0,FUNCTION,1); break;
3504+#else
32683505 case LDIV: call="__divdi3"; break;
32693506 case LUDIV: call="__udivdi3"; break;
32703507 case LMOD: call="__moddi3"; break;
32713508 case LUMOD: call="__umoddi3"; break;
3509+#endif
32723510 default: error(-1);
32733511 }
32743512 if (opl) {
@@ -3276,11 +3514,18 @@
32763514 printf("\tlea 8(%%esp),%%esp\n");
32773515 check_lreg(reg);
32783516 } else if (call) {
3517+#ifdef __APPLE__
3518+ clear_ptr_cache();
32793519 printf("\tpushl %%edx\n");
32803520 printf("\tpushl %%eax\n");
32813521 printf("\tcall %s\n",call);
3282- // printf("\taddl $8,%%esp\n");
32833522 printf("\tlea 16(%%esp),%%esp\n");
3523+#else
3524+ printf("\tpushl %%edx\n");
3525+ printf("\tpushl %%eax\n");
3526+ printf("\tcall %s\n",call);
3527+ printf("\tlea 16(%%esp),%%esp\n");
3528+#endif
32843529 check_lreg(reg);
32853530 } else {
32863531 error(-1);
@@ -3488,9 +3733,17 @@
34883733 void code_d2ull(int reg)
34893734 {
34903735 use_longlong(reg);
3736+#ifdef __APPLE__
3737+ clear_ptr_cache();
3738+#endif
34913739 printf("\tsubl $16,%%esp\n");
34923740 printf("\tfstpl (%%esp)\n");
3741+#ifdef __APPLE__
3742+ printf("\tcall L___fixunsdfdi$stub\n");
3743+ extern_define("__fixunsdfdi",0,FUNCTION,1);
3744+#else
34933745 printf("\tcall __fixunsdfdi\n");
3746+#endif
34943747 printf("\taddl $16,%%esp\n");
34953748 }
34963749
@@ -3502,9 +3755,17 @@
35023755 void code_f2ull(int reg)
35033756 {
35043757 use_longlong(reg);
3758+#ifdef __APPLE__
3759+ clear_ptr_cache();
3760+#endif
35053761 printf("\tsubl $16,%%esp\n");
35063762 printf("\tfstps (%%esp)\n");
3763+#ifdef __APPLE__
3764+ printf("\tcall L___fixunssfdi$stub\n");
3765+ extern_define("__fixunssfdi",0,FUNCTION,1);
3766+#else
35073767 printf("\tcall __fixunssfdi\n");
3768+#endif
35083769 printf("\taddl $16,%%esp\n");
35093770 }
35103771
@@ -3615,28 +3876,60 @@
36153876 printf("\tcmpl\t$%d,%s\n",max-min,crn);
36163877 printf("\tja\t_%d\n",dlabel);
36173878 if (delta==1) {
3879+#ifdef __APPLE__
3880+ printf("\tmovl\t_%d-_%d(%%ebx,%s,4),%s\n",l,goffset_label,crn,crn);
3881+ printf("\taddl\t%%ebx,%s\n",crn);
3882+ printf("\tjmp\t*%s\n",crn);
3883+#else
36183884 printf("\tjmp\t*_%d(,%s,4)\n",l,crn);
3885+#endif
36193886 return;
36203887 }
3888+#ifdef __APPLE__
3889+ if (regs[REG_EAX]==PTRC_REG)
3890+ clear_ptr_cache_reg(REG_EAX);
3891+#endif
36213892 use_register(creg,REG_EAX,1);
3893+ crn = "%eax";
36223894
36233895 switch(delta) {
36243896 case 2:
36253897 printf("\tmovl\t$1,%%edx\n");
36263898 printf("\tandl\t%%eax,%%edx\n");
36273899 printf("\tjne\t_%d\n",dlabel);
3628- printf("\tjmp\t*_%d(,%%eax,2)\n",l); break;
3900+#ifdef __APPLE__
3901+ printf("\tmovl\t_%d-_%d(%%ebx,%s,2),%s\n",l,goffset_label,crn,crn);
3902+ printf("\taddl\t%%ebx,%s\n",crn);
3903+ printf("\tjmp\t*%s\n",crn);
3904+#else
3905+ printf("\tjmp\t*_%d(,%%eax,2)\n",l);
3906+#endif
3907+ break;
36293908 case 4:
36303909 printf("\tmovl\t$3,%%edx\n");
36313910 printf("\tandl\t%%eax,%%edx\n");
36323911 printf("\tjne\t_%d\n",dlabel);
3633- printf("\tjmp\t*_%d(%%eax)\n",l); break;
3912+#ifdef __APPLE__
3913+ printf("\tmovl\t_%d-_%d(%%ebx,%s),%s\n",l,goffset_label,crn,crn);
3914+ printf("\taddl\t%%ebx,%s\n",crn);
3915+ printf("\tjmp\t*%s\n",crn);
3916+#else
3917+ printf("\tjmp\t*_%d(%%eax)\n",l);
3918+#endif
3919+ break;
36343920 default:
36353921 printf("\tmovl $%d,%%ecx\n",delta);
36363922 printf("\txor %%edx,%%edx\n\tdivl %%ecx\n");
36373923 printf("\tandl\t%%edx,%%edx\n");
36383924 printf("\tjne\t_%d\n",dlabel);
3639- printf("\tjmp\t*_%d(,%%eax,4)\n",l); break;
3925+#ifdef __APPLE__
3926+ printf("\tmovl\t_%d-_%d(%%ebx,%s,4),%s\n",l,goffset_label,crn,crn);
3927+ printf("\taddl\t%%ebx,%s\n",crn);
3928+ printf("\tjmp\t*%s\n",crn);
3929+#else
3930+ printf("\tjmp\t*_%d(,%%eax,4)\n",l);
3931+#endif
3932+ break;
36403933 }
36413934
36423935 }
@@ -3646,7 +3939,7 @@
36463939 {
36473940 output_mode=DATA_EMIT_MODE;
36483941 #ifdef __APPLE__
3649- printf(" \t.literal4\n\t.align 2\n");
3942+ printf(" \t.align 2\n");
36503943 #else
36513944 printf(" \t.section\t.rodata\n\t.align 4\n");
36523945 #endif
@@ -3656,7 +3949,11 @@
36563949 void
36573950 code_table_value(int label,int table_top)
36583951 {
3952+#ifdef __APPLE__
3953+ printf("\t.long _%d-_%d\n",label,goffset_label);
3954+#else
36593955 printf("\t.long _%d\n",label);
3956+#endif
36603957 }
36613958
36623959 void
@@ -3682,9 +3979,17 @@
36823979 } else if (car(rstr)==CONST) {
36833980 printf("%d",cadr(rstr));
36843981 } else if (car(rstr)==FNAME) {
3982+#ifdef __APPLE__
3983+ printf("_%s-_%d",(char*)cadr(rstr),goffset_label);
3984+#else
36853985 printf("%s",(char*)cadr(rstr));
3986+#endif
36863987 } else if (car(rstr)==STRING) {
3988+#ifdef __APPLE__
3989+ printf("_%d-_%d",cadr(rstr),goffset_label);
3990+#else
36873991 printf("_%d",cadr(rstr));
3992+#endif
36883993 } else {
36893994 error(-1);
36903995 }
@@ -4022,7 +4327,7 @@
40224327 mask = make_mask(32-bitpos-bitsize,31-bitpos);
40234328 make_mask_and_or(mask,value,adr);
40244329 printf("\tpopl %s\n",register_name(adr,0));
4025- code_assign(adr,size,value);
4330+ code_assign(adr,size==4?0:size,value);
40264331 }
40274332 if (use) {
40284333 code_bit_field(type,adr,USE_CREG);
@@ -4111,7 +4416,7 @@
41114416 /* make and-mask */
41124417 mask = make_mask(32-bitpos-bitsize,31-bitpos);
41134418 make_mask_and_or_const(mask,lvalue,c);
4114- code_assign(adr,size,lvalue);
4419+ code_assign(adr,size==4?0:size,lvalue);
41154420 free_register(lvalue);
41164421 }
41174422 if (use)
diff -r ce2364964f1e -r 8106ea9cf185 mc-code-powerpc.c
--- a/mc-code-powerpc.c Wed Sep 27 16:28:22 2006 +0900
+++ b/mc-code-powerpc.c Wed Oct 11 18:45:41 2006 +0900
@@ -46,7 +46,11 @@
4646 #define __FLT_MIN__ 1.17549435e-38F\n\
4747 #define __DBL_MIN__ 2.2250738585072014e-308\n\
4848 #define __LDBL_MIN__ 2.00416836000897277799610805135016e-292L\n\
49-";
49+"
50+#ifdef __APPLE__
51+"#define __APPLE__ 1\n"
52+#endif
53+;
5054
5155 #define TEXT_EMIT_MODE 0
5256 #define DATA_EMIT_MODE 1
diff -r ce2364964f1e -r 8106ea9cf185 mc-inline.c
--- a/mc-inline.c Wed Sep 27 16:28:22 2006 +0900
+++ b/mc-inline.c Wed Oct 11 18:45:41 2006 +0900
@@ -20,6 +20,29 @@
2020 ** You don't have to ask before copying, redistribution or publishing.
2121 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
2222 ***********************************************************************/
23+
24+/*
25+
26+ Inline handler
27+
28+ inline code is stored as parse tree
29+ expr already has parse tree
30+ statement part is handled here
31+
32+ st_hoge() code generator (called from gexpr())
33+ p_hoge() partial evaluator called in gen_inline()
34+ after p_hoge(), it contains no ST_* node.
35+
36+ PVAR has an offset for pvartable, it can be
37+ constant, local variable or global variable
38+ other complex expression is evaluated before the code expansion
39+
40+ We always perform inline expansion.
41+ Non static inline function can be referenced from other. Real
42+ function body is generated by pfdecl() in mc-parse.c.
43+
44+ */
45+
2346 #include <stdio.h>
2447 #include "mc.h"
2548 #include "mc-parse.h"
@@ -753,6 +776,7 @@
753776 int p1=pexpr(cadr(e1));
754777 int p2=pexpr(caddr(e1));
755778 int p3=pexpr(cadddr(e1));
779+ // unfolding for constant case?
756780 return list3(ST_FOR,pexpr(cadr(e)), list4(p0,p1,p2,p3));
757781 }
758782
@@ -761,6 +785,8 @@
761785 {
762786 int e2 = pexpr(caddr(e));
763787 int e3 = pexpr(cadddr(e));
788+ // if cadr(e) is a constant, we have to prune case statement.
789+ // here?
764790 return list4(ST_SWITCH,pexpr(cadr(e)),e2,e3);
765791 }
766792
@@ -796,7 +822,7 @@
796822 static int
797823 p_default(int e)
798824 {
799- // should be removed if constant case value
825+ // should be removed if case value is a constant
800826 return list2(ST_DEFAULT,pexpr(cadr(e)));
801827 }
802828
@@ -1179,11 +1205,16 @@
11791205 return VOID;
11801206 }
11811207
1208+/*
1209+ Prepare pvariable table
1210+ */
1211+
11821212 static int
11831213 replace_inline_parameter(NMTBL *anptr,int t,int e4,int narg,int evals)
11841214 {
11851215 int arg;
11861216 if (has_attr(anptr,KONST) && !has_attr(anptr,HAS_ADDRESS)) {
1217+ // replacable const variable
11871218 if (is_memory(e4)) {
11881219 heap[pdisp+narg]=reference(e4);
11891220 return evals;
@@ -1192,6 +1223,7 @@
11921223 return evals;
11931224 }
11941225 }
1226+ // we need real local variable for this inline
11951227 arg = heap[pdisp+narg]=list3(LVAR,new_lvar(size(t)),0);
11961228 inline_lvars = glist2(arg,inline_lvars);
11971229 evals=list2(assign_expr0(arg,e4,anptr->ty,t),evals);
@@ -1262,6 +1294,8 @@
12621294 n->next = local_static_list; local_static_list = local_statics;
12631295 cadddr(e1) = 0; // prevent duplicate initialize
12641296 }
1297+
1298+ // free used local variables or registers
12651299 while(inline_lvars) {
12661300 int e;
12671301 int l = car(inline_lvars);
diff -r ce2364964f1e -r 8106ea9cf185 mc-macro.c
--- a/mc-macro.c Wed Sep 27 16:28:22 2006 +0900
+++ b/mc-macro.c Wed Oct 11 18:45:41 2006 +0900
@@ -71,11 +71,15 @@
7171
7272 save_cheap(&scheap,cheap);
7373
74+ // call macro evaluation interpreter
7475 if (nptrm->sc == FMACRO) {
7576 macrop=macro_function(macrop,&chptr,nptrm,0);
7677 } else {
7778 macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0);
7879 }
80+
81+ // copy output from resulted listed string
82+
7983 cheap = reset_cheap(&scheap);
8084 macropp = cheap->ptr;
8185 // append result override, working cheap, but it's OK.
@@ -86,14 +90,17 @@
8690 t = cheap->ptr-2;
8791 cheap->ptr[0] =0;
8892 cheap = increment_cheap(cheap,&macropp);
93+
94+ // if we have ## (concatenation),
95+ // remove \s**##\s*
96+ // it is difficult to remove former space on the fly,
97+ // so multi path loop is required
98+
8999 while (mconcat) {
90100 // ## re-eval macro
91101 // if (lsrc) printf("## before %s",macropp);
92102 mconcat = 0;
93103 macrop = 0;
94- // remove \s**##\s*
95- // it is difficult to remove previous space on the fly,
96- // so multi path loop is required
97104 for(s=t=macropp;*s;) {
98105 if ((c=*s++)=='#'&&*s=='#') {
99106 if (t>s-3) t=s-2; else t--;
@@ -105,7 +112,7 @@
105112 }
106113 *t++=0;
107114 // evaluate generated result again
108-if (lsrc) {
115+if (0 && lsrc) {
109116 printf("### %s\n",macropp);
110117 if (t[-2]!='\n') putchar('\n');
111118 }
@@ -128,7 +135,7 @@
128135 if (t[-2]!='\n') putchar('\n');
129136 }
130137 // push previous chptr, and change it to the generate macro
131- chptrsave = glist2((int)chptr,chptrsave);
138+ chptrsave = glist2((int)chptr,chptrsave); // push old one into the stack
132139 chsave = glist2(ch,chsave);
133140 chptr = macropp;
134141 ch = *chptr++;
@@ -158,6 +165,34 @@
158165 }
159166
160167 /*
168+ internal string compare routine
169+ nameeq in mc-parse.c relies on
170+ global name variable
171+ */
172+
173+static int
174+nameeq(char *p, char *q)
175+{
176+ if (!p)
177+ return 0;
178+ while(*p)
179+ if(*p++ != *q++) return 0;
180+ return (*q==0);
181+}
182+
183+/*
184+ file name expansion
185+
186+ Get file name from input stream.
187+ Result is store in filep structure.
188+ included file is put on the filep stack
189+ return filep
190+
191+ filename is copied into cheap
192+
193+ possibly expanded by search path (including current
194+ directory ).
195+
161196 get file name
162197 <name> => name
163198 current_file_name_dir / name
@@ -169,15 +204,6 @@
169204 next flag ignores the first occurence.
170205 */
171206
172-static int
173-nameeq(char *p, char *q)
174-{
175- if (!p)
176- return 0;
177- while(*p)
178- if(*p++ != *q++) return 0;
179- return (*q==0);
180-}
181207
182208 static FILE *
183209 getfname(int next)
@@ -246,6 +272,7 @@
246272 *cheap->ptr = 0;
247273 cheap = increment_cheap(cheap,&name);
248274 }
275+ // should check filep over flow (sigh...)
249276 (filep+1)->inc = end;
250277 (filep+1)->name0 = name;
251278 return ( (filep+1)->fcb = fp );
@@ -311,7 +338,7 @@
311338 {
312339 int i;
313340 int c;
314- char num[10];
341+ char num[10]; // for 32bit
315342 char *p;
316343
317344 if (next_eof) {
@@ -320,6 +347,7 @@
320347 }
321348 do {
322349 if (chinput) {
350+ // some another input source ( init string )
323351 if (! *chinput) {
324352 chinput=0;
325353 continue;
@@ -330,6 +358,7 @@
330358 if (++i > LBUFSIZE-2) error(LNERR);
331359 }
332360 } else {
361+ // get the line from input stream
333362 lineno++;
334363 glineno++;
335364 chptr=linebuf;
@@ -353,11 +382,15 @@
353382 }
354383 }
355384 }
385+
356386 *chptr = '\0';
357387 if (lsrc && !asmf && !macro_if_skip && linebuf[0]) {
358- gen_comment(linebuf);
388+ gen_comment(linebuf); // #if ed line will not be commented
359389 if (inmode) {
360390 // inline mode
391+
392+ // generate inlined line in assembler output
393+
361394 int i=0;
362395 int c;
363396 // should be done in some init
@@ -389,6 +422,7 @@
389422 }
390423 p = chptr = linebuf; while(*p==' '||*p=='\t') p++;
391424 if (*p == '#' && !in_comment && !in_quote) {
425+ // macro directive
392426 chptr = p;
393427 if (macro_processing()) return;
394428 }
@@ -446,6 +480,13 @@
446480 type=stype;
447481 }
448482
483+/*
484+ Macro directive
485+
486+ implemented in simple hash
487+
488+ */
489+
449490 static int
450491 macro_processing()
451492 {
@@ -626,7 +667,13 @@
626667 ch = chsave;
627668 }
628669
629-/* macro define from chptr */
670+/* macro define from chptr
671+
672+ body will be copied and stored in nptr->dsp
673+ list2( string, list of argments (if any))
674+ We don't expand macro here, it just copied.
675+
676+ */
630677
631678 static void
632679 macro_define0()
@@ -693,6 +740,8 @@
693740
694741 // create function macro argument list
695742 // return list2((char*)arg,next)
743+// it can be sepearted by \ or comments
744+// no expansion
696745
697746 static int
698747 macro_args(char **pchptr)
@@ -788,7 +837,11 @@
788837 return reverse0(args);
789838 }
790839
791-/* output macro expansion result into macrobuf (macropp) */
840+/* output macro expansion
841+
842+ This is a recursive interpreter.
843+
844+ result into macrobuf (macropp) */
792845
793846 static int
794847 macro_function(int macrop,char **pchptr,NMTBL *nptr,int history)
@@ -828,6 +881,10 @@
828881 return macrop;
829882 }
830883
884+/*
885+ define name in the local scope
886+ */
887+
831888 static void
832889 local_define(char *macro,char *value)
833890 {
@@ -840,6 +897,9 @@
840897
841898 /*
842899 Evaluate macro string.
900+
901+ This is a recursive interpreter.
902+
843903 reuslt: list2("replaced string",next)
844904 history is necessary to avoid recursion
845905 */
diff -r ce2364964f1e -r 8106ea9cf185 mc-parse.c
--- a/mc-parse.c Wed Sep 27 16:28:22 2006 +0900
+++ b/mc-parse.c Wed Oct 11 18:45:41 2006 +0900
@@ -2100,6 +2100,7 @@
21002100 e=list3(a->sc==IVAR?LVAR:a->sc,a->dsp,(int)a);
21012101 cargs = list3(e,cargs,a->ty);
21022102 }
2103+ // build inline function call
21032104 e = list4(INLINE,list2(FNAME,(int)n),cargs,
21042105 list3(car(fnptr->ty),cadr(fnptr->ty),caddr(fnptr->ty)));
21052106
@@ -4367,6 +4368,7 @@
43674368 {
43684369 char *name = cheap->ptr;
43694370 int i= 0;
4371+ int c;
43704372 unsigned int hash = 0;
43714373 struct cheap scheap;
43724374
@@ -4377,13 +4379,19 @@
43774379 getch();
43784380 while (ch != '"') {
43794381 if (i>STRSIZE) error(STRERR);
4380- hash_value(hash, *cheap->ptr = escape());
4382+ if ((c = escape())==0 && ch=='"') {
4383+ in_quote = 0;
4384+ getch();
4385+ goto possible_string_concatenate;
4386+ }
4387+ hash_value(hash, *cheap->ptr = c);
43814388 cheap = increment_cheap(cheap,&name);
43824389 i++;
43834390 }
43844391 in_quote = 0;
43854392 getch();
43864393 }
4394+possible_string_concatenate:
43874395 skipspc();
43884396 } while (ch=='"');
43894397 *cheap->ptr = 0;
@@ -4391,6 +4399,7 @@
43914399 i++;
43924400 nptr = name_space_search(hash_search(name,&scheap,i,hash,DEF),STRING);
43934401 // if we already have this, hash_search will reset cheap
4402+ // should do the same thing for float/double constant
43944403 nptr->dsp = i;
43954404 symval = i;
43964405 }
diff -r ce2364964f1e -r 8106ea9cf185 test/code-gen.c
--- a/test/code-gen.c Wed Sep 27 16:28:22 2006 +0900
+++ b/test/code-gen.c Wed Oct 11 18:45:41 2006 +0900
@@ -154,13 +154,15 @@
154154
155155 // code_fname(NMTBL *n,int creg)
156156
157+extern void exit();
157158 INLINE void
158159 code_fname()
159160 {
160- int i,j;
161+ int i,j,k;
161162 i = (int)code_fname;
162163 j = (int)code_register;
163- printf("#0162:code_fname %d\n",i==j);
164+ k = (int)exit;
165+ printf("#0162:code_fname %d %d\n",i==j,i==k);
164166 }
165167
166168
Show on old repository browser