Skip to content

Commit c673f7e

Browse files
committed
simx86: move Addr2PC array to TNode
We now only have the actual code in the code buffer. mblock and pmeta pointers are then redundant, we can just use addr.
1 parent d49c40a commit c673f7e

File tree

3 files changed

+27
-47
lines changed

3 files changed

+27
-47
lines changed

src/base/emu-i386/simx86/codegen.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,43 +393,34 @@ void Gen(int op, int mode, ...)
393393
/////////////////////////////////////////////////////////////////////////////
394394

395395

396-
static CodeBuf *ProduceCode(unsigned int PC, IMeta *I0)
396+
static unsigned char *ProduceCode(unsigned int PC, IMeta *I0)
397397
{
398-
int i,j,nap,mall_req;
398+
int i,j,mall_req;
399399
unsigned char *cp, *cp1, *BaseGenBuf, *CodePtr;
400400
size_t GenBufSize;
401-
CodeBuf *GenCodeBuf;
402401

403402
if (debug_level('e')>1) {
404403
e_printf("---------------------------------------------\n");
405404
e_printf("ProduceCode: CurrIMeta=%d\n",CurrIMeta);
406405
}
407406
if (CurrIMeta < 0) leavedos_main(0xbac3);
408407

409-
/* reserve space for auto-ptr and info structures */
410-
nap = I0->ncount+1;
411-
412408
/* allocate the actual code buffer here; size is a worst-case
413409
* estimate based on measured bytes per opcode.
414410
*
415-
* Code buffer layout:
416-
* 0000/0000 Addr2Pc table (nap) pointed from {TNode}.pmeta
417-
* nap actual code produced (BaseGenBuf)
418-
* plus tail code
419-
* Only the code part is filled here.
420411
* GenBufSize contain a first guess of the amount of space required
421412
*
422413
*/
423414
GenBufSize = 0;
424415
for (i=0; i<=CurrIMeta; i++)
425416
GenBufSize += I0[i].ngen * MAX_GEND_BYTES_PER_OP;
426-
mall_req = GenBufSize + offsetof(CodeBuf, meta) + sizeof(Addr2Pc) * nap + 32;// 32 for tail
427-
GenCodeBuf = dlmalloc(mall_req);
417+
mall_req = GenBufSize + 32;// 32 for tail
418+
BaseGenBuf = dlmalloc(mall_req);
428419
/* actual code buffer starts from here */
429-
BaseGenBuf = CodePtr = (unsigned char *)&GenCodeBuf->meta[nap];
420+
CodePtr = BaseGenBuf;
430421
I0->daddr = 0;
431422
if (debug_level('e')>1)
432-
e_printf("CodeBuf=%p siz %zd CodePtr=%p\n",GenCodeBuf,GenBufSize,CodePtr);
423+
e_printf("CodeBuf=%p siz %zd\n",BaseGenBuf,GenBufSize);
433424

434425
for (i=0; i<=CurrIMeta; i++) {
435426
IMeta *I = &I0[i];
@@ -475,12 +466,12 @@ static CodeBuf *ProduceCode(unsigned int PC, IMeta *I0)
475466
I0->totlen = CodePtr - BaseGenBuf;
476467

477468
/* shrink buffer to what is actually needed */
478-
mall_req = I0->totlen + offsetof(CodeBuf, meta) + sizeof(Addr2Pc) * nap;
479-
GenCodeBuf = dlrealloc(GenCodeBuf, mall_req);
469+
mall_req = I0->totlen;
470+
BaseGenBuf = dlrealloc(BaseGenBuf, mall_req);
480471
if (debug_level('e')>3)
481472
e_printf("Seq len %#x:%#x\n",I0->seqlen,I0->totlen);
482473

483-
return GenCodeBuf;
474+
return BaseGenBuf;
484475
}
485476

486477

@@ -512,7 +503,7 @@ TNode *Close(unsigned int PC, unsigned int Interp_LONG_CS, int mode,
512503
{
513504
IMeta *I0;
514505
TNode *G;
515-
CodeBuf *GenCodeBuf;
506+
unsigned char *GenCodeBuf;
516507

517508
assert (CurrIMeta >= 0);
518509

src/base/emu-i386/simx86/trees.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ void avltr_delete(const int key)
446446
pthread_mutex_unlock(&trees_mtx);
447447
leavedos_main(0x8130);
448448
}
449-
if (G->mblock) dlfree(G->mblock);
449+
if (G->addr) dlfree(G->addr);
450450
__atomic_store_n(&findtree_cache[G->key&FINDTREE_CACHE_HASH_MASK],
451451
NULL, __ATOMIC_RELAXED);
452452
free(G);
@@ -636,7 +636,7 @@ void avltr_destroy(void)
636636
B = B->next;
637637
free(B2);
638638
}
639-
if (p->data->mblock) dlfree(p->data->mblock);
639+
if (p->data->addr) dlfree(p->data->addr);
640640
free(p->data);
641641
p->data = NULL;
642642
}
@@ -670,7 +670,7 @@ unsigned int FindPC(const unsigned char *addr)
670670
G = FindTree(LONG_CS + TheCPU.eip);
671671
if (!G)
672672
return 0;
673-
AP = G->pmeta;
673+
AP = G->meta;
674674
for (i = 0; i < G->seqnum + 1; i++) {
675675
e_printf(" %08x:%p",(G->key+AP->dnpc),G->addr+AP->daddr);
676676
if (addr < G->addr+AP->daddr) break;
@@ -1030,8 +1030,8 @@ static void DumpTree (FILE *fd)
10301030
nn++;
10311031
continue;
10321032
}
1033-
fprintf(fd,"%04d Node %p at %08x..%08x mblock=%p flags=%#x\n",
1034-
nn,G,G->key,(G->key+G->seqlen-1),G->mblock,G->flags);
1033+
fprintf(fd,"%04d Node %p at %08x..%08x addr=%p flags=%#x\n",
1034+
nn,G,G->key,(G->key+G->seqlen-1),G->addr,G->flags);
10351035
fprintf(fd," AVL (%p:%p),%d,%d,%d,%d\n",G->link[0],G->link[1],
10361036
G->bal,G->cache,G->pad,G->rtag);
10371037
fprintf(fd," source: instr=%d, len=%#x\n",G->seqnum,G->seqlen);
@@ -1054,10 +1054,10 @@ static void DumpTree (FILE *fd)
10541054
B = B->next;
10551055
}
10561056
}
1057-
if (G->addr && G->pmeta) {
1057+
if (G->addr) {
10581058
int i, j, k;
10591059
unsigned char *p = G->addr;
1060-
Addr2Pc *AP = G->pmeta;
1060+
Addr2Pc *AP = G->meta;
10611061
for (i=0; i<G->seqnum; i++) {
10621062
fprintf(fd," %08x:%p",(G->key+AP->dnpc),G->addr+AP->daddr);
10631063
k = 0;
@@ -1148,7 +1148,7 @@ static int TraverseAndClean(void)
11481148
* code addresses. At the end, we reset both CodeBuf and InstrMeta to prepare
11491149
* for a new sequence.
11501150
*/
1151-
TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf)
1151+
TNode *Move2Tree(IMeta *I0, unsigned char *GenCodeBuf)
11521152
{
11531153
TNode *nG = NULL;
11541154
#if PROFILE >= 2
@@ -1162,12 +1162,12 @@ TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf)
11621162
IGen *IG;
11631163
int i, apl=0;
11641164
Addr2Pc *ap;
1165-
CodeBuf *mallmb;
11661165
unsigned int op;
11671166

11681167
key = I0->npc;
11691168

1170-
TNode *G = calloc(1, sizeof(TNode));
1169+
nap = I0->ncount + 1;
1170+
TNode *G = calloc(1, sizeof(TNode) + sizeof(Addr2Pc) * nap);
11711171
if (G==NULL) {
11721172
leavedos_main(0x8201);
11731173
}
@@ -1184,7 +1184,7 @@ TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf)
11841184
/* ->REPLACE the code of the node found with the latest
11851185
compiled version */
11861186
NodeUnlinker(nG);
1187-
if (nG->mblock) dlfree(nG->mblock);
1187+
if (nG->addr) dlfree(nG->addr);
11881188
free(nG);
11891189
}
11901190
else {
@@ -1213,12 +1213,7 @@ TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf)
12131213
__atomic_store_n(&findtree_cache[key&FINDTREE_CACHE_HASH_MASK], nG,
12141214
__ATOMIC_RELAXED);
12151215

