Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newCTFE rebase-dump #14243

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft

newCTFE rebase-dump #14243

wants to merge 26 commits into from

Conversation

maxhaton
Copy link
Member

This adds a preview flag but it shouldn't be merged just yet but you can play with it yourself.

I'll add a markdown file that explains the design in more depth at some point.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @maxhaton!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#14243"

@maxhaton maxhaton added the Trivial typos, formatting, comments label Jun 23, 2022
Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review round on fpconv_ctfe.d

int exp;
};

enum seperate_arrays = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note sure why this is needed. IMO, just pick a value.

Comment on lines 8 to 22
enum npowers = 87;
enum steppowers = 8;
enum firstpower = -348; /* 10 ^ -348 */

enum expmax = -32;
enum expmin = -60;

enum expbits = 11; // 64 - 1 /*for signbit*/ - fracbits;
enum fracbits = 52;

enum fracmask = (1UL << fracbits) - 1;
enum hiddenbit = (1UL << fracbits);
enum expmask = ((1UL << expbits) - 1) << fracbits;
enum signbit = (1UL << (double.sizeof * 8) -1); // 1 << (64 - 1)
enum expbias = (((1 << (expbits - 1)) -1) + fracbits);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be members of Fp

return bits;
}

Fp build_fp(double d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a constructor of Fp

static assert(build_double(Fp(9007199254740991LU, 971)) == double.max);
static assert(() { return build_double(build_fp(double.max)); } () == double.max);

void normalize(Fp* fp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a method

fp.exp -= shift;
}

void get_normalized_boundaries(Fp* fp, Fp* lower, Fp* upper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a method and lower and upper should be out

lower.exp = upper.exp;
}

Fp multiply(const Fp* a, const Fp* b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be opBinary!("*")

}
}

static int grisu2(double d, char* digits, int* K)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these static functions should be private

return generate_digits(&w, &upper, &lower, digits, K);
}

static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review round for ctfe/bc.d

} +/


static if (is(typeof({import dmd.globals : Loc; })))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume this will always be true for building dmd. Why is this a thing?

static assert(!bc_order_errors.length, bc_order_errors);


pragma(msg, 2 ^^ 7 - LongInst.max, " opcodes remaining");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this behind a debug static if of some kind, or just make it a static assert

Compiled,
}

//static if (is(typeof(() { import dmd.declaration : FuncDeclaration; })))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


struct BCFunction
{
void* funcDecl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this always a FuncDeclaration? if it is the type should reflect this.

}
}

extern (D) BCValue genLocal(BCType bct, string name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this explicitly marked extern(D)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it uses the string type which will die with the default extern(C++) used for visitors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not part of a visitor interface though. I don't follow.

emitLongInst(LongInst.HeapStore8, _to.stackAddr, value.stackAddr);
}

void Load16(BCValue _to, BCValue from)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these LoadNN and StoreNN look very similar, there should probably be a common definition.


BCValue pushOntoStack(BCValue val)
{
if (!__ctfe) debug { import std.stdio; writeln("pushOntoStack: ", val); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std usage

{
case LongInst.SetHighImm32:
{
result ~= "SetHigh " ~ localName(stackMap, lw >> 16) ~ ", #" ~ itos(hi) ~ "\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this switch could be less cluttered with a local string function(string, uint) foo("SetHigh ",hi)

}
break;

case LongInst.Jmp:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto for the jump instructions

Comment on lines 2318 to 2320
__gshared int[ushort.max * 2] byteCodeCache;

__gshared int byteCodeCacheTop = 4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do hope these are private... Also they could just be an int[]

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review round for bc_common.d

}


static assert(()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unittest?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unittests don't run at compile time ...

{
BCTypeEnum commonType = commonTypeEnum(this.type.type, rhs.type.type);

if (this.vType == rhs.vType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ((this.vType != rhs.vType) return false;

}
}

pragma(msg, "Sizeof BCValue: ", BCValue.sizeof);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put this behind debug conditional

}

pragma(msg, "Sizeof BCValue: ", BCValue.sizeof);
__gshared static immutable bcLastCond = () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these do not need __gshared and they feel like they should be (static immutable) members of their types

return result;
}();

__gshared static immutable bcNull = () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment on lines 1024 to 1028
__gshared static immutable bcFour = BCValue(Imm32(4));
__gshared static immutable bcOne = BCValue(Imm32(1));
__gshared static immutable bcZero = BCValue(Imm32(0));
__gshared static immutable i32Type = BCType(BCTypeEnum.i32);
__gshared static immutable u32Type = BCType(BCTypeEnum.u32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


BCTypeEnum commonTypeEnum(BCTypeEnum lhs, BCTypeEnum rhs) pure @safe
{
// HACK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what hack?

bool ifTrue;
}

const(uint) align4(const uint val) @safe pure @nogc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these first couple of functions feel like they should be part of root or common, the statics should probably be private.

@ljmf00 ljmf00 removed the Trivial typos, formatting, comments label Jun 25, 2022
@maxhaton
Copy link
Member Author

The whole fpconv module is probably going to be deleted

@maxhaton maxhaton added the Trivial typos, formatting, comments label Jun 25, 2022
Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review round for bc_interpreter.

General comments: lots of std usage for debug.

Comment on lines 116 to 117
{
if (cRetval.vType == BCValueType.Exception)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert(cRetval.vType == BCValueType.Exception); and remove the massive indentation


// we return to unroll
// lets first handle the case in which there are catches on the catch stack
if (catches.length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use early return here


bool Return()
{
if (n_return_addrs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

break;
case LongInst.ImmEq:
{
if ((*lhsStackRef) == imm32c)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cond = (*lhsStackRef) == imm32c);, ditto for the cases that follow this

Comment on lines 591 to 594
uint _lhs = *lhsRef & uint.max;
float flhs = *cast(float*)&_lhs;
uint _rhs = *rhs & uint.max;
float frhs = *cast(float*)&_rhs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a templated local function, templated on (T=={float, double} and make these cases shorter

if (Return()) return cRetval;
}
break;
case LongInst.RetS64:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

{
if (*rhs == 0 && *lhsRef == 0)
{
*lhsStackRef = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use an early break

const lhs_length = _lhs ? loadu32(llbasep) : 0;
const rhs_length = _rhs ? loadu32(rlbasep) : 0;

if (const newLength = lhs_length + rhs_length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

assert(_lhs && _rhs, "trying to deref nullPointers");
if (_lhs == _rhs)
{
cond = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early break

}
}

static assert(testFact().interpret([imm32(5)]) == imm32(120));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fails with

src\dmd\ctfe\bc_interpreter.d(1697): Error: function `dmd.ctfe.bc_interpreter.interpret(ref BCGen gen, BCValue[] args, BCHeap* heapPtr = null)` is not callable using argument types `(BCGen, BCValue[])`

src\dmd\ctfe\bc_interpreter.d(1697):        cannot pass rvalue argument `testFact()` of type `BCGen` to parameter `ref BCGen gen`

src\dmd\ctfe\bc_interpreter.d(1697):        while evaluating: `static assert(interpret(testFact(), [imm32(5u, false)]) == imm32(120u, false))`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants