Skip to content

Commit 6f929c5

Browse files
committed
Merge branch 'develop'
2 parents 5fc406e + cf0ac71 commit 6f929c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+368
-284
lines changed

Cate/AddOrSubtractInstruction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public abstract class AddOrSubtractInstruction : BinomialInstruction
44
{
55
protected AddOrSubtractInstruction(Function function, int operatorId, AssignableOperand destinationOperand, Operand leftOperand, Operand rightOperand) : base(function, operatorId, destinationOperand, leftOperand, rightOperand) { }
66

7-
protected bool IncrementOrDecrement()
7+
protected virtual bool IncrementOrDecrement()
88
{
99
if (RightOperand is not IntegerOperand rightIntegerOperand)
1010
return false;

Cate/BinomialInstruction.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected void ExchangeOperands()
6565

6666
protected virtual void OperateByte(string operation, int count)
6767
{
68-
if (DestinationOperand.Equals(LeftOperand) && count == 1) {
68+
if (DestinationOperand.Equals(LeftOperand) && count == 1 && IsOperatable(DestinationOperand)) {
6969
ByteOperation.Operate(this, operation, true, DestinationOperand, count);
7070
return;
7171
}
@@ -87,5 +87,7 @@ void ViaRegister(ByteRegister register)
8787
ViaRegister(reservation.ByteRegister);
8888
reservation.ByteRegister.Store(this, DestinationOperand);
8989
}
90+
91+
protected virtual bool IsOperatable(AssignableOperand operand) => true;
9092
}
9193
}

Cate/Compiler.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1569,16 +1569,17 @@ public virtual void SaveRegisters(StreamWriter writer, ISet<Register> registers)
15691569
{
15701570
var set = SavingRegisters(registers).ToImmutableSortedSet();
15711571
foreach (var r in set) {
1572-
r.Save(writer, null, false, 0);
1572+
r.Save(writer, null, null, 0);
15731573
}
15741574
}
15751575

1576-
public virtual void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
1576+
public virtual void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction,
1577+
int tabCount)
15771578
{
15781579
var dictionary = DistinctRegisters(variables);
15791580
foreach (var (register, list) in dictionary.OrderBy(p => p.Key)) {
15801581
var comment = "\t; " + string.Join(',', list.Select(v => v.Name).ToArray());
1581-
register.Save(writer, comment, jump, tabCount);
1582+
register.Save(writer, comment, instruction, tabCount);
15821583
}
15831584
}
15841585

@@ -1591,15 +1592,16 @@ public virtual void RestoreRegisters(StreamWriter writer, ISet<Register> registe
15911592

15921593
protected virtual void RestoreRegister(StreamWriter writer, Register register, int byteCount)
15931594
{
1594-
register.Restore(writer, null, false, 0);
1595+
register.Restore(writer, null, null, 0);
15951596
}
15961597

1597-
public virtual void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
1598+
public virtual void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction,
1599+
int tabCount)
15981600
{
15991601
var dictionary = DistinctRegisters(variables);
16001602
foreach (var (register, list) in dictionary.OrderByDescending(p => p.Key)) {
16011603
var comment = "\t; " + string.Join(',', list.Select(v => v.Name).ToArray());
1602-
register.Restore(writer, comment, jump, tabCount);
1604+
register.Restore(writer, comment, instruction, tabCount);
16031605
}
16041606
}
16051607

Cate/Function.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ bool SavingChanged(Instruction instruction1, Instruction instruction2)
170170

171171
if (prevInstruction == null || SavingChanged(prevInstruction, instruction)) {
172172
if (instruction.SavingVariables.Count > 0) {
173-
compiler.SaveRegisters(writer, instruction.SavingVariables, instruction.IsJump(), tabCount++);
173+
compiler.SaveRegisters(writer, instruction.SavingVariables, instruction, tabCount++);
174174
}
175175
}
176176
instruction.WriteAssembly(writer, tabCount);
177177
if (nextInstruction == null || SavingChanged(instruction, nextInstruction)) {
178178
if (instruction.SavingVariables.Count > 0) {
179-
compiler.RestoreRegisters(writer, instruction.SavingVariables, instruction.IsJump(), --tabCount);
179+
compiler.RestoreRegisters(writer, instruction.SavingVariables, instruction, --tabCount);
180180
}
181181
}
182182
instruction.WriteAssemblyAfterRestoring(writer, tabCount);
@@ -284,7 +284,7 @@ static List<Variable> TargetVariables(IEnumerable<Variable> sourceVariables)
284284

