Skip to content

Commit

Permalink
Make dt_t.nbytes accept a slice instead of size+pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Korpel committed Jan 7, 2025
1 parent 6523a70 commit ba8b992
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 40 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dmd/backend/cc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,8 @@ struct dt_t
targ_size_t DTazeros; // DTazeros,DTcommon,DTsymsize
struct // DTabytes
{
byte *DTpbytes; // pointer to the bytes
uint DTnbytes; // # of bytes
byte* DTpbytes; // pointer to the bytes
size_t DTnbytes; // # of bytes
int DTseg; // segment it went into
targ_size_t DTabytes; // offset of abytes for DTabytes
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/dout.d
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ Symbol *out_string_literal(const(char)* str, uint len, uint sz)
}

auto dtb = DtBuilder(0);
dtb.nbytes(cast(uint)(len * sz), str);
dtb.nbytes(str[0 .. len * sz]);
dtb.nzeros(cast(uint)sz); // include terminating 0
s.Sdt = dtb.finish();
s.Sfl = FLdata;
Expand Down
33 changes: 20 additions & 13 deletions compiler/src/dmd/backend/dt.d
Original file line number Diff line number Diff line change
Expand Up @@ -252,37 +252,44 @@ nothrow:
/***********************
* Append data represented by ptr[0..size]
*/
void nbytes(const(char)[] ptr)
{
return nbytes(cast(const(ubyte)[]) ptr);
}

/// ditto
@trusted
void nbytes(uint size, const(char)* ptr)
void nbytes(const(ubyte)[] data)
{
if (!size)
if (!data.length)
return;

bool allZero = true;
foreach (i; 0 .. size)
foreach (i; 0 .. data.length)
{
if (ptr[i] != 0)
if (data.ptr[i] != 0)
{
allZero = false;
break;
}
}
if (allZero)
return nzeros(size);
return nzeros(data.length);

dt_t *dt;

if (size < dt_t.DTibytesMax)
{ dt = dt_calloc(DT_ibytes);
dt.DTn = cast(ubyte)size;
memcpy(dt.DTdata.ptr,ptr,size);
if (data.length < dt_t.DTibytesMax)
{
dt = dt_calloc(DT_ibytes);
dt.DTn = cast(ubyte)data.length;
memcpy(dt.DTdata.ptr, data.ptr, data.length);
}
else
{
dt = dt_calloc(DT_nbytes);
dt.DTnbytes = size;
dt.DTpbytes = cast(byte *) mem_malloc(size);
memcpy(dt.DTpbytes,ptr,size);
dt.DTnbytes = data.length;
dt.DTpbytes = cast(byte*) mem_malloc(data.length);
memcpy(dt.DTpbytes, data.ptr, data.length);
}

assert(!*pTail);
Expand Down Expand Up @@ -383,7 +390,7 @@ nothrow:
/***********************
* Write a bunch of zeros
*/
void nzeros(uint size)
void nzeros(size_t size)
{
if (!size)
return;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/backend/pdata.d
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,6 @@ static if (1)
ui.UnwindCode[ui.CountOfCodes-1].FrameOffset = setUnwindCode(1, UWOP.PUSH_NONVOL, BP);

auto dtb = DtBuilder(0);
dtb.nbytes(4 + ((ui.CountOfCodes + 1) & ~1) * 2,cast(char *)&ui);
dtb.nbytes((cast(const(ubyte*)) &ui)[0 .. 4 + ((ui.CountOfCodes + 1) & ~1) * 2]);
return dtb.finish();
}
2 changes: 1 addition & 1 deletion compiler/src/dmd/glue.d
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ private void genObjFile(Module m, bool multiobj)
bcov.Sfl = FLdata;

auto dtb = DtBuilder(0);
dtb.nbytes(cast(uint)(m.covb.length * m.covb[0].sizeof), cast(char*)m.covb.ptr);
dtb.nbytes(cast(const(ubyte)[]) m.covb);
bcov.Sdt = dtb.finish();

outdata(bcov);
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/objc_glue.d
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ static:

// create data
auto dtb = DtBuilder(0);
dtb.nbytes(cast(uint) (str.length + 1), str.toStringz());
dtb.nbytes(str.toStringz()[0 .. str.length + 1]);

// find segment
auto seg = Segments[segment];
Expand Down
32 changes: 16 additions & 16 deletions compiler/src/dmd/todt.d
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ void Expression_toDt(Expression e, ref DtBuilder dtb)
{
//printf("IntegerExp.toDt() %d\n", e.op);
auto value = e.getInteger();
dtb.nbytes(cast(uint)e.type.size(), cast(char*) &value);
dtb.nbytes((cast(ubyte*) &value)[0 .. e.type.size()]);
}

void visitReal(RealExp e)
Expand All @@ -319,23 +319,23 @@ void Expression_toDt(Expression e, ref DtBuilder dtb)
case Timaginary32:
{
auto fvalue = cast(float)e.value;
dtb.nbytes(4, cast(char*)&fvalue);
dtb.nbytes((cast(ubyte*)&fvalue)[0 .. 4]);
break;
}

case Tfloat64:
case Timaginary64:
{
auto dvalue = cast(double)e.value;
dtb.nbytes(8, cast(char*)&dvalue);
dtb.nbytes((cast(ubyte*)&dvalue)[0 .. 8]);
break;
}

case Tfloat80:
case Timaginary80:
{
auto evalue = e.value;
dtb.nbytes(target.realsize - target.realpad, cast(char*)&evalue);
dtb.nbytes((cast(ubyte*)&evalue)[0 .. target.realsize - target.realpad]);
dtb.nzeros(target.realpad);
break;
}
Expand All @@ -354,28 +354,28 @@ void Expression_toDt(Expression e, ref DtBuilder dtb)
case Tcomplex32:
{
auto fvalue = cast(float)creall(e.value);
dtb.nbytes(4, cast(char*)&fvalue);
dtb.nbytes((cast(ubyte*)&fvalue)[0 .. 4]);
fvalue = cast(float)cimagl(e.value);
dtb.nbytes(4, cast(char*)&fvalue);
dtb.nbytes((cast(ubyte*)&fvalue)[0 .. 4]);
break;
}

case Tcomplex64:
{
auto dvalue = cast(double)creall(e.value);
dtb.nbytes(8, cast(char*)&dvalue);
dtb.nbytes((cast(ubyte*)&dvalue)[0 .. 8]);
dvalue = cast(double)cimagl(e.value);
dtb.nbytes(8, cast(char*)&dvalue);
dtb.nbytes((cast(ubyte*)&dvalue)[0 .. 8]);
break;
}

case Tcomplex80:
{
auto evalue = creall(e.value);
dtb.nbytes(target.realsize - target.realpad, cast(char*)&evalue);
dtb.nbytes((cast(ubyte*)&evalue)[0 .. target.realsize - target.realpad]);
dtb.nzeros(target.realpad);
evalue = cimagl(e.value);
dtb.nbytes(target.realsize - target.realpad, cast(char*)&evalue);
dtb.nbytes((cast(ubyte*)&evalue)[0 .. target.realsize - target.realpad]);
dtb.nzeros(target.realpad);
break;
}
Expand Down Expand Up @@ -435,7 +435,7 @@ void Expression_toDt(Expression e, ref DtBuilder dtb)
{
auto tsa = t.isTypeSArray();

dtb.nbytes(n * e.sz, p);
dtb.nbytes((cast(ubyte*) p)[0 .. n * e.sz]);
if (tsa.dim)
{
dinteger_t dim = tsa.dim.toInteger();
Expand Down Expand Up @@ -855,7 +855,7 @@ private void membersToDt(AggregateDeclaration ad, ref DtBuilder dtb,
offset = bitByteOffset;
}

dtb.nbytes(bitFieldSize, cast(char*)&bitFieldValue);
dtb.nbytes((cast(ubyte*) &bitFieldValue)[0 .. bitFieldSize]);
offset += bitFieldSize;
bitOffset = 0;
bitFieldValue = 0;
Expand Down Expand Up @@ -1280,7 +1280,7 @@ private extern (C++) class TypeInfoDtVisitor : Visitor
}

// Put out name[] immediately following TypeInfo_Enum
dtb.nbytes(cast(uint)(namelen + 1), name);
dtb.nbytes(name[0 .. namelen + 1]);
}

override void visit(TypeInfoPointerDeclaration d)
Expand Down Expand Up @@ -1384,7 +1384,7 @@ private extern (C++) class TypeInfoDtVisitor : Visitor
dtb.xoff(d.csym, Type.typeinfofunction.structsize);

// Put out name[] immediately following TypeInfo_Function
dtb.nbytes(cast(uint)(namelen + 1), name);
dtb.nbytes(name[0 .. namelen + 1]);
}

override void visit(TypeInfoDelegateDeclaration d)
Expand All @@ -1408,7 +1408,7 @@ private extern (C++) class TypeInfoDtVisitor : Visitor
dtb.xoff(d.csym, Type.typeinfodelegate.structsize);

// Put out name[] immediately following TypeInfo_Delegate
dtb.nbytes(cast(uint)(namelen + 1), name);
dtb.nbytes(name[0 .. namelen + 1]);
}

