private static Operation[] DecodeShader(ulong address, IGpuAccessor gpuAccessor, TranslationFlags flags, out ShaderConfig config) { Block[] cfg; if ((flags & TranslationFlags.Compute) != 0) { config = new ShaderConfig(gpuAccessor, flags); cfg = Decoder.Decode(gpuAccessor, address); } else { config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, flags); cfg = Decoder.Decode(gpuAccessor, address + HeaderSize); } if (cfg == null) { gpuAccessor.Log("Invalid branch detected, failed to build CFG."); return(Array.Empty <Operation>()); } EmitterContext context = new EmitterContext(config); ulong maxEndAddress = 0; for (int blkIndex = 0; blkIndex < cfg.Length; blkIndex++) { Block block = cfg[blkIndex]; if (maxEndAddress < block.EndAddress) { maxEndAddress = block.EndAddress; } context.CurrBlock = block; context.MarkLabel(context.GetLabel(block.Address)); for (int opIndex = 0; opIndex < block.OpCodes.Count; opIndex++) { OpCode op = block.OpCodes[opIndex]; if ((flags & TranslationFlags.DebugMode) != 0) { string instName; if (op.Emitter != null) { instName = op.Emitter.Method.Name; } else { instName = "???"; gpuAccessor.Log($"Invalid instruction at 0x{op.Address:X6} (0x{op.RawOpCode:X16})."); } string dbgComment = $"0x{op.Address:X6}: 0x{op.RawOpCode:X16} {instName}"; context.Add(new CommentNode(dbgComment)); } if (op.NeverExecute) { continue; } Operand predSkipLbl = null; bool skipPredicateCheck = op is OpCodeBranch opBranch && !opBranch.PushTarget; if (op is OpCodeBranchPop opBranchPop) { // If the instruction is a SYNC or BRK instruction with only one // possible target address, then the instruction is basically // just a simple branch, we can generate code similar to branch // instructions, with the condition check on the branch itself. skipPredicateCheck = opBranchPop.Targets.Count < 2; } if (!(op.Predicate.IsPT || skipPredicateCheck)) { Operand label; if (opIndex == block.OpCodes.Count - 1 && block.Next != null) { label = context.GetLabel(block.Next.Address); } else { label = Label(); predSkipLbl = label; } Operand pred = Register(op.Predicate); if (op.InvertPredicate) { context.BranchIfTrue(label, pred); } else { context.BranchIfFalse(label, pred); } } context.CurrOp = op; op.Emitter?.Invoke(context); if (predSkipLbl != null) { context.MarkLabel(predSkipLbl); } } } config.SizeAdd((int)maxEndAddress + (flags.HasFlag(TranslationFlags.Compute) ? 0 : HeaderSize)); return(context.GetOperations()); }
private static FunctionCode[] DecodeShader(ulong address, IGpuAccessor gpuAccessor, TranslationFlags flags, out ShaderConfig config) { Block[][] cfg; if ((flags & TranslationFlags.Compute) != 0) { config = new ShaderConfig(gpuAccessor, flags); cfg = Decoder.Decode(gpuAccessor, address); } else { config = new ShaderConfig(new ShaderHeader(gpuAccessor, address), gpuAccessor, flags); cfg = Decoder.Decode(gpuAccessor, address + HeaderSize); } if (cfg == null) { gpuAccessor.Log("Invalid branch detected, failed to build CFG."); return(Array.Empty <FunctionCode>()); } Dictionary <ulong, int> funcIds = new Dictionary <ulong, int>(); for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++) { funcIds.Add(cfg[funcIndex][0].Address, funcIndex); } List <FunctionCode> funcs = new List <FunctionCode>(); ulong maxEndAddress = 0; for (int funcIndex = 0; funcIndex < cfg.Length; funcIndex++) { EmitterContext context = new EmitterContext(config, funcIndex != 0, funcIds); for (int blkIndex = 0; blkIndex < cfg[funcIndex].Length; blkIndex++) { Block block = cfg[funcIndex][blkIndex]; if (maxEndAddress < block.EndAddress) { maxEndAddress = block.EndAddress; } context.CurrBlock = block; context.MarkLabel(context.GetLabel(block.Address)); EmitOps(context, block); } funcs.Add(new FunctionCode(context.GetOperations())); } config.SizeAdd((int)maxEndAddress + (flags.HasFlag(TranslationFlags.Compute) ? 0 : HeaderSize)); return(funcs.ToArray()); }