Exemplo n.º 1
0
        public Formatter VisitUnion(UnionType ut)
        {
            string n = name;

            writer.WriteKeyword("union");
            writer.Write(" ");
            writer.Write(ut.Name);
            OpenBrace();
            int i = 0;

            foreach (UnionAlternative alt in ut.Alternatives.Values)
            {
                BeginLine();
                name = alt.MakeName(i);
                var trf = new TypeReferenceFormatter(writer, true);
                trf.WriteDeclaration(alt.DataType, name);
                EndLine(";");
                ++i;
            }
            CloseBrace();

            name = n;
            WriteName(true);
            return(writer);
        }
Exemplo n.º 2
0
        private int recursionGuard;     //$REVIEW: remove this once deep recursion bugs have been flushed out.

        public GlobalDataWriter(Program program, Formatter formatter, bool chasePointers, bool showAddressInComment, IServiceProvider services)
        {
            this.program              = program;
            this.formatter            = formatter;
            this.chasePointers        = chasePointers;
            this.showAddressInComment = showAddressInComment;
            this.services             = services;
            this.cmp           = new DataTypeComparer();
            this.codeFormatter = new AbsynCodeFormatter(formatter);
            this.tw            = new TypeReferenceFormatter(formatter);
            var eqGlobalStruct = program.Globals.TypeVariable?.Class;

            if (eqGlobalStruct != null)
            {
                var globals = eqGlobalStruct.ResolveAs <StructureType>();
                if (globals != null)
                {
                    this.globals = globals;
                }
                else
                {
                    this.globals = new StructureType();
                }
            }
            else
            {
                this.globals = new StructureType();
            }
            this.queue = new Queue <StructureField>(globals.Fields);
        }
Exemplo n.º 3
0
        public void WriteGlobals(Formatter formatter)
        {
            this.codeFormatter = new CodeFormatter(formatter);
            var tw = new TypeReferenceFormatter(formatter, true);

            this.globals = (StructureType)((EquivalenceClass)((Pointer)program.Globals.TypeVariable.DataType).Pointee).DataType;
            foreach (var field in globals.Fields)
            {
                var name = string.Format("g_{0:X}", field.Name);
                var addr = Address.Ptr32((uint)field.Offset);   //$BUG: this is completely wrong; offsets should be as wide as the platform permits.
                try
                {
                    tw.WriteDeclaration(field.DataType, name);
                    if (program.Image.IsValidAddress(addr))
                    {
                        formatter.Write(" = ");
                        this.rdr = program.CreateImageReader(addr);
                        field.DataType.Accept(this);
                    }
                }
                catch (Exception ex)
                {
                    var dc = services.RequireService <DecompilerEventListener>();
                    dc.Error(
                        dc.CreateAddressNavigator(program, addr),
                        ex,
                        string.Format("Failed to write global variable {0}.", name));
                }
                formatter.Terminate(";");
            }
        }
Exemplo n.º 4
0
 public void WriteGlobalVariable(Address address, DataType dataType, string name, Formatter formatter)
 {
     this.formatter     = formatter;
     this.codeFormatter = new CodeFormatter(formatter);
     this.tw            = new TypeReferenceFormatter(formatter);
     this.globals       = new StructureType();
     this.queue         = new Queue <StructureField>(globals.Fields);
     try
     {
         tw.WriteDeclaration(dataType, name);
         if (program.SegmentMap.IsValidAddress(address))
         {
             formatter.Write(" = ");
             this.rdr = program.CreateImageReader(program.Architecture, address);
             dataType.Accept(this);
         }
     }
     catch (Exception ex)
     {
         var dc = services.RequireService <DecompilerEventListener>();
         dc.Error(
             dc.CreateAddressNavigator(program, address),
             ex,
             "Failed to write global variable {0}.",
             name);
     }
     formatter.Terminate(";");
 }
Exemplo n.º 5
0
 public void SetUp()
 {
     sw = new StringWriter();
     var tf = new TextFormatter(sw) { Indentation = 0 };
     tyfo = new TypeFormatter(tf, false);
     tf = new TextFormatter(sw) { Indentation = 0 };
     tyreffo = new TypeReferenceFormatter(tf, false);
 }
Exemplo n.º 6
0
 public void Tyreffmt_ptr_TypeReference()
 {
     var sw = new StringWriter();
     var trf = new TypeReferenceFormatter(new TextFormatter(sw));
     trf.WriteDeclaration(
         new Pointer(
             new TypeReference("LONG", PrimitiveType.Int32), 4),
         "l0");
     Assert.AreEqual("LONG * l0", sw.ToString());
 }
Exemplo n.º 7
0
 public void Tyreffmt_ptr_function()
 {
     var sw = new StringWriter();
     var trf = new TypeReferenceFormatter(new TextFormatter(sw));
     trf.WriteDeclaration(
         new Pointer(
                 FunctionType.Action(new [] { Arg("arg0", 0) }), 
                 4),
         "pfn");
     Assert.AreEqual("void (* pfn)(word32 arg0)", sw.ToString());
 }
Exemplo n.º 8
0
        public void VisitConversion(Conversion conversion)
        {
            InnerFormatter.Write("CONVERT(");
            var trf = new TypeReferenceFormatter(InnerFormatter);

            WriteExpression(conversion.Expression);
            InnerFormatter.Write(", ");
            trf.WriteTypeReference(conversion.SourceDataType);
            InnerFormatter.Write(", ");
            trf.WriteTypeReference(conversion.DataType);
            InnerFormatter.Write(")");
        }
Exemplo n.º 9
0
        public Formatter VisitStructure(StructureType str)
        {
            string n = name;

            if (mode == Mode.Writing)
            {
                object v;
                if (visited.TryGetValue(str, out v) && (v == Defined))
                {
                    writer.WriteKeyword("struct");
                    writer.Write(" ");
                    writer.Write(str.Name);
                }
                else
                {
                    visited[str] = Declared;
                    ScanFields(str);
                    writer.WriteKeyword("struct");
                    writer.Write(" ");
                    writer.WriteHyperlink(str.Name, str);
                    OpenBrace(str.Size > 0 ? string.Format("size: {0} {0:X}", str.Size) : null);
                    if (str.Fields != null)
                    {
                        foreach (StructureField f in str.Fields)
                        {
                            BeginLine();
                            var trf = new TypeReferenceFormatter(writer);
                            trf.WriteDeclaration(f.DataType, f.Name);
                            EndLine(";", string.Format("{0:X}", f.Offset));
                        }
                    }
                    CloseBrace();
                    visited[str] = Defined;
                }

                name = n;
                WriteName(true);
            }
            else
            {
                if (!visited.ContainsKey(str))
                {
                    visited[str] = Declared;
                    writer.Write("struct ");
                    writer.WriteHyperlink(str.Name, str);
                    writer.Write(";");
                    writer.WriteLine();
                }
            }
            return(writer);
        }
Exemplo n.º 10
0
		public void Write(TextWriter writer)
		{
            TextFormatter f = new TextFormatter(writer);
			foreach (var de in Signatures.OrderBy(d => d.Key, StringComparer.InvariantCulture))
			{
				string name = de.Key;
				de.Value.Emit(de.Key, FunctionType.EmitFlags.ArgumentKind, f);
				writer.WriteLine();
			}

            var tf = new TypeReferenceFormatter(f);
            foreach (var de in Globals.OrderBy(d => d.Key, StringComparer.InvariantCulture))
            {
                tf.WriteDeclaration(de.Value, de.Key);
            }
		}
Exemplo n.º 11
0
        public void VisitDeclaration(Declaration decl)
        {
            InnerFormatter.Indent();
            Debug.Assert(decl.Identifier.DataType != null, "The DataType property can't ever be null");

            var tf = new TypeReferenceFormatter(InnerFormatter);

            tf.WriteDeclaration(decl.Identifier.DataType ?? new UnknownType(), decl.Identifier.Name);

            if (decl.Expression != null)
            {
                InnerFormatter.Write(" = ");
                decl.Expression.Accept(this);
            }
            InnerFormatter.Terminate();
        }
Exemplo n.º 12
0
        public virtual void VisitProcedureConstant(ProcedureConstant pc)
        {
            InnerFormatter.WriteHyperlink(pc.Procedure.Name, pc.Procedure);
            var genArgs = pc.Procedure.GetGenericArguments();

            if (genArgs.Length > 0)
            {
                var sep = '<';
                var tf  = new TypeReferenceFormatter(InnerFormatter);
                foreach (var arg in genArgs)
                {
                    InnerFormatter.Write(sep);
                    sep = ',';
                    tf.WriteTypeReference(arg);
                }
                InnerFormatter.Write('>');
            }
        }
Exemplo n.º 13
0
        public void VisitDeclaration(Declaration decl)
        {
            writer.Indent();
            Debug.Assert(decl.Identifier.DataType != null, "The DataType property can't ever be null");

#if OLD
            TypeFormatter tf = new TypeFormatter(writer, true);
            tf.Write(decl.Identifier.DataType, decl.Identifier.Name);
#else
            TypeReferenceFormatter tf = new TypeReferenceFormatter(writer);
            tf.WriteDeclaration(decl.Identifier.DataType, decl.Identifier.Name);
#endif
            if (decl.Expression != null)
            {
                writer.Write(" = ");
                decl.Expression.Accept(this);
            }
            writer.Terminate();
        }
Exemplo n.º 14
0
 public void WriteFormalArgument(Identifier arg, bool writeStorage, TypeReferenceFormatter t)
 {
     if (writeStorage)
     {
         WriteFormalArgumentType(arg, writeStorage);
         writer.Write(" ");
         writer.Write(arg.Name);
     }
     else
     {
         if (arg.Storage is OutArgumentStorage)
         {
             t.WriteDeclaration(new ReferenceTo(arg.DataType), arg.Name);
         }
         else
         {
             t.WriteDeclaration(arg.DataType, arg.Name);
         }
     }
 }
