LCOV - code coverage report
Current view: top level - flamenco/vm/jit - dasm_x86.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 18 346 5.2 %
Date: 2024-11-13 11:58:15 Functions: 1 10 10.0 %

          Line data    Source code
       1             : /*
       2             : ** DynASM x86 encoding engine.
       3             : ** Copyright (C) 2005-2023 Mike Pall. All rights reserved.
       4             : ** Released under the MIT license. See dynasm.lua for full copyright notice.
       5             : */
       6             : 
       7             : #include <stddef.h>
       8             : #include <stdarg.h>
       9             : #include <string.h>
      10             : #include <stdlib.h>
      11             : 
      12             : #define DASM_ARCH               "x86"
      13             : 
      14             : #ifndef DASM_EXTERN
      15           0 : #define DASM_EXTERN(a,b,c,d)    0
      16             : #endif
      17             : 
      18             : /* Action definitions. DASM_STOP must be 255. */
      19             : enum {
      20             :   DASM_DISP = 233,
      21             :   DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB,
      22             :   DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC,
      23             :   DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN,
      24             :   DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP
      25             : };
      26             : 
      27             : /* Maximum number of section buffer positions for a single dasm_put() call. */
      28           3 : #define DASM_MAXSECPOS          25
      29             : 
      30             : /* DynASM encoder status codes. Action list offset or number are or'ed in. */
      31           3 : #define DASM_S_OK               0x00000000
      32             : #define DASM_S_NOMEM            0x01000000
      33           0 : #define DASM_S_PHASE            0x02000000
      34             : #define DASM_S_MATCH_SEC        0x03000000
      35             : #define DASM_S_RANGE_I          0x11000000
      36             : #define DASM_S_RANGE_SEC        0x12000000
      37             : #define DASM_S_RANGE_LG         0x13000000
      38             : #define DASM_S_RANGE_PC         0x14000000
      39             : #define DASM_S_RANGE_VREG       0x15000000
      40             : #define DASM_S_UNDEF_L          0x21000000
      41             : #define DASM_S_UNDEF_PC         0x22000000
      42             : 
      43             : /* Macros to convert positions (8 bit section + 24 bit index). */
      44             : #define DASM_POS2IDX(pos)       ((pos)&0x00ffffff)
      45           0 : #define DASM_POS2BIAS(pos)      ((pos)&0xff000000)
      46           3 : #define DASM_SEC2POS(sec)       ((sec)<<24)
      47           0 : #define DASM_POS2SEC(pos)       ((pos)>>24)
      48           0 : #define DASM_POS2PTR(D, pos)    (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
      49             : 
      50             : /* Action list type. */
      51             : typedef const unsigned char *dasm_ActList;
      52             : 
      53             : /* Per-section structure. */
      54             : typedef struct dasm_Section {
      55             :   int *rbuf;            /* Biased buffer pointer (negative section bias). */
      56             :   int *buf;             /* True buffer pointer. */
      57             :   size_t bsize;         /* Buffer size in bytes. */
      58             :   int pos;              /* Biased buffer position. */
      59             :   int epos;             /* End of biased buffer position - max single put. */
      60             :   int ofs;              /* Byte offset into section. */
      61             : } dasm_Section;
      62             : 
      63             : /* Core structure holding the DynASM encoding state. */
      64             : struct dasm_State {
      65             :   size_t psize;                 /* Allocated size of this structure. */
      66             :   dasm_ActList actionlist;      /* Current actionlist pointer. */
      67             :   int *lglabels;                /* Local/global chain/pos ptrs. */
      68             :   size_t lgsize;
      69             :   int *pclabels;                /* PC label chains/pos ptrs. */
      70             :   size_t pcsize;
      71             :   void **globals;               /* Array of globals. */
      72             :   dasm_Section *section;        /* Pointer to active section. */
      73             :   size_t codesize;              /* Total size of all code sections. */
      74             :   int maxsection;               /* 0 <= sectionidx < maxsection. */
      75             :   int status;                   /* Status code. */
      76             :   dasm_Section sections[1];     /* All sections. Alloc-extended. */
      77             : };
      78             : 
      79             : /* The size of the core structure depends on the max. number of sections. */
      80           6 : #define DASM_PSZ(ms)    (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
      81             : 
      82             : #ifndef FD_DASM_HEADER_ONLY
      83             : #define FD_DASM_HEADER_ONLY 0
      84             : #endif
      85             : 
      86             : #if !FD_DASM_HEADER_ONLY
      87             : 
      88             : /* Initialize DynASM state. */
      89             : void dasm_init(Dst_DECL, int maxsection)
      90           0 : {
      91           0 :   dasm_State *D;
      92           0 :   size_t psz = 0;
      93           0 :   Dst_REF = NULL;
      94           0 :   DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
      95           0 :   D = Dst_REF;
      96           0 :   D->psize = psz;
      97           0 :   D->lglabels = NULL;
      98           0 :   D->lgsize = 0;
      99           0 :   D->pclabels = NULL;
     100           0 :   D->pcsize = 0;
     101           0 :   D->globals = NULL;
     102           0 :   D->maxsection = maxsection;
     103           0 :   memset((void *)D->sections, 0, maxsection * sizeof(dasm_Section));
     104           0 : }
     105             : 
     106             : /* Free DynASM state. */
     107             : void dasm_free(Dst_DECL)
     108           0 : {
     109           0 :   dasm_State *D = Dst_REF;
     110           0 :   int i;
     111           0 :   for (i = 0; i < D->maxsection; i++)
     112           0 :     if (D->sections[i].buf)
     113           0 :       DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
     114           0 :   if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
     115           0 :   if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
     116           0 :   DASM_M_FREE(Dst, D, D->psize);
     117           0 : }
     118             : 
     119             : /* Setup global label array. Must be called before dasm_setup(). */
     120             : void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
     121           0 : {
     122           0 :   dasm_State *D = Dst_REF;
     123           0 :   D->globals = gl;
     124           0 :   DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
     125           0 : }
     126             : 
     127             : /* Grow PC label array. Can be called after dasm_setup(), too. */
     128             : void dasm_growpc(Dst_DECL, unsigned int maxpc)
     129           0 : {
     130           0 :   dasm_State *D = Dst_REF;
     131           0 :   size_t osz = D->pcsize;
     132           0 :   DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
     133           0 :   memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
     134           0 : }
     135             : 
     136             : /* Setup encoder. */
     137             : void dasm_setup(Dst_DECL, const void *actionlist)
     138           3 : {
     139           3 :   dasm_State *D = Dst_REF;
     140           3 :   int i;
     141           3 :   D->actionlist = (dasm_ActList)actionlist;
     142           3 :   D->status = DASM_S_OK;
     143           3 :   D->section = &D->sections[0];
     144           3 :   memset((void *)D->lglabels, 0, D->lgsize);
     145           3 :   if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
     146           6 :   for (i = 0; i < D->maxsection; i++) {
     147           3 :     D->sections[i].pos = DASM_SEC2POS(i);
     148           3 :     D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos;
     149           3 :     D->sections[i].ofs = 0;
     150           3 :   }
     151           3 : }
     152             : 
     153             : 
     154             : #ifdef DASM_CHECKS
     155             : #define CK(x, st) \
     156             :   do { if (!(x)) { \
     157             :     D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
     158             : #define CKPL(kind, st) \
     159             :   do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
     160             :     D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
     161             : #else
     162           0 : #define CK(x, st)       ((void)0)
     163           0 : #define CKPL(kind, st)  ((void)0)
     164             : #endif
     165             : 
     166             : /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
     167             : void dasm_put(Dst_DECL, int start, ...)
     168           0 : {
     169           0 :   va_list ap;
     170           0 :   dasm_State *D = Dst_REF;
     171           0 :   dasm_ActList p = D->actionlist + start;
     172           0 :   dasm_Section *sec = D->section;
     173           0 :   int pos = sec->pos, ofs = sec->ofs, mrm = -1;
     174           0 :   int *b;
     175             : 
     176           0 :   if (pos >= sec->epos) {
     177           0 :     DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
     178           0 :       sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
     179           0 :     sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
     180           0 :     sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
     181           0 :   }
     182             : 
     183           0 :   b = sec->rbuf;
     184           0 :   b[pos++] = start;
     185             : 
     186           0 :   va_start(ap, start);
     187           0 :   while (1) {
     188           0 :     int action = *p++;
     189           0 :     if (action < DASM_DISP) {
     190           0 :       ofs++;
     191           0 :     } else if (action <= DASM_REL_A) {
     192           0 :       int n = va_arg(ap, int);
     193           0 :       b[pos++] = n;
     194           0 :       switch (action) {
     195           0 :       case DASM_DISP:
     196           0 :         if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
     197             :         /* fallthrough */
     198           0 :       case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */
     199           0 :       case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
     200           0 :       case DASM_IMM_D: ofs += 4; break;
     201           0 :       case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob;
     202           0 :       case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break;
     203           0 :       case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */
     204           0 :       case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
     205           0 :       case DASM_SPACE: p++; ofs += n; break;
     206           0 :       case DASM_SETLABEL: b[pos-2] = -0x40000000; break;  /* Neg. label ofs. */
     207           0 :       case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
     208           0 :         if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
     209           0 :         if (*p < 0x20 && (n&7) == 4) ofs++;
     210           0 :         switch ((*p++ >> 3) & 3) {
     211           0 :         case 3: n |= b[pos-3]; /* fallthrough */
     212           0 :         case 2: n |= b[pos-2]; /* fallthrough */
     213           0 :         case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
     214           0 :         }
     215           0 :         continue;
     216           0 :       }
     217           0 :       mrm = -1;
     218           0 :     } else {
     219           0 :       int *pl, n;
     220           0 :       switch (action) {
     221           0 :       case DASM_REL_LG:
     222           0 :       case DASM_IMM_LG:
     223           0 :         n = *p++; pl = D->lglabels + n;
     224             :         /* Bkwd rel or global. */
     225           0 :         if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
     226           0 :         pl -= 246; n = *pl;
     227           0 :         if (n < 0) n = 0;  /* Start new chain for fwd rel if label exists. */
     228           0 :         goto linkrel;
     229           0 :       case DASM_REL_PC:
     230           0 :       case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
     231           0 :       putrel:
     232           0 :         n = *pl;
     233           0 :         if (n < 0) {  /* Label exists. Get label pos and store it. */
     234           0 :           b[pos] = -n;
     235           0 :         } else {
     236           0 :       linkrel:
     237           0 :           b[pos] = n;  /* Else link to rel chain, anchored at label. */
     238           0 :           *pl = pos;
     239           0 :         }
     240           0 :         pos++;
     241           0 :         ofs += 4;  /* Maximum offset needed. */
     242           0 :         if (action == DASM_REL_LG || action == DASM_REL_PC) {
     243           0 :           b[pos++] = ofs;  /* Store pass1 offset estimate. */
     244           0 :         } else if (sizeof(ptrdiff_t) == 8) {
     245           0 :           ofs += 4;
     246           0 :         }
     247           0 :         break;
     248           0 :       case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel;
     249           0 :       case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
     250           0 :       putlabel:
     251           0 :         n = *pl;  /* n > 0: Collapse rel chain and replace with label pos. */
     252           0 :         while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; }
     253           0 :         *pl = -pos;  /* Label exists now. */
     254           0 :         b[pos++] = ofs;  /* Store pass1 offset estimate. */
     255           0 :         break;
     256           0 :       case DASM_ALIGN:
     257           0 :         ofs += *p++;  /* Maximum alignment needed (arg is 2**n-1). */
     258           0 :         b[pos++] = ofs;  /* Store pass1 offset estimate. */
     259           0 :         break;
     260           0 :       case DASM_EXTERN: p += 2; ofs += 4; break;
     261           0 :       case DASM_ESC: p++; ofs++; break;
     262           0 :       case DASM_MARK: mrm = p[-2]; break;
     263           0 :       case DASM_SECTION:
     264           0 :         n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n];
     265           0 :       case DASM_STOP: goto stop;
     266           0 :       }
     267           0 :     }
     268           0 :   }
     269           0 : stop:
     270           0 :   va_end(ap);
     271           0 :   sec->pos = pos;
     272           0 :   sec->ofs = ofs;
     273           0 : }
     274             : #undef CK
     275             : 
     276             : /* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */
     277             : int dasm_link(Dst_DECL, size_t *szp)
     278           0 : {
     279           0 :   dasm_State *D = Dst_REF;
     280           0 :   int secnum;
     281           0 :   int ofs = 0;
     282             : 
     283             : #ifdef DASM_CHECKS
     284             :   *szp = 0;
     285             :   if (D->status != DASM_S_OK) return D->status;
     286             :   {
     287             :     int pc;
     288             :     for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
     289             :       if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
     290             :   }
     291             : #endif
     292             : 
     293           0 :   { /* Handle globals not defined in this translation unit. */
     294           0 :     int idx;
     295           0 :     for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
     296           0 :       int n = D->lglabels[idx];
     297             :       /* Undefined label: Collapse rel chain and replace with marker (< 0). */
     298           0 :       while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
     299           0 :     }
     300           0 :   }
     301             : 
     302             :   /* Combine all code sections. No support for data sections (yet). */
     303           0 :   for (secnum = 0; secnum < D->maxsection; secnum++) {
     304           0 :     dasm_Section *sec = D->sections + secnum;
     305           0 :     int *b = sec->rbuf;
     306           0 :     int pos = DASM_SEC2POS(secnum);
     307           0 :     int lastpos = sec->pos;
     308             : 
     309           0 :     while (pos != lastpos) {
     310           0 :       dasm_ActList p = D->actionlist + b[pos++];
     311           0 :       int op = 0;
     312           0 :       while (1) {
     313           0 :         int action = *p++;
     314           0 :         switch (action) {
     315           0 :         case DASM_REL_LG: p++;
     316             :           /* fallthrough */
     317           0 :         case DASM_REL_PC: {
     318           0 :           int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
     319           0 :           if (shrink) {  /* Shrinkable branch opcode? */
     320           0 :             int lofs, lpos = b[pos];
     321           0 :             if (lpos < 0) goto noshrink;  /* Ext global? */
     322           0 :             lofs = *DASM_POS2PTR(D, lpos);
     323           0 :             if (lpos > pos) {  /* Fwd label: add cumulative section offsets. */
     324           0 :               int i;
     325           0 :               for (i = secnum; i < DASM_POS2SEC(lpos); i++)
     326           0 :                 lofs += D->sections[i].ofs;
     327           0 :             } else {
     328           0 :               lofs -= ofs;  /* Bkwd label: unfix offset. */
     329           0 :             }
     330           0 :             lofs -= b[pos+1];  /* Short branch ok? */
     331           0 :             if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink;  /* Yes. */
     332           0 :             else { noshrink: shrink = 0; }  /* No, cannot shrink op. */
     333           0 :           }
     334           0 :           b[pos+1] = shrink;
     335           0 :           pos += 2;
     336           0 :           break;
     337           0 :         }
     338             :           /* fallthrough */
     339           0 :         case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
     340             :           /* fallthrough */
     341           0 :         case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
     342           0 :         case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
     343           0 :         case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break;
     344           0 :         case DASM_LABEL_LG: p++;
     345             :           /* fallthrough */
     346           0 :         case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
     347           0 :         case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
     348           0 :         case DASM_EXTERN: p += 2; break;
     349           0 :         case DASM_ESC: op = *p++; break;
     350           0 :         case DASM_MARK: break;
     351           0 :         case DASM_SECTION: case DASM_STOP: goto stop;
     352           0 :         default: op = action; break;
     353           0 :         }
     354           0 :       }
     355           0 :       stop: (void)0;
     356           0 :     }
     357           0 :     ofs += sec->ofs;  /* Next section starts right after current section. */
     358           0 :   }
     359             : 
     360           0 :   D->codesize = ofs;  /* Total size of all code sections */
     361           0 :   *szp = ofs;
     362           0 :   return DASM_S_OK;
     363           0 : }
     364             : 
     365           0 : #define dasmb(x)        *cp++ = (unsigned char)(x)
     366             : #ifndef DASM_ALIGNED_WRITES
     367             : #define dasmw(x) \
     368           0 :   do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0)
     369             : #define dasmd(x) \
     370           0 :   do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0)
     371             : #define dasmq(x) \
     372           0 :   do { *((unsigned long long *)cp) = (unsigned long long)(x); cp+=8; } while (0)
     373             : #else
     374             : #define dasmw(x)        do { dasmb(x); dasmb((x)>>8); } while (0)
     375             : #define dasmd(x)        do { dasmw(x); dasmw((x)>>16); } while (0)
     376             : #define dasmq(x)        do { dasmd(x); dasmd((x)>>32); } while (0)
     377             : #endif
     378             : static unsigned char *dasma_(unsigned char *cp, ptrdiff_t x)
     379           0 : {
     380           0 :   if (sizeof(ptrdiff_t) == 8)
     381           0 :     dasmq((unsigned long long)x);
     382           0 :   else
     383           0 :     dasmd((unsigned int)x);
     384           0 :   return cp;
     385           0 : }
     386           0 : #define dasma(x)        (cp = dasma_(cp, (x)))
     387             : 
     388             : /* Pass 3: Encode sections. */
     389             : int dasm_encode(Dst_DECL, void *buffer)
     390           0 : {
     391           0 :   dasm_State *D = Dst_REF;
     392           0 :   unsigned char *base = (unsigned char *)buffer;
     393           0 :   unsigned char *cp = base;
     394           0 :   int secnum;
     395             : 
     396             :   /* Encode all code sections. No support for data sections (yet). */
     397           0 :   for (secnum = 0; secnum < D->maxsection; secnum++) {
     398           0 :     dasm_Section *sec = D->sections + secnum;
     399           0 :     int *b = sec->buf;
     400           0 :     int *endb = sec->rbuf + sec->pos;
     401             : 
     402           0 :     while (b != endb) {
     403           0 :       dasm_ActList p = D->actionlist + *b++;
     404           0 :       unsigned char *mark = NULL;
     405           0 :       while (1) {
     406           0 :         int action = *p++;
     407           0 :         int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0;
     408           0 :         switch (action) {
     409           0 :         case DASM_DISP: if (!mark) mark = cp; {
     410           0 :           unsigned char *mm = mark;
     411           0 :           if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL;
     412           0 :           if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7;
     413           0 :             if (mrm != 5) { mm[-1] -= 0x80; break; } }
     414           0 :           if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40;
     415           0 :         }
     416             :           /* fallthrough */
     417           0 :         case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break;
     418           0 :         case DASM_IMM_DB: if (((n+128)&-256) == 0) {
     419           0 :             db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb;
     420           0 :           } else mark = NULL;
     421             :           /* fallthrough */
     422           0 :         case DASM_IMM_D: wd: dasmd(n); break;
     423           0 :         case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL;
     424             :           /* fallthrough */
     425           0 :         case DASM_IMM_W: dasmw(n); break;
     426           0 :         case DASM_VREG: {
     427           0 :           int t = *p++;
     428           0 :           unsigned char *ex = cp - (t&7);
     429           0 :           if ((n & 8) && t < 0xa0) {
     430           0 :             if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
     431           0 :             n &= 7;
     432           0 :           } else if (n & 0x10) {
     433           0 :             if (*ex & 0x80) {
     434           0 :               *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
     435           0 :             }
     436           0 :             while (++ex < cp) ex[-1] = *ex;
     437           0 :             if (mark) mark--;
     438           0 :             cp--;
     439           0 :             n &= 7;
     440           0 :           }
     441           0 :           if (t >= 0xc0) n <<= 4;
     442           0 :           else if (t >= 0x40) n <<= 3;
     443           0 :           else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
     444           0 :           cp[-1] ^= n;
     445           0 :           break;
     446           0 :         }
     447           0 :         case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
     448           0 :           b++; n = (int)(ptrdiff_t)D->globals[-n-10];
     449             :           /* fallthrough */
     450           0 :         case DASM_REL_A: rel_a:
     451           0 :           n -= (unsigned int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */
     452           0 :         case DASM_REL_PC: rel_pc: {
     453           0 :           int shrink = *b++;
     454           0 :           int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; }
     455           0 :           n = *pb - ((int)(cp-base) + 4-shrink);
     456           0 :           if (shrink == 0) goto wd;
     457           0 :           if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb;
     458           0 :           goto wb;
     459           0 :         }
     460           0 :         case DASM_IMM_LG:
     461           0 :           p++;
     462           0 :           if (n < 0) { dasma((ptrdiff_t)D->globals[-n-10]); break; }
     463             :           /* fallthrough */
     464           0 :         case DASM_IMM_PC: {
     465           0 :           int *pb = DASM_POS2PTR(D, n);
     466           0 :           dasma(*pb < 0 ? (ptrdiff_t)pb[1] : (*pb + (ptrdiff_t)base));
     467           0 :           break;
     468           0 :         }
     469           0 :         case DASM_LABEL_LG: {
     470           0 :           int idx = *p++;
     471           0 :           if (idx >= 10)
     472           0 :             D->globals[idx-10] = (void *)(base + (*p == DASM_SETLABEL ? *b : n));
     473           0 :           break;
     474           0 :         }
     475           0 :         case DASM_LABEL_PC: case DASM_SETLABEL: break;
     476           0 :         case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; }
     477           0 :         case DASM_ALIGN:
     478           0 :           n = *p++;
     479           0 :           while (((cp-base) & n)) *cp++ = 0x90; /* nop */
     480           0 :           break;
     481           0 :         case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd;
     482           0 :         case DASM_MARK: mark = cp; break;
     483           0 :         case DASM_ESC: action = *p++;
     484             :           /* fallthrough */
     485           0 :         default: *cp++ = action; break;
     486           0 :         case DASM_SECTION: case DASM_STOP: goto stop;
     487           0 :         }
     488           0 :       }
     489           0 :       stop: (void)0;
     490           0 :     }
     491           0 :   }
     492             : 
     493           0 :   if (base + D->codesize != cp)  /* Check for phase errors. */
     494           0 :     return DASM_S_PHASE;
     495           0 :   return DASM_S_OK;
     496           0 : }
     497             : 
     498             : /* Get PC label offset. */
     499             : int dasm_getpclabel(Dst_DECL, unsigned int pc)
     500           0 : {
     501           0 :   dasm_State *D = Dst_REF;
     502           0 :   if (pc*sizeof(int) < D->pcsize) {
     503           0 :     int pos = D->pclabels[pc];
     504           0 :     if (pos < 0) return *DASM_POS2PTR(D, -pos);
     505           0 :     if (pos > 0) return -1;  /* Undefined. */
     506           0 :   }
     507           0 :   return -2;  /* Unused or out of range. */
     508           0 : }
     509             : 
     510             : #ifdef DASM_CHECKS
     511             : /* Optional sanity checker to call between isolated encoding steps. */
     512             : int dasm_checkstep(Dst_DECL, int secmatch)
     513             : {
     514             :   dasm_State *D = Dst_REF;
     515             :   if (D->status == DASM_S_OK) {
     516             :     int i;
     517             :     for (i = 1; i <= 9; i++) {
     518             :       if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; }
     519             :       D->lglabels[i] = 0;
     520             :     }
     521             :   }
     522             :   if (D->status == DASM_S_OK && secmatch >= 0 &&
     523             :       D->section != &D->sections[secmatch])
     524             :     D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
     525             :   return D->status;
     526             : }
     527             : #endif
     528             : 
     529             : #endif /* !FD_DASM_HEADER_ONLY */

Generated by: LCOV version 1.14