home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / oxcc1433.zip / SRC / OXCCB.C < prev    next >
C/C++ Source or Header  |  1995-11-05  |  198KB  |  9,117 lines

  1. /*
  2.     oxccb.c -- v1.430 architecture neutral format (anf) to bytecode generator
  3.  
  4.     Copyright (c) 1995
  5.     Norman D. Culver dba
  6.     Oxbow Software
  7.     1323 S.E. 17th Street #662
  8.     Ft. Lauderdale, FL 33316
  9.     (305) 527-1663 Voice
  10.     (305) 760-7584 Fax
  11.     (305) 760-4679 Data
  12.     norman.culver@channel1.com
  13.     All rights reserved.
  14.  
  15.  * Redistribution and use in source and binary forms are permitted
  16.  * provided that: (1) source distributions retain this entire copyright
  17.  * notice and comment, and (2) distributions including binaries display
  18.  * the following acknowledgement:  ``This product includes software
  19.  * developed by Norman D. Culver dba Oxbow Software''
  20.  * in the documentation or other materials provided with the distribution
  21.  * and in all advertising materials mentioning features or use of this
  22.  * software.
  23.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  24.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  25.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26.  
  27. */
  28. #define MAJOR_VERSION 1
  29. #define MINOR_VERSION 433
  30.  
  31. void oxcc_debug();
  32. int __builtin_iv();
  33. void bterpdebug(void);
  34.  
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <stdio.h>
  38. #include <setjmp.h>
  39. #include <time.h>
  40.  
  41. #define SUPPORT_LONG_DOUBLE 1
  42. #define SUPPORT_LONG_LONG 1
  43.  
  44. #define NEED_SPELLING 1
  45. #define NEED_BYTECODES 1
  46. #define NEED_AOUT_FORMAT 1
  47. #include "oxbytes.h"
  48.  
  49. #define NEED_FUNCTHUNK 1
  50. #define NEED_ANFDEFS 1
  51. #include "oxanf.h"
  52.     
  53. #define PROG oxccb
  54. #define USING_FRAMEWORK 1
  55. #define HOST_IS_LITTLE_ENDIAN 1
  56. #define REALLY_NEED_OFFSETS 1
  57. #define FUNCDATA (iv->category+1)
  58.  
  59. #define VFPRINTF(a,b) vfprintf(stderr,a,b)
  60. #define PERROR prerror
  61. #define PWARN prwarn
  62. #define PRINTF info
  63. static void prerror(const char *, ...);
  64. static void prwarn(const char *, ...);
  65. static void info(const char *, ...);
  66. int cfeprintf(const char *, ...);
  67.  
  68. #define FILEWRITE(buf, cnt)\
  69. {if(!iv->errors){if(fwrite(buf, 1, cnt, iv->outfile) != cnt)iv->errors = 12;}}
  70.  
  71. #define ROUNDING(a,b) ((b-(a&(b-1)))&(b-1))
  72. #define ROUNDUP(a,b) a += ROUNDING(a,b)
  73.  
  74. #define KEYEQ(a,b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
  75. #define KEYLT(a,b) (((a)[1] < (b)[1]) || ((a)[1] == (b)[1] && (a)[0] < (b)[0]))
  76. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  77.  
  78. /* ======================== CONCATENIZATION MACROS ==================== */
  79.  
  80. #define    _cat2_(a, b)    a##b
  81. #define _cat_(a, b)    _cat2_(a, b)
  82. #define Global(a) _cat_(PROG, a)
  83.  
  84. #define _pname2_(x)    #x
  85. #define _pname1_(x)    _pname2_(x)
  86. #define pName        _pname1_(PROG)
  87.  
  88.  
  89. /* ============== ENDIAN MACROS (input format is litle endian) ==== */
  90.  
  91. #if HOST_IS_LITTLE_ENDIAN
  92. #define GL(a) a
  93. #define GS(a) a
  94. #define PL(a) a
  95. #define PS(a) a
  96. #else
  97. #endif
  98.  
  99. /* =================== INPUT DATA FORMATS ========================== */
  100.  
  101. #define INFILE_SYMNUM 1
  102. #define OUTFILE_SYMNUM 2
  103.  
  104. static unsigned char binops[] = {0,0,
  105. ADD,SUB,MUL,DIV,LSH,RSH,MOD,OR,XOR,AND,EQ,NE,LT,GT,LE,GE,
  106. NEG,COMP,NOT
  107. };
  108. /* ====================== STRUCTURES AND TYPEDEFS ======================== */
  109. typedef struct _jl {
  110.     struct _jl *next;
  111.     void *p;
  112.     char *q;
  113.     long *plabelval;
  114.     long offset;
  115. } *PJL;
  116.  
  117. typedef struct _el {
  118.     struct _el *next;
  119.     long spot;
  120.     short symnum;
  121. } *PEL;
  122.  
  123. typedef struct _afile {
  124.     unsigned char *file_p;
  125.     PopI header_p;
  126.     PopI size_p;
  127.     unsigned char *symtext_p;
  128.     unsigned char *prog_p;
  129.     unsigned char *data_p;
  130.     unsigned char *switch_p;
  131.     unsigned char *decl_p;
  132.     unsigned char *maxtemp_p;
  133.     unsigned char *seg_p;
  134.     unsigned char **symaddr;
  135.     unsigned char **decladdr;
  136.     unsigned long thunk_offset;
  137.     unsigned long bss_offset;
  138.     int maxtemp;
  139.     int maxtempclass;
  140.     void *datatbl;
  141.     short *symtran;
  142.     unsigned short *decltran;
  143.     int filenum;
  144.     int numsyms;
  145.     int numdecls;
  146.     int numrelocs;
  147.     int numsegs;
  148. } *Pafile;
  149.  
  150. typedef struct _iv {
  151.     int category;
  152.     FILE *outfile;
  153.     struct exec *header;
  154.     unsigned char **symaddr;
  155.     unsigned char **decladdr;
  156.     int remove_infile;
  157.     int argc;
  158.     char **argv;
  159.     int numfiles;
  160.     int lastlabel;
  161.     int errors;
  162.     int numsyms;
  163.     int numdecls;
  164.     int numsegs;
  165.     int maxtemp;
  166.     int maxtempclass;
  167.     unsigned long total_size;
  168.     unsigned long thunk_offset;
  169.     unsigned long bss_offset;
  170.  
  171.     struct _nodeO *ob_usedhead;
  172.     struct _nodeO *ob_usedtail;
  173.     struct _nodeO *ob;
  174.     unsigned char *ob_buf;
  175.     int ob_bufcnt;
  176.     struct _nodeO *first_ob;
  177.  
  178.     struct _nodeC *cod_usedhead;
  179.     struct _nodeC *cod_usedtail;
  180.     struct _nodeC *cod;
  181.     unsigned char *cod_buf;
  182.     int cod_bufcnt;
  183.     struct _nodeC *first_cod;
  184.     struct _nodeC *regcode;
  185.  
  186.     long first_temp;
  187.     long killop;
  188.     long stackdepth;
  189.     long maxdepth;
  190.     long mindepth;
  191.     long numnested;
  192.     long lastline;
  193.     void *reloctbl;
  194.     void *extrntbl;
  195.     void *gbltbl;
  196.     void *symtbl;
  197.     void *labeltbl;
  198.     void *newlabeltbl;
  199.     void *tmptbl;
  200.     void *segtbl;
  201.     void *functbl;
  202.     void *finalsymtbl;
  203.     void *finalstringpack;
  204.     long finalpacksize;
  205.     void *datatbl;
  206.     void *builtintbl;
  207.     int in_builtin;
  208.     int has_structret;
  209.     int temps_written;
  210.     unsigned char *obuf;
  211.     unsigned char *obufstart;
  212.     PEL extbuf;
  213.     void *extbufstart;
  214.     int extcnt;
  215.     int extbufcnt;
  216.     PEL finextbuf;
  217.     void *finextbufstart;
  218.     int finextcnt;
  219.     int finextbufcnt;
  220.     PJL jbuf;
  221.     void *jbufstart;
  222.     int jmpcnt;
  223.     int jbufcnt;
  224.     long obufcnt;
  225.     long out_offset;
  226.     long func_offset;
  227.     int extmark;
  228.     short markedsym[10];
  229.     char *markedbuf[10];
  230.     int filenum;
  231.     Pafile files[1024];
  232.     char debug;
  233.     char only_debug;
  234.     char strip;
  235.     char listing_wanted;
  236. } *Piv;
  237.  
  238. struct _gloval {
  239.     char *symname;
  240.     int symnum;
  241.     unsigned char *p;
  242.     Pafile pf;
  243. };
  244. struct _rkey {/* key area of reloctbl node */
  245.     unsigned long spot;
  246.     short fileno;
  247.     unsigned char opcode;
  248.     char rsize;
  249. };
  250. struct _rval {/* value area of reloctbl node */
  251.     unsigned char *p;
  252.     unsigned long *base;
  253.     long offset;
  254.     short rsym;
  255. };
  256.  
  257.  
  258. /* Internal User API */
  259. static void *Cmalloc(int category, unsigned amount);
  260. static void *Ccalloc(int category, unsigned nelems, unsigned elemsize);
  261. static void *Crealloc(int category, void* buf, unsigned newsize);
  262. static void Cfree(int category, void* buf);
  263. static void Cfreecat(int category);
  264. static int Cmemrange(int category, unsigned* minp, unsigned* maxp);
  265. static int Cusedrange(int category, unsigned* minp, unsigned* maxp);
  266. static void Ctotrange(unsigned* minp,unsigned* maxp);
  267. static int Cnewcat(void);
  268. static void Cguard(int category);
  269. static void* NewSymTable(int category, int nbins);
  270. static int SymFind(void *tbl, void *key, void *result);
  271. static int SymFindRange(void *tbl, void *key, void *result);
  272. static void *SymInsert(void *tbl, void *key, void *value, int datsiz);
  273. static int StringInsert(void *tbl, char *string, void *result);
  274. static int StringFind(void *tbl, char *string, void *result);
  275. static void SymDelete(void *tbl, void *key);
  276. static int SymHead(void *tbl);
  277. static int SymNext(void *tbl);
  278. static void SymGetMark(void *tbl, void *markptr);
  279. static int SymMarkNext(void *tbl, void *mark);
  280. static void SymSetMark(void *tbl, void *markptr);
  281. static void SymKey(void *tbl, void *keyptr);
  282. static void SymValue(void *tbl, void *datptr);
  283. static void *seg_find(Piv iv, int id);
  284. static char *filenameof(char *path);
  285. static char *propernameof(Piv iv, char *name);
  286.  
  287. /* END: User API */
  288.  
  289. /* ====================== PUT UNIQUE CODE HERE ========================= */
  290. static void newlabel_insert(Piv iv, long label);
  291. static long newlabel_fix(Piv iv, long label);
  292. static void *do_stmt(Piv iv, unsigned char *p);
  293. static void *do_expr(Piv iv, unsigned char *p);
  294. static void do_bracket(Piv iv, unsigned char *p, unsigned char *q);
  295. static void *do_something(Piv iv, unsigned char *p);
  296. extern char *ctime();
  297.  
  298. /* ===================== BYTECODE OUTPUT GENERATOR ======================= */
  299. struct _nodeOBUF
  300. {
  301.     struct _nodeOBUF *next;
  302.     long cnt;
  303.     char buf[1];
  304. };
  305. typedef struct _nodeC
  306. {
  307.     struct _nodeC *next;
  308.     struct _nodeOBUF *ee;
  309. } NODEC, *PNODEC;
  310.  
  311. typedef struct _nodeO
  312. {
  313.     struct _nodeO *next;
  314.     unsigned char *p;
  315.     ND d;
  316.     ND l;
  317.     ND r;
  318.     PNODEC startinst;
  319.     PNODEC endinst;
  320. } NODEO, *PNODEO;
  321.  
  322. static unsigned char get_datasize(unsigned char, PND);
  323. static void link_cod(Piv);
  324.  
  325. static char *notice = 
  326.     "  Generated by Oxbow Software Bytecode Backend version %d.%d\n*/\n\n";
  327.  
  328. static struct _nd longtype =         {D_SIGNED,0,B4,0,4,0,0};
  329. static struct _nd longlongtype =     {D_SIGNED,0,B8,0,8,0,0};
  330. static char padit[8];    /* in bss */
  331.  
  332. void
  333. bterpdebug(){}
  334.  
  335. static long
  336. symnumof(Piv iv, char *symb)
  337. {
  338. struct _gloval *valp;
  339.  
  340.     if(StringFind(iv->gbltbl, symb, &valp))
  341.         return (long)valp->pf->symtran[valp->symnum];
  342.     return 0;
  343. }
  344. static void
  345. buildin(Piv iv, char *symb, unsigned char code)
  346. {
  347. long key[2];
  348.  
  349.     if((key[0] = symnumof(iv, symb)))
  350.     {
  351.         key[1] = 0;
  352.         SymInsert(iv->builtintbl, key, &code, 1);
  353.     }
  354. }
  355. static void
  356. install_builtins(Piv iv)
  357. {/* USE THIS TO INSTALL WHATEVER BUILTINS ARE IN THE TARGET INTERPRETER */
  358. #define BUILDIN(a,b) buildin(iv,#a,b)
  359.  
  360.     iv->builtintbl = NewSymTable(iv->category, 191);
  361.  
  362.     BUILDIN(alloca,ALLOCA);
  363.     BUILDIN(strlen,STRLEN);
  364.     BUILDIN(strcpy,STRCPY);
  365.     BUILDIN(strcat,STRCAT);
  366.     BUILDIN(memcpy,MEMCPY);
  367.     BUILDIN(memmove,MEMMOVE);
  368.     BUILDIN(bzero,BZERO);
  369.     BUILDIN(malloc,MALLOC);
  370.     BUILDIN(calloc,CALLOC);
  371.     BUILDIN(realloc,REALLOC);         
  372.     BUILDIN(setjmp,SETJMP);
  373.     BUILDIN(longjmp,LONGJMP);
  374.     BUILDIN(abort,ABORT);
  375.     BUILDIN(exit, EXIT);
  376.     BUILDIN(_exit,EXIT);
  377.     BUILDIN(bterpdebug,DEBUG);
  378.     BUILDIN(bterpnodebug,NODEBUG);
  379.  
  380. #undef BUILDIN
  381. }
  382. static long
  383. final_strofs(Piv iv, char *string)
  384. {
  385. long *result;
  386.  
  387.     if(StringFind(iv->finalsymtbl, string, &result))
  388.         return result[2];    
  389.     return 0;
  390. }
  391. static short
  392. final_symnum(Piv iv, short symnum)
  393. {
  394. long *result;
  395.  
  396.     if(StringFind(iv->finalsymtbl, iv->symaddr[symnum], &result)) {
  397.         return result[1]-1;
  398.     }
  399.     return 0;
  400. }
  401. static void
  402. make_final_symtab(Piv iv)
  403. {
  404. int i;
  405.     iv->finalsymtbl = NewSymTable(iv->category, 0);
  406.     if(SymHead(iv->gbltbl))
  407.     {
  408.         i = 0;
  409.         while(SymNext(iv->gbltbl))
  410.         {
  411.         long *result;
  412.         struct _gloval *valp;
  413.  
  414.             SymValue(iv->gbltbl, &valp);
  415.             if(*(valp->p))
  416.             {
  417.             long key[2];
  418.                 key[0] = valp->pf->symtran[valp->symnum];
  419.                 key[1] = 0;
  420.                 if(!SymFind(iv->builtintbl, key, NULL))
  421.                 {
  422.                   if(!StringInsert(iv->finalsymtbl, valp->symname, &result))
  423.                   {/* New Entry */
  424.                     result[1] = ++i;
  425.                   }
  426.                 }
  427.             }
  428.         }
  429.     }
  430. }
  431. static void
  432. adjust_labels(Piv iv, long base, long adjust)
  433. {
  434. long *key;
  435. long *val;
  436.     if(SymHead(iv->newlabeltbl))
  437.     {
  438.         while(SymNext(iv->newlabeltbl))
  439.         {
  440.             SymKey(iv->newlabeltbl, &key);
  441.             SymValue(iv->newlabeltbl, &val);
  442.             if(key[1] == iv->filenum &&    val[0] > base) {
  443.                 val[0] += adjust;
  444.             }
  445.         }
  446.     }
  447. }
  448. static void
  449. addto_extlist(Piv iv, char *buf)
  450. {
  451. void *next;
  452.     while(iv->extmark > 0)
  453.     {
  454.     long offset = iv->markedbuf[iv->extmark] - buf;
  455.         next = iv->extbuf;
  456.         if(iv->extbufcnt >= sizeof(struct _el))
  457.         {
  458.             iv->extbuf++;
  459.         }
  460.         else
  461.         {
  462.             iv->extbufcnt = 4080;
  463.             iv->extbuf = Ccalloc(FUNCDATA, 1, iv->extbufcnt);
  464.         }
  465.         *((void**)next) = iv->extbuf;
  466.         iv->extbuf->spot = iv->out_offset+iv->func_offset+offset;
  467.         iv->extbuf->symnum = iv->markedsym[iv->extmark];
  468.         ++iv->extcnt;
  469.         --iv->extmark;
  470.         iv->extbufcnt -= sizeof(struct _el);
  471.     }
  472. }
  473. static void
  474. save_extlocs(Piv iv)
  475. {
  476. PEL pel = iv->extbufstart;
  477. void *next;
  478.  
  479.     while(pel)
  480.     {
  481.         next = iv->finextbuf;
  482.         if(iv->finextbufcnt >= sizeof(struct _el))
  483.         {
  484.             iv->finextbuf++;
  485.         }
  486.         else
  487.         {
  488.             iv->finextbufcnt = 4080;
  489.             iv->finextbuf = Ccalloc(iv->category, 1, iv->finextbufcnt);
  490.         }
  491.         *((void**)next) = iv->finextbuf;
  492.         iv->finextbuf->spot = pel->spot;
  493.         iv->finextbuf->symnum = pel->symnum;
  494.         ++iv->finextcnt;
  495.         iv->finextbufcnt -= sizeof(struct _el);
  496.         pel = pel->next;
  497.     }
  498. }
  499. static void
  500. adjust_externs(Piv iv, long base, long adjust)
  501. {
  502. PEL pel = iv->extbufstart;
  503.  
  504.     while(pel)
  505.     {
  506.         if(pel->spot > base)
  507.             pel->spot += adjust;
  508.         pel = pel->next;
  509.     }
  510. }
  511.  
  512. static int
  513. shorten_jmps(Piv iv)
  514. {
  515. int scnt;
  516. PJL jp;
  517. long jmp_offset;
  518. long label_offset;
  519. long diff;
  520. long offset_adjust;
  521. int sign;
  522. unsigned char *q, *osiz;
  523. int cursize, newsize;
  524.  
  525.     scnt = 0;
  526.     jp = iv->jbufstart;
  527.     offset_adjust = 0;
  528.     while(jp)
  529.     {
  530.         osiz = jp->q+4;                /* points to size in obuf */
  531.         q = osiz+4;                    /* points to the JMP inst in obuf */
  532.         cursize = *q & 3;            /* from the output bytecode */
  533.         jmp_offset = jp->offset + offset_adjust;
  534.         jp->offset = jmp_offset;    /* reset the offset for this inst */
  535.         label_offset = *jp->plabelval;
  536.         diff = label_offset - jmp_offset;
  537.  
  538.         sign = 1;
  539.         if(diff < 0)
  540.         {
  541.             sign = -1;
  542.             diff = -diff;
  543.         }
  544.         if(diff < 0x7fL)
  545.             newsize = 0;
  546.         else if(diff < 0x7fffL)
  547.             newsize = 1;
  548.         else if(diff < 0x007fffffL)
  549.             newsize = 2;
  550.         else
  551.             newsize = 3;
  552.         if(cursize != newsize)
  553.         {/* DO SOMETHING */
  554.         long adj;
  555.         static long codesize[4] = {2L,3L,4L,5L};
  556.             ++scnt;                                /* something changed */
  557.             *q &= 0xfc;                        /* mask opcode */
  558.             *q |= newsize;                    /* set size in opcode */
  559.             *((long*)osiz) = codesize[newsize];    /* set new output size */
  560.             adj = codesize[newsize] - codesize[cursize];
  561.             offset_adjust += adj;    /* jmps below this point have new offset */
  562.             if(sign > 0) diff += adj;    /* label is below this point */
  563.  
  564.             /* Adjust all labels below this point */
  565.             adjust_labels(iv, jmp_offset, adj);
  566.             /* adjust all external address spots (text relocs) below this point */
  567.             adjust_externs(iv, jmp_offset, adj);
  568.         }
  569.         diff *= sign;    /* restore sign to the relative address */
  570.  
  571.         /* generate the pc relative address */
  572.  
  573.         ++q;    /* points the the address field of the JMP inst */
  574.         if(newsize == 0)
  575.             *((signed char*)q) = (signed char)(diff & 0xff);
  576.         else if(newsize == 1)
  577.             *((short*)q) = (short)(diff & 0xffff);
  578.         else if(newsize == 2)
  579.             *((long*)q) = diff<<8;
  580.         else
  581.             *((long*)q) = diff;
  582.         jp = jp->next;
  583.     }/* END: while (jp) */
  584.  
  585.     return scnt;
  586. }
  587.  
  588. static void
  589. setup_jmps(Piv iv, unsigned char *pdef)
  590. {
  591. struct {
  592.     long k1;
  593.     long k2;
  594. } key;    
  595. long *result;
  596. PJL jp;
  597. unsigned char *p;
  598.  
  599.     jp = iv->jbufstart;
  600.     while(jp)
  601.     {
  602.         p = jp->p;
  603.         key.k1 = GL( POP->data );    /* label number from source code */
  604.         key.k2 = iv->filenum;            /* source file number */
  605.         if(SymFind(iv->newlabeltbl, &key, &result))
  606.         {
  607.             jp->plabelval = result;    /* save pointer to label value */
  608.         }
  609.         else
  610.         {
  611.         PERROR(pName ":Syserr: jmp setup failed for `%s' label=%d file=%d p=%x code=%d\n",
  612.         iv->symaddr[GL(((Pop)pdef)->data)], key.k1, key.k2, p, *p);
  613.         }
  614.         jp = jp->next;
  615.     }
  616. }
  617. static void
  618. addto_jmplist(Piv iv, unsigned char *p, void *q)
  619. {
  620. void *next = iv->jbuf;
  621.  
  622.     if(iv->jbufcnt >= sizeof(struct _jl))
  623.     {
  624.         iv->jbuf++;
  625.     }
  626.     else
  627.     {
  628.         iv->jbufcnt = 4080;
  629.         iv->jbuf = Ccalloc(FUNCDATA, 1, iv->jbufcnt);
  630.     }
  631.     *((void**)next) = iv->jbuf;
  632.     iv->jbuf->p = p;
  633.     iv->jbuf->q = q;
  634.     iv->jbuf->offset = iv->out_offset+iv->func_offset-5;
  635.     iv->jbufcnt -= sizeof(struct _jl);
  636. }
  637. static void
  638. save_maxdepth(Piv iv, long symnum)
  639. {
  640. long key[2];
  641. long val[3];
  642.  
  643.     if(iv->functbl == 0)
  644.     {
  645.         iv->functbl = NewSymTable(iv->category, 277);
  646.     }
  647.     key[0] = symnum;
  648.     key[1] = 0;
  649.     val[0] = iv->maxdepth + (4*iv->numnested);
  650.     val[1] = iv->mindepth;    
  651.     val[2] = 0;
  652.     SymInsert(iv->functbl, key, val, 12);
  653. }    
  654. static long
  655. get_maxdepth(Piv iv, short symnum)
  656. {
  657. long key[2];
  658. long *result;
  659.  
  660.     key[0] = symnum;
  661.     key[1] = 0;
  662.     if(SymFind(iv->functbl, key, &result))
  663.     {
  664.         return result[0];
  665.     }
  666.     return 0;
  667. }
  668. static void
  669. printline(Piv iv, void *ptr)
  670. {
  671.     fprintf(iv->outfile, "Line:%ld:\n", *((long*)ptr));
  672. }
  673. static int
  674. print8(Piv iv, unsigned char *ptr, int size, long offset, int lf)
  675. {
  676. char buf[40];
  677. int i, j=0;
  678.  
  679.     i = sprintf(buf, "%8.8lx: ", offset);
  680.     while(j < size && j < 8)
  681.     {
  682.         i += sprintf(&buf[i], "%2.2x ", *ptr++);
  683.         ++j;
  684.     }
  685.     if(lf)
  686.         buf[i++] = '\n';
  687.     else
  688.     {
  689.         while(i < 35)
  690.             buf[i++] = ' ';
  691.     }
  692.     buf[i] = 0;
  693.     fprintf(iv->outfile, buf);
  694.     return j;
  695. }
  696. static void
  697. printinst(Piv iv, unsigned char *pc, long size)
  698. {
  699. int i, bufcnt;
  700. unsigned char *epc;
  701. char *pbuf[20];
  702.  
  703.   bufcnt = 0;
  704.   epc = pc + size;
  705.   for( ;pc < epc; ++pc)
  706.   {
  707.     switch(*pc)
  708.     {
  709.         case LOCATE|J1:
  710.         case LOCATE|J2:
  711.         case LOCATE|J3:
  712.         case LOCATE|J4:
  713.             pbuf[bufcnt++] = "locate|";
  714.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  715.             pc += Jcnt[*pc & 0x03];
  716.             break;
  717.         case LS|A1|B1:
  718.         case LS|A1|B2:
  719.         case LS|A1|B4:
  720.         case LS|A1|B8:
  721.         case LS|A2|B1:
  722.         case LS|A2|B2:
  723.         case LS|A2|B4:
  724.         case LS|A2|B8:
  725.         case LS|A3|B1:
  726.         case LS|A3|B2:
  727.         case LS|A3|B4:
  728.         case LS|A3|B8:
  729.             pbuf[bufcnt++] = "ls|";
  730.             pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  731.             pc += Acnt[(*pc>>2) & 0x03];
  732.             break;
  733.         case NEG|BYTE:
  734.         case NEG|SHORT:
  735.         case NEG|LONG:
  736.         case NEG|UBYTE:
  737.         case NEG|USHORT:
  738.         case NEG|ULONG:
  739.         case NEG|FLOAT:
  740.         case NEG|DOUBLE:
  741.             pbuf[bufcnt++] = "neg|";
  742.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  743.             break;
  744.  
  745.         case LM|A1|B1:
  746.         case LM|A1|B2:
  747.         case LM|A1|B4:
  748.         case LM|A1|B8:
  749.         case LM|A2|B1:
  750.         case LM|A2|B2:
  751.         case LM|A2|B4:
  752.         case LM|A2|B8:
  753.         case LM|A3|B1:
  754.         case LM|A3|B2:
  755.         case LM|A3|B4:
  756.         case LM|A3|B8:
  757.         case LM|A4|B1:
  758.         case LM|A4|B2:
  759.         case LM|A4|B4:
  760.         case LM|A4|B8:
  761.             pbuf[bufcnt++] = "lm|";
  762.             pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  763.             pc += Acnt[(*pc>>2) & 0x03];
  764.             break;
  765.  
  766.         case COMP|B1:
  767.         case COMP|B2:
  768.         case COMP|B4:
  769.         case COMP|B8:
  770.             pbuf[bufcnt++] = "comp|";
  771.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  772.             break;
  773.  
  774.         case JMP|J1:
  775.         case JMP|J2:
  776.         case JMP|J3:
  777.         case JMP|J4:
  778.             pbuf[bufcnt++] = "jmp|";
  779.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  780.             pc += Jcnt[*pc & 0x03];
  781.             break;
  782.  
  783.         case JMPT|J1:
  784.         case JMPT|J2:
  785.         case JMPT|J3:
  786.         case JMPT|J4:
  787.             pbuf[bufcnt++] = "jmpt|";
  788.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  789.             pc += Jcnt[*pc & 0x03];
  790.             break;
  791.  
  792.         case LJMPT|J1:
  793.         case LJMPT|J2:
  794.         case LJMPT|J3:
  795.         case LJMPT|J4:
  796.             pbuf[bufcnt++] = "ljmpt|";
  797.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  798.             pc += Jcnt[*pc & 0x03];
  799.             break;
  800.  
  801.         case JMPF|J1:
  802.         case JMPF|J2:
  803.         case JMPF|J3:
  804.         case JMPF|J4:
  805.             pbuf[bufcnt++] = "jmpf|";
  806.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  807.             pc += Jcnt[*pc & 0x03];
  808.             break;
  809.  
  810.         case LJMPF|J1:
  811.         case LJMPF|J2:
  812.         case LJMPF|J3:
  813.         case LJMPF|J4:
  814.             pbuf[bufcnt++] = "ljmpf|";
  815.             pbuf[bufcnt++] = Jbuf[*pc & 0x03];
  816.             pc += Jcnt[*pc & 0x03];
  817.             break;
  818.  
  819.         case NOT|B1:
  820.         case NOT|B2:
  821.         case NOT|B4:    /* also FLOAT */
  822.         case NOT|B8:    /* also DOUBLE */
  823.             pbuf[bufcnt++] = "not|";
  824.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  825.             break;
  826.  
  827.         case SS|A1|B1:
  828.         case SS|A1|B2:
  829.         case SS|A1|B4:
  830.         case SS|A1|B8:
  831.         case SS|A2|B1:
  832.         case SS|A2|B2:
  833.         case SS|A2|B4:
  834.         case SS|A2|B8:
  835.         case SS|A3|B1:
  836.         case SS|A3|B2:
  837.         case SS|A3|B4:
  838.         case SS|A3|B8:
  839.             pbuf[bufcnt++] = "ss|";
  840.             pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  841.             pc += Acnt[(*pc>>2) & 0x03];
  842.             break;
  843.  
  844.         case TRUTHOF|B2:
  845.         case TRUTHOF|B4:
  846.         case TRUTHOF|B8:
  847.             pbuf[bufcnt++] = "truthof|";
  848.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  849.             break;
  850.         case CVT:
  851.         {
  852.         unsigned char cvs,cvd;
  853.             pbuf[bufcnt++] = "cvt ";
  854.             ++pc;
  855.             cvs = (*pc & 0x70)>>4;
  856.             cvd = (*pc & 0x07);
  857.             pbuf[bufcnt++] = Tbuf[cvd];
  858.             pbuf[bufcnt++] = "<-";
  859.             pbuf[bufcnt++] = Tbuf[cvs];
  860.             break;
  861.         }
  862.         case LI|B1:
  863.         case LI|B2:
  864.         case LI|B4:
  865.         case LI|B8:
  866.             pbuf[bufcnt++] = "li|";
  867.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  868.             pc += Bcnt[*pc & 0x03];
  869.             break;
  870.  
  871.         case LAI|D1:
  872.         case LAI|D2:
  873.         case LAI|D3:
  874.         case LAI|D4:
  875.             pbuf[bufcnt++] = "lai|";
  876.             pbuf[bufcnt++] = Dbuf[*pc & 0x03];
  877.             pc += Dcnt[*pc & 0x03];
  878.             break;
  879.  
  880.         case LUI|B1:
  881.         case LUI|B2:
  882.         case LUI|B4:
  883.         case LUI|B8:
  884.             pbuf[bufcnt++] = "lui|";
  885.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  886.             pc += Bcnt[*pc & 0x03];
  887.             break;
  888.  
  889.         case IMMED:
  890.         {
  891.             ++pc;
  892.             switch(*pc)
  893.             {
  894.                 case SMI|A1|B1:
  895.                 case SMI|A1|B2:
  896.                 case SMI|A1|B4:
  897.                 case SMI|A1|B8:
  898.                 case SMI|A2|B1:
  899.                 case SMI|A2|B2:
  900.                 case SMI|A2|B4:
  901.                 case SMI|A2|B8:
  902.                 case SMI|A3|B1:
  903.                 case SMI|A3|B2:
  904.                 case SMI|A3|B4:
  905.                 case SMI|A3|B8:
  906.                 case SMI|A4|B1:
  907.                 case SMI|A4|B2:
  908.                 case SMI|A4|B4:
  909.                 case SMI|A4|B8:
  910.                     pbuf[bufcnt++] = "smi|";
  911.                     pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  912.                     pc += ABcnt[*pc & 0x0f];
  913.                     break;
  914.  
  915.                 case SSI|A1|B1:
  916.                 case SSI|A1|B2:
  917.                 case SSI|A1|B4:
  918.                 case SSI|A1|B8:
  919.                 case SSI|A2|B1:
  920.                 case SSI|A2|B2:
  921.                 case SSI|A2|B4:
  922.                 case SSI|A2|B8:
  923.                 case SSI|A3|B1:
  924.                 case SSI|A3|B2:
  925.                 case SSI|A3|B4:
  926.                 case SSI|A3|B8:
  927.                     pbuf[bufcnt++] = "ssi|";
  928.                     pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  929.                     pc += ABcnt[*pc & 0x0f];
  930.                     break;
  931.  
  932.                 case MODI|BYTE:
  933.                 case MODI|SHORT:
  934.                 case MODI|LONG:
  935.                 case MODI|UBYTE:
  936.                 case MODI|USHORT:
  937.                 case MODI|ULONG:
  938.                     pbuf[bufcnt++] = "modi|";
  939.                     pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  940.                     pc += 2;
  941.                     break;
  942.  
  943.                 case DEREF|BYTE:
  944.                 case DEREF|SHORT:
  945.                 case DEREF|LONG:
  946.                 case DEREF|UBYTE:
  947.                 case DEREF|USHORT:
  948.                 case DEREF|ULONG:
  949.                 case DEREF|FLOAT:
  950.                 case DEREF|DOUBLE:
  951.                     pbuf[bufcnt++] = "deref|";
  952.                     pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  953.                     break;
  954.  
  955.                 case DEREF1|BYTE:
  956.                 case DEREF1|SHORT:
  957.                 case DEREF1|LONG:
  958.                 case DEREF1|UBYTE:
  959.                 case DEREF1|USHORT:
  960.                 case DEREF1|ULONG:
  961.                 case DEREF1|FLOAT:
  962.                 case DEREF1|DOUBLE:
  963.                     pbuf[bufcnt++] = "deref1|";
  964.                     pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  965.                     break;
  966.             }
  967.             break;
  968.         }
  969.         case SM|A1|B1:
  970.         case SM|A1|B2:
  971.         case SM|A1|B4:
  972.         case SM|A1|B8:
  973.         case SM|A2|B1:
  974.         case SM|A2|B2:
  975.         case SM|A2|B4:
  976.         case SM|A2|B8:
  977.         case SM|A3|B1:
  978.         case SM|A3|B2:
  979.         case SM|A3|B4:
  980.         case SM|A3|B8:
  981.         case SM|A4|B1:
  982.         case SM|A4|B2:
  983.         case SM|A4|B4:
  984.         case SM|A4|B8:
  985.             pbuf[bufcnt++] = "sm|";
  986.             pbuf[bufcnt++] = ABbuf[*pc & 0x0f];
  987.             pc += Acnt[(*pc>>2) & 3];
  988.             break;
  989.  
  990.         case ADD|BYTE:
  991.         case ADD|SHORT:
  992.         case ADD|LONG:
  993.         case ADD|UBYTE:
  994.         case ADD|USHORT:
  995.         case ADD|ULONG:
  996.         case ADD|FLOAT:
  997.         case ADD|DOUBLE:
  998.             pbuf[bufcnt++] = "add|";
  999.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1000.             break;
  1001.  
  1002.         case SUB|BYTE:
  1003.         case SUB|SHORT:
  1004.         case SUB|LONG:
  1005.         case SUB|UBYTE:
  1006.         case SUB|USHORT:
  1007.         case SUB|ULONG:
  1008.         case SUB|FLOAT:
  1009.         case SUB|DOUBLE:
  1010.             pbuf[bufcnt++] = "sub|";
  1011.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1012.             break;
  1013.  
  1014.         case MUL|BYTE:
  1015.         case MUL|SHORT:
  1016.         case MUL|LONG:
  1017.         case MUL|UBYTE:
  1018.         case MUL|USHORT:
  1019.         case MUL|ULONG:
  1020.         case MUL|FLOAT:
  1021.         case MUL|DOUBLE:
  1022.             pbuf[bufcnt++] = "mul|";
  1023.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1024.             break;
  1025.  
  1026.         case DIV|BYTE:
  1027.         case DIV|SHORT:
  1028.         case DIV|LONG:
  1029.         case DIV|UBYTE:
  1030.         case DIV|USHORT:
  1031.         case DIV|ULONG:
  1032.         case DIV|FLOAT:
  1033.         case DIV|DOUBLE:
  1034.             pbuf[bufcnt++] = "div|";
  1035.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1036.             break;
  1037.  
  1038.         case OR|B1:
  1039.         case OR|B2:
  1040.         case OR|B4:
  1041.         case OR|B8:
  1042.             pbuf[bufcnt++] = "or|";
  1043.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1044.             break;
  1045.  
  1046.         case AND|B1:
  1047.         case AND|B2:
  1048.         case AND|B4:
  1049.         case AND|B8:
  1050.             pbuf[bufcnt++] = "and|";
  1051.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1052.             break;
  1053.  
  1054.         case MOD|BYTE:
  1055.         case MOD|SHORT:
  1056.         case MOD|LONG:
  1057.         case MOD|UBYTE:
  1058.         case MOD|USHORT:
  1059.         case MOD|ULONG:
  1060.             pbuf[bufcnt++] = "mod|";
  1061.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1062.             break;
  1063.  
  1064.         case XTD:
  1065.         {
  1066.             ++pc;
  1067.             switch(*pc)
  1068.             {
  1069.                 case LI:
  1070.                     pbuf[bufcnt++] = "li";
  1071.                     pc += XSZ;
  1072.                     break;
  1073.  
  1074.                 case LSH|B1:
  1075.                 case LSH|B2:
  1076.                 case LSH|B4:
  1077.                 case LSH|B8:
  1078.                     pbuf[bufcnt++] = "lsh|";
  1079.                     pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1080.                     break;
  1081.  
  1082.                 case LSHI|B1:
  1083.                 case LSHI|B2:
  1084.                 case LSHI|B4:
  1085.                 case LSHI|B8:
  1086.                     pbuf[bufcnt++] = "lshi|";
  1087.                     pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1088.                     ++pc;
  1089.                     break;
  1090.  
  1091.                 case RSH|BYTE:
  1092.                 case RSH|SHORT:
  1093.                 case RSH|LONG:
  1094.                 case RSH|UBYTE:
  1095.                 case RSH|USHORT:
  1096.                 case RSH|ULONG:
  1097.                     pbuf[bufcnt++] = "rsh|";
  1098.                     pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1099.                     break;
  1100.  
  1101.                 case RSHI|BYTE:
  1102.                 case RSHI|SHORT:
  1103.                 case RSHI|LONG:
  1104.                 case RSHI|UBYTE:
  1105.                 case RSHI|USHORT:
  1106.                 case RSHI|ULONG:
  1107.                     pbuf[bufcnt++] = "rshi|";
  1108.                     pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1109.                     ++pc;
  1110.                     break;
  1111.  
  1112.                 case BUILTIN:
  1113.                 {
  1114.                     pbuf[bufcnt++] = "builtin ";
  1115.                     ++pc;
  1116.                     pbuf[bufcnt++] = BUbuf[*pc];
  1117.                     break;
  1118.                 } /* END: XTD BUILTIN */
  1119.                 case CLRDAT:
  1120.                     pbuf[bufcnt++] = "clrdat";
  1121.                     break;
  1122.                 case SWITCH:
  1123.                     pbuf[bufcnt++] = "switch";
  1124.                     pc += 2;
  1125.                     break;
  1126.                 case CALLSETUP:
  1127.                     pbuf[bufcnt++] = "callsetup";
  1128.                     pc += 4;
  1129.                     break;
  1130.                 case RETSTRUCT:
  1131.                     pbuf[bufcnt++] = "retstruct";
  1132.                     pc += 4;
  1133.                     break;
  1134.                 case PRUNESTRUCT:
  1135.                     pbuf[bufcnt++] = "prunestruct";
  1136.                     break;
  1137.                 case GETBITFIELD:
  1138.                     pbuf[bufcnt++] = "getbitfield";
  1139.                     pc += 3;
  1140.                     break;
  1141.                 case PUTBITFIELD:
  1142.                     pbuf[bufcnt++] = "putbitfield";
  1143.                     pc += 3;
  1144.                     break;
  1145.                 case IMMED:
  1146.                 {
  1147.                     ++pc;
  1148.                     switch(*pc)
  1149.                     {
  1150.                         case SMI|A1:
  1151.                         case SMI|A2:
  1152.                         case SMI|A3:
  1153.                         case SMI|A4:
  1154.                             pbuf[bufcnt++] = "smi|";
  1155.                             pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1156.                             pc += Acnt[(*pc>>2) & 0x03] + XSZ;
  1157.                             break;
  1158.  
  1159.                         case SSI|A1:
  1160.                         case SSI|A2:
  1161.                         case SSI|A3:
  1162.                             pbuf[bufcnt++] = "ssi|";
  1163.                             pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1164.                             pc += Acnt[(*pc>>2) & 0x03] + XSZ;
  1165.                             break;
  1166.  
  1167.                         case MODI|LONGLONG:
  1168.                         case MODI|ULONGLONG:
  1169.                             pbuf[bufcnt++] = "modi|";
  1170.                             pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1171.                             pc += 2;
  1172.                             break;
  1173.  
  1174.                         case DEREF|LONGLONG:
  1175.                         case DEREF|ULONGLONG:
  1176.                         case DEREF|LONGDOUBLE:
  1177.                             pbuf[bufcnt++] = "deref|";
  1178.                             pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1179.                             break;
  1180.  
  1181.                         case DEREF1|LONGLONG:
  1182.                         case DEREF1|ULONGLONG:
  1183.                         case DEREF1|LONGDOUBLE:
  1184.                             pbuf[bufcnt++] = "deref1|";
  1185.                             pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1186.                             break;
  1187.                     }
  1188.                     break;
  1189.                 }/* END: XTD IMMED */
  1190.                 
  1191.                 case ADD|LONGLONG:
  1192.                 case ADD|ULONGLONG:
  1193.                 case ADD|LONGDOUBLE:
  1194.                     pbuf[bufcnt++] = "add|";
  1195.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1196.                     break;
  1197.                 case SUB|LONGLONG:
  1198.                 case SUB|ULONGLONG:
  1199.                 case SUB|LONGDOUBLE:
  1200.                     pbuf[bufcnt++] = "sub|";
  1201.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1202.                 case MUL|LONGLONG:
  1203.                 case MUL|ULONGLONG:
  1204.                 case MUL|LONGDOUBLE:
  1205.                     pbuf[bufcnt++] = "mul|";
  1206.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1207.                     break;
  1208.                 case DIV|LONGLONG:
  1209.                 case DIV|ULONGLONG:
  1210.                 case DIV|LONGDOUBLE:
  1211.                     pbuf[bufcnt++] = "div|";
  1212.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1213.                     break;
  1214.                 case LT|LONGLONG:
  1215.                 case LT|ULONGLONG:
  1216.                 case LT|LONGDOUBLE:
  1217.                     pbuf[bufcnt++] = "lt|";
  1218.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1219.                     break;
  1220.                 case GT|LONGLONG:
  1221.                  case GT|ULONGLONG:
  1222.                 case GT|LONGDOUBLE:
  1223.                     pbuf[bufcnt++] = "gt|";
  1224.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1225.                     break;
  1226.                 case LE|LONGLONG:
  1227.                 case LE|ULONGLONG:
  1228.                 case LE|LONGDOUBLE:
  1229.                     pbuf[bufcnt++] = "le|";
  1230.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1231.                     break;
  1232.                 case GE|LONGLONG:
  1233.                 case GE|ULONGLONG:
  1234.                 case GE|LONGDOUBLE:
  1235.                     pbuf[bufcnt++] = "ge|";
  1236.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1237.                     break;
  1238.                 case NE|LONGLONG:
  1239.                 case NE|ULONGLONG:
  1240.                 case NE|LONGDOUBLE:
  1241.                     pbuf[bufcnt++] = "ne|";
  1242.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1243.                     break;
  1244.                 case EQ|LONGLONG:
  1245.                 case EQ|ULONGLONG:
  1246.                 case EQ|LONGDOUBLE:
  1247.                     pbuf[bufcnt++] = "eq|";
  1248.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1249.                     break;
  1250.                 case NEG|LONGLONG:
  1251.                 case NEG|ULONGLONG:
  1252.                 case NEG|LONGDOUBLE:
  1253.                     pbuf[bufcnt++] = "neg|";
  1254.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1255.                     break;
  1256.                 case NOT|BX:
  1257.                     pbuf[bufcnt++] = "not|BX";
  1258.                     break;
  1259.                 case COMP|LONGLONG:
  1260.                 case COMP|ULONGLONG:
  1261.                     pbuf[bufcnt++] = "comp|";
  1262.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1263.                     break;
  1264.                 case RSH|SLONGLONG:
  1265.                 case RSH|SULONGLONG:
  1266.                     pbuf[bufcnt++] = "rsh|";
  1267.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1268.                     break;
  1269.  
  1270.                 case MOD|LONGLONG:
  1271.                 case MOD|ULONGLONG:
  1272.                     pbuf[bufcnt++] = "mod|";
  1273.                     pbuf[bufcnt++] = XTbuf[*pc & 0x03];
  1274.                     break;
  1275.                 case RSHI|SLONGLONG:
  1276.                 case RSHI|SULONGLONG:
  1277.                     pbuf[bufcnt++] = "rshi|";
  1278.                     pbuf[bufcnt++] = XTbuf[*pc & 0x07];
  1279.                     ++pc;
  1280.                     break;
  1281.                 case TRUTHOF|BX:
  1282.                     pbuf[bufcnt++] = "truthof|BX";
  1283.                     break;
  1284.  
  1285.                 case LS|A1:
  1286.                 case LS|A2:
  1287.                 case LS|A3:
  1288.                     pbuf[bufcnt++] = "xtd ls|";
  1289.                     pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1290.                     pc += Acnt[(*pc>>2) & 0x03];
  1291.                     break;
  1292.  
  1293.                 case LM|A1:
  1294.                 case LM|A2:
  1295.                 case LM|A3:
  1296.                 case LM|A4:
  1297.                     pbuf[bufcnt++] = "xtd lm|";
  1298.                     pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1299.                     pc += Acnt[(*pc>>2) & 0x03];
  1300.                     break;
  1301.  
  1302.                 case SS|A1:
  1303.                 case SS|A2:
  1304.                 case SS|A3:
  1305.                     pbuf[bufcnt++] = "xtd ss|";
  1306.                     pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1307.                     pc += Acnt[(*pc>>2) & 0x03];
  1308.                     break;
  1309.  
  1310.                 case SM|A1:
  1311.                 case SM|A2:
  1312.                 case SM|A3:
  1313.                 case SM|A4:
  1314.                     pbuf[bufcnt++] = "xtd sm|";
  1315.                     pbuf[bufcnt++] = Abuf[(*pc>>2) & 0x03];
  1316.                     pc += Acnt[(*pc>>2) & 0x03];
  1317.                     break;
  1318.  
  1319.                 case MOVSS:
  1320.                     pbuf[bufcnt++] = "xtd movss";
  1321.                     ++pc;
  1322.                     pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1323.                     pc += SDcnt[*pc & 0x0f];
  1324.                     break;
  1325.                 case MOVSM:
  1326.                     pbuf[bufcnt++] = "xtd movsm";
  1327.                     ++pc;
  1328.                     pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1329.                     pc += SDcnt[*pc & 0x0f];
  1330.                     break;
  1331.                 case MOVMS:
  1332.                     pbuf[bufcnt++] = "xtd movms";
  1333.                     ++pc;
  1334.                     pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1335.                     pc += SDcnt[*pc & 0x0f];
  1336.                     break;
  1337.                 case MOVMM:
  1338.                     pbuf[bufcnt++] = "xtd movmm";
  1339.                     ++pc;
  1340.                     pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1341.                     pc += SDcnt[*pc & 0x0f];
  1342.                     break;
  1343.             }
  1344.             break;
  1345.         }/* END: XTD */
  1346.  
  1347.         case XOR|B1:
  1348.         case XOR|B2:
  1349.         case XOR|B4:
  1350.         case XOR|B8:
  1351.             pbuf[bufcnt++] = "xor|";
  1352.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1353.             break;
  1354.  
  1355.         case GT|BYTE:
  1356.         case GT|SHORT:
  1357.         case GT|LONG:
  1358.         case GT|UBYTE:
  1359.         case GT|USHORT:
  1360.         case GT|ULONG:
  1361.         case GT|FLOAT:
  1362.         case GT|DOUBLE:
  1363.             pbuf[bufcnt++] = "gt|";
  1364.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1365.             break;
  1366.  
  1367.         case LT|BYTE:
  1368.         case LT|SHORT:
  1369.         case LT|LONG:
  1370.         case LT|UBYTE:
  1371.         case LT|USHORT:
  1372.         case LT|ULONG:
  1373.         case LT|FLOAT:
  1374.         case LT|DOUBLE:
  1375.             pbuf[bufcnt++] = "lt|";
  1376.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1377.             break;
  1378.  
  1379.         case GE|BYTE:
  1380.         case GE|SHORT:
  1381.         case GE|LONG:
  1382.         case GE|UBYTE:
  1383.         case GE|USHORT:
  1384.         case GE|ULONG:
  1385.         case GE|FLOAT:
  1386.         case GE|DOUBLE:
  1387.             pbuf[bufcnt++] = "ge|";
  1388.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1389.             break;
  1390.  
  1391.         case LE|BYTE:
  1392.         case LE|SHORT:
  1393.         case LE|LONG:
  1394.         case LE|UBYTE:
  1395.         case LE|USHORT:
  1396.         case LE|ULONG:
  1397.         case LE|FLOAT:
  1398.         case LE|DOUBLE:
  1399.             pbuf[bufcnt++] = "le|";
  1400.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1401.             break;
  1402.  
  1403.         case NE|BYTE:
  1404.         case NE|SHORT:
  1405.         case NE|LONG:
  1406.         case NE|UBYTE:
  1407.         case NE|USHORT:
  1408.         case NE|ULONG:
  1409.         case NE|FLOAT:
  1410.         case NE|DOUBLE:
  1411.             pbuf[bufcnt++] = "ne|";
  1412.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1413.             break;
  1414.  
  1415.         case EQ|BYTE:
  1416.         case EQ|SHORT:
  1417.         case EQ|LONG:
  1418.         case EQ|UBYTE:
  1419.         case EQ|USHORT:
  1420.         case EQ|ULONG:
  1421.         case EQ|FLOAT:
  1422.         case EQ|DOUBLE:
  1423.             pbuf[bufcnt++] = "eq|";
  1424.             pbuf[bufcnt++] = Tbuf[*pc & 0x07];
  1425.             break;
  1426.  
  1427.         case ARG:
  1428.             pbuf[bufcnt++] = "arg";
  1429.             break;
  1430.         case ARGA:
  1431.             pbuf[bufcnt++] = "arga";
  1432.             break;
  1433.         case ARGF:
  1434.             pbuf[bufcnt++] = "argf";
  1435.             break;
  1436.  
  1437.         case MOVSS|B1:
  1438.         case MOVSS|B2:
  1439.         case MOVSS|B4:
  1440.         case MOVSS|B8:
  1441.             pbuf[bufcnt++] = "movss|";
  1442.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1443.             ++pc;
  1444.             pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1445.             pc += SDcnt[*pc & 0x0f];
  1446.             break;
  1447.  
  1448.         case MOVSM|B1:
  1449.         case MOVSM|B2:
  1450.         case MOVSM|B4:
  1451.         case MOVSM|B8:
  1452.             pbuf[bufcnt++] = "movsm|";
  1453.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1454.             ++pc;
  1455.             pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1456.             pc += SDcnt[*pc & 0x0f];
  1457.             break;
  1458.  
  1459.         case MOVMS|B1:
  1460.         case MOVMS|B2:
  1461.         case MOVMS|B4:
  1462.         case MOVMS|B8:
  1463.             pbuf[bufcnt++] = "movms|";
  1464.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1465.             ++pc;
  1466.             pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1467.             pc += SDcnt[*pc & 0x0f];
  1468.             break;
  1469.  
  1470.         case MOVMM|B1:
  1471.         case MOVMM|B2:
  1472.         case MOVMM|B4:
  1473.         case MOVMM|B8:
  1474.             pbuf[bufcnt++] = "movmm|";
  1475.             pbuf[bufcnt++] = Bbuf[*pc & 0x03];
  1476.             ++pc;
  1477.             pbuf[bufcnt++] = SDbuf[*pc & 0x0f];
  1478.             pc += SDcnt[*pc & 0x0f];
  1479.             break;
  1480.  
  1481.         case DUMP:
  1482.             pbuf[bufcnt++] = "dump ";
  1483.             break;
  1484.         case REGAIN:
  1485.             pbuf[bufcnt++] = "regain ";
  1486.             break;
  1487.         case CALL:
  1488.             pbuf[bufcnt++] = "call ";
  1489.             break;
  1490.         case RET:
  1491.             pbuf[bufcnt++] = "ret ";
  1492.             break;
  1493.         case SWAP:
  1494.             pbuf[bufcnt++] = "swap ";
  1495.             break;
  1496.         case SWAP4:
  1497.             pbuf[bufcnt++] = "swap4 ";
  1498.             break;
  1499.         case SWAP4DEEP:
  1500.             pbuf[bufcnt++] = "swap4deep ";
  1501.             break;
  1502.         case DUP:
  1503.             pbuf[bufcnt++] = "dup ";
  1504.             break;
  1505.         case DUP4:
  1506.             pbuf[bufcnt++] = "dup4 ";
  1507.             break;
  1508.         case ABSMEM:
  1509.             pbuf[bufcnt++] = "absmem ";
  1510.             break;
  1511.         case ABSSTK:
  1512.             pbuf[bufcnt++] = "absstk ";
  1513.             break;
  1514.         case MOVDA1:
  1515.             pbuf[bufcnt++] = "movda1 ";
  1516.             break;
  1517.         case MOVDA2:
  1518.             pbuf[bufcnt++] = "movda2 ";
  1519.             break;
  1520.         case MOVDA4:
  1521.             pbuf[bufcnt++] = "movda4 ";
  1522.             break;
  1523.         case MOVDA8:
  1524.             pbuf[bufcnt++] = "movda8 ";
  1525.             break;
  1526.         case MOVDAX:
  1527.             pbuf[bufcnt++] = "movdax ";
  1528.             break;
  1529.         case MOVAA1:
  1530.             pbuf[bufcnt++] = "movaa1 ";
  1531.             break;
  1532.         case MOVAA2:
  1533.             pbuf[bufcnt++] = "movaa2 ";
  1534.             break;
  1535.         case MOVAA4:
  1536.             pbuf[bufcnt++] = "movaa4 ";
  1537.             break;
  1538.         case MOVAA8:
  1539.             pbuf[bufcnt++] = "movaa8 ";
  1540.             break;
  1541.         case MOVAAX:
  1542.             pbuf[bufcnt++] = "movaax ";
  1543.             break;
  1544.         case MOVAAC:
  1545.             pbuf[bufcnt++] = "movaac ";
  1546.             break;
  1547.         case NOP:
  1548.             pbuf[bufcnt++] = "nop ";
  1549.             break;
  1550.     } /* END: switch(*pc) */
  1551.   }/* END: for() */
  1552.   for(i = 0; i < bufcnt; ++i)
  1553.         fwrite(pbuf[i], 1, strlen(pbuf[i]), iv->outfile);
  1554.   fwrite("\n", 1, 1, iv->outfile);
  1555. }/* END: printinst() */
  1556. static void
  1557. disassemble(Piv iv, void *ptr, long size)
  1558. {
  1559. int x;
  1560. long offset = iv->out_offset;
  1561.  
  1562.     x = print8(iv, ptr, size, offset, 0);
  1563.     printinst(iv, ptr, size);
  1564.     while((size -= x) > 0)
  1565.     {
  1566.         offset += x;
  1567.         ((char*)ptr) += x;
  1568.         x = print8(iv, ptr, size, offset, 1);
  1569.     }
  1570. }
  1571. static void
  1572. reset_funcdata(Piv iv)
  1573. {
  1574.     iv->obuf = (char*)&iv->obufstart;
  1575.     iv->obufstart = 0;
  1576.     iv->obufcnt = 0;
  1577.     iv->func_offset = 0;
  1578.     iv->jbuf = (PJL)&iv->jbufstart;
  1579.     iv->jmpcnt = 0;
  1580.     iv->jbufcnt = 0;
  1581.     iv->jbufstart = 0;
  1582.     iv->extbuf = (PEL)&iv->extbufstart;
  1583.     iv->extcnt = 0;
  1584.     iv->extbufcnt = 0;
  1585.     iv->extbufstart = 0;
  1586.     iv->stackdepth = 0;
  1587.     iv->maxdepth = 0;
  1588.     iv->mindepth = 0;
  1589.     iv->numnested = 0;
  1590.     iv->cod_bufcnt = 0;
  1591.     iv->ob_bufcnt = 0;
  1592.     iv->cod_usedhead = 0;
  1593.     iv->ob_usedhead = 0;
  1594.     iv->first_cod = 0;
  1595.     iv->first_ob = 0;
  1596.     Cfreecat(FUNCDATA);
  1597. }
  1598. static void
  1599. write_funcdata(Piv iv, unsigned char *pdef)
  1600. {
  1601. long *p;
  1602.     if((p = (long*)iv->obufstart))
  1603.     {
  1604.         setup_jmps(iv, pdef);
  1605.         while(shorten_jmps(iv))
  1606.             ;
  1607.         while(p)
  1608.         {
  1609.             if(iv->listing_wanted)
  1610.             {
  1611.                 if(((unsigned char*)p)[8] == LINENO)
  1612.                 {
  1613.                     printline(iv, &((unsigned char*)p)[9]);
  1614.                 }
  1615.                 else if(((unsigned char*)p)[8] == NFUNC)
  1616.                 {
  1617.                 char *funcname = *((char**)&(((char*)p)[9]));
  1618.                     fprintf(iv->outfile, "\n%8.8lx:    .nested .function _%s\n",
  1619.                     iv->out_offset, funcname);
  1620.                 }
  1621.                 else
  1622.                 {
  1623.                     disassemble(iv, &p[2], p[1]);
  1624.                     iv->out_offset += p[1];
  1625.                 }
  1626.             }
  1627.             else
  1628.             {
  1629.                 FILEWRITE(&p[2],p[1]);
  1630.                 iv->out_offset += p[1];
  1631.             }
  1632.             p = (void*)p[0];
  1633.         }
  1634.     }
  1635. if(iv->debug >= '1')
  1636. cfeprintf("MAXDEPTH=%d MINDEPTH=%d func=%s\n",
  1637. iv->maxdepth, iv->mindepth, iv->symaddr[GL( ((Pop)pdef)->data )]);
  1638.  
  1639.     save_maxdepth(iv, GL( ((Pop)pdef)->data ));
  1640. }
  1641. static void *
  1642. write_obuf(Piv iv, unsigned char *buf, long cnt)
  1643. {/* Output first goes to a linked list */
  1644. void *next = iv->obuf;
  1645.  
  1646.     if(iv->obuf != (unsigned char*)&iv->obufstart)
  1647.     {/* Suppress duplicate RETs */
  1648.       if(buf[0] == RET && (iv->obuf[8] == RET || iv->obuf[9] == RETSTRUCT) )
  1649.       {
  1650.         return iv->obuf;
  1651.       }
  1652.     }
  1653.     if(iv->obufcnt >= cnt+8)
  1654.     {/* There is sufficient room in the current chunk */
  1655.     long size = ((long*)iv->obuf)[1];
  1656.       iv->obuf += size+8;
  1657.     }
  1658.     else
  1659.     {/* Allocate a new chunk of linked list space */
  1660.       iv->obufcnt = 4080;
  1661.       iv->obuf = Ccalloc(FUNCDATA, 1, iv->obufcnt);
  1662.     }
  1663.  
  1664.     *((void**)next) = iv->obuf;        /* link back to old entry */
  1665.     ((long*)iv->obuf)[1] = cnt;        /* record the size of the data */
  1666.     iv->obufcnt -= cnt+8;            /* deduct data size plus overhead */
  1667.     memcpy(&((long*)iv->obuf)[2], buf, cnt);  /* copy the data to this area */
  1668.     if(buf[0] != LINENO && buf[0] != NFUNC)
  1669.     {/* A line number record is an anomoly */
  1670.         if(iv->extmark)
  1671.             addto_extlist(iv, buf);        /* reference to external variable here */
  1672.         iv->func_offset += cnt;            /* increase the program counter */
  1673.     }
  1674.  
  1675.     iv->cod->ee = (struct _nodeOBUF *)iv->obuf;    /* points to obuf chunks */
  1676.     link_cod(iv);
  1677.  
  1678.     return iv->obuf;    /* return the address of this sub chunk */
  1679. }
  1680. static void
  1681. do_conversion(Piv iv, PND dst, PND src)
  1682. {
  1683. unsigned char obuf[2];
  1684. int dsize = dst->size;
  1685. int ssize = src->size;
  1686. unsigned char ddtype = dst->dtype;
  1687. unsigned char sdtype = src->dtype;
  1688. #define SRC(a) (a<<4)
  1689.  
  1690.     if(src->atype & (A_ABSOLUTE|A_POINTER|A_VALUE) == (A_POINTER|A_VALUE))
  1691.     {
  1692.         if(src->atype & A_DATA)
  1693.             obuf[0] = ABSMEM;
  1694.         else if(src->atype & (A_AUTO|A_PARAM))
  1695.             obuf[0] = ABSSTK;
  1696.         else
  1697.             PERROR(pName ": Line:%d bad conversion0\n", iv->lastline);
  1698.         write_obuf(iv, obuf, 1);
  1699.         return;
  1700.     }
  1701.     obuf[0] = CVT;
  1702.     switch(ddtype)
  1703.     {
  1704.         case    D_SIGNED:            /* dest is signed integer */
  1705.         {
  1706.             if(dsize == 1)
  1707.                 obuf[1] = BYTE;
  1708.             else if(dsize == 2)
  1709.                 obuf[1] = SHORT;
  1710.             else if(dsize == 4)
  1711.                 obuf[1] = LONG;
  1712.             else if(dsize == 8)
  1713.                 obuf[1] = CLONGLONG;
  1714.             switch(sdtype)
  1715.             {
  1716.                 case D_SIGNED:
  1717.                 {/* src is signed integer */
  1718.                     if(dsize <= ssize)
  1719.                         return;
  1720.                     if(ssize == 1)
  1721.                         obuf[1] |= SRC(BYTE);
  1722.                     else if(ssize == 2)
  1723.                         obuf[1] |= SRC(SHORT);
  1724.                     else if(ssize == 4)
  1725.                         obuf[1] |= SRC(LONG);
  1726.                     else if(ssize == 8)
  1727.                         obuf[1] |= SRC(CLONGLONG);
  1728.                     break;
  1729.                 }
  1730.                 case D_UNSIGNED:
  1731.                 case D_SEGMENT:
  1732.                 {/* src is unsigned integer */
  1733.                     if(dsize <= ssize)
  1734.                         return;
  1735.                     if(ssize == 1)
  1736.                         obuf[1] |= SRC(UBYTE);
  1737.                     else if(ssize == 2)
  1738.                         obuf[1] |= SRC(USHORT);
  1739.                     else if(ssize == 4)
  1740.                         obuf[1] |= SRC(ULONG);
  1741.                     else if(ssize == 8)
  1742.                         obuf[1] |= SRC(CULONGLONG);
  1743.                     break;
  1744.                 }
  1745.                 case D_FLOAT:
  1746.                 {/* src is floating point */
  1747.                     if(ssize == 4)
  1748.                         obuf[1] |= SRC(FLOAT);
  1749.                     else if(ssize == 8)
  1750.                         obuf[1] |= SRC(DOUBLE);
  1751.                     else
  1752.                         obuf[1] |= SRC(CLONGDOUBLE);
  1753.                     break;
  1754.                 }
  1755.                 case D_POINTER:
  1756.                 case D_FUNCPTR:
  1757.                 {/* src is unsigned int */
  1758.                     if(dsize <= ssize)
  1759.                         return;
  1760.                     if(ssize == 1)
  1761.                         obuf[1] |= SRC(UBYTE);
  1762.                     else if(ssize == 2)
  1763.                         obuf[1] |= SRC(USHORT);
  1764.                     else if(ssize == 4)
  1765.                         obuf[1] |= SRC(ULONG);
  1766.                     else if(ssize == 8)
  1767.                         obuf[1] |= SRC(CULONGLONG);
  1768.                     break;
  1769.                 }
  1770.                 default:
  1771.                     PERROR(pName ": Line:%d bad conversion1\n", iv->lastline);
  1772.             }
  1773.             break;
  1774.         }
  1775.         case    D_UNSIGNED:    /* dest is unsigned integer */
  1776.         case    D_SEGMENT:
  1777.         {
  1778.             if(dsize == 1)
  1779.                 obuf[1] = UBYTE;
  1780.             else if(dsize == 2)
  1781.                 obuf[1] = USHORT;
  1782.             else if(dsize == 4)
  1783.                 obuf[1] = ULONG;
  1784.             else if(dsize == 8)
  1785.                 obuf[1] = CULONGLONG;
  1786.             switch(sdtype)
  1787.             {
  1788.                 case D_SIGNED:
  1789.                 {/* src is signed integer */
  1790.                     if(dsize <= ssize)
  1791.                         return;
  1792.                     if(ssize == 1)
  1793.                         obuf[1] |= SRC(BYTE);
  1794.                     else if(ssize == 2)
  1795.                         obuf[1] |= SRC(SHORT);
  1796.                     else if(ssize == 4)
  1797.                         obuf[1] |= SRC(LONG);
  1798.                     else if(ssize == 8)
  1799.                         obuf[1] |= SRC(CLONGLONG);
  1800.                     break;
  1801.                 }
  1802.                 case D_UNSIGNED:
  1803.                 case D_SEGMENT:
  1804.                 {/* src is unsigned integer */
  1805.                     if(dsize <= ssize)
  1806.                         return;
  1807.                     if(ssize == 1)
  1808.                         obuf[1] |= SRC(UBYTE);
  1809.                     else if(ssize == 2)
  1810.                         obuf[1] |= SRC(USHORT);
  1811.                     else if(ssize == 4)
  1812.                         obuf[1] |= SRC(ULONG);
  1813.                     else if(ssize == 8)
  1814.                         obuf[1] |= SRC(CULONGLONG);
  1815.                     break;
  1816.                 }
  1817.                 case D_FLOAT:
  1818.                 {/* src is floating point */
  1819.                     if(ssize == 4)
  1820.                         obuf[1] |= SRC(FLOAT);
  1821.                     else if(ssize == 8)
  1822.                         obuf[1] |= SRC(DOUBLE);
  1823.                     else
  1824.                         obuf[1] |= SRC(CLONGDOUBLE);
  1825.                     break;
  1826.                 }
  1827.                 case D_POINTER:
  1828.                 case D_FUNCPTR:
  1829.                 {/* src is unsigned integer */
  1830.                     if(dsize <= ssize)
  1831.                         return;                    
  1832.                     if(ssize == 1)
  1833.                         obuf[1] |= SRC(UBYTE);
  1834.                     else if(ssize == 2)
  1835.                         obuf[1] |= SRC(USHORT);
  1836.                     else if(ssize == 4)
  1837.                         obuf[1] |= SRC(ULONG);
  1838.                     else if(ssize == 8)
  1839.                         obuf[1] |= SRC(CULONGLONG);
  1840.                     break;
  1841.                 }
  1842.                 default:
  1843.                     PERROR(pName ": Line:%d bad conversion2\n", iv->lastline);
  1844.             }
  1845.             break;
  1846.         }
  1847.         case    D_FLOAT:    /* dest is float, double, long double */
  1848.         {
  1849.             if(dsize == 4)
  1850.                 obuf[1] = FLOAT;
  1851.             else if(dsize == 8)
  1852.                 obuf[1] = DOUBLE;
  1853.             else
  1854.                 obuf[1] = CLONGDOUBLE;
  1855.             switch(sdtype)
  1856.             {
  1857.                 case D_SIGNED:
  1858.                 {/* src is signed integer */
  1859.                     if(ssize == 1)
  1860.                         obuf[1] |= SRC(BYTE);
  1861.                     else if(ssize == 2)
  1862.                         obuf[1] |= SRC(SHORT);
  1863.                     else if(ssize == 4)
  1864.                         obuf[1] |= SRC(LONG);
  1865.                     else if(ssize == 8)
  1866.                         obuf[1] |= SRC(CLONGLONG);
  1867.                     break;
  1868.                 }
  1869.                 case D_UNSIGNED:
  1870.                 case D_SEGMENT:
  1871.                 {/* src is unsigned integer */
  1872.                     if(ssize == 1)
  1873.                         obuf[1] |= SRC(UBYTE);
  1874.                     else if(ssize == 2)
  1875.                         obuf[1] |= SRC(USHORT);
  1876.                     else if(ssize == 4)
  1877.                         obuf[1] |= SRC(ULONG);
  1878.                     else if(ssize == 8)
  1879.                         obuf[1] |= SRC(CULONGLONG);
  1880.                     break;
  1881.                 }
  1882.                 case D_FLOAT:
  1883.                 {/* src is floating point */
  1884.                     if(dsize == ssize)
  1885.                         return;
  1886.                     if(ssize == 4)
  1887.                         obuf[1] |= SRC(FLOAT);
  1888.                     else if(ssize == 8)
  1889.                         obuf[1] |= SRC(DOUBLE);
  1890.                     else
  1891.                         obuf[1] |= SRC(CLONGDOUBLE);
  1892.                     break;
  1893.                 }
  1894.                 case D_POINTER:
  1895.                 case D_FUNCPTR:
  1896.                 {/* src is unsigned integer */
  1897.                     if(ssize == 1)
  1898.                         obuf[1] |= SRC(UBYTE);
  1899.                     else if(ssize == 2)
  1900.                         obuf[1] |= SRC(USHORT);
  1901.                     else if(ssize == 4)
  1902.                         obuf[1] |= SRC(ULONG);
  1903.                     else if(ssize == 8)
  1904.                         obuf[1] |= SRC(CULONGLONG);
  1905.                     break;
  1906.                 }
  1907.                 default:
  1908.                     PERROR(pName ": Line:%d bad conversion3\n", iv->lastline);
  1909.             }
  1910.             break;
  1911.         }
  1912.         case    D_POINTER:
  1913.         case    D_FUNCPTR:
  1914.         {/* dest is unsigned integer */
  1915.             if(dsize == 1)
  1916.                 obuf[1] = BYTE;
  1917.             else if(dsize == 2)
  1918.                 obuf[1] = SHORT;
  1919.             else if(dsize == 4)
  1920.                 obuf[1] = LONG;
  1921.             else if(dsize == 8)
  1922.                 obuf[1] = CLONGLONG;
  1923.             switch(sdtype)
  1924.             {
  1925.                 case D_SIGNED:
  1926.                 {/* src is signed integer */
  1927.                     if(dsize <= ssize)
  1928.                         return;
  1929.                     if(ssize == 1)
  1930.                         obuf[1] |= SRC(BYTE);
  1931.                     else if(ssize == 2)
  1932.                         obuf[1] |= SRC(SHORT);
  1933.                     else if(ssize == 4)
  1934.                         obuf[1] |= SRC(LONG);
  1935.                     else if(ssize == 8)
  1936.                         obuf[1] |= SRC(CLONGLONG);
  1937.                     break;
  1938.                 }
  1939.                 case D_UNSIGNED:
  1940.                 case D_SEGMENT:
  1941.                 {/* src is unsigned integer */
  1942.                     if(dsize <= ssize)
  1943.                         return;
  1944.                     if(ssize == 1)
  1945.                         obuf[1] |= SRC(UBYTE);
  1946.                     else if(ssize == 2)
  1947.                         obuf[1] |= SRC(USHORT);
  1948.                     else if(ssize == 4)
  1949.                         obuf[1] |= SRC(ULONG);
  1950.                     else if(ssize == 8)
  1951.                         obuf[1] |= SRC(CULONGLONG);
  1952.                     break;
  1953.                 }
  1954.                 case D_FLOAT:
  1955.                 {/* src is floating point */
  1956.                     if(ssize == 4)
  1957.                         obuf[1] |= SRC(FLOAT);
  1958.                     else if(ssize == 8)
  1959.                         obuf[1] |= SRC(DOUBLE);
  1960.                     else
  1961.                         obuf[1] |= SRC(CLONGDOUBLE);
  1962.                     break;
  1963.                 }
  1964.                 case D_POINTER:
  1965.                 case D_FUNCPTR:
  1966.                 {/* src is unsigned integer */
  1967.                     if(dsize <= ssize)
  1968.                         return;
  1969.                     if(ssize == 1)
  1970.                         obuf[1] |= SRC(UBYTE);
  1971.                     else if(ssize == 2)
  1972.                         obuf[1] |= SRC(USHORT);
  1973.                     else if(ssize == 4)
  1974.                         obuf[1] |= SRC(ULONG);
  1975.                     else if(ssize == 8)
  1976.                         obuf[1] |= SRC(CULONGLONG);
  1977.                     break;
  1978.                 }
  1979.                 default:
  1980.                     PERROR(pName ": Line:%d bad conversion4\n", iv->lastline);
  1981.             }
  1982.             break;
  1983.         }
  1984.         default:
  1985.             PRINTF("Line:%d no conversion\n", iv->lastline);
  1986.             return;
  1987.     }
  1988.     write_obuf(iv, obuf, 2);
  1989. #undef SRC
  1990. }
  1991. static int
  1992. check_assignment_conversion(PND dst, PND src)
  1993. {
  1994. long dsize = dst->size;
  1995. long ssize = src->size;
  1996. unsigned char ddtype = dst->dtype;
  1997. unsigned char sdtype = src->dtype;
  1998. int ret = 0;
  1999.  
  2000.     if(src->atype & (A_ABSOLUTE|A_POINTER|A_VALUE) == (A_POINTER|A_VALUE))
  2001.         return 1;
  2002.     switch(ddtype)
  2003.     {
  2004.         case    D_SIGNED:            /* dest is signed integer */
  2005.         {
  2006.             switch(sdtype)
  2007.             {
  2008.                 case D_SIGNED:
  2009.                 case D_UNSIGNED:
  2010.                 case D_SEGMENT:
  2011.                 case D_POINTER:
  2012.                 case D_FUNCPTR:
  2013.                 {
  2014.                     if(dsize > ssize)
  2015.                         ret = 1;
  2016.                     break;
  2017.                 }
  2018.                 case D_FLOAT:
  2019.                 {
  2020.                     ret = 1;
  2021.                     break;
  2022.                 }
  2023.             }
  2024.             break;
  2025.         }
  2026.         case    D_UNSIGNED:    /* dest is unsigned integer */
  2027.         case    D_SEGMENT:
  2028.         {
  2029.             switch(sdtype)
  2030.             {
  2031.                 case D_SIGNED:
  2032.                 case D_UNSIGNED:
  2033.                 case D_SEGMENT:
  2034.                 case D_POINTER:
  2035.                 case D_FUNCPTR:
  2036.                 {
  2037.                     if(dsize > ssize)
  2038.                         ret = 1;
  2039.                     break;
  2040.                 }
  2041.                 case D_FLOAT:
  2042.                 {
  2043.                     ret = 1;
  2044.                     break;
  2045.                 }
  2046.             }
  2047.             break;
  2048.         }
  2049.         case    D_FLOAT:    /* dest is float, double, long double */
  2050.         {
  2051.             if(sdtype == D_FLOAT)
  2052.             {
  2053.                 if(dsize != ssize)
  2054.                     ret = 1;
  2055.             }
  2056.             else ret = 1;
  2057.             break;
  2058.         }
  2059.         case    D_POINTER:
  2060.         case    D_FUNCPTR:        /* dest is pointer */
  2061.         {
  2062.             switch(sdtype)
  2063.             {
  2064.                 case D_SIGNED:
  2065.                 case D_UNSIGNED:
  2066.                 case D_SEGMENT:
  2067.                 case D_POINTER:
  2068.                 case D_FUNCPTR:
  2069.                 {
  2070.                     if(dsize > ssize)
  2071.                         ret = 1;
  2072.                     break;
  2073.                 }
  2074.                 case D_FLOAT:
  2075.                 {
  2076.                     ret = 1;
  2077.                     break;
  2078.                 }
  2079.             }
  2080.             break;
  2081.         }
  2082.     }
  2083.     return ret;
  2084. }
  2085. static int
  2086. check_binop_conversion(PND dst, PND src)
  2087. {
  2088. long dsize = dst->size;
  2089. long ssize = src->size;
  2090. unsigned char ddtype = dst->dtype;
  2091. unsigned char sdtype = src->dtype;
  2092. int ret = 0;
  2093.  
  2094.     switch(ddtype)
  2095.     {
  2096.         case    D_SIGNED:            /* dest is signed integer */
  2097.         {
  2098.             switch(sdtype)
  2099.             {
  2100.                 case D_SIGNED:
  2101.                 case D_UNSIGNED:
  2102.                 case D_SEGMENT:
  2103.                 case D_POINTER:
  2104.                 case D_FUNCPTR:
  2105.                 {
  2106.                     if(dsize > ssize)
  2107.                         ret = 1;
  2108.                     break;
  2109.                 }
  2110.             }
  2111.             break;
  2112.         }
  2113.         case    D_UNSIGNED:    /* dest is unsigned integer */
  2114.         case    D_SEGMENT:
  2115.         {
  2116.             switch(sdtype)
  2117.             {
  2118.                 case D_SIGNED:
  2119.                 case D_UNSIGNED:
  2120.                 case D_SEGMENT:
  2121.                 case D_POINTER:
  2122.                 case D_FUNCPTR:
  2123.                 {
  2124.                     if(dsize > ssize)
  2125.                         ret = 1;
  2126.                     break;
  2127.                 }
  2128.             }
  2129.             break;
  2130.         }
  2131.         case    D_FLOAT:    /* dest is float, double, long double */
  2132.         {
  2133.             if(sdtype == D_FLOAT)
  2134.             {
  2135.                 if(dsize > ssize)
  2136.                     ret = 1;
  2137.             }
  2138.             else ret = 1;
  2139.             break;
  2140.         }
  2141.         case    D_POINTER:
  2142.         case    D_FUNCPTR:        /* dest is pointer */
  2143.         {
  2144.             switch(sdtype)
  2145.             {
  2146.                 case D_SIGNED:
  2147.                 case D_UNSIGNED:
  2148.                 case D_SEGMENT:
  2149.                 case D_POINTER:
  2150.                 case D_FUNCPTR:
  2151.                 {
  2152.                     if(dsize > ssize)
  2153.                         ret = 1;
  2154.                     break;
  2155.                 }
  2156.             }
  2157.             break;
  2158.         }
  2159.     }
  2160.     return ret;
  2161. }
  2162. static int
  2163. get_size(int type, int size)
  2164. {
  2165.     switch(type)
  2166.     {
  2167.         case    D_ARRAY:
  2168.         case    D_STRUCT:
  2169.         case    D_FUNCTION:
  2170.             return B4;
  2171.  
  2172.         default:
  2173.             switch(size)
  2174.             {
  2175.                 case 1:
  2176.                     return B1;
  2177.                 case 2:
  2178.                     return B2;
  2179.                 case 4:
  2180.                     return B4;
  2181.                 case 8:
  2182.                     return B8;
  2183.                 default:
  2184. #if SUPPORT_LONG_DOUBLE
  2185.                     return BX;
  2186. #endif
  2187.             }
  2188.     }
  2189.     return 0;
  2190. }
  2191. static int
  2192. load_addr(Piv iv, unsigned char *poc, void *buf, PND pnd, int shft)
  2193. {/* Use the shortest possible representation */
  2194. long offset = pnd->OFFSET;
  2195. long ofs = offset >> 2;
  2196.  
  2197.     if(pnd->atype & (A_EXTERN|A_ABSOLUTE))
  2198.     {/* 4 bytes for absolute values */
  2199.         *poc |= 3<<shft;
  2200.         *((long*)buf) = offset;
  2201.         iv->extmark += 1;
  2202.         iv->markedsym[iv->extmark] = pnd->SYMNUM;
  2203.         iv->markedbuf[iv->extmark] = buf;
  2204.         return 4;
  2205.     }
  2206.     if(offset & 0xff000000)
  2207.     {/* Relative values are restricted to 28 bits max */
  2208.         PERROR(pName ": Line:%d offset too large %x\n", iv->lastline, ofs);
  2209.         return 0;
  2210.     }
  2211.     if(offset & 0xffff0000 || offset & 0x00000003)
  2212.     {/* 3 bytes for large values and non-aligned values */
  2213.         *poc |= 2<<shft;
  2214.         *((long*)buf) = offset;
  2215.         return 3;
  2216.     }
  2217.     else if(ofs & 0xffffff00)
  2218.     {/* 2 or fewer bytes for small aligned values */
  2219.         *poc |= 1<<shft;
  2220.         *((short*)buf) = (short)ofs;
  2221.         return 2;
  2222.     }
  2223.     else
  2224.     {
  2225.         *((char*)buf) = (char)ofs;
  2226.         return 1;
  2227.     }
  2228.  
  2229. }
  2230. static void
  2231. load_val(Piv iv, unsigned long val)
  2232. {
  2233. unsigned char obuf[10];
  2234.  
  2235.     obuf[0] = LUI;
  2236.     ++iv->stackdepth;
  2237.     if(val & 0xffff0000)
  2238.     {
  2239.         obuf[0] |= 2;
  2240.         *((unsigned long*)&obuf[1]) = val;
  2241.         write_obuf(iv, obuf, 5);
  2242.     }
  2243.     else if(val & 0xffffff00)
  2244.     {
  2245.         obuf[0] |= 1;
  2246.         *((unsigned short*)&obuf[1]) = (unsigned short)val;
  2247.         write_obuf(iv, obuf, 3);
  2248.     }
  2249.     else
  2250.     {
  2251.         obuf[1] = (unsigned char)val;
  2252.         write_obuf(iv, obuf, 2);
  2253.     }
  2254. }
  2255. static int
  2256. load_immed(Piv iv, unsigned char *poc, void *obuf, PND pnd, int osize)
  2257. {
  2258. unsigned short dtype = pnd->dtype;
  2259. unsigned char mosize = pnd->opsize;
  2260. void *ibuf = pnd->data;
  2261.  
  2262.     if(osize >= 0)
  2263.     {/* force the immediate value to conform to osize */
  2264.         if(osize == mosize)
  2265.         {
  2266.             switch(osize)
  2267.             {
  2268.                 case    B1:
  2269.                     *((char*)obuf) = *((char*)ibuf);
  2270.                     return 1;            
  2271.                 case    B2:
  2272.                     *((short*)obuf) = *((short*)ibuf);
  2273.                     return 2;
  2274.                 case    B4:
  2275.                     *((long*)obuf) = *((long*)ibuf);
  2276.                     return 4;
  2277.                 case    B8:
  2278.                     *((double*)obuf) = *((double*)ibuf);
  2279.                     return 8;
  2280.                 case    BX:
  2281. #if SUPPORT_LONG_DOUBLE
  2282.                     *((long double*)obuf) = *((long double*)ibuf);
  2283. #else
  2284.                     memcpy(obuf, ibuf, XSZ);
  2285. #endif
  2286.                     return XSZ;
  2287.                 default:
  2288.                     PERROR(pName ": Line:%d LOAD IMMED SYSERR osize=%d\n", iv->lastline, osize);
  2289.             }
  2290.         }
  2291.         else /* osize != mosize */
  2292.         {
  2293.             if(dtype == D_FLOAT)
  2294.             {
  2295.             float f;
  2296.             double d;
  2297. #if SUPPORT_LONG_DOUBLE
  2298.             long double ld;
  2299. #endif
  2300.                 switch(osize)
  2301.                 {
  2302.                     case    B4:
  2303.                         switch(mosize)
  2304.                         {
  2305.                             case    B8:
  2306.                                 f = (float)*((double*)ibuf);
  2307.                                 *((float*)obuf) = f;
  2308.                                 return 4;
  2309.                             case    BX:
  2310. #if SUPPORT_LONG_DOUBLE
  2311.                                 f = (float)*((long double*)ibuf);
  2312.                                 *((float*)obuf) = f;
  2313.                                 return 4;
  2314. #else
  2315.                                 PERROR(pName ": Line:%d long double conversion not supported\n", iv->lastline);
  2316. #endif
  2317.                             default:
  2318.                                 PERROR(pName ": Line:%d bad floating immediate input\n",iv->lastline);
  2319.                         }
  2320.                     case    B8:
  2321.                         switch(mosize)
  2322.                         {
  2323.                             case    B4:
  2324.                                 d = (double)*((float*)ibuf);
  2325.                                 *((double*)obuf) = d;
  2326.                                 return 8;
  2327.                             case    BX:
  2328. #if SUPPORT_LONG_DOUBLE
  2329.                                 d = (double)*((long double*)ibuf);
  2330.                                 *((double*)obuf) = d;
  2331.                                 return 8;
  2332. #else
  2333.                                 PERROR(pName ": Line:%d long double conversion not supported\n", iv->lastline);
  2334. #endif
  2335.                             default:
  2336.                                 PERROR(pName ": Line:%d bad floating immediate input\n", iv->lastline);
  2337.                         }
  2338.                     case    BX:
  2339. #if SUPPORT_LONG_DOUBLE
  2340.                         switch(mosize)
  2341.                         {
  2342.                             case    B4:
  2343.                                 ld = (long double)*((float*)ibuf);
  2344.                                 *((long double*)obuf) = ld;
  2345.                                 return XSZ;
  2346.                             case    B8:
  2347.                                 ld = (long double)*((double*)ibuf);
  2348.                                 *((long double*)obuf) = ld;
  2349.                                 return XSZ;
  2350.                             default:
  2351.                                 PERROR(pName ": Line:%d bad floating immediate input\n", iv->lastline);
  2352.                         }
  2353.                         break;
  2354. #else
  2355.                         PERROR(pName ": Line:%d long double conversion not supported\n", iv->lastline);
  2356. #endif
  2357.                         break;
  2358.                     default:
  2359.                         PERROR(pName ": Line:%d bad floating immediate output\n", iv->lastline);
  2360.                 }
  2361.             }/* END: dtype == D_FLOAT */
  2362.             else if(dtype == D_UNSIGNED || dtype == D_POINTER)
  2363.             {
  2364.                 switch(osize)
  2365.                 {
  2366.                     case B1:
  2367.                         *((unsigned char*)obuf) = *((unsigned char *)ibuf);
  2368.                         return 1;
  2369.                     case B2:
  2370.                         switch(mosize)
  2371.                         {
  2372.                             case B1:
  2373.                                 *((unsigned short*)obuf) = *((unsigned char*)ibuf);
  2374.                                 return 2;
  2375.                             case B4:
  2376.                             case B8:
  2377.                                 *((unsigned short*)obuf) = *((unsigned short*)ibuf);
  2378.                                 return 2;
  2379.                             case BX:
  2380.                                 PERROR(pName ": Line:%d invalid integer size\n", iv->lastline);
  2381.                             default:
  2382.                                 PERROR(pName ": Line:%d invalid immediate input\n", iv->lastline);
  2383.                         }
  2384.                     case B4:
  2385.                         switch(mosize)
  2386.                         {
  2387.                             case B1:
  2388.                                 *((unsigned long*)obuf) = *((unsigned char*)ibuf);
  2389.                                 return 4;
  2390.                             case B2:
  2391.                                 *((unsigned long*)obuf) = *((unsigned short*)ibuf);
  2392.                                 return 4;
  2393.                             case B8:
  2394.                                 *((unsigned long*)obuf) = *((unsigned long*)ibuf);
  2395.                                 return 4;
  2396.                             case BX:
  2397.                                 PERROR(pName ": Line: invalid integer size\n", iv->lastline);
  2398.                             default:
  2399.                                 PERROR(pName ": Line:%d invalid immediate input\n", iv->lastline);
  2400.                         }
  2401.                     case B8:
  2402.                     {
  2403.                     unsigned long l[2];
  2404.                         l[0] = 0;
  2405.                         l[1] = 0;
  2406.                         if(mosize == B4)
  2407.                             l[0] = *((unsigned long*)ibuf);
  2408.                         else if(mosize == B2)
  2409.                             l[0] = *((unsigned short*)ibuf);
  2410.                         else if(mosize == B1)
  2411.                             l[0] = *((unsigned char*)ibuf);
  2412.                         else
  2413.                             PERROR(pName ": Line:%d invalid integer size\n", iv->lastline);
  2414.                         memcpy(obuf, l, 8);
  2415.                         return 8;
  2416.                     }
  2417.                     default:
  2418.                     PERROR(pName ": Line:%d invalid integer size=%d\n",
  2419.                             iv->lastline, osize);
  2420.                 }
  2421.             }
  2422.             else /* signed integer */
  2423.             {
  2424.                 switch(osize)
  2425.                 {
  2426.                     case B1:
  2427.                         *((char*)obuf) = *((char *)ibuf);
  2428.                         return 1;
  2429.                     case B2:
  2430.                         switch(mosize)
  2431.                         {
  2432.                             case B1:
  2433.                                 *((short*)obuf) = *((char*)ibuf);
  2434.                                 return 2;
  2435.                             case B4:
  2436.                             case B8:
  2437.                                 *((short*)obuf) = *((short*)ibuf);
  2438.                                 return 2;
  2439.                             case BX:
  2440.                                 PERROR(pName ": Line:%d invalid integer size\n", iv->lastline);
  2441.                             default:
  2442.                                 PERROR(pName ": Line:%d invalid immediate input\n",iv->lastline);
  2443.                         }
  2444.                     case B4:
  2445.                         switch(mosize)
  2446.                         {
  2447.                             case B1:
  2448.                                 *((long*)obuf) = *((char*)ibuf);
  2449.                                 return 4;
  2450.                             case B2:
  2451.                                 *((long*)obuf) = *((short*)ibuf);
  2452.                                 return 4;
  2453.                             case B8:
  2454.                                 *((long*)obuf) = *((long*)ibuf);
  2455.                                 return 4;
  2456.                             case BX:
  2457.                                 PERROR(pName ": Line:%d invalid integer size\n", iv->lastline);
  2458.                             default:
  2459.                                 PERROR(pName ": Line:%d invalid immediate input\n", iv->lastline);
  2460.                         }
  2461.                     case B8:
  2462.                     {
  2463.                     long l[2];
  2464.                         l[0] = 0;
  2465.                         l[1] = 0;
  2466.                         if(mosize == B4)
  2467.                             l[0] = *((long*)ibuf);
  2468.                         else if(mosize == B2)
  2469.                             l[0] = *((short*)ibuf);
  2470.                         else if(mosize == B1)
  2471.                             l[0] = *((char*)ibuf);
  2472.                         else
  2473.                             PERROR(pName ": Line:%d invalid integer size\n", iv->lastline);
  2474.                         if(l[0] < 0)
  2475.                             l[1] = -1;
  2476.                         memcpy(obuf, l, 8);
  2477.                         return 8;
  2478.                     }
  2479.                     default:
  2480.                       PERROR(pName ": Line:%d invalid integer size=%d\n",
  2481.                           iv->lastline, osize);
  2482.                 }
  2483.             }/* END: signed integer */
  2484.         }/* END: osize != mosize */
  2485.         /* NOT REACHED */
  2486.         PERROR(pName ": Line:%d LOAD IMMED SYSERR1 osize=%d mosize=%d\n", iv->lastline, osize, mosize);
  2487.         return 0; /* suppress compiler warning */
  2488.     }
  2489.     else
  2490.     {/* generate the shortest possible immediate value */
  2491.     int thesize = 0;
  2492.         if(dtype == D_FLOAT)
  2493.         {
  2494.             switch(mosize)
  2495.             {
  2496.                 case    B4:
  2497.                     *((float*)obuf) = *((float*)ibuf);
  2498.                     thesize = 4;
  2499.                     break;
  2500.                 case    B8:
  2501.                     *((double*)obuf) = *((double*)ibuf);
  2502.                     thesize = 8;
  2503.                     break;
  2504.                 case    BX:
  2505. #if SUPPORT_LONG_DOUBLE
  2506.                     *((long double*)obuf) = *((long double*)ibuf);
  2507. #else
  2508.                     memcpy(obuf, ibuf, XSZ);
  2509. #endif
  2510.                     thesize = XSZ;
  2511.                     break;
  2512.                 default:
  2513.                     PERROR(pName ": Line:%d wrong sized floating immediate\n", iv->lastline);
  2514.             }
  2515.         }
  2516.         else /* dtype not D_FLOAT */
  2517.         {
  2518.         int negative;
  2519.         int sgned;
  2520.         unsigned long ul[2];
  2521.         long *sl;
  2522.  
  2523.             ul[1] = 0;        
  2524.             negative = 0;
  2525.             sl = ul;
  2526.             if(dtype == D_UNSIGNED || dtype == D_POINTER)
  2527.             {
  2528.                 sgned = 0;
  2529.                 if(mosize == B8)
  2530.                 {
  2531.                     ul[0] = *((unsigned long*)ibuf);
  2532.                     ul[1] = *((unsigned long*)(((char*)ibuf)+4));
  2533.                 }
  2534.                 else
  2535.                 {
  2536.                     if(mosize == B4)
  2537.                         ul[0] = *((unsigned long*)ibuf);
  2538.                     else if(mosize == B2)
  2539.                         ul[0] = *((unsigned short*)ibuf);
  2540.                     else if(mosize == B1)
  2541.                         ul[0] = *((unsigned char*)ibuf);
  2542.                     else
  2543.                         PERROR(pName ": Line:%d invalid integer immediate\n", iv->lastline);
  2544.                 }
  2545.             }
  2546.             else /* sgned */
  2547.             {
  2548.                 sgned = 1;
  2549.                 if(mosize == B8)
  2550.                 {
  2551.                     sl[0] = *((long*)ibuf);
  2552.                     sl[1] = *((long*)(((char*)ibuf)+4));
  2553.                     if(sl[1] < 0)
  2554.                     {
  2555.                         negative = 1;
  2556.                         ul[1] = ~ul[1];
  2557.                         ul[0] = ~ul[0];
  2558.                         if(ul[0] == 0xffffffff)
  2559.                         {
  2560.                             ul[0] = 0;
  2561.                             ul[1] += 1;
  2562.                         }
  2563.                         else ul[0] += 1;
  2564.                     }
  2565.                 }
  2566.                 else
  2567.                 {
  2568.                     if(mosize == B4)
  2569.                         sl[0] = *((long*)ibuf);
  2570.                     else if(mosize == B2)
  2571.                         sl[0] = *((short*)ibuf);
  2572.                     else if(mosize == B1)
  2573.                         sl[0] = *((char*)ibuf);
  2574.                     else
  2575.                         PERROR(pName ": Line:%d invalid integer immediate\n",iv->lastline);
  2576.                     if(sl[0] < 0)
  2577.                     {
  2578.                         negative = 1;
  2579.                         sl[0] = -sl[0];
  2580.                     }
  2581.                 }
  2582.             } /* END: sgned */
  2583.             if(negative)
  2584.             {
  2585.                 if(ul[1])
  2586.                     mosize = B8;
  2587.                 else if(ul[0] & 0xffff8000)
  2588.                     mosize = B4;
  2589.                 else if(ul[0] & 0xffffff80)
  2590.                     mosize = B2;
  2591.                 else
  2592.                     mosize = B1;
  2593.             }
  2594.             else
  2595.             {
  2596.                 if(sgned)
  2597.                 {
  2598.                     if(ul[1])
  2599.                         mosize = B8;
  2600.                     else if(ul[0] & 0xffff8000)
  2601.                     {
  2602.                         if(!(ul[0] & 0xffff0000))
  2603.                         {
  2604.                             *poc = LUI;
  2605.                             mosize = B2;
  2606.                         }
  2607.                         else
  2608.                             mosize = B4;
  2609.                     }
  2610.                     else if(ul[0] & 0xffffff80)
  2611.                     {
  2612.                         if(!(ul[0] & 0xffffff00))
  2613.                         {
  2614.                             *poc = LUI;
  2615.                             mosize = B1;
  2616.                         }
  2617.                         else
  2618.                             mosize = B2;
  2619.                     }
  2620.                     else
  2621.                         mosize = B1;
  2622.                 }
  2623.                 else /* unsigned */
  2624.                 {
  2625.                     if(ul[1])
  2626.                         mosize = B8;
  2627.                     else if(ul[0] & 0xffff0000)
  2628.                         mosize = B4;
  2629.                     else if(ul[0] & 0xffffff00)
  2630.                         mosize = B2;
  2631.                     else
  2632.                         mosize = B1;
  2633.                 }
  2634.             }
  2635.             if(negative)
  2636.             {
  2637.                 ul[1] = ~ul[1];
  2638.                 ul[0] = ~ul[0];
  2639.                 if(ul[0] == 0xffffffff)
  2640.                 {
  2641.                     ul[0] = 0;
  2642.                     ul[1] += 1;
  2643.                 }
  2644.                 else ul[0] += 1;
  2645.             }
  2646.             switch(mosize)
  2647.             {
  2648.                 case B1:
  2649.                     *((char*)obuf) = *((char*)sl);
  2650.                     thesize = 1;
  2651.                     break;
  2652.                 case B2:
  2653.                     *((short*)obuf) = *((short*)sl);
  2654.                     thesize = 2;
  2655.                     break;
  2656.                 case B4:
  2657.                     *((long*)obuf) = *((long*)sl);
  2658.                     thesize = 4;
  2659.                     break;
  2660.                 case B8:
  2661.                     *((double*)obuf) = *((double*)sl);
  2662.                     thesize = 8;
  2663.                     break;
  2664.             }
  2665.         }/* END: not D_FLOAT */
  2666.         *poc |= mosize;
  2667.         return thesize;
  2668.     }
  2669. }
  2670. static void
  2671. addr_toevalstack(Piv iv, PND pnd)
  2672. {
  2673. unsigned char obuf[20];
  2674. int atype = pnd->atype;
  2675.  
  2676.     if(atype & (A_AUTO|A_PARAM|A_DATA))
  2677.     {
  2678.     int l;
  2679.         obuf[0] = LAI;
  2680.         l = 1;
  2681.         l += load_addr(iv, &obuf[0], &obuf[1], pnd, 0);
  2682.  
  2683.         write_obuf(iv, obuf, l);
  2684.         if(atype & (A_AUTO|A_PARAM))
  2685.         {
  2686.             obuf[0] = ABSSTK;
  2687.             write_obuf(iv, obuf, 1);
  2688.         }
  2689.         else if(l < 5)
  2690.         {
  2691.             obuf[0] = ABSMEM;
  2692.             write_obuf(iv, obuf, 1);
  2693.         }
  2694.         ++iv->stackdepth;
  2695.     }
  2696. }
  2697. static void
  2698. data_toevalstack(Piv iv, PND pnd, PND dst)
  2699. {
  2700. unsigned char obuf[20];
  2701. int xtra, oc;
  2702. unsigned short dtype, atype;
  2703. unsigned char osize;
  2704.  
  2705.         atype = pnd->atype;
  2706.         dtype = pnd->dtype;
  2707.         if(atype & A_IMMED)
  2708.         {
  2709.             if(dtype == D_UNSIGNED)
  2710.                 obuf[0] = LUI;
  2711.             else
  2712.                 obuf[0] = LI;
  2713.             xtra = load_immed(iv, &obuf[0], &obuf[1], pnd, -1);
  2714.             write_obuf(iv, obuf, 1+xtra);
  2715.             ++iv->stackdepth;
  2716.             return;
  2717.         }
  2718.         xtra = oc = 0;
  2719.         osize = pnd->opsize;
  2720.         if(osize == BX)
  2721.         {
  2722.             obuf[0] = XTD;
  2723.             osize = 0;
  2724.             oc = 1;
  2725.             xtra = 1;
  2726.         }
  2727.         if(atype & (A_TEMP|A_RET))
  2728.         {
  2729.             if(atype & A_MEMADDR)
  2730.             {
  2731.             unsigned char xsize = get_datasize(0, pnd);
  2732.                 if(osize == B8 && dtype < D_FLOAT)
  2733.                     obuf[xtra++] = XTD;
  2734.  
  2735.                 if(        dst    
  2736.                     &&    (pnd->TMPNUM == dst->TMPNUM)
  2737.                     &&    ((dst->atype & (A_ABSOLUTE|A_MEMADDR))
  2738.                             == (A_ABSOLUTE|A_MEMADDR))
  2739.                   )
  2740.                 {/* generally used for a += b etc. */
  2741.                     obuf[xtra++] = IMMED;
  2742.                     obuf[xtra] = DEREF1|xsize;
  2743.                     write_obuf(iv, obuf, 1+xtra);
  2744.                     ++iv->stackdepth;
  2745.                 }
  2746.                 else
  2747.                 {
  2748.                     obuf[xtra++] = IMMED;
  2749.                     obuf[xtra] = DEREF|get_datasize(0, pnd);
  2750.                     write_obuf(iv, obuf, 1+xtra);
  2751.                 }
  2752.                 pnd->atype &= ~A_MEMADDR;
  2753.                 pnd->atype |= A_VALUE;
  2754.             }
  2755.             return;
  2756.         }
  2757.  
  2758.         if(atype & (A_AUTO|A_PARAM))
  2759.         {
  2760.             if(atype & (A_POINTER|A_VALUE) == (A_POINTER|A_VALUE))
  2761.             {/* Put address on eval stack */
  2762.                 addr_toevalstack(iv, pnd);
  2763.                 pnd->atype |= A_ABSOLUTE;
  2764.                 return;
  2765.             }
  2766.             else
  2767.             {
  2768.                 obuf[oc] = LS|osize;
  2769.                 xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], pnd, 2);
  2770.                 ++iv->stackdepth;
  2771.             }
  2772.         }
  2773.         else if(atype & A_DATA)
  2774.         {
  2775.             if(atype & (A_POINTER|A_VALUE) == (A_POINTER|A_VALUE))
  2776.             {/* Put address on eval stack */
  2777.                 addr_toevalstack(iv, pnd);
  2778.                 pnd->atype |= A_ABSOLUTE;
  2779.                 return;
  2780.             }
  2781.             else
  2782.             {
  2783.                 obuf[oc] = LM|osize;
  2784.                 xtra += load_addr(iv,&obuf[oc], &obuf[oc+1], pnd, 2);
  2785.                 ++iv->stackdepth;
  2786.             }
  2787.         }
  2788.         write_obuf(iv, obuf, 1+xtra);
  2789. }
  2790. static void
  2791. promote_arg(Piv iv, PND pnd, long argsize)
  2792. {
  2793. long dsize = pnd->size;
  2794. struct _nd dst;
  2795.  
  2796.     if(dsize != argsize)
  2797.     {
  2798.         dst.size = argsize;
  2799.         dst.dtype = pnd->dtype;
  2800.         dst.atype = 0;
  2801.         do_conversion(iv, &dst, pnd);
  2802.     }
  2803. }
  2804. static unsigned char
  2805. arg_toevalstack(Piv iv, PND pnd, long argsize)
  2806. {
  2807. unsigned char dtype;
  2808.  
  2809.     if(pnd->atype & A_IMMED)
  2810.     {
  2811.         data_toevalstack(iv, pnd, 0);
  2812.         return ARG;
  2813.     }
  2814.     dtype = pnd->dtype;
  2815.     if(dtype == D_STRUCT)
  2816.     {
  2817.         addr_toevalstack(iv, pnd);
  2818.         return ARGA;
  2819.     }
  2820.     else if(dtype == D_FUNCTION)
  2821.     {
  2822.         addr_toevalstack(iv, pnd);
  2823.         return ARGF;
  2824.     }
  2825.     else if(dtype == D_FUNCPTR)
  2826.     {
  2827.         data_toevalstack(iv, pnd, 0);
  2828.         return ARGF;
  2829.     }
  2830.     else if(dtype == D_ARRAY)
  2831.     {
  2832.         addr_toevalstack(iv, pnd);
  2833.     }
  2834.     else
  2835.     {
  2836.         data_toevalstack(iv, pnd, 0);
  2837.         promote_arg(iv, pnd, argsize);
  2838.     }
  2839.     return ARG;
  2840. }
  2841. static void
  2842. mov_esdata(Piv iv, unsigned short atype, int size)
  2843. {
  2844. unsigned char obuf[2];
  2845.  
  2846.     if(atype & A_MEMADDR)
  2847.     {
  2848.         if(size == B1)
  2849.             obuf[0] = MOVAA1;
  2850.         else if(size == B2)
  2851.             obuf[0] = MOVAA2;
  2852.         else if(size == B4)
  2853.             obuf[0] = MOVAA4;
  2854.         else if(size == B8)
  2855.             obuf[0] = MOVAA8;
  2856. #if SUPPORT_LONG_DOUBLE
  2857.         else if(size == BX)
  2858.             obuf[0] = MOVAAX;
  2859. #endif
  2860.         else {obuf[0] = MOVAAC; size = XSZ;}
  2861.     }
  2862.     else
  2863.     {
  2864.         if(size == B1)
  2865.             obuf[0] = MOVDA1;
  2866.         else if(size == B2)
  2867.             obuf[0] = MOVDA2;
  2868.         else if(size == B4)
  2869.             obuf[0] = MOVDA4;
  2870.         else if(size == B8)
  2871.             obuf[0] = MOVDA8;
  2872. #if SUPPORT_LONG_DOUBLE
  2873.         else if(size == BX)
  2874.             obuf[0] = MOVDAX;
  2875. #endif
  2876.         else
  2877.             PERROR(pName ": Line:%d illegal mov data size=%d\n", iv->lastline, size);
  2878.     }
  2879.     if(obuf[0] == MOVAAC)
  2880.     {
  2881.         load_val(iv, size);
  2882.         --iv->stackdepth;
  2883.     }
  2884.     write_obuf(iv, obuf, 1);    
  2885.     iv->stackdepth -= 2;
  2886. }
  2887. static void
  2888. from_evalstack(Piv iv, PND d, PND s)
  2889. {
  2890. unsigned char obuf[20];
  2891. int xtra, oc;
  2892. unsigned short datype = d->atype;
  2893. unsigned char osize = d->opsize;
  2894.  
  2895.     xtra = oc = 0;
  2896.  
  2897.     if(datype & (A_TEMP|A_RET))
  2898.     {
  2899.         if(datype & A_MEMADDR)
  2900.         {
  2901.             mov_esdata(iv, s->atype, osize);
  2902.         }
  2903.         return;
  2904.     }
  2905.  
  2906.     if(osize == BX)
  2907.     {
  2908.         obuf[0] = XTD;
  2909.         oc = 1;
  2910.         osize = 0;
  2911.         xtra = 1;
  2912.     }
  2913.     if(datype & (A_AUTO|A_PARAM))
  2914.     {
  2915.         obuf[oc] = SS|osize;
  2916.         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  2917.         --iv->stackdepth;
  2918.     }
  2919.     else if(datype & OPDATA)
  2920.     {
  2921.         obuf[oc] = SM|osize;
  2922.         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  2923.         --iv->stackdepth;
  2924.     }
  2925.     write_obuf(iv, obuf, 1+xtra);
  2926. }
  2927. static unsigned char
  2928. get_datasize(unsigned char opcode, PND pnd)
  2929. {
  2930. unsigned char dtype = pnd->dtype;
  2931. long dsize = pnd->size;
  2932.  
  2933.     switch(dtype)
  2934.     {
  2935.         case    D_ARRAY:
  2936.         case    D_FUNCTION:
  2937.         case    D_STRUCT:
  2938.             return LONG;
  2939.     }
  2940.  
  2941.     if(        opcode == orop
  2942.         ||    opcode == andop
  2943.         ||    opcode == notop
  2944.         ||    opcode == lshop
  2945.         ||    opcode == complop
  2946.         ||    opcode == xorop
  2947.         )
  2948.     {
  2949.         if(dsize == 1)
  2950.             return B1;
  2951.         else if(dsize == 2)
  2952.             return B2;
  2953.         else if(dsize == 4)
  2954.             return B4;
  2955.         else if(dsize == 8)
  2956.             return B8;        
  2957.         else
  2958.             return BX;
  2959.     }
  2960.     else
  2961.     {
  2962.         if(dtype == D_UNSIGNED)
  2963.         {
  2964.             if(dsize == 1)
  2965.                 return UBYTE;
  2966.             else if(dsize == 2)
  2967.                 return USHORT;
  2968.             else if(dsize == 4)
  2969.                 return ULONG;
  2970.             else
  2971.             {
  2972.                 if(opcode == rshop)
  2973.                     return SULONGLONG;
  2974.                 return ULONGLONG;
  2975.             }
  2976.         }
  2977.         else if(dtype == D_FLOAT)
  2978.         {
  2979.             if(dsize == 4)
  2980.                 return FLOAT;
  2981.             else if(dsize == 8)
  2982.                 return DOUBLE;
  2983.             else
  2984.                 return LONGDOUBLE;
  2985.         }
  2986.         else
  2987.         {
  2988.             if(dsize == 1)
  2989.                 return BYTE;
  2990.             else if(dsize == 2)
  2991.                 return SHORT;
  2992.             else if(dsize == 4)
  2993.                 return LONG;
  2994.             else
  2995.             {
  2996.                 if(opcode == rshop)
  2997.                     return SLONGLONG;
  2998.                 return LONGLONG;
  2999.             }
  3000.         }
  3001.     }
  3002.     return 0;
  3003. }
  3004. static void *
  3005. new_nodeO(Piv iv)
  3006. {
  3007. PNODEO p;
  3008.  
  3009.     if(iv->ob_bufcnt < sizeof(NODEO))
  3010.     {/* Allocate a new chunk of linked list space */
  3011.       iv->ob_bufcnt = 4080;
  3012.       iv->ob_buf = Ccalloc(FUNCDATA, 1, iv->ob_bufcnt);
  3013.     }
  3014.     p = (PNODEO)iv->ob_buf;
  3015.     iv->ob_buf += sizeof(NODEO);
  3016.     iv->ob_bufcnt -= sizeof(NODEO);
  3017.     return p;    
  3018. }
  3019. static void
  3020. link_ob(Piv iv)
  3021. {/* Attach to the used list */
  3022.  
  3023.     if(!iv->ob_usedhead)
  3024.     {
  3025.         iv->ob_usedhead = iv->ob;
  3026.         iv->ob_usedtail = iv->ob;
  3027.     }
  3028.     else
  3029.     {
  3030.         iv->ob_usedtail->next = iv->ob;
  3031.         iv->ob_usedtail = iv->ob;
  3032.     }
  3033.     iv->ob = new_nodeO(iv);
  3034. }
  3035. static void *
  3036. new_nodeC(Piv iv)
  3037. {
  3038. PNODEC p;
  3039.  
  3040.     if(iv->cod_bufcnt < sizeof(NODEC))
  3041.     {/* Allocate a new chunk of linked list space */
  3042.       iv->cod_bufcnt = 4080;
  3043.       iv->cod_buf = Ccalloc(FUNCDATA, 1, iv->cod_bufcnt);
  3044.     }
  3045.     p = (PNODEC)iv->cod_buf;
  3046.     iv->cod_buf += sizeof(NODEC);
  3047.     iv->cod_bufcnt -= sizeof(NODEC);
  3048.     return p;    
  3049. }
  3050. static void
  3051. link_cod(Piv iv)
  3052. {/* Attach to the used list */
  3053.  
  3054.     if(!iv->cod_usedhead)
  3055.     {
  3056.         iv->cod_usedhead = iv->cod;
  3057.         iv->cod_usedtail = iv->cod;
  3058.     }
  3059.     else
  3060.     {
  3061.         iv->cod_usedtail->next = iv->cod;
  3062.         iv->cod_usedtail = iv->cod;
  3063.     }
  3064.     iv->cod = new_nodeC(iv);
  3065. }
  3066. static PNODEC
  3067. gen_inst(Piv iv, PNODEO pnode)
  3068. {
  3069. unsigned char *p;
  3070. unsigned char obuf[40];
  3071. int xtra, oc;
  3072. unsigned char opcode;
  3073. PND d,l,r;
  3074.  
  3075.     d = &pnode->d;
  3076.     l = &pnode->l;
  3077.     r = &pnode->r;
  3078.     p = pnode->p;
  3079.     opcode = *p;
  3080.  
  3081.     xtra = oc = 0;
  3082.     
  3083.     switch(opcode)
  3084.     {
  3085.         case regainop:
  3086.             obuf[0] = REGAIN;
  3087.             write_obuf(iv, obuf, 1);
  3088.             ++iv->stackdepth;
  3089.             break;
  3090.         case grabop:
  3091.             obuf[0] = SWAP4DEEP;
  3092.             write_obuf(iv, obuf, 1);
  3093.             break;
  3094.         case getvalop:
  3095.         case derefop:
  3096.         case assignop:
  3097.         case duptmpop:
  3098.         {
  3099.             if(        opcode == getvalop 
  3100.                 &&    !(l->atype & A_IMMED)
  3101.                 &&  l->dtype == D_FUNCTION)
  3102.             {
  3103.                 addr_toevalstack(iv, l);
  3104.             }
  3105.             else if(    opcode != derefop 
  3106.                     &&    !(l->atype & A_IMMED)
  3107.                     && check_assignment_conversion(d, l))
  3108.             {/* Must move data through evaluation stack */
  3109.                 data_toevalstack(iv, l, d);
  3110.                 do_conversion(iv, d, l);
  3111.                 l->atype &= ~A_MEMADDR;
  3112.                 l->atype |= A_VALUE;
  3113.                 if(opcode == assignop)
  3114.                   from_evalstack(iv, d, l);
  3115.             }
  3116.             else
  3117.             {/* Can move data directly from memory to memory */
  3118.             unsigned char ddtype = d->dtype;
  3119.             unsigned char osize = d->opsize;
  3120.             unsigned short latype = l->atype;
  3121.             unsigned short datype = d->atype;
  3122.                 if(osize == BX)
  3123.                 {
  3124.                     obuf[0] = XTD;
  3125.                     oc = 1;
  3126.                     osize = 0;
  3127.                     xtra = 1;
  3128.                 }
  3129.                 if(latype & A_IMMED)
  3130.                 {
  3131.                     if(datype & (A_TEMP|A_RET))
  3132.                     {
  3133.                         if(l->dtype == D_UNSIGNED)
  3134.                             obuf[oc] = LUI;
  3135.                         else
  3136.                             obuf[oc] = LI;
  3137.                         xtra += load_immed(iv, &obuf[oc], &obuf[oc+1], l, -1);
  3138.                         ++iv->stackdepth;
  3139.                         if(datype & A_MEMADDR)
  3140.                         {
  3141.                             write_obuf(iv, obuf, 1+xtra);
  3142.                             mov_esdata(iv, 0, osize);
  3143.                             break;
  3144.                         }
  3145.                     }
  3146.                     else if(datype & (A_AUTO|A_PARAM))
  3147.                     {
  3148.                     int lx;
  3149.                         obuf[0] = IMMED;
  3150.                         oc = 1;
  3151.                         xtra = 1;
  3152.                         obuf[oc] = SSI|osize;
  3153.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  3154.                         lx += load_immed(iv, &obuf[oc], &obuf[oc+1+lx], l, osize);
  3155.                         xtra += lx;
  3156.                     }
  3157.                     else if(datype & A_DATA)
  3158.                     {
  3159.                     int lx;
  3160.                         obuf[0] = IMMED;
  3161.                         oc = 1;
  3162.                         xtra = 1;
  3163.                         obuf[oc] = SMI|osize;
  3164.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  3165.                         lx += load_immed(iv, &obuf[oc], &obuf[oc+1+lx], l, osize);
  3166.                         xtra += lx;
  3167.                     }
  3168.                     else
  3169.                     {
  3170.                         return iv->cod_usedtail;
  3171.                     }
  3172.                 }
  3173.                 else if(latype & (A_TEMP|A_RET))
  3174.                 {
  3175.                     if(datype & (A_TEMP|A_RET))
  3176.                     {
  3177.                         if(opcode == duptmpop)
  3178.                         {
  3179.                             if(osize <= B4)
  3180.                                 obuf[0] = DUP4;
  3181.                             else
  3182.                                 obuf[0] = DUP;
  3183.                             xtra = 0;
  3184.                             ++iv->stackdepth;
  3185.                         }
  3186.                         else if(opcode == getvalop || opcode == derefop)
  3187.                         {
  3188.                             if(latype & A_MEMADDR)
  3189.                             {
  3190.                             unsigned char xsize = get_datasize(0, l);
  3191.                                 if(osize == B8 && ddtype < D_FLOAT)
  3192.                                     obuf[xtra++] = XTD;
  3193.                                 if(datype & A_MEMADDR)
  3194.                                     xsize = ULONG;
  3195.                                 obuf[xtra++] = IMMED;
  3196.                                 obuf[xtra] = DEREF|xsize;
  3197.                             }
  3198.                             else return iv->cod_usedtail;    /* nop */
  3199.                         }
  3200.                         else if(datype & A_MEMADDR)
  3201.                         {
  3202.                             mov_esdata(iv, latype, osize);
  3203.                             break;
  3204.                         }
  3205.                         else
  3206.                         {/* move a value temp to a value temp, effective nop */
  3207.                             return iv->cod_usedtail;
  3208.                         }
  3209.                     }
  3210.                     else if(datype & (A_AUTO|A_PARAM))
  3211.                     {
  3212.                         if(latype & A_MEMADDR)
  3213.                         {
  3214.                         unsigned char xsize = get_datasize(0, l);
  3215.                         int qc = 1;
  3216.                             if(        osize == BX
  3217.                                 ||    (osize == B8 && ddtype < D_FLOAT))
  3218.                                 obuf[qc++] = XTD;
  3219.                             obuf[qc++] = IMMED;
  3220.                             obuf[qc] = DEREF|xsize;
  3221.                             write_obuf(iv, obuf+1, qc);
  3222.                         }
  3223.                         obuf[oc] = SS|osize;
  3224.                         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  3225.                         --iv->stackdepth;
  3226.                     }
  3227.                     else if(datype & A_DATA)
  3228.                     {
  3229.                         if(latype & A_MEMADDR)
  3230.                         {
  3231.                         unsigned char xsize = get_datasize(0, l);
  3232.                         int qc = 1;
  3233.                             if(        osize == BX
  3234.                                 ||    (osize == B8 && ddtype < D_FLOAT))
  3235.                                 obuf[qc++] = XTD;
  3236.                             obuf[qc++] = IMMED;
  3237.                             obuf[qc] = DEREF|xsize;
  3238.                             write_obuf(iv, obuf+1, qc);
  3239.                         }
  3240.                         obuf[oc] = SM|osize;
  3241.                         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], d, 2);
  3242.                         --iv->stackdepth;
  3243.                     }
  3244.                     else
  3245.                     {
  3246.                         return iv->cod_usedtail;
  3247.                     }
  3248.                 }
  3249.                 else if(latype & (A_AUTO|A_PARAM))
  3250.                 {
  3251.                 int lx;
  3252.                     if((latype & (A_POINTER|A_VALUE)) == (A_POINTER|A_VALUE))
  3253.                     {/* Put address on eval stack */
  3254.                         addr_toevalstack(iv, l);
  3255.                         if(opcode == assignop)
  3256.                           from_evalstack(iv, d, l);
  3257.                         break;
  3258.                     }
  3259.                     if(datype & (A_TEMP|A_RET))
  3260.                     {
  3261.                         if(opcode == derefop)
  3262.                             osize = B4;
  3263.                         obuf[oc] = LS|osize;
  3264.                         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3265.                         ++iv->stackdepth;
  3266.                         if(opcode == assignop)
  3267.                         {
  3268.                             /* Here we need a 'store from stack addr' inst */
  3269.                             if(datype & A_MEMADDR)
  3270.                             {/* Address was on the eval stack */
  3271.                                 write_obuf(iv, obuf, 1+xtra);
  3272.                                 mov_esdata(iv, 0, osize);
  3273.                                 break;
  3274.                             }
  3275.                         }
  3276.                     }
  3277.                     else if(datype & (A_AUTO|A_PARAM))
  3278.                     {
  3279.                         obuf[oc] = MOVSS|osize;
  3280.                         obuf[++oc] = 0;
  3281.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3282.                         lx += load_addr(iv, &obuf[oc], &obuf[oc+1+lx], d, 0);
  3283.                         xtra += lx+1;
  3284.                     }
  3285.                     else if(datype & A_DATA)
  3286.                     {
  3287.                         obuf[oc] = MOVSM|osize;
  3288.                         obuf[++oc] = 0;
  3289.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3290.                         lx += load_addr(iv, &obuf[oc], &obuf[oc+1+lx], d, 0);
  3291.                         xtra += lx+1;
  3292.                     }
  3293.                     else
  3294.                     {
  3295.                         return iv->cod_usedtail;
  3296.                     }
  3297.                 }
  3298.                 else if(latype & A_DATA)
  3299.                 {
  3300.                 int lx;
  3301.                     if((latype & (A_POINTER|A_VALUE)) == (A_POINTER|A_VALUE))
  3302.                     {/* Put address on eval stack */
  3303.                         addr_toevalstack(iv, l);
  3304.                         if(opcode == assignop)
  3305.                           from_evalstack(iv, d, l);
  3306.                         break;
  3307.                     }
  3308.                     if(datype & (A_TEMP|A_RET))
  3309.                     {
  3310.                         if(opcode == derefop)
  3311.                             osize = B4;
  3312.                         obuf[oc] = LM|osize;
  3313.                         xtra += load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3314.                         ++iv->stackdepth;
  3315.                         if(opcode == assignop)
  3316.                         {
  3317.                             /* Here we need a 'store from stack addr' inst */
  3318.                              if(datype & A_MEMADDR)
  3319.                             {/* Address was on the eval stack */
  3320.                                 write_obuf(iv, obuf, 1+xtra);
  3321.                                 mov_esdata(iv, 0, osize);
  3322.                                 break;
  3323.                             }
  3324.                         }
  3325.                     }
  3326.                     else if(datype & (A_AUTO|A_PARAM))
  3327.                     {
  3328.                         obuf[oc] = MOVMS|osize;
  3329.                         obuf[++oc] = 0;
  3330.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3331.                         lx += load_addr(iv, &obuf[oc], &obuf[oc+1+lx], d, 0);
  3332.                         xtra += lx+1;
  3333.                     }
  3334.                     else if(datype & A_DATA)
  3335.                     {
  3336.                         obuf[oc] = MOVMM|osize;
  3337.                         obuf[++oc] = 0;
  3338.                         lx = load_addr(iv, &obuf[oc], &obuf[oc+1], l, 2);
  3339.                         lx += load_addr(iv, &obuf[oc], &obuf[oc+1+lx], d, 0);
  3340.                         xtra += lx+1;
  3341.                     }
  3342.                     else
  3343.                     {
  3344.                         return iv->cod_usedtail;
  3345.                     }
  3346.                 }
  3347.                 else
  3348.                 {
  3349.                     return iv->cod_usedtail;
  3350.                 }
  3351.                 write_obuf(iv, obuf, 1+xtra);
  3352.             }/* END: move data directly */
  3353.             if(opcode == assignop && iv->has_structret)
  3354.             {
  3355.                 obuf[0] = XTD;
  3356.                 obuf[1] = PRUNESTRUCT;
  3357.                 write_obuf(iv, obuf, 2);
  3358.                 iv->has_structret = 0;
  3359.             }
  3360.             break;
  3361.         }
  3362.         case plusop:
  3363.         case minusop:
  3364.         case mulop:
  3365.         case divop:
  3366.         case orop:
  3367.         case xorop:
  3368.         case andop:
  3369.         case eqop:
  3370.         case neqop:
  3371.         case ltop:
  3372.         case gtop:
  3373.         case leop:
  3374.         case geop:
  3375.         {/* BINOPS */
  3376.         unsigned char ddtype;
  3377.         unsigned char osize;
  3378.         unsigned short latype = l->atype;
  3379.         unsigned short ratype = r->atype;
  3380.         PND bestptr = 0;
  3381.  
  3382.             if(latype & A_IMMED || ratype & A_IMMED)
  3383.             {
  3384.               if(ratype & A_IMMED)
  3385.               {/* IMMEDIATE IS ON THE RIGHT */
  3386.               bestptr = l;
  3387.  
  3388.                 data_toevalstack(iv, l, d);
  3389.                 if(check_binop_conversion(r, l))
  3390.                     do_conversion(iv, r, l);
  3391.  
  3392.                 data_toevalstack(iv, r, d);
  3393.  
  3394.                 ddtype = bestptr->dtype;
  3395.                 osize = l->opsize;
  3396.  
  3397.                 if(osize == BX || (osize == B8 && ddtype != D_FLOAT))
  3398.                     obuf[oc++] = XTD;
  3399.                 obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3400.                 --iv->stackdepth;
  3401.                 write_obuf(iv, obuf, oc);
  3402.               }
  3403.               else /* IMMEDIATE IS ON THE LEFT */
  3404.               {
  3405.                 bestptr = r;
  3406.                 if(ratype & (A_TEMP|A_RET))
  3407.                 {
  3408.                     data_toevalstack(iv, r, d);
  3409.                     if(check_binop_conversion(l, r))
  3410.                         do_conversion(iv, l, r);
  3411.                     data_toevalstack(iv, l, d);
  3412.  
  3413.                     ddtype = bestptr->dtype;
  3414.                     osize = r->opsize;
  3415.  
  3416.                     if(        opcode != plusop
  3417.                         &&    opcode != eqop
  3418.                         &&    opcode != neqop
  3419.                         &&    opcode != andop
  3420.                         &&    opcode != orop
  3421.                         &&    opcode != mulop
  3422.                         &&    opcode != xorop
  3423.                         )
  3424.                     {/* stack order relevant */
  3425.                         if(osize <= B4)
  3426.                             obuf[0] = SWAP4;
  3427.                         else
  3428.                             obuf[0] = SWAP;
  3429.                         write_obuf(iv, obuf, 1);
  3430.                     }
  3431.                 }
  3432.                 else
  3433.                 {
  3434.                     data_toevalstack(iv, l, d);
  3435.                     data_toevalstack(iv, r, d);
  3436.                     if(check_binop_conversion(l, r))
  3437.                         do_conversion(iv, l, r);
  3438.  
  3439.                     ddtype = r->dtype;
  3440.                     osize = r->opsize;
  3441.                 }
  3442.  
  3443.                 if(osize == BX || (osize == B8 && ddtype != D_FLOAT))
  3444.                     obuf[oc++] = XTD;
  3445.                 obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3446.                 write_obuf(iv, obuf, oc);
  3447.                 --iv->stackdepth;
  3448.               }/* END: IMMEDIATE IS ON LEFT */
  3449.             }/* IMMEDIATES */
  3450.             else
  3451.             {/* NOT IMMEDIATES */
  3452.  
  3453.                 if(ratype & (A_TEMP|A_RET))
  3454.                 {/* RIGHT HAND SIDE IS ALREADY ON STACK */
  3455.  
  3456.                     data_toevalstack(iv, r, d);
  3457.  
  3458.                     if(latype & (A_TEMP|A_RET))
  3459.                     {/* LEFT HAND SIDE WAS ALSO ON STACK */
  3460.  
  3461.                         /* Is it the same data as right hand side ?? */
  3462.                         if(l->TMPNUM == r->TMPNUM) 
  3463.                         {
  3464.                             if(r->opsize <= B4)
  3465.                                 obuf[0] = DUP4;
  3466.                             else
  3467.                                 obuf[0] = DUP;
  3468.                             write_obuf(iv, obuf, 1);
  3469.                             ++iv->stackdepth;
  3470.                             l->atype = r->atype;
  3471.                         }
  3472.                         else
  3473.                         {
  3474.                         unsigned char lltype = l->dtype;
  3475.                         unsigned lsize = l->opsize;
  3476.                             if(latype & A_MEMADDR)
  3477.                             {
  3478.                             unsigned char xsize = get_datasize(0, l);
  3479.                             int qc = 0;
  3480.  
  3481.                                 if(        (l->TMPNUM == d->TMPNUM)
  3482.                                     &&    ((d->atype & (A_ABSOLUTE|A_MEMADDR))
  3483.                                             == (A_ABSOLUTE|A_MEMADDR))
  3484.                                     )
  3485.                                 {/* it must expand with a DEREF1 */
  3486.                                     if(lsize <= B4)
  3487.                                         obuf[0] = DUP4;
  3488.                                     else
  3489.                                         obuf[0] = DUP;
  3490.                                     obuf[1] = DUMP;
  3491.                                     obuf[2] = DUMP;
  3492.                                     write_obuf(iv, obuf, 3);
  3493.  
  3494.                                     if(        lsize == BX
  3495.                                         ||    (lsize == B8 && lltype < D_FLOAT))
  3496.                                         obuf[qc++] = XTD;
  3497.                                     obuf[qc++] = IMMED;
  3498.                                     obuf[qc++] = DEREF1|xsize;
  3499.                                     write_obuf(iv, obuf, qc);
  3500.                                     ++iv->stackdepth;
  3501.                                 }
  3502.                                 else
  3503.                                 {
  3504.                                     obuf[0] = DUMP;
  3505.                                     write_obuf(iv, obuf, 1);
  3506.  
  3507.                                     if(        lsize == BX
  3508.                                         ||    (lsize == B8 && lltype < D_FLOAT))
  3509.                                         obuf[qc++] = XTD;
  3510.                                     obuf[qc++] = IMMED;
  3511.                                     obuf[qc++] = DEREF|xsize;
  3512.                                     write_obuf(iv, obuf, qc);
  3513.                                 }
  3514.                                 l->atype &= ~A_MEMADDR;
  3515.                                 l->atype |= A_VALUE;
  3516.  
  3517.                                 if(check_binop_conversion(r, l))
  3518.                                 {
  3519.                                     bestptr = r;
  3520.                                     do_conversion(iv, r, l);
  3521.                                 }
  3522.                                 obuf[0] = REGAIN;
  3523.                                 write_obuf(iv, obuf, 1);
  3524.  
  3525.                                 if(!bestptr)
  3526.                                  if(check_binop_conversion(l, r))
  3527.                                  {
  3528.                                     bestptr = l;
  3529.                                     do_conversion(iv, l, r);
  3530.                                  }
  3531.                             }
  3532.                         }
  3533.                     }
  3534.                     else
  3535.                     {/* LEFT HAND SIDE IS NOT ON STACK */
  3536.  
  3537.                         if(check_binop_conversion(l, r))
  3538.                         {
  3539.                             bestptr = l;
  3540.                             do_conversion(iv, l, r);
  3541.                         }
  3542.                         data_toevalstack(iv, l, d);
  3543.                         if(!bestptr)
  3544.                          if(check_binop_conversion(r, l))
  3545.                          {
  3546.                             bestptr = r;
  3547.                             do_conversion(iv, r, l);
  3548.                          }
  3549.                         if(        opcode != plusop
  3550.                             &&    opcode != eqop
  3551.                             &&    opcode != neqop
  3552.                             &&    opcode != andop
  3553.                             &&    opcode != orop
  3554.                             &&  opcode != mulop
  3555.                             &&    opcode != xorop
  3556.                             )
  3557.                         {/* stack order relevant */
  3558.                             if(r->opsize <= B4)
  3559.                                 obuf[0] = SWAP4;
  3560.                             else
  3561.                                 obuf[0] = SWAP;
  3562.                             write_obuf(iv, obuf, 1);
  3563.                         }
  3564.                     }
  3565.                 }
  3566.                 else /* ONLY LEFT HAND SIDE CAN BE ON STACK */
  3567.                 {
  3568.                     data_toevalstack(iv, l, d);
  3569.                     if(check_binop_conversion(r, l))
  3570.                     {
  3571.                         bestptr = r;
  3572.                         do_conversion(iv, r, l);
  3573.                     }
  3574.  
  3575.                     data_toevalstack(iv, r, d);
  3576.                     if(!bestptr)
  3577.                      if(check_binop_conversion(l, r))
  3578.                      {
  3579.                         bestptr = l;
  3580.                         do_conversion(iv, l, r);
  3581.                     }
  3582.                 }
  3583.                 if(!bestptr)
  3584.                     bestptr = l;
  3585.  
  3586.                 ddtype = bestptr->dtype;
  3587.                 osize = bestptr->opsize;
  3588.  
  3589.                 if(osize == BX || (osize==B8 && ddtype != D_FLOAT))
  3590.                     obuf[oc++] = XTD;
  3591.                 obuf[oc++] = binops[opcode]|get_datasize(opcode, bestptr);
  3592.                 write_obuf(iv, obuf, oc);
  3593.                 --iv->stackdepth;
  3594.             }/* END: NOT IMMEDIATES */
  3595.  
  3596.             if(opcode >= eqop)
  3597.             {
  3598.                 bestptr = &longtype;
  3599.             }
  3600.  
  3601.             if(check_assignment_conversion(d, bestptr))
  3602.                 do_conversion(iv, d, bestptr);
  3603.             bestptr->atype &= ~A_MEMADDR;
  3604.             bestptr->atype |= A_VALUE;
  3605.             from_evalstack(iv, d, bestptr);
  3606.  
  3607.             break;
  3608.         }/* END: BINOPS */
  3609.  
  3610.         case lshop:
  3611.         case rshop:
  3612.         {/* SHIFTS */
  3613.         unsigned short latype = l->atype;
  3614.         unsigned short ratype = r->atype;
  3615.         PND bestptr;
  3616.  
  3617.  
  3618.             if(latype & A_IMMED || ratype & A_IMMED)
  3619.             {
  3620.               if(ratype & A_IMMED)
  3621.               {/* IMMEDIATE IS ON THE RIGHT */
  3622.                 data_toevalstack(iv, l, d);
  3623.  
  3624.                 obuf[0] = XTD;
  3625.                 if(opcode == lshop)
  3626.                 {
  3627.                     obuf[1] = LSHI|get_datasize(opcode, l);
  3628.                     obuf[2] = r->data->Uuchar;
  3629.                 }
  3630.                 else if(opcode == rshop)
  3631.                 {
  3632.                     obuf[1] = RSHI|get_datasize(opcode, l);
  3633.                     obuf[2] = r->data->Uuchar;
  3634.                 }
  3635.                 write_obuf(iv, obuf, 3);
  3636.               }
  3637.               else /* IMMEDIATE IS ON THE LEFT */
  3638.               {
  3639.                 if(ratype & (A_TEMP|A_RET))
  3640.                 {
  3641.                     data_toevalstack(iv, r, d);
  3642.                     data_toevalstack(iv, l, d);
  3643.                     if(r->opsize <= B4)
  3644.                         obuf[0] = SWAP4;
  3645.                     else
  3646.                         obuf[0] = SWAP;
  3647.                     write_obuf(iv, obuf, 1);
  3648.                 }
  3649.                 else
  3650.                 {
  3651.                     data_toevalstack(iv, l, d);
  3652.                     data_toevalstack(iv, r, d);
  3653.                 }
  3654.  
  3655.                 obuf[0] = XTD;
  3656.                 obuf[1] = binops[opcode]|get_datasize(opcode, l);
  3657.                 write_obuf(iv, obuf, 2);
  3658.                 --iv->stackdepth;
  3659.               }
  3660.             }
  3661.             else
  3662.             {/* NOT IMMEDIATES */
  3663.  
  3664.                 if(ratype & (A_TEMP|A_RET))
  3665.                 {/* RIGHT HAND SIDE IS ALREADY ON STACK */
  3666.  
  3667.                     data_toevalstack(iv, r, d);
  3668.  
  3669.                     if(latype & (A_TEMP|A_RET))
  3670.                     {/* LEFT HAND SIDE WAS ALSO ON STACK */
  3671.  
  3672.                         /* Is it the same data as right hand side ?? */
  3673.                         if(l->TMPNUM == r->TMPNUM)
  3674.                         {
  3675.                             if(l->opsize <= B4 && r->opsize <= B4)
  3676.                                 obuf[0] = DUP4;
  3677.                             else
  3678.                                 obuf[0] = DUP;
  3679.                             write_obuf(iv, obuf, 1);
  3680.                             ++iv->stackdepth;
  3681.                             l->atype = r->atype;
  3682.                         }
  3683.                         else
  3684.                         {/* reversed stack and not same data */
  3685.  
  3686.                             if(latype & A_MEMADDR)
  3687.                             {
  3688.                             unsigned char xsize = l->opsize;
  3689.                             int qc = 0;
  3690.  
  3691.                                 if(        (l->TMPNUM == d->TMPNUM)
  3692.                                     &&    ((d->atype & (A_ABSOLUTE|A_MEMADDR))
  3693.                                             == (A_ABSOLUTE|A_MEMADDR))
  3694.                                     )
  3695.                                 {/* it must expand with a DEREF1 */
  3696.                                     if(l->opsize <= B4)
  3697.                                         obuf[0] = DUP4;
  3698.                                     else
  3699.                                         obuf[0] = DUP;
  3700.                                     obuf[1] = DUMP;
  3701.                                     obuf[2] = DUMP;
  3702.                                     write_obuf(iv, obuf, 3);
  3703.  
  3704.                                     if(l->opsize == B8)
  3705.                                         obuf[qc++] = XTD;
  3706.                                     obuf[qc++] = IMMED;
  3707.                                     obuf[qc++] = DEREF1|xsize;
  3708.                                     write_obuf(iv, obuf, qc);
  3709.                                     ++iv->stackdepth;
  3710.                                 }
  3711.                                 else
  3712.                                 {
  3713.                                     obuf[0] = DUMP;
  3714.                                     write_obuf(iv, obuf, 1);
  3715.  
  3716.                                     if(l->opsize == B8)
  3717.                                         obuf[qc++] = XTD;
  3718.                                     obuf[qc++] = IMMED;
  3719.                                     obuf[qc++] = DEREF|xsize;
  3720.                                     write_obuf(iv, obuf, qc);
  3721.                                 }
  3722.                                 l->atype &= ~A_MEMADDR;
  3723.                                 l->atype |= A_VALUE;
  3724.  
  3725.                                 obuf[0] = REGAIN;
  3726.                                 write_obuf(iv, obuf, 1);
  3727.                             }
  3728.                         }
  3729.                     }
  3730.                     else
  3731.                     {/* LEFT HAND SIDE IS NOT ON STACK */
  3732.  
  3733.                         data_toevalstack(iv, l, d);
  3734.                         if(r->opsize <= B4 && l->opsize <= B4)
  3735.                             obuf[0] = SWAP4;
  3736.                         else
  3737.                             obuf[0] = SWAP;
  3738.                         write_obuf(iv, obuf, 1);
  3739.                     }
  3740.                 }
  3741.                 else /* ONLY LEFT HAND SIDE CAN BE ON STACK */
  3742.                 {
  3743.                     data_toevalstack(iv, l, d);
  3744.                     data_toevalstack(iv, r, d);
  3745.                 }
  3746.  
  3747.                 /* ENSURE THAT THE RIGHT SIDE IS A LONG OR A LONG LONG */
  3748.                 bestptr = (l->size > 4) ? &longlongtype : &longtype;
  3749.                 if(check_assignment_conversion(bestptr, r))
  3750.                     do_conversion(iv, bestptr, r);
  3751.  
  3752.                 obuf[0] = XTD;
  3753.                 obuf[1] = binops[opcode]|get_datasize(opcode, l);
  3754.                 write_obuf(iv, obuf, 2);
  3755.                 --iv->stackdepth;
  3756.             }/* END: NOT IMMEDIATES */
  3757.  
  3758.             if(check_assignment_conversion(d, l))
  3759.                 do_conversion(iv, d, l);
  3760.             l->atype &= ~A_MEMADDR;
  3761.             l->atype |= A_VALUE;
  3762.             from_evalstack(iv, d, l);
  3763.  
  3764.             break;
  3765.         }/* END: SHIFTS */
  3766.         case modop:
  3767.         {/* MODULUS */
  3768.         unsigned short latype = l->atype;
  3769.         unsigned short ratype = r->atype;
  3770.         PND bestptr;
  3771.  
  3772.             if(latype & A_IMMED || ratype & A_IMMED)
  3773.             {
  3774.               if(ratype & A_IMMED)
  3775.               {/* IMMEDIATE IS ON THE RIGHT */
  3776.                 data_toevalstack(iv, l, d);
  3777.  
  3778.                 if(l->opsize == B8)
  3779.                 {
  3780.                     obuf[oc++] = XTD;
  3781.                 }
  3782.                 if(d->size == 2)
  3783.                 {
  3784.                     obuf[oc++] = IMMED;
  3785.                     obuf[oc++] = MODI|get_datasize(opcode, l);
  3786.                     *((short*)&obuf[oc]) = r->data->Uushort;
  3787.                     xtra = 2;
  3788.                 }
  3789.                 else
  3790.                 {/* no fast instructions available */
  3791.                     data_toevalstack(iv, r, d);
  3792.                     obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3793.                     --iv->stackdepth;
  3794.                 }
  3795.                 write_obuf(iv, obuf, oc+xtra);
  3796.               }
  3797.               else /* IMMEDIATE IS ON THE LEFT */
  3798.               {
  3799.  
  3800.                 if(ratype & (A_TEMP|A_RET))
  3801.                 {
  3802.                     data_toevalstack(iv, r, d);
  3803.                     data_toevalstack(iv, l, d);
  3804.                     if(r->opsize <= B4)
  3805.                         obuf[0] = SWAP4;
  3806.                     else
  3807.                         obuf[0] = SWAP;
  3808.                     write_obuf(iv, obuf, 1);
  3809.                 }
  3810.                 else
  3811.                 {/* load eval stack without regard for conversion */
  3812.                     data_toevalstack(iv, l, d);
  3813.                     data_toevalstack(iv, r, d);
  3814.                 }
  3815.  
  3816.                 if(r->opsize == B8)
  3817.                 {
  3818.                     obuf[oc++] = XTD;
  3819.                 }
  3820.                 obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3821.                 write_obuf(iv, obuf, oc);
  3822.                 --iv->stackdepth;
  3823.               }
  3824.             }
  3825.             else
  3826.             {/* NOT IMMEDIATES */
  3827.                 if(ratype & (A_TEMP|A_RET))
  3828.                 {/* RIGHT HAND SIDE IS ALREADY ON STACK */
  3829.  
  3830.                     data_toevalstack(iv, r, d);
  3831.  
  3832.                     if(latype & (A_TEMP|A_RET))
  3833.                     {/* LEFT HAND SIDE WAS ALSO ON STACK */
  3834.  
  3835.                         /* Is it the same data as right hand side ?? */
  3836.                         if(l->TMPNUM == r->TMPNUM)
  3837.                         {
  3838.                             if(l->opsize <= B4 && r->opsize <= B4)
  3839.                                 obuf[0] = DUP4;
  3840.                             else
  3841.                                 obuf[0] = DUP;
  3842.                             write_obuf(iv, obuf, 1);
  3843.                             ++iv->stackdepth;
  3844.                             l->atype = r->atype;
  3845.                         }
  3846.                         else
  3847.                         {/* reversed stack and not same data */
  3848.  
  3849.                             if(latype & A_MEMADDR)
  3850.                             {
  3851.                             unsigned char xsize = get_datasize(0, l);
  3852.                             int qc = 0;
  3853.  
  3854.                                 if(        (l->TMPNUM == d->TMPNUM)    
  3855.                                     &&    ((d->atype & (A_ABSOLUTE|A_MEMADDR))
  3856.                                             == (A_ABSOLUTE|A_MEMADDR))
  3857.                                     )
  3858.                                 {/* it must expand with a DEREF1 */
  3859.                                     if(r->opsize <= B4)
  3860.                                         obuf[0] = DUP4;
  3861.                                     else
  3862.                                         obuf[0] = DUP;
  3863.                                     obuf[1] = DUMP;
  3864.                                     obuf[2] = DUMP;
  3865.                                     write_obuf(iv, obuf, 3);
  3866.  
  3867.                                     if(r->opsize == B8)
  3868.                                         obuf[qc++] = XTD;
  3869.                                     obuf[qc++] = IMMED;
  3870.                                     obuf[qc++] = DEREF1|xsize;
  3871.                                     write_obuf(iv, obuf, qc);
  3872.                                     ++iv->stackdepth;
  3873.                                 }
  3874.                                 else
  3875.                                 {
  3876.                                     obuf[0] = DUMP;
  3877.                                     write_obuf(iv, obuf, 1);
  3878.  
  3879.                                     if(r->opsize == B8)
  3880.                                         obuf[qc++] = XTD;
  3881.                                     obuf[qc++] = IMMED;
  3882.                                     obuf[qc++] = DEREF|xsize;
  3883.                                     write_obuf(iv, obuf, qc);
  3884.                                 }
  3885.                                 l->atype &= ~A_MEMADDR;
  3886.                                 l->atype |= A_VALUE;
  3887.  
  3888.                                 obuf[0] = REGAIN;
  3889.                                 write_obuf(iv, obuf, 1);
  3890.                             }
  3891.                         }
  3892.                     }
  3893.                     else
  3894.                     {/* LEFT HAND SIDE IS NOT ON STACK */
  3895.  
  3896.                         data_toevalstack(iv, l, d);
  3897.                         if(r->opsize <= B4 && l->opsize <= B4)
  3898.                             obuf[0] = SWAP4;
  3899.                         else
  3900.                             obuf[0] = SWAP;
  3901.                         write_obuf(iv, obuf, 1);
  3902.                     }
  3903.                 }
  3904.                 else /* ONLY LEFT HAND SIDE CAN BE ON STACK */
  3905.                 {
  3906.                     data_toevalstack(iv, l, d);
  3907.                     data_toevalstack(iv, l, d);
  3908.                 }
  3909.  
  3910.                 /* ENSURE THAT THE RIGHT SIDE IS A LONG OR A LONG LONG */
  3911.                 bestptr = (l->size > 4) ? &longlongtype : &longtype;
  3912.                 if(check_assignment_conversion(bestptr, r))
  3913.                     do_conversion(iv, bestptr, r);
  3914.  
  3915.                 if(l->opsize == B8)
  3916.                 {
  3917.                     obuf[oc++] = XTD;
  3918.                 }
  3919.                 obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3920.                 write_obuf(iv, obuf, oc);
  3921.                 --iv->stackdepth;
  3922.             }/* END: NOT IMMEDIATES */
  3923.  
  3924.             if(check_assignment_conversion(d, l))
  3925.                 do_conversion(iv, d, l);
  3926.             l->atype &= ~A_MEMADDR;
  3927.             l->atype |= A_VALUE;
  3928.             from_evalstack(iv, d, l);
  3929.  
  3930.             break;
  3931.         }/* END: MODULUS */
  3932.         case truthop:
  3933.         case aliastmpop:
  3934.         {/* no immediates */
  3935.  
  3936.             data_toevalstack(iv, l, d);
  3937.  
  3938.             if(l->opsize != B1)
  3939.             {/* no need to get truth of byte sized elements */
  3940.                 if(l->opsize == BX)
  3941.                 {
  3942.                     obuf[oc++] = XTD;
  3943.                 }
  3944.                 obuf[oc++] = TRUTHOF|l->opsize;
  3945.                 write_obuf(iv, obuf, oc);
  3946.             }
  3947.             break;
  3948.         }
  3949.         case complop:
  3950.         case notop:
  3951.         {/* no immediates */
  3952.         unsigned char osize = get_datasize(opcode, l);
  3953.  
  3954.             data_toevalstack(iv, l, d);
  3955.             if(osize == BX)
  3956.             {/* only notop can operate on long double */
  3957.                 obuf[oc++] = XTD;
  3958.             }
  3959.             obuf[oc++] = binops[opcode]|osize;
  3960.             write_obuf(iv, obuf, oc);
  3961.             break;
  3962.         }
  3963.         case negop:
  3964.         {/* no immediates */
  3965.  
  3966.             data_toevalstack(iv, l, d);
  3967.             if(l->opsize == BX || (l->opsize==B8 && l->dtype != D_FLOAT))
  3968.             {
  3969.                 obuf[oc++] = XTD;
  3970.             }
  3971.             obuf[oc++] = binops[opcode]|get_datasize(opcode, l);
  3972.             write_obuf(iv, obuf, oc);
  3973.             break;
  3974.         }
  3975.         case copyop:
  3976.         {
  3977.             if(d->atype & (A_TEMP|A_RET))
  3978.             {
  3979.                 if(!(l->atype & (A_TEMP|A_RET)))
  3980.                 {
  3981.                     data_toevalstack(iv, l, d);
  3982.                 }
  3983.             }
  3984.             else
  3985.             {
  3986.                 if(!(l->atype & (A_TEMP|A_RET)))
  3987.                 {
  3988.                     data_toevalstack(iv, d, d);
  3989.                     data_toevalstack(iv, l, d);
  3990.                 }
  3991.                 else
  3992.                 {
  3993.                     data_toevalstack(iv, d, d);
  3994.                     obuf[0] = SWAP4;
  3995.                     write_obuf(iv, obuf, 1);
  3996.                 }
  3997.             }
  3998.             load_val(iv, d->size);
  3999.             obuf[0] = MOVAAC;
  4000.             write_obuf(iv, obuf, 1);
  4001.             iv->stackdepth -= 3;
  4002.             break;
  4003.         }
  4004.         case castop:
  4005.         {
  4006.             data_toevalstack(iv, l, d);
  4007.             if(check_assignment_conversion(d, l))
  4008.                 do_conversion(iv, d, l);
  4009.             break;
  4010.         }
  4011.         case jmploopop:
  4012.         case jmpcontinueop:
  4013.         case jmpbreakop:
  4014.         case jmpgotoop:
  4015.         case ljmptrueop:
  4016.         case jmptrueop:
  4017.         case ljmpfalseop:
  4018.         case jmpfalseop:
  4019.         case funcstartop:
  4020.         case funcstopop:
  4021.         {/* start with max sized jmps, they will be shortened later */
  4022.         void *fixjmp;
  4023.             if(opcode == jmptrueop)
  4024.             {
  4025.                 obuf[0] = JMPT|J4;
  4026.                 --iv->stackdepth;
  4027.             }
  4028.             else if(opcode == ljmptrueop)
  4029.             {
  4030.                 obuf[0] = LJMPT|J4;
  4031.             }
  4032.             else if(opcode == jmpfalseop)
  4033.             {
  4034.                 obuf[0] = JMPF|J4;
  4035.                 --iv->stackdepth;
  4036.             }
  4037.             else if(opcode == ljmpfalseop)
  4038.             {
  4039.                 obuf[0] = LJMPF|J4;
  4040.             }
  4041.             else if(opcode == funcstartop || opcode == funcstopop)
  4042.             {
  4043.                 obuf[0] = LOCATE|J4;
  4044.             }
  4045.             else
  4046.                 obuf[0] = JMP|J4;
  4047.             fixjmp = write_obuf(iv, obuf, 5);
  4048.             addto_jmplist(iv, p, fixjmp);
  4049.             break;
  4050.         }
  4051.         case clrdatop:
  4052.         {
  4053.             addr_toevalstack(iv, d);
  4054.             data_toevalstack(iv, l, 0);
  4055.             obuf[0] = XTD;
  4056.             obuf[1] = CLRDAT;
  4057.             write_obuf(iv, obuf, 2);
  4058.             iv->stackdepth -= 2;
  4059.             break;
  4060.         }
  4061.         case getbitfieldop:
  4062.         {
  4063.             data_toevalstack(iv, r, d);
  4064.             obuf[0] = XTD;
  4065.             obuf[1] = GETBITFIELD;
  4066.             obuf[2] = l->data->uchr.d[0];    /* shift */
  4067.             obuf[3] = l->data->uchr.d[1];    /* size */
  4068.             obuf[4] = l->data->uchr.d[2];    /* sign */
  4069.             write_obuf(iv, obuf, 5);
  4070.             break;
  4071.         }
  4072.         case putbitfieldop:
  4073.         {
  4074.             if(!(d->atype & (A_TEMP|A_RET)))
  4075.             {/* destination address to stack */
  4076.                 addr_toevalstack(iv, d);
  4077.  
  4078.                 if(!(r->atype & (A_TEMP|A_RET)))
  4079.                 {
  4080.                     data_toevalstack(iv, r, d);
  4081.                 }
  4082.                 else
  4083.                 {/* data was on stack */
  4084.                     obuf[0] = SWAP;
  4085.                     write_obuf(iv, obuf, 1);
  4086.                 }
  4087.             }
  4088.             else
  4089.             {/* destination address is on stack */
  4090.                 data_toevalstack(iv, r, d);
  4091.             }
  4092.             if(check_assignment_conversion(d, r))
  4093.                 do_conversion(iv, d, r);
  4094.  
  4095.             obuf[0] = XTD;
  4096.             obuf[1] = PUTBITFIELD;
  4097.             obuf[2] = l->data->uchr.d[0];    /* shift */
  4098.             obuf[3] = l->data->uchr.d[1];    /* size */
  4099.             obuf[4] = d->size;
  4100.             write_obuf(iv, obuf, 5);
  4101.             iv->stackdepth -= 2;
  4102.             break;
  4103.         }
  4104.         case retstructop:
  4105.         {
  4106.             addr_toevalstack(iv, d);
  4107.             obuf[0] = XTD;
  4108.             obuf[1] = RETSTRUCT;
  4109.             *((unsigned short*)&obuf[2]) = l->data->ulng.d[0] >>2;    /* size */
  4110.             *((unsigned short*)&obuf[4]) = l->data->ulng.d[1] >>2;    /* offset */
  4111.             write_obuf(iv, obuf, 6);
  4112.             break;
  4113.         }
  4114.         case retdataop:
  4115.         case retvoidop:
  4116.         {
  4117.             obuf[0] = RET;
  4118.             write_obuf(iv, obuf, 1);
  4119.             break;
  4120.         }
  4121.         case callfuncop:
  4122.         {
  4123.             obuf[0] = XTD;
  4124.             obuf[1] = CALLSETUP;
  4125.             *((long*)&obuf[2]) = l->data->ulng.d[0];    /* argsize */
  4126.             write_obuf(iv, obuf, 6);
  4127.             break;
  4128.         }
  4129.         case switchop:
  4130.         {
  4131.         void *fixjmp;
  4132.             obuf[0] = XTD;
  4133.             obuf[1] = SWITCH;
  4134.             *((short*)(&obuf[2])) = d->data->ulng.d[1];    /* swnode */
  4135.             write_obuf(iv, obuf, 4);
  4136.  
  4137.             /* Default jmp */
  4138.             obuf[0] = JMP|D4;
  4139.             fixjmp = write_obuf(iv, obuf, 5);
  4140.             addto_jmplist(iv, p, fixjmp);
  4141.             --iv->stackdepth;
  4142.             break;
  4143.         }
  4144.         case compsavop:
  4145.         case totmpop:
  4146.         {
  4147.             data_toevalstack(iv, l, d);
  4148.             break;
  4149.         }
  4150.         case argop:
  4151.         {
  4152.         unsigned char op =    arg_toevalstack(iv, l, d->ARGSIZE);
  4153.             if(!(iv->in_builtin))
  4154.             {
  4155.                 load_val(iv, d->ARGOFS);
  4156.                 load_val(iv, d->ARGSIZE);
  4157.                 obuf[0] = op;
  4158.                 write_obuf(iv, obuf, 1);
  4159.                 iv->stackdepth -= 3;
  4160.             }
  4161.             break;
  4162.         }
  4163.         default:
  4164.         {
  4165. #if 0
  4166.             obuf[0] = NOP;
  4167.             write_obuf(iv, obuf, 1);
  4168. #endif
  4169.             break;
  4170.         }
  4171.     }/* END: switch(opcode) */
  4172.     if(iv->stackdepth > iv->maxdepth)
  4173.         iv->maxdepth = iv->stackdepth;
  4174.     if(iv->stackdepth < iv->mindepth)
  4175.         iv->mindepth = iv->stackdepth;
  4176.  
  4177.     return iv->cod_usedtail;
  4178. }/* END: gen_inst() */
  4179.  
  4180. static void
  4181. set_op(PND pnd, PopA ptr, unsigned char otype)
  4182. {
  4183. unsigned char optype  = otype & 0xe0;
  4184. unsigned char isize = otype & 0x1f;
  4185.  
  4186.     pnd->data = (DATUM *)ptr;
  4187.     if(optype <= OPIMMED4)
  4188.     {
  4189.         pnd->dtype = optype >> 5;
  4190.         pnd->quals = Q_CONST;
  4191.         pnd->size = isize;
  4192.         pnd->opsize = get_size(pnd->dtype, isize);
  4193.         pnd->atype = A_IMMED;
  4194.     } /* END: OPIMMED */
  4195.     else
  4196.     {
  4197.     unsigned short type = GS(ptr->dtype);
  4198.         pnd->dtype = type & 0xff;
  4199.         pnd->quals = (type >> 8) & 0xff;
  4200.         pnd->size = GL(ptr->dsize);
  4201.         pnd->opsize = get_size(pnd->dtype, pnd->size);
  4202.         pnd->atype = GS(ptr->atype);
  4203.  
  4204.         if(optype == OPAUTO)
  4205.         {
  4206.             if(GL(ptr->pofs) >= 0)
  4207.             {
  4208.                 pnd->atype &= ~A_AUTO;
  4209.                 pnd->atype |= A_PARAM;
  4210.             }
  4211.         }
  4212.     }
  4213. }/* END: set_op() */
  4214. static void *
  4215. decode_anf(Piv iv, unsigned char *p)
  4216. {
  4217. void *dptr;
  4218. void *lptr;
  4219. void *rptr;
  4220.  
  4221.     if(iv->debug >= '4')
  4222.         cfeprintf("DECODE inst(%u) `%s'\n", *p, oxgenops[*p]);
  4223.  
  4224.     if(*p == 0)
  4225.         return POP->next;
  4226.  
  4227.  
  4228.     iv->ob->p = p;
  4229.     switch(*p)
  4230.     {
  4231.         case regainop:
  4232.         case grabop:
  4233.         case retvoidop:
  4234.         {/* 0 address mode */
  4235.             break;
  4236.         }
  4237.         case jmploopop:
  4238.         case jmpcontinueop:
  4239.         case jmpbreakop:
  4240.         case jmpgotoop:
  4241.         case ljmptrueop:
  4242.         case jmptrueop:
  4243.         case ljmpfalseop:
  4244.         case jmpfalseop:
  4245.         case funcstartop:
  4246.         case funcstopop:
  4247.         case retdataop:
  4248.         {/* 1 address mode */
  4249.             dptr = (p+8);
  4250.             set_op(&iv->ob->d, dptr, p[1]);
  4251.             break;
  4252.         }
  4253.         case getvalop:
  4254.         case derefop:
  4255.         case assignop:
  4256.         case duptmpop:
  4257.         case truthop:
  4258.         case aliastmpop:
  4259.         case negop:
  4260.         case complop:
  4261.         case notop:
  4262.         case copyop:
  4263.         case castop:
  4264.         case clrdatop:
  4265.         case compsavop:
  4266.         case totmpop:
  4267.         case retstructop:
  4268.         case switchop:
  4269.         case argop:
  4270.         {/* 2 address mode */
  4271.             dptr = (p+8);
  4272.             lptr = (((char*)dptr) + (p[1] & 0x1f));
  4273.             set_op(&iv->ob->d, dptr, p[1]);
  4274.             set_op(&iv->ob->l, lptr, p[2]);
  4275.             break;
  4276.         }
  4277.         case plusop:
  4278.         case minusop:
  4279.         case mulop:
  4280.         case divop:
  4281.         case modop:
  4282.         case orop:
  4283.         case xorop:
  4284.         case andop:
  4285.         case eqop:
  4286.         case neqop:
  4287.         case ltop:
  4288.         case gtop:
  4289.         case leop:
  4290.         case geop:
  4291.         case lshop:
  4292.         case rshop:
  4293.         case getbitfieldop:
  4294.         case callfuncop:
  4295.         case putbitfieldop:
  4296.         {/* 3 address mode */
  4297.             dptr = (p+8);
  4298.             lptr = (((char*)dptr) + (p[1] & 0x1f));
  4299.             rptr = (((char*)lptr) + (p[2] & 0x1f));
  4300.             set_op(&iv->ob->d, dptr, p[1]);
  4301.             set_op(&iv->ob->l, lptr, p[2]);
  4302.             set_op(&iv->ob->r, rptr, p[3]);
  4303.             break;
  4304.         }
  4305.         default:
  4306.         {
  4307.             return POP->next;
  4308.         }
  4309.     }/* END: switch(*p) */
  4310.  
  4311.     /* Save pointers to the machine instructions generated by this ANF code */
  4312.     iv->ob->startinst = iv->cod;
  4313.     iv->ob->endinst = gen_inst(iv, iv->ob);
  4314.  
  4315.     if(iv->cod != iv->ob->startinst)
  4316.     {/* instructions were generated */
  4317.         link_ob(iv);
  4318.     }
  4319.     return POP->next;
  4320. }/* END: decode_anf() */
  4321.  
  4322. static void *
  4323. skip_bracket(unsigned char *p)
  4324. {
  4325. long opcode = *p;
  4326. int opcount = 0;
  4327.  
  4328.     for(;;)
  4329.     {
  4330.         if(*p == opcode)
  4331.             ++opcount;
  4332.         else if(*p == endop && GL(POP->data) == opcode)
  4333.         {
  4334.             if(--opcount == 0) {
  4335.                 return p;
  4336.             }
  4337.         }
  4338.         else if(*p == endfileop || *p == endallop)
  4339.         {
  4340.             PERROR(pName ": Malformed input file3=%u=%p",*p, p);
  4341.         }
  4342.         p = POP->next;
  4343.     }
  4344.     return 0;
  4345. }
  4346. static void *
  4347. do_strelem(Piv iv, unsigned char *p)
  4348. {
  4349. void *sp = p;
  4350. unsigned char *q = skip_bracket(p);
  4351. void *elem[20];    
  4352. void *elemq[20];
  4353. int elemcnt, i;
  4354.  
  4355.     elemcnt = 0;
  4356.     p = POP->next;
  4357.     while(p < q)
  4358.     {/* Pick up nested elements */
  4359.     unsigned char opcode = *p;
  4360.         if(opcode == strelemop || opcode == ptrelemop || opcode == arrayelemop)
  4361.         {
  4362.         void *qq = skip_bracket(p);
  4363.             elem[elemcnt] = p;
  4364.             elemq[elemcnt++] = qq;
  4365.             p = qq;
  4366.         }
  4367.         p = POP->next;
  4368.     }
  4369.  
  4370.     /* Dump the nested elements in the order encountered */
  4371.     for(i = 0; i < elemcnt; ++i)
  4372.     {
  4373.         do_something(iv, elem[i]);
  4374.     } 
  4375.  
  4376.     /* Dump the remainder of bracket */
  4377.     p = sp;
  4378.     elemcnt = 0;
  4379.     p = POP->next;
  4380.      while(p < q)
  4381.     {
  4382.     unsigned char opcode = *p;
  4383.         if(opcode == strelemop || opcode == ptrelemop || opcode == arrayelemop)
  4384.             p = ((Pop)elemq[elemcnt++])->next;
  4385.         else
  4386.             p = do_something(iv, p);
  4387.     }
  4388.     return ((Pop)q)->next;
  4389. }
  4390. static void *
  4391. do_ptrelem(Piv iv, unsigned char *p)
  4392. {
  4393. void *sp = p;
  4394. unsigned char *q = skip_bracket(p);
  4395. void *elem[20];    
  4396. void *elemq[20];
  4397. int elemcnt, i;
  4398.  
  4399.     elemcnt = 0;
  4400.     p = POP->next;
  4401.     while(p < q)
  4402.     {/* Pick up nested elements */
  4403.     unsigned char opcode = *p;
  4404.         if(opcode == arrayelemop || opcode == ptrelemop || opcode == strelemop)
  4405.         {
  4406.         void *qq = skip_bracket(p);
  4407.             elem[elemcnt] = p;
  4408.             elemq[elemcnt++] = qq;
  4409.             p = qq;
  4410.         }
  4411.         p = POP->next;
  4412.     }
  4413.  
  4414.     /* Dump the nested elements in the order encountered */
  4415.     for(i = 0; i < elemcnt; ++i)
  4416.     {
  4417.         do_something(iv, elem[i]);
  4418.     } 
  4419.  
  4420.     /* Dump the remainder of bracket */
  4421.     p = sp;
  4422.     elemcnt = 0;
  4423.     p = POP->next;
  4424.      while(p < q)
  4425.     {
  4426.     unsigned char opcode = *p;
  4427.         if(opcode == arrayelemop || opcode == strelemop || opcode == ptrelemop)
  4428.             p = ((Pop)elemq[elemcnt++])->next;
  4429.         else
  4430.             p = do_something(iv, p);
  4431.     }
  4432.     return ((Pop)q)->next;
  4433. }
  4434. static void *
  4435. do_arrayelem(Piv iv, unsigned char *p)
  4436. {/* Arrange output for the stack machine */
  4437. void *sp = p;
  4438. unsigned char *q = skip_bracket(p);
  4439. void *sadims[10];
  4440. void *sadimsq[10];
  4441. void *spdims[10];    
  4442. void *spdimsq[10];
  4443. void *elem[20];    
  4444. void *elemq[20];
  4445. int sacnt, spcnt, elemcnt, i;
  4446.  
  4447.     /* Scan over the bracket and pick up the dimension computations */
  4448.     elemcnt= sacnt = spcnt = 0;
  4449.     p = POP->next;
  4450.     while(p < q)
  4451.     {
  4452.     unsigned char opcode = *p;
  4453.         if(opcode == arraydimsop)
  4454.         {
  4455.         void *qq = skip_bracket(p);
  4456.             sadims[sacnt] = p;
  4457.             sadimsq[sacnt++] = qq;
  4458.             p = qq;
  4459.         }
  4460.         else if(opcode == ptrdimsop)
  4461.         {
  4462.         void *qq = skip_bracket(p);
  4463.             spdims[spcnt] = p;
  4464.             spdimsq[spcnt++] = qq;
  4465.             p = qq;
  4466.         }
  4467.         else if(opcode==arrayelemop || opcode==ptrelemop || opcode==strelemop)
  4468.         {
  4469.         void *qq = skip_bracket(p);
  4470.             elem[elemcnt] = p;
  4471.             elemq[elemcnt++] = qq;
  4472.             p = qq;
  4473.         }
  4474.         p = POP->next;
  4475.     }
  4476.  
  4477.     /* Dump the dimension computations in stack order */
  4478.     for(i = spcnt-1; i >= 0; --i)
  4479.     {
  4480.         do_bracket(iv, spdims[i], spdimsq[i]);
  4481.     }
  4482.     for(i = sacnt-1; i >= 0; --i)
  4483.     {
  4484.         do_bracket(iv, sadims[i], sadimsq[i]);
  4485.     }
  4486.  
  4487.     /* Dump the nested elements in the order encountered */
  4488.     for(i = 0; i < elemcnt; ++i)
  4489.     {
  4490.         do_something(iv, elem[i]);
  4491.     } 
  4492.  
  4493.     /* Dump the remainder of the bracket */
  4494.     p = sp;
  4495.     elemcnt = sacnt = spcnt = 0;
  4496.     p = POP->next;
  4497.      while(p < q)
  4498.     {
  4499.     unsigned char opcode = *p;
  4500.         if(opcode == arraydimsop)
  4501.             p = ((Pop)sadimsq[sacnt++])->next;
  4502.         else if(opcode == ptrdimsop)
  4503.             p = ((Pop)spdimsq[spcnt++])->next;
  4504.         else if(opcode==arrayelemop || opcode==ptrelemop || opcode==strelemop)
  4505.             p = ((Pop)elemq[elemcnt++])->next;
  4506.         else
  4507.             p = do_something(iv, p);
  4508.     }
  4509.     return ((Pop)q)->next;
  4510. }
  4511. static int
  4512. funcret_used(PopT op, unsigned char *p)
  4513. {
  4514. long tmpnum;
  4515.     if(        op->dtype == 0
  4516.         &&    op->dsize == 0)
  4517.     {
  4518.         return 1;                        /* void function */
  4519.     }
  4520.     tmpnum = op->tmpnum;
  4521.  
  4522.     while(*p != funcexitop)
  4523.     {
  4524.         if(*p && *p <= (unsigned char)100)
  4525.         {
  4526.         unsigned char *qp = p+8;
  4527.             if(*p == callfuncop)
  4528.             {/* function using same temp */
  4529.                 if(GL(((PopT)qp)->tmpnum) == tmpnum)
  4530.                 {
  4531.                     return 1;        /* not used earlier */
  4532.                 }
  4533.             }
  4534.             else
  4535.             {
  4536.                 if((p[1]&0xe0) == OPRET)
  4537.                 {
  4538.                      if(GL(((PopT)qp)->tmpnum) == tmpnum)
  4539.                     {
  4540.                         return 0;            /* ret used */
  4541.                     }
  4542.                 }                
  4543.                 qp += p[1]&0x1f;
  4544.                 if((p[2]&0xe0) == OPRET)
  4545.                 {
  4546.                     if(GL(((PopT)qp)->tmpnum) == tmpnum)
  4547.                     {
  4548.                         return 0;            /* ret used */
  4549.                     }
  4550.                 }
  4551.                 qp += p[2]&0x1f;
  4552.                 if((p[3]&0xe0) == OPRET)
  4553.                 {
  4554.                     if(GL(((PopT)qp)->tmpnum) == tmpnum)
  4555.                     {
  4556.                         return 0;            /* ret used */
  4557.                     }
  4558.                 }
  4559.             }
  4560.         }
  4561.         p = POP->next;
  4562.     }
  4563.     return 1;                    /* ret not used */
  4564. }
  4565. static unsigned char
  4566. check_for_builtin(Piv iv, unsigned char *p, int flag)
  4567. {
  4568. short symnum;
  4569. long key[2];
  4570. unsigned char *result;
  4571.  
  4572.     if(flag == 0)
  4573.     {
  4574.         symnum = GS(((PopA)(p+20))->symnum);
  4575.     }
  4576.     else if(flag == 1)
  4577.     {
  4578.         symnum = GS(((PopI)(p+16))->s.symnum);
  4579.     }
  4580.     else if(flag == 2)
  4581.     {
  4582.         symnum = GS(POPI->s.symnum);
  4583.     }
  4584.     else return 0;
  4585.  
  4586.     key[0] = symnum;
  4587.     key[1] = 0;
  4588.     if(SymFind(iv->builtintbl, key, &result))
  4589.         return *result;
  4590.     return 0;
  4591. }
  4592. static void *
  4593. do_funcall(Piv iv, unsigned char *p)
  4594. {/* Arrange output for the stack machine */
  4595. char obuf[4];
  4596. void *sp = p;
  4597. unsigned char *q = skip_bracket(p);
  4598. int argcnt, i, dump;
  4599. void *argloads[100];
  4600. void *argloadsq[100];
  4601. char *callop = 0;
  4602. unsigned char builtin = 0;
  4603.  
  4604.     /* Scan over the bracket and pick up argument loads */
  4605.     argcnt = 0;
  4606.     p = POP->next;
  4607.     while(p < q)
  4608.     {
  4609.         if(*p == getvalop && callop == 0)
  4610.         {
  4611.             builtin = check_for_builtin(iv, p, 0);
  4612.         }
  4613.         else if(*p == callfuncop)
  4614.             callop = p;
  4615.         else if(*p == argloadop)
  4616.         {
  4617.         void *qq = skip_bracket(p);
  4618.             argloads[argcnt] = p;
  4619.             argloadsq[argcnt++] = qq;
  4620.             p = qq;
  4621.         }
  4622.         p = POP->next;
  4623.     }
  4624.  
  4625.     /* Check out whether the function return is used */
  4626.     dump = funcret_used((PopT)(callop+8), q);
  4627.  
  4628.     /* Calling an interpreter builtin ?? */
  4629.     if(builtin)
  4630.     {
  4631.         iv->in_builtin = 1;
  4632.  
  4633.         /* Dump the argument loads in order */
  4634.         /* The ARG instruction will be suppressed because iv->in_builtin > 0 */
  4635.         for(i = 0; i < argcnt; ++i)
  4636.         {
  4637.             do_bracket(iv, argloads[i], argloadsq[i]);
  4638.         }
  4639.         iv->stackdepth -= argcnt;
  4640.  
  4641.         /* Generate the BUILTIN instruction */
  4642.         i = 3;
  4643.         ++iv->stackdepth;
  4644.         obuf[0] = XTD;
  4645.         obuf[1] = BUILTIN;
  4646.         obuf[2] = builtin;
  4647.         if(dump)
  4648.         {
  4649.             obuf[3] = DUMP;
  4650.             --iv->stackdepth;
  4651.             i = 4;
  4652.         }
  4653.         write_obuf(iv, obuf, i);
  4654.         iv->in_builtin = 0;
  4655.     }
  4656.     else
  4657.     {
  4658.         /* Generate the CALLSETUP instruction */
  4659.         p = sp;
  4660.         p = POP->next;
  4661.         while(p < q)
  4662.         {
  4663.             if(*p == argloadop)
  4664.                 break;
  4665.             else
  4666.                 p = do_something(iv, p);
  4667.         }            
  4668.  
  4669.         /* Dump the argument loads in order */
  4670.         for(i = 0; i < argcnt; ++i)
  4671.         {
  4672.             do_bracket(iv, argloads[i], argloadsq[i]);
  4673.         }
  4674.         /* Generate the CALL instruction */
  4675.         i = 1;
  4676.         obuf[0] = CALL;
  4677.         if(dump)
  4678.         {
  4679.             obuf[1] = DUMP;
  4680.             --iv->stackdepth;
  4681.             i = 2;
  4682.         }
  4683.         write_obuf(iv, obuf, i);
  4684.         if((GL(((Pop)(callop))->data1) & 0xff) == D_STRUCT)
  4685.         {
  4686.             ++iv->has_structret;
  4687.         }
  4688.     }
  4689.     return ((Pop)q)->next;
  4690. }
  4691. static void *
  4692. do_expr(Piv iv, unsigned char *p)
  4693. {
  4694. unsigned char *q = skip_bracket(p);
  4695.  
  4696.     if(iv->debug >= '2')
  4697.         cfeprintf("EXPR inst(%u) `%s'\n", *p, oxgenops[*p]);
  4698.  
  4699.     while(p < q)
  4700.     {
  4701.         switch(*p)
  4702.         {
  4703.             case    arrayelemop:
  4704.                 p = do_arrayelem(iv, p);
  4705.                 break;
  4706.             case    strelemop:
  4707.                 p = do_strelem(iv, p);
  4708.                 break;
  4709.             case    ptrelemop:
  4710.                 p = do_ptrelem(iv, p);
  4711.                 break;
  4712.             case    funcallop:
  4713.                 p = do_funcall(iv, p);
  4714.                 break;
  4715.             case    condop:
  4716.             case    twopathop:
  4717.             case    logicalop:
  4718.             case    binopop:
  4719.             case    argloadop:
  4720.             case    preincrdecop:
  4721.             case    postincrdecop:
  4722.             case    compoundop:
  4723.             case    unopop:
  4724.                 p = POP->next;
  4725.                 break;
  4726.             default:
  4727.                 p = do_something(iv, p);
  4728.                 break;
  4729.         }
  4730.     }
  4731.     return ((Pop)q)->next;
  4732. }
  4733. static void *
  4734. do_expstmt(Piv iv, unsigned char *p)
  4735. {
  4736. unsigned char *q = skip_bracket(p);
  4737.  
  4738.     p = POP->next;
  4739.     while(p < q)
  4740.     {
  4741.       p = do_something(iv, p);
  4742.     }
  4743.     return q;
  4744. }
  4745. static void *
  4746. do_ifstmt(Piv iv, unsigned char *p)
  4747. {
  4748. unsigned char *q = skip_bracket(p);
  4749.  
  4750.     p = POP->next;
  4751.     while(p < q)
  4752.     {
  4753.       p = do_something(iv, p);
  4754.     }
  4755.  
  4756.     return ((Pop)q)->next;
  4757. }
  4758. static void *
  4759. do_ifelsestmt(Piv iv, unsigned char *p)
  4760. {
  4761. unsigned char *q = skip_bracket(p);
  4762.  
  4763.     p = POP->next;
  4764.     while(p < q)
  4765.     {
  4766.       p = do_something(iv, p);
  4767.     }
  4768.  
  4769.     return ((Pop)q)->next;
  4770. }
  4771. static void *
  4772. do_switchstmt(Piv iv, unsigned char *p)
  4773. {
  4774. unsigned char *q = skip_bracket(p);
  4775.  
  4776.     p = POP->next;
  4777.     while(p < q)
  4778.     {
  4779.       p = do_something(iv, p);
  4780.     }
  4781.  
  4782.     return ((Pop)q)->next;
  4783. }
  4784. static void *
  4785. do_whilestmt(Piv iv, unsigned char *p)
  4786. {
  4787. unsigned char *q = skip_bracket(p);
  4788.  
  4789.     p = POP->next;
  4790.     while(p < q)
  4791.     {
  4792.       p = do_something(iv, p);
  4793.     }
  4794.     return ((Pop)q)->next;
  4795. }
  4796. static void *
  4797. do_dostmt(Piv iv, unsigned char *p)
  4798. {
  4799. unsigned char *q = skip_bracket(p);
  4800.  
  4801.     p = POP->next;
  4802.     while(p < q)
  4803.     {
  4804.       p = do_something(iv, p);
  4805.     }
  4806.     return ((Pop)q)->next;
  4807. }
  4808. static void *
  4809. do_forstmt(Piv iv, unsigned char *p)
  4810. {
  4811. unsigned char *q = skip_bracket(p);
  4812.  
  4813.     p = POP->next;
  4814.     while(p < q)
  4815.     {
  4816.       p = do_something(iv, p);
  4817.     }
  4818.     return ((Pop)q)->next;
  4819. }
  4820. static void *
  4821. do_asmstmt(Piv iv, unsigned char *p)
  4822. {
  4823. unsigned char *q = skip_bracket(p);
  4824.  
  4825.     p = POP->next;
  4826.     while(p < q)
  4827.     {
  4828.       p = do_something(iv, p);
  4829.     }
  4830.     return ((Pop)q)->next;
  4831. }
  4832. static void *
  4833. do_initstmt(Piv iv, unsigned char *p)
  4834. {
  4835. unsigned char *q = skip_bracket(p);
  4836.  
  4837.     p = POP->next;
  4838.     while(p < q)
  4839.     {
  4840.       p = do_something(iv, p);
  4841.     }
  4842.     return ((Pop)q)->next;
  4843. }
  4844. static void *
  4845. do_anfblock(Piv iv, unsigned char *p)
  4846. {
  4847. unsigned char *q = skip_bracket(p);
  4848.  
  4849.     p = POP->next;
  4850.     while(p < q)
  4851.     {
  4852.       p = do_something(iv, p);
  4853.     }
  4854.     return ((Pop)q)->next;
  4855. }
  4856. static char *
  4857. nodot(Piv iv, char *name)
  4858. {
  4859. char *cp;
  4860.     if((cp = strchr(name, '.')))
  4861.     {
  4862.     char *newname;
  4863.         newname = Cmalloc(iv->category, strlen(name)+1);
  4864.         strcpy(newname,name);
  4865.         cp = strchr(newname, '.');
  4866.         *cp = 0;
  4867.         return newname;
  4868.     }
  4869.     return name;
  4870. }
  4871. static void
  4872. printfunc(Piv iv, unsigned char *p)
  4873. {
  4874. char *funcname = nodot(iv, iv->symaddr[GS(POPI->funcdef.symnum)]);
  4875.  
  4876.     if(*p == gfuncdefop)
  4877.     {
  4878.         fprintf(iv->outfile, "\n%8.8lx:    .global .function _%s\n",
  4879.             iv->out_offset, funcname);
  4880.     }
  4881.     else if(*p == sfuncdefop)
  4882.     {
  4883.         fprintf(iv->outfile, "\n%8.8lx:    .local .function _%s\n", 
  4884.             iv->out_offset, funcname);
  4885.     }
  4886. }
  4887. static void *
  4888. do_stmt(Piv iv, unsigned char *p)
  4889. {
  4890. void *q;
  4891.  
  4892.     if(iv->debug >= '2')
  4893.         cfeprintf("STMT inst(%u) `%s'\n", *p, oxgenops[*p]);
  4894.  
  4895.     q = POP->next;
  4896.  
  4897.     switch(*p)
  4898.     {
  4899.         case labelop:
  4900.             newlabel_insert(iv, GL( POP->data));
  4901.             break;
  4902.         case gfuncdefop:
  4903.         case sfuncdefop:
  4904.         case funcexitop:
  4905.             PERROR(pName ": Malformed input file1=%u=%p",*p, p);
  4906.         case nestedfuncdefop:
  4907.         {
  4908.             if(iv->listing_wanted)
  4909.             {
  4910.             char obuf[8];
  4911.                 obuf[0] = NFUNC;
  4912.                 *((char**)&obuf[1]) = nodot(iv, iv->symaddr[GS(POPI->funcdef.symnum)]);
  4913.                 write_obuf(iv, obuf, 5);
  4914.             }
  4915.             iv->numnested += 1;
  4916.             break;
  4917.         }
  4918.         case nestedfuncexitop:
  4919.         {
  4920.         char obuf[2];
  4921.             obuf[0] = RET;
  4922.             write_obuf(iv, obuf, 1);
  4923.             break;        
  4924.         }
  4925.         case anfblockop:
  4926.             q = do_anfblock(iv, p);
  4927.             break;
  4928.         case expstmtop:
  4929.             q = do_expstmt(iv, p);
  4930.             break;
  4931.         case ifstmtop:
  4932.             q = do_ifstmt(iv, p);
  4933.             break;
  4934.         case ifelsestmtop:
  4935.             q = do_ifelsestmt(iv, p);
  4936.             break;
  4937.         case switchstmtop:
  4938.             q = do_switchstmt(iv, p);
  4939.             break;
  4940.         case whilestmtop:
  4941.             q = do_whilestmt(iv, p);
  4942.             break;
  4943.         case dostmtop:
  4944.             q = do_dostmt(iv, p);
  4945.             break;
  4946.         case forstmtop:
  4947.             q = do_forstmt(iv, p);
  4948.             break;
  4949.         case asmstmtop:
  4950.             q = do_asmstmt(iv, p);
  4951.             break;
  4952.         case initstmtop:
  4953.             q = do_initstmt(iv, p);
  4954.             break;
  4955.         case lineop:
  4956.             if(iv->debug >= '3')
  4957.             {
  4958.                 cfeprintf("Line=%u depth=%d\n", GL(POP->data), iv->stackdepth);
  4959.             }
  4960.             if(iv->listing_wanted)
  4961.             {
  4962.             char obuf[8];
  4963.                 obuf[0] = LINENO;
  4964.                 *((long*)&obuf[1]) = GL(POP->data);
  4965.                 write_obuf(iv, obuf, 5);
  4966.             }
  4967.             iv->lastline = GL(POP->data);
  4968.             break;
  4969.         default:
  4970.             break;
  4971.     }
  4972.     return q;
  4973. }
  4974. static void *
  4975. do_something(Piv iv, unsigned char *p)
  4976. {
  4977.     if(*p < labelop)
  4978.         return decode_anf(iv, p);
  4979.     else if(*p >= condop && *p < symbop)
  4980.         return do_expr(iv, p);
  4981.     else
  4982.         return do_stmt(iv, p);
  4983. }
  4984. static void
  4985. do_bracket(Piv iv, unsigned char *p, unsigned char *q)
  4986. {
  4987.     p = POP->next;
  4988.     while(p < q)
  4989.     {
  4990.       p = do_something(iv, p);
  4991.     }
  4992. }
  4993. static void *
  4994. dumpa_func(Piv iv, unsigned char *p)
  4995. {
  4996. unsigned char obuf[2];
  4997. Pafile pf;
  4998. unsigned char *pdef = p;
  4999.  
  5000.     iv->ob = new_nodeO(iv);        /* setup first intermediate output node */
  5001.     link_ob(iv);                /* null node never unlinked */
  5002.     iv->first_ob = iv->ob_usedtail;
  5003.  
  5004.     iv->cod = new_nodeC(iv);    /* setup first code node */
  5005.     link_cod(iv);                /* null node never unlinked */
  5006.     iv->first_cod = iv->cod_usedtail;
  5007.  
  5008.     pf = iv->files[iv->filenum];
  5009.     if(iv->listing_wanted)
  5010.     {
  5011.         printfunc(iv, p);
  5012.     }
  5013.     p = POP->next;
  5014.  
  5015.     for(;;)
  5016.     {
  5017.         if(*p == funcexitop)
  5018.         {
  5019.             obuf[0] = RET;
  5020.             write_obuf(iv, obuf, 1);
  5021.             write_funcdata(iv, pdef);
  5022.             break;
  5023.         }
  5024.         else
  5025.             p = do_something(iv, p);
  5026.     }
  5027.     save_extlocs(iv);
  5028.     reset_funcdata(iv);
  5029.     return p;
  5030. }
  5031. static void
  5032. dump_funcs(Piv iv)
  5033. {
  5034. long pad;
  5035. Pafile pf;
  5036. unsigned char *p;
  5037. int i;
  5038.  
  5039. #if 0
  5040. oxcc_debug(__builtin_iv(),0x40040);
  5041. #endif
  5042.  
  5043.     reset_funcdata(iv);
  5044.     for(i = 0; i < iv->numfiles; ++i)
  5045.     {
  5046.         iv->filenum = i;
  5047.         pf = iv->files[i];
  5048.         p = pf->file_p;
  5049.         if(iv->listing_wanted)
  5050.             fprintf(iv->outfile, "\n\nFile:%s:\n\n", pf->symaddr[1]);
  5051.  
  5052.         while(*p != endfileop)
  5053.         {
  5054.             if(*p == labelop)
  5055.             {
  5056.                 newlabel_insert(iv, GL( POP->data ));
  5057.             }
  5058.             else if(*p == lineop)
  5059.             {
  5060.                 iv->lastline = GL(POP->data);
  5061.                 if(iv->debug >= '3')
  5062.                 {
  5063.                     cfeprintf("Line=%u depth=%d\n", iv->lastline, iv->stackdepth);
  5064.                 }
  5065.                 if(iv->listing_wanted)
  5066.                     fprintf(iv->outfile, "Line:%ld:\n", iv->lastline);
  5067.             }
  5068.             else if(*p == gfuncdefop || *p == sfuncdefop)
  5069.             {
  5070.                 p = dumpa_func(iv, p);
  5071.             }
  5072.             else if(*p == anfblockop)
  5073.             {
  5074.                 PERROR(pName ": Sorry, Outer anf blocks not handled.\n");
  5075.             }
  5076.             p = POP->next;;
  5077.         }
  5078.     }
  5079.  
  5080.     /* Start the data area on an 8 byte boundary */
  5081.     if((pad = iv->out_offset & 7))
  5082.         pad = 8-pad;
  5083.     iv->out_offset += pad;
  5084.  
  5085.     if(pad && !iv->listing_wanted)
  5086.         FILEWRITE(padit, pad);
  5087.     iv->header->a_text = iv->out_offset;
  5088. }
  5089. static void
  5090. fix_thunks(Piv iv)
  5091. {
  5092. Pafile pf;
  5093. int i;
  5094. unsigned char **vp;
  5095. unsigned char *p;
  5096.  
  5097.     for(i = 0; i < iv->numfiles; ++i)
  5098.     {
  5099.         iv->filenum = i;
  5100.         pf = iv->files[i];
  5101.         if(SymHead(pf->datatbl))
  5102.         {
  5103.             while(SymNext(pf->datatbl))
  5104.             {
  5105.                 SymValue(pf->datatbl, &vp);
  5106.                 p = vp[1];
  5107.                 if(*p == thunkblockop)
  5108.                 {/* rearrange the FUNCTHUNK */
  5109.                 unsigned short mods;
  5110.                     mods = GS(POP->data7);
  5111.                     mods &= 0xc000;
  5112.                     mods |= GS(POP->data9) & 0x1f1f;
  5113.                     PS( POP->data7 ) = mods;
  5114.                     PS( POP->data9) = get_maxdepth(iv, GS( POP->data2 ));
  5115.                     if(mods & Fextern)
  5116.                     {/* Put symbol number in the offset slot */
  5117.                         PL( POP->data5 ) = final_symnum(iv, GS( POP->data2 ));
  5118.                     }
  5119.                     else
  5120.                     {/* Put function offset in the offset slot */
  5121.                         PL( POP->data5 ) = newlabel_fix(iv, GL( POP->data5 ));
  5122.                     }
  5123.                 }
  5124.             }
  5125.         }
  5126.     }
  5127. }
  5128. static void
  5129. printdata(Piv iv, char *sym, char *msg, void *ptr, int size, 
  5130.     long offset, int locid)
  5131. {
  5132. int x;
  5133.  
  5134.     x = print8(iv, ptr, size, offset, 0);
  5135.     if(locid > 0)
  5136.         fprintf(iv->outfile,"  _%s.%d (%s)\n", sym, locid, msg);
  5137.     else
  5138.         fprintf(iv->outfile,"  _%s (%s)\n", sym, msg);
  5139.     while((size -= x) > 0)
  5140.     {
  5141.         offset += x;
  5142.         ((char*)ptr) += x;
  5143.         x = print8(iv, ptr, size, offset, 1);
  5144.     }
  5145. }
  5146. static void
  5147. printbss(Piv iv, char *sym, int size, int offset, unsigned char prevopcode,
  5148.                                 int locid)
  5149. {
  5150.     if(prevopcode == globssop)
  5151.       fprintf(iv->outfile, "%8.8x:  _%s (BSS %d)\n", offset, sym, size);
  5152.     else
  5153.       fprintf(iv->outfile, "%8.8x:  _%s.%d (bss %d)\n", offset, sym, locid, size);
  5154. }
  5155. static void
  5156. dump_data(Piv iv)
  5157. {
  5158. struct _val {
  5159. unsigned long size;
  5160. unsigned char *p;
  5161. unsigned char *prevp;
  5162. long locid;
  5163. };
  5164. struct _val *val;
  5165. unsigned long *key;
  5166. long datsize = 0;
  5167. long bsssize = 0;
  5168. long curr_offset = iv->out_offset;
  5169. unsigned char opcode, prevopcode = 0;
  5170.  
  5171.     if(SymHead(iv->datatbl))
  5172.     {
  5173.     void *savaddr;
  5174.         if(iv->listing_wanted)
  5175.         {
  5176.             fprintf(iv->outfile, "\n\t.data\n\n");
  5177.             savaddr = iv->symaddr[0];
  5178.             iv->symaddr[0] = "STRING_";
  5179.         }
  5180.         while(SymNext(iv->datatbl))
  5181.         {
  5182.         unsigned char *p;
  5183.         long size;
  5184.         long pad;
  5185.             SymKey(iv->datatbl, &key);
  5186.             SymValue(iv->datatbl, &val);
  5187.             p = val->p;
  5188.             opcode = *p;
  5189.             if(val->prevp)
  5190.                 prevopcode = *(val->prevp);
  5191.             size = val->size;
  5192.             if((pad = size & 3))  /* the interpreter requires 4 byte alignment */
  5193.                 pad = 4-pad;
  5194.  
  5195.             if(        opcode == datablockop
  5196.                 ||    opcode == mallocblockop
  5197.                 ||  opcode == thunkblockop
  5198.                 ||    opcode == stringblockop)
  5199.             {
  5200.                 if(iv->listing_wanted)
  5201.                 {
  5202.                 char *msg;
  5203.  
  5204.                     if(        opcode == datablockop
  5205.                         ||    opcode == stringblockop)
  5206.                     {
  5207.                         if(prevopcode == glodatop)
  5208.                             msg = "DATA";
  5209.                         else
  5210.                         {
  5211.                             msg = "data";
  5212.                         }
  5213.                     }
  5214.                     else if(opcode == mallocblockop)
  5215.                     {
  5216.                         msg = "alloced";
  5217.                     }
  5218.                     else /* thunkblockop */
  5219.                     {
  5220.                         if(prevopcode == extfuncop)
  5221.                         {
  5222.                         long bu;
  5223.                             if((bu = check_for_builtin(iv, p, 2)))
  5224.                             {
  5225.                             Pft ft = (Pft)(p+24);
  5226.                                 ft->funcaddr = bu;
  5227.                                 ft->fmods |= Fbuiltin;
  5228.                                 ft->fmods &= ~Fextern;
  5229.                                 *(val->prevp) = 0xff;
  5230.                                 msg = "thunk";
  5231.                             }
  5232.                             else
  5233.                                 msg = "EXTHUNK";
  5234.                         }
  5235.                         else if(prevopcode == glofuncop)
  5236.                             msg = "THUNK";
  5237.                         else
  5238.                         {
  5239.                             msg = "thunk";
  5240.                         }
  5241.                     }
  5242.                     printdata(iv, iv->symaddr[GS(POP->data2)], 
  5243.                             msg, p+24, size, GL(POP->data1), val->locid);
  5244.                 }
  5245.                 else
  5246.                 {
  5247.                     if(opcode == thunkblockop)
  5248.                     {
  5249.                         if(prevopcode == extfuncop)
  5250.                         {
  5251.                         long bu;
  5252.                             if((bu = check_for_builtin(iv, p, 2)))
  5253.                             {
  5254.                             Pft ft = (Pft)(p+24);
  5255.                                 ft->funcaddr = bu;
  5256.                                 ft->fmods |= Fbuiltin;
  5257.                                 ft->fmods &= ~Fextern;
  5258.                                 *(val->prevp) = 0xff;
  5259.                             }
  5260.                         }
  5261.                     }
  5262.                     FILEWRITE(p+24, size);
  5263.                     if(pad > 0)
  5264.                         FILEWRITE(padit, pad);
  5265.                 }
  5266.                 datsize += size+pad;
  5267.                 iv->out_offset += size+pad;
  5268.             }
  5269.             else if(opcode == bssblockop)
  5270.             {
  5271.                 if(iv->listing_wanted)
  5272.                 {
  5273.                     printbss(iv, iv->symaddr[GS(POP->data2)], size, 
  5274.                         GL(POP->data1), prevopcode, val->locid);
  5275.                 }
  5276.                 bsssize += size+pad;
  5277.             }
  5278.         }
  5279.         if(iv->listing_wanted)
  5280.         {
  5281.             iv->symaddr[0] = savaddr;
  5282.         }
  5283.     }
  5284.     iv->header->a_data = iv->out_offset - curr_offset;
  5285.     if(datsize != iv->header->a_data)
  5286.         PERROR(pName,":Syserr: data size incorrect (%d)!=(%d)\n",
  5287.             datsize, iv->header->a_data);
  5288. }
  5289. static void
  5290. dump_bss(Piv iv)
  5291. {
  5292.     iv->header->a_bss = iv->total_size - iv->bss_offset;
  5293. }
  5294. static void
  5295. make_final_string_pack(Piv iv)
  5296. {
  5297. long strsize = 0;
  5298. int strcnt = 0;
  5299. char *pack;
  5300. char *cp;
  5301. struct {
  5302.     char *str;
  5303.     long symnum;
  5304.     long symofs;
  5305. } *val;
  5306.  
  5307.     if(SymHead(iv->finalsymtbl))
  5308.     {
  5309.         while(SymNext(iv->finalsymtbl))
  5310.         {
  5311.             SymValue(iv->finalsymtbl, &val);
  5312.             ++strcnt;
  5313.             val->symofs = strsize+4;
  5314.             strsize += strlen(val->str)+2;
  5315.         }
  5316.     }
  5317.     pack = Ccalloc(iv->category, 1, strsize+4);
  5318.     *((long*)pack) = strsize + 4;
  5319.     cp = pack + 4;
  5320.     if(SymHead(iv->finalsymtbl))
  5321.     {
  5322.         while(SymNext(iv->finalsymtbl))
  5323.         {
  5324.             SymValue(iv->finalsymtbl, &val);
  5325.             *cp++ = '_';
  5326.             strcpy(cp, val->str);
  5327.             cp += strlen(val->str)+1;
  5328.         }
  5329.     }
  5330.     iv->finalstringpack = pack;
  5331.     iv->finalpacksize = strsize+4;
  5332. }
  5333. static unsigned char *
  5334. dump_switch(Piv iv, unsigned char *p, int filenum)
  5335. {
  5336. struct nlist nl;
  5337. unsigned char *q = skip_bracket(p);
  5338.  
  5339.     nl.n_type = N_SWTAB;
  5340.     nl.n_other = 0;
  5341.     while(p < q)
  5342.     {
  5343.         if(*p == switchidop)
  5344.         {
  5345.             nl.n_desc = GS(POP->data1);    /* id */
  5346.         }
  5347.         else if(*p == casevalop)
  5348.         {
  5349.         long key[2];
  5350.         long *result;
  5351.             nl.n_un.n_strx = GL(POP->data1);    /* value */
  5352.             key[0] = GL(POP->data); /* label number */
  5353.             key[1] = filenum;
  5354.             if(SymFind(iv->newlabeltbl, &key, &result))
  5355.             {
  5356.                 nl.n_value = *result;            /* offset */
  5357.             }
  5358.             else
  5359.             {
  5360.                 PERROR(pName ":Syserr: case label not found\n");
  5361.             }
  5362.             if(iv->listing_wanted)
  5363.             {
  5364.                 /* print nothing */
  5365.             }
  5366.             else
  5367.             {
  5368.                 FILEWRITE(&nl, sizeof(struct nlist));
  5369.                 iv->out_offset += sizeof(struct nlist);
  5370.             }
  5371.         }
  5372.         p = POP->next;
  5373.     }
  5374.     return ((Pop)q)->next;
  5375. }
  5376. static void
  5377. dump_symbols(Piv iv)
  5378. {
  5379. long curr_offset = iv->out_offset;
  5380. int i;
  5381. struct nlist nl;
  5382.  
  5383.      nl.n_other = 0;
  5384.     nl.n_desc = 0;
  5385.  
  5386.     make_final_string_pack(iv);
  5387.  
  5388.     if(SymHead(iv->gbltbl))
  5389.     {
  5390.         if(iv->listing_wanted)
  5391.         {
  5392.             fprintf(iv->outfile, "\n\t.symbols\n\n");
  5393.         }
  5394.         while(SymNext(iv->gbltbl))
  5395.         {
  5396.         struct _gloval *valp;
  5397.         unsigned char opcode;
  5398.  
  5399.             SymValue(iv->gbltbl, &valp);
  5400.             if((opcode = *(valp->p)))
  5401.             {
  5402.             PopI pp;
  5403.                 pp = (PopI) (((Pop)(valp->p))->next+8);
  5404.                 nl.n_un.n_strx = final_strofs(iv, valp->symname);
  5405.                 if(opcode == glodatop)
  5406.                 {
  5407.                     nl.n_type = N_DATA|N_EXT;
  5408.                     nl.n_value = pp->s.offset + iv->header->a_text;
  5409.                 }
  5410.                 else if(opcode == globssop)
  5411.                 {
  5412.                     nl.n_type = N_BSS|N_EXT;
  5413.                     nl.n_value = pp->s.offset + iv->header->a_text;
  5414.                 }
  5415.                 else if(opcode == glofuncop || opcode == extfuncop)
  5416.                 {/* The symbol is really a thunk in the data section */
  5417.                     nl.n_type = N_NDC|N_EXT;
  5418.                     nl.n_value = pp->s.offset;
  5419.                 }
  5420.                 else if(opcode == extvarop)
  5421.                 {
  5422.                     nl.n_type = N_UNDF|N_EXT;
  5423.                     nl.n_value = 0;
  5424.                 }
  5425.                 else if(opcode == 0xff)
  5426.                 {/* Builtin thunk */
  5427.                     continue;
  5428.                 }
  5429.                 if(iv->listing_wanted)
  5430.                 {
  5431.                 int symval = nl.n_value;
  5432.                     if(nl.n_type & (N_DATA|N_BSS))
  5433.                         symval -= iv->header->a_text;
  5434.                     fprintf(iv->outfile, "%8.8x:  _%s\n", symval, valp->symname);
  5435.                 }
  5436.                 else
  5437.                 {
  5438.                     FILEWRITE(&nl, sizeof(struct nlist));
  5439.                 }
  5440.                 iv->out_offset += sizeof(struct nlist);
  5441.             }
  5442.         }
  5443.     }
  5444.     /* DUMP SWITCH TABLE INFO */
  5445.     for(i = 0; i < iv->numfiles; ++i)
  5446.     {
  5447.     Pafile pf = iv->files[i];
  5448.     unsigned char *p;
  5449.         if((p = pf->switch_p))
  5450.         {
  5451.             while(*p != endfileop)
  5452.             {
  5453.                 if(*p == switchidop)
  5454.                 {
  5455.                     p = dump_switch(iv, p, i);
  5456.                 }
  5457.                 else p = POP->next;
  5458.             }
  5459.         }
  5460.     }
  5461.     iv->header->a_syms = iv->out_offset - curr_offset;;
  5462. }/* END: dump_symbols() */
  5463.  
  5464. static void
  5465. dump_text_relocs(Piv iv)
  5466. {
  5467. long curr_offset = iv->out_offset;
  5468. PEL pel = iv->finextbufstart;
  5469. struct relocation_info r;
  5470.  
  5471.     r.r_pcrel = 0;
  5472.     r.r_extern = 1;
  5473.     r.r_length = 2;
  5474.     r.r_pad = 0;
  5475.     if(pel && iv->listing_wanted)
  5476.     {
  5477.         fprintf(iv->outfile,"\n\t.textrels\n\n");
  5478.     }
  5479.     while(pel)
  5480.     {
  5481.         r.r_symbolnum = final_symnum(iv, pel->symnum);
  5482.         r.r_address = pel->spot;
  5483.         if(pel->symnum > 0)
  5484.         {
  5485.             if(iv->listing_wanted)
  5486.             {
  5487.                 fprintf(iv->outfile,"%8.8lx: .extern _%s\n", 
  5488.                     r.r_address, iv->symaddr[pel->symnum]);
  5489.             }
  5490.             else
  5491.             {
  5492.                 FILEWRITE(&r, sizeof(struct relocation_info));
  5493.             }
  5494.             iv->out_offset += sizeof(struct relocation_info);
  5495.         }
  5496.         pel = pel->next;
  5497.     }
  5498.     iv->header->a_trsize = iv->out_offset - curr_offset;
  5499.  
  5500. }/* END: dump_text_relocs() */
  5501.  
  5502. static void
  5503. dump_data_relocs(Piv iv)
  5504. {
  5505. long curr_offset = iv->out_offset;
  5506. struct relocation_info r;
  5507.  
  5508.     r.r_pcrel = 0;
  5509.     r.r_length = 2;
  5510.     r.r_pad = 0;
  5511.     if(SymHead(iv->reloctbl))
  5512.     {
  5513.         if(iv->listing_wanted)
  5514.         {
  5515.             fprintf(iv->outfile,"\n\t.datarels\n\n");
  5516.         }
  5517.         while(SymNext(iv->reloctbl))
  5518.         {
  5519.         struct _rkey *kp;
  5520.         struct _rval *vp;
  5521.         unsigned char *p;
  5522.         int symnum;
  5523.           SymKey(iv->reloctbl, &kp);
  5524.           SymValue(iv->reloctbl, &vp);
  5525.           p = vp->p;        /* pointer to relocop in input buffer */
  5526.           symnum = GS(POPI->reloc.rsym);
  5527.  
  5528.           if(kp->rsize == 1)
  5529.               r.r_length = 0;
  5530.           else if(kp->rsize == 2)
  5531.               r.r_length = 1;
  5532.           else if(kp->rsize == 4)
  5533.               r.r_length = 2;
  5534.           else PERROR(pName ":error: reloc size too large\n");
  5535.  
  5536.           r.r_address = GL( POPI->reloc.spot );
  5537.           if(*p == extlocop)
  5538.           {/* External variable */
  5539.             r.r_extern = 1;
  5540.             r.r_symbolnum = final_symnum(iv, symnum);
  5541.           }
  5542.           else if(*p)
  5543.           {
  5544.             r.r_extern = 0;
  5545.             r.r_symbolnum = N_DATA;
  5546.           }
  5547.           if(*p)
  5548.           {
  5549.               if(iv->listing_wanted)
  5550.               {
  5551.                 if(r.r_extern)
  5552.                 {
  5553.                     if(vp->rsym <= 0)
  5554.                     {/* an absolute value */
  5555.                         continue;
  5556.                     }
  5557.                     fprintf(iv->outfile,"%8.8lx: .extern _%s\n",
  5558.                         r.r_address, iv->symaddr[symnum]);
  5559.                 }
  5560.                 else
  5561.                 {
  5562.                     fprintf(iv->outfile, "%8.8lx: .segrel\n", r.r_address);
  5563.                 }
  5564.               }
  5565.               else
  5566.               {
  5567.                   if(r.r_extern && vp->rsym <= 0)
  5568.                   {/* an absolute value */
  5569.                     continue;
  5570.                   }
  5571.                   FILEWRITE(&r, sizeof(struct relocation_info));
  5572.               }
  5573.               iv->out_offset += sizeof(struct relocation_info);
  5574.             }
  5575.         }
  5576.     }
  5577.     iv->header->a_drsize = iv->out_offset - curr_offset;
  5578.  
  5579. }/* END: dump_data_relocs() */
  5580.  
  5581. static void
  5582. prepare_data_relocs(Piv iv)
  5583. {
  5584.     if(SymHead(iv->reloctbl))
  5585.     {
  5586.         while(SymNext(iv->reloctbl))
  5587.         {
  5588.         struct _rkey *kp;
  5589.         struct _rval *vp;
  5590.         unsigned char *p;
  5591.           SymKey(iv->reloctbl, &kp);
  5592.           SymValue(iv->reloctbl, &vp);
  5593.           p = vp->p;        /* pointer to relocop in input buffer */
  5594.  
  5595.           if(*p == extlocop)
  5596.           {/* External variable */
  5597.           }
  5598.           else if(*p)
  5599.           {/* a.out format requires a flat address space for relocations */
  5600.             if(kp->rsize == 1)
  5601.             {
  5602.                 *((char*)vp->base) += iv->header->a_text;
  5603.             }
  5604.             else if(kp->rsize == 2)
  5605.             {
  5606.                 *((short*)vp->base) += iv->header->a_text;
  5607.             }
  5608.             else if(kp->rsize == 4)
  5609.             {
  5610.                 *(vp->base) += iv->header->a_text;
  5611.             }
  5612.             else PERROR(pName ":error: reloc size too large\n");
  5613.  
  5614.           }
  5615.         }
  5616.     }
  5617. }/* END: prepare_data_relocs() */
  5618.  
  5619. static void
  5620. dump_symbol_strings(Piv iv)
  5621. {
  5622.     if(iv->listing_wanted)
  5623.     {
  5624.         /* print nothing */
  5625.     }
  5626.     else
  5627.     {
  5628.         FILEWRITE(iv->finalstringpack, iv->finalpacksize);
  5629.     }
  5630. }/* END: dump_symbol_strings() */
  5631.  
  5632. static int
  5633. gen_output(Piv iv, char *outpath)
  5634. {/* Bytecode output */
  5635. char *cp;
  5636. int i;
  5637. char outname[256];
  5638.     
  5639.     strcpy(outname, outpath);
  5640.     if((cp = strrchr(outname, '.')))
  5641.     {
  5642. #if 0
  5643.       if(iv->listing_wanted)
  5644.         strcpy(cp, ".lst");
  5645.       else
  5646.         strcpy(cp, ".byt");
  5647. #endif
  5648.     }
  5649.     else
  5650.     {
  5651.       if(iv->listing_wanted)
  5652.         strcat(outname, ".lst");
  5653.       else
  5654.         strcat(outname, ".byt");
  5655.     }
  5656.     for(i = 1; i < iv->argc; ++i)
  5657.     {
  5658.       if(!strcmp(outname, iv->argv[i]))
  5659.       {
  5660.         PERROR(pName ": ERROR output file `%s' is same as input file\n", outname);
  5661.       }
  5662.     }
  5663.     if(!(iv->outfile = fopen(outname, "wb")))
  5664.     {
  5665.         PERROR(pName ": Cannot open output file %s\n", outname);
  5666.     }
  5667.  
  5668.     /* Allocate a header struct */
  5669.     iv->header = Ccalloc(iv->category, 1, sizeof(struct exec));
  5670.  
  5671.     if(iv->listing_wanted)
  5672.     {
  5673.     long tim = time(0);
  5674.     char *date = ctime(&tim);
  5675.         fprintf(iv->outfile,"/*\n  `%s'     %s", outname, date);
  5676.         fprintf(iv->outfile,notice,MAJOR_VERSION,MINOR_VERSION);
  5677.     }
  5678.     else
  5679.     {/* Seek past the header area */
  5680.         iv->header->a_info = OMAGIC;
  5681.         fseek(iv->outfile, sizeof(struct exec), SEEK_SET);
  5682.     }
  5683.     install_builtins(iv);
  5684.     make_final_symtab(iv);
  5685.  
  5686. #if 0
  5687. if(iv->debug)
  5688. bterpdebug();
  5689. #endif
  5690.     dump_funcs(iv);
  5691.     fix_thunks(iv);
  5692.     prepare_data_relocs(iv);    /* adjust addresses for flat space */
  5693.     dump_data(iv);
  5694.     dump_bss(iv);
  5695.     dump_text_relocs(iv);
  5696.     dump_data_relocs(iv);
  5697.     dump_symbols(iv);
  5698.     dump_symbol_strings(iv);
  5699.  
  5700.     /* Update the header block */
  5701.     if(!iv->listing_wanted)
  5702.     {
  5703.         fseek(iv->outfile, 0, SEEK_SET);
  5704.         FILEWRITE(iv->header, sizeof(struct exec));
  5705.     }
  5706.     fclose(iv->outfile);
  5707.     iv->outfile = 0;    
  5708.     return iv->errors;
  5709. }
  5710. /* ======================= END BYTECODE OUTPUT GENERATOR ==================== */
  5711.  
  5712. /* ===================== GENERIC CODE BELOW THIS POINT ================== */
  5713. static jmp_buf run_env;
  5714. static void
  5715. prerror(const char *fmt, ...)
  5716. {
  5717.     VFPRINTF(fmt, (char *)(((int *)(&fmt))+1));
  5718.     longjmp(run_env, 3);
  5719. }
  5720. static void
  5721. prwarn(const char *fmt, ...)
  5722. {
  5723.     VFPRINTF(fmt, (char *)(((int *)(&fmt))+1));
  5724. }
  5725. static void
  5726. info(const char *fmt, ...)
  5727. {
  5728.     vfprintf(stdout, fmt, (char *)(((int *)(&fmt))+1));
  5729. }
  5730. /* ========================= MULTI HEAP MALLOC ========================== */
  5731. #define LOCAL static
  5732.  
  5733. #if USING_FRAMEWORK
  5734. #define THEWELL(a) mallocC(local_category, a)
  5735. static int local_category;
  5736. static int num_instance;
  5737. extern void *mallocC(int, int);
  5738. extern void freecat(int);
  5739. extern void oxlink_clear_bss();
  5740. extern int NewMallocCategory();
  5741. #endif /* USING_FRAMEWORK */
  5742.  
  5743. #define BASE_CATEGORY 0
  5744. #define MEMORY_BUG 0
  5745. #define PRINT_RAWDATA 0
  5746.  
  5747. #if MEMORY_BUG == 1
  5748. #define MPRINTF printf
  5749. #else
  5750. #define MPRINTF(args...)
  5751. #endif
  5752.  
  5753. #define PAGESIZE (4096)    /* can use `pagesize' function in OS */
  5754. #define ALIGNMENTM (sizeof(unsigned long))
  5755. #define MAL_MAXLEVEL (12)
  5756. #define ROUNDINGM(a) ((ALIGNMENTM-(a&(ALIGNMENTM-1)))&(ALIGNMENTM-1))
  5757. #define ALLOCSIZE (4096)
  5758. #define FRNTGUARD (0x544e5246UL)
  5759. #define BACKGUARD (0x48434142UL)
  5760. #ifndef THEWELL
  5761. #define THEWELL do_sbrk
  5762. #endif
  5763.  
  5764. #define NUMTYPES 3
  5765. #define SIZEH 0
  5766. #define FREEH 1
  5767. #define USEDH 2
  5768.  
  5769. #define SKIPVARS NodePM update[MAL_MAXLEVEL+1];NodePM node,prev;int level
  5770.  
  5771. #define DELETENODE(TYPE) \
  5772. {for(level=0;level<=bp->TYPE##level; level++)\
  5773. {if(update[level]->fptr[level] == node)\
  5774. update[level]->fptr[level] = node->fptr[level];else break;}\
  5775. while(bp->TYPE##level>0 && bp->TYPE##header->fptr[bp->TYPE##level]==_NILLL)\
  5776. bp->TYPE##level--;free_Mnode(bp,node,TYPE);}
  5777.  
  5778. #define INSERT() \
  5779. {while(level >= 0){\
  5780. node->fptr[level] = update[level]->fptr[level];\
  5781. update[level]->fptr[level] = node;level--;}}
  5782.  
  5783. #define SETLEVEL(TYPE) \
  5784. {level = getMlevel(bp, bp->TYPE##level);\
  5785. while(bp->TYPE##level < level)update[++bp->TYPE##level]=bp->TYPE##header;}
  5786.  
  5787. #define FINDKEY(TYPE, KEYVAL)\
  5788. {node = bp->TYPE##header;\
  5789. for(level = bp->TYPE##level; level >= 0; level--){\
  5790. while(node->fptr[level]->key < KEYVAL)\
  5791. node = node->fptr[level];\
  5792. update[level] = node;}prev=node;node=node->fptr[0];}
  5793.  
  5794. #define DETACH(SN)\
  5795. {SN->bptr->fptr=SN->fptr;if(SN->fptr)SN->fptr->bptr=SN->bptr;}
  5796.  
  5797. #define UNLINK(SN, N)\
  5798. {if(!sp->fptr&&sp->bptr->bptr<=(AddrP)(MAL_MAXLEVEL+1))dsize[N]=sp->size;\
  5799. DETACH(SN);free_addr(bp,SN);}
  5800.  
  5801. #define CHECKGUARDS(MSG)\
  5802. {if(bp->guarded){\
  5803. unsigned *p2;\
  5804. p2 = (void*)((char*)address+cursize-ALIGNMENTM);\
  5805. if(*address != FRNTGUARD)\
  5806. PERROR(pName #MSG ":%d: corrupted at 0x%x\n", bp->bincat, addr);\
  5807. if(*p2 != BACKGUARD)\
  5808. PERROR(pName #MSG ":%d: corrupted by 0x%x\n", bp->bincat, addr);}}
  5809.  
  5810. #if MEMORY_BUG == 1
  5811. #define HEAPCHECK \
  5812. {void *lastaddr;\
  5813. if(category > 0){\
  5814. guardC(category);\
  5815. if((lastaddr = heapcheckC(category, NULL))){\
  5816. FINDKEY(USEDH, (unsigned)lastaddr-ALIGNMENTM)\
  5817. MPRINTF("bad heap at %x c:%u size=%u\n", lastaddr, category, node->value);\
  5818. (void)print_rawdata(lastaddr-ALIGNMENTM, node->value);\
  5819. abort();}}}
  5820. #else
  5821. #define HEAPCHECK
  5822. #endif
  5823.  
  5824. struct _catlocs {
  5825.     void *addr;
  5826.     struct _catlocs *fptr;
  5827. };
  5828.  
  5829. typedef struct _nodeM
  5830. {
  5831.     unsigned key;
  5832.     unsigned value;
  5833.     unsigned levels;    /* must always be after value */
  5834.     struct _nodeM *fptr[1];
  5835. } NodeM, *NodePM;
  5836.  
  5837. typedef struct _addr
  5838. {
  5839.     struct _addr *fptr;
  5840.     struct _addr *bptr;
  5841.     NodePM maddr;
  5842.     unsigned size;
  5843. } *AddrP;
  5844.  
  5845. struct _bins {
  5846.     unsigned bits;
  5847.     unsigned nbits;
  5848.     NodePM SIZEHheader;
  5849.     int SIZEHlevel;
  5850.     NodePM FREEHheader;
  5851.     int FREEHlevel; 
  5852.     NodePM USEDHheader;
  5853.     int USEDHlevel;
  5854.  
  5855.     unsigned bincat;
  5856.     unsigned maxloc;
  5857.     unsigned minloc;
  5858.     struct _catlocs *catlocs;
  5859.     struct _bins *fptr;
  5860.     NodePM freenodes[NUMTYPES][MAL_MAXLEVEL+2];
  5861.     struct _addr *freeaddrlocs;
  5862.     char *chunkbase[NUMTYPES];
  5863.     int chunksize[NUMTYPES];
  5864.     int guarded;
  5865.     int addrbump;
  5866. };
  5867.  
  5868. static struct _bins zbp;
  5869. static struct _bins *hmap[1009];
  5870. static struct _nodeM _nilll = {0xffffffff,0,0,{0}};
  5871. static struct _nodeM *_NILLL = &_nilll;
  5872. static unsigned maxloc;
  5873. static unsigned minloc;
  5874. static struct _bins *freebinlocs;
  5875. static struct _catlocs *freecatlocs;
  5876. static char *binbase;
  5877. static int binsize;
  5878. static int chunksizes[] = {ALLOCSIZE,3*ALLOCSIZE,2*ALLOCSIZE};
  5879.  
  5880.  
  5881. static long randtbl[32]    = { 0L,
  5882.     0x9a319039L, 0x32d9c024L, 0x9b663182L, 0x5da1f342L, 
  5883.     0xde3b81e0L, 0xdf0a6fb5L, 0xf103bc02L, 0x48f340fbL, 
  5884.     0x7449e56bL, 0xbeb1dbb0L, 0xab5c5918L, 0x946554fdL, 
  5885.     0x8c2e680fL, 0xeb3d799fL, 0xb11ee0b7L, 0x2d436b86L, 
  5886.     0xda672e2aL, 0x1588ca88L, 0xe369735dL, 0x904f35f7L, 
  5887.     0xd7158fd6L, 0x6fa6f051L, 0x616e6b96L, 0xac94efdcL, 
  5888.     0x36413f93L, 0xc622c298L, 0xf5a42ab8L, 0x8a88d77bL, 
  5889.     0xf5ad9d0eL, 0x8999220bL, 0x27fb47b9L
  5890. };
  5891.  
  5892. static  long *fptr    = &randtbl[4];
  5893. static  long *rptr    = &randtbl[1];
  5894.  
  5895. /* ======================== START OF CODE =========================== */
  5896. #if PRINT_RAWDATA == 1
  5897. static char
  5898. hexbyte(unsigned int c)
  5899. {
  5900. char x = c & 0xf;
  5901.  
  5902.     return x + ((x>9) ? 55 : 48);
  5903. }
  5904. static void
  5905. print_rawdata(void *rawdata, long size)
  5906. {
  5907. unsigned long vaddr = 0;
  5908. unsigned char *d = rawdata;
  5909. int i,j;
  5910. char addr[9];
  5911. char hex1[24];
  5912. char hex2[24];
  5913. char side1[9];
  5914. char side2[9];
  5915.  
  5916.     addr[8] = 0;
  5917.     hex1[23] = 0;
  5918.     hex2[23] = 0;
  5919.     side1[8] = 0;
  5920.     side2[8] = 0;
  5921.     while(size > 0)
  5922.     {
  5923.     unsigned long qaddr = vaddr;
  5924.         memset(addr, '0', 8);
  5925.         memset(hex1, ' ', 23);
  5926.         memset(hex2, ' ', 23);
  5927.         memset(side1, ' ', 8);
  5928.         memset(side2, ' ', 8);
  5929.         i = 7;
  5930.         while(qaddr)
  5931.         {
  5932.             addr[i--] = hexbyte(qaddr);
  5933.             qaddr >>= 4;
  5934.         }
  5935.         for(i=0,j=0; i < 8; ++i)
  5936.         {
  5937.             if(--size >= 0)
  5938.             {
  5939.             unsigned int c = *d++;
  5940.                 if(isprint(c))
  5941.                     side1[i] = c;
  5942.                 else
  5943.                     side1[i] = '.';
  5944.                 hex1[j++] = hexbyte(c>>4);
  5945.                 hex1[j++] = hexbyte(c);
  5946.                     ++j;
  5947.             }
  5948.             else break;
  5949.         }
  5950.         for(i=0,j=0; i < 8; ++i)
  5951.         {
  5952.             if(--size >= 0)
  5953.             {
  5954.             unsigned int c = *d++;
  5955.                 if(isprint(c))
  5956.                     side2[i] = c;                    
  5957.                 else
  5958.                     side2[i] = '.';
  5959.                 hex2[j++] = hexbyte(c>>4);
  5960.                 hex2[j++] = hexbyte(c);
  5961.                 ++j;
  5962.             }
  5963.             else break;
  5964.         }
  5965.         VPRINTF("%s  %s%s%s  %s%s%s\n",addr,hex1," | ",hex2,side1,"|",side2);
  5966.         vaddr += 16;
  5967.     }
  5968. }
  5969. #endif
  5970.  
  5971. /*
  5972.  * Returns a really good 31-bit random number.
  5973.  */
  5974. static long
  5975. lrandom()
  5976. {
  5977. long i;
  5978.     
  5979.     *fptr += *rptr;
  5980.     i = (*fptr >> 1) & 0x7fffffffUL;
  5981.     if(++fptr > &randtbl[31])
  5982.     {
  5983.         fptr = &randtbl[1];
  5984.         ++rptr;
  5985.     }
  5986.     else
  5987.     {
  5988.         if(++rptr > &randtbl[31])  
  5989.             rptr = &randtbl[1];
  5990.     }
  5991.     return( i );
  5992. }
  5993. #if !USING_FRAMEWORK
  5994. static void *
  5995. do_sbrk(unsigned amount)
  5996. {
  5997. void *address;
  5998.  
  5999.     address = sbrk(amount);    /* OR WHATEVER TO ACCESS THE OPERATING SYSTEM */
  6000.     if(address == (void*)-1)
  6001.     {
  6002.         PERROR(pName "\nsystem out of memory, requested %u bytes\n", amount);
  6003.     }
  6004.     return address;
  6005. }
  6006. #endif
  6007. static struct _catlocs *
  6008. new_catloc(void)
  6009. {
  6010. struct _catlocs *p;
  6011.     if((p=freecatlocs))
  6012.     {
  6013.         freecatlocs = p->fptr;
  6014.         return p;
  6015.     }
  6016.     if(binsize < sizeof(struct _catlocs))
  6017.     {
  6018.         binbase = THEWELL(4096);
  6019.         binsize = 4096;
  6020.     }
  6021.     binsize -= sizeof(struct _catlocs);
  6022.     p = (void*)binbase;
  6023.     binbase += sizeof(struct _catlocs);
  6024.     return p;
  6025. }
  6026. static void
  6027. free_catloc(struct _catlocs *p)
  6028. {
  6029.     p->fptr = freecatlocs;
  6030.     freecatlocs = p;
  6031. }
  6032. static void *
  6033. new_chunk(struct _bins *bp, int size, int type)
  6034. {
  6035. char *p;
  6036.      if(bp->chunksize[type] < size)
  6037.     {
  6038.         if(bp->bincat == 0) {
  6039.             bp->chunkbase[type] = THEWELL(chunksizes[type]);
  6040.             bp->chunksize[type] = chunksizes[type];
  6041.         }
  6042.         else {
  6043.         struct _catlocs *cl;
  6044.             bp->chunkbase[type] = Cmalloc(0,chunksizes[type]-zbp.guarded);
  6045.             bp->chunksize[type] = chunksizes[type]-zbp.guarded;
  6046.             cl = new_catloc();
  6047.             cl->addr = bp->chunkbase[type];
  6048.             cl->fptr = bp->catlocs;
  6049.             bp->catlocs = cl;
  6050.         }
  6051.     }
  6052.     bp->chunksize[type] -= size;
  6053.     p = bp->chunkbase[type];
  6054.     bp->chunkbase[type] += size;
  6055.     return p;
  6056. }
  6057. static void *
  6058. new_Mnode(struct _bins *bp, int levels, int type)
  6059. {
  6060. int size;
  6061. NodePM p;
  6062.  
  6063.     if((p=bp->freenodes[type][levels]))
  6064.     {
  6065.         bp->freenodes[type][levels] = p->fptr[0];
  6066.         p->value = 0;
  6067.         return p;
  6068.     }
  6069.      size = sizeof(struct _nodeM) + levels * sizeof(void*);
  6070.     p = new_chunk(bp, size, type);
  6071.     p->levels = levels;
  6072.     p->value = 0;
  6073.     return p;    
  6074. }
  6075. static void
  6076. free_Mnode(struct _bins *bp, NodePM p, int type)
  6077. {
  6078.     p->fptr[0] = bp->freenodes[type][p->levels];
  6079.     bp->freenodes[type][p->levels] = p;
  6080. }
  6081. static struct _addr *
  6082. new_addr(struct _bins *bp)
  6083. {
  6084. struct _addr *p;
  6085.     if((p=bp->freeaddrlocs))
  6086.     {
  6087.         bp->freeaddrlocs = p->fptr;
  6088.         return p;
  6089.     }
  6090.     return new_chunk(bp, sizeof(struct _addr), FREEH);
  6091. }
  6092. static void
  6093. free_addr(struct _bins *bp, struct _addr *p)
  6094. {
  6095.     p->fptr = bp->freeaddrlocs;
  6096.     bp->freeaddrlocs = p;
  6097. }
  6098. static struct _bins *
  6099. new_bins(void)
  6100. {
  6101. struct _bins *p;
  6102.     if((p=freebinlocs))
  6103.     {
  6104.         freebinlocs = p->fptr;
  6105.         return p;
  6106.     }
  6107.      if(binsize < sizeof(struct _bins))
  6108.     {
  6109.         binbase = THEWELL(4096);
  6110.         binsize = 4096;
  6111.     }
  6112.     binsize -= sizeof(struct _bins);
  6113.     p = (struct _bins*)binbase;
  6114.     binbase += sizeof(struct _bins);
  6115.     return p;
  6116. }
  6117. static void
  6118. free_bins(struct _bins *p)
  6119. {
  6120.     p->fptr = freebinlocs;
  6121.     freebinlocs = p;
  6122. }
  6123. static int
  6124. getMlevel (struct _bins *p, int binlevel)
  6125. {
  6126. int level = -1;
  6127. int bits = 0;
  6128.  
  6129.     while(bits == 0)
  6130.     {
  6131.         if (p->nbits == 0)
  6132.         {
  6133.             p->bits = lrandom();
  6134.             p->nbits = 15;
  6135.         }
  6136.         bits = p->bits & 3;
  6137.         p->bits >>= 2;
  6138.         p->nbits--;
  6139.  
  6140.         if(++level > binlevel)
  6141.             break;
  6142.     }
  6143.     return (level > MAL_MAXLEVEL) ? MAL_MAXLEVEL : level;
  6144. }
  6145.  
  6146. static void
  6147. init_bins(struct _bins *bp, int category)
  6148. {
  6149. int i;
  6150. int binnum = category % 1009;
  6151.  
  6152.     bzero(bp, sizeof(struct _bins));
  6153.     bp->bincat = category;
  6154.     bp->minloc = 0xffffffff;
  6155.     bp->fptr = hmap[binnum];
  6156.     hmap[binnum] = bp;
  6157.     bp->SIZEHheader = new_Mnode(bp, MAL_MAXLEVEL+1, SIZEH);
  6158.     bp->FREEHheader = new_Mnode(bp, MAL_MAXLEVEL+1, FREEH);
  6159.     bp->USEDHheader = new_Mnode(bp, MAL_MAXLEVEL+1, USEDH);
  6160.  
  6161.     for(i = 0; i <= MAL_MAXLEVEL; ++i)
  6162.     {
  6163.         bp->SIZEHheader->fptr[i] = _NILLL;
  6164.         bp->FREEHheader->fptr[i] = _NILLL;
  6165.         bp->USEDHheader->fptr[i] = _NILLL;
  6166.     }
  6167. }
  6168.  
  6169. static struct _bins*
  6170. getcat(int category)
  6171. {
  6172. struct _bins *hbp;
  6173.  
  6174.     hbp = hmap[category % 1009];
  6175.     while(hbp)
  6176.     {
  6177.         if(hbp->bincat == category)
  6178.             return hbp;
  6179.         hbp = hbp->fptr;
  6180.     }
  6181.     return 0;
  6182. }
  6183. static struct _bins *
  6184. initcat(int category)
  6185. {
  6186. struct _bins *bp;
  6187.  
  6188.     if(category == 0)
  6189.     {
  6190.         bp = &zbp;
  6191.         if(zbp.SIZEHheader == 0)
  6192.             init_bins(bp, category);
  6193.         return bp;
  6194.     }
  6195.     /* do this to set zbp.guarded properly on startup */
  6196.     if(zbp.SIZEHheader == 0)
  6197.         initcat(0);
  6198.  
  6199.     if((bp = new_bins()))
  6200.     {
  6201.         init_bins(bp, category);
  6202.         return bp;
  6203.     }
  6204.     return 0;
  6205. }
  6206. static void *
  6207. getspace(struct _bins *bp, unsigned size, unsigned *remainder)
  6208. {
  6209. unsigned desired;
  6210. void *address;
  6211.   
  6212.     desired = ((size+ALLOCSIZE-1)/ALLOCSIZE)*ALLOCSIZE;
  6213.     if(bp->bincat == 0)
  6214.     {
  6215.         address = THEWELL(desired);
  6216.         *remainder = desired - size;
  6217.     }
  6218.     else
  6219.     {
  6220.     struct _catlocs *cl;
  6221.  
  6222.         if((desired-size) > zbp.guarded)
  6223.             desired -= zbp.guarded;
  6224.         
  6225.         address = Cmalloc(0, desired);
  6226.         *remainder = desired - size;
  6227.  
  6228.         /* save the gross allocations for the category */
  6229.         cl = new_catloc();
  6230.         cl->addr = address;
  6231.         cl->fptr = bp->catlocs;
  6232.         bp->catlocs = cl;
  6233.     }
  6234.     /* maintain address range info */
  6235.     if((unsigned)address < bp->minloc)
  6236.         bp->minloc = (unsigned)address;
  6237.     if(((unsigned)address + desired) > bp->maxloc)
  6238.         bp->maxloc = (unsigned)address + desired;
  6239.      if(bp->minloc < minloc)
  6240.          minloc = bp->minloc;
  6241.      if(bp->maxloc > maxloc)
  6242.          maxloc = bp->maxloc;
  6243.     return address;
  6244. }
  6245. static void
  6246. addto_sizelist(struct _bins *bp, AddrP ap)
  6247. {
  6248. SKIPVARS;
  6249.  
  6250.     /* INSERT IN SIZE LIST */
  6251.     FINDKEY(SIZEH, ap->size)
  6252.  
  6253.     if(node->key == ap->size)
  6254.     {/* size node exists */
  6255.         ap->fptr = (AddrP)node->value;
  6256.         ap->bptr = (AddrP)&node->value;
  6257.         if(ap->fptr) ap->fptr->bptr = ap;
  6258.         node->value = (unsigned)ap;
  6259.     }
  6260.     else
  6261.     {/* create new size node */
  6262.         SETLEVEL(SIZEH)
  6263.         node = new_Mnode(bp, level, SIZEH);
  6264.         node->key = ap->size;
  6265.         node->value = (unsigned)ap;
  6266.         ap->fptr = 0;
  6267.         ap->bptr = (AddrP)&node->value;
  6268.         INSERT()
  6269.     }
  6270. }
  6271. static void
  6272. addto_freelist(struct _bins *bp, void *addr, unsigned size)
  6273. {
  6274. SKIPVARS;
  6275. AddrP ap,sp;
  6276. unsigned dsize[2];
  6277.  
  6278.     /* GET NEW ADDR STRUCT */
  6279.     ap = new_addr(bp);
  6280.     ap->size = size;
  6281.  
  6282.     dsize[1] = dsize[0] = 0; /* sizenode deletion markers */
  6283.  
  6284.     /* CHECK FREE LIST */
  6285.     FINDKEY(FREEH, (unsigned)addr)
  6286.  
  6287.     /* CHECK FOR MERGE OR INSERT */
  6288.     if(prev->value && prev->key+((AddrP)prev->value)->size == (unsigned)addr)
  6289.     {/* merge with previous block */
  6290.         ap->size += ((AddrP)prev->value)->size;
  6291.  
  6292.         if(prev->key + ap->size == node->key)
  6293.         {/* merge with previous and next block */
  6294.             sp = (AddrP) node->value;;
  6295.             ap->size += sp->size;
  6296.  
  6297.             /* delete size struct for next block */
  6298.             UNLINK(sp, 0)
  6299.  
  6300.             /* delete next block */
  6301.             DELETENODE(FREEH);
  6302.         }
  6303.         /* delete size struct for prev block */
  6304.         sp = (AddrP)prev->value;
  6305.         UNLINK(sp, 1)
  6306.  
  6307.         /* set new address struct */
  6308.         prev->value = (unsigned)ap;
  6309.         ap->maddr = prev;
  6310.     }
  6311.     else if(node->value && (char*)addr + size == (void*)node->key)
  6312.     {/* merge with next block */
  6313.         sp = (AddrP) node->value;;
  6314.         node->key = (unsigned)addr;
  6315.         ap->size += sp->size;
  6316.  
  6317.         /* unlink size struct for next block */
  6318.         UNLINK(sp,0)
  6319.  
  6320.         /* set new address struct */
  6321.         node->value = (unsigned)ap;
  6322.         ap->maddr = node;
  6323.     }
  6324.     else
  6325.     {/* insert in free list */
  6326.  
  6327.         SETLEVEL(FREEH)
  6328.         node = new_Mnode(bp, level, FREEH);
  6329.         node->key = (unsigned)addr;
  6330.         node->value = (unsigned)ap;
  6331.         ap->maddr = node;
  6332.         INSERT()
  6333.     }
  6334.     addto_sizelist(bp, ap);
  6335.  
  6336.     /* Remove sizenodes eliminated by merge */
  6337.     if(dsize[0])
  6338.     {
  6339.         FINDKEY(SIZEH, dsize[0])
  6340.         if(node->value == 0)
  6341.           DELETENODE(SIZEH)
  6342.     }
  6343.     if(dsize[1])
  6344.     {
  6345.         FINDKEY(SIZEH, dsize[1])
  6346.         if(node->value == 0)
  6347.           DELETENODE(SIZEH)
  6348.     }
  6349. }
  6350.  
  6351. LOCAL void* 
  6352. Cmemalign(int category, unsigned alignment, unsigned req)
  6353. {
  6354. SKIPVARS;
  6355. NodePM fnode;
  6356. unsigned remainder;
  6357. unsigned *address;
  6358. struct _bins *bp;
  6359. unsigned mask, size;
  6360.  
  6361.  
  6362.     if(!(bp = getcat(category)))
  6363.       if(!(bp = initcat(category)))
  6364.         return 0;
  6365. HEAPCHECK
  6366.     if(req == 0)
  6367.         req = ALIGNMENTM;
  6368.     else
  6369.         req += ROUNDINGM(req);
  6370.     size = req += bp->guarded;
  6371.  
  6372.     if(alignment)
  6373.     {
  6374.         alignment += ROUNDINGM(alignment);
  6375.         if(alignment > ALIGNMENTM)
  6376.         {
  6377.             mask = alignment -1;
  6378.             size = req + alignment + bp->guarded;
  6379.         }
  6380.         else
  6381.         {
  6382.             alignment = 0;
  6383.         }
  6384.     }
  6385.  
  6386.     /* check sizelist for candidate */
  6387.     FINDKEY(SIZEH, size)
  6388.     fnode = node;
  6389. trynext:
  6390.     if(node->key != 0xffffffff)
  6391.     {/* found an appropriately sized block */
  6392.     AddrP sp = (AddrP)node->value;
  6393.  
  6394.         if(!sp && node == fnode)
  6395.         {
  6396.         NodePM q;
  6397.             q = node->fptr[0];
  6398.             DELETENODE(SIZEH)
  6399.             node = q;
  6400.             goto trynext;
  6401.         }
  6402.         if(!sp)
  6403.         {/* no available space at this size */
  6404.             node = node->fptr[0];
  6405.             goto trynext;
  6406.         }
  6407.  
  6408.         /* extract some space from this block */
  6409.         remainder = node->key - size;
  6410.         address = (void*)sp->maddr->key;
  6411.         sp->maddr->key += size;
  6412.         DETACH(sp);
  6413.  
  6414.         if(node->value == 0)
  6415.         {/* no more blocks of this size, delete sizenode */
  6416.             if(node != fnode)
  6417.               FINDKEY(SIZEH, size)
  6418.             DELETENODE(SIZEH)
  6419.         }
  6420.  
  6421.         if(remainder == 0)
  6422.         {/* no remaining space,the node in freelist is exhausted, delete it */
  6423.             FINDKEY(FREEH, sp->maddr->key)
  6424.             DELETENODE(FREEH)
  6425.             free_addr(bp, sp);
  6426.         }
  6427.         else
  6428.         {/* space remains in block, move it to new size loc */
  6429.             sp->size = remainder;
  6430.             addto_sizelist(bp, sp);
  6431.         }
  6432.     }
  6433.     else
  6434.     {
  6435.         address = getspace(bp, size, &remainder);
  6436.         if(remainder)
  6437.           addto_freelist(bp, ((char*)address)+size, remainder);
  6438.     }
  6439.     if(alignment)
  6440.     {
  6441.     unsigned diff;
  6442.         if((diff = (unsigned)address & mask))
  6443.         {/* move address forward */
  6444.         char *naddress;
  6445.         unsigned lose;
  6446.             lose = alignment - diff;
  6447.             naddress = (char*)address + lose;
  6448.             addto_freelist(bp, address, lose);
  6449.             address = (unsigned*)naddress;
  6450.         }
  6451.     }
  6452.     if(bp->guarded)
  6453.     {
  6454.       *address = FRNTGUARD;
  6455.       *((unsigned*)(((char*)address)+req-ALIGNMENTM)) = BACKGUARD;
  6456.  
  6457.     }
  6458.  
  6459.     FINDKEY(USEDH, (unsigned)address)
  6460.  
  6461.     if(node->key == (unsigned)address) {
  6462.       PERROR(pName "allocC:%d: bookkeeping nodes are corrupted at:0x%x\n",
  6463.           category, address);
  6464.     }
  6465.     SETLEVEL(USEDH)
  6466.     node = new_Mnode(bp, level, USEDH);
  6467.     node->key = (unsigned)address;
  6468.     node->value = req;
  6469.     INSERT()    
  6470.  
  6471.     return address+bp->addrbump;
  6472. }
  6473. LOCAL void*
  6474. Ccalloc(int category, unsigned cnt, unsigned elem_size)
  6475. {
  6476. unsigned size = cnt * elem_size;
  6477. void* buf;;
  6478.  
  6479.   if((buf = Cmalloc(category, size)))
  6480.       bzero(buf, size);
  6481.   return buf;
  6482. };
  6483. LOCAL void
  6484. Cfree(int category, void* addr)
  6485. {
  6486. unsigned cursize;
  6487. unsigned *address;
  6488. struct _bins *bp;
  6489. SKIPVARS;
  6490.     if(addr)
  6491.     {
  6492.         if(!(bp = getcat(category))) {
  6493.             PERROR(pName "Cfree:%d: non-existant category at:0x%x\n",category,addr);
  6494.         }
  6495. HEAPCHECK
  6496.         address = (void*) ((unsigned*)addr - bp->addrbump);
  6497.         FINDKEY(USEDH, (unsigned)address)
  6498.         if(node->key != (unsigned)address) {
  6499.           PERROR(pName "Cfree:%d: bogus address=0x%x\n", category, addr);
  6500.         }
  6501.         cursize = node->value;
  6502.         CHECKGUARDS(Cfree)
  6503.         DELETENODE(USEDH)
  6504.  
  6505.         addto_freelist(bp, address, cursize);
  6506.     }
  6507.     else PERROR(pName "Cfree:%d: null pointer\n", category);
  6508. }
  6509. LOCAL void* 
  6510. Crealloc(int category, void* addr, unsigned newsize)
  6511. {
  6512. SKIPVARS;
  6513. unsigned cursize;
  6514. unsigned *address;
  6515. struct _bins *bp;
  6516. NodePM onode;
  6517.  
  6518.     if(addr == 0) 
  6519.         return Cmalloc(category, newsize);
  6520.     else
  6521.     {
  6522.         if(!(bp = getcat(category))) {
  6523.            PERROR(pName "reallocC:%d: non-existant category at:%x\n",category,addr);
  6524.         }
  6525. HEAPCHECK 
  6526.         if(newsize == 0)
  6527.             newsize = ALIGNMENTM;
  6528.         else
  6529.             newsize += ROUNDINGM(newsize);
  6530.         newsize += bp->guarded;
  6531.  
  6532.         address = (void*)(((char*)addr)-(bp->guarded/2));
  6533.         FINDKEY(USEDH, (unsigned)address)
  6534.         if(node->key != (unsigned)address) {
  6535.           PERROR(pName "reallocC:%d: bogus address=0x%x\n", category, addr);
  6536.         }
  6537.         cursize = node->value;
  6538.         node->value = newsize;
  6539.         onode = node;
  6540.  
  6541.         CHECKGUARDS(reallocC)
  6542.  
  6543.         if(newsize == cursize)
  6544.             return addr;
  6545.         if(newsize > cursize)
  6546.         {/* check if block can be extended */
  6547.         void *taddr = ((char*)address) + cursize;
  6548.         unsigned extendsize = newsize-cursize;
  6549.  
  6550.             /* check freelist for an available block at the right address */
  6551.             FINDKEY(FREEH, (unsigned)taddr)
  6552.             if(node->key == (unsigned)taddr)
  6553.             {
  6554.             AddrP sp = (AddrP)node->value;
  6555.                 if(sp->size >= extendsize)
  6556.                 {/* BLOCK CAN BE EXTENDED INTERNALLY */
  6557.                     node->key += extendsize;
  6558.                     sp->size -= extendsize;
  6559.                     DETACH(sp)
  6560.                     if(sp->size == 0)
  6561.                     {/* the extension block is used up, delete this node */
  6562.                         free_addr(bp, sp);
  6563.                         DELETENODE(FREEH)
  6564.                     }
  6565.                     else
  6566.                     {/* shift the remainder in the sizelist */
  6567.                         addto_sizelist(bp, sp);
  6568.                     }
  6569.                     /* SUCCESS */
  6570.                     if(bp->guarded)
  6571.                     {
  6572.                         *((unsigned*)(((char*)address)+newsize-ALIGNMENTM))
  6573.                             = BACKGUARD;
  6574.                     }
  6575.                     return addr;
  6576.                 }
  6577.             }
  6578.             /* HERE WE COULD CHECK OTHER SOURCES OF SPACE */
  6579.  
  6580.             /* can't extend block, malloc some new space */
  6581.             if((taddr = Cmalloc(category,newsize-bp->guarded)))
  6582.             {
  6583.                 memmove(taddr,addr,cursize-bp->guarded);
  6584.                 onode->value = cursize;
  6585.                 Cfree(category, addr);
  6586.             }
  6587.             /* SUCCESS */
  6588.             return taddr;
  6589.         }
  6590.         else
  6591.         {/* shrink block */
  6592.             if(bp->guarded)
  6593.             {
  6594.                 *((unsigned*)(((char*)address)+newsize-ALIGNMENTM))
  6595.                     = BACKGUARD;
  6596.             }
  6597.             addto_freelist(bp, ((char*)address)+newsize, cursize-newsize); 
  6598.             return addr;
  6599.         }
  6600.       }
  6601. }
  6602. LOCAL void
  6603. Cfreecat(int category)
  6604. {
  6605. struct _bins *bp;
  6606.  
  6607.     if(category == 0)
  6608.         return;
  6609.  
  6610.     if((bp = getcat(category)))
  6611.     {
  6612.     struct _catlocs *cl = bp->catlocs;
  6613.     struct _bins *hbp;
  6614.     struct _bins *prev;
  6615.  
  6616.         while(cl)
  6617.         {/* Space allocated to the category is moved to category 0 */
  6618.         void *ql = cl->fptr;
  6619.  
  6620.             Cfree(0, cl->addr);
  6621.             free_catloc(cl);
  6622.             cl = ql;
  6623.         }
  6624.         /* space for the _bins struct is placed on a free list */
  6625.         hbp = hmap[category % 1009];
  6626.         prev = 0;
  6627.         while(hbp)
  6628.         {
  6629.             if(hbp->bincat == category)
  6630.             {
  6631.                 if(prev == 0)
  6632.                     hmap[category % 1009] = hbp->fptr;
  6633.                 else
  6634.                     prev->fptr = hbp->fptr;
  6635.                 free_bins(hbp);
  6636.                 return;
  6637.             }
  6638.             prev = hbp;
  6639.             hbp = hbp->fptr;
  6640.         }
  6641.     }
  6642. }
  6643. LOCAL int
  6644. Cmemrange(int category, unsigned *min, unsigned *max)
  6645. {
  6646. struct _bins *bp;
  6647.  
  6648.     if((bp = getcat(category)))
  6649.     {
  6650.         *min = bp->minloc;
  6651.         *max = bp->maxloc;
  6652.         return 1;
  6653.     }
  6654.     return 0;
  6655. }
  6656. LOCAL int
  6657. Cusedrange(int category, unsigned *min, unsigned *max)
  6658. {
  6659. struct _bins *bp;
  6660. NodePM node;
  6661. int level;
  6662.  
  6663.     if((bp = getcat(category)))
  6664.     {
  6665.         node = bp->USEDHheader;
  6666.         *min = node->fptr[0]->key;
  6667.         for(level = bp->USEDHlevel; level >= 0; level--)
  6668.           while(node->fptr[level]->key < 0xffffffff)
  6669.             node = node->fptr[level];
  6670.         *max = node->key;
  6671.         return 1;
  6672.     }
  6673.     return 0;
  6674. }
  6675. LOCAL void
  6676. Ctotrange(unsigned *min, unsigned *max)
  6677. {
  6678.     *min = minloc;
  6679.     *max = maxloc;
  6680. }
  6681. LOCAL void
  6682. Cguard(int category)
  6683. {
  6684. struct _bins *bp;
  6685.  
  6686.     if(!(bp = getcat(category)))
  6687.       if(!(bp = initcat(category)))
  6688.           return;
  6689.  
  6690.     if(!bp->guarded)
  6691.     {
  6692.         bp->guarded = 2*ALIGNMENTM;
  6693.         bp->addrbump = 1;
  6694.     }
  6695. }
  6696. LOCAL void*
  6697. Cheapcheck(int category, void *start)
  6698. {
  6699. struct _bins *bp;
  6700. NodePM node,prev;
  6701. unsigned *p1,*p2;
  6702.  
  6703.     if((bp = getcat(category)))
  6704.     {
  6705.         if(bp->guarded)
  6706.         {
  6707.             prev = 0;
  6708.             node = bp->USEDHheader;
  6709.             while(        (node = node->fptr[0]) != (NodePM)0xffffffff
  6710.                     &&    node->key != 0xffffffffUL)
  6711.             {
  6712.                 if((void*)node->key > start)
  6713.                 {
  6714.                     p1 = (unsigned*)node->key;
  6715.                     if(*p1 != FRNTGUARD)
  6716.                     {
  6717.                         if(prev)
  6718.                             return (char*)prev->key+ALIGNMENTM;
  6719.                         else
  6720.                             return (void*)1;
  6721.                     }
  6722.                     p2 = (unsigned*)(((char*)p1)+node->value-ALIGNMENTM);
  6723.                     if(*p2 != BACKGUARD)
  6724.                         return (char*)node->key+ALIGNMENTM;
  6725.                 }
  6726.                 prev = node;
  6727.             }
  6728.         }
  6729.     }
  6730.     return 0;
  6731. }
  6732. LOCAL void* 
  6733. Cmalloc(int category, unsigned size)
  6734. {
  6735.     return Cmemalign(category, 0, size);
  6736. }
  6737.  
  6738. LOCAL void* 
  6739. Cvalloc(int category, unsigned bytes)
  6740. {
  6741.   return Cmemalign (category, PAGESIZE, bytes);
  6742. }
  6743. LOCAL unsigned
  6744. Cmallocsize(int category, void* addr)
  6745. {
  6746. struct _bins *bp;
  6747. SKIPVARS;
  6748.  
  6749.     if(addr && (bp = getcat(category)))
  6750.     {
  6751.     unsigned address = (unsigned)((unsigned*)addr - bp->addrbump);
  6752.         FINDKEY(USEDH, address)
  6753.         if(node->key == address)
  6754.             return node->value - bp->guarded;
  6755.     }
  6756.     return 0;
  6757. }
  6758.  
  6759. LOCAL int
  6760. Cnewcat()
  6761. {
  6762. static unsigned int cat = BASE_CATEGORY;
  6763.     return ++cat;
  6764. }
  6765. /* ====================== END MULTI-HEAP MALLOC ============================ */
  6766.  
  6767.  
  6768. /* ====================== SYMBOL TABLE HANDLERS ============================ */
  6769.  
  6770. typedef struct _key
  6771. {
  6772.     unsigned long k[2];
  6773.     unsigned long hv;
  6774. } KEY, *KEYP;
  6775.  
  6776. typedef struct _nodeS
  6777. {/* 40 bytes -- adjust size to suit application */
  6778.     unsigned long value[4];    /* 16 bytes */
  6779.     unsigned long key[2];    /* 8 bytes */
  6780.     struct _nodeS *fptr[4];    /* 16 bytes */
  6781. } NodeS, *NodePS;
  6782.  
  6783. typedef struct _pbuf
  6784. {/* symbol table object */
  6785.     int    nbins;            /* number of bins in dictionary */
  6786.     int lastbin;        /* for seq access */
  6787.     NodePS lastptr;        /* ditto */
  6788.     int category;        /* heap number */
  6789.     char *chunkbase;    /* node allocation base */
  6790.     int chunksize;        /* number of bytes available in current chunk */
  6791.     NodePS freelist;    /* list of freed nodes for allocation */
  6792.     int level;            /* sorted level */
  6793.     int bits;            /* sorted bits */
  6794.     int bitcnt;            /* sorted bitcnt */
  6795.     NodePS header;        /* sorted header */
  6796.     NodePS bins[0];        /* bins if hashed dictionary */
  6797. } *PbufP;
  6798.  
  6799. #define SYM_MAXLEVEL 12
  6800. #define TBL ((PbufP)tbl)
  6801.  
  6802. static struct _nodeS _nnil = {{0,0,0,0},{0xffffffff,0xffffffff},{0,0,0,0}};
  6803. static struct _nodeS *_NNIL = &_nnil;
  6804.  
  6805. static int
  6806. getSlevel (PbufP tbl)
  6807. {
  6808. int level = -1;
  6809. int bits = 0;
  6810.  
  6811.     while (bits == 0)
  6812.     {
  6813.         if (tbl->bitcnt == 0)
  6814.         {
  6815.             tbl->bits = lrandom();
  6816.             tbl->bitcnt = 15;
  6817.         }
  6818.  
  6819.         bits = tbl->bits & 3;
  6820.         tbl->bits >>= 2;
  6821.         tbl->bitcnt--;
  6822.  
  6823.         if(++level > tbl->level)
  6824.             break;
  6825.     }
  6826.     return (level > SYM_MAXLEVEL) ? SYM_MAXLEVEL : level;
  6827.  
  6828. }
  6829.  
  6830. static void
  6831. hash(void *key, KEY *cat)
  6832. {
  6833.     cat->k[0] = ((unsigned long*)key)[0];
  6834.     cat->k[1] = ((unsigned long*)key)[1];
  6835.     cat->hv = ((cat->k[0] ^ cat->k[1]) * 1103515245UL) + 12345;
  6836. }
  6837. static void
  6838. sym_hash(unsigned long *key, char *symb)
  6839. {
  6840. int len = strlen(symb);
  6841. int i;
  6842.     for(i = 0; i < len; ++i)
  6843.       ((unsigned char *)key)[i & 7] ^= symb[i];
  6844.     key[0] = ((key[0] ^ key[1]) * 1103515245UL) + 12345;
  6845.     key[1] = len;
  6846. }
  6847. static void *
  6848. new_Snode(PbufP tbl, int levels)
  6849. {
  6850. NodePS p;
  6851. int size;
  6852.     if(levels <= 3)
  6853.     {
  6854.         if(tbl->freelist)
  6855.         {
  6856.             p = tbl->freelist;
  6857.             tbl->freelist = p->fptr[0];
  6858.             p->fptr[0] = 0;
  6859.             return p;
  6860.         }
  6861.     }
  6862.     size = sizeof(struct _nodeS) + ((levels-3) * sizeof(void*));
  6863.     if(tbl->chunksize < size)
  6864.     {
  6865.         tbl->chunkbase = Ccalloc(tbl->category, 1, 4080);
  6866.         tbl->chunksize = 4080;
  6867.     } 
  6868.     tbl->chunksize -= size;
  6869.     p = (NodePS)tbl->chunkbase;
  6870.     tbl->chunkbase += size;
  6871.     return p;
  6872. }
  6873. static void
  6874. free_Snode(PbufP tbl, NodePS node)
  6875. {
  6876.     bzero(node, sizeof(struct _nodeS));
  6877.     node->fptr[0] = tbl->freelist;
  6878.     tbl->freelist = node;
  6879. }
  6880.  
  6881. static void*
  6882. NewSymTable(int category, int nbins)
  6883. {
  6884. PbufP tbl;
  6885.  
  6886.     tbl = Ccalloc(category, 1, nbins*sizeof(NodePS) + sizeof(struct _pbuf));
  6887.     if(nbins == 0)
  6888.     {/* sorted dictionary */
  6889.     int i;
  6890.         tbl->header = new_Snode(tbl, SYM_MAXLEVEL+1);
  6891.         for(i = 0; i <= SYM_MAXLEVEL; ++i)
  6892.             tbl->header->fptr[i] = _NNIL;
  6893.     }
  6894.     tbl->nbins = nbins;
  6895.     tbl->category = category;
  6896.     return tbl;
  6897. }
  6898. static int
  6899. SymFind(void *tbl, void *key, void *result)
  6900. {
  6901. NodePS node;
  6902.  
  6903.     if(tbl && key)
  6904.     {
  6905.       if(TBL->nbins)
  6906.       {/* hashed dictionary */
  6907.       KEY cat;
  6908.  
  6909.         hash(key, &cat);
  6910.         if((node = TBL->bins[cat.hv % TBL->nbins]))
  6911.         {
  6912.             do {
  6913.                 if(        node->key[0] == cat.k[0]
  6914.                     &&    node->key[1] == cat.k[1])
  6915.                 {
  6916.                     if(result)
  6917.                       *((NodePS *)result) = node;
  6918.                     TBL->lastbin = cat.hv % TBL->nbins;
  6919.                     TBL->lastptr = node;
  6920.                     return 1;
  6921.                 }
  6922.              } while((node = node->fptr[0]));
  6923.         }
  6924.         return 0;
  6925.       }
  6926.       else
  6927.       {/* sorted dictionary */
  6928.       int level;
  6929.  
  6930.         node = TBL->header;
  6931.         for(level = TBL->level; level >= 0; level--)
  6932.         {
  6933.           while( KEYLT(node->fptr[level]->key, ((unsigned long*)key)) )
  6934.             node = node->fptr[level];
  6935.         }
  6936.         node = node->fptr[0];
  6937.  
  6938.         TBL->lastptr = node;
  6939.         if(result)
  6940.             *((NodePS *)result) = node;
  6941.         return (KEYEQ(node->key, ((unsigned long*)key))) ? 1 : 0;
  6942.       }
  6943.     }
  6944.     return 0;
  6945. }
  6946. static int
  6947. SymFindRange(void *tbl, void *key, void *result)
  6948. {/* assumes 4 byte key and value (the value can be bigger) */
  6949. NodePS node;
  6950.  
  6951.     if(tbl && key)
  6952.     {
  6953.       if(TBL->nbins)
  6954.       {/* hashed dictionary */
  6955.         return 0;
  6956.       }
  6957.       else
  6958.       {/* sorted dictionary */
  6959.       NodePS prev;
  6960.       int level;
  6961.  
  6962.         node = TBL->header;
  6963.         for(level = TBL->level; level >= 0; level--)
  6964.         {
  6965.           while ( node->fptr[level]->key[0] < ((unsigned long*)key)[0] )
  6966.             node = node->fptr[level];
  6967.         }
  6968.         prev = node;
  6969.         node = node->fptr[0];
  6970.  
  6971.         if( node->key[0] == ((unsigned long*)key)[0] )
  6972.         {
  6973.             TBL->lastptr = node;
  6974.             if(result)
  6975.                 *((NodePS *)result) = node;
  6976.             return 1;
  6977.         }        
  6978.         if( ((unsigned long*)key)[0] < prev->key[0]+prev->value[0] )
  6979.         {
  6980.             TBL->lastptr = prev;
  6981.             if(result)
  6982.                 *((NodePS *)result) = prev;
  6983.             return 1;
  6984.         }
  6985.         return 0;
  6986.       }
  6987.     }
  6988.     return 0;
  6989. }
  6990. static void *
  6991. SymInsert(void *tbl, void *key, void *value, int datsiz)
  6992. {
  6993. NodePS node;
  6994.  
  6995.     if(tbl && key)
  6996.     {
  6997.       if(TBL->nbins)
  6998.       {/* hashed dictionary */
  6999.       KEY cat;
  7000.       NodePS *binp;
  7001.         hash(key, &cat);
  7002.         node = new_Snode(tbl, 0);
  7003.         TBL->lastbin = cat.hv % TBL->nbins;
  7004.         TBL->lastptr = node;
  7005.         binp = &TBL->bins[TBL->lastbin];
  7006.         if(value && datsiz)
  7007.           memcpy(node, value, MIN(datsiz,16));
  7008.         node->key[0] = cat.k[0];
  7009.         node->key[1] = cat.k[1];
  7010.         node->fptr[0] = *binp;
  7011.         *binp = node;
  7012.         return node;
  7013.       }
  7014.       else
  7015.       {/* sorted dictionary */
  7016.       int level;
  7017.       NodePS update[SYM_MAXLEVEL+1];
  7018.  
  7019.         node = TBL->header;
  7020.         for (level = TBL->level; level >= 0; level--)
  7021.         {
  7022.           while ( KEYLT(node->fptr[level]->key,((unsigned long*)key)) )
  7023.             node = node->fptr[level];
  7024.           update[level] = node;
  7025.         }
  7026.  
  7027.         level = getSlevel(tbl);
  7028.  
  7029.         while(TBL->level < level)
  7030.             update[++TBL->level] = TBL->header;
  7031.  
  7032.         node = new_Snode(tbl, level);
  7033.  
  7034.         if(value && datsiz)
  7035.           memcpy(node, value, MIN(datsiz,16));
  7036.         node->key[0] = ((unsigned long*)key)[0];
  7037.         node->key[1] = ((unsigned long*)key)[1];
  7038.  
  7039.         while(level >= 0)
  7040.         {
  7041.             node->fptr[level] = update[level]->fptr[level];
  7042.             update[level]->fptr[level] = node;
  7043.             level--;
  7044.         }
  7045.         TBL->lastptr = node;
  7046.         return node;
  7047.      }
  7048.     }
  7049.     return 0;
  7050. }
  7051. static int
  7052. StringFind(void *tbl, char *string, void *result)
  7053. {
  7054. unsigned long key[2];
  7055. struct {
  7056.     char *symname;
  7057. } *valp;
  7058.  
  7059.     key[0] = 0;
  7060.     key[1] = 0;
  7061.     sym_hash(key, string);
  7062.  
  7063.     if(SymFind(tbl, key, &valp))
  7064.     {
  7065.     unsigned long *key1;
  7066.         do {
  7067.             if(!strcmp(string, valp->symname))
  7068.             {
  7069.                 if(result)
  7070.                     *((void**)result) = valp;
  7071.                 return 1;
  7072.             }
  7073.             /* Check duplicates */
  7074.             if(!SymNext(tbl))
  7075.                 break;
  7076.             SymKey(tbl, &key1);
  7077.             SymValue(tbl, &valp);
  7078.         } while(KEYEQ(key, key1));
  7079.     }
  7080.     return 0;
  7081. }
  7082. static int
  7083. StringInsert(void *tbl, char *string, void *result)
  7084. {
  7085. unsigned long key[2];
  7086. struct {
  7087.     char *symname;
  7088. } *valp;
  7089.  
  7090.     key[0] = 0;
  7091.     key[1] = 0;
  7092.     sym_hash(key, string);
  7093.     if(SymFind(tbl, key, &valp))
  7094.     {/* hash keys match */
  7095.     unsigned long *key1;
  7096.         do {
  7097.             if(!strcmp(string, valp->symname))
  7098.             {
  7099.                 if(result)
  7100.                     *((void**)result) = valp;
  7101.                 return 1;
  7102.             }
  7103.             /* Check duplicates */
  7104.             if(!SymNext(tbl))
  7105.                 break;
  7106.             SymKey(tbl, &key1);
  7107.             SymValue(tbl, &valp);
  7108.         } while(KEYEQ(key, key1));
  7109.     }
  7110.     /* NOMATCH */
  7111.     valp = SymInsert(tbl, key, &string, 4);
  7112.     if(result)
  7113.         *((void**)result) = valp;
  7114.     return 0;
  7115. }
  7116. static void
  7117. SymDelete(void *tbl, void *key)
  7118. {
  7119. NodePS node;
  7120.  
  7121.     if(tbl && key)
  7122.     {
  7123.       if(TBL->nbins)
  7124.       {/* hashed dictionary */
  7125.       KEY cat;
  7126.       NodePS *binp;
  7127.       NodePS prev = 0;
  7128.  
  7129.         hash(key, &cat);
  7130.         binp = &TBL->bins[cat.hv % TBL->nbins];
  7131.         if((node = *binp))
  7132.         {
  7133.             do {
  7134.                 if(        node->key[0] == cat.k[0]
  7135.                     &&    node->key[0] == cat.k[1])
  7136.                 {
  7137.                     if(prev)
  7138.                         prev->fptr[0] = node->fptr[0];
  7139.                     else
  7140.                         *binp = node->fptr[0];
  7141.  
  7142.                     free_Snode(tbl, node);
  7143.                     if(TBL->lastptr == node)
  7144.                     {
  7145.                         TBL->lastptr = 0;
  7146.                         TBL->lastbin = TBL->nbins;
  7147.                     }
  7148.                     return;
  7149.                 }
  7150.                 prev = node;
  7151.              } while((node = node->fptr[0]));
  7152.         }
  7153.       }
  7154.       else
  7155.       {/* sorted dictionary */
  7156.       int level;
  7157.       NodePS update[SYM_MAXLEVEL+1];
  7158.  
  7159.         node = TBL->header;
  7160.         for(level = TBL->level; level >= 0; level--)
  7161.         {
  7162.           while ( KEYLT(node->fptr[level]->key, ((unsigned long*)key)) )
  7163.             node = node->fptr[level];
  7164.           update[level] = node;
  7165.         }
  7166.         node = node->fptr[0];
  7167.  
  7168.         if( KEYEQ(node->key, ((unsigned long*)key)) )
  7169.         {
  7170.             for(level = 0; level <= TBL->level; level++)
  7171.             {
  7172.                 if (update[level]->fptr[level] == node)
  7173.                      update[level]->fptr[level] = node->fptr[level];
  7174.                 else break;
  7175.             }
  7176.  
  7177.             while((TBL->level > 0) && (TBL->header->fptr[TBL->level] == _NNIL))
  7178.                 TBL->level--;
  7179.  
  7180.             if(TBL->lastptr == node)
  7181.                 TBL->lastptr = 0;
  7182.             free_Snode(tbl, node);
  7183.         }
  7184.       }
  7185.     }
  7186. }
  7187. static int
  7188. SymHead(void *tbl)
  7189. {/* Set up for sequential access */
  7190. int nbins;
  7191.  
  7192.     if(tbl)
  7193.     {
  7194.       if((nbins = TBL->nbins))
  7195.       {/* hashed dictionary */
  7196.       NodePS node;
  7197.       int i;
  7198.         TBL->lastptr = 0;
  7199.         for(i = 0; i < nbins; ++i)
  7200.         {
  7201.             if( (node = TBL->bins[i]) != 0)
  7202.             {
  7203.                 TBL->lastbin = i;
  7204.                 return 1;
  7205.             }
  7206.         }
  7207.         TBL->lastbin = nbins;
  7208.         return 0; /* empty */
  7209.       }
  7210.       else
  7211.       {/* sorted dictionary */
  7212.         TBL->lastptr = TBL->header;
  7213.         return (TBL->lastptr->fptr[0] == _NNIL) ? 0 : 1;
  7214.       }
  7215.     }
  7216.     return 0;
  7217. }
  7218. static int
  7219. SymNext(void *tbl)
  7220. {/* Move to next sequential entry */
  7221. int nbins;
  7222.  
  7223.     if(tbl)
  7224.     {
  7225.       if((nbins = TBL->nbins))
  7226.       {/* hashed dictionary */
  7227.         if(TBL->lastptr && ((TBL->lastptr = TBL->lastptr->fptr[0])))
  7228.             return 1;
  7229.         else
  7230.         {
  7231.         int i;
  7232.             for(i = TBL->lastbin; i < nbins; ++i)
  7233.             {
  7234.                 if((TBL->lastptr = TBL->bins[i]) != 0)
  7235.                 {
  7236.                     TBL->lastbin = i+1;
  7237.                     return 1;
  7238.                 }
  7239.             }
  7240.             return 0;
  7241.         }
  7242.       }
  7243.       else
  7244.       {/* sorted dictionary */
  7245.         if(TBL->lastptr)
  7246.         {
  7247.             if(TBL->lastptr != _NNIL)
  7248.                 TBL->lastptr = TBL->lastptr->fptr[0];
  7249.             return (TBL->lastptr == _NNIL) ? 0 : 1;
  7250.         }
  7251.       }
  7252.     }
  7253.     return 0;
  7254. }
  7255. static void
  7256. SymGetMark(void *tbl, void *markptr)
  7257. {
  7258.     if(tbl && markptr)
  7259.     {
  7260.         ((long*)markptr)[0] = TBL->lastbin;
  7261.         ((long*)markptr)[1] = (long)TBL->lastptr;
  7262.     }
  7263. }
  7264. static int
  7265. SymMarkNext(void *tbl, void *mark)
  7266. {/* Mark current position, and move to next sequential entry */
  7267.     SymGetMark(tbl, mark);
  7268.     return SymNext(tbl);
  7269. }
  7270. static void
  7271. SymSetMark(void *tbl, void *markptr)
  7272. {
  7273.     if(tbl && markptr)
  7274.     {
  7275.         TBL->lastbin = ((long*)markptr)[0];
  7276.         TBL->lastptr = (NodePS)((long*)markptr)[1];
  7277.     }
  7278. }
  7279. static void
  7280. SymKey(void *tbl, void *keyptr)
  7281. {/* Retrieve key info pointer for current spot */
  7282.  
  7283.     if(tbl && keyptr && TBL->lastptr)
  7284.         *((unsigned long**)keyptr) = &TBL->lastptr->key[0];
  7285. }
  7286. static void
  7287. SymValue(void *tbl, void *datptr)
  7288. {/* Retrieve value pointer for current spot */
  7289.  
  7290.     if(tbl && datptr && TBL->lastptr)
  7291.         *((unsigned long**)datptr) = &TBL->lastptr->value[0];
  7292. }
  7293.  
  7294. /* ==================== END SYMBOL TABLE HANDLERS ========================== */
  7295.  
  7296. /* ========================== OPTIMIZATION ================================= */
  7297. static int
  7298. forward(unsigned char *p)
  7299. {
  7300. unsigned char *next;
  7301.  
  7302.     do {
  7303.         next = (void*)((Pop)p)->next;
  7304.         while(        *next == 0
  7305.                 ||    *next == lineop
  7306.                 ||    *next == labelop)
  7307.             next = (void*)((Pop)next)->next;
  7308.  
  7309.         if(*next == endop)
  7310.         {
  7311.             if(*p == *(next+8))
  7312.             {
  7313.                 *p = 0;
  7314.                 *next = 0;
  7315.                 return 1;
  7316.             }
  7317.             return 0;
  7318.         }
  7319.     } while(forward(next));
  7320.  
  7321.     return 0;
  7322. }
  7323. static void
  7324. eliminate_extraneous_infops(Piv iv, int level)
  7325. {
  7326. Pafile pf;
  7327. unsigned char *p;
  7328. int i;
  7329.     for(i = 0; i < iv->numfiles; ++i)
  7330.     {
  7331.         iv->filenum = i;
  7332.         pf = iv->files[i];
  7333.         if(!(p = pf->prog_p))
  7334.             continue;
  7335.         if(pf->header_p->hdr.opt_level >= level)
  7336.             continue;
  7337.         pf->header_p->hdr.opt_level = level;
  7338.         while(*p != endfileop)
  7339.         {
  7340.             switch(*p)
  7341.             {
  7342.                 case    unopop:
  7343.                 case    arrayelemop:
  7344.                 case    ptrelemop:
  7345.                 case    strelemop:
  7346.                 case    ptrdimsop:
  7347.                 case    arraydimsop:
  7348.                     forward(p);
  7349.                     break;
  7350.             }
  7351.             p = POP->next;
  7352.         }
  7353.     }
  7354. }
  7355. static void
  7356. clean_temps(Piv iv)
  7357. {
  7358. long *key;
  7359. long *val;
  7360. long hitemp = iv->first_temp & 0xffff0000;
  7361.  
  7362.     if(iv->temps_written == 0)
  7363.         return;
  7364.  
  7365.     if(SymHead(iv->tmptbl))
  7366.     {
  7367.         while(SymNext(iv->tmptbl))
  7368.         {
  7369.             SymKey(iv->tmptbl, &key);
  7370.  
  7371.             if((key[0] & 0xffff0000) == hitemp)
  7372.             {
  7373.             char *ptr;
  7374.             long saveit;
  7375.  
  7376.                 SymValue(iv->tmptbl, &val);
  7377.                 saveit = val[1];
  7378.                 ptr = (void*)val[0];
  7379.                 val[0] = 0;    /* allow reuse of this slot */
  7380.                 val[1] = 0;
  7381.  
  7382.                 while(ptr)
  7383.                 {
  7384.                 void *nptr = (void*)((PopT)ptr)->tmpnum;
  7385.                     ((PopT)ptr)->tmpnum = key[0];
  7386.  
  7387.                     if(!saveit)
  7388.                     {
  7389.                     unsigned char *p = ptr-8;                    
  7390.                     unsigned char op = *p;
  7391.  
  7392.                         *p = 0;                        
  7393.                         ++iv->killop;
  7394.                         if(op == duptmpop)
  7395.                         {/* special test for post increment */
  7396.                             p = POP->next;
  7397.                             p = POP->next;
  7398.                             if(*p == grabop)
  7399.                                 *p = 0;
  7400.                         }
  7401.                     }
  7402.                     ptr = nptr;
  7403.                 }
  7404.             }
  7405.         }
  7406.         if(!hitemp)
  7407.             iv->temps_written = 0;
  7408.     }
  7409. }
  7410.  
  7411. static void
  7412. read_temp(Piv iv, PopT ptr, unsigned long last)
  7413. {
  7414. unsigned long key[2];
  7415. long *result;
  7416.  
  7417.     if(last == ptr->tmpnum)
  7418.         return;
  7419.  
  7420.     key[0] = ptr->tmpnum;
  7421.     key[1] = 0;
  7422.  
  7423.     if(SymFind(iv->tmptbl, key, &result))
  7424.     {
  7425.         result[1] = 1;
  7426.     }
  7427.     else PERROR(pName ":Syserr: read temp %d not found\n", key[0]);
  7428. }
  7429. static int 
  7430. reading_self(unsigned char *p, long tmpnum)
  7431. {
  7432.     if((p[2]&0xe0) == OPTEMP || (p[2]&0xe0) == OPRET)
  7433.     {
  7434.           return 1;
  7435.     }
  7436.     if((p[3]&0xe0) == OPTEMP || (p[3]&0xe0) == OPRET)
  7437.     {
  7438.         return 1;
  7439.     }
  7440.     return 0;
  7441. }
  7442. static long
  7443. write_temp(Piv iv, PopT ptr, unsigned char opcode)
  7444. {
  7445. long key[2];
  7446. long val[2];
  7447. long *result;
  7448. long hitemp = ptr->tmpnum & 0xffff0000;
  7449.  
  7450.     if(ptr->atype & A_MEMADDR && opcode < duptmpop)
  7451.     {/* actually reading from this destination slot */
  7452.         read_temp(iv, ptr, 0);
  7453.         return 0;
  7454.     }
  7455.  
  7456.     if(hitemp > (iv->first_temp & 0xffff0000))
  7457.     {/* Inner block, CompoundExp or NestedFunc */
  7458.         iv->first_temp = hitemp + 1;
  7459.     }
  7460.     else if(hitemp < (iv->first_temp & 0xffff0000))
  7461.     {/* Exit inner block */
  7462.         if(!reading_self(((char*)ptr)-8, ptr->tmpnum))
  7463.         {
  7464.             while(hitemp < (iv->first_temp & 0xffff0000))
  7465.             {
  7466.                 clean_temps(iv);
  7467.                 iv->first_temp -= 0x00010000;
  7468.             }
  7469.         }
  7470.     }
  7471.     if(ptr->tmpnum == iv->first_temp)
  7472.     {
  7473.         if(!reading_self(((char*)ptr)-8, ptr->tmpnum))
  7474.             clean_temps(iv);
  7475.     }
  7476.     ++iv->temps_written;
  7477.     key[0] = ptr->tmpnum;
  7478.     key[1] = 0;
  7479.  
  7480.     if(SymFind(iv->tmptbl, key, &result))
  7481.     {
  7482.     PopT optr = (PopT)result[0];
  7483.         result[0] = (long)ptr;
  7484.         ptr->tmpnum = (long)optr;
  7485.     }
  7486.     else
  7487.     {
  7488.         val[0] = (long)ptr;
  7489.         val[1] = 0;
  7490.  
  7491.         SymInsert(iv->tmptbl, key, val, 8);
  7492.         ptr->tmpnum = 0;
  7493.     }
  7494.     return key[0];
  7495. }
  7496. static void
  7497. eliminate_unused_temps(Piv iv, int level)
  7498. {
  7499. Pafile pf;
  7500. unsigned char *p;
  7501. int i;
  7502. long last_write;
  7503.  
  7504.     iv->tmptbl = NewSymTable(iv->category, 111);
  7505.     for(i = 0; i < iv->numfiles; ++i)
  7506.     {
  7507.         iv->filenum = i;
  7508.         pf = iv->files[i];
  7509.         if(pf->header_p->hdr.opt_level >= level)
  7510.             continue;
  7511.         pf->header_p->hdr.opt_level = level;
  7512. rekill:
  7513.         if(!(p = pf->prog_p))
  7514.             continue;
  7515.         iv->first_temp = 1;
  7516.         iv->temps_written = 0;
  7517.         iv->killop = 0;
  7518.         while(*p != endfileop)
  7519.         {
  7520.             while(*p < labelop)
  7521.             {
  7522.                 if(*p == truthop)
  7523.                 {/* truthops of single chars are unnecessary */
  7524.                   if((p[2]&0xe0) == OPTEMP)
  7525.                   {
  7526.                     if(((PopT)(p+20))->dsize == 1)
  7527.                     {
  7528.                         if(((PopT)(p+8))->tmpnum == ((PopT)(p+20))->tmpnum)
  7529.                         {
  7530.                             if(!(((PopT)(p+20))->atype & A_MEMADDR))
  7531.                                 *p = 0;
  7532.                             break;
  7533.                         }
  7534.                         else
  7535.                         {/* may be needed for code generation */
  7536.                             *p = aliastmpop;
  7537.                         }
  7538.                     }
  7539.                   }
  7540.                 }
  7541.                 if(*p)
  7542.                 {
  7543.                     if(        *p == jmptrueop 
  7544.                         ||  *p == jmpfalseop
  7545.                         ||    *p == ljmptrueop
  7546.                         ||    *p == ljmpfalseop)
  7547.                         read_temp(iv,(PopT)(p+4), 0);
  7548.                     if(*p == retdataop)
  7549.                     {
  7550.                         read_temp(iv, (PopT)p, 0);
  7551.                     }
  7552.                     else
  7553.                     {
  7554.                         last_write = 0;
  7555.                         if((p[1]&0xe0) == OPTEMP)
  7556.                             last_write = write_temp(iv, (PopT)(p+8), *p);
  7557.                         if((p[2]&0xe0) == OPTEMP || (p[2]&0xe0) == OPRET)
  7558.                             read_temp(iv, (PopT)((p+8+(p[1]&0x1f))), last_write);
  7559.                         if((p[3]&0xe0) == OPTEMP || (p[3]&0xe0) == OPRET)
  7560.                             read_temp(iv, (PopT)((p+8+(p[1]&0x1f))+(p[2]&0x1f)), last_write);
  7561.                     }
  7562.                 }
  7563.                 break;
  7564.             }
  7565.             p = POP->next;
  7566.         }
  7567.         do {
  7568.             clean_temps(iv);
  7569.             iv->first_temp -= 0x00010000;
  7570.         } while(iv->first_temp > 0);
  7571.         if(iv->killop)
  7572.         {
  7573.             goto rekill;
  7574.         }
  7575.     }
  7576. }
  7577. static void
  7578. retarget_jmps(Piv iv, int level)
  7579. {
  7580. Pafile pf;
  7581. unsigned char *p;
  7582. int i;
  7583.     for(i = 0; i < iv->numfiles; ++i)
  7584.     {
  7585.         iv->filenum = i;
  7586.         pf = iv->files[i];
  7587.         if(!(p = pf->prog_p))
  7588.             continue;
  7589.         if(pf->header_p->hdr.opt_level >= level)
  7590.             continue;
  7591.     }
  7592. }
  7593.  
  7594.  
  7595. static void
  7596. optimize(Piv iv)
  7597. {
  7598.     eliminate_extraneous_infops(iv, 50);
  7599.     eliminate_unused_temps(iv, 51);
  7600.     retarget_jmps(iv, 52);
  7601. }
  7602. /* ========================== END OPTIMIZATION ============================= */
  7603. /* ====================== BASIC INPUT FILE PROCESSING ====================== */
  7604. static long
  7605. label_insert(Piv iv, long label, int filenum, unsigned char *p)
  7606. {
  7607. long *result;
  7608. struct {
  7609.     long k1;
  7610.     long k2;
  7611. } key;
  7612.  
  7613. struct {
  7614.     long newlabel;
  7615. } val;
  7616.  
  7617.     key.k1 = label;
  7618.     key.k2 = filenum;
  7619.  
  7620.     /* check for duplicate label -- they happen */
  7621.     if(SymFind(iv->labeltbl, &key, &result))
  7622.     {
  7623.         if(*p == labelop)
  7624.             *p = 0; /* kill the instruction */
  7625.         return 0;
  7626.     }
  7627.  
  7628.     val.newlabel = ++iv->lastlabel;
  7629.     SymInsert(iv->labeltbl, &key, &val, 4);
  7630.  
  7631. #if REALLY_NEED_OFFSETS
  7632.     key.k1 = val.newlabel;
  7633.     val.newlabel = -1;
  7634.     SymInsert(iv->newlabeltbl, &key, &val, 4);
  7635. #endif
  7636.  
  7637.     return iv->lastlabel;
  7638. }
  7639. static long
  7640. label_find(Piv iv, long label, int filenum)
  7641. {
  7642. struct {
  7643.     long k1;
  7644.     long k2;
  7645. } key;
  7646.  
  7647. long *result;
  7648.  
  7649.     key.k1 = label;
  7650.     key.k2 = filenum;
  7651.  
  7652.     if(SymFind(iv->labeltbl, &key, &result))
  7653.         return *result;
  7654.     else
  7655.         return 0;
  7656. }
  7657. #if REALLY_NEED_OFFSETS
  7658. static void
  7659. newlabel_insert(Piv iv, long label)
  7660. {
  7661. struct {
  7662.     long k1;
  7663.     long k2;
  7664. } key;
  7665.  
  7666. long *result;
  7667.  
  7668.     key.k1 = label;
  7669.     key.k2 = iv->filenum;
  7670.     if(SymFind(iv->newlabeltbl, &key, &result))
  7671.     {
  7672.         *result = iv->out_offset + iv->func_offset;
  7673.     }
  7674.     else PERROR(pName ":Syserr: Label %d not found\n", label);
  7675. }
  7676. static long
  7677. newlabel_fix(Piv iv, long label)
  7678. {
  7679.     if(label)
  7680.     {
  7681.     struct {
  7682.         long k1;
  7683.         long k2;
  7684.     } key ;
  7685.  
  7686.     long *val;
  7687.  
  7688.         key.k1 = label;
  7689.         key.k2 = iv->filenum;
  7690.         if(SymFind(iv->newlabeltbl, &key, &val))
  7691.         {
  7692.             return val[0];
  7693.         }
  7694.     }
  7695.     return label;
  7696. }
  7697. #endif /* REALLY_NEED_OFFSETS */
  7698.  
  7699. static void
  7700. extern_insert(Piv iv, unsigned char *p, int filenum)
  7701. {
  7702. struct {
  7703.     short k1;
  7704.     short k2;
  7705.     long k3;
  7706. } key;
  7707. struct {
  7708.     unsigned char *p;
  7709. } val;
  7710.  
  7711.     key.k1 = GS(POPI->s.symnum);
  7712.     key.k2 = filenum;
  7713.     key.k3 = 0;
  7714.  
  7715.     val.p = p;
  7716.     SymInsert(iv->extrntbl, &key, &val, 4);
  7717. }
  7718. static void
  7719. reloc_insert(Piv iv, int fileno, unsigned char *p)
  7720. {
  7721. struct _rkey key;
  7722. struct _rval val;
  7723.  
  7724.     key.spot = GL(POPI->reloc.spot);    /* reloc target offset */
  7725.     key.fileno = (short)fileno;            /* fileno */
  7726.     key.opcode = *p;                    /* opcode */
  7727.     key.rsize = GL(POPI->reloc.rsize);    /* reloc size */
  7728.  
  7729.     val.p = p;                                /* pointer to input buffer */
  7730.     val.base = (void*)GL(POPI->reloc.base);    /* base of data object pointed to */
  7731.     val.offset = GL(POPI->reloc.offset);    /* offset to be added to base */
  7732.     val.rsym = GS(POPI->reloc.rsym);        /* symbol number if external */
  7733.     SymInsert(iv->reloctbl, &key, &val, 14);
  7734. }
  7735. static void
  7736. data_insert(void *tbl, unsigned long offset,
  7737.             unsigned long size, void *p, void *prevp)
  7738. {
  7739. static long locid = 1;
  7740. struct {
  7741.     unsigned long k1;
  7742.     long k2;
  7743. } key;
  7744. struct {
  7745.     unsigned long size;
  7746.     void *p;
  7747.     void *prevp;
  7748.     long locid;
  7749. } val;
  7750. unsigned char opcode, prevopcode = 0;
  7751.  
  7752.     key.k1 = offset;
  7753.     key.k2 = 0;
  7754.  
  7755.     val.size = size;
  7756.     val.p = p;
  7757.     val.prevp = prevp;
  7758.     val.locid = 0;
  7759.     opcode = *((unsigned char *)p);
  7760.     if(prevp)
  7761.     {
  7762.         prevopcode = *((unsigned char*)prevp);
  7763.         if(        prevopcode != glodatop
  7764.             &&    prevopcode != glofuncop
  7765.             &&    prevopcode != extfuncop
  7766.             &&    prevopcode != globssop)
  7767.         {
  7768.             val.locid = locid++;
  7769.         }    
  7770.     }
  7771.     SymInsert(tbl, &key, &val, sizeof(val));
  7772. }
  7773.  
  7774. static void
  7775. global_insert(Piv iv, Pafile pf, unsigned char *p)
  7776. {
  7777. unsigned long key[2];
  7778. struct _gloval val;
  7779. PopI pp;
  7780. unsigned char opcode = *p;
  7781.  
  7782.     if(opcode == extvarop)
  7783.         pp = POPI;
  7784.     else
  7785.         pp = (PopI)(POP->next+8);
  7786.  
  7787.     key[0] = 0;
  7788.     key[1] = 0;
  7789.  
  7790.     val.symnum = GS(pp->s.symnum);
  7791.     val.symname = pf->symaddr[val.symnum];
  7792.     val.p = p;
  7793.     val.pf = pf;
  7794.     if(val.symnum < 0 || val.symnum >= pf->numsyms)
  7795.     {
  7796.         PERROR(pName ":Syserr: BAD SYMNUM=%d opcode=%d\n", val.symnum, opcode);
  7797.     }
  7798.     sym_hash(key, val.symname);
  7799.  
  7800.     /* Duplicate entries are allowed */
  7801.     SymInsert(iv->gbltbl, key, &val, sizeof(val));
  7802. }
  7803.  
  7804. static int
  7805. setup_nodelinks(Piv iv, char *infile_name, void *inbuf, int insize)
  7806. {
  7807. unsigned char *p = inbuf;
  7808. unsigned char *endbuf = inbuf+insize;
  7809. Pafile pf=0;
  7810. int lastline = 0;
  7811. unsigned char *funcp;
  7812. unsigned char *nfuncp;
  7813.  
  7814.     while(p < endbuf && *p != endallop)
  7815.     {
  7816.     unsigned char *q = p;
  7817.         if(iv->debug >= '5')
  7818.         {
  7819.          cfeprintf("OP(%u '%s' p=%p line=%d)\n", *p, oxgenops[*p], p, lastline);
  7820.         }
  7821.         switch(*p)
  7822.         {
  7823.             case headerop:
  7824.                 if(iv->numfiles >= 1024) {
  7825.                     PERROR(pName ": Sorry, too many files\n");
  7826.                 }
  7827.                 pf = iv->files[iv->numfiles] = 
  7828.                     Ccalloc(iv->category, 1, sizeof(struct _afile));
  7829.                 pf->filenum = iv->numfiles++;
  7830.                 pf->file_p = p;
  7831.                 pf->header_p = POPI;
  7832.                 if(iv->strip)
  7833.                 {/* Gonna strip declarations and line numbers */
  7834.                     pf->header_p->hdr.target_debugger = 0;
  7835.                 }
  7836.                 break;
  7837.  
  7838.             case dataop:
  7839.                 pf->size_p = POPI;
  7840.                 pf->thunk_offset = GL(POPI->dat.thunk_offset);
  7841.                 pf->bss_offset = GL(POPI->dat.bss_offset);
  7842.                 break;
  7843.             case gfuncdefop:
  7844.             case sfuncdefop:
  7845.                 if(pf->prog_p == 0)
  7846.                     pf->prog_p = p;
  7847.                 funcp = p;
  7848.                 break;
  7849.             case funcexitop:
  7850.                 PS(((PopI)(funcp+8))->funcdef.tempmax) = GL(POPI->funcexit.tempmax);
  7851.                 break;
  7852.             case nestedfuncdefop:
  7853.                 nfuncp = p;
  7854.                 break;
  7855.             case nestedfuncexitop:
  7856.                 PS(((PopI)(nfuncp+8))->funcdef.tempmax) = GL(POPI->funcexit.tempmax);
  7857.                 break;
  7858.             case segdefop:
  7859.                 if(pf->seg_p == 0)
  7860.                     pf->seg_p = p;
  7861.                 pf->numsegs += 1;
  7862.                 iv->numsegs += 1;
  7863.                 break;
  7864.             case lineop:
  7865.                 lastline = GL( POPI->line.line );
  7866.                 if(iv->strip)
  7867.                     *p = 0;        /* strip line numbers */
  7868.                 break;
  7869.             case declop:
  7870.                 if(iv->strip)
  7871.                 {/* strip declarations */
  7872.                     do {
  7873.                         *p = 0;
  7874.                         q += (long)GL(POP->next);
  7875.                         POP->next = q;
  7876.                         p = q;
  7877.                     } while(*p != endop);                    
  7878.                     *p = 0;
  7879.                 }
  7880.                 else
  7881.                 {
  7882.                     if(pf->decl_p == 0)
  7883.                         pf->decl_p = p;
  7884.                     pf->numdecls += 1;
  7885.                     iv->numdecls += 1;
  7886.                 }
  7887.                 break;
  7888.             case switchidop:
  7889.                 if(pf->switch_p == 0)
  7890.                     pf->switch_p = p;
  7891.                 break;
  7892.             case labelop:
  7893.                 PL( POP->data ) = 
  7894.                         label_insert(iv, GL( POP->data ), pf->filenum, p);
  7895.                 break;            
  7896.             case symbop:
  7897.                 pf->numsyms = GL(POP->data);
  7898.                 iv->numsyms += pf->numsyms;
  7899.                 break;
  7900.             case symblockop:
  7901.                 pf->symtext_p = p + 12;
  7902.                 goto blka;
  7903.             case stringblockop:
  7904.             case datablockop:
  7905.             case mallocblockop:
  7906.             case thunkblockop:
  7907.             {
  7908.             long size;
  7909.                 if(pf->data_p == 0)
  7910.                     pf->data_p = p;
  7911. blka:
  7912.                 size = GL(POP->data);
  7913.                 q += size+((4-(size&3))&3);
  7914.                 break;
  7915.             }
  7916.             case glofuncop:
  7917.             case extfuncop:
  7918.             case glodatop:
  7919.             case globssop:
  7920.             case extvarop:
  7921.             case bssblockop:
  7922.                 if(pf->data_p == 0)
  7923.                     pf->data_p = p;
  7924.                 break;
  7925.             case maxtempop:
  7926.                 pf->maxtemp = GL(POP->data);
  7927.                 pf->maxtempclass = GL(POP->data1);
  7928.                 pf->maxtemp_p = p;
  7929.                 break;
  7930.         }
  7931.         q += (long)GL(POP->next);
  7932.         POP->next = q;
  7933.         p = q;
  7934.     }
  7935.     if(*p != endallop)
  7936.     {
  7937.         PERROR(pName ": Malformed input file: %s\n", infile_name);
  7938.     }
  7939.     return 0;
  7940. }
  7941. static void
  7942. setup_syms_decls(Piv iv)
  7943. {
  7944. int i;
  7945.  
  7946.     for(i = 0; i < iv->numfiles; ++i)
  7947.     {
  7948.     int symnum = 0;
  7949.     Pafile pf = iv->files[i];
  7950.     unsigned char *p = pf->file_p;
  7951.     unsigned char *prevp = 0;
  7952.  
  7953.         pf->symaddr = Ccalloc(iv->category, sizeof(void*), pf->numsyms+1);
  7954.         pf->decladdr = Ccalloc(iv->category, sizeof(void*), pf->numdecls+1);
  7955.  
  7956.         while(*p != endfileop)
  7957.         {
  7958.             switch(*p)
  7959.             {
  7960.                 case    symoffsetop:
  7961.                   pf->symaddr[symnum] = pf->symtext_p + GL(POP->data);
  7962.                   ++symnum;
  7963.                   break;
  7964.  
  7965.                 case    declop:
  7966.                   pf->decladdr[GS(POPI->dcl.declnum)] = p;
  7967.                   break;
  7968.  
  7969.                 case    relocop:
  7970.                 case    extlocop:
  7971.                   ++pf->numrelocs;
  7972.                   reloc_insert(iv, i, p);
  7973.                   break;
  7974.  
  7975.                 case    glodatop:
  7976.                 case    globssop:
  7977.                 case    glofuncop:
  7978.                 case    extfuncop:
  7979.                   global_insert(iv, pf, p);
  7980.                   break;
  7981.  
  7982.                 case    extvarop:
  7983.                   extern_insert(iv, p, i);
  7984.                   global_insert(iv, pf, p);
  7985.                   break;
  7986.  
  7987.                 case    stringblockop:
  7988.                 case    datablockop:
  7989.                 case    mallocblockop:
  7990.                 case    thunkblockop:
  7991.                 case    bssblockop:
  7992.                   if(!pf->datatbl)
  7993.                     pf->datatbl = NewSymTable(iv->category, 0);  /* sorted */
  7994.  
  7995.                   data_insert(pf->datatbl,GL(DATI.offset),GL(DATI.size),
  7996.                               p, prevp);
  7997.                   if(*p == thunkblockop) {
  7998.                     PL(POP->data5) = label_find(iv, GL(POP->data5), i);
  7999.                   }
  8000.                   break;
  8001.  
  8002.                 case    jmploopop:
  8003.                 case    jmpcontinueop:
  8004.                 case    jmpbreakop:
  8005.                 case    jmpgotoop:
  8006.                 case    jmptrueop:
  8007.                 case    jmpfalseop:
  8008.                 case    ljmptrueop:
  8009.                 case    ljmpfalseop:
  8010.                 case    funcstartop:
  8011.                 case    funcstopop:
  8012.                 case    casevalop:
  8013.                 case    switchop:
  8014.                   PL(POP->data) = label_find(iv, GL(POP->data), i);
  8015.                   break;
  8016.             }
  8017.             prevp = p;
  8018.             p = POP->next;
  8019.         }
  8020.     }
  8021. }
  8022. static int
  8023. sym_insert(Piv iv, char *symname, int symnum)
  8024. {/* Used only for combining symbols in link phase */
  8025. struct {
  8026.     char *symname;
  8027.     int symnum;
  8028. } *valp;
  8029.  
  8030.     if(StringInsert(iv->symtbl, symname, &valp))
  8031.         return -valp->symnum;    /* MATCH */
  8032.     valp->symnum = symnum;
  8033.     return symnum;
  8034. }
  8035. static void
  8036. combine_syms_decls(Piv iv)
  8037. {
  8038. int i,j;
  8039. Pafile pf;
  8040. int numsyms;
  8041. int numdecls;
  8042.  
  8043.     /* COMBINE SYMBOLS */
  8044.     pf = iv->files[0];
  8045.     numsyms = pf->numsyms;
  8046.     pf->symtran = Ccalloc(iv->category, sizeof(short), pf->numsyms+1);
  8047.     memcpy(iv->symaddr, pf->symaddr, sizeof(void*) * numsyms);
  8048.  
  8049.  
  8050.     for(i = 0; i < numsyms; ++i)
  8051.     {/* file 0 */
  8052.         sym_insert(iv, pf->symaddr[i], i);
  8053.         pf->symtran[i] = i;
  8054.     }
  8055.     for(i = 1; i < iv->numfiles; ++i)
  8056.     {
  8057.     int start;
  8058.  
  8059.         pf = iv->files[i];
  8060.         pf->symtran = Ccalloc(iv->category, sizeof(short), pf->numsyms+1);
  8061.         if(pf->header_p->hdr.target_debugger)
  8062.             start = 1;
  8063.         else
  8064.             start = 3;
  8065.         for(j = start; j < pf->numsyms; ++j)
  8066.         {
  8067.         int k;
  8068.           if((k = sym_insert(iv, pf->symaddr[j], numsyms)) > 0)
  8069.           { /* new entry */
  8070.             iv->symaddr[numsyms++] = pf->symaddr[j];
  8071.             pf->symtran[j] = k;
  8072.           }
  8073.           else pf->symtran[j] = -k;
  8074.         }
  8075.     }
  8076.     iv->numsyms = numsyms;
  8077.  
  8078.     /* COMBINE DECLARATIONS */
  8079.     pf = iv->files[0];
  8080.     numdecls = pf->numdecls;
  8081.     pf->decltran = Ccalloc(iv->category, sizeof(short), numdecls+1);
  8082.     memcpy(iv->decladdr, pf->decladdr, sizeof(void*) * numdecls);
  8083.     for(i = 0; i < numdecls; ++i)
  8084.     {/* file 0 */
  8085.         pf->decltran[i] = i;
  8086.     }
  8087.     for(i = 1; i < iv->numfiles; ++i)
  8088.     {
  8089.         pf = iv->files[i];
  8090.         pf->decltran = Ccalloc(iv->category, sizeof(short), pf->numdecls+1);
  8091.         if(pf->numdecls < 21)
  8092.             continue;
  8093.         for(j = 1; j <= 21; ++j)
  8094.             pf->decltran[j] = j;
  8095.         for(j = 22; j < pf->numdecls; ++j) {
  8096.             iv->decladdr[numdecls] = pf->decladdr[j];
  8097.             pf->decltran[j] = numdecls++;
  8098.         }
  8099.     }
  8100.     iv->numdecls = numdecls;
  8101. }
  8102.  
  8103. static void
  8104. link_dups(Piv iv, int dupcnt, struct _gloval *valp[])
  8105. {
  8106. int i;
  8107. int vars[5] = {0,0,0,0,0};
  8108. unsigned long cdsize = 0;
  8109. unsigned long cdoffset = 0;
  8110. short cdfile = 0;
  8111. int cdnum = 0;
  8112. short segid = 0;
  8113.  
  8114. #define GDAT vars[0]
  8115. #define GBSS vars[1]
  8116. #define GFUNC vars[2]
  8117. #define EVAR vars[3]
  8118. #define EFUNC vars[4]
  8119.  
  8120.     /* Count the types of matches */
  8121.     for(i = 0; i <= dupcnt; ++i)
  8122.         vars[*(valp[i]->p) - glodatop] += 1;
  8123.  
  8124.     /* Check for errors */
  8125.     if(        GDAT > 1 
  8126.         ||    GFUNC > 1
  8127.         ||    (GFUNC && (GDAT || GBSS || EVAR))
  8128.         ||    (EFUNC && (GDAT || GBSS || EVAR)))
  8129.     {
  8130.         ++iv->errors;
  8131.         for(i = 0; i < dupcnt; ++i)
  8132.         {
  8133.             PWARN(pName ": Symbol `%s' multiply defined or mistyped.\n",
  8134.                   valp[i]->symname);
  8135.             PWARN(pName":  In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  8136.         }
  8137.         return;
  8138.     }
  8139.     if(EFUNC && GFUNC)
  8140.     {/* match up functions */
  8141.     Pop dp;
  8142.         for(i = 0; i <= dupcnt; ++i)
  8143.           if(*(valp[i]->p) == glofuncop)
  8144.             break;
  8145.         dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  8146.  
  8147.         cdoffset = GL(dp->data1);            /* save this offset */
  8148.         cdfile = valp[i]->pf->filenum;        /* save this file */
  8149.         for(i = 0; i <= dupcnt; ++i)
  8150.         {
  8151.           if(*(valp[i]->p) == extfuncop)
  8152.           {
  8153.             *(valp[i]->p) = 0;                    /* convert to nilop */
  8154.             dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  8155.  
  8156.             /* Kill the thunkblock */
  8157.             *((char*)dp) = 0;
  8158.             PL(dp->data4) = cdoffset;            /* use this offset for access */
  8159.             PS(((short*)dp)[1]) = cdfile;        /* fileno to unused slots */
  8160.           }
  8161.         }
  8162.     }
  8163.     else if(EFUNC)
  8164.     {/* multiple references to external function */
  8165.     Pop    dp = (Pop)((Pop)valp[0]->p)->next;    /* points to first thunkblockop */
  8166.  
  8167.         cdoffset = GL(dp->data1);            /* save first offset */
  8168.         cdfile = valp[0]->pf->filenum;        /* save first file */
  8169.         for(i = 1; i <= dupcnt; ++i)
  8170.         {/* Kill all thunkblocks except the first */
  8171.             *(valp[i]->p) = 0;                    /* convert to nilop */
  8172.             dp = (Pop)((Pop)valp[i]->p)->next;    /* points to thunkblockop */
  8173.             *((char*)dp) = 0;
  8174.             PL(dp->data4) = cdoffset;            /* use this offset for access */
  8175.             PS(((short*)dp)[1]) = cdfile;        /* fileno to unused slots */
  8176.         }
  8177.     }
  8178.     else if(GBSS)
  8179.     {/* comdefs */
  8180.     int multsize = 0;
  8181.  
  8182.         /* PICK THE BIGGEST GLOBAL BSS (comdef) */
  8183.         for(i = 0; i <= dupcnt; ++i)
  8184.         {
  8185.         Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to bssblockop */
  8186.           if((short)dp->data4 && segid == 0)
  8187.           {
  8188.             segid = (short)dp->data4;
  8189.           }
  8190.           else if((short)dp->data4 && (short)dp->data4 != segid)
  8191.           {
  8192.             ++iv->errors;
  8193.             PWARN(pName ": Variable `%s' defined in multiple segments.\n",
  8194.                 valp[i]->symname);
  8195.             PWARN(pName ":  In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  8196.           }
  8197.           if(*(valp[i]->p) == globssop)
  8198.           {
  8199.           long size = GL(dp->data);
  8200.             if(cdsize && size != cdsize)
  8201.                 multsize = 1;
  8202.             if(size > cdsize) {
  8203.                 cdsize = size;
  8204.                 cdoffset = GL(dp->data1);
  8205.                 cdfile = valp[i]->pf->filenum;
  8206.                 cdnum = i;
  8207.             }
  8208.           }
  8209.         }
  8210.         if(GDAT)
  8211.         {
  8212.           /* INITIALIZED DATA WILL ALWAYS OVERRIDE BSS */
  8213.           for(i = 0; i <= dupcnt; ++i)
  8214.           {
  8215.             if(*(valp[i]->p) == glodatop)
  8216.             {
  8217.             Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to datablockop */
  8218.             long size = GL(dp->data);
  8219.               if(cdsize && size != cdsize)
  8220.                   multsize = 1;
  8221.               if(size < cdsize)
  8222.               {
  8223.             ++iv->errors;
  8224.             PWARN(pName ": Initialized variable `%s' of size (%d)\n",
  8225.               valp[i]->symname, size);
  8226.             PWARN(pName ":  In file: `%s'\n",
  8227.               valp[i]->pf->symaddr[INFILE_SYMNUM]);
  8228.             PWARN(pName ":  Is incommensurate with common size (%d).\n",
  8229.               cdsize);
  8230.               }
  8231.               else
  8232.               {
  8233.                   cdsize = size;
  8234.                   cdoffset = GL(dp->data1);
  8235.                   cdfile = valp[i]->pf->filenum;
  8236.                   cdnum = i;
  8237.               }
  8238.             }
  8239.           }
  8240.         }
  8241.         if(multsize)
  8242.         {
  8243.           PWARN(pName ":warning: Common Variable `%s' has multiple sizes.\n",
  8244.               valp[0]->symname);
  8245.           for(i = 0; i <= dupcnt; ++i)
  8246.           {
  8247.           unsigned char opcode = *(valp[i]->p);
  8248.             if(opcode == globssop || opcode == glodatop)
  8249.             {
  8250.                 PWARN(pName ":warning:Size=%d in file: `%s'\n",
  8251.                   GL(((Pop)((Pop)valp[i]->p)->next)->data),
  8252.                   valp[i]->pf->symaddr[INFILE_SYMNUM]);
  8253.             }
  8254.           }
  8255.         }
  8256.         /* FINALLY, LINK COMMONS TO THE CHOSEN ONE */
  8257.         for(i = 0; i <= dupcnt; ++i)
  8258.         {
  8259.           if(i != cdnum && *(valp[i]->p) == globssop)
  8260.           {
  8261.           Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to bssblockop */
  8262.  
  8263.             *(valp[i]->p) = 0;            /* globssop becomes nilop */
  8264.             *((char*)dp) = 0;            /* bssblockop becomes nilop */
  8265.             PL(dp->data4) = cdoffset;    /* use this new offset for access */
  8266.             PS(((short*)dp)[1]) = cdfile;    /* put fileno in unused slots */
  8267.           }
  8268.         }
  8269.     }
  8270.     else if(GDAT)
  8271.     {
  8272.         for(i = 0; i <= dupcnt; ++i)
  8273.         {
  8274.         Pop dp = (Pop)((Pop)valp[i]->p)->next;    /* points to datablockop */
  8275.           if((short)dp->data4 && segid == 0)
  8276.           {
  8277.             segid = (short)dp->data4;
  8278.           }
  8279.           else if((short)dp->data4 && (short)dp->data4 != segid)
  8280.           {
  8281.             ++iv->errors;
  8282.             PWARN(pName ": Variable `%s' defined in multiple segments.\n",
  8283.                 valp[i]->symname);
  8284.             PWARN(pName ": In file: `%s'\n", valp[i]->pf->symaddr[INFILE_SYMNUM]);
  8285.           }
  8286.           if(*(valp[i]->p) == glodatop)
  8287.           {
  8288.             cdsize = GL(dp->data);
  8289.             cdoffset = GL(dp->data1);
  8290.             cdfile = valp[i]->pf->filenum;
  8291.             cdnum = i;
  8292.             break;
  8293.           }
  8294.         }
  8295.     }
  8296.     if(EVAR && (GDAT || GBSS))
  8297.     {/* match up variables */
  8298.         /* LINK EXTERNS TO THE CHOSEN ONE */
  8299.         for(i = 0; i <= dupcnt; ++i)
  8300.         {
  8301.           if(*(valp[i]->p) == extvarop)
  8302.           {
  8303.           Pop dp = (Pop)valp[i]->p;
  8304.  
  8305.             *((char*)dp) = 0;            /* extvarop becomes nilop */
  8306.             PL(dp->data1) = cdoffset;    /* use this new offset for access */
  8307.             PS(((short*)dp)[1]) = cdfile;    /* put fileno in unused slots */
  8308.             PS(dp->data4) = segid;
  8309.             break;
  8310.           }
  8311.         }
  8312.     }
  8313. #undef GDAT
  8314. #undef GBSS
  8315. #undef GFUNC
  8316. #undef EVAR
  8317. #undef EFUNC
  8318.  
  8319. }
  8320. static void
  8321. link_globals(Piv iv)
  8322. {
  8323.     if(SymHead(iv->gbltbl))
  8324.     {
  8325.     struct _gloval *valp[1024];    /* pointers to symtable value structs */
  8326.  
  8327.       /* Pass over the sorted symbol table and process duplicate entries */
  8328.       while(SymNext(iv->gbltbl))
  8329.       {
  8330.       unsigned long *key;
  8331.       long mark[2];                            /* Table position saver */
  8332.       int dupcnt = 0;
  8333.         SymKey(iv->gbltbl, &key);            /* Pointer to first key */
  8334.         SymValue(iv->gbltbl, &valp[0]);        /* Pointer to first value */
  8335.  
  8336.         while(SymMarkNext(iv->gbltbl, mark))
  8337.         {/* Look forward for duplicates */
  8338.         unsigned long *key1;
  8339.           SymKey(iv->gbltbl, &key1);                /* Pointer to next key */
  8340.           if(KEYEQ(key, key1))
  8341.           {/* Hashed keys match, check the strings */
  8342.             SymValue(iv->gbltbl, &valp[dupcnt+1]);    /* Pointer to next value */
  8343.             if(!strcmp(valp[dupcnt]->symname, valp[dupcnt+1]->symname))
  8344.             {/* Duplicate entry found */
  8345.                 ++dupcnt;
  8346.                 continue;
  8347.             }
  8348.           }
  8349.           break;
  8350.         }
  8351.         if(dupcnt > 0)
  8352.         {/* Process a collection of duplicate symbol names */
  8353.           link_dups(iv, dupcnt, valp);
  8354.         }
  8355.         SymSetMark(iv->gbltbl, mark);
  8356.  
  8357.       }/* END: while(SymNext) */
  8358.     }/* END: if(SymHead) */
  8359. }
  8360. static void
  8361. realloc_data(Piv iv)
  8362. {
  8363. int i;
  8364. Pafile pf;
  8365. unsigned char *p, *prevp;
  8366. unsigned long offset = 0;
  8367.  
  8368.     iv->datatbl = NewSymTable(iv->category, 0);     /* sorted table */
  8369.  
  8370.     for(i = 0; i < iv->numfiles; ++i)
  8371.     {
  8372.         pf = iv->files[i];
  8373.         p = pf->data_p;
  8374.         prevp = 0;
  8375.         while(*p != endfileop)
  8376.         {
  8377.             if(        *p == datablockop
  8378.                 ||    *p == mallocblockop
  8379.                 ||    *p == stringblockop)
  8380.             {
  8381.                 PL(POP->data1) = offset;                
  8382.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  8383.                 offset += GL(POP->data);
  8384.                 ROUNDUP(offset, 4);
  8385.             }
  8386.             prevp = p;
  8387.             p = POP->next;
  8388.         }
  8389.     }
  8390.     iv->thunk_offset = offset;
  8391.  
  8392.     for(i = 0; i < iv->numfiles; ++i)
  8393.     {
  8394.         pf = iv->files[i];
  8395.         p = pf->data_p;
  8396.         prevp = 0;
  8397.         while(*p != endfileop)
  8398.         {
  8399.             if(*p == thunkblockop)
  8400.             {
  8401.                 PL(POP->data1) = offset;
  8402.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  8403.                 offset += GL(POP->data);
  8404.                 ROUNDUP(offset, 4);
  8405.             }
  8406.             prevp = p;
  8407.             p = POP->next;
  8408.         }
  8409.     }
  8410.     iv->bss_offset = offset;
  8411.  
  8412.     for(i = 0; i < iv->numfiles; ++i)
  8413.     {
  8414.         pf = iv->files[i];
  8415.         p = pf->data_p;
  8416.         prevp = 0;
  8417.         while(*p != endfileop)
  8418.         {
  8419.             if(*p == bssblockop)
  8420.             {
  8421.                 PL(POP->data1) = offset;
  8422.                 data_insert(iv->datatbl, offset, GL(POP->data), p, prevp);
  8423.                 offset += GL(POP->data);
  8424.                 ROUNDUP(offset, 4);
  8425.             }
  8426.             prevp = p;
  8427.             p = POP->next;
  8428.         }
  8429.     }
  8430.     iv->total_size = offset;
  8431. }
  8432. static void
  8433. reset_data_relocs(Piv iv)
  8434. {/* Pass over initialized data and set new offsets in each relocatable slot */
  8435. struct _data {/* datatbl node */
  8436. /* value area 16 bytes */
  8437.     unsigned long size;
  8438.     unsigned char *p;
  8439.     unsigned char opcode;
  8440.     unsigned char unused[7];
  8441. /* key area 8 bytes */
  8442.     unsigned long offset;
  8443.     long unused1;
  8444. };
  8445.     /* PASS OVER ALL THE ENTRIES IN `reloctbl' */
  8446.     if(SymHead(iv->reloctbl))
  8447.     {
  8448.         while(SymNext(iv->reloctbl))
  8449.         {
  8450.         struct _rkey *kp;
  8451.         struct _rval *vp;
  8452.         struct _data *dp, *ndp;
  8453.         unsigned char *p;
  8454.         Pafile pf, npf;
  8455.         unsigned long object_base;
  8456.         int noset = 0;
  8457.  
  8458.           SymKey(iv->reloctbl, &kp);
  8459.           SymValue(iv->reloctbl, &vp);
  8460.           npf = pf = iv->files[kp->fileno];    /* pointer to file struct */
  8461.           p = vp->p;        /* pointer to relocop in input buffer */
  8462.  
  8463.           if(kp->opcode == extlocop)
  8464.           {/* External variable */
  8465.           short key[4];
  8466.           struct {
  8467.           unsigned char *p;    /* pointer to extvarop in input buffer */
  8468.           } *ep;
  8469.             key[0] = vp->rsym;         /* external symbol number */
  8470.             key[1] = pf->filenum;
  8471.             key[2] = 0;
  8472.             key[3] = 0;
  8473.  
  8474.             /* LOOK UP THE EXTERNAL SYMBOL */
  8475.             if(SymFind(iv->extrntbl, key, &ep) && *(ep->p) == 0)
  8476.             {/* symbol exists and the extvarop was filled in */
  8477.  
  8478.                 npf = iv->files[GS( ((short*)(ep->p))[1] )];
  8479.                 PL( POPI->reloc.base ) = GL( ((Pop)(ep->p))->data1 );
  8480.                 *p = relocop;    /* switch input file from `extlocop' */
  8481.             }
  8482.             else
  8483.             {/* Not found or not filled in, leave it alone */
  8484.                 noset = 1;
  8485.             }
  8486.           }
  8487.  
  8488.           /* RESET THE ENTRY IN THE INITIALIZED DATA SLOT */
  8489.           if(SymFindRange(pf->datatbl, &kp->spot, &dp))
  8490.           {/* This entry describes a block of data containg the reloc target */
  8491.           unsigned char *ip = dp->p;    /* points to input buffer */
  8492.           unsigned long extra = kp->spot - dp->offset; /* offset into data */
  8493.  
  8494.  
  8495.             /* Reset the relocop target in the input file */
  8496.  
  8497.             PL( POPI->reloc.spot ) = GL( ((PopI)(ip+8))->s.offset ) + extra;
  8498.             if(noset)
  8499.                 continue;
  8500.  
  8501.             if(kp->rsize == 4)
  8502.             {/* 32 bit relocation */
  8503.             unsigned long *lp;
  8504.  
  8505.                 lp = (unsigned long*)(ip+24+extra);    /* pointer to target */
  8506.                 object_base = GL( POPI->reloc.base );
  8507.  
  8508.                 /* Find the object that the target points to */
  8509. relink32:
  8510.                 if(SymFindRange(npf->datatbl, &object_base, &ndp))
  8511.                 {
  8512.                     if(*(ndp->p) == 0)
  8513.                     {/* The found object is a discarded thunkblock, relink */
  8514.                         npf = iv->files[GS( ((short*)(ndp->p))[1] )];
  8515.                         object_base = GL( ((Pop)(ndp->p))->data4 );
  8516.                         goto relink32;
  8517.                     }
  8518.                     else
  8519.                     {/* Use the new offset in the input file */
  8520.  
  8521.                         object_base = GL( ((Pop)(ndp->p))->data1 );
  8522.                     }
  8523.                     PL( POPI->reloc.base ) = object_base; /* the `relocop' */
  8524.                     PL(*lp) = object_base + GL( POPI->reloc.offset );/* data */
  8525.                     vp->base = lp;
  8526.                 }
  8527.                 else
  8528.                 {
  8529.                     ++iv->errors;
  8530.                     PWARN(pName ":syserr: 32 bit object at offset %d not found\n",object_base);
  8531.                 }
  8532.             }
  8533.             else if(kp->rsize == 2)
  8534.             {/* 16 bit relocation (MORE WORK NEEDED) */
  8535.             unsigned short *sp;
  8536.  
  8537.                 sp = (unsigned short*)(ip+24+extra);    /* pointer to target */
  8538.                 object_base = GL( POPI->reloc.base );
  8539. relink16:
  8540.                 if(SymFindRange(npf->datatbl, &object_base, &ndp))
  8541.                 {
  8542.                     if(*(ndp->p) == 0)
  8543.                     {/* The found object is a discarded thunkblock, relink */
  8544.                         npf = iv->files[GS( ((short*)(ndp->p))[1] )];
  8545.                         object_base = GL( ((Pop)(ndp->p))->data4 );
  8546.                         goto relink16;
  8547.                     }
  8548.                     else
  8549.                     {/* Use the new offset in the input file */
  8550.  
  8551.                         object_base = GL( ((Pop)(ndp->p))->data1 );
  8552.                     }
  8553.                     PL( POPI->reloc.base ) = object_base; /* the `relocop' */
  8554.                     PS(*sp) = object_base + GL( POPI->reloc.offset );/* data */
  8555.                 }
  8556.                 else
  8557.                 {
  8558.                     ++iv->errors;
  8559.                     PWARN(pName ":syserr: 16 bit object at offset %d not found\n", object_base);
  8560.                 }
  8561.  
  8562.             }
  8563.           }
  8564.           else /* !SymFindRange */
  8565.           {
  8566.             ++iv->errors;
  8567.             PWARN(pName ":syserr: reloc not found at %d in file %d\n", 
  8568.                     kp->spot, kp->fileno);
  8569.           }
  8570.         }/* END: While(SymNext) */
  8571.     }/* END: if(SymHead) */
  8572. }
  8573. static void
  8574. reset_offset(Piv iv, Pafile pf, PopA pa)
  8575. {/* All offsets are guaranteed to be inside objects */
  8576. struct _data {/* datatbl node */
  8577. /* value area 16 bytes */
  8578.     unsigned long size;
  8579.     unsigned char *p;
  8580.     unsigned long unused[2];
  8581. /* key area 8 bytes */
  8582.     unsigned long offset;
  8583.     long unused1;
  8584. };
  8585.  
  8586. unsigned long offset;
  8587. struct _data *dp;
  8588. unsigned long object_base;
  8589. long extra;
  8590. unsigned short atype;
  8591. short symnum;
  8592.  
  8593.     offset = GL( pa->offset );
  8594.     atype = GS( pa->atype );
  8595.     symnum = GS( pa->symnum );
  8596.  
  8597.     PS( pa->symnum ) = pf->symtran[symnum];
  8598.     PS( pa->declnum ) = pf->decltran[GS(pa->declnum)];
  8599.  
  8600.     if(atype & A_EXTERN)
  8601.     {
  8602.     short key[4];
  8603.     struct {
  8604.     unsigned char *p;    /* pointer to extvarop in input buffer */
  8605.     } *ep;
  8606.         key[0] = symnum;         /* external symbol number */
  8607.         key[1] = pf->filenum;
  8608.         key[2] = 0;
  8609.         key[3] = 0;
  8610.  
  8611.             /* LOOK UP THE EXTERNAL SYMBOL */
  8612.         if(!SymFind(iv->extrntbl, key, &ep) && *(ep->p))
  8613.         {/* symbol exists and the extvarop was filled in */
  8614.  
  8615.             pf = iv->files[GS( ((short*)(ep->p))[1] )];
  8616.             offset += GL( ((Pop)(ep->p))->data1 );
  8617.         }
  8618.         else
  8619.         {/* Not found or not filled in, leave it alone */
  8620.  
  8621.             return;
  8622.         }
  8623.     }
  8624.     extra = 0;    /* first time through */
  8625.  
  8626.     /* Find the object that the offset points to */
  8627. relink:
  8628.     if(SymFindRange(pf->datatbl, &offset, &dp))
  8629.     {
  8630.         if(extra == 0)
  8631.             extra = offset - dp->offset;
  8632.         object_base = dp->offset;
  8633.  
  8634.         if(*(dp->p) == 0)
  8635.         {/* The found object is a discarded block, relink */
  8636.             pf = iv->files[GS( ((short*)(dp->p))[1] )];
  8637.             offset = GL( ((Pop)(dp->p))->data4 );
  8638.             goto relink;
  8639.         }
  8640.         else
  8641.         {/* Use the adjusted offset in the input buffer */
  8642.  
  8643.             object_base = GL( ((Pop)(dp->p))->data1 );
  8644.         }
  8645.         PL( pa->offset ) = object_base + extra;
  8646.         if(atype & A_EXTERN)
  8647.         {
  8648.             PS( pa->atype ) = atype & ~A_EXTERN;        
  8649.         }
  8650.     }
  8651.     else
  8652.     {
  8653.         ++iv->errors;
  8654.         PWARN(pName ":syserr: object `%s' at offset %d not found\n", 
  8655.             pf->symaddr[symnum], offset);
  8656.     }
  8657. }
  8658. static void
  8659. reset_text_relocs(Piv iv)
  8660. {/* Pass over text and set new offsets in instructions that reference data */
  8661. int i;
  8662.  
  8663.     for(i = 0; i < iv->numfiles; ++i)
  8664.     {
  8665.     Pafile pf;
  8666.     unsigned char *p;
  8667.  
  8668.         pf = iv->files[i];
  8669.         if(!(p = pf->prog_p))
  8670.             continue;
  8671.  
  8672.         while(*p != endfileop)
  8673.         {
  8674.             if(*p && *p <= (unsigned char)100)
  8675.             {/* instruction */
  8676.             int inc = 8;
  8677.                 if((p[1]&0xe0) == OPDATA)
  8678.                     reset_offset(iv, pf, POPA);
  8679.                 inc += (p[1]&0x1f);
  8680.                 if((p[2]&0xe0) == OPDATA)
  8681.                     reset_offset(iv, pf, POPA);
  8682.                 inc += (p[2]&0x1f);
  8683.                 if((p[3]&0xe0) == OPDATA)
  8684.                     reset_offset(iv, pf, POPA);
  8685.             }
  8686.             p = POP->next;                
  8687.         }
  8688.     }
  8689.  
  8690. }
  8691. static void *
  8692. seg_find(Piv iv, int id)
  8693. {
  8694. long key[2];
  8695. void **result;
  8696.  
  8697.     if(iv->segtbl)
  8698.     {
  8699.         key[0] = id;
  8700.         key[1] = 0;
  8701.         if(SymFind(iv->segtbl, key, &result))
  8702.             return *result;
  8703.     }
  8704.     return 0;    
  8705. }
  8706. static void
  8707. check_seg(Piv iv, unsigned char *p, Pafile pf)
  8708. {
  8709. PopI np, op;
  8710.     if(!(iv->segtbl))
  8711.     {
  8712.         iv->segtbl = NewSymTable(iv->category, 111);
  8713.     }
  8714.     if((op = seg_find(iv, GS(POPI->segdef.segid))))
  8715.     {
  8716.         np = POPI;
  8717.         if(        GL(np->segdef.v1) == GL(op->segdef.v1)
  8718.             &&    GL(np->segdef.v2) == GL(op->segdef.v2)
  8719.             &&    GL(np->segdef.v3) == GL(op->segdef.v3))
  8720.         {/* segments of same name have the same values */
  8721.             *p = 0;    /* kill the new definition */
  8722.             return;
  8723.         }
  8724.         else
  8725.         {/* segments of same name have different values */
  8726.             ++iv->errors;
  8727.             PWARN(pName ":Segment `%s' defined differently.\n",
  8728.                 iv->symaddr[GS(POPI->segdef.segid)]);
  8729.             PWARN(pName ":  In file: `%s'\n", pf->symaddr[INFILE_SYMNUM]);
  8730.             return;
  8731.         }
  8732.     }
  8733.     else
  8734.     {
  8735.     long key[2];
  8736.     PopI pp = POPI;
  8737.         key[0] = GS(POPI->segdef.segid);
  8738.         key[1] = 0;
  8739.         SymInsert(iv->segtbl, key, &pp, 4);
  8740.     }
  8741. }
  8742. static void
  8743. reset_syms_decls(Piv iv)
  8744. {
  8745. int i;
  8746.     for(i = 0; i < iv->numfiles; ++i)
  8747.     {
  8748.     Pafile pf;
  8749.     unsigned char *p;
  8750.  
  8751.         pf = iv->files[i];
  8752.         p = pf->file_p;
  8753.  
  8754.         while(*p != endfileop)
  8755.         {
  8756.           if(*p == segdefop)
  8757.           {
  8758.             PS(POPI->segdef.segid) = pf->symtran[GS(POPI->segdef.segid)];
  8759.             check_seg(iv, p, pf);
  8760.           }
  8761.           else if(i > 0)
  8762.           {
  8763.             switch(*p)
  8764.             {
  8765.                 case    declop:
  8766.                     if(GS(POPI->dcl.declnum) < 22)
  8767.                     {/* kill the base declarations */
  8768.                         *p = 0;
  8769.                         p = POP->next;                
  8770.                         *p = 0;
  8771.                     }
  8772.                     else
  8773.                      PS(POPI->dcl.declnum)=pf->decltran[GS(POPI->dcl.declnum)];
  8774.                     break;
  8775.                 case    extlocop:
  8776.                     PS(POPI->reloc.rsym) = pf->symtran[GS(POPI->reloc.rsym)];
  8777.                     break;
  8778.                 case    gfuncdefop:
  8779.                 case    sfuncdefop:
  8780.                     if(pf->numsegs)
  8781.                     PS(POPI->funcdef.segid) = pf->symtran[GS(POPI->funcdef.segid)];
  8782.                 case    nestedfuncdefop:
  8783.                     PL(POPI->funcdef.symnum) = pf->symtran[GL(POPI->funcdef.symnum)];
  8784.                     break;
  8785.                 case    bssblockop:
  8786.                 case    datablockop:
  8787.                     if(pf->numsegs)
  8788.                     PS( POPI->s.segid ) = pf->symtran[GS(POPI->s.segid)];
  8789.                 case    stringblockop:
  8790.                 case    mallocblockop:
  8791.                 case    thunkblockop:
  8792.                 case    extvarop:
  8793.                     PS( POPI->s.symnum ) = pf->symtran[GS(POPI->s.symnum)];
  8794.                     PS( POPI->s.declnum ) = pf->decltran[GS(POPI->s.declnum)];
  8795.                     break;
  8796.                 case    memberinfop:
  8797.                 case    bfieldinfop:
  8798.                     PS(POPI->memb.symnum) = pf->symtran[GS(POPI->memb.symnum)];
  8799.                     PS(POPI->memb.declnum) = pf->decltran[GS(POPI->memb.declnum)];
  8800.                     PS(POPI->memb.cdeclnum) = pf->decltran[GS(POPI->memb.cdeclnum)];
  8801.                     break;
  8802.                 case    structinfop:
  8803.                     PS(POPI->suinf.symnum) = pf->symtran[GS(POPI->suinf.symnum)];
  8804.                     break;
  8805.                 case    funcptrinfop:
  8806.                 case    ptrinfop:
  8807.                     PS(POPI->ptrinf.declnum) = pf->decltran[GS(POPI->ptrinf.declnum)];
  8808.                     break;
  8809.                 case    funcinfop:
  8810.                     PS(POPI->funcd.declnum) = pf->decltran[GS(POPI->funcd.declnum)];
  8811.                     PS(POPI->funcd.symnum) = pf->symtran[GS(POPI->funcd.symnum)];
  8812.                     break;
  8813.                 case    arrayinfop:
  8814.                     PS(POPI->ary.declnum) = pf->decltran[GS(POPI->ary.declnum)];
  8815.                     break;
  8816.                 case    lineop:
  8817.                     PL(POPI->line.filenamenum) = pf->symtran[GL(POPI->line.filenamenum)];
  8818.                     break;
  8819.             }/* END: switch(*p) */
  8820.           }/* END: i > 0 */
  8821.           p = POP->next;                
  8822.         }
  8823.     }
  8824. }
  8825. static int
  8826. link_files(Piv iv)
  8827. {
  8828.     iv->extrntbl = NewSymTable(iv->category, 4092);    /* hashed table */
  8829.     iv->reloctbl = NewSymTable(iv->category, 4092); /* hashed table */
  8830.     iv->gbltbl = NewSymTable(iv->category, 0);    /* sorted table */
  8831.  
  8832.     setup_syms_decls(iv);
  8833.  
  8834.     if(iv->numfiles > 1)
  8835.     {
  8836.         iv->symaddr = Ccalloc(iv->category, sizeof(void*), iv->numsyms+1);
  8837.         iv->decladdr = Ccalloc(iv->category, sizeof(void*), iv->numdecls+1);
  8838.         iv->symtbl = NewSymTable(iv->category, 0); /* sorted table */
  8839.         combine_syms_decls(iv);
  8840.  
  8841.         link_globals(iv);
  8842.         realloc_data(iv);
  8843.         reset_data_relocs(iv);
  8844.         reset_text_relocs(iv);
  8845.  
  8846.         reset_syms_decls(iv);
  8847.     }
  8848.     else
  8849.     {
  8850.         iv->symaddr = iv->files[0]->symaddr;
  8851.         iv->decladdr = iv->files[0]->decladdr;
  8852.         iv->symtbl = NewSymTable(iv->category, 0); /* sorted table */
  8853.         combine_syms_decls(iv);
  8854.  
  8855.         realloc_data(iv);
  8856.         reset_data_relocs(iv);
  8857.         reset_text_relocs(iv);
  8858.     }
  8859.     return iv->errors;
  8860. }
  8861.  
  8862. /* ======================== GLOBAL ROUTINES ========================== */
  8863. int
  8864. Global(readfile) (Piv iv, char *infile_name)
  8865. {
  8866. FILE *infile;
  8867. long infile_size;
  8868. char *inbuf;
  8869.  
  8870.     if(!(infile = fopen(infile_name, "rb")))
  8871.     {
  8872.         PERROR(pName ": Can't open input file: %s\n", infile_name);
  8873.     }
  8874.     fseek(infile, 0, SEEK_END);
  8875.     infile_size = ftell(infile);    
  8876.     fseek(infile, 0, SEEK_SET);
  8877.  
  8878.     if(infile_size == 0)
  8879.     {
  8880.         PERROR(pName ": Empty input file: %s\n", infile_name);
  8881.     }
  8882.     inbuf = Cmalloc(iv->category, infile_size);
  8883.  
  8884.     if(fread(inbuf, 1, infile_size, infile) != infile_size)
  8885.     {
  8886.         fclose(infile);
  8887.         PERROR(pName ": Error reading input file: %s\n", infile_name);
  8888.     }
  8889.     fclose(infile);
  8890.  
  8891.     if(setup_nodelinks(iv, infile_name, inbuf, infile_size))
  8892.         return 4;
  8893.     return 0;
  8894. }
  8895. int
  8896. Global(proc_files) (Piv iv, void *name)
  8897. {
  8898. int ret;
  8899.  
  8900.     if(!(ret = link_files(iv)))
  8901.     {
  8902.         optimize(iv);
  8903.         if(name)
  8904.           iv->symaddr[2] = name;    /* symbol 2 is the output filename */
  8905.  
  8906.         ret = gen_output(iv, iv->symaddr[2]);
  8907.     }
  8908.     return ret;
  8909. }
  8910. void *
  8911. Global(open_instance) (void)
  8912. {
  8913. Piv iv;
  8914. int category;
  8915. #if USING_FRAMEWORK
  8916.     if(num_instance <= 0)
  8917.     {
  8918.         oxlink_clear_bss(pName ".o");    /* reset global storage */
  8919.         local_category = NewMallocCategory();
  8920.     }
  8921.     ++num_instance;
  8922. #endif
  8923.     category = Cnewcat();
  8924.     iv = Ccalloc(category, 1, sizeof(struct _iv));
  8925.     iv->category = category;
  8926.     iv->finextbuf = (PEL)&iv->finextbufstart;
  8927.     return iv;
  8928. }
  8929. void
  8930. Global(close_instance) (Piv iv)
  8931. {
  8932.     if(iv->outfile)
  8933.       fclose(iv->outfile);
  8934.     if(iv->remove_infile)
  8935.     {
  8936.     int i;
  8937.       for(i = 1; i < iv->argc; ++i)
  8938.         unlink(propernameof(iv, iv->argv[i]));
  8939.     }
  8940.     Cfreecat(iv->category);
  8941. #if USING_FRAMEWORK
  8942.     if(--num_instance == 0)
  8943.         freecat(local_category);
  8944. #endif
  8945. }
  8946.  
  8947. /* =========================== THE MAIN PROGRAM ======================= */
  8948.  
  8949. static char *
  8950. filenameof(char *path)
  8951. {
  8952. char *ret = path;
  8953. int i;
  8954.  
  8955.     for(i = 0; path[i]; ++i)
  8956.       if(path[i] == '/')
  8957.         ret = &path[i+1];
  8958.     return ret;
  8959. }
  8960.  
  8961. static char *
  8962. propernameof(Piv iv, char *path)
  8963. {
  8964. char *name = filenameof(path);
  8965. int namlen = strlen(name);
  8966. int i;
  8967.     for(i = namlen-1; i >= 0; --i)
  8968.     {
  8969.       if(name[i] == '/' || name[i] == '\\')
  8970.           break;
  8971.       else if(name[i] == '.')
  8972.         return path;
  8973.     }
  8974.     name = Cmalloc(iv->category, strlen(path)+8);
  8975.     strcpy(name, path);
  8976.     strcat(name, ".anf");
  8977.     return name;
  8978. }
  8979. static void
  8980. Usage()
  8981. {
  8982. fputs(
  8983. "Usage: " pName " [-odsDLR?] [infile...]\n"
  8984. "   -o outfile == name of output file\n"
  8985. "   -d == print debug output\n"
  8986. "   -D == only print debug output\n"
  8987. "   -s == strip declarations and line numbers\n"
  8988. "   -L == generate listing only (to .lst)\n"
  8989. "   -R == remove input file\n"
  8990. "   -? == print this message\n"
  8991. "   Default input file is `code.anf'.\n"
  8992. "   Default output file is specified by the input.\n"
  8993. ,stderr);
  8994. }
  8995.  
  8996. #if USING_FRAMEWORK
  8997. int
  8998. PROG (int argc, char **argv)
  8999. #else
  9000. int
  9001. main (int argc, char **argv)
  9002. #endif
  9003. {
  9004. int i,j;
  9005. char *outfilename = 0;
  9006. volatile Piv iv;
  9007. char debug, only_debug, strip, listing_wanted, remove_infile;
  9008. int ret;
  9009.  
  9010.     remove_infile = listing_wanted = strip = debug = only_debug = 0;
  9011.  
  9012.     /* Get options */
  9013.     for(i = 1; i < argc; ++i)
  9014.     {
  9015.     int trimsize = 1;
  9016.  
  9017.         if(argv[i][0] == '-')
  9018.         {
  9019.             for (j=1; argv[i][j]; j++)
  9020.             {
  9021.                 switch(argv[i][j])
  9022.                 {    
  9023.                     case    'D':
  9024.                         only_debug = 1;
  9025.                         /* FALL THROUGH */
  9026.                     case    'd':
  9027.                         debug = argv[i][++j];
  9028.                         break;
  9029.                     case    's':
  9030.                         strip = 1;
  9031.                         break;
  9032.                     case    'o':
  9033.                         if(argv[i][j+1]) {
  9034.                             outfilename = &argv[i][j+1];
  9035.                         }
  9036.                         else if(i < argc-1) {
  9037.                             outfilename = argv[i+1];
  9038.                             trimsize = 2;
  9039.                         } else {
  9040.                             PWARN(pName ": no output filename\n");
  9041.                             Usage();
  9042.                             return 0;
  9043.                         }
  9044.                         goto trim;
  9045.                         break;
  9046.                     case    'L':
  9047.                         listing_wanted = 1;
  9048.                         break;
  9049.                     case 'R':
  9050.                         remove_infile = 1;
  9051.                         break;
  9052.                     case '?':
  9053.                         Usage();
  9054.                         return 0;
  9055.                     default:
  9056.                         PWARN(pName ": Invalid switch: 0x%x\n", argv[i][j]);
  9057.                         Usage();
  9058.                         return 0;
  9059.                 }
  9060.             }/* END: for(j) */
  9061. trim:
  9062.             /* Trim switch */
  9063.             for(j = i; j < argc-trimsize; ++j)
  9064.                 argv[j] = argv[j+trimsize];
  9065.             argc -= trimsize;
  9066.             --i;
  9067.         }/* END: if('-') */
  9068.     }/* END: for(argc) */
  9069.  
  9070. #if 0
  9071. oxcc_debug(__builtin_iv(),0x40000);
  9072. #endif
  9073.  
  9074.     iv = Global(open_instance) ();
  9075.     if((ret = setjmp(run_env))) {
  9076.         Global(close_instance) (iv);
  9077. #if USING_FRAMEWORK
  9078.         return ret;
  9079. #else
  9080.         exit(ret);
  9081. #endif
  9082.     }
  9083.     iv->debug = debug;
  9084.     iv->only_debug = only_debug;
  9085.     iv->labeltbl = NewSymTable(iv->category, 4092);
  9086. #if REALLY_NEED_OFFSETS
  9087.     iv->newlabeltbl = NewSymTable(iv->category, 4092);
  9088. #endif
  9089.     iv->strip = strip;
  9090.     iv->listing_wanted = listing_wanted;
  9091.     iv->remove_infile = remove_infile;
  9092.     iv->argc = argc;
  9093.     iv->argv = argv;
  9094.  
  9095.     if(argc < 2)
  9096.     {/* Default input filename is 'code.anf' */
  9097.         ret = Global(readfile) (iv, "code.anf");
  9098.     }
  9099.     else
  9100.     {/* READ EACH INPUT FILE */
  9101.         for(i = 1; i < argc; ++i)
  9102.           if((ret = Global(readfile) (iv, propernameof(iv,argv[i]))))
  9103.             break;
  9104.     }
  9105.     if(!ret && !iv->only_debug)
  9106.     {
  9107.         ret = Global(proc_files) (iv, outfilename);
  9108.     }
  9109.     Global(close_instance) (iv);
  9110. #if USING_FRAMEWORK
  9111.     return ret;
  9112. #else
  9113.     exit(ret);
  9114. #endif
  9115. }
  9116.  
  9117.