Skip to content

Commit 2a8dd86

Browse files
Getting the new unit test to work
1 parent 764ba1b commit 2a8dd86

File tree

10 files changed

+517
-118
lines changed

10 files changed

+517
-118
lines changed

buildscripts/tests/1.lls

+8-5
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,36 @@ function void Print(u8 byte)
5151
}
5252

5353
array<u32, 8> copy;
54-
u64 remainingSize = sizeof(indices);
54+
u64 remainingSize = countof(indices);
5555
u64 offset = 0;
5656

57-
while (remainingSize >= 0)
57+
while (remainingSize > 0)
5858
{
5959
u64 sizeToCopy = 8;
6060

6161
if (remainingSize < 8)
6262
sizeToCopy = remainingSize;
6363

64-
memcpy(cast<ptr<u8>>(copy), cast<ptr<u8>>(indices + offset), sizeToCopy);
64+
memcpy(cast<ptr<u8>>(copy), cast<ptr<u8>>(indices + offset), sizeToCopy * sizeof(u32));
6565

6666
array<u8, 4> out;
6767

6868
u64 i = 0;
6969

7070
while (i < sizeToCopy / 2)
7171
{
72-
out[i] = cast<u8>(copy[i * 2] #<# 8) | cast<u8>(copy[i * 2 + 1]);
72+
out[i] = cast<u8>(copy[i * 2] #<# 4) | cast<u8>(copy[i * 2 + 1]);
7373
i++;
7474
}
7575

7676
i = 0;
7777

78-
while (i <= countof(out))
78+
while (i < countof(out))
7979
{
8080
Print(out[i]);
8181
i++;
8282
}
83+
84+
remainingSize = remainingSize - sizeToCopy;
85+
offset = offset + sizeToCopy;
8386
}

llsc/llsc.csproj

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
<OutputPath>..\builds\bin\</OutputPath>
2121
<BaseIntermediateOutputPath>intermediate\obj\x64\Debug\</BaseIntermediateOutputPath>
2222
<IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>
23-
<DefineConstants>
24-
</DefineConstants>
23+
<DefineConstants></DefineConstants>
2524
<ErrorReport>prompt</ErrorReport>
2625
<WarningLevel>4</WarningLevel>
2726
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
@@ -33,8 +32,7 @@
3332
<OutputPath>..\builds\bin\</OutputPath>
3433
<BaseIntermediateOutputPath>intermediate\obj\x64\Release\</BaseIntermediateOutputPath>
3534
<IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>
36-
<DefineConstants>
37-
</DefineConstants>
35+
<DefineConstants></DefineConstants>
3836
<ErrorReport>prompt</ErrorReport>
3937
<WarningLevel>4</WarningLevel>
4038
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

llsc/project.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ProjectName = "llsc"
1+
ProjectName = "llsc"
22
project(ProjectName)
33

44
--Settings

llsc/src/ByteCodeState.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public void MoveRegisterValueToHome(CNamedValue value, SharedValue<long> stackSi
376376
if (!value.hasHomePosition && value.hasPosition && value.position.type == PositionType.OnStack)
377377
throw new Exception("How is this value on the stack?!");
378378

379-
bool storeInCodeBase = value.type.isConst || (value.type is PtrCType && (value.type as PtrCType).pointsTo.isConst) || (value.type is ArrayCType && (value.type as ArrayCType).type.isConst) || (value.isStatic && Compiler.Assumptions.ByteCodeMutable);
379+
bool storeInCodeBase = (value.type is ArrayCType && (value.type as ArrayCType).type.isConst) || (value.isStatic && Compiler.Assumptions.ByteCodeMutable);
380380
bool storeOnGlobalStack = !storeInCodeBase && value.isStatic;
381381

382382
if (storeInCodeBase || storeOnGlobalStack)

llsc/src/CInstruction.cs

+95-36
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,11 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
8383
for (int i = 0; i < byteCodeState.registers.Length; i++)
8484
byteCodeState.registers[i] = null;
8585

86-
foreach (var parameter in function.parameters)
87-
if (parameter.value.position.type == PositionType.InRegister)
88-
byteCodeState.registers[parameter.value.position.registerIndex] = parameter.value;
86+
byteCodeState.instructions.Add(new LLI_StackIncrementImm(function.minStackSize, 0));
8987

90-
foreach (var param in function.parameters)
91-
byteCodeState.instructions.Add(new LLI_Location_PseudoInstruction(param.value, function.minStackSize, byteCodeState));
88+
foreach (var parameter in function.parameters)
89+
byteCodeState.MarkValueAsPosition(parameter.value, parameter.value.position, function.minStackSize, false);
9290

93-
byteCodeState.instructions.Add(new LLI_StackIncrementImm(function.minStackSize, 0));
9491
byteCodeState.instructions.Add(new LLI_Location_PseudoInstruction(new CValue(file, line, new PtrCType(VoidCType.Instance), true) { description = $"Instruction pointer of the calling function", hasPosition = true, position = Position.StackOffset(0) }, function.minStackSize, byteCodeState));
9592
}
9693
}
@@ -443,11 +440,11 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
443440

444441
if (size <= 8)
445442
{
446-
int sourceValueRegister = byteCodeState.CopyValueToAnyRegister(sourceValue, stackSize);
443+
int sourceValueRegister = byteCodeState.MoveValueToAnyRegister(sourceValue, stackSize);
447444

448445
using (var lockedRegister = byteCodeState.LockRegister(sourceValueRegister))
449446
{
450-
int targetPtrRegister = byteCodeState.CopyValueToAnyRegister(targetValuePtr, stackSize);
447+
int targetPtrRegister = byteCodeState.MoveValueToAnyRegister(targetValuePtr, stackSize);
451448

452449
if (size < 8)
453450
byteCodeState.instructions.Add(new LLI_MovRegisterToPtrInRegister_NBytes(targetPtrRegister, sourceValueRegister, (byte)size));
@@ -836,7 +833,7 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
836833

837834
if (!value.hasPosition)
838835
{
839-
if (value.type.isConst || (value.type is ArrayCType && (value.type as ArrayCType).type.isConst) || (value.type is PtrCType && (value.type as PtrCType).pointsTo.isConst) || (value.isStatic && Compiler.Assumptions.ByteCodeMutable))
836+
if ((value.type is ArrayCType && (value.type as ArrayCType).type.isConst) || (value.isStatic && Compiler.Assumptions.ByteCodeMutable))
840837
{
841838
value.position = Position.CodeBaseOffset(value, new byte[value.type.GetSize()], file, line);
842839
byteCodeState.postInstructionDataStorage.Add(value.position.codeBaseOffset);
@@ -1000,19 +997,9 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
1000997
else
1001998
byteCodeState.DumpValue(source);
1002999

1003-
1004-
if (!(source is CNamedValue))
1005-
{
1006-
source.hasPosition = false;
1007-
source.position.type = PositionType.Invalid;
1008-
}
1009-
1010-
byteCodeState.MarkValueAsPosition(target, originalPosition, stackSize, true);
1011-
10121000
source.remainingReferences--;
10131001

1014-
if (target.position.type == PositionType.InRegister)
1015-
byteCodeState.registers[target.position.registerIndex] = target;
1002+
byteCodeState.MarkValueAsPosition(target, originalPosition, stackSize, true);
10161003
}
10171004

10181005
public override string ToString()
@@ -1332,6 +1319,10 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
13321319

13331320
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(rightRegisterIndex, BitConverter.GetBytes((left.type as PtrCType).pointsTo.GetSize())));
13341321
}
1322+
else if (left is CNamedValue)
1323+
{
1324+
rightRegisterIndex = byteCodeState.CopyValueToAnyRegister(right, stackSize);
1325+
}
13351326
else
13361327
{
13371328
rightRegisterIndex = byteCodeState.MoveValueToAnyRegister(right, stackSize);
@@ -1412,30 +1403,75 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
14121403
if (!right.isInitialized)
14131404
Compiler.Error($"Cannot perform operator on uninitialized right value {right}.", file, line);
14141405

1406+
bool isNop = (right is CConstIntValue && ((right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).uvalue == 1) || (!(right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).ivalue == 1)) || (right is CConstFloatValue && (right as CConstFloatValue).value == 1.0);
1407+
1408+
if (isNop)
1409+
{
1410+
left.remainingReferences--;
1411+
right.remainingReferences--;
1412+
return;
1413+
}
1414+
1415+
bool isWrite = (right is CConstIntValue && (((right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).uvalue == 0) || (!(right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).ivalue == 0))) || (right is CConstFloatValue && ((right as CConstFloatValue).value == 0 || double.IsNaN((right as CConstFloatValue).value)));
1416+
14151417
bool leftIsMutableValue = !(left is CNamedValue);
1416-
int leftRegisterIndex = leftIsMutableValue || toSelf ? byteCodeState.MoveValueToAnyRegister(left, stackSize) : byteCodeState.CopyValueToAnyRegister(left, stackSize);
1418+
int leftRegisterIndex = 0;
14171419

1418-
using (var registerLock = byteCodeState.LockRegister(leftRegisterIndex))
1420+
if (isWrite)
14191421
{
1420-
if (!toSelf && (left is CNamedValue || left is CGlobalValueReference))
1421-
{
1422-
if (left is CNamedValue)
1423-
byteCodeState.MoveValueToHome(left as CNamedValue, stackSize);
1424-
else if (left is CGlobalValueReference)
1425-
throw new NotImplementedException();
1426-
}
1422+
if ((leftIsMutableValue || toSelf) && left.position.type == PositionType.InRegister)
1423+
leftRegisterIndex = left.position.registerIndex;
1424+
else
1425+
leftRegisterIndex = (left.type as BuiltInCType).IsFloat() ? byteCodeState.GetFreeFloatRegister(stackSize) : byteCodeState.GetFreeIntegerRegister(stackSize);
1426+
}
1427+
else
1428+
{
1429+
leftRegisterIndex = leftIsMutableValue || toSelf ? byteCodeState.MoveValueToAnyRegister(left, stackSize) : byteCodeState.CopyValueToAnyRegister(left, stackSize);
1430+
}
14271431

1432+
using (var registerLock = byteCodeState.LockRegister(leftRegisterIndex))
1433+
{
14281434
// lvalue can't be imm if rvalue is const, values would've been swapped by the constructor.
14291435
if (right is CConstIntValue)
14301436
{
14311437
if ((left.type is BuiltInCType && !(left.type as BuiltInCType).IsUnsigned()) || (right.type is BuiltInCType && !(right.type as BuiltInCType).IsUnsigned()))
1432-
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstIntValue).uvalue)));
1438+
{
1439+
var rimm = right as CConstIntValue;
1440+
1441+
if (rimm.ivalue == 0)
1442+
byteCodeState.instructions.Add(new LLI_MovImmToRegister(leftRegisterIndex, new byte[8]));
1443+
else if (rimm.ivalue == 1)
1444+
{ }
1445+
else
1446+
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(leftRegisterIndex, BitConverter.GetBytes(rimm.uvalue)));
1447+
}
14331448
else
1434-
byteCodeState.instructions.Add(new LLI_MultiplyUnsignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstIntValue).uvalue)));
1449+
{
1450+
var rimm = right as CConstIntValue;
1451+
1452+
if (rimm.uvalue == 0)
1453+
byteCodeState.instructions.Add(new LLI_MovImmToRegister(leftRegisterIndex, new byte[8]));
1454+
else if (rimm.uvalue == 1)
1455+
{ }
1456+
else
1457+
byteCodeState.instructions.Add(new LLI_MultiplyUnsignedImm(leftRegisterIndex, BitConverter.GetBytes(rimm.uvalue)));
1458+
}
14351459
}
14361460
else if (right is CConstFloatValue)
14371461
{
1438-
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstFloatValue).value)));
1462+
var rimm = right as CConstFloatValue;
1463+
1464+
if (rimm.value == 0)
1465+
byteCodeState.instructions.Add(new LLI_MovImmToRegister(leftRegisterIndex, new byte[8]));
1466+
else if (double.IsNaN(rimm.value))
1467+
{
1468+
Compiler.Warn($"Multiplying {left} with {right}, which is NaN.", file, line);
1469+
byteCodeState.instructions.Add(new LLI_MovImmToRegister(leftRegisterIndex, BitConverter.GetBytes(rimm.value)));
1470+
}
1471+
else if (rimm.value == 1.0)
1472+
{ }
1473+
else
1474+
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstFloatValue).value)));
14391475
}
14401476
else
14411477
{
@@ -1503,9 +1539,18 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
15031539
if (!left.isInitialized)
15041540
Compiler.Error($"Cannot perform operator on uninitialized left value {left}.", file, line);
15051541

1506-
if (!left.isInitialized)
1542+
if (!right.isInitialized)
15071543
Compiler.Error($"Cannot perform operator on uninitialized right value {right}.", file, line);
15081544

1545+
bool isNop = (right is CConstIntValue && ((right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).uvalue == 1) || (!(right.type as BuiltInCType).IsUnsigned() && (right as CConstIntValue).ivalue == 1)) || (right is CConstFloatValue && (right as CConstFloatValue).value == 1.0);
1546+
1547+
if (isNop)
1548+
{
1549+
left.remainingReferences--;
1550+
right.remainingReferences--;
1551+
return;
1552+
}
1553+
15091554
bool leftIsMutableValue = !(left is CNamedValue);
15101555
int leftRegisterIndex = leftIsMutableValue || toSelf ? byteCodeState.MoveValueToAnyRegister(left, stackSize) : byteCodeState.CopyValueToAnyRegister(left, stackSize);
15111556

@@ -1521,14 +1566,28 @@ public override void GetLLInstructions(ref ByteCodeState byteCodeState)
15211566

15221567
if (right is CConstIntValue)
15231568
{
1569+
var rimm = right as CConstIntValue;
1570+
15241571
if ((left.type is BuiltInCType && !(left.type as BuiltInCType).IsUnsigned()) || (right.type is BuiltInCType && !(right.type as BuiltInCType).IsUnsigned()))
1525-
byteCodeState.instructions.Add(new LLI_DivideSignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstIntValue).uvalue)));
1572+
{
1573+
if (rimm.ivalue == 0)
1574+
Compiler.Error($"Attempting to divide {left} by zero ({right}).", file, line);
1575+
1576+
byteCodeState.instructions.Add(new LLI_DivideSignedImm(leftRegisterIndex, BitConverter.GetBytes(rimm.ivalue)));
1577+
}
15261578
else
1527-
byteCodeState.instructions.Add(new LLI_DivideUnsignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstIntValue).uvalue)));
1579+
{
1580+
if (rimm.uvalue == 0)
1581+
Compiler.Error($"Attempting to divide {left} by zero ({right}).", file, line);
1582+
1583+
byteCodeState.instructions.Add(new LLI_DivideUnsignedImm(leftRegisterIndex, BitConverter.GetBytes(rimm.uvalue)));
1584+
}
15281585
}
15291586
else if (right is CConstFloatValue)
15301587
{
1531-
byteCodeState.instructions.Add(new LLI_DivideSignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstFloatValue).value)));
1588+
// Multiplying should be faster than dividing.
1589+
//byteCodeState.instructions.Add(new LLI_DivideSignedImm(leftRegisterIndex, BitConverter.GetBytes((right as CConstFloatValue).value)));
1590+
byteCodeState.instructions.Add(new LLI_MultiplySignedImm(leftRegisterIndex, BitConverter.GetBytes(1.0 / (right as CConstFloatValue).value)));
15321591
}
15331592
else
15341593
{

llsc/src/CValue.cs

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public virtual CValue MakeCastableClone(CType targetType, Scope scope, ref ByteC
6666

6767
return ret;
6868
}
69+
70+
public static bool IsPowerOfTwo(ulong x)
71+
{
72+
return (x != 0) && ((x & (x - 1)) == 0);
73+
}
6974
}
7075

7176
public class CConstIntValue : CValue

0 commit comments

Comments
 (0)