Exemplo n.º 15
0
        public void WriteGlobals(Formatter formatter)
        {
            this.formatter     = formatter;
            this.codeFormatter = new CodeFormatter(formatter);
            this.tw            = new TypeReferenceFormatter(formatter);
            var eqGlobalStruct = program.Globals.TypeVariable.Class;

            this.globals = eqGlobalStruct.ResolveAs <StructureType>();
            if (this.globals == null)
            {
                Debug.Print("No global variables found.");
                return;
            }
            this.queue = new Queue <StructureField>(globals.Fields);
            while (queue.Count > 0)
            {
                var field = queue.Dequeue();
                WriteGlobalVariable(field);
            }
        }
Exemplo n.º 16
0
 public void VisitDeclaration(AbsynDeclaration decl)
 {
     writer.Indent();
     if (decl.Identifier.DataType != null)
     {
         TypeReferenceFormatter tf = new TypeReferenceFormatter(writer);
         tf.WriteDeclaration(decl.Identifier.DataType, decl.Identifier.Name);
     }
     else
     {
         writer.Write("?unknown?");
         writer.Write(" ");
         decl.Identifier.Accept(this);
     }
     if (decl.Expression != null)
     {
         writer.Write(" = ");
         decl.Expression.Accept(this);
     }
     writer.Terminate(";");
 }
Exemplo n.º 17
0
        private void WriteClassMembers(ClassType ct, ClassProtection protection, string sectionName)
        {
            var methods = ct.Methods.Where(m => m.Protection == protection)
                          .OrderBy(m => m.Offset).ThenBy(m => m.Name)
                          .ToList();
            var fields = ct.Fields.Where(f => f.Protection == protection)
                         .OrderBy(m => m.Offset)
                         .ToList();

            if (methods.Count == 0 && fields.Count == 0)
            {
                return;
            }
            writer.Indentation -= writer.TabSize;
            BeginLine();
            writer.WriteKeyword(sectionName);
            writer.WriteLine(":");
            writer.Indentation += writer.TabSize;

            foreach (var m in methods)
            {
                //$TODO: finish this.
                BeginLine();
                writer.Write(m.Name);
                writer.Write("()");
                EndLine(";");
            }
            if (methods.Count > 0 && fields.Count > 0)
            {
                // separate methods from fields.
                writer.WriteLine();
            }
            foreach (var f in fields)
            {
                BeginLine();
                var trf = new TypeReferenceFormatter(writer);
                trf.WriteDeclaration(f.DataType, f.Name);
                EndLine(";", string.Format("{0:X}", f.Offset));
            }
        }
Exemplo n.º 18
0
 public void SetUp()
 {
     sw = new StringWriter();
     tyfo = new TypeFormatter(new TextFormatter(sw), false);
     tyreffo = new TypeReferenceFormatter(new TextFormatter(sw), false);
 }
Exemplo n.º 19
0
		public void VisitDeclaration(Declaration decl)
		{
			writer.Indent();
            Debug.Assert(decl.Identifier.DataType != null, "The DataType property can't ever be null");

#if OLD
            TypeFormatter tf = new TypeFormatter(writer, true);
            tf.Write(decl.Identifier.DataType, decl.Identifier.Name);
#else
            TypeReferenceFormatter tf = new TypeReferenceFormatter(writer);
            tf.WriteDeclaration(decl.Identifier.DataType, decl.Identifier.Name);
#endif
            if (decl.Expression != null)
			{
				writer.Write(" = ");
				decl.Expression.Accept(this);
			}
			writer.Terminate();
		}
Exemplo n.º 20
0
		public void VisitDeclaration(AbsynDeclaration decl)
		{
			writer.Indent();
			if (decl.Identifier.DataType != null)
			{
                TypeReferenceFormatter tf = new TypeReferenceFormatter(writer);
                tf.WriteDeclaration(decl.Identifier.DataType, decl.Identifier.Name);
			}
			else
			{
                writer.Write("?unknown?");
                writer.Write(" ");
                decl.Identifier.Accept(this);
            }
			if (decl.Expression != null)
			{
				writer.Write(" = ");
				decl.Expression.Accept(this);
			}
			writer.Terminate(";");
		}
Exemplo n.º 21
0
        public void TypeReference()
        {
            tyreffo = new TypeReferenceFormatter(new TextFormatter(sw), true);
            EquivalenceClass b = new EquivalenceClass(new TypeVariable(1));
            b.DataType = new StructureType("b", 0) { Fields = { { 4, PrimitiveType.Word32 } } };

            tyfo.Write(new Pointer(b, 2), "pb");
            Assert.AreEqual("b * pb", sw.ToString());
        }