]> git.gag.com Git - fw/sdcc/commitdiff
a better version of array initializers
authorjohanknol <johanknol@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 12 Sep 2001 15:30:35 +0000 (15:30 +0000)
committerjohanknol <johanknol@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 12 Sep 2001 15:30:35 +0000 (15:30 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1266 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/SDCCast.c
src/SDCCicode.c
src/mcs51/gen.c

index ffa15d16f959fe8a19b45f6901b761c3a96ceb3e..807ada669eb11710d23c25353b6f2eb81affb094 100644 (file)
@@ -1036,67 +1036,35 @@ createIval (ast * sym, sym_link * type, initList * ilist, ast * wid)
 /* this has to go */ void printIval (symbol *, sym_link *, initList *, FILE *);
 
 ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
+  ast *ast;
+  symbol *newSym;
 
   if (getenv("TRY_THE_NEW_INITIALIZER")) {
 
     if (!TARGET_IS_MCS51 || !(options.model==MODEL_LARGE)) {
       fprintf (stderr, "Can't \"TRY_THE_NEW_INITIALIZER\" unless "
-              "with -mmcs51 and --model-large\n");
+              "with -mmcs51 and --model-large");
       exit(404);
     }
 
-    if (SPEC_OCLS(sym->type->next)!=xdata) {
-      fprintf (stderr, "Can't \"TRY_THE_NEW_INITALIZER\" unless in xdata\n");
-      exit (405);
-    }
-    
-    if (getSize(sym->type) > 64) { // else it is'n worth it: do it the old way
-      // emit the inital values in cseg, then copy it to where it belongs
-      initList *iLoop;
-      int count, size=getSize(sym->type->next);
-      
-      if (ival->type != INIT_DEEP) {
-       werror (E_INIT_STRUCT, sym->name);
-       return NULL;
-      }
-      
-      tfprintf (code->oFile, "; initial data for %s\n", sym->name);
-      // TODO: this has to be a unique name
-      tfprintf (code->oFile, "_init_%s:", sym->name);
-      
-      for (count=0, iLoop=ival->init.deep; iLoop; iLoop=iLoop->next) {
-       count += size;
-       printIval (sym, sym->type->next, iLoop, code->oFile);
-      }
-      
-      // Now we only have to copy <count> bytes from cseg.
-      // This is VERY -mmcs51 --model-large specific for now, 
-      // in fact we should generate
-      // some icodes here that does the trick. But ok: experimental
-      // Trick: memcpy (sym->name, _init_(sym->name), count)
-      fprintf (statsg->oFile, ";       %s      %d\n", filename, sym->lineDef);
-      fprintf (statsg->oFile, ";       copy initial data from cseg _init_%s to %s\n", 
-              sym->name, sym->name);
-      fprintf (statsg->oFile, "        mov dptr,#_memcpy_PARM_2\n");
-      fprintf (statsg->oFile, "        mov a,#_init_%s\n", sym->name);
-      fprintf (statsg->oFile, "        movx @dptr,a\n");
-      fprintf (statsg->oFile, "        inc dptr\n");
-      fprintf (statsg->oFile, "        mov a,#(_init_%s>>8)\n", sym->name);
-      fprintf (statsg->oFile, "        movx @dptr,a\n");
-      fprintf (statsg->oFile, "        inc dptr\n");
-      fprintf (statsg->oFile, "        mov a,#%02x;    from cseg\n", 1);
-      fprintf (statsg->oFile, "        movx @dptr,a\n");
-      fprintf (statsg->oFile, "        mov dptr,#_memcpy_PARM_3\n");
-      fprintf (statsg->oFile, "        mov a,#(%d>>0); number of bytes\n", count);
-      fprintf (statsg->oFile, "        movx @dptr,a\n");
-      fprintf (statsg->oFile, "        inc dptr\n");
-      fprintf (statsg->oFile, "        mov a,#(%d>>8)\n", count);
-      fprintf (statsg->oFile, "        movx @dptr,a\n");
-      fprintf (statsg->oFile, "        mov dptr,#_%s\n", sym->name);
-      fprintf (statsg->oFile, "        mov b,#%02x;    only to xseg for now\n", 2);
-      fprintf (statsg->oFile, "        lcall _memcpy\n");
-      
-      return NULL;
+    if (SPEC_OCLS(sym->etype)==xdata &&
+       getSize(sym->type) > 16) { // else it isn't worth it: do it the old way
+
+      // copy this symbol
+      newSym=copySymbol (sym);
+      SPEC_OCLS(newSym->type->next)=code;
+      sprintf (newSym->name, "%s_init__", sym->name);
+      sprintf (newSym->rname,"%s_init__", sym->rname);
+      addSym (SymbolTab, newSym, newSym->name, 0, 0, 1);
+
+      // emit it in the static segment
+      addSet(&statsg->syms, newSym);
+
+      // now memcpy() the entire array from cseg
+      ast=newNode (ARRAYINIT, // ASSIGN_AGGREGATE
+                  newAst_VALUE (symbolVal (sym)), 
+                  newAst_VALUE (symbolVal (newSym)));
+      return decorateType(resolveSymbols(ast));
     }
   }
   
index 393a6b20e631a84002b5a5dbfb8cdd052ae5e0a7..919b9526267f06161ca4b4784cc2d0c6beaae5fd 100644 (file)
@@ -3035,8 +3035,16 @@ geniCodeArrayInit (ast * tree, operand *array)
 {
   iCode *ic;
 
-  ic = newiCode (ARRAYINIT, array, NULL);
-  IC_ARRAYILIST (ic) = tree->values.constlist;
+  if (!getenv("TRY_THE_NEW_INITIALIZER")) {
+    ic = newiCode (ARRAYINIT, array, NULL);
+    IC_ARRAYILIST (ic) = tree->values.constlist;
+  } else {
+    operand *left=newOperand(), *right=newOperand();
+    left->type=right->type=SYMBOL;
+    OP_SYMBOL(left)=AST_SYMBOL(tree->left);
+    OP_SYMBOL(right)=AST_SYMBOL(tree->right);
+    ic = newiCode (ARRAYINIT, left, right);
+  }
   ADDTOCHAIN (ic);
 }
 
index f239639daf51e9c5b896594c463f1bcd8d3c2876..6863646aece3409c80311520566008d7e5d28f3d 100644 (file)
@@ -8477,6 +8477,46 @@ genReceive (iCode * ic)
   freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
 }
 
+/*-----------------------------------------------------------------*/
+/* gen51AggregateAssign - copy complete array's or structures            */
+/*-----------------------------------------------------------------*/
+void gen51AggregateAssign(iCode *ic) {
+  operand *left=IC_LEFT(ic);
+  operand *right=IC_RIGHT(ic);
+  char *fromName=OP_SYMBOL(right)->rname;
+  char *toName=OP_SYMBOL(left)->rname;
+  int fromSize=getSize(OP_SYMBOL(right)->type);
+  int toSize=getSize(OP_SYMBOL(left)->type);
+  int count=toSize;
+
+  if (fromSize!=toSize) {
+    fprintf (stderr, "*** error aggregates have different size");
+    if (fromSize<toSize)
+      count=fromSize;
+  }
+  
+  // memcpy from cseg to xseg
+  emitcode (";", "initialize %s", OP_SYMBOL(IC_LEFT(ic))->name);
+  emitcode ("mov", "dptr,#_memcpy_PARM_2");
+  emitcode ("mov", "a,#%s", fromName);
+  emitcode ("movx", "@dptr,a");
+  emitcode ("inc", "dptr");
+  emitcode ("mov", "a,#(%s>>8)", fromName);
+  emitcode ("movx", "@dptr,a");
+  emitcode ("inc", "dptr");
+  emitcode ("mov", "a,#%02x;   from cseg", 2);
+  emitcode ("movx", "@dptr,a");
+  emitcode ("mov", "dptr,#_memcpy_PARM_3");
+  emitcode ("mov", "a,#(%d>>0);        number of bytes", count);
+  emitcode ("movx", "@dptr,a");
+  emitcode ("inc", "dptr");
+  emitcode ("mov", "a,#(%d>>8)", count);
+  emitcode ("movx", "@dptr,a");
+  emitcode ("mov", "dptr,#%s", toName);
+  emitcode ("mov", "b,#%02x;   only to xseg for now", 1);
+  emitcode ("lcall", "_memcpy");
+}
+
 /*-----------------------------------------------------------------*/
 /* gen51Code - generate code for 8051 based controllers            */
 /*-----------------------------------------------------------------*/
@@ -8717,6 +8757,10 @@ gen51Code (iCode * lic)
          addSet (&_G.sendSet, ic);
          break;
 
+       case ARRAYINIT:
+         gen51AggregateAssign(ic);
+         break;
+
        default:
          ic = ic;
        }