285285
foreach (var instruction in Instructions) {
286286
#if DEBUG
287-
if (instruction.ToString().Contains("pMonster[2] = sprite")) {
287+
if (instruction.ToString().Contains("__2 = x & 3")) {
288288
var aaa = 111;
289289
}
290290
#endif

Cate/Instruction.cs

+7
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ public void RemoveStaticVariableAssignments()
308308
}
309309
}
310310

311+
public bool IsRegisterAssigned(Register register)
312+
{
313+
return RegisterAssignments.Any(a => Equals(a.Key, register));
314+
}
315+
311316
public bool IsRegisterInVariableRange(Register? register, Variable? excludedVariable)
312317
{
313318
if (register == null)
@@ -720,4 +725,6 @@ public bool IsChanged(Register register)
720725
}
721726
return changedRegisters.Contains(register);
722727
}
728+
729+
723730
}

Cate/LoadInstruction.cs

+23-14
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,29 @@ public override void BuildAssembly()
5656
return;
5757
}
5858

59-
if (SourceOperand is IntegerOperand integerOperand && DestinationOperand is IndirectOperand indirectOperand) {
60-
var pointer = indirectOperand.Variable;
61-
var offset = indirectOperand.Offset;
62-
var register = GetVariableRegister(pointer, 0);
63-
if (register is PointerRegister pointerRegister) {
64-
ByteOperation.StoreConstantIndirect(this, pointerRegister, offset, integerOperand.IntegerValue);
65-
return;
66-
}
67-
var pointerRegisters = PointerOperation.RegistersToOffset(offset);
68-
if (pointerRegisters.Any()) {
69-
using var reservation = PointerOperation.ReserveAnyRegister(this, pointerRegisters, SourceOperand);
70-
reservation.PointerRegister.LoadFromMemory(this, pointer, 0);
71-
ByteOperation.StoreConstantIndirect(this, reservation.PointerRegister, offset, integerOperand.IntegerValue);
72-
return;
59+
if (SourceOperand is IntegerOperand integerOperand) {
60+
switch (DestinationOperand) {
61+
case VariableOperand variableOperand when DestinationOperand.Register == null && integerOperand.IntegerValue == 0:
62+
ByteOperation.ClearByte(this, variableOperand.MemoryAddress());
63+
return;
64+
case IndirectOperand indirectOperand: {
65+
var pointer = indirectOperand.Variable;
66+
var offset = indirectOperand.Offset;
67+
var register = GetVariableRegister(pointer, 0);
68+
if (register is PointerRegister pointerRegister) {
69+
ByteOperation.StoreConstantIndirect(this, pointerRegister, offset, integerOperand.IntegerValue);
70+
return;
71+
}
72+
var pointerRegisters = PointerOperation.RegistersToOffset(offset);
73+
if (pointerRegisters.Any()) {
74+
using var reservation = PointerOperation.ReserveAnyRegister(this, pointerRegisters, SourceOperand);
75+
reservation.PointerRegister.LoadFromMemory(this, pointer, 0);
76+
ByteOperation.StoreConstantIndirect(this, reservation.PointerRegister, offset, integerOperand.IntegerValue);
77+
return;
78+
}
79+
80+
break;
81+
}
7382
}
7483
}
7584

Cate/Register.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public virtual bool Matches(Register register)
5050
return Equals(register, this);
5151
}
5252

53-
public abstract void Save(StreamWriter writer, string? comment, bool jump, int tabCount);
53+
public abstract void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount);
5454

55-
public abstract void Restore(StreamWriter writer, string? comment, bool jump, int tabCount);
55+
public abstract void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount);
5656
public abstract void Save(Instruction instruction);
5757
public abstract void Restore(Instruction instruction);
5858

