public Identifier DeserializeReturnValue(Argument_v1 arg)
        {
            argCur = arg;
            DataType dt = null;

            if (this.argCur.Type != null)
            {
                dt = this.argCur.Type.Accept(procSer.TypeLoader);
            }
            if (dt is VoidType)
            {
                return(null);
            }
            Identifier id;

            if (arg.Kind != null)
            {
                id          = arg.Kind.Deserialize(this);
                id.DataType = dt ?? id.DataType;
            }
            else
            {
                var reg = procSer.GetReturnRegister(arg, dt.BitSize);
                id = new Identifier(reg.ToString(), dt, reg);
            }
            return(id);
        }
Example #2
0
        public SerializedType VisitFunctionType(FunctionType ft)
        {
            Argument_v1 ret = null;

            if (!ft.HasVoidReturn)
            {
                ret = SerializeArgument(null, null, ft.ReturnValue.DataType);
            }
            Argument_v1[] parms;
            if (ft.Parameters != null)
            {
                parms = new Argument_v1[ft.Parameters.Length];
                for (int i = 0; i < ft.Parameters.Length; ++i)
                {
                    parms[i] = SerializeArgument(ft.Parameters[i].Name, null, ft.Parameters[i].DataType);
                }
            }
            else
            {
                parms = new Argument_v1[0];
            }
            return(new SerializedSignature
            {
                Arguments = parms,
                ReturnValue = ret
            });
        }
Example #3
0
        public SerializedType VisitFunctionType(FunctionType ft)
        {
            Argument_v1 ret = null;

            if (ft.ReturnType != null)
            {
                ret = SerializeArgument(null, null, ft.ReturnType);
            }
            Argument_v1[] parms;
            if (ft.ArgumentTypes != null)
            {
                parms = new Argument_v1[ft.ArgumentTypes.Length];
                for (int i = 0; i < ft.ArgumentTypes.Length; ++i)
                {
                    var type = ft.ArgumentTypes[i].Accept(this);
                    parms[i] = SerializeArgument(ft.ArgumentNames[i], null, ft.ArgumentTypes[i]);
                }
            }
            else
            {
                parms = new Argument_v1[0];
            }
            return(new SerializedSignature
            {
                Arguments = parms,
                ReturnValue = ret
            });
        }
Example #4
0
 public Identifier DeserializeArgument(Argument_v1 sArg, int idx, string convention)
 {
     if (sArg.Kind != null)
     {
         return argser.Deserialize(sArg, sArg.Kind);
     }
     return argser.Deserialize(sArg, new StackVariable_v1());
 }
Example #5
0
        private Identifier DeserializeArgument(ArgumentDeserializer argser, Argument_v1 sArg)
        {
            Identifier arg;
            if (sArg.Kind != null)
                return sArg.Kind.Deserialize(argser);

            var dtArg = sArg.Type.Accept(TypeLoader);
            var prim = dtArg as PrimitiveType;
            //if (prim != null && prim.Domain == Domain.Real)
            //{
            //    if (this.fr > 8)
            //    {
            //        arg = argser.Deserialize(sArg, new StackVariable_v1());
            //    }
            //    else
            //    {
            //        arg = argser.Deserialize(sArg, new Register_v1("f" + this.fr));
            //        ++this.fr;
            //    }
            //    return arg;
            //}
            if (dtArg.Size <= 4)
            {
                if (this.gr > 3)
                {
                    arg = argser.Deserialize(sArg, new StackVariable_v1());
                }
                else
                {
                    arg = argser.Deserialize(sArg, new Register_v1("r" + gr));
                    ++this.gr;
                }
                return arg;
            }
            if (dtArg.Size <= 8)
            {
                if (this.gr > 9)
                {
                    arg = argser.Deserialize(sArg, new StackVariable_v1());
                }
                else
                {
                    if ((gr & 1) == 0)
                        ++gr;
                    arg = argser.Deserialize(sArg, new SerializedSequence
                    {
                        Registers = new Register_v1[] { 
                            new Register_v1("r" + gr), 
                            new Register_v1("r" + (gr+1))
                        }
                    });
                    gr += 2;
                }
                return arg;
            }
            throw new NotImplementedException();
        }
