Skip to content

Commit dcfe231

Browse files
committed
More work on getting expressions to work
1 parent bdcef5d commit dcfe231

41 files changed

Lines changed: 611 additions & 223 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Genbox.FastData.Abstracts;
2+
using Genbox.FastData.Generator.CPlusPlus.Internal;
3+
using Genbox.FastData.Generator.CPlusPlus.Internal.Framework;
4+
using Genbox.FastData.Generator.Framework;
5+
using Genbox.FastData.InternalShared;
6+
7+
namespace Genbox.FastData.Generator.CPlusPlus.Tests;
8+
9+
public class ExpressionCompilerTests
10+
{
11+
[Theory]
12+
[ClassData(typeof(ExpressionHashDataClass))]
13+
internal async Task GenerateExpression(HashType type, IStringHash hash)
14+
{
15+
CPlusPlusExpressionCompiler compiler = new CPlusPlusExpressionCompiler(new TypeHelper(new TypeMap(new CPlusPlusLanguageDef().TypeDefinitions)));
16+
17+
string code = compiler.GetCode(hash.GetExpression());
18+
Assert.NotEmpty(code);
19+
20+
await Verify(code)
21+
.UseFileName($"{hash.GetType().Name}-{type}")
22+
.UseDirectory("Expressions")
23+
.DisableDiff();
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
 int32_t length;
2+
int32_t offset;
3+
uint64_t hash;
4+
length = value.Length;
5+
offset = 0;
6+
length -= offset;
7+
while ((length > 0))
8+
{
9+
if ((length >= 8))
10+
{
11+
hash = (hash + ReadU64(value, offset));
12+
offset += 8;
13+
length -= 8;
14+
}
15+
else
16+
{
17+
if ((length >= 4))
18+
{
19+
hash = (hash + ReadU32(value, offset));
20+
offset += 4;
21+
length -= 4;
22+
}
23+
else
24+
{
25+
if ((length >= 2))
26+
{
27+
hash = (hash + ReadU16(value, offset));
28+
offset += 2;
29+
length -= 2;
30+
}
31+
else
32+
{
33+
hash = (hash + ReadU8(value, offset));
34+
offset += 1;
35+
length -= 1;
36+
}
37+
}
38+
}
39+
}
40+
41+
hash = hash;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
 int32_t length;
2+
int32_t offset;
3+
uint64_t hash;
4+
length = value.Length;
5+
offset = 1;
6+
hash = (hash + ReadU32(value, offset));
7+
offset += 4;
8+
hash = hash;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
 int32_t length;
2+
int32_t offset;
3+
uint64_t hash;
4+
offset = 0;
5+
hash = 352654597ull;
6+
length = value.Length;
7+
while ((length > 0))
8+
{
9+
hash = hash = ((((hash << 5) | (hash >> 27)) + hash) ^ ReadU8(value, offset));
10+
offset += 1;
11+
length -= 1;
12+
}
13+
14+
hash = (352654597ull + (hash * 1566083941ull));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
 uint8_t[] bytes;
2+
uint64_t hash;
3+
bytes = Encoding.ASCII.GetBytes(str);
4+
hash = 0;
5+
hash = (hash + (System.Int32[])[(bytes)[1]]);
6+
hash = (hash + (System.Int32[])[(bytes)[2]]);
7+
hash = (hash + (System.Int32[])[(bytes.Length - 1)]);
8+
hash;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
 int32_t length;
2+
int32_t offset;
3+
uint64_t hash;
4+
length = value.Length;
5+
offset = 0;
6+
length -= offset;
7+
while ((length > 0))
8+
{
9+
if ((length >= 8))
10+
{
11+
hash = (((((hash + ReadU64(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU64(value, offset)) + 18397679294719823053ull) >> 42)) << 23) | ((((hash + ReadU64(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU64(value, offset)) + 18397679294719823053ull) >> 42)) >> 41));
12+
offset += 8;
13+
length -= 8;
14+
}
15+
else
16+
{
17+
if ((length >= 4))
18+
{
19+
hash = (((((hash + ReadU32(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU32(value, offset)) + 18397679294719823053ull) >> 42)) << 23) | ((((hash + ReadU32(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU32(value, offset)) + 18397679294719823053ull) >> 42)) >> 41));
20+
offset += 4;
21+
length -= 4;
22+
}
23+
else
24+
{
25+
if ((length >= 2))
26+
{
27+
hash = (((((hash + ReadU16(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU16(value, offset)) + 18397679294719823053ull) >> 42)) << 23) | ((((hash + ReadU16(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU16(value, offset)) + 18397679294719823053ull) >> 42)) >> 41));
28+
offset += 2;
29+
length -= 2;
30+
}
31+
else
32+
{
33+
hash = (((((hash + ReadU8(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU8(value, offset)) + 18397679294719823053ull) >> 42)) << 23) | ((((hash + ReadU8(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU8(value, offset)) + 18397679294719823053ull) >> 42)) >> 41));
34+
offset += 1;
35+
length -= 1;
36+
}
37+
}
38+
}
39+
}
40+
41+
hash = ((((1ull | hash) + (hash * hash)) ^ (((1ull | hash) + (hash * hash)) >> 48)) ^ ((((1ull | hash) + (hash * hash)) ^ (((1ull | hash) + (hash * hash)) >> 48)) >> 46));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
 int32_t length;
2+
int32_t offset;
3+
uint64_t hash;
4+
length = value.Length;
5+
offset = 1;
6+
hash = (((((hash + ReadU32(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU32(value, offset)) + 18397679294719823053ull) >> 42)) << 23) | ((((hash + ReadU32(value, offset)) + 18397679294719823053ull) ^ (((hash + ReadU32(value, offset)) + 18397679294719823053ull) >> 42)) >> 41));
7+
offset += 4;
8+
hash = ((((1ull | hash) + (hash * hash)) ^ (((1ull | hash) + (hash * hash)) >> 48)) ^ ((((1ull | hash) + (hash * hash)) ^ (((1ull | hash) + (hash * hash)) >> 48)) >> 46));

Src/FastData.Generator.CPlusPlus/CPlusPlusCodeGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Genbox.FastData.Generator.CPlusPlus.Internal;
12
using Genbox.FastData.Generator.CPlusPlus.Internal.Framework;
23
using Genbox.FastData.Generator.CPlusPlus.Internal.Generators;
34
using Genbox.FastData.Generator.Framework;
@@ -9,15 +10,15 @@ public sealed class CPlusPlusCodeGenerator : CodeGenerator
910
{
1011
private readonly CPlusPlusCodeGeneratorConfig _cfg;
1112

12-
private CPlusPlusCodeGenerator(CPlusPlusCodeGeneratorConfig cfg, ILanguageDef langDef, IConstantsDef constDef, IEarlyExitDef earlyExitDef, IHashDef hashDef)
13-
: base(langDef, constDef, earlyExitDef, hashDef) => _cfg = cfg;
13+
private CPlusPlusCodeGenerator(CPlusPlusCodeGeneratorConfig cfg, ILanguageDef langDef, IConstantsDef constDef, IEarlyExitDef earlyExitDef, IHashDef hashDef, ExpressionCompiler compiler)
14+
: base(langDef, constDef, earlyExitDef, hashDef, compiler) => _cfg = cfg;
1415

1516
public static CPlusPlusCodeGenerator Create(CPlusPlusCodeGeneratorConfig userCfg)
1617
{
1718
CPlusPlusLanguageDef langDef = new CPlusPlusLanguageDef();
1819
TypeHelper helper = new TypeHelper(new TypeMap(langDef.TypeDefinitions));
1920

20-
return new CPlusPlusCodeGenerator(userCfg, langDef, new CPlusPlusConstantsDef(), new CPlusPlusEarlyExitDef(helper, userCfg.GeneratorOptions), new CPlusPlusHashDef());
21+
return new CPlusPlusCodeGenerator(userCfg, langDef, new CPlusPlusConstantsDef(), new CPlusPlusEarlyExitDef(helper, userCfg.GeneratorOptions), new CPlusPlusHashDef(), new CPlusPlusExpressionCompiler(helper));
2122
}
2223

2324
public override bool TryGenerate<T>(GeneratorConfig<T> genCfg, IContext context, out string? source)
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using Genbox.FastData.Generator.Framework;
2+
3+
namespace Genbox.FastData.Generator.CPlusPlus.Internal;
4+
5+
internal sealed class CPlusPlusExpressionCompiler(TypeHelper helper) : ExpressionCompiler(helper);

0 commit comments

Comments
 (0)