Skip to content

Commit

Permalink
move funcoffset to CGstate (dlang#16475)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored May 12, 2024
1 parent e7d7752 commit efbdea4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
13 changes: 6 additions & 7 deletions compiler/src/dmd/backend/cgcod.d
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ regm_t allregs; // ALLREGS optionally including mBP

int dfoidx; /* which block we are in */

targ_size_t funcoffset; // offset of start of function
targ_size_t prolog_allocoffset; // offset past adj of stack allocation
targ_size_t startoffset; // size of function entry code
targ_size_t retoffset; /* offset from start of func to ret code */
Expand Down Expand Up @@ -349,7 +348,7 @@ void codgen(Symbol *sfunc)
if (cprolog)
pinholeopt(cprolog,null); // optimize

funcoffset = Offset(sfunc.Sseg);
cgstate.funcoffset = Offset(sfunc.Sseg);
targ_size_t coffset = Offset(sfunc.Sseg);

if (eecontext.EEelem)
Expand Down Expand Up @@ -383,7 +382,7 @@ void codgen(Symbol *sfunc)
pinholeopt(b.Bcode,b); // do pinhole optimization
if (b.Bflags & BFL.prolog) // do function prolog
{
startoffset = coffset + calcblksize(cprolog) - funcoffset;
startoffset = coffset + calcblksize(cprolog) - cgstate.funcoffset;
b.Bcode = cat(cprolog,b.Bcode);
}
cgsched_block(b);
Expand Down Expand Up @@ -502,7 +501,7 @@ void codgen(Symbol *sfunc)

assert(0);
}
sfunc.Ssize = Offset(sfunc.Sseg) - funcoffset; // size of function
sfunc.Ssize = Offset(sfunc.Sseg) - cgstate.funcoffset; // size of function

if (configv.vasm)
disassemble(disasmBuf[]); // disassemble the code
Expand Down Expand Up @@ -531,7 +530,7 @@ void codgen(Symbol *sfunc)
case BCret:
case BCretexp:
/* Compute offset to return code from start of function */
retoffset = b.Boffset + b.Bsize - cgstate.retsize - funcoffset;
retoffset = b.Boffset + b.Bsize - cgstate.retsize - cgstate.funcoffset;

/* Add 3 bytes to retoffset in case we have an exception
* handler. THIS PROBABLY NEEDS TO BE IN ANOTHER SPOT BUT
Expand All @@ -542,7 +541,7 @@ void codgen(Symbol *sfunc)
break;

default:
retoffset = b.Boffset + b.Bsize - funcoffset;
retoffset = b.Boffset + b.Bsize - cgstate.funcoffset;
break;
}
}
Expand All @@ -552,7 +551,7 @@ void codgen(Symbol *sfunc)
*/
/* Instead, try offset to cleanup code */
if (retoffset < sfunc.Ssize)
objmod.linnum(sfunc.Sfunc.Fendline,sfunc.Sseg,funcoffset + retoffset);
objmod.linnum(sfunc.Sfunc.Fendline,sfunc.Sseg,cgstate.funcoffset + retoffset);