Cate/WordPointerRegister.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ public override bool Matches(Register register)
4949
}
5050

5151

52-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
52+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
5353
{
54-
WordRegister.Save(writer, comment, jump, tabCount);
54+
WordRegister.Save(writer, comment, instruction, tabCount);
5555
}
5656

57-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
57+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
5858
{
59-
WordRegister.Restore(writer, comment, jump, tabCount);
59+
WordRegister.Restore(writer, comment, instruction, tabCount);
6060
}
6161

6262
public override void Save(Instruction instruction)

Cate09/ByteRegister.cs

+2-17
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ public ByteRegister(int id, string name) : base(id, name)
2424
Registers.Add(this);
2525
}
2626

27-
//public static Cate.ByteRegister TemporaryRegister(Instruction instruction, List<Cate.ByteRegister> registers)
28-
//{
29-
// var register = registers.First(r => !instruction.IsRegisterReserved(r));
30-
// return register;
31-
//}
32-
33-
//public static void Using(Instruction instruction, List<Cate.ByteRegister> candidates,
34-
// Action<Cate.ByteRegister> action)
35-
//{
36-
// Cate.ByteRegister temporaryRegister = TemporaryRegister(instruction, candidates);
37-
// instruction.BeginRegister(temporaryRegister);
38-
// action(temporaryRegister);
39-
// instruction.EndRegister(temporaryRegister);
40-
//}
41-
4227
public override void LoadConstant(Instruction instruction, string value)
4328
{
4429
instruction.WriteLine("\tld" + Name + "\t#" + value);
@@ -197,13 +182,13 @@ public override bool Matches(Cate.Register register)
197182
return Conflicts(register) || register.Equals(WordRegister.D);
198183
}
199184

200-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
185+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
201186
{
202187
// save together
203188
throw new NotImplementedException();
204189
}
205190

206-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
191+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
207192
{
208193
// save together
209194
throw new NotImplementedException();

Cate09/Compiler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void SaveRegisters(StreamWriter writer, ISet<Register> registers
5353
SaveRegisters(writer, registers, null, 0);
5454
}
5555

56-
public override void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
56+
public override void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction, int tabCount)
5757
{
5858
var list = variables.ToList();
5959
var savingRegisterIds = SavingRegisters(list);
@@ -79,7 +79,7 @@ public override void RestoreRegisters(StreamWriter writer, ISet<Register> regist
7979
RestoreRegisters(writer, registers, null, 0);
8080
}
8181

82-
public override void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
82+
public override void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction, int tabCount)
8383
{
8484
var savingRegisterIds = SavingRegisters(variables);
8585
var comment = "\t; " + Join(',', variables.Select(v => v.Name).ToArray());

Cate09/WordRegister.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ protected WordRegister(int id, string name) : base(id, name)
2525
}
2626

2727

28-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
28+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
2929
{
3030
// save together
3131
throw new NotImplementedException();
3232
}
3333

34-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
34+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3535
{
3636
// save together
3737
throw new NotImplementedException();

Cate61h/ByteRegister.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ static ByteRegister()
1414

1515
private ByteRegister(int id) : base(id, Compiler.RegisterHead + id) { }
1616

17-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
17+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
1818
{
1919
Instruction.WriteTabs(writer, tabCount);
2020
writer.WriteLine("\tphs " + AsmName + (comment != "" ? " ;" + comment : ""));
2121
}
2222

23-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
23+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
2424
{
2525
Instruction.WriteTabs(writer, tabCount);
2626
writer.WriteLine("\tpps " + AsmName + (comment != "" ? " ;" + comment : ""));

Cate61h/Compiler.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override void SaveRegisters(StreamWriter writer, ISet<Register> registers
1414
SaveRegisters(writer, registers, 0, _ => "");
1515
}
1616

17-
public override void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
17+
public override void SaveRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction, int tabCount)
1818
{
1919
var comments = RegisterComments(variables, out var registers);
2020
SaveRegisters(writer, registers, tabCount, register => comments[register]);
@@ -44,7 +44,7 @@ private static void SaveRegisters(StreamWriter writer, ISet<Register> registers,
4444
}
4545
else {
4646
var register = list.Last();
47-
register.Save(writer, toComment(register), false, tabCount);
47+
register.Save(writer, toComment(register), null, tabCount);
4848
}
4949
}
5050
}
@@ -72,12 +72,12 @@ private static void RestoreRegisters(StreamWriter writer, ISet<Register> registe
7272
}
7373
else {
7474
var register = list.First();
75-
register.Restore(writer, toComment(register), false, tabCount);
75+
register.Restore(writer, toComment(register), null, tabCount);
7676
}
7777
}
7878
}
7979

