home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Harvest C 1.3 / Source Code / initdata.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  8.4 KB  |  288 lines  |  [TEXT/ALFA]

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.  * Harvest C
  31.  * 
  32.  * Copyright 1991 Eric W. Sink   All rights reserved.
  33.  * 
  34.  * This file contains routines for initializations of data.
  35.  * 
  36.  * 
  37.  */
  38.  
  39.  
  40. #include "conditcomp.h"
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "structs.h"
  44. #include "CHarvestDoc.h"
  45. #include "CHarvestOptions.h"
  46.  
  47. extern CHarvestDoc *gProject;
  48.  
  49.  
  50. #pragma segment Initializers
  51.  
  52. SYMVia_t
  53. NextSym(SymListVia_t tbl, SYMVia_t cur)
  54. {
  55.     return TableGetNum(tbl, Via(cur)->IndexInserted + 1);
  56. }
  57.  
  58. SYMVia_t
  59. LastSym(SymListVia_t tbl)
  60. {
  61.     return TableGetNum(tbl, Via(tbl)->count);
  62. }
  63.  
  64. void
  65. ImageStruct(SymListVia_t tbl, SYMVia_t member, ParseTreeVia_t init, InstListVia_t Codes)
  66. {
  67.     /*
  68.      * Both member and init are probably lists. We need to be at the tail of
  69.      * both.
  70.      */
  71.     if (init && member) {
  72.     if ((Via(init)->kind == PTF_initializer_list) && (NextSym(tbl, member))) {
  73.         ImageStruct(tbl, NextSym(tbl, member), Via(init)->b, Codes);
  74.         ImageInit(Via(member)->TP, Via(init)->a, Codes);
  75.     } else if ((Via(init)->kind == PTF_initializer_list) && (!NextSym(tbl, member))) {
  76.         ImageStruct(tbl, member, Via(init)->b, Codes);
  77.         ImageInit(Via(member)->TP, Via(init)->a, Codes);
  78.     } else if ((Via(init)->kind != PTF_initializer_list) && (!NextSym(tbl, member))) {
  79.         ImageInit(Via(member)->TP, init, Codes);
  80.     } else if ((Via(init)->kind != PTF_initializer_list) && (NextSym(tbl, member))) {
  81.         ImageStruct(tbl, NextSym(tbl, member), init, Codes);
  82.     }
  83.     }
  84. }
  85.  
  86. void
  87. ImageArray(TypeRecordVia_t typerec, ParseTreeVia_t init, InstListVia_t Codes)
  88. {
  89.     if (init) {
  90.     if ((Via(init)->kind == PTF_initializer_list) && !isArrayType(typerec)) {
  91.         ImageArray(typerec, Via(init)->b, Codes);
  92.         ImageInit(typerec, Via(init)->a, Codes);
  93.     } else if ((Via(init)->kind == PTF_initializer_list) && isArrayType(typerec)) {
  94.         ImageArray(GetTPBase(typerec), init, Codes);
  95.     } else {
  96.         ImageInit(typerec, init, Codes);
  97.     }
  98.     }
  99. }
  100.  
  101. LocAMVia_t
  102. BuildFoldK(FoldValue_t val)
  103. {
  104.     if (val.isint) {
  105.     return BuildAbsolute(val.intval);
  106.     } else if (val.isstring) {
  107.     return BuildLabelLoc(GetLocLabel(Via(Via(val.init)->data.SLit)->M68kDef.Loc));
  108.     } else if (val.issymb) {
  109.     return Via(val.thesymbol)->M68kDef.Loc;
  110.     } else if (val.isoffset) {
  111.     return BuildLabelOffset(GetLocLabel(Via(val.thesymbol)->M68kDef.Loc), val.intval);
  112.     } else
  113.     return BuildAbsolute(0);
  114. }
  115.  
  116. int
  117. ImageInit(TypeRecordVia_t typerec, ParseTreeVia_t init, InstListVia_t Codes)
  118. {
  119.     FoldValue_t                     val;
  120.     int                             ndx;
  121.     ParseTreeVia_t                  tmptree;
  122.     int                             oldcount;
  123.     union {
  124.     float                           val1;
  125.     double                          val2;
  126.     long double                     val3;
  127.     char                            bytes[10];
  128.     }                               floatbytes;
  129.     if (init && typerec) {
  130.     ConstExprValue(init, &val);
  131.     switch (GetTPKind(typerec)) {
  132.     case TRC_typedef:
  133.         return ImageInit(GetTPBase(typerec), init, Codes);
  134.         break;
  135.     case TRC_char:
  136.         if (Via(init)->kind == PTF_string_literal) {
  137.         InitDataAccIndex += strlen((Via(Via(init)->data.SLit)->name));
  138.         GenOneLit(Via(init)->data.SLit, Codes);
  139.         } else {
  140.         GenInst(M68op_DC, M68sz_byte, BuildFoldK(val), NULL, Codes);
  141.         InitDataAccIndex++;
  142.         }
  143.         break;
  144.     case TRC_short:
  145.         GenInst(M68op_DC, M68sz_word, BuildFoldK(val), NULL, Codes);
  146.         InitDataAccIndex += 2;
  147.         break;
  148.     case TRC_int:
  149.         if (gProject->itsOptions->int2byte) {
  150.         GenInst(M68op_DC, M68sz_word, BuildFoldK(val), NULL, Codes);
  151.         InitDataAccIndex += 2;
  152.         } else {
  153.         GenInst(M68op_DC, M68sz_long, BuildFoldK(val), NULL, Codes);
  154.         InitDataAccIndex += 4;
  155.         }
  156.         break;
  157.     case TRC_long:
  158.         GenInst(M68op_DC, M68sz_long, BuildFoldK(val), NULL, Codes);
  159.         InitDataAccIndex += 4;
  160.         break;
  161.     case TRC_enum:
  162.         if (gProject->itsOptions->int2byte) {
  163.         GenInst(M68op_DC, M68sz_word, BuildFoldK(val), NULL, Codes);
  164.         InitDataAccIndex += 2;
  165.         } else {
  166.         GenInst(M68op_DC, M68sz_long, BuildFoldK(val), NULL, Codes);
  167.         InitDataAccIndex += 4;
  168.         }
  169.         break;
  170.     case TRC_pointer:
  171. #ifdef Undefined
  172.         if (Via(init)->kind == PTF_string_literal) {
  173.         GenInst(M68op_DC, M68sz_long, BuildLabelLoc(GetLocLabel(Via(Via(init)->data.SLit)->M68kDef.Loc)), NULL, Codes);
  174.         InitDataAccIndex += 4;
  175.         } else if (Via(init)->kind == PTF_identifier) {
  176.         GenInst(M68op_DC, M68sz_long, Via(Via(init)->data.thesymbol)->M68kDef.Loc, NULL, Codes);
  177.         InitDataAccIndex += 4;
  178.         } else {
  179.         GenInst(M68op_DC, M68sz_long, BuildFoldK(val), NULL, Codes);
  180.         InitDataAccIndex += 4;
  181.         }
  182. #endif
  183.         GenInst(M68op_DC, M68sz_long, BuildFoldK(val), NULL, Codes);
  184.         InitDataAccIndex += 4;
  185.         break;
  186.     case TRC_float:
  187.         if (val.isint) {
  188.         floatbytes.val1 = val.intval;
  189.         } else {
  190.         floatbytes.val1 = val.realval;
  191.         }
  192.         ndx = 0;
  193.         /*
  194.          * Problem with constants here - depends on whether or not SANE
  195.          * is running right now. TODO DANGER
  196.          */
  197.         while (ndx < SizeOfFloat) {
  198.         GenInst(M68op_DC, M68sz_byte, BuildAbsolute(floatbytes.bytes[ndx]), NULL, Codes);
  199.         InitDataAccIndex++;
  200.         ndx++;
  201.         }
  202.         break;
  203.     case TRC_double:
  204.         if (val.isint) {
  205.         floatbytes.val2 = val.intval;
  206.         } else {
  207.         floatbytes.val2 = val.realval;
  208.         }
  209.         ndx = 0;
  210.         /*
  211.          * Problem with constants here - depends on whether or not SANE
  212.          * is running right now. TODO DANGER
  213.          */
  214.         while (ndx < SizeOfDouble) {
  215.         GenInst(M68op_DC, M68sz_byte, BuildAbsolute(floatbytes.bytes[ndx]), NULL, Codes);
  216.         InitDataAccIndex++;
  217.         ndx++;
  218.         }
  219.         break;
  220.     case TRC_longdouble:
  221.         if (val.isint) {
  222.         floatbytes.val3 = val.intval;
  223.         } else {
  224.         floatbytes.val3 = val.realval;
  225.         }
  226.         ndx = 0;
  227.         /*
  228.          * Problem with constants here - depends on whether or not SANE
  229.          * is running right now. TODO DANGER
  230.          */
  231.         while (ndx < SizeOfLongDouble) {
  232.         GenInst(M68op_DC, M68sz_byte, BuildAbsolute(floatbytes.bytes[ndx]), NULL, Codes);
  233.         InitDataAccIndex++;
  234.         ndx++;
  235.         }
  236.         break;
  237.     case TRC_struct:
  238.         oldcount = InitDataAccIndex;
  239.         tmptree = init;
  240.         if (Via(tmptree)->kind == PTF_multi_initializer) {
  241.         tmptree = Via(init)->a;
  242.         }
  243.         ImageStruct(GetTPMembers(typerec), FirstSym(GetTPMembers(typerec)), tmptree, Codes);
  244.         if (((InitDataAccIndex - oldcount) < GetTPSize(typerec))) {
  245.         ndx = 0;
  246.         while (ndx < (GetTPSize(typerec) - (InitDataAccIndex - oldcount))) {
  247.             GenInst(M68op_DC, M68sz_byte, BuildAbsolute(0), NULL, Codes);
  248.             InitDataAccIndex++;
  249.             ndx++;
  250.         }
  251.         }
  252.         break;
  253.     case TRC_union:
  254.         tmptree = init;
  255.         /* Should this change be mandatory ?! */
  256.         if (Via(tmptree)->kind == PTF_multi_initializer) {
  257.         tmptree = Via(init)->a;
  258.         }
  259.         ImageInit(Via(LastSym(GetTPMembers(typerec)))->TP, tmptree, Codes);
  260.         break;
  261.     case TRC_array:
  262.         oldcount = InitDataAccIndex;
  263.         tmptree = init;
  264.         if (Via(tmptree)->kind == PTF_multi_initializer) {
  265.         tmptree = Via(init)->a;
  266.         }
  267.         ImageArray(GetTPBase(typerec), tmptree, Codes);
  268.         if (GetTPSize(typerec)) {
  269.         if (((InitDataAccIndex - oldcount) < GetTPSize(typerec))) {
  270.             ndx = 0;
  271.             while (ndx < (GetTPSize(typerec) - (InitDataAccIndex - oldcount))) {
  272.             GenInst(M68op_DC, M68sz_byte, BuildAbsolute(0), NULL, Codes);
  273.             InitDataAccIndex++;
  274.             ndx++;
  275.             }
  276.         } else if (((InitDataAccIndex - oldcount) > GetTPSize(typerec))) {
  277.             DeclError("Too much data in initializer");
  278.         }
  279.         } else {
  280.         SetTPFlags(typerec, GetTPFlags(typerec) & (~INCOMPLETEMASK));
  281.         SetTPSize(typerec, InitDataAccIndex - oldcount);
  282.         }
  283.         break;
  284.     }
  285.     }
  286.     return InitDataAccIndex;
  287. }
  288.