Skip to content

Commit

Permalink
Shrink Loc from 12 to 4 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jan 26, 2025
1 parent b289b6a commit 065be3d
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 178 deletions.
1 change: 0 additions & 1 deletion compiler/src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -6324,7 +6324,6 @@ final class CParser(AST) : Parser!AST
while (*p)
++p;
++p; // advance to start of next line
scanloc.linnum = scanloc.linnum + 1;
}

if (newSymbols.length)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ extern (C++) final class Module : Package
checkCompiledImport();
members = p.parseModule();
assert(!p.md); // C doesn't have module declarations
numlines = p.scanloc.linnum;
numlines = p.linnum;
}
else
{
Expand All @@ -831,7 +831,7 @@ extern (C++) final class Module : Package
checkCompiledImport();

members = p.parseModuleContent();
numlines = p.scanloc.linnum;
numlines = p.linnum;
}

/* The symbol table into which the module is to be inserted.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/dstruct.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ extern (C++) class StructDeclaration : AggregateDeclaration
extern (D) this(const ref Loc loc, Identifier id, bool inObject)
{
super(loc, id);
zeroInit = false; // assume false until we do semantic processing
// zeroInit = false; // assume false until we do semantic processing
ispod = ThreeState.none;
// For forward references
type = new TypeStruct(this);
Expand Down
41 changes: 24 additions & 17 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1848,8 +1848,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
buf.writeByte(0);
const str = buf.extractSlice()[0 .. len];
const bool doUnittests = global.params.parsingUnittestsRequired();
auto loc = adjustLocForMixin(str, cd.loc, global.params.mixinOut);
scope p = new Parser!ASTCodegen(loc, sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
adjustLocForMixin(str, cd.loc, *p.baseLoc, global.params.mixinOut);
p.linnum = p.baseLoc.startLine;
p.nextToken();

auto d = p.parseDeclDefs(0);
Expand Down Expand Up @@ -5920,34 +5921,40 @@ private CallExp doAtomicOp (string op, Identifier var, Expression arg)
* Set up loc for a parse of a mixin. Append the input text to the mixin.
* Params:
* input = mixin text
* loc = location to adjust
* loc = location of expansion
* baseLoc = location to adjust
* mixinOut = sink for mixin text data
* Returns:
* adjusted loc suitable for Parser
*/

Loc adjustLocForMixin(const(char)[] input, ref const Loc loc, ref Output mixinOut)
void adjustLocForMixin(const(char)[] input, Loc loc, ref BaseLoc baseLoc, ref Output mixinOut)
{
Loc result;
if (mixinOut.doOutput)
{
const lines = mixinOut.bufferLines;
writeMixin(input, loc, mixinOut.bufferLines, *mixinOut.buffer);
result = Loc(mixinOut.name.ptr, lines + 2, loc.charnum);
baseLoc.startLine = lines + 2;
baseLoc.filename = mixinOut.name;
return;
}
else if (loc.filename)

SourceLoc sl = SourceLoc(loc);
if (sl.filename.length == 0)
{
/* Create a pseudo-filename for the mixin string, as it may not even exist
* in the source file.
*/
auto len = strlen(loc.filename) + 7 + (loc.linnum).sizeof * 3 + 1;
char* filename = cast(char*)mem.xmalloc(len);
snprintf(filename, len, "%s-mixin-%d", loc.filename, cast(int)loc.linnum);
result = Loc(filename, loc.linnum, loc.charnum);
// Rare case of compiler-generated mixin exp, e.g. __xtoHash
baseLoc.filename = "";
return;
}
else
result = loc;
return result;

/* Create a pseudo-filename for the mixin string, as it may not even exist
* in the source file.
*/
auto len = sl.filename.length + 7 + (sl.linnum).sizeof * 3 + 1;
char* filename = cast(char*) mem.xmalloc(len);
snprintf(filename, len, "%.*s-mixin-%d", cast(int) sl.filename.length, sl.filename.ptr, cast(int) sl.linnum);
baseLoc.startLine = sl.line;
baseLoc.filename = filename.toDString;
}