1216-
nap = nG->seqnum+1;
1217-
mallmb = GenCodeBuf;
1218-
nG->mblock = GenCodeBuf;
1219-
nG->pmeta = mallmb->meta;
1220-
if (nG->pmeta==NULL) leavedos_main(0x504d45);
1221-
nG->addr = (unsigned char *)&mallmb->meta[nap];
1216+
nG->addr = GenCodeBuf;
12221217

12231218
/* setup structures for inter-node linking */
12241219
nG->unlinked_jmp_targets = 0;
@@ -1242,7 +1237,7 @@ TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf)
12421237
}
12431238

12441239
/* setup source/xlated instruction offsets */
1245-
ap = nG->pmeta;
1240+
ap = nG->meta;
12461241
I = I0;
12471242
for (i=0; i<nG->seqnum; i++) {
12481243
ap->daddr = I->daddr;
@@ -1435,7 +1430,7 @@ TNode *FindTree_unlocked(int key)
14351430

14361431
static void BreakNode(TNode *G, unsigned char *eip)
14371432
{
1438-
Addr2Pc *A = G->pmeta;
1433+
Addr2Pc *A = G->meta;
14391434
int ebase;
14401435
unsigned char *p;
14411436
int i;
@@ -1505,7 +1500,7 @@ int InvalidateNodeRange(int al, int len, unsigned char *eip)
15051500
*/
15061501
/* Exclude last instruction, as there is no need to break
15071502
* node after last instruction (it ends there anyway). */
1508-
ahE = G->addr + G->pmeta[G->seqnum - 1].daddr;
1503+
ahE = G->addr + G->meta[G->seqnum - 1].daddr;
15091504
if (eip && ADDR_IN_RANGE(eip,G->addr,ahE)) {
15101505
if (debug_level('e')>1)
15111506
e_printf("### Node self hit %p->%p..%p\n",

src/base/emu-i386/simx86/trees.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ typedef struct _imeta {
8989
IGen gen[NUMGENS];
9090
} IMeta;
9191

92-
typedef struct _codebufhdr {
93-
Addr2Pc meta[0]; /* there are nap of these */
94-
/* behind these follows the code */
95-
} CodeBuf;
96-
9792
extern IMeta InstrMeta[MAXINODES];
9893
extern int NodesExecd;
9994
extern int TotalNodesExecd;
@@ -127,9 +122,7 @@ typedef struct TNode
127122
int key; /* signed! and don't move it from here! */
128123
/* -------------------------------------------------------------- */
129124
int alive;
130-
CodeBuf *mblock;
131125
unsigned char *addr;
132-
Addr2Pc *pmeta;
133126
unsigned short len, flags, seqlen, seqnum __attribute__ ((packed));
134127
unsigned short nrefs;
135128
linkdesc clink_t;
@@ -138,6 +131,7 @@ typedef struct TNode
138131
backref bkr;
139132
unsigned cs;
140133
unsigned mode;
134+
Addr2Pc meta[]; /* there are seqnum+1 of these */
141135
} TNode;
142136

143137
/* Used for traversing a right-threaded AVL tree. */
@@ -159,7 +153,7 @@ typedef struct avltr_tree
159153
#define MINUS -1
160154

161155
TNode *FindTree(int key);
162-
TNode *Move2Tree(IMeta *I0, CodeBuf *GenCodeBuf);
156+
TNode *Move2Tree(IMeta *I0, unsigned char *GenCodeBuf);
163157
void tree_gc(void);
164158

165159
void InitTrees(void);

0 commit comments

Comments
 (0)