override void visit(TypeInfoStructDeclaration d)
Expand Down Expand Up @@ -1572,7 +1572,7 @@ private extern (C++) class TypeInfoDtVisitor : Visitor
dtb.size(0);

// Put out mangledName[] immediately following TypeInfo_Struct
dtb.nbytes(cast(uint)(mangledNameLen + 1), mangledName);
dtb.nbytes(mangledName[0 .. mangledNameLen + 1]);
}

override void visit(TypeInfoClassDeclaration d)
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dmd/toobj.d
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ void genModuleInfo(Module m)
m.nameoffset = dtb.length();
const(char) *name = m.toPrettyChars();
m.namelen = strlen(name);
dtb.nbytes(cast(uint)m.namelen + 1, name);
dtb.nbytes(name[0 .. m.namelen + 1]);
//printf("nameoffset = x%x\n", nameoffset);
}

Expand Down Expand Up @@ -1399,7 +1399,7 @@ Louter:
import dmd.common.blake3;
const hash = blake3((cast(ubyte*)name)[0 .. namelen]);
//truncate and use the first 16 bytes only
dtb.nbytes(16, cast(char*)hash.ptr);
dtb.nbytes(hash[0 .. 16]);
}

//////////////////////////////////////////////
Expand Down Expand Up @@ -1465,7 +1465,7 @@ Louter:

dtpatchoffset(pdtname, offset);

dtb.nbytes(cast(uint)(namelen + 1), name);
dtb.nbytes(name[0 .. namelen + 1]);
const size_t namepad = -(namelen + 1) & (target.ptrsize - 1); // align
dtb.nzeros(cast(uint)namepad);
}
Expand Down Expand Up @@ -1608,7 +1608,7 @@ private void InterfaceInfoToDt(ref DtBuilder dtb, InterfaceDeclaration id)
import dmd.common.blake3;
const hash = blake3((cast(ubyte*)name)[0 .. namelen]);
//only use the first 16 bytes
dtb.nbytes(16, cast(char*)hash.ptr);
dtb.nbytes(hash[0 .. 16]);
}

//////////////////////////////////////////////
Expand Down Expand Up @@ -1637,7 +1637,7 @@ private void InterfaceInfoToDt(ref DtBuilder dtb, InterfaceDeclaration id)

dtpatchoffset(pdtname, offset);

dtb.nbytes(cast(uint)(namelen + 1), name);
dtb.nbytes(name[0 .. namelen + 1]);
const size_t namepad = -(namelen + 1) & (target.ptrsize - 1); // align
dtb.nzeros(cast(uint)namepad);
}

0 comments on commit ba8b992

Please sign in to comment.