/**************************************
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7654,8 +7654,9 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
const len = buf.length;
const str = buf.extractChars()[0 .. len];
const bool doUnittests = global.params.parsingUnittestsRequired();
auto loc = adjustLocForMixin(str, exp.loc, global.params.mixinOut);
scope p = new Parser!ASTCodegen(loc, sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
adjustLocForMixin(str, exp.loc, *p.baseLoc, global.params.mixinOut);
p.linnum = p.baseLoc.startLine;
p.nextToken();
//printf("p.loc.linnum = %d\n", p.loc.linnum);

Expand Down
45 changes: 20 additions & 25 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,23 @@ enum class MessageStyle : uint8_t
struct Loc final
{
private:
uint32_t _linnum;
uint32_t _charnum;
uint32_t fileIndex;
uint32_t index;
public:
static bool showColumns;
static MessageStyle messageStyle;
static void set(bool showColumns, MessageStyle messageStyle);
Loc(const char* filename, uint32_t linnum, uint32_t charnum);
uint32_t charnum() const;
uint32_t charnum(uint32_t num);
uint32_t linnum() const;
uint32_t linnum(uint32_t num);
const char* filename() const;
void filename(const char* name);
const char* toChars(bool showColumns = Loc::showColumns, MessageStyle messageStyle = Loc::messageStyle) const;
bool equals(const Loc& loc) const;
Loc() :
_linnum(),
_charnum(),
fileIndex()
index(0u)
{
}
Loc(uint32_t index) :
index(index)
{}
};

enum class PASS : uint8_t
Expand Down Expand Up @@ -5402,23 +5397,23 @@ struct UnionExp final
private:
union _AnonStruct_u
{
char exp[30LLU];
char integerexp[40LLU];
char errorexp[30LLU];
char exp[22LLU];
char integerexp[32LLU];
char errorexp[22LLU];
char realexp[48LLU];
char complexexp[64LLU];
char symoffexp[64LLU];
char stringexp[51LLU];
char arrayliteralexp[48LLU];
char assocarrayliteralexp[56LLU];
char structliteralexp[72LLU];
char compoundliteralexp[40LLU];
char nullexp[30LLU];
char dotvarexp[49LLU];
char addrexp[40LLU];
char indexexp[58LLU];
char sliceexp[65LLU];
char vectorexp[53LLU];
char symoffexp[56LLU];
char stringexp[43LLU];
char arrayliteralexp[40LLU];
char assocarrayliteralexp[48LLU];
char structliteralexp[64LLU];
char compoundliteralexp[32LLU];
char nullexp[22LLU];
char dotvarexp[41LLU];
char addrexp[32LLU];
char indexexp[50LLU];
char sliceexp[57LLU];
char vectorexp[45LLU];
};
#pragma pack(pop)

Expand Down
18 changes: 2 additions & 16 deletions compiler/src/dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,7 @@ typedef unsigned long long uinteger_t;
struct Loc
{
private:
unsigned _linnum;
unsigned _charnum;
unsigned fileIndex;
unsigned int index;
public:
static void set(bool showColumns, MessageStyle messageStyle);

Expand All @@ -428,24 +426,12 @@ struct Loc

Loc()
{
_linnum = 0;
_charnum = 0;
fileIndex = 0;
}

Loc(const char *filename, unsigned linnum, unsigned charnum)
{
this->linnum(linnum);
this->charnum(charnum);
this->filename(filename);
index = 0;
}

uint32_t charnum() const;
uint32_t charnum(uint32_t num);
uint32_t linnum() const;
uint32_t linnum(uint32_t num);
const char *filename() const;
void filename(const char *name);

const char *toChars(
bool showColumns = Loc::showColumns,
Expand Down
11 changes: 6 additions & 5 deletions compiler/src/dmd/identifier.d
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ nothrow:
extern (D) static Identifier generateIdWithLoc(string prefix, const ref Loc loc, string parent = "")
{
// generate `<prefix>_L<line>_C<col>`
auto sl = SourceLoc(loc);
OutBuffer idBuf;
idBuf.writestring(prefix);
idBuf.writestring("_L");
idBuf.print(loc.linnum);
idBuf.print(sl.line);
idBuf.writestring("_C");
idBuf.print(loc.charnum);
idBuf.print(sl.column);

/**
* Make sure the identifiers are unique per filename, i.e., per module/mixin
Expand All @@ -244,13 +245,13 @@ nothrow:
* directly, but that would unnecessary lengthen symbols names. See issue:
* https://issues.dlang.org/show_bug.cgi?id=23722
*/
static struct Key { Loc loc; string prefix; string parent; }
static struct Key { uint fileOffset; string prefix; string parent; }
__gshared uint[Key] counters;

static if (__traits(compiles, counters.update(Key.init, () => 0u, (ref uint a) => 0u)))
{
// 2.082+
counters.update(Key(loc, prefix, parent),
counters.update(Key(loc.fileOffset, prefix, parent),
() => 1u, // insertion
(ref uint counter) // update
{
Expand All @@ -262,7 +263,7 @@ nothrow:
}
else
{
const key = Key(loc, prefix, parent);
const key = Key(loc.fileOffset, prefix, parent);
if (auto pCounter = key in counters)
{
idBuf.writestring("_");
Expand Down
Loading

0 comments on commit 065be3d

Please sign in to comment.