コード例 #1
0
 public static IMOperand Reference(DataType dataType, IMOperand refValue)
 {
     return(new IMOperand()
     {
         DataType = dataType, Kind = IMOperandKind.Reference, ChildValue = refValue
     });
 }
コード例 #2
0
 public override List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(new List <IMOperation>()
     {
         IMOperation.Mul(target, operand1, operand2)
     });
 }
コード例 #3
0
ファイル: RelationalOperator.cs プロジェクト: ericlass/erc
 public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(IMOperation.Create(_setInstruction, new List <IMOperand>()
     {
         target, operand1, operand2
     }).AsList);
 }
コード例 #4
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public IMOperation(IMInstruction instruction, IMOperand operand1, IMOperand operand2, IMOperand operand3)
 {
     Assert.True(instruction.NumOperands == 3, "Instruction " + instruction.Name + " expected " + instruction.NumOperands + " operands, but 3 given!");
     Instruction = instruction;
     Operands    = new List <IMOperand> {
         operand1, operand2, operand3
     };
 }
コード例 #5
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 private IMOperation(IMInstruction instruction, IMOperand operand1, IMOperand operand2)
 {
     Assert.True(instruction.NumOperands == 2, "Instruction " + instruction.Name + " expected " + instruction.NumOperands + " operands, but 2 given!");
     Instruction = instruction;
     Operands    = new List <IMOperand> {
         operand1, operand2
     };
 }
コード例 #6
0
        private static string ImmediateChar8ToAsmCode(IMOperand operand)
        {
            var strValue  = (string)operand.ImmediateValue;
            var firstChar = strValue[0];
            var number    = (ushort)firstChar;

            return(number.ToString() + " ; '" + StringUtils.CharToPrintableStr(firstChar) + "'");
        }
コード例 #7
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
        public static IMOperation GVec(IMOperand target, List <IMOperand> values)
        {
            var allOperands = new List <IMOperand>()
            {
                target
            };

            allOperands.AddRange(values);
            return(new IMOperation()
            {
                Instruction = IMInstruction.GVEC, Operands = allOperands
            });
        }
コード例 #8
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
        public static IMOperation Call(string functionName, IMOperand result, List <IMOperand> paramValues)
        {
            var allOperands = new List <IMOperand>()
            {
                IMOperand.Identifier(functionName), result
            };

            allOperands.AddRange(paramValues);
            return(new IMOperation()
            {
                Instruction = IMInstruction.CALL, Operands = allOperands
            });
        }
コード例 #9
0
        public override List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
        {
            switch (operand1.DataType.Kind)
            {
            case DataTypeKind.ARRAY:
                throw new NotImplementedException();

            case DataTypeKind.STRING8:
                return(GenerateStringConcat(env, target, operand1, operand2));

            default:
                return(IMOperation.Add(target, operand1, operand2).AsList);
            }
        }
コード例 #10
0
ファイル: EqualityOperator.cs プロジェクト: ericlass/erc
        public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
        {
            var result = new List <IMOperation>();

            if (_negate)
            {
                result.Add(IMOperation.SetNE(target, operand1, operand2));
            }
            else
            {
                result.Add(IMOperation.SetE(target, operand1, operand2));
            }

            return(result);
        }
コード例 #11
0
        public List <IMOperation> Generate(IMOperand target, IMOperand operand)
        {
            if (operand.DataType.Kind == DataTypeKind.ARRAY || operand.DataType.Kind == DataTypeKind.STRING8)
            {
                //Array already is a pointer, so directly use it
                //Add 8 bytes to array pointer so it points to first value, not to the length
                var result = new List <IMOperation>(2)
                {
                    IMOperation.Mov(target, operand),
                    IMOperation.Add(target, target, IMOperand.Immediate(DataType.U64, 8L))
                };
                return(result);
            }

            return(IMOperation.Lea(target, operand).AsList);
        }
