home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d110 / pdc.lha / Pdc / src / Decl.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  17KB  |  488 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *68000 C compiler
  9.  *
  10.  *Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *This compiler is intended as an instructive tool for personal use. Any
  14.  *use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *This compiler may be distributed freely for non-commercial use as long
  17.  *as this notice stays intact. Please forward any enhancements or questions
  18.  *to:
  19.  *
  20.  *Matthew Brandt
  21.  *Box 920337
  22.  *Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             *head = 0;
  26. TYP             *tail = 0;
  27. char            *declid = 0;
  28. TABLE           tagtable = {0,0};
  29. TYP             stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
  30. SYM        *search();
  31. extern long    intexpr();
  32.  
  33. int     imax(i,j)
  34. int     i,j;
  35. {       return (i > j) ? i : j;
  36. }
  37.  
  38. char    *litlate(s)
  39. char    *s;
  40. {       char    *p;
  41.         p = xalloc(strlen(s) + 1);
  42.         strcpy(p,s);
  43.         return p;
  44. }
  45.  
  46. TYP     *maketype(bt,siz)
  47. enum e_bt    bt;
  48. int        siz;
  49. {       TYP     *tp;
  50.         tp = xalloc(sizeof(TYP));
  51.         tp->val_flag = 0;
  52.         tp->size = siz;
  53.         tp->type = bt;
  54.         tp->sname = 0;
  55.         tp->lst.head = 0;
  56.         return tp;
  57. }
  58.  
  59. int     decl(table)
  60. TABLE   *table;
  61. {       switch (lastst) {
  62.                 case kw_char:
  63.                         head = tail = maketype(bt_char,1);
  64.                         getsym();
  65.                         break;
  66.                 case kw_short:
  67.                         head = tail = maketype(bt_short,2);
  68.                         getsym();
  69.                         break;
  70.                 case kw_int: case kw_long:
  71.                         head = tail = maketype(bt_long,4);
  72.                         getsym();
  73.                         break;
  74.                 case kw_unsigned:
  75.                         head = tail = maketype(bt_unsigned,4);
  76.                         getsym();
  77.                         if( lastst == kw_int )
  78.                                 getsym();
  79.                         break;
  80.                 case id:                /* no type declarator */
  81.                         head = tail = maketype(bt_long,4);
  82.                         break;
  83.                 case kw_float:
  84.                         head = tail = maketype(bt_float,4);
  85.                         getsym();
  86.                         break;
  87.                 case kw_double:
  88.                         head = tail = maketype(bt_double,8);
  89.                         getsym();
  90.                         break;
  91.                 case kw_enum:
  92.                         getsym();
  93.                         declenum(table);
  94.                         break;
  95.                 case kw_struct:
  96.                         getsym();
  97.                         declstruct(bt_struct);
  98.                         break;
  99.                 case kw_union:
  100.                         getsym();
  101.                         declstruct(bt_union);
  102.                         break;
  103.                 }
  104. }
  105.  
  106. decl1()
  107. {       TYP     *temp1, *temp2, *temp3, *temp4;
  108.         switch (lastst) {
  109.                 case id:
  110.                         declid = litlate(lastid);
  111.                         getsym();
  112.                         decl2();
  113.                         break;
  114.                 case star:
  115.                         temp1 = maketype(bt_pointer,4);
  116.                         temp1->btp = head;
  117.                         head = temp1;
  118.                         if(tail == NULL)
  119.                                 tail = head;
  120.                         getsym();
  121.                         decl1();
  122.                         break;
  123.                 case openpa:
  124.                         getsym();
  125.                         temp1 = head;
  126.                         temp2 = tail;
  127.                         head = tail = NULL;
  128.                         decl1();
  129.                         needpunc(closepa);
  130.                         temp3 = head;
  131.                         temp4 = tail;
  132.                         head = temp1;
  133.                         tail = temp2;
  134.                         decl2();
  135.                         temp4->btp = head;
  136.                         if(temp4->type == bt_pointer &&
  137.                                 temp4->val_flag != 0 && head != NULL)
  138.                                 temp4->size *= head->size;
  139.                         head = temp3;
  140.                         break;
  141.                 default:
  142.                         decl2();
  143.                         break;
  144.                 }
  145. }
  146.  
  147. decl2()
  148. {       TYP     *temp1;
  149.         switch (lastst) {
  150.                 case openbr:
  151.                         getsym();
  152.                         temp1 = maketype(bt_pointer,0);
  153.                         temp1->val_flag = 1;
  154.                         temp1->btp = head;
  155.                         if(lastst == closebr) {
  156.                                 temp1->size = 0;
  157.                                 getsym();
  158.                                 }
  159.                         else if(head != NULL) {
  160.                                 temp1->size = intexpr() * head->size;
  161.                                 needpunc(closebr);
  162.                                 }
  163.                         else {
  164.                                 temp1->size = intexpr();
  165.                                 needpunc(closebr);
  166.                                 }
  167.                         head = temp1;
  168.                         if( tail == NULL)
  169.                                 tail = head;
  170.                         decl2();
  171.                         break;
  172.                 case openpa:
  173.                         getsym();
  174.                         temp1 = maketype(bt_func,0);
  175.                         temp1->val_flag = 1;
  176.                         temp1->btp = head;
  177.                         head = temp1;
  178.                         if( lastst == closepa) {
  179.                                 getsym();
  180.                                 if(lastst == begin)
  181.                                         temp1->type = bt_ifunc;
  182.                                 }
  183.                         else
  184.                                 temp1->type = bt_ifunc;
  185.                         break;
  186.                 }
  187. }
  188.  
  189. int     alignment(tp)
  190. TYP     *tp;
  191. {       switch(tp->type) {
  192.                 case bt_char:           return AL_CHAR;
  193.                 case bt_short:          return AL_SHORT;
  194.                 case bt_long:           return AL_LONG;
  195.                 case bt_enum:           return AL_SHORT;
  196.                 case bt_pointer:
  197.                         if(tp->val_flag)
  198.                                 return alignment(tp->btp);
  199.                         else
  200.                                 return AL_POINTER;
  201.                 case bt_float:          return AL_FLOAT;
  202.                 case bt_double:         return AL_DOUBLE;
  203.                 case bt_struct:
  204.                 case bt_union:          return AL_STRUCT;
  205.                 default:                return AL_CHAR;
  206.                 }
  207. }
  208.  
  209. int     declare(table,al,ilc,ztype)
  210. /*
  211.  *      process declarations of the form:
  212.  *
  213.  *              <type>  <decl>, <decl>...;
  214.  *
  215.  *      leaves the declarations in the symbol table pointed to by
  216.  *      table and returns the number of bytes declared. al is the
  217.  *      allocation type to assign, ilc is the initial location
  218.  *      counter. if al is sc_member then no initialization will
  219.  *      be processed. ztype should be bt_struct for normal and in
  220.  *      structure declarations and sc_union for in union declarations.
  221.  */
  222. TABLE           *table;
  223. enum e_sc    al;
  224. int        ilc;
  225. enum e_bt    ztype;
  226. {       SYM     *sp, *sp1;
  227.         TYP     *dhead;
  228.         int     nbytes;
  229.         nbytes = 0;
  230.         decl(table);
  231.         dhead = head;
  232.         for(;;) {
  233.                 declid = 0;
  234.                 decl1();
  235.                 if( declid != 0) {      /* otherwise just struct tag... */
  236.                         sp = xalloc(sizeof(SYM));
  237.                         sp->name = declid;
  238.                         sp->storage_class = al;
  239.                         while( (ilc + nbytes) % alignment(head)) {
  240.                                 if( al != sc_member &&
  241.                                         al != sc_external &&
  242.                                         al != sc_auto) {
  243.                                         dseg();
  244.                                         genbyte(0);
  245.                                         }
  246.                                 ++nbytes;
  247.                                 }
  248.                         if( al == sc_static)
  249.                                 sp->value.i = nextlabel++;
  250.                         else if( ztype == bt_union)
  251.                                 sp->value.i = ilc;
  252.                         else if( al != sc_auto )
  253.                                 sp->value.i = ilc + nbytes;
  254.                         else
  255.                                 sp->value.i = -(ilc + nbytes + head->size);
  256.                         sp->tp = head;
  257.                         if( sp->tp->type == bt_func && 
  258.                                 sp->storage_class == sc_global )
  259.                                 sp->storage_class = sc_external;
  260.                         if(ztype == bt_union)
  261.                                 nbytes = imax(nbytes,sp->tp->size);
  262.                         else if(al != sc_external)
  263.                                 nbytes += sp->tp->size;
  264.                         if( sp->tp->type == bt_ifunc &&
  265.                                 (sp1 = search(sp->name,table->head)) != 0 &&
  266.                                 sp1->tp->type == bt_func )
  267.                                 {
  268.                                 sp1->tp = sp->tp;
  269.                                 sp1->storage_class = sp->storage_class;
  270.                                 sp1->value.i = sp->value.i;
  271.                                 sp = sp1;
  272.                                 }
  273.                         else
  274.                                 insert(sp,table);
  275.                         if( sp->tp->type == bt_ifunc) { /* function body follows */
  276.                                 funcbody(sp);
  277.                                 return nbytes;
  278.                                 }
  279.                         if( (al == sc_global || al == sc_static) &&
  280.                                 sp->tp->type != bt_func)
  281.                                 doinit(sp);
  282.                         }
  283.                 if(lastst == semicolon)
  284.                         break;
  285.                 needpunc(comma);
  286.                 if(declbegin(lastst) == 0)
  287.                         break;
  288.                 head = dhead;
  289.                 }
  290.         getsym();
  291.         return nbytes;
  292. }
  293.  
  294. int     declbegin(st)
  295. enum e_sym       st;
  296. {       return st == star || st == id || st == openpa ||
  297.                 st == openbr;
  298. }
  299.  
  300. declenum(table)
  301. TABLE   *table;
  302. {       SYM     *sp;
  303.         TYP     *tp;
  304.         if( lastst == id) {
  305.                 if((sp = search(lastid,tagtable.head)) == 0) {
  306.                         sp = xalloc(sizeof(SYM));
  307.                         sp->tp = xalloc(sizeof(TYP));
  308.                         sp->tp->type = bt_enum;
  309.                         sp->tp->size = 2;
  310.                         sp->tp->lst.head = sp->tp->btp = 0;
  311.                         sp->storage_class = sc_type;
  312.                         sp->name = litlate(lastid);
  313.                         sp->tp->sname = sp->name;
  314.                         getsym();
  315.                         if( lastst != begin)
  316.                                 error(ERR_INCOMPLETE);
  317.                         else    {
  318.                                 insert(sp,&tagtable);
  319.                                 getsym();
  320.                                 enumbody(table);
  321.                                 }
  322.                         }
  323.                 else
  324.                         getsym();
  325.                 head = sp->tp;
  326.                 }
  327.         else    {
  328.                 tp = xalloc(sizeof(tp));
  329.                 tp->type = bt_short;
  330.                 if( lastst != begin)
  331.                         error(ERR_INCOMPLETE);
  332.                 else    {
  333.                         getsym();
  334.                         enumbody(table);
  335.                         }
  336.                 head = tp;
  337.                 }
  338. }
  339.  
  340. enumbody(table)
  341. TABLE   *table;
  342. {       int     evalue;
  343.         SYM     *sp;
  344.         evalue = 0;
  345.         while(lastst == id) {
  346.                 sp = xalloc(sizeof(SYM));
  347.                 sp->value.i = evalue++;
  348.                 sp->name = litlate(lastid);
  349.                 sp->storage_class = sc_const;
  350.                 sp->tp = &stdconst;
  351.                 insert(sp,table);
  352.                 getsym();
  353.                 if( lastst == comma)
  354.                         getsym();
  355.                 else if(lastst != end)
  356.                         break;
  357.                 }
  358.         needpunc(end);
  359. }
  360.  
  361. declstruct(ztype)
  362. /*
  363.  *      declare a structure or union type. ztype should be either
  364.  *      bt_struct or bt_union.
  365.  */
  366. enum e_bt       ztype;
  367. {       SYM     *sp;
  368.         TYP     *tp;
  369.         if(lastst == id) {
  370.                 if((sp = search(lastid,tagtable.head)) == 0) {
  371.                         sp = xalloc(sizeof(SYM));
  372.                         sp->name = litlate(lastid);
  373.                         sp->tp = xalloc(sizeof(TYP));
  374.                         sp->tp->type = ztype;
  375.                         sp->tp->lst.head = 0;
  376.                         sp->storage_class = sc_type;
  377.                         sp->tp->sname = sp->name;
  378.                         getsym();
  379.                         if(lastst != begin)
  380.                                 error(ERR_INCOMPLETE);
  381.                         else    {
  382.                                 insert(sp,&tagtable);
  383.                                 getsym();
  384.                                 structbody(sp->tp,ztype);
  385.                                 }
  386.                         }
  387.                 else
  388.                         getsym();
  389.                 head = sp->tp;
  390.                 }
  391.         else    {
  392.                 tp = xalloc(sizeof(TYP));
  393.                 tp->type = ztype;
  394.                 tp->sname = 0;
  395.                 tp->lst.head = 0;
  396.                 if( lastst != begin)
  397.                         error(ERR_INCOMPLETE);
  398.                 else    {
  399.                         getsym();
  400.                         structbody(tp,ztype);
  401.                         }
  402.                 head = tp;
  403.                 }
  404. }
  405.  
  406. structbody(tp,ztype)
  407. TYP             *tp;
  408. enum e_bt       ztype;
  409. {       int     slc;
  410.         slc = 0;
  411.         tp->val_flag = 1;
  412.         while( lastst != end) {
  413.                 if(ztype == bt_struct)
  414.                         slc += declare(&(tp->lst),sc_member,slc,ztype);
  415.                 else
  416.                         slc = imax(slc,declare(&tp->lst,sc_member,0,ztype));
  417.                 }
  418.         tp->size = slc;
  419.         getsym();
  420. }
  421.  
  422. compile()
  423. /*
  424.  *      main compiler routine. this routine parses all of the
  425.  *      declarations using declare which will call funcbody as
  426.  *      functions are encountered.
  427.  */
  428. {       while(lastst != eof) {
  429.                 dodecl(sc_global);
  430.                 if( lastst != eof)
  431.                         getsym();
  432.                 }
  433.         dumplits();
  434. }
  435.  
  436. dodecl(defclass)
  437. enum e_sc       defclass;
  438. {
  439.         for(;;) {
  440.             switch(lastst) {
  441.                 case kw_register:
  442.                         getsym();
  443.                         if( defclass != sc_auto && defclass != sc_member )
  444.                                 error(ERR_ILLCLASS);
  445.                         goto do_decl;
  446.                 case id:
  447.                         if(defclass == sc_auto)
  448.                                 return;
  449. /*                      else fall through to declare    */
  450.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  451.                 case kw_long: case kw_struct: case kw_union:
  452.                 case kw_enum: case kw_void:
  453.                 case kw_float: case kw_double:
  454. do_decl:            if( defclass == sc_global)
  455.                         lc_static +=
  456.                             declare(&gsyms,sc_global,lc_static,bt_struct);
  457.                     else if( defclass == sc_auto)
  458.                         lc_auto +=
  459.                             declare(&lsyms,sc_auto,lc_auto,bt_struct);
  460.                     else
  461.                         declare(&lsyms,sc_auto,0,bt_struct);
  462.                     break;
  463.                 case kw_static:
  464.                         getsym();
  465.                         if( defclass == sc_member)
  466.                             error(ERR_ILLCLASS);
  467.             if( defclass == sc_auto )
  468.               lc_static += 
  469.               declare(&lsyms,sc_static,lc_static,bt_struct);
  470.             else
  471.               lc_static +=
  472.               declare(&gsyms,sc_static,lc_static,bt_struct);
  473.                         break;
  474.                 case kw_extern:
  475.                         getsym();
  476.                         if( defclass == sc_member)
  477.                             error(ERR_ILLCLASS);
  478.                         ++global_flag;
  479.                         declare(&gsyms,sc_external,0,bt_struct);
  480.                         --global_flag;
  481.                         break;
  482.                 default:
  483.                         return;
  484.                 }
  485.             }
  486. }
  487.  
  488.