diff --git a/compiler/src/dmd/backend/cc.d b/compiler/src/dmd/backend/cc.d index 49b14db8fd7..f9c033abacd 100644 --- a/compiler/src/dmd/backend/cc.d +++ b/compiler/src/dmd/backend/cc.d @@ -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 } diff --git a/compiler/src/dmd/backend/dout.d b/compiler/src/dmd/backend/dout.d index 17f6ad1d28d..128a986a04b 100644 --- a/compiler/src/dmd/backend/dout.d +++ b/compiler/src/dmd/backend/dout.d @@ -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; diff --git a/compiler/src/dmd/backend/dt.d b/compiler/src/dmd/backend/dt.d index b728baf58a5..54d09c88aa8 100644 --- a/compiler/src/dmd/backend/dt.d +++ b/compiler/src/dmd/backend/dt.d @@ -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); @@ -383,7 +390,7 @@ nothrow: /*********************** * Write a bunch of zeros */ - void nzeros(uint size) + void nzeros(size_t size) { if (!size) return; diff --git a/compiler/src/dmd/backend/pdata.d b/compiler/src/dmd/backend/pdata.d index 5f9f0749812..e502af5b7ee 100644 --- a/compiler/src/dmd/backend/pdata.d +++ b/compiler/src/dmd/backend/pdata.d @@ -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(); } diff --git a/compiler/src/dmd/glue.d b/compiler/src/dmd/glue.d index 0a1767c39e6..76ba39b00f4 100644 --- a/compiler/src/dmd/glue.d +++ b/compiler/src/dmd/glue.d @@ -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); diff --git a/compiler/src/dmd/objc_glue.d b/compiler/src/dmd/objc_glue.d index db17af93d60..a705fd0c54f 100644 --- a/compiler/src/dmd/objc_glue.d +++ b/compiler/src/dmd/objc_glue.d @@ -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]; diff --git a/compiler/src/dmd/todt.d b/compiler/src/dmd/todt.d index 7b7114abee8..3d04a8e40bb 100644 --- a/compiler/src/dmd/todt.d +++ b/compiler/src/dmd/todt.d @@ -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) @@ -319,7 +319,7 @@ 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; } @@ -327,7 +327,7 @@ void Expression_toDt(Expression e, ref DtBuilder dtb) case Timaginary64: { auto dvalue = cast(double)e.value; - dtb.nbytes(8, cast(char*)&dvalue); + dtb.nbytes((cast(ubyte*)&dvalue)[0 .. 8]); break; } @@ -335,7 +335,7 @@ void Expression_toDt(Expression e, ref DtBuilder dtb) 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; } @@ -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; } @@ -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(); @@ -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; @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/compiler/src/dmd/toobj.d b/compiler/src/dmd/toobj.d index 46cdae5d1a3..adcffcfa443 100644 --- a/compiler/src/dmd/toobj.d +++ b/compiler/src/dmd/toobj.d @@ -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); } @@ -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]); } ////////////////////////////////////////////// @@ -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); } @@ -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]); } ////////////////////////////////////////////// @@ -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); }