コード例 #12
0
ファイル: TypeCastOperator.cs プロジェクト: ericlass/erc
     public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
     {
         if (target.DataType.Kind == operand1.DataType.Kind)
         {
             return new List <IMOperation>()
                    {
                        IMOperation.Mov(target, operand1)
                    }
         }
         ;
         else
         {
             return new List <IMOperation>()
                    {
                        IMOperation.Cast(target, operand1)
                    }
         };
     }
 }
コード例 #13
0
        private static string ImmediateString8ToAsmCode(IMOperand operand)
        {
            var strValue = (string)operand.ImmediateValue;

            var numBytes       = 8 + strValue.Length + 1; //size + string value + terminating zero
            var immediateBytes = new List <byte>(numBytes);

            //Add 4 bytes length
            var length = (ulong)strValue.Length;

            immediateBytes.AddRange(BitConverter.GetBytes(length));

            //Add string value encoded as bytes
            immediateBytes.AddRange(_isoEncoding.GetBytes(strValue));

            //Add terminating zero
            immediateBytes.Add(0);

            //Create final string
            return(String.Join(",", immediateBytes.ConvertAll((b) => b.ToString())) + "; \"" + StringUtils.Escape(strValue) + "\"");
        }
コード例 #14
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Lea(IMOperand target, IMOperand source)
 {
     return(new IMOperation(IMInstruction.LEA, target, source));
 }
コード例 #15
0
ファイル: NoOpOperator.cs プロジェクト: ericlass/erc
 public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(IMOperation.Nop().AsList);
 }
コード例 #16
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Shr(IMOperand target, IMOperand source, IMOperand numBits)
 {
     return(new IMOperation(IMInstruction.SHR, target, source, numBits));
 }
コード例 #17
0
ファイル: DivisionOperator.cs プロジェクト: ericlass/erc
 public override List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(IMOperation.Div(target, operand1, operand2).AsList);
 }
コード例 #18
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Add(IMOperand target, IMOperand summand1, IMOperand summand2)
 {
     return(new IMOperation(IMInstruction.ADD, target, summand1, summand2));
 }
コード例 #19
0
 public static string GetMemLocationName(IMOperand target)
 {
     return("mem(" + target.FullName + ")");
 }
コード例 #20
0
 public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     throw new NotImplementedException("StaticAccessOperator should not generate anything at the moment!");
 }
コード例 #21
0
 public List <IMOperation> Generate(IMOperand target, IMOperand operand)
 {
     return(IMOperation.Neg(target, operand).AsList);
 }
コード例 #22
0
 public List <IMOperation> Generate(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(new IMOperation(_instruction, target, operand1, operand2).AsList);
 }
コード例 #23
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Push(IMOperand source)
 {
     return(new IMOperation(IMInstruction.PUSH, source));
 }
