11/*
22** DynASM ARM64 encoding engine.
3- ** Copyright (C) 2005-2021 Mike Pall. All rights reserved.
3+ ** Copyright (C) 2005-2023 Mike Pall. All rights reserved.
44** Released under the MIT license. See dynasm.lua for full copyright notice.
55*/
66
@@ -72,7 +72,7 @@ struct dasm_State {
7272 size_t lgsize ;
7373 int * pclabels ; /* PC label chains/pos ptrs. */
7474 size_t pcsize ;
75- void * * globals ; /* Array of globals (bias -10) . */
75+ void * * globals ; /* Array of globals. */
7676 dasm_Section * section ; /* Pointer to active section. */
7777 size_t codesize ; /* Total size of all code sections. */
7878 int maxsection ; /* 0 <= sectionidx < maxsection. */
@@ -89,7 +89,6 @@ void dasm_init(Dst_DECL, int maxsection)
8989{
9090 dasm_State * D ;
9191 size_t psz = 0 ;
92- int i ;
9392 Dst_REF = NULL ;
9493 DASM_M_GROW (Dst , struct dasm_State , Dst_REF , psz , DASM_PSZ (maxsection ));
9594 D = Dst_REF ;
@@ -100,12 +99,7 @@ void dasm_init(Dst_DECL, int maxsection)
10099 D -> pcsize = 0 ;
101100 D -> globals = NULL ;
102101 D -> maxsection = maxsection ;
103- for (i = 0 ; i < maxsection ; i ++ ) {
104- D -> sections [i ].buf = NULL ; /* Need this for pass3. */
105- D -> sections [i ].rbuf = D -> sections [i ].buf - DASM_SEC2POS (i );
106- D -> sections [i ].bsize = 0 ;
107- D -> sections [i ].epos = 0 ; /* Wrong, but is recalculated after resize. */
108- }
102+ memset ((void * )D -> sections , 0 , maxsection * sizeof (dasm_Section ));
109103}
110104
111105/* Free DynASM state. */
@@ -125,7 +119,7 @@ void dasm_free(Dst_DECL)
125119void dasm_setupglobal (Dst_DECL , void * * gl , unsigned int maxgl )
126120{
127121 dasm_State * D = Dst_REF ;
128- D -> globals = gl - 10 ; /* Negative bias to compensate for locals. */
122+ D -> globals = gl ;
129123 DASM_M_GROW (Dst , int , D -> lglabels , D -> lgsize , (10 + maxgl )* sizeof (int ));
130124}
131125
@@ -150,6 +144,7 @@ void dasm_setup(Dst_DECL, const void *actionlist)
150144 if (D -> pclabels ) memset ((void * )D -> pclabels , 0 , D -> pcsize );
151145 for (i = 0 ; i < D -> maxsection ; i ++ ) {
152146 D -> sections [i ].pos = DASM_SEC2POS (i );
147+ D -> sections [i ].rbuf = D -> sections [i ].buf - D -> sections [i ].pos ;
153148 D -> sections [i ].ofs = 0 ;
154149 }
155150}
@@ -158,10 +153,10 @@ void dasm_setup(Dst_DECL, const void *actionlist)
158153#ifdef DASM_CHECKS
159154#define CK (x , st ) \
160155 do { if (!(x)) { \
161- D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
156+ D->status = DASM_S_##st|(int)( p-D->actionlist-1); return; } } while (0)
162157#define CKPL (kind , st ) \
163158 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
164- D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
159+ D->status = DASM_S_RANGE_##st|(int)( p-D->actionlist-1); return; } } while (0)
165160#else
166161#define CK (x , st ) ((void)0)
167162#define CKPL (kind , st ) ((void)0)
@@ -190,7 +185,9 @@ static int dasm_imm13(int lo, int hi)
190185 unsigned long long n = (((unsigned long long )hi ) << 32 ) | (unsigned int )lo ;
191186 unsigned long long m = 1ULL , a , b , c ;
192187 if (n & 1 ) { n = ~n ; inv = 1 ; }
193- a = n & - n ; b = (n + a )& - (n + a ); c = (n + a - b )& - (n + a - b );
188+ a = n & (unsigned long long )- (long long )n ;
189+ b = (n + a )& (unsigned long long )- (long long )(n + a );
190+ c = (n + a - b )& (unsigned long long )- (long long )(n + a - b );
194191 xa = dasm_ffs (a ); xb = dasm_ffs (b );
195192 if (c ) {
196193 w = dasm_ffs (c ) - xa ;
@@ -415,7 +412,7 @@ int dasm_link(Dst_DECL, size_t *szp)
415412
416413#ifdef DASM_CHECKS
417414#define CK (x , st ) \
418- do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
415+ do { if (!(x)) return DASM_S_##st|(int)( p-D->actionlist-1); } while (0)
419416#else
420417#define CK (x , st ) ((void)0)
421418#endif
@@ -451,7 +448,7 @@ int dasm_encode(Dst_DECL, void *buffer)
451448 break ;
452449 case DASM_REL_LG :
453450 if (n < 0 ) {
454- ptrdiff_t na = (ptrdiff_t )D -> globals [- n ] - (ptrdiff_t )cp + 4 ;
451+ ptrdiff_t na = (ptrdiff_t )D -> globals [- n - 10 ] - (ptrdiff_t )cp + 4 ;
455452 n = (int )na ;
456453 CK_REL ((ptrdiff_t )n == na , na );
457454 goto patchrel ;
@@ -494,7 +491,7 @@ int dasm_encode(Dst_DECL, void *buffer)
494491 goto patchrel ;
495492 }
496493 case DASM_LABEL_LG :
497- ins &= 2047 ; if (ins >= 20 ) D -> globals [ins - 10 ] = (void * )(base + n );
494+ ins &= 2047 ; if (ins >= 20 ) D -> globals [ins - 20 ] = (void * )(base + n );
498495 break ;
499496 case DASM_LABEL_PC : break ;
500497 case DASM_IMM :
@@ -563,7 +560,7 @@ int dasm_checkstep(Dst_DECL, int secmatch)
563560 }
564561 if (D -> status == DASM_S_OK && secmatch >= 0 &&
565562 D -> section != & D -> sections [secmatch ])
566- D -> status = DASM_S_MATCH_SEC |(D -> section - D -> sections );
563+ D -> status = DASM_S_MATCH_SEC |(int )( D -> section - D -> sections );
567564 return D -> status ;
568565}
569566#endif
0 commit comments