• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SamboyCoding / Cpp2IL / 10704905744

04 Sep 2024 03:33PM UTC coverage: 28.42% (-0.4%) from 28.835%
10704905744

push

github

SamboyCoding
Implement some x86 instructions

1227 of 6002 branches covered (20.44%)

Branch coverage included in aggregate %.

3 of 132 new or added lines in 3 files covered. (2.27%)

2 existing lines in 2 files now uncovered.

3317 of 9987 relevant lines covered (33.21%)

102420.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.47
/Cpp2IL.Core/ISIL/IsilBuilder.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using Cpp2IL.Core.Exceptions;
5

6
namespace Cpp2IL.Core.ISIL;
7

8
public class IsilBuilder
9
{
10
    public List<InstructionSetIndependentInstruction> BackingStatementList;
11

12
    public Dictionary<ulong, List<InstructionSetIndependentInstruction>> InstructionAddressMap;
13

14
    // (Goto instruction, target instruction address)
15
    private readonly List<(InstructionSetIndependentInstruction, ulong)> _jumpsToFix;
16

17
    public IsilBuilder()
4✔
18
    {
19
        BackingStatementList = [];
4✔
20
        InstructionAddressMap = new();
4✔
21
        _jumpsToFix = [];
4✔
22
    }
4✔
23

24
    public IsilBuilder(List<InstructionSetIndependentInstruction> backingStatementList)
×
25
    {
26
        BackingStatementList = backingStatementList;
×
27
        InstructionAddressMap = new();
×
28
        _jumpsToFix = [];
×
29
    }
×
30

31
    private void AddInstruction(InstructionSetIndependentInstruction instruction)
32
    {
33
        if (InstructionAddressMap.TryGetValue(instruction.ActualAddress, out var list))
134!
34
        {
35
            list.Add(instruction);
×
36
        }
37
        else
38
        {
39
            InstructionAddressMap[instruction.ActualAddress] = [instruction];
134✔
40
        }
41

42
        BackingStatementList.Add(instruction);
134✔
43
        instruction.InstructionIndex = (uint)BackingStatementList.Count;
134✔
44
    }
134✔
45

46
    public void FixJumps()
47
    {
48
        foreach (var tuple in _jumpsToFix)
40✔
49
        {
50
            if (InstructionAddressMap.TryGetValue(tuple.Item2, out var list))
16!
51
            {
52
                var target = list.First();
16✔
53

54
                if (target.Equals(tuple.Item1))
16!
55
                    tuple.Item1.Invalidate("Invalid jump target for instruction: Instruction can't jump to itself");
×
56
                else
57
                    tuple.Item1.Operands = [InstructionSetIndependentOperand.MakeInstruction(target)];
16✔
58
            }
59
            else
60
            {
61
                tuple.Item1.Invalidate("Jump target not found in method.");
×
62
            }
63
        }
64
    }
4✔
65

66
    public void Move(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand src) => AddInstruction(new(InstructionSetIndependentOpCode.Move, instructionAddress, IsilFlowControl.Continue, dest, src));
60✔
67

68
    public void LoadAddress(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand src) => AddInstruction(new(InstructionSetIndependentOpCode.LoadAddress, instructionAddress, IsilFlowControl.Continue, dest, src));
4✔
69

70
    public void ShiftStack(ulong instructionAddress, int amount) => AddInstruction(new(InstructionSetIndependentOpCode.ShiftStack, instructionAddress, IsilFlowControl.Continue, InstructionSetIndependentOperand.MakeImmediate(amount)));
8✔
71

72
    public void Push(ulong instructionAddress, InstructionSetIndependentOperand stackPointerRegister, InstructionSetIndependentOperand operand) => AddInstruction(new(InstructionSetIndependentOpCode.Push, instructionAddress, IsilFlowControl.Continue, stackPointerRegister, operand));
2✔
73
    public void Pop(ulong instructionAddress, InstructionSetIndependentOperand stackPointerRegister, InstructionSetIndependentOperand operand) => AddInstruction(new(InstructionSetIndependentOpCode.Pop, instructionAddress, IsilFlowControl.Continue, operand, stackPointerRegister));
2✔
74

75
    public void Exchange(ulong instructionAddress, InstructionSetIndependentOperand place1, InstructionSetIndependentOperand place2) => AddInstruction(new(InstructionSetIndependentOpCode.Exchange, instructionAddress, IsilFlowControl.Continue, place1, place2));
×
76

77
    public void Subtract(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.Subtract, instructionAddress, IsilFlowControl.Continue, dest, left, right));
×
78
    public void Add(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.Add, instructionAddress, IsilFlowControl.Continue, dest, left, right));
×
79

80
    public void Xor(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.Xor, instructionAddress, IsilFlowControl.Continue, dest, left, right));
×
81

82
    // The following 4 had their opcode implemented but not the builder func
83
    // I don't know why
84
    public void ShiftLeft(ulong instructionAddress, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.ShiftLeft, instructionAddress, IsilFlowControl.Continue, left, right));
×
85
    public void ShiftRight(ulong instructionAddress, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.ShiftRight, instructionAddress, IsilFlowControl.Continue, left, right));
×
86
    public void And(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.And, instructionAddress, IsilFlowControl.Continue, dest, left, right));
×
87
    public void Or(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.Or, instructionAddress, IsilFlowControl.Continue, dest, left, right));
×
88

