Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/cc.d
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ struct Symbol
nothrow:

Symbol* Sl, Sr; // left, right child
Symbol* Snext; // next in threaded list
Symbol* Sforward; // forward to another Symbol
Symbol* Sisym; // import version of this symbol
dt_t* Sdt; // variables: initializer
int Salignment; // variables: alignment, 0 or -1 means default alignment
Expand Down
56 changes: 33 additions & 23 deletions compiler/src/dmd/backend/machobj.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,11 @@ nothrow:

private enum log = false;

alias _compare_fp_t = extern(C) nothrow int function(const void*, const void*);
extern(C) void qsort(void* base, size_t nmemb, size_t size, _compare_fp_t compar);

import dmd.backend.dwarf;
import dmd.backend.mach;

alias nlist = dmd.backend.mach.nlist; // avoid conflict with dmd.backend.dlist.nlist

/****************************************
* Sort the relocation entry buffer.
* put before nothrow because qsort was not marked nothrow until version 2.086
*/

extern (C)
@trusted
private int mach_rel_fp(scope const(void*) e1, scope const(void*) e2)
{ Relocation* r1 = cast(Relocation*)e1;
Relocation* r2 = cast(Relocation*)e2;

return cast(int)(r1.offset - r2.offset);
}

@trusted
void mach_relsort(OutBuffer* buf)
{
qsort(buf.buf, buf.length() / Relocation.sizeof, Relocation.sizeof, &mach_rel_fp);
}

struct MachObj
{
OutBuffer* fobjbuf;
Expand Down Expand Up @@ -525,6 +502,13 @@ void patch(seg_data* pseg, targ_size_t offset, int seg, targ_size_t value)
@trusted
int mach_numbersyms()
{
// Sort Symbols for faster searching
sortSymbols(machobj.localSymbols[]);
sortSymbols(machobj.publicSymbols[]);
//sortSymbols(machobj.externSymbols[]); // no need to sort these
//sortComdefs(machobj.comdefs[]); // not implemented yet


//printf("mach_numbersyms()\n");
int n = 0;

Expand Down Expand Up @@ -3701,3 +3685,29 @@ void dumpFixup(ref Symbol s, uint fixup)
symbol_print(s);
printf("fixup %s RELOC_%s\n", s.Sident.ptr, reloc[fixup]);
}

/****************************************
* Sort the array of Symbol pointers by their identifiers.
* Params:
* symbols = array to be in-place sorted
*/

@trusted
private void sortSymbols(Symbol*[] symbols)
{
qsort(symbols.ptr, symbols.length, symbols.ptr.sizeof, &symbolQsortFp);
}

/** qsort() comparison function
*/
extern (C)
@trusted
private int symbolQsortFp(scope const(void*) e1, scope const(void*) e2)
{ Symbol* s1 = *cast(Symbol**)e1;
Symbol* s2 = *cast(Symbol**)e2;

return strcmp(s1.Sident.ptr, s2.Sident.ptr);
}

alias _compare_fp_t = extern(C) nothrow int function(const void*, const void*);
extern(C) void qsort(void* base, size_t nmemb, size_t size, _compare_fp_t compar);
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/symbol.d
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ Symbol* symbol_copy(ref Symbol s)
/*printf("symbol_copy(%s)\n",s.Sident.ptr);*/
scopy = symbol_calloc(s.Sident.ptr[0 .. strlen(s.Sident.ptr)]);
memcpy(scopy, &s, Symbol.sizeof - s.Sident.sizeof);
scopy.Sl = scopy.Sr = scopy.Snext = null;
scopy.Sl = scopy.Sr = scopy.Sforward = null;
scopy.Ssymnum = SYMIDX.max;
if (scopy.Sdt)
{
Expand Down
Loading