コード例 #24
0
        private List <IMOperation> GenerateStringConcat(IMGeneratorEnv env, IMOperand target, IMOperand operand1, IMOperand operand2)
        {
            var result = new List <IMOperation>(20);

            var op1LengthLocation = IMOperand.Reference(DataType.U64, operand1);
            var op2LengthLocation = IMOperand.Reference(DataType.U64, operand2);

            //Calculate new string length
            var lengthLocation = env.NewTempLocal(DataType.U64);

            result.Add(IMOperation.Mov(lengthLocation, op1LengthLocation));
            result.Add(IMOperation.Add(lengthLocation, lengthLocation, op2LengthLocation));

            //Calculate new string byte size
            var byteSizeLocation = env.NewTempLocal(DataType.U64);

            result.Add(IMOperation.Mov(byteSizeLocation, lengthLocation));
            result.Add(IMOperation.Add(lengthLocation, lengthLocation, IMOperand.Immediate(DataType.U64, 9L)));

            //Reserve new memory on stack, which is freed automatically
            result.Add(IMOperation.SAloc(target, byteSizeLocation));

            //Copy target address
            var targetAddressLocation = env.NewTempLocal(target.DataType);

            result.Add(IMOperation.Mov(targetAddressLocation, target));

            //Write new string length to target
            result.Add(IMOperation.Mov(IMOperand.Reference(DataType.U64, targetAddressLocation), lengthLocation));

            //Move pointer to first char
            result.Add(IMOperation.Add(targetAddressLocation, targetAddressLocation, IMOperand.Immediate(DataType.U64, 8L)));

            var targetRefLocation = IMOperand.Reference(DataType.CHAR8, targetAddressLocation);

            //Get address of first char of operand1
            var sourceAddressLocation = env.NewTempLocal(target.DataType);

            result.Add(IMOperation.Mov(sourceAddressLocation, operand1));
            result.Add(IMOperation.Add(sourceAddressLocation, sourceAddressLocation, IMOperand.Immediate(DataType.U64, 8L)));

            var sourceRefLocation = IMOperand.Reference(DataType.CHAR8, sourceAddressLocation);

            var startLabel = env.NewLabelName();
            var endLabel   = env.NewLabelName();

            //Start label
            result.Add(IMOperation.Labl(startLabel));

            //Check if more chars to copy (string ends with 0 char)
            result.Add(IMOperation.JmpE(sourceRefLocation, IMOperand.Immediate(DataType.CHAR8, "\0"), endLabel));

            //Copy char
            result.Add(IMOperation.Mov(targetRefLocation, sourceRefLocation));

            //Inc target and source pointers
            result.Add(IMOperation.Add(targetAddressLocation, targetAddressLocation, IMOperand.Immediate(DataType.U64, 1L)));
            result.Add(IMOperation.Add(sourceAddressLocation, sourceAddressLocation, IMOperand.Immediate(DataType.U64, 1L)));

            //Next iteration
            result.Add(IMOperation.Jmp(startLabel));
            result.Add(IMOperation.Labl(endLabel));

            //Get address of first char of operand2
            result.Add(IMOperation.Mov(sourceAddressLocation, operand2));
            result.Add(IMOperation.Add(sourceAddressLocation, sourceAddressLocation, IMOperand.Immediate(DataType.U64, 8L)));

            startLabel = env.NewLabelName();
            endLabel   = env.NewLabelName();

            //Start label
            result.Add(IMOperation.Labl(startLabel));

            //Check if more chars to copy (string ends with 0 char)
            result.Add(IMOperation.JmpE(sourceRefLocation, IMOperand.Immediate(DataType.CHAR8, "\0"), endLabel));

            //Copy char
            result.Add(IMOperation.Mov(targetRefLocation, sourceRefLocation));

            //Inc target and source pointers
            result.Add(IMOperation.Add(targetAddressLocation, targetAddressLocation, IMOperand.Immediate(DataType.U64, 1L)));
            result.Add(IMOperation.Add(sourceAddressLocation, sourceAddressLocation, IMOperand.Immediate(DataType.U64, 1L)));

            //Next iteration
            result.Add(IMOperation.Jmp(startLabel));
            result.Add(IMOperation.Labl(endLabel));

            //Terminating 0
            result.Add(IMOperation.Mov(targetRefLocation, IMOperand.Immediate(DataType.U8, (byte)0)));

            return(result);
        }
コード例 #25
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Pop(IMOperand target)
 {
     return(new IMOperation(IMInstruction.POP, target));
 }
コード例 #26
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Sub(IMOperand target, IMOperand minuend, IMOperand subtrahend)
 {
     return(new IMOperation(IMInstruction.SUB, target, minuend, subtrahend));
 }
コード例 #27
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Cast(IMOperand target, IMOperand source)
 {
     return(new IMOperation(IMInstruction.CAST, target, source));
 }
コード例 #28
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation Mov(IMOperand target, IMOperand source)
 {
     return(new IMOperation(IMInstruction.MOV, target, source));
 }
コード例 #29
0
ファイル: IMGeneratorEnv.cs プロジェクト: ericlass/erc
 public IMOperand NewTempLocal(DataType dataType)
 {
     _tempLocalCounter += 1;
     return(IMOperand.Local(dataType, _tempLocalCounter.ToString()));
 }
コード例 #30
0
ファイル: IMOperation.cs プロジェクト: ericlass/erc
 public static IMOperation BinaryOperator(IMInstruction instruction, IMOperand target, IMOperand operand1, IMOperand operand2)
 {
     return(new IMOperation(instruction, target, operand1, operand2));
 }