static if (MARS)
{
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dmd/backend/cod3.d
Original file line number Diff line number Diff line change
Expand Up @@ -7364,13 +7364,13 @@ private void do64bit(ref MiniCodeBuf pbuf, FL fl, ref evc uev,int flags)

case FLblock: /* displacement to another block */
ad = uev.Vblock.Boffset - pbuf.getOffset() - 4;
//printf("FLblock: funcoffset = %x, pbuf.getOffset = %x, Boffset = %x, ad = %x\n", funcoffset, pbuf.getOffset(), uev.Vblock.Boffset, ad);
//printf("FLblock: funcoffset = %x, pbuf.getOffset = %x, Boffset = %x, ad = %x\n", cgstate.funcoffset, pbuf.getOffset(), uev.Vblock.Boffset, ad);
goto L1;

case FLblockoff:
pbuf.flush();
assert(uev.Vblock);
//printf("FLblockoff: offset = %x, Boffset = %x, funcoffset = %x\n", pbuf.offset, uev.Vblock.Boffset, funcoffset);
//printf("FLblockoff: offset = %x, Boffset = %x, funcoffset = %x\n", pbuf.offset, uev.Vblock.Boffset, cgstate.funcoffset);
pbuf.write64(uev.Vblock.Boffset);
objmod.reftocodeseg(pbuf.seg,pbuf.offset,uev.Vblock.Boffset);
break;
Expand Down Expand Up @@ -7544,13 +7544,13 @@ private void do32bit(ref MiniCodeBuf pbuf, FL fl, ref evc uev,int flags, int val

case FLblock: /* displacement to another block */
ad = uev.Vblock.Boffset - pbuf.getOffset() - 4;
//printf("FLblock: funcoffset = %x, pbuf.getOffset = %x, Boffset = %x, ad = %x\n", funcoffset, pbuf.getOffset(), uev.Vblock.Boffset, ad);
//printf("FLblock: funcoffset = %x, pbuf.getOffset = %x, Boffset = %x, ad = %x\n", cgstate.funcoffset, pbuf.getOffset(), uev.Vblock.Boffset, ad);
goto L1;

case FLblockoff:
pbuf.flush();
assert(uev.Vblock);
//printf("FLblockoff: offset = %x, Boffset = %x, funcoffset = %x\n", pbuf.offset, uev.Vblock.Boffset, funcoffset);
//printf("FLblockoff: offset = %x, Boffset = %x, funcoffset = %x\n", pbuf.offset, uev.Vblock.Boffset, cgstate.funcoffset);
objmod.reftocodeseg(pbuf.seg,pbuf.offset,uev.Vblock.Boffset);
pbuf.write32(cast(uint)(uev.Vblock.Boffset));
break;
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/backend/code.d
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ struct CGstate

regm_t[4] lastRetregs; // used to not allocate the same register over and over again,
// to improve instruction scheduling
targ_size_t funcoffset; // offset of start of function
targ_size_t retsize; // size of function return code
}

Expand Down
24 changes: 12 additions & 12 deletions compiler/src/dmd/backend/dwarfdbginf.d
Original file line number Diff line number Diff line change
Expand Up @@ -1912,8 +1912,8 @@ static if (1)
debug_info.buf.writeuLEB128(sfunc.Sfunc.Fstartline.Scharnum); // DW_AT_decl_column

// DW_AT_low_pc and DW_AT_high_pc
dwarf_appreladdr(debug_info.seg, debug_info.buf, seg, funcoffset);
dwarf_appreladdr(debug_info.seg, debug_info.buf, seg, funcoffset + sfunc.Ssize);
dwarf_appreladdr(debug_info.seg, debug_info.buf, seg, cgstate.funcoffset);
dwarf_appreladdr(debug_info.seg, debug_info.buf, seg, cgstate.funcoffset + sfunc.Ssize);

// DW_AT_frame_base
if (config.objfmt == OBJ_ELF)
Expand Down Expand Up @@ -2027,14 +2027,14 @@ static if (1)

if (sd.SDaranges_offset)
// Extend existing entry size
*cast(ulong *)(debug_aranges.buf.buf + sd.SDaranges_offset + _tysize[TYnptr]) = funcoffset + sfunc.Ssize;
*cast(ulong *)(debug_aranges.buf.buf + sd.SDaranges_offset + _tysize[TYnptr]) = cgstate.funcoffset + sfunc.Ssize;
else
{ // Add entry
sd.SDaranges_offset = cast(uint)debug_aranges.buf.length();
// address of start of .text segment
dwarf_appreladdr(debug_aranges.seg, debug_aranges.buf, seg, 0);
// size of .text segment
append_addr(debug_aranges.buf, funcoffset + sfunc.Ssize);
append_addr(debug_aranges.buf, cgstate.funcoffset + sfunc.Ssize);
}

/* ============= debug_ranges =========================== */
Expand All @@ -2043,8 +2043,8 @@ static if (1)
* indicate this by adding to the debug_ranges
*/
// start of function and end of function
dwarf_appreladdr(debug_ranges.seg, debug_ranges.buf, seg, funcoffset);
dwarf_appreladdr(debug_ranges.seg, debug_ranges.buf, seg, funcoffset + sfunc.Ssize);
dwarf_appreladdr(debug_ranges.seg, debug_ranges.buf, seg, cgstate.funcoffset);
dwarf_appreladdr(debug_ranges.seg, debug_ranges.buf, seg, cgstate.funcoffset + sfunc.Ssize);

/* ============= debug_loc =========================== */

Expand All @@ -2055,22 +2055,22 @@ static if (1)

// set the entry for this function in .debug_loc segment
// after call
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + 0);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + 1);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + 0);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + 1);

loc_op = cast(ushort)(((cgstate.Para.size - REGSIZE) << 8) | (DW_OP_breg0 + dwarf_regno(SP)));
debug_loc.buf.write32(loc_op << 16 | op_size);

// after push EBP
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + 1);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + 3);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + 1);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + 3);

loc_op = cast(ushort)(((cgstate.Para.size) << 8) | (DW_OP_breg0 + dwarf_regno(SP)));
debug_loc.buf.write32(loc_op << 16 | op_size);

// after mov EBP, ESP
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + 3);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, funcoffset + sfunc.Ssize);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + 3);
dwarf_appreladdr(debug_loc.seg, debug_loc.buf, seg, cgstate.funcoffset + sfunc.Ssize);

loc_op = cast(ushort)(((cgstate.Para.size) << 8) | (DW_OP_breg0 + dwarf_regno(BP)));
debug_loc.buf.write32(loc_op << 16 | op_size);
Expand Down

0 comments on commit efbdea4

Please sign in to comment.