80-
public override void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, bool jump, int tabCount)
80+
public override void RestoreRegisters(StreamWriter writer, IEnumerable<Variable> variables, Instruction? instruction, int tabCount)
8181
{
8282
var comments = RegisterComments(variables, out var registers);
8383
RestoreRegisters(writer, registers, tabCount, register => comments[register]);

Cate61h/IndexRegister.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public static string OffsetValue(int offset)
2525
}
2626
private IndexRegister(int id, string name) : base(id, 2, name) { }
2727

28-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
28+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
2929
{ }
3030

31-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
31+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3232
{ }
3333

3434
public override void Save(Instruction instruction)
@@ -50,6 +50,7 @@ public override void Restore(Instruction instruction)
5050
public override void LoadConstant(Instruction instruction, string value)
5151
{
5252
instruction.WriteLine("\tpre " + AsmName + "," + value);
53+
instruction.RemoveRegisterAssignment(this);
5354
}
5455

5556
public override void LoadFromMemory(Instruction instruction, string label)

Cate61h/WordRegister.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ private WordRegister(int id) : base(id, Compiler.RegisterHead + id) { }
1616
public string HighByteName => Compiler.RegisterHead + (Id + 1);
1717

1818

19-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
19+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
2020
{
2121
Instruction.WriteTabs(writer, tabCount);
2222
writer.WriteLine("\tphsw " + HighByteName + (comment != "" ? " ;" + comment : ""));
2323
}
2424

25-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
25+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
2626
{
2727
Instruction.WriteTabs(writer, tabCount);
2828
writer.WriteLine("\tppsw " + AsmName + (comment != "" ? " ;" + comment : ""));

Cate62/ByteInternalRam.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public ByteInternalRam(int index) : base(IndexToName(index)) { }
4242

4343
internal ByteInternalRam(string name) : base(name) { }
4444

45-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
45+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
4646
{
4747
Instruction.WriteTabs(writer, tabCount);
4848
writer.WriteLine("\tmv [--s]," + Name + "\t" + comment);
4949
}
5050

51-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
51+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
5252
{
5353
Instruction.WriteTabs(writer, tabCount);
5454
writer.WriteLine("\tmv " + Name + ",[s++]\t" + comment);

Cate62/ByteRegister.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public override bool Conflicts(Register? register)
2828

2929
protected ByteRegister(string name) : base(Compiler.NewRegisterId(), name) { }
3030

31-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
31+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3232
{
3333
Instruction.WriteTabs(writer, tabCount);
3434
writer.WriteLine("\tpushs " + Name + "\t" + comment);
3535
}
3636

37-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
37+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3838
{
3939
Instruction.WriteTabs(writer, tabCount);
4040
writer.WriteLine("\tpops " + Name + "\t" + comment);

Cate62/PointerInternalRam.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ private static string IndexToLabel(int index)
2929
}
3030

3131
public override string MV => "mvp";
32-
public override void Save(StreamWriter writer, string? comment, bool jump, int tabCount)
32+
public override void Save(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3333
{
3434
Instruction.WriteTabs(writer, tabCount);
3535
writer.WriteLine("\tmvp [--s]," + Name + "\t" + comment);
3636
}
3737

38-
public override void Restore(StreamWriter writer, string? comment, bool jump, int tabCount)
38+
public override void Restore(StreamWriter writer, string? comment, Instruction? instruction, int tabCount)
3939
{
4040
Instruction.WriteTabs(writer, tabCount);
4141
writer.WriteLine("\tmvp " + Name + ",[s++]\t" + comment);

0 commit comments

Comments
 (0)