89
    public void Not(ulong instructionAddress, InstructionSetIndependentOperand src) => AddInstruction(new(InstructionSetIndependentOpCode.Not, instructionAddress, IsilFlowControl.Continue, src));
×
NEW
90
    public void Neg(ulong instructionAddress, InstructionSetIndependentOperand src) => AddInstruction(new(InstructionSetIndependentOpCode.Neg, instructionAddress, IsilFlowControl.Continue, src));
×
UNCOV
91
    public void Multiply(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand src1, InstructionSetIndependentOperand src2) => AddInstruction(new(InstructionSetIndependentOpCode.Multiply, instructionAddress, IsilFlowControl.Continue, dest, src1, src2));
×
NEW
92
    public void Divide(ulong instructionAddress, InstructionSetIndependentOperand dest, InstructionSetIndependentOperand src1, InstructionSetIndependentOperand src2) => AddInstruction(new(InstructionSetIndependentOpCode.Divide, instructionAddress, IsilFlowControl.Continue, dest, src1, src2));
×
93

94
    public void Call(ulong instructionAddress, ulong dest, params InstructionSetIndependentOperand[] args) => AddInstruction(new(InstructionSetIndependentOpCode.Call, instructionAddress, IsilFlowControl.MethodCall, PrepareCallOperands(dest, args)));
22✔
95

96
    public void CallRegister(ulong instructionAddress, InstructionSetIndependentOperand dest, bool noReturn = false) => AddInstruction(new(noReturn ? InstructionSetIndependentOpCode.CallNoReturn : InstructionSetIndependentOpCode.Call, instructionAddress, IsilFlowControl.MethodCall, dest));
×
97

98
    public void Return(ulong instructionAddress, InstructionSetIndependentOperand? returnValue = null) => AddInstruction(new(InstructionSetIndependentOpCode.Return, instructionAddress, IsilFlowControl.MethodReturn, returnValue != null
2!
99
        ?
2✔
100
        [
2✔
101
            returnValue.Value
2✔
102
        ]
2✔
103
        : []));
2✔
104

105
    public void Goto(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.Goto, IsilFlowControl.UnconditionalJump);
×
106

107
    public void JumpIfEqual(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfEqual, IsilFlowControl.ConditionalJump);
8✔
108
    
NEW
109
    public void JumpIfSign(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfSign, IsilFlowControl.ConditionalJump);
×
110
    
NEW
111
    public void JumpIfNotSign(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfNotSign, IsilFlowControl.ConditionalJump);
×
112

113
    public void JumpIfNotEqual(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfNotEqual, IsilFlowControl.ConditionalJump);
8✔
114

115
    public void JumpIfGreater(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfGreater, IsilFlowControl.ConditionalJump);
×
116

117
    public void JumpIfLess(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfLess, IsilFlowControl.ConditionalJump);
×
118

119
    public void JumpIfGreaterOrEqual(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfGreaterOrEqual, IsilFlowControl.ConditionalJump);
×
120

121
    public void JumpIfLessOrEqual(ulong instructionAddress, ulong target) => CreateJump(instructionAddress, target, InstructionSetIndependentOpCode.JumpIfLessOrEqual, IsilFlowControl.ConditionalJump);
×
122

123
    private void CreateJump(ulong instructionAddress, ulong target, InstructionSetIndependentOpCode independentOpCode, IsilFlowControl flowControl)
124
    {
125
        var newInstruction = new InstructionSetIndependentInstruction(
16✔
126
            independentOpCode,
16✔
127
            instructionAddress,
16✔
128
            flowControl
16✔
129
        );
16✔
130
        AddInstruction(newInstruction);
16✔
131
        _jumpsToFix.Add((newInstruction, target));
16✔
132
    }
16✔
133

134

135
    public void Compare(ulong instructionAddress, InstructionSetIndependentOperand left, InstructionSetIndependentOperand right) => AddInstruction(new(InstructionSetIndependentOpCode.Compare, instructionAddress, IsilFlowControl.Continue, left, right));
16✔
136

137
    public void NotImplemented(ulong instructionAddress, string text) => AddInstruction(new(InstructionSetIndependentOpCode.NotImplemented, instructionAddress, IsilFlowControl.Continue, InstructionSetIndependentOperand.MakeImmediate(text)));
×
138

139
    public void Invalid(ulong instructionAddress, string text) => AddInstruction(new(InstructionSetIndependentOpCode.Invalid, instructionAddress, IsilFlowControl.Continue, InstructionSetIndependentOperand.MakeImmediate(text)));
×
140

141
    public void Interrupt(ulong instructionAddress) => AddInstruction(new(InstructionSetIndependentOpCode.Interrupt, instructionAddress, IsilFlowControl.Interrupt));
2✔
142
    public void Nop(ulong instructionAddress) => AddInstruction(new(InstructionSetIndependentOpCode.Nop, instructionAddress, IsilFlowControl.Continue));
×
143

144
    private InstructionSetIndependentOperand[] PrepareCallOperands(ulong dest, InstructionSetIndependentOperand[] args)
145
    {
146
        var parameters = new InstructionSetIndependentOperand[args.Length + 1];
22✔
147
        parameters[0] = InstructionSetIndependentOperand.MakeImmediate(dest);
22✔
148
        Array.Copy(args, 0, parameters, 1, args.Length);
22✔
149
        return parameters;
22✔
150
    }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc