home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / object.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  11KB  |  463 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1991 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #include "defines.h"
  7. #include "nasm.h"
  8. #include "labels.h"
  9. #include "seg.h"
  10. #include "object.h"
  11. #include "debug.h"
  12. #include "code.h"
  13. #include "imm.h"
  14.  
  15. #if __NSTDC__
  16. static unsigned   cnt_refs( ref huge *),
  17.                   help_tree( expr huge *);
  18. static void       hdump_tree( expr huge *),
  19.                   cnt_tree( expr huge *),
  20.                   dump_tree( expr huge *);
  21. #else
  22. static unsigned   cnt_refs(),
  23.                   help_tree();
  24. static void       hdump_tree(),
  25.                   cnt_tree(),
  26.                   dump_tree();
  27. #endif
  28.  
  29. static byte  huge *dspace;
  30. extern label huge *h_global[],
  31.              huge *h_macro[];
  32.  
  33. extern lword    __lx;
  34.  
  35. #define loop_symbols( p, i, k)                        \
  36.    for( k = 0; k != 2; k++)                           \
  37.       for( i = 0; i != SEP; i++)                      \
  38.          if( p = k ? h_macro[ i] : h_global[ i])
  39.  
  40.  
  41. /* ---------------------------------------------------------- */
  42. /* If I weren't so enamored of having all structures kept in  */
  43. /* seperate little file areas,this might be done a bit easier */
  44. /* For clean_labels: Get rid of leftover expression trees     */
  45. /*                   that fix nothing  (--> error ??)         */
  46. /* ---------------------------------------------------------- */
  47. void  clean_labels()
  48. {
  49.    register label huge *p;
  50.    register ref huge   *q, huge *old;
  51.    register expr huge  *e;
  52.    register int         i, j;
  53.  
  54.    clean_mats();                 /* clean macro @... labels */
  55.    loop_symbols( p, i, j)
  56.       do
  57.          if( dumpable( p) && (q = p->refs))
  58.             for( old = 0; q; q = q->next)
  59.             {
  60.                for( e = q->ref; ! e->fix; e = e->zonk.t);  /* ;; */
  61.                if( e->fix == FIX_NOTHING)
  62.                   if( ! old)
  63.                      p->refs = q->next;
  64.                   else
  65.                      old->next = q->next;
  66.                else
  67.                   old = q;
  68.             }
  69.       while( p = p->next);
  70. }
  71.  
  72.  
  73. static unsigned   cnt_refs( p)
  74. register ref huge *p;
  75. {
  76.    register unsigned   i;
  77.  
  78.    ENTER("cnt_refs");
  79.    for( i = 0; p; i++, p = p->next);
  80.    LEAVE();
  81.    return( i);
  82. }
  83.  
  84. lword   sym_size()
  85. {
  86.    register lword      x = 0;
  87.    register label huge *p;
  88.    static unsigned     index = 1;
  89.    register int        i;
  90.    int                 j;
  91.  
  92.    ENTER("sym_size");
  93.    loop_symbols( p, i, j)
  94.       do
  95.          if( dumpable( p) && (! j || (p->refs)))
  96.          {
  97.             x    += sizeof( linksymbol);
  98.             p->no = index++;
  99.          }
  100.       while( p = p->next);
  101.    LEAVE();
  102.    return( x);
  103. }
  104.  
  105.  
  106. void  sym_makbuf()
  107. {
  108.    register label huge *p;
  109.    register byte huge  *q = __program;
  110.    register byte huge  *s;
  111.    register int        i, j;
  112.    int                 k;
  113.  
  114.    ENTER("sym_makbuf");
  115.    loop_symbols( p, i, k)
  116.       do
  117.          if( dumpable( p) && (! k || (p->refs)))
  118.          {
  119.             if( ! p->refs)
  120.             {
  121.                pdbyte( q, p->val);
  122.             }
  123.             else
  124.             {
  125.                pdbyte( q, 0);
  126.             }
  127.             pdbyte( q, p->no);
  128.             pdbyte( q, p->type);
  129.             j = cnt_refs( p->refs);
  130.             pdbyte( q, 0xC002);         /* just a filler    */
  131.             plbyte( q, j);              /* VORSICHT! MACRO  */
  132.             s = (byte *) p->name;
  133.             j = 0;
  134.             while( j++ < SIGNIFICANT && (*q++ = *s++));
  135.             while( j++ < SIGNIFICANT)
  136.                *q++ = 0;
  137.          }
  138.       while( p = p->next);
  139.    LEAVE();
  140. }
  141.  
  142.  
  143. lword  rel_size()
  144. {
  145.    register label huge *p;
  146.    register lword      x = 0;
  147.    int                 i, j;
  148.  
  149.    ENTER("rel_size");
  150.    loop_symbols( p, i, j)
  151.       do
  152.          if( dumpable( p) && p->refs)
  153.             x += (lword) (cnt_refs( p->refs) << 1);
  154.       while( p = p->next);
  155.    LEAVE();
  156.    return( x);
  157. }
  158.  
  159.  
  160. void  rel_makbuf()
  161. {
  162.    register label huge *p;
  163.    register byte huge  *q = __program;
  164.    register ref huge   *s;
  165.    int                 i, k;
  166.  
  167.    ENTER("rel_makbuf");
  168.    loop_symbols( p, i, k)
  169.       do
  170.          if( dumpable( p) && (s = p->refs))
  171.             do
  172.             {
  173.                pdbyte( q, s->ref->no);
  174.             }
  175.             while( s = s->next);
  176.       while( p = p->next);
  177.    LEAVE();
  178. }
  179.  
  180.  
  181. static unsigned   fixes;
  182.  
  183. lword    fix_size()
  184. {
  185.    return( (lword) fixes * sizeof( f_dropped));
  186. }
  187.  
  188.  
  189. void  fix_makbuf()
  190. {
  191.    register label huge *p;
  192.    register ref huge   *q;
  193.    register expr huge  *e;
  194.    register int        i, k;
  195.  
  196.    ENTER("fix_makbuf");
  197.    dspace =  __program;
  198.    loop_symbols( p, i, k)
  199.       do
  200.          if( dumpable( p) && (q = p->refs))
  201.             do
  202.             {
  203.                register byte huge *x = dspace;
  204.  
  205.                for( e = q->ref; ! e->fix; e = e->zonk.t);
  206.                dspace += sizeof( f_dropped);
  207.                if( e->fix == FIX_LABEL)
  208.                {
  209.                   pdbyte( x, e->zonk.fixp->poof.label->no);
  210.                   pdclr( x);
  211.                   dclr( x);
  212.                }
  213.                else
  214.                {
  215.                   register fix huge  *f = e->zonk.fixp;
  216.                   
  217.                   pdbyte( x, f->poof.block->no);
  218.                   if( f->imm)
  219.                   {
  220.                      pdbyte( x, f->imm->no);
  221.                   }
  222.                   else
  223.                   {
  224.                      pdclr( x);
  225.                   } 
  226.                   dbyte( x, f->index);
  227.                }  
  228.             }
  229.             while( q = q->next);
  230.       while( p = p->next);
  231.    LEAVE();
  232. }
  233.  
  234. /* ---------------------------------------------------------- */
  235. /*                     Can do this only ONCE!!                */
  236. /* ---------------------------------------------------------- */
  237. static unsigned   xindex = 1;
  238.  
  239. static unsigned   help_tree( p)
  240. register expr huge *p;
  241. {
  242.    p->no = xindex++;
  243.    if( p->l)
  244.    {
  245.       help_tree( p->l);
  246.       if( p->r)
  247.          help_tree( p->r);
  248.    }
  249. }
  250.  
  251. static void cnt_tree( p)
  252. expr huge *p;
  253. {
  254.    ENTER("cnt_tree");
  255.    if( ! p->no)
  256.    {
  257.       while( ! p->fix)    /* walk to the top */
  258.          p = p->zonk.t;
  259.       p->zonk.fixp->no = fixes++;
  260.       help_tree( p);
  261.    }
  262. }
  263.  
  264.  
  265. lword    exp_size()
  266. {
  267.    register label huge *p;
  268.    register ref huge   *q;
  269.    int                 i, k;
  270.  
  271.    ENTER("exp_size");
  272.    loop_symbols( p, i, k)
  273.       do
  274.          if( dumpable( p) && (q = p->refs))
  275.             do
  276.                cnt_tree( q->ref);
  277.             while( q = q->next);
  278.       while( p = p->next);
  279.    LEAVE();
  280.    return( (lword) (xindex - 1) * sizeof( e_dropped));
  281. }
  282.  
  283.  
  284. static void   hdump_tree( x)
  285. expr huge *x;
  286. {
  287.    ENTER("hdump_tree");
  288.    {  /* ; */
  289.       register expr huge *p = x;
  290.       register byte huge *q = dspace;
  291.  
  292.       dspace += sizeof( e_dropped);
  293.  
  294.       pdbyte( q, p->val);
  295.       pdbyte( q, p->aux);
  296.       pdbyte( q, p->label ? p->label->no : 0);
  297.       *q++ = p->op;
  298.       if( *q++ = p->fix)
  299.          if( p->fix == FIX_LABEL)
  300.          {
  301.             pdbyte( q, ((label *) p->zonk.t)->no);
  302.          }
  303.          else
  304.          {
  305.             pdbyte( q, p->zonk.fixp->no);
  306.          }
  307.       else
  308.       {
  309.          pdbyte( q, p->zonk.t ? p->zonk.t->no : 0);
  310.       }
  311.       pdbyte( q, p->l ? p->l->no : 0);
  312.       dbyte( q,  p->r ? p->r->no : 0);
  313.    }
  314.  
  315.    if( x->l)
  316.    {
  317.       hdump_tree( x->l);
  318.       if( x->r)
  319.          hdump_tree( x->r);
  320.    }
  321.    x->no = 0;                             /* clear >no< so that expr */
  322.    LEAVE();                               /* won't be dumped again   */
  323. }
  324.  
  325.  
  326. void  dump_tree( p)
  327. expr huge *p;
  328. {
  329.    ENTER("dump_tree");
  330.    if( p->no)
  331.    {
  332.       while( ! p->fix)    /* walk to the top */
  333.          p = p->zonk.t;
  334.       hdump_tree( p);
  335.    }
  336.    LEAVE();
  337. }
  338.  
  339.  
  340. void  exp_makbuf()
  341. {
  342.    register label huge *p;
  343.    register ref huge   *q;
  344.    int                 i, k;
  345.  
  346.    ENTER("exp_makbuf");
  347.    dspace =  __program;
  348.    loop_symbols( p, i, k)
  349.       do
  350.          if( dumpable( p) && (q = p->refs))
  351.             do
  352.                dump_tree( q->ref);
  353.             while( q = q->next);
  354.       while( p = p->next);
  355.    LEAVE();
  356. }
  357.  
  358. extern imm huge *hip, huge *cip;
  359.  
  360. lword   imm_size()
  361. {
  362.    register imm huge *p;
  363.    register word     i = 1;
  364.  
  365.    ENTER("imm_size");
  366.    for( p = hip; p; p = p->next)
  367.       p->no = i++;
  368.    LEAVE();
  369.    return( (lword) (i - 1) * sizeof( i_dropped));
  370. }
  371.  
  372.  
  373. void  imm_makbuf()
  374. {
  375.    register imm huge  *p;
  376.    register byte huge *q = __program;
  377.  
  378.    ENTER("imm_makbuf");
  379.    for( p = hip; p; p = p->next)
  380.    {
  381.       pdbyte( q, p->block->no);
  382.       pdbyte( q, p->type);
  383.       pdbyte( q, p->offset);
  384.       pdbyte( q, p->val);
  385.    }
  386.    LEAVE();
  387. }
  388.  
  389.  
  390. extern seg huge *h_seg, *sp;
  391.  
  392. lword    seg_size()
  393. {
  394.    register seg huge *p;
  395.    register word  i;
  396.  
  397.    for( p = h_seg, i = 0;  p; i++, p = p->next)
  398.       p->no = i;
  399.    return( (lword) i * sizeof( s_dropped));
  400. }
  401.  
  402.  
  403. void  seg_makbuf()
  404. {
  405.    register seg huge  *p;
  406.    register byte huge *q;
  407.  
  408.    for( p = h_seg, q = __program; p ; p = p->next)
  409.    {
  410.       pdbyte( q, p->type);
  411.       pdbyte( q, p->index);
  412.       pdbyte( q, p->size);
  413.    }
  414. }
  415.  
  416.  
  417.  
  418. /* ;
  419.    -- "This looks exceptionally braindead, son. What where you dreaming
  420.       about, when you wrote this ??"
  421.    -- "Well ackshually *hic* there ish a shertain rashional behind
  422.       *hic* all shat."
  423.    -- <slap> "Swine! Have you been drinking again ? Well I just gonna
  424.       have to teach you some ... <slap><slap> ..manners <slap> <*crack*>
  425.                                  <WAAAAH!!>
  426.    -- <Sob> "No need <Sob-whine> to break my arm, I was just kidding.."
  427.                                  <Waaah>
  428.    -- "Well that'll teach you to make fun of your old dad, now son
  429.        about this piece of shit up there..."
  430.    --  <Sob>
  431.    -- "Well!!" <slap>
  432.    -- <WAAH!> "Don't hit me again! <Sob> I am telling you, I am telling
  433.       <Sob> you!!" <SNEEEZE> <Sob>
  434.    -- "I am listening son, and I am getting impatient" [and so do we...]
  435.    -- "Ok just <Sob> assume for a moment, that we have some really large
  436.       expression and some compiler that rather prefers to not use registers,
  437.       for parameter passing (as there are some). Now for the little
  438.       piece enclosed in the { } it would be preferable, to have
  439.       this executed with register variables, but we certainly don't want
  440.       those to be stacked when the recursive calls take place."
  441.    -- "Oh son, why didn't you say so in the first place. I needn't have to
  442.       hit you then. Lets be good again, shall we ?"
  443.    -- "Oh yes <whimper>. -- uh Dad ??
  444.    -- "..Yes.."
  445.    -- "Could you -er- drive me to the hospital? My arm is hurting pretty
  446.        <sob> badly."
  447.    -- "Don't push your luck again son!"
  448.    -- <whimper>
  449.    
  450.    (later: That was when I was still learning to write C-programs. Of 
  451.            course this doesn't make sense on a more profound level of
  452.            understanding.)
  453.    (much later: I must have dropped down a level of profoundness again)
  454. */
  455.  
  456. /* ;;
  457.    If the top fix is FIX_NOTHING, that means it is an assembler internal
  458.    expression, that wasn't used anywhere. Presently this only happenes
  459.    when macros are started. -> LEXPR -> COPYEXPR ... lexpr->expr remains
  460.    unused  except for copying.
  461.    (later: well not completely true anymore, still "about right")
  462. */
  463.