public static string PrettyPrint(this Instruction i, CompiledZMachine machine) { var builder = new StringBuilder(); builder.AppendFormat("{0:x4}: {1}", i.Address, i.Opcode.Name); if (i.OperandCount > 0) { builder.Append(" "); if (i.Opcode.IsCall) { if (i.Operands[0].Kind != OperandKind.Variable) { builder.Append(machine.UnpackRoutineAddress(i.Operands[0].Value).ToString("x4")); } else { builder.Append(Variable.FromByte((byte)i.Operands[0].Value)); } if (i.OperandCount > 1) { builder.Append(" ("); builder.Append(string.Join(", ", i.Operands.Skip(1).Select(op => op.PrettyPrint()))); builder.Append(")"); } } else if (i.Opcode.IsJump) { var jumpOffset = (short)i.Operands[0].Value; var jumpAddress = i.Address + i.Length + jumpOffset - 2; builder.Append(jumpAddress.ToString("x4")); } else if (i.Opcode.IsFirstOpByRef) { builder.Append(i.Operands[0].PrettyPrintByRef()); if (i.OperandCount > 1) { builder.Append(", "); builder.Append(string.Join(", ", i.Operands.Skip(1).Select(op => op.PrettyPrint()))); } } else { builder.Append(string.Join(", ", i.Operands.Select(op => op.PrettyPrint()))); } } if (i.HasZText) { var ztext = machine.ConvertZText(i.ZText); ztext = ztext.Replace("\n", @"\n").Replace(' ', '\u00b7').Replace("\t", @"\t"); builder.Append(" "); builder.Append(ztext); } if (i.HasStoreVariable) { builder.Append(" -> "); builder.Append(i.StoreVariable.PrettyPrint(@out: true)); } if (i.HasBranch) { builder.Append(" ["); builder.Append(i.Branch.Condition.ToString().ToUpper()); builder.Append("] "); switch (i.Branch.Kind) { case BranchKind.Address: var jumpAddress = i.Address + i.Length + i.Branch.Offset - 2; builder.Append(jumpAddress.ToString("x4")); break; case BranchKind.RFalse: builder.Append("rfalse"); break; case BranchKind.RTrue: builder.Append("rtrue"); break; } } return(builder.ToString()); }
public static ZCompilerResult Compile(ZRoutine routine, CompiledZMachine machine) { return(new ZCompiler(routine, machine).Compile()); }
public ZRoutineCall(ZRoutine routine, CompiledZMachine machine) { this.Machine = machine; this.Routine = routine; }
private ZCompiler(ZRoutine routine, CompiledZMachine machine, bool debugging = false) { this.routine = routine; this.machine = machine; this.debugging = debugging; }