Example #6
0
 public void SargWriteNamelessRegisterArgument()
 {
     Argument_v1 sarg = new Argument_v1
     {
         Type = new SerializedTypeReference("int"),
         Kind = new Register_v1("eax"),
     };
     Verify(sarg, "Core/SargWriteNamelessRegisterArgument.txt");
 }
Example #7
0
		public void SargWriteRegisterArgument()
		{
			Argument_v1 sarg = new Argument_v1 {
			    Name = "foo",
			    Type = new TypeReference_v1("int"),
			    Kind = new Register_v1("eax"),
            };
			Verify(sarg, "Core/SargWriteRegisterArgument.txt");
		}
Example #8
0
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     if (sArg.Kind == null)
         throw new NotImplementedException();
     var reg = sArg.Kind as Register_v1;
     if (reg != null)
         return Architecture.GetRegister(reg.Name);
     throw new NotImplementedException();
 }
Example #9
0
        /// <summary>
        /// Deserializes an argument.
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="idx"></param>
        /// <param name="convention"></param>
        /// <returns></returns>
        public Identifier DeserializeArgument(Argument_v1 arg, int idx, string convention)
        {
            var kind = arg.Kind;

            if (kind == null)
            {
                kind = new StackVariable_v1();
            }
            return(argDeser.Deserialize(arg, kind));
        }
Example #10
0
		public void SargWriteStackArgument()
		{
			Argument_v1 sarg = new Argument_v1
            {
			    Name = "bar",
			    Type = new TypeReference_v1("int"),
			    Kind = new StackVariable_v1()
            };
			Verify(sarg, "Core/SargWriteStackArgument.txt");
		}
Example #11
0
 public void SargWriteOutArgument()
 {
     Argument_v1 sarg = new Argument_v1
     {
         Name = "bxOut",
         Type = new SerializedTypeReference("int"),
         OutParameter = true,
         Kind = new Register_v1("bx")
     };
     Verify(sarg, "Core/SargWriteOutArgument.txt");
 }
Example #12
0
        private Identifier DeserializeArgument(ArgumentDeserializer argser, Argument_v1 sArg)
        {
            Identifier arg;
            if (sArg.Kind != null)
                return argser.Deserialize(sArg);

            var dtArg = sArg.Type.Accept(TypeLoader);
            var prim = dtArg as PrimitiveType;
            arg = argser.Deserialize(sArg, new Register_v1("r" + gr));
            ++this.gr;
            return arg;
        }
		public void ArgSer_DeserializeRegister()
		{
			Register_v1 reg = new Register_v1("eax");
            Argument_v1 arg = new Argument_v1
            {
                Name = "eax",
                Kind = reg,
            };
            Identifier id = argser.Deserialize(arg);
			Assert.AreEqual("eax", id.Name);
			Assert.AreEqual(32, id.DataType.BitSize);
		}
Example #14
0
        public Argument_v1 Serialize(Identifier arg)
        {
            if (arg == null)
                return null;
            if (arg.DataType == null)
                throw new ArgumentNullException("arg.DataType");
            Argument_v1 sarg = new Argument_v1 
            {
			    Name = arg.Name,
			    Kind = arg.Storage.Serialize(),
                OutParameter = arg.Storage is OutArgumentStorage,
                Type = arg.DataType.Accept(new DataTypeSerializer()),
            };
            return sarg;
        }
Example #15
0
        public static SerializedSignature BuildSsigAxBxCl()
        {
            SerializedSignature ssig = new SerializedSignature();

            Argument_v1 sarg = new Argument_v1();
            sarg.Type = new PrimitiveType_v1(Domain.SignedInt, 2);
            sarg.Kind = new Register_v1("ax");
            ssig.ReturnValue = sarg;

            ssig.Arguments = new Argument_v1[]
            {
                new Argument_v1 { Kind = new Register_v1("bx") },
                new Argument_v1 { Kind = new Register_v1("cl") },
            };
            return ssig;
        }
		public void SseqBuildSequence()
		{
            Frame f = arch.CreateFrame();
			Identifier head = f.EnsureRegister(Registers.dx);
			Identifier tail = f.EnsureRegister(Registers.ax);
			Identifier seq = f.EnsureSequence(head, tail, PrimitiveType.Word32);
			SerializedSequence sq = new SerializedSequence((SequenceStorage) seq.Storage);
			Argument_v1 sa = new Argument_v1();
			sa.Kind = sq;
			SerializedSignature ssig = new SerializedSignature();
			ssig.Arguments = new Argument_v1[] { sa };

            Given_X86ProcedureSerializer();
			ProcedureSignature ps = ser.Deserialize(ssig, f);
			Assert.AreEqual("void foo(Sequence word32 dx_ax)", ps.ToString("foo"));
		}
Example #17
0
        public Argument_v1 Serialize(Identifier arg)
        {
            if (arg == null)
            {
                return(null);
            }
            if (arg.DataType == null)
            {
                throw new ArgumentNullException("arg.DataType");
            }
            Argument_v1 sarg = new Argument_v1
            {
                Name = arg.Name,
                Kind = arg.Storage != null?arg.Storage.Serialize() : null,
                           OutParameter = arg.Storage is OutArgumentStorage,
                           Type         = arg.DataType.Accept(new DataTypeSerializer()),
            };

            return(sarg);
        }
 public Identifier DeserializeArgument(Argument_v1 arg, int idx, string convention)
 {
     if (arg.Kind != null)
     {
         return argser.Deserialize(arg, arg.Kind);
     }
     if (convention == null)
         return argser.Deserialize(arg, new StackVariable_v1());
     switch (convention)
     {
     case "":
     case "stdapi":
     case "__cdecl":
     case "__stdcall":
         return argser.Deserialize(arg, new StackVariable_v1 { });
     case "__thiscall":
         if (idx == 0)
             return argser.Deserialize(arg, new Register_v1("ecx"));
         else
             return argser.Deserialize(arg, new StackVariable_v1());
     }
     throw new NotSupportedException(string.Format("Unsupported calling convention '{0}'.", convention));
 }
Example #19
0
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     var dtArg = sArg.Type.Accept(TypeLoader) as PrimitiveType;
     if (dtArg != null && dtArg.Domain == Domain.Real)
     {
         return argser.Deserialize(new FpuStackVariable_v1 { ByteSize = dtArg.Size }).Storage;
     }
     var eax = Architecture.GetRegister("eax");
     if (bitSize <= 32)
         return eax;
     if (bitSize <= 64)
     {
         var edx = Architecture.GetRegister("edx");
         return new SequenceStorage(edx, eax);
     }
     throw new NotImplementedException();
 }
Example #20
0
 public abstract Storage GetReturnRegister(Argument_v1 sArg, int bitSize);
Example #21
0
		private Argument_v1 ParseStackArgument(string typeName, string argName)
		{
			PrimitiveType p;
			int sizeInWords;
			int wordSize = arch.WordWidth.Size;
			if (PrimitiveType.TryParse(typeName, out p))
			{
				sizeInWords = (p.Size + (wordSize - 1))/wordSize;
			}
			else
			{
				sizeInWords = 1;      // A reasonable guess, but is it a good one?
			}

			Argument_v1 arg = new Argument_v1();
			arg.Name = argName;
			arg.Type = new SerializedTypeReference { TypeName = typeName };
			arg.Kind = new StackVariable_v1(); //  (sizeInWords * wordSize);
			return arg;
		}
Example #22
0
        private Argument_v1 ParseArg()
        {
            var w = GetNextWord();
            if (w == null)
                return null;

            RegisterStorage reg;
            string type = null;
            if (!arch.TryGetRegister(w, out reg))
            {
                type = w;
                w = GetNextWord();
                if (w == null)
                    return null;
                if (w.Contains("_"))
                {
                    var retval = ParseRegisterSequenceWithUnderscore(w);
                    if (retval != null)
                        return retval;
                }
                if (!arch.TryGetRegister(w, out reg))
                {
                    return ParseStackArgument(type, w);
                }
            }

            if (PeekChar(':') || PeekChar('_'))
            {
                return ParseRegisterSequence(reg, type);
            }

            var arg = new Argument_v1()
            {
                Name = reg.Name,
                Kind = new Register_v1(reg.Name),
                OutParameter = false,
                Type = new SerializedTypeReference { TypeName = type },
            };
            return arg;
        }
Example #23
0
 public Identifier Deserialize(Argument_v1 arg, SerializedKind kind)
 {
     argCur = arg;
     return kind.Deserialize(this);
 }
Example #24
0
 public Identifier Deserialize(Argument_v1 arg)
 {
     argCur = arg;
     return arg.Kind.Deserialize(this);
 }
Example #25
0
 public Identifier Deserialize(Argument_v1 arg)
 {
     argCur = arg;
     return(arg.Kind.Deserialize(this));
 }
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     switch (bitSize)
     {
     case 32: 
         if (Architecture.WordWidth.BitSize == 16)
             return new SequenceStorage(
                 new Identifier("dx", PrimitiveType.Word16, Architecture.GetRegister("dx")),
                 new Identifier("ax", PrimitiveType.Word16, Architecture.GetRegister("ax")));
         break;
     case 64: if (Architecture.WordWidth.BitSize == 32)
             return new SequenceStorage(
                 new Identifier("edx", PrimitiveType.Word16, Architecture.GetRegister("edx")),
                 new Identifier("eax", PrimitiveType.Word16, Architecture.GetRegister("eax")));
         break;
     }
     return Architecture.GetRegister("rax").GetSubregister(0, bitSize);
 }
Example #27
0
        private void Verify(Argument_v1 sarg, string outputFilename)
        {
            using (FileUnitTester fut = new FileUnitTester(outputFilename))
            {
                XmlTextWriter x = new FilteringXmlWriter(fut.TextWriter);
                x.Formatting = Formatting.Indented;
                XmlSerializer ser = SerializedLibrary.CreateSerializer_v1(sarg.GetType());
                ser.Serialize(x, sarg);

                fut.AssertFilesEqual();
            }
        }
Example #28
0
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     if (bitSize == 0)
         bitSize = Architecture.WordWidth.BitSize;
     switch (bitSize)
     {
     case 32: 
         if (Architecture.WordWidth.BitSize == 16)
             return new SequenceStorage(
                 Architecture.GetRegister("dx"),
                 Architecture.GetRegister("ax"));
         break;
     case 64: if (Architecture.WordWidth.BitSize == 32)
             return new SequenceStorage(
                 Architecture.GetRegister("edx"),
                 Architecture.GetRegister("eax"));
         break;
     }
     var reg = Architecture.GetRegister("rax");
     return Architecture.GetSubregister(reg, 0, bitSize);
 }
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     var prim = sArg.Type as PrimitiveType_v1;
     if (prim != null)
     {
         if (prim.Domain == Domain.Real)
             return arch.GetRegister("f1");
     }
     return arch.GetRegister("r3");
 }
Example #30
0
        private AmigaSystemFunction ParseLine(string line)
        {
            lex = new Lexer(line);
            if (!PeekAndDiscard(TokenType.HASH))
                return null;
            Expect(TokenType.Number);   // Ordinal
            var decOffset = (int) Expect(TokenType.Number);   // offset (in decimal)
            var hexOffset = (int) Expect(TokenType.Number);   // offset (in hex)
            if (decOffset != hexOffset)
                throw new InvalidOperationException(string.Format(
                    "Decimal offset {0} and hexadecimal offset {1:X} do not match.", decOffset, hexOffset));
            var name = (string) Expect(TokenType.Id);         // fn name

            var parameters = new List<Argument_v1>();
            Argument_v1 returnValue = null;
            Expect(TokenType.LPAREN);
            if (!PeekAndDiscard(TokenType.RPAREN))
            {
                do {
                    var argName = (string) Expect(TokenType.Id);
                    Expect(TokenType.SLASH);
                    var regName = (string)Expect(TokenType.Id);
                    parameters.Add(new Argument_v1
                    {
                        Kind = new Register_v1 { Name = regName },
                        Name = argName,
                    });
                } while (PeekAndDiscard(TokenType.COMMA));
                if (PeekAndDiscard(TokenType.SEMI))
                {
                    var argName = (string) Expect(TokenType.Id);
                    Expect(TokenType.SLASH);
                    var regName = (string) Expect(TokenType.Id);
                    returnValue = new Argument_v1
                    {
                        Kind = new Register_v1 { Name = regName }
                    };
                }
                Expect(TokenType.RPAREN);
            }
            return new AmigaSystemFunction
            {
                Offset = -decOffset,
                Name = name,
                Signature = new SerializedSignature
                {
                    Arguments = parameters.ToArray(),
                    ReturnValue = returnValue,
                }
            };
        }
Example #31
0
 public Identifier DeserializeReturnValue(Argument_v1 arg)
 {
     argCur = arg;
     DataType dt = null;
     if (this.argCur.Type != null)
         dt = this.argCur.Type.Accept(procSer.TypeLoader);
     if (dt is VoidType)
         return null;
     Identifier id;
     if (arg.Kind != null)
     {
         id = arg.Kind.Deserialize(this);
         id.DataType = dt ?? id.DataType;
     }
     else
     {
         var reg = procSer.GetReturnRegister(arg, dt.BitSize);
         id = new Identifier(reg.ToString(), dt, reg);
     }
     return id;
 }
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     throw new NotImplementedException();
 }
Example #33
0
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     return A32Registers.r0;
 }
Example #34
0
 private bool HasExplicitStorage(Argument_v1 sArg)
 {
     return(sArg.Kind != null);
 }
Example #35
0
 public override Storage GetReturnRegister(Argument_v1 sArg, int bitSize)
 {
     var dtArg = sArg.Type.Accept(TypeLoader) as PrimitiveType;
     if (dtArg != null && dtArg.Domain == Domain.Real)
     {
         var xmm0 = Architecture.GetRegister("xmm0");
         if (bitSize <= 64)
             return xmm0;
         if (bitSize <= 128)
         {
             var xmm1 = Architecture.GetRegister("xmm1");
             return new SequenceStorage(xmm1, xmm0);
         }
         throw new NotImplementedException();
     }
     var rax = Architecture.GetRegister("rax");
     if (bitSize <= 64)
         return rax;
     if (bitSize <= 128)
     {
         var rdx = Architecture.GetRegister("rdx");
         return new SequenceStorage(rdx, rax);
     }
     throw new NotImplementedException();
 }
Example #36
0
 public Identifier Deserialize(Argument_v1 arg, SerializedKind kind)
 {
     argCur = arg;
     return(kind.Deserialize(this));
 }
Example #37
0
 public Identifier DeserializeArgument(Argument_v1 sArg, int idx, string convention)
 {
     if (sArg.Kind != null)
     {
         return argser.Deserialize(sArg, sArg.Kind);
     }
     Identifier arg;
     var dtArg = sArg.Type.Accept(TypeLoader);
     var prim = dtArg as PrimitiveType;
     if (prim != null && prim.Domain == Domain.Real)
     {
         if (this.fr >= fregs.Length)
         {
             arg = argser.Deserialize(sArg, new StackVariable_v1());
         }
         else
         {
             arg = argser.Deserialize(sArg, new Register_v1 { Name = fregs[fr] });
         }
         ++this.fr;
         return arg;
     }
     if (dtArg.Size <= 8)
     {
         if (this.ir >= iregs.Length)
         {
             arg = argser.Deserialize(sArg, new StackVariable_v1());
         }
         else
         {
             arg = argser.Deserialize(sArg, new Register_v1 { Name = iregs[ir] });
         }
         ++this.ir;
         return arg;
     }
     int regsNeeded = (dtArg.Size + 7) / 8;
     if (regsNeeded > 4 || ir + regsNeeded >= iregs.Length)
     {
         return argser.Deserialize(sArg, new StackVariable_v1());
     }
     throw new NotImplementedException();
 }
Example #38
0
		public void SsigDeserializeOutArgument()
		{
            Argument_v1 arg = new Argument_v1
            {
                Kind = new Register_v1("bp"),
                OutParameter = true
            };
			SerializedSignature sig = new SerializedSignature();
			sig.Arguments = new Argument_v1[] { arg };
            Given_X86ProcedureSerializer();
            FunctionType ps = sser.Deserialize(sig, arch.CreateFrame());
			Assert.AreEqual("void foo(Register out ptr16 bpOut)", ps.ToString("foo"));
		}
Example #39
0
 private Identifier DeserializeImplicitThisArgument(SerializedSignature ss)
 {
     var sArg = new Argument_v1
     {
         Type = new PointerType_v1(ss.EnclosingType),
         Name = "this",
     };
     if (ss.Convention == "__thiscall")
     {
         sArg.Kind = new Register_v1("ecx");
     }
     else
         sArg.Kind = new StackVariable_v1();
     var arg = argDeser.Deserialize(sArg);
     return arg;
 }