Esempio n. 1
0
 public override void CaseAHexConstExp(AHexConstExp node)
 {
     int i = Convert.ToInt32(node.GetHexLiteral().Text.Substring(2), 16);
     AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral(i.ToString(), node.GetHexLiteral().Line, node.GetHexLiteral().Pos));
     node.ReplaceBy(intConst);
     intConst.Apply(this);
 }
 public override void CaseAArrayLvalue(AArrayLvalue node)
 {
     bool containedLiteral = containsLiteral;
     containsLiteral = false;
     node.GetIndex().Apply(this);
     bool hasLiteral = containsLiteral;
     containsLiteral = containedLiteral;
     PType type = data.ExpTypes[node.GetIndex()];
     if (!hasLiteral && type is ANamedType && ((ANamedType)type).IsPrimitive("byte"))
     {
         AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("0"));
         ABinopExp binop = new ABinopExp(node.GetIndex(), new APlusBinop(new TPlus("+")), intConst);
         data.ExpTypes[intConst] =
             data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null);
         node.SetIndex(binop);
     }
     node.GetBase().Apply(this);
 }
            public override void CaseADeleteStm(ADeleteStm node)
            {
                List<Node> visitMeNext = new List<Node>();
                APointerType pointer = (APointerType) data.ExpTypes[node.GetExp()];

                //Call deconstructor if it exists
                {
                    AMethodDecl deconstructor = null;
                    if (pointer.GetType() is ANamedType &&
                        data.StructTypeLinks.ContainsKey((ANamedType) pointer.GetType()))
                    {
                        AStructDecl str = data.StructTypeLinks[(ANamedType) pointer.GetType()];

                        deconstructor =
                            data.DeconstructorMap[data.StructDeconstructor[str]];

                    }
                    else //Look for enrichment deconstructor
                    {
                        foreach (AEnrichmentDecl enrichment in data.Enrichments)
                        {
                            if (Util.TypesEqual(pointer.GetType(), enrichment.GetType(), data))
                            {
                                foreach (PDecl decl in enrichment.GetDecl())
                                {
                                    if (decl is ADeconstructorDecl)
                                    {
                                        deconstructor = data.DeconstructorMap[(ADeconstructorDecl) decl];
                                        break;
                                    }
                                }
                                if (deconstructor != null)
                                    break;
                            }
                        }
                    }
                    if (deconstructor != null)
                    {
                        /*
                         * Convert delete <exp>; to
                         *
                         * var deleteVar = <exp>;
                         * Deconstructor(deleteVar);
                         * delete deleteVar;
                         */

                        AALocalDecl deleteVarDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null,
                                                                    null,
                                                                    new ANamedType(
                                                                        new TIdentifier(Util.IsIntPointer(node,
                                                                                                          pointer.
                                                                                                              GetType(),
                                                                                                          data)
                                                                                            ? "int"
                                                                                            : "string"), null),
                                                                    new TIdentifier("deleteVar"), node.GetExp());
                        ALocalLvalue deleteVarRef = new ALocalLvalue(new TIdentifier("deleteVar"));
                        ALvalueExp deleteVarRefExp = new ALvalueExp(deleteVarRef);
                        node.SetExp(deleteVarRefExp);

                        AABlock pBlock = (AABlock) node.Parent();
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node),
                                                      new ALocalDeclStm(new TSemicolon(";"), deleteVarDecl));

                        data.LocalLinks[deleteVarRef] = deleteVarDecl;
                        data.LvalueTypes[deleteVarRef] =
                            data.ExpTypes[deleteVarRefExp] = data.ExpTypes[deleteVarDecl.GetInit()];

                        deleteVarRef = new ALocalLvalue(new TIdentifier("deleteVar"));
                        deleteVarRefExp = new ALvalueExp(deleteVarRef);

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(deconstructor.GetName().Text),
                                                                       new ArrayList() {deleteVarRefExp});
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node),
                                                      new AExpStm(new TSemicolon(";"), invoke));

                        data.LocalLinks[deleteVarRef] = deleteVarDecl;
                        data.LvalueTypes[deleteVarRef] =
                            data.ExpTypes[deleteVarRefExp] = data.ExpTypes[deleteVarDecl.GetInit()];
                        data.SimpleMethodLinks[invoke] = deconstructor;
                        data.ExpTypes[invoke] = deconstructor.GetReturnType();
                        visitMeNext.Add(deleteVarDecl);

                    }
                }

                if (pointer.GetType() is AArrayTempType || pointer.GetType() is ADynamicArrayType)
                {
                    //If struct array, delete all struct data
                    PType baseType;
                    if (pointer.GetType() is AArrayTempType)
                        baseType = ((AArrayTempType) pointer.GetType()).GetType();
                    else
                        baseType = ((ADynamicArrayType)pointer.GetType()).GetType();

                    PExp pointerString = node.GetExp();
                    if (Util.IsBulkCopy(baseType))
                    {
                        node.GetExp().Apply(new MoveMethodDeclsOut("pointerVar", data));
                        pointerString = node.GetExp();
                        ANamedType aBaseType = (ANamedType) baseType;
                        /* Add the following
                         *
                         * string deleteMe = node.getExp(); <-- no
                         * int i = 0;
                         * while (i < array.length)
                         * {
                         *      DeleteStruct<name>(deleteMe + StringToInt(i));
                         *      i = i + 1;
                         * }
                         *
                         */
                        //string deleteMe = node.getExp();
                        /*AALocalDecl deleteMeVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                  new ANamedType(new TIdentifier("string"), null),
                                                                  new TIdentifier("deleteMe"), node.GetExp());*/
                        //pointerString = deleteMeVar;
                        //int i = 0;
                        AALocalDecl iVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                           new ANamedType(new TIdentifier("int"), null),
                                                           new TIdentifier("i"),
                                                           new AIntConstExp(new TIntegerLiteral("0")));
                        //i < array.length
                        ASimpleInvokeExp lenghCall = new ASimpleInvokeExp(new TIdentifier("DataTableGetInt"), new ArrayList());
                        lenghCall.GetArgs().Add(new ABooleanConstExp(new ATrueBool()));
                        //ALocalLvalue deleteMeUse1 = new ALocalLvalue(new TIdentifier("deleteMeVar"));
                        ABinopExp arrayLengthString = new ABinopExp(Util.MakeClone(pointerString, data),
                                                                    new APlusBinop(new TPlus("+")),
                                                                    new AStringConstExp(
                                                                        new TStringLiteral("\"\\\\Length\"")));
                        lenghCall.GetArgs().Add(arrayLengthString);
                        ALocalLvalue iUse1 = new ALocalLvalue(new TIdentifier("i"));
                        ABinopExp cond = new ABinopExp(new ALvalueExp(iUse1), new ALtBinop(new TLt("<")), lenghCall);

                        //DeleteStruct<name>(deleteMe + StringToInt(i));
                        //ALocalLvalue deleteMeUse2 = new ALocalLvalue(new TIdentifier("deleteMeVar"));
                        ALocalLvalue iUse2 = new ALocalLvalue(new TIdentifier("i"));
                        ASimpleInvokeExp intToString = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList());
                        intToString.GetArgs().Add(new ALvalueExp(iUse2));
                        ABinopExp binopExp1 = new ABinopExp(Util.MakeClone(pointerString, data), new APlusBinop(new TPlus("+")), new AStringConstExp(new TStringLiteral("\"[\"")));
                        ABinopExp binopExp2 = new ABinopExp(binopExp1, new APlusBinop(new TPlus("+")), intToString);
                        ABinopExp binopExp3 = new ABinopExp(binopExp2, new APlusBinop(new TPlus("+")), new AStringConstExp(new TStringLiteral("\"]\"")));
                        ASimpleInvokeExp deleteStructInvoke = new ASimpleInvokeExp(new TIdentifier("DeleteStruct" + aBaseType.AsIdentifierString()), new ArrayList());
                        deleteStructInvoke.GetArgs().Add(binopExp3);
                        //i = i + 1;
                        ALocalLvalue iUse3 = new ALocalLvalue(new TIdentifier("i"));
                        ALocalLvalue iUse4 = new ALocalLvalue(new TIdentifier("i"));
                        ABinopExp binopExp = new ABinopExp(new ALvalueExp(iUse4), new APlusBinop(new TPlus("+")), new AIntConstExp(new TIntegerLiteral("1")));
                        AAssignmentExp assign = new AAssignmentExp(new TAssign("="), iUse3, binopExp);

                        //While (...){...}
                        AABlock innerWhile = new AABlock();
                        innerWhile.GetStatements().Add(new AExpStm(new TSemicolon(";"), deleteStructInvoke));
                        innerWhile.GetStatements().Add(new AExpStm(new TSemicolon(";"), assign));
                        AWhileStm whileStm = new AWhileStm(new TLParen("("), cond, new ABlockStm(new TLBrace("{"), innerWhile));

                        AABlock pBlock = (AABlock) node.Parent();
                        //pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), deleteMeVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), iVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), whileStm);
                        //visitMeNext.Add(deleteMeVar);
                        visitMeNext.Add(whileStm);

                        data.ExpTypes[iVar.GetInit()] =
                            data.LvalueTypes[iUse1] =
                            data.LvalueTypes[iUse2] =
                            data.LvalueTypes[iUse3] =
                            data.LvalueTypes[iUse4] =
                            data.ExpTypes[cond.GetLeft()] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[(PExp) intToString.GetArgs()[0]] =
                            data.ExpTypes[binopExp.GetLeft()] =
                            data.ExpTypes[binopExp.GetRight()] =
                            data.ExpTypes[binopExp] =
                            data.ExpTypes[assign] = new ANamedType(new TIdentifier("int"), null);
                        data.ExpTypes[(PExp) lenghCall.GetArgs()[0]] =
                            data.ExpTypes[cond] = new ANamedType(new TIdentifier("bool"), null);
                        data.ExpTypes[lenghCall] =
                           // data.LvalueTypes[deleteMeUse1] =
                           // data.LvalueTypes[deleteMeUse2] =
                            data.ExpTypes[arrayLengthString.GetLeft()] =
                            data.ExpTypes[arrayLengthString.GetRight()] =
                            data.ExpTypes[arrayLengthString] =
                            data.ExpTypes[intToString] =
                            data.ExpTypes[binopExp1] =
                            data.ExpTypes[binopExp1.GetLeft()] =
                            data.ExpTypes[binopExp1.GetRight()] =
                            data.ExpTypes[binopExp2] =
                            data.ExpTypes[binopExp3] =
                            data.ExpTypes[binopExp3.GetRight()] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] =
                            data.ExpTypes[lenghCall] = new ANamedType(new TIdentifier("string"), null);
                        data.ExpTypes[deleteStructInvoke] = new AVoidType(new TVoid("void"));

                        data.Locals[pBlock].Add(iVar);
                        //data.Locals[pBlock].Add(deleteMeVar);

                        data.LocalLinks[iUse1] =
                            data.LocalLinks[iUse2] =
                            data.LocalLinks[iUse3] =
                            data.LocalLinks[iUse4] = iVar;

                        //data.LocalLinks[deleteMeUse1] =
                        //    data.LocalLinks[deleteMeUse2] = deleteMeVar;

                        data.SimpleMethodLinks[lenghCall] =
                            data.Libraries.Methods.First(method => method.GetName().Text == lenghCall.GetName().Text);
                        data.SimpleMethodLinks[intToString] =
                            data.Libraries.Methods.First(method => method.GetName().Text == intToString.GetName().Text);

                        AStructDecl structDecl = data.StructTypeLinks[aBaseType];

                        if (!deleteStructMethod.ContainsKey(structDecl))
                            CreateDeleteStructMethod(node, structDecl, data);
                        data.SimpleMethodLinks[deleteStructInvoke] = deleteStructMethod[structDecl];
                    }

                    /*
                     * Convert delete <exp>
                     * to
                     * DeleteArray(<exp>);
                     *
                     */

                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteArray"), new ArrayList(){pointerString});
                    /*if (pointerString == null)
                    {
                        invoke.GetArgs().Add(node.GetExp());
                    }
                    else
                    {
                        ALocalLvalue local = new ALocalLvalue(new TIdentifier("pointerString"));
                        invoke.GetArgs().Add(new ALvalueExp(local));

                        data.LocalLinks[local] = pointerString;
                        data.LvalueTypes[local] =
                            data.ExpTypes[(PExp) invoke.GetArgs()[0]] = new ANamedType(new TIdentifier("string"), null);
                    }*/
                    data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                    if (deleteArrayMethod == null)
                        CreateDeleteArrayMethod(node, data);
                    data.SimpleMethodLinks[invoke] = deleteArrayMethod;
                    visitMeNext.Add(invoke);

                    node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));
                }
                else
                {
                    //Not array type
                    PExp pointerString = node.GetExp();
                    bool isIntPointer = Util.IsIntPointer(node, pointer.GetType(), data);
                    bool createdStructDelete = false;
                    if (Util.IsBulkCopy(pointer.GetType()))
                    {
                        node.GetExp().Apply(new MoveMethodDeclsOut("pointerVar", data));
                        pointerString = node.GetExp();

                        ANamedType aBaseType = (ANamedType) pointer.GetType();
                        /* Insert
                         *
                         * string deleteMeVar = node.getExp();
                         * DeleteStruct<name>(deleteMeVar);
                         *
                         */

                       /* AALocalDecl deleteMeVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                  new ANamedType(new TIdentifier("string"), null),
                                                                  new TIdentifier("deleteMe"), node.GetExp());*/
                        //pointerString = deleteMeVar;
                        //ALocalLvalue deleteMeUse = new ALocalLvalue(new TIdentifier("deleteMeVar"));

                        PExp deleteExp = Util.MakeClone(pointerString, data);
                        AStructDecl structDecl = data.StructTypeLinks[aBaseType];
                        if (isIntPointer)
                        {
                            GlobalStructVars vars = CreateStructFields(node, structDecl, data);
                            int allocateLimit = int.Parse(structDecl.GetIntDim().Text);
                            if (vars.IdentifierArray != null)
                            {
                                int usedBits = allocateLimit == 0
                                                   ? 0
                                                   : ((int) Math.Floor(Math.Log(allocateLimit, 2)) + 1);
                                int bitsLeft = 31 - usedBits;
                                int biggestIdentifier = (1 << (bitsLeft + 1)) - 1;

                                AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                                deleteExp = new ABinopExp(deleteExp, new ARBitShiftBinop(new TRBitShift(">>")),
                                                          bitsLeftConst);

                                data.ExpTypes[bitsLeftConst] =
                                    data.ExpTypes[deleteExp] = new ANamedType(new TIdentifier("int"), null);
                            }
                        }

                        ASimpleInvokeExp deleteStructInvoke = new ASimpleInvokeExp(new TIdentifier("DeleteStruct" + aBaseType.AsIdentifierString()), new ArrayList());
                        deleteStructInvoke.GetArgs().Add(deleteExp);

                        AABlock pBlock = (AABlock)node.Parent();
                        //pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new ALocalDeclStm(new TSemicolon(";"), deleteMeVar));
                        pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node), new AExpStm(new TSemicolon(";"), deleteStructInvoke));
                        //visitMeNext.Add(deleteMeVar);
                        visitMeNext.Add(deleteStructInvoke);

                        /*data.ExpTypes[(PExp) deleteStructInvoke.GetArgs()[0]] =
                            data.LvalueTypes[deleteMeUse] = new ANamedType(new TIdentifier("string"), null);*/
                        data.ExpTypes[deleteStructInvoke] = new AVoidType(new TVoid("void"));

                        //data.Locals[pBlock].Add(deleteMeVar);

                        //data.LocalLinks[deleteMeUse] = deleteMeVar;

                        if (!deleteStructMethod.ContainsKey(structDecl))
                            CreateDeleteStructMethod(node, structDecl, data);
                        data.SimpleMethodLinks[deleteStructInvoke] = deleteStructMethod[structDecl];
                        createdStructDelete = true;
                    }
                    if (!isIntPointer)
                    {
                        /*
                         * Convert delete <exp>
                         * to
                         * DeleteSimple(<exp>);
                         *
                         */

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteObject"),
                                                                       new ArrayList() {pointerString});
                        /*if (pointerString == null)
                        {
                            invoke.GetArgs().Add(node.GetExp());
                        }
                        else
                        {
                            ALocalLvalue local = new ALocalLvalue(new TIdentifier("pointerString"));
                            invoke.GetArgs().Add(new ALvalueExp(local));

                            data.LocalLinks[local] = pointerString;
                            data.LvalueTypes[local] =
                                data.ExpTypes[(PExp)invoke.GetArgs()[0]] = new ANamedType(new TIdentifier("string"), null);
                        }*/
                        data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                        if (deleteObjectMethod == null)
                            CreateDeleteObjectMethod(node, data);
                        data.SimpleMethodLinks[invoke] = deleteObjectMethod;
                        visitMeNext.Add(invoke);

                        node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));
                    }
                    else if (createdStructDelete)
                    {
                        node.Parent().RemoveChild(node);
                    }
                    else
                    {
                        //There is an enrichment

                        PExp deleteExp = pointerString;
                        AEnrichmentDecl enrichmentDecl = data.EnrichmentTypeLinks[pointer.GetType()];
                        if (isIntPointer)
                        {
                            GlobalStructVars vars = CreateEnrichmentFields(node, enrichmentDecl, data);
                            int allocateLimit = int.Parse(enrichmentDecl.GetIntDim().Text);
                            if (vars.IdentifierArray != null)
                            {
                                int usedBits = allocateLimit == 0
                                                   ? 0
                                                   : ((int)Math.Floor(Math.Log(allocateLimit, 2)) + 1);
                                int bitsLeft = 31 - usedBits;

                                AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                                deleteExp = new ABinopExp(deleteExp, new ARBitShiftBinop(new TRBitShift(">>")),
                                                          bitsLeftConst);

                                data.ExpTypes[bitsLeftConst] =
                                    data.ExpTypes[deleteExp] = new ANamedType(new TIdentifier("int"), null);
                            }
                        }

                        ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("DeleteObject"),
                                                                       new ArrayList() { deleteExp });

                        data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                        data.SimpleMethodLinks[invoke] = CreateDeleteEnrichmentMethod(node, enrichmentDecl, data);
                        visitMeNext.Add(invoke);

                        node.ReplaceBy(new AExpStm(new TSemicolon(";"), invoke));

                    }
                }
                bool hadPointerPreviously = hadPointer;
                PExp previosNameExp = nameExp;
                for (int i = 0; i < visitMeNext.Count; i++)
                {
                    hadPointer = false;
                    nameExp = null;
                    visitMeNext[i].Apply(this);
                }
                hadPointer = hadPointerPreviously;
                nameExp = previosNameExp;
            }
 public override void OutAIntConstExp(AIntConstExp node)
 {
     value = int.Parse(node.GetIntegerLiteral().Text);
     base.OutAIntConstExp(node);
 }
 public override void CaseAIntConstExp(AIntConstExp node)
 {
     if (folding)
         value = int.Parse(node.GetIntegerLiteral().Text);
 }
 public virtual void CaseAIntConstExp(AIntConstExp node)
 {
     DefaultCase(node);
 }
 public override void CaseAIntConstExp(AIntConstExp node)
 {
     InAIntConstExp(node);
     if (node.GetIntegerLiteral() != null)
     {
         node.GetIntegerLiteral().Apply(this);
     }
     OutAIntConstExp(node);
 }
Esempio n. 8
0
        public override void OutAEnumDecl(AEnumDecl node)
        {
            AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(),
                                                   node.GetName(), new ArrayList(), null, new ArrayList());

            TIdentifier typeIdentifier = new TIdentifier("byte");
            ASwitchStm switchStm = new ASwitchStm(new TSwitch("switch"),
                                                  new ALvalueExp(
                                                      new AAmbiguousNameLvalue(new AAName(new ArrayList() {new TIdentifier("enum")}))),
                                                  new ArrayList());
                ;
            AMethodDecl toStringMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("toString"),
                                                         new ArrayList()
                                                             {
                                                                 new AALocalDecl(new APublicVisibilityModifier(), null,
                                                                                 null, null, null,
                                                                                 new ANamedType(typeIdentifier, null),
                                                                                 new TIdentifier("enum"), null)
                                                             },
                                                         new AABlock(
                                                             new ArrayList()
                                                                 {
                                                                     switchStm,
                                                                     new AValueReturnStm(new TReturn("return"), new ANullExp())
                                                                 }, new TRBrace("}")));
            replacer.GetLocals().Add(new ADeclLocalDecl(toStringMethod));

            int intVal = 0;
            int min = int.MaxValue;
            int max = int.MinValue;
            List<TIdentifier> types = new List<TIdentifier>(){typeIdentifier};
            Dictionary<int, List<AALocalDecl>> usedValues = new Dictionary<int, List<AALocalDecl>>();
            foreach (AAEnumLocal value in node.GetValues())
            {
                AIntConstExp intConst;
                if (value.GetValue() != null)
                {
                    intConst = (AIntConstExp) value.GetValue();
                    intVal = int.Parse(intConst.GetIntegerLiteral().Text) + 1;
                }
                else
                {
                    intConst = new AIntConstExp(new TIntegerLiteral(intVal.ToString(), value.GetName().Line, value.GetName().Pos));
                    intVal++;
                }
                min = Math.Min(intVal - 1, min);
                max = Math.Max(intVal - 1, max);
                typeIdentifier = new TIdentifier("byte", value.GetName().Line, value.GetName().Pos);
                types.Add(typeIdentifier);
                switchStm.GetCases().Add(
                    new ASwitchCaseStm(new ACaseSwitchCaseType(new TCase("case"), (PExp) intConst.Clone()),
                                       new AABlock(
                                           new ArrayList()
                                               {
                                                   new AValueReturnStm(new TReturn("return"),
                                                                       new AStringConstExp(
                                                                           new TStringLiteral("\"" +
                                                                                              value.GetName().Text +
                                                                                              "\"")))
                                               }, new TRBrace("}"))));
                AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(),
                                                        new TStatic("static", value.GetName().Line, value.GetName().Pos),
                                                        null, null,
                                                        new TConst("const", value.GetName().Line, value.GetName().Pos),
                                                        new ANamedType(typeIdentifier, null), value.GetName(), intConst);
                replacer.GetLocals().Add(localDecl);
                if (!usedValues.ContainsKey(intVal - 1))
                    usedValues[intVal - 1] = new List<AALocalDecl>();
                usedValues[intVal - 1].Add(localDecl);
            }
            if (min < 0 || max > 255)
                foreach (TIdentifier identifier in types)
                {
                    identifier.Text = "int";
                }
            node.ReplaceBy(replacer);
            foreach (KeyValuePair<int, List<AALocalDecl>> pair in usedValues)
            {
                if (pair.Value.Count <= 1)
                    continue;
                int value = pair.Key;
                List<ErrorCollection.Error> subErrors = new List<ErrorCollection.Error>();
                foreach (AALocalDecl decl in pair.Value)
                {
                    subErrors.Add(new ErrorCollection.Error(decl.GetName(), LocRM.GetString("ErrorText179")));
                }
                errors.Add(new ErrorCollection.Error(replacer.GetName(), LocRM.GetString("ErrorText180") + value + ".", false, subErrors.ToArray()));
            }
            replacer.Apply(this);
            data.Enums.Add(replacer, min < 0 || max > 255);
        }
        public override void OutAEnumDecl(AEnumDecl node)
        {
            AStructDecl replacer = new AStructDecl(node.GetVisibilityModifier(), null, null, null, node.GetEndToken(),
                                                   node.GetName(), new ArrayList(), null, new ArrayList());

            int intVal = 0;
            //int min = int.MaxValue;
            //int max = int.MinValue;
            //List<TIdentifier> types = new List<TIdentifier>();
            foreach (AAEnumLocal value in node.GetValues())
            {
                AIntConstExp intConst;
                if (value.GetValue() != null)
                {
                    intConst = (AIntConstExp) value.GetValue();
                    intVal = int.Parse(intConst.GetIntegerLiteral().Text) + 1;
                }
                else
                {
                    intConst = new AIntConstExp(new TIntegerLiteral(intVal.ToString(), value.GetName().Line, value.GetName().Pos));
                    intVal++;
                }
            //    min = Math.Min(intVal - 1, min);
            //    max = Math.Max(intVal - 1, max);
                TIdentifier typeIdentifier = new TIdentifier(replacer.GetName().Text, value.GetName().Line, value.GetName().Pos);
               // types.Add(typeIdentifier);
                AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(),
                                                        new TStatic("static", value.GetName().Line, value.GetName().Pos),
                                                        null, null,
                                                        new TConst("const", value.GetName().Line, value.GetName().Pos),
                                                        new ANamedType(typeIdentifier, null), value.GetName(), intConst);
                replacer.GetLocals().Add(localDecl);
            }
             /*   if (min < 0 || max > 255)
                foreach (TIdentifier identifier in types)
                {
                    identifier.Text = "int";
                }*/
            node.ReplaceBy(replacer);

            replacer.Apply(this);
            replacer.GetName().Text = "enum " + replacer.GetName().Text;
        }
        private void MakeAssignments(AABlock block, PType type, PLvalue leftSide, bool onEnhritedFields)
        {
            if (type is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType) type))
            {
                AStructDecl str = data.StructTypeLinks[(ANamedType) type];
                foreach (AALocalDecl field in str.GetLocals().OfType<AALocalDecl>())
                {
                    if (!onEnhritedFields && data.EnheritanceLocalMap.ContainsKey(field))
                        continue;

                    ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data));
                    data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide];
                    AStructLvalue newLeftSide = new AStructLvalue(lvalueExp, new ADotDotType(new TDot(".")), new TIdentifier(field.GetName().Text));
                    data.StructFieldLinks[newLeftSide] = field;
                    data.LvalueTypes[newLeftSide] = field.GetType();

                    if (field.GetInit() != null)
                    {
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLeftSide,
                                                                       Util.MakeClone(field.GetInit(), data));
                        data.ExpTypes[assignment] = data.LvalueTypes[newLeftSide];

                        block.GetStatements().Add(new AExpStm(new TSemicolon(";"),
                                                              assignment));
                    }
                    else
                    {
                        MakeAssignments(block, field.GetType(), newLeftSide, onEnhritedFields);
                    }
                }
            }
            else if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType) type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    AIntConstExp index = new AIntConstExp(new TIntegerLiteral(i.ToString()));
                    data.ExpTypes[index] = new ANamedType(new TIdentifier("int"), null);

                    ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(leftSide, data));
                    data.ExpTypes[lvalueExp] = data.LvalueTypes[leftSide];
                    AArrayLvalue newLeftSide = new AArrayLvalue(new TLBracket("["), lvalueExp, index);
                    data.LvalueTypes[newLeftSide] = aType.GetType();

                    MakeAssignments(block, aType.GetType(), newLeftSide, onEnhritedFields);
                }
            }
        }
Esempio n. 11
0
            private List<PStm> MakeAssignments(PLvalue lvalue, PExp exp, List<AALocalDecl> declChain)
            {
                PType type = data.LvalueTypes[lvalue]; //data.ExpTypes[exp];
                PType oldType = data.ExpTypes[exp];
                List<PStm> returner = new List<PStm>();
                if (type is ADynamicArrayType ||type is AArrayTempType)
                {
                    AArrayTempType aType;
                    if (type is ADynamicArrayType)
                        aType = (AArrayTempType) data.LvalueTypes[lvalue];
                    else
                        aType = (AArrayTempType) type;
                    for (int j = 0; j < int.Parse(aType.GetIntDim().Text); j++)
                    {
                        ALvalueExp bulkCopyRefExp = new ALvalueExp(Util.MakeClone(lvalue, data));
                        AIntConstExp index1 = new AIntConstExp(new TIntegerLiteral(j.ToString()));
                        AArrayLvalue leftSideIndex = new AArrayLvalue(new TLBracket("{"), bulkCopyRefExp, index1);

                        AIntConstExp index2 = new AIntConstExp(new TIntegerLiteral(j.ToString()));
                        AArrayLvalue rightSideIndex = new AArrayLvalue(new TLBracket("{"), Util.MakeClone(exp, data), index2);
                        ALvalueExp rightSideExp = new ALvalueExp(rightSideIndex);
                        data.ExpTypes[bulkCopyRefExp] = data.LvalueTypes[lvalue];
                        data.ExpTypes[index1] =
                            data.ExpTypes[index2] = new ANamedType(new TIdentifier("int"), null);
                        data.LvalueTypes[leftSideIndex] =
                            data.LvalueTypes[rightSideIndex] =
                            data.ExpTypes[rightSideExp] = aType.GetType();
                        returner.AddRange(MakeAssignments(leftSideIndex, rightSideExp, declChain));
                    }
                }
                else if (type is ANamedType)
                {
                    ANamedType aType = (ANamedType) type;
                    if (Util.IsBulkCopy(type))
                    {
                        AStructDecl str = data.StructTypeLinks[aType];
                        foreach (AALocalDecl localDecl in str.GetLocals())
                        {
                            ALvalueExp bulkCopyRefExp = new ALvalueExp(Util.MakeClone(lvalue, data));
                            AStructLvalue leftSide = new AStructLvalue(bulkCopyRefExp, new ADotDotType(new TDot(".")),
                                                                       new TIdentifier(localDecl.GetName().Text));

                            AStructLvalue rightSide = new AStructLvalue(Util.MakeClone(exp, data), new ADotDotType(new TDot(".")),
                                                                       new TIdentifier(localDecl.GetName().Text));
                            ALvalueExp rightSideExp = new ALvalueExp(rightSide);
                            List<AALocalDecl> newDeclChain = new List<AALocalDecl>();
                            newDeclChain.AddRange(declChain);
                            newDeclChain.Add(localDecl);
                            data.StructFieldLinks[leftSide] =
                                data.StructFieldLinks[rightSide] = localDecl;
                            data.ExpTypes[bulkCopyRefExp] = data.LvalueTypes[lvalue];
                            data.LvalueTypes[leftSide] =
                                data.LvalueTypes[rightSide] =
                                data.ExpTypes[rightSideExp] = localDecl.GetType();
                            returner.AddRange(MakeAssignments(leftSide, rightSideExp, newDeclChain));
                        }
                    }
                    else
                    {
                        //Primitive, find out if it is used
                        if (declChain[0] == null || UsedParameters[Util.GetAncestor<AMethodDecl>(declChain[0])].Where(usedParameter => usedParameter.Count <= declChain.Count).Any(
                                usedParameter => !usedParameter.Where((t, i) => t != declChain[i]).Any()))
                        {
                            AAssignmentExp assignment = new AAssignmentExp(new TAssign("="),
                                                                           Util.MakeClone(lvalue, data),
                                                                           Util.MakeClone(exp, data));
                            data.ExpTypes[assignment] = data.LvalueTypes[lvalue];
                            returner.Add(new AExpStm(new TSemicolon(";"), assignment));
                        }
                    }
                }
                else if (type is APointerType)
                {
                    //Assign as primitive, find out if it is used
                    if (declChain[0] == null || UsedParameters[Util.GetAncestor<AMethodDecl>(declChain[0])].Where(usedParameter => usedParameter.Count <= declChain.Count).Any(
                            usedParameter => !usedParameter.Where((t, i) => t != declChain[i]).Any()))
                    {
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="),
                                                                       Util.MakeClone(lvalue, data),
                                                                       Util.MakeClone(exp, data));
                        data.ExpTypes[assignment] = data.LvalueTypes[lvalue];
                        returner.Add(new AExpStm(new TSemicolon(";"), assignment));
                    }
                }
                else
                {
                    throw new Exception("Unexpected type. Got " + (type == null ? "null" : type.ToString()));
                }
                return returner;
            }
Esempio n. 12
0
            private static AMethodDecl CreateNullCheckMethodP(Node node, TIntegerLiteral intLiteral, string prefix, GlobalStructVars vars, SharedData data)
            {
                /*
                 *
                            <<usedBits := floor(log2(42))+1>>
                            <<bitsLeft := 31 - usedBits>>
                            <<biggestIdentifier := 2^(bitsLeft + 1) - 1>>
                 *
                 *  bool prefix_IsNull(int pointer)
                 *  {
                 *      int identifier;
                 *      if (pointer == 0)
                 *      {
                 *          return true;
                 *      }
                 *      identifier = pointer & biggestIdentifier;
                 *      pointer = pointer >> bitsLeft;
                 *      return (Str_used[pointer / 31] & (1 << (pointer % 31))) == 0 || identifierArray[pointer] != identifier;
                 *  }
                 */
                int usedLimit = int.Parse(intLiteral.Text);
                int usedBits = usedLimit == 0 ? 0 : ((int)Math.Floor(Math.Log(usedLimit, 2)) + 1);
                int bitsLeft = 31 - usedBits;
                int biggestIdentifier = (1 << (bitsLeft + 1)) - 1;

                AALocalDecl pointerDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("int"), null),
                                                          new TIdentifier("pointer"), null);
                AALocalDecl identifierDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                             new ANamedType(new TIdentifier("int"), null),
                                                             new TIdentifier("identifier"), null);

                ALocalLvalue pointerRef1 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef2 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef3 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef4 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef5 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef6 = new ALocalLvalue(new TIdentifier("pointer"));
                ALocalLvalue pointerRef7 = new ALocalLvalue(new TIdentifier("pointer"));
                ALvalueExp pointerRef1Exp = new ALvalueExp(pointerRef1);
                ALvalueExp pointerRef2Exp = new ALvalueExp(pointerRef2);
                ALvalueExp pointerRef4Exp = new ALvalueExp(pointerRef4);
                ALvalueExp pointerRef5Exp = new ALvalueExp(pointerRef5);
                ALvalueExp pointerRef6Exp = new ALvalueExp(pointerRef6);
                ALvalueExp pointerRef7Exp = new ALvalueExp(pointerRef7);

                ALocalLvalue identifierRef1 = new ALocalLvalue(new TIdentifier("identifier"));
                ALocalLvalue identifierRef2 = new ALocalLvalue(new TIdentifier("identifier"));
                ALvalueExp identifierRef2Exp = new ALvalueExp(identifierRef2);

                AFieldLvalue usedRef = new AFieldLvalue(new TIdentifier("used"));
                ALvalueExp usedRefExp = new ALvalueExp(usedRef);

                AFieldLvalue identifierArrayRef = new AFieldLvalue(new TIdentifier("identifierArray"));
                ALvalueExp identifierArrayRefExp = new ALvalueExp(identifierArrayRef);

                AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("0"));
                AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral(biggestIdentifier.ToString()));
                AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("0"));

                ABinopExp binop4 = new ABinopExp(pointerRef5Exp, new ADivideBinop(new TDiv("/")), intConst4);

                AArrayLvalue arrayLvalue1 = new AArrayLvalue(new TLBracket("["), usedRefExp, binop4);
                AArrayLvalue arrayLvalue2 = new AArrayLvalue(new TLBracket("["), identifierArrayRefExp, pointerRef7Exp);
                ALvalueExp arrayLvalue1Exp = new ALvalueExp(arrayLvalue1);
                ALvalueExp arrayLvalue2Exp = new ALvalueExp(arrayLvalue2);

                ABinopExp binop1 = new ABinopExp(pointerRef1Exp, new AEqBinop(new TEq("==")), intConst1);
                ABinopExp binop2 = new ABinopExp(pointerRef2Exp, new AAndBinop(new TAnd("&")), intConst2);
                ABinopExp binop3 = new ABinopExp(pointerRef4Exp, new ARBitShiftBinop(new TRBitShift(">>")), intConst3);
                ABinopExp binop5 = new ABinopExp(pointerRef6Exp, new AModuloBinop(new TMod("%")), intConst6);
                ABinopExp binop6 = new ABinopExp(intConst5, new ALBitShiftBinop(new TLBitShift("<<")), binop5);
                ABinopExp binop7 = new ABinopExp(arrayLvalue1Exp, new AAndBinop(new TAnd("&")), binop6);
                ABinopExp binop8 = new ABinopExp(binop7, new AEqBinop(new TEq("==")), intConst7);
                ABinopExp binop9 = new ABinopExp(arrayLvalue2Exp, new ANeBinop(new TNeq("!=")), identifierRef2Exp);
                ABinopExp binop10 = new ABinopExp(binop8, new ALazyOrBinop(new TOrOr("||")), binop9);

                AAssignmentExp assignment1 = new AAssignmentExp(new TAssign("="), identifierRef1, binop2);
                AAssignmentExp assignment2 = new AAssignmentExp(new TAssign("="), pointerRef3, binop3);

                ABooleanConstExp boolConst = new ABooleanConstExp(new ATrueBool());

                AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                     new ANamedType(new TIdentifier("bool"), null),
                                                     new TIdentifier(prefix + "_IsNull"), new ArrayList() {pointerDecl},
                                                     new AABlock(
                                                         new ArrayList()
                                                             {
                                                                 new ALocalDeclStm(new TSemicolon(";"), identifierDecl),
                                                                 new AIfThenStm(new TLParen("("), binop1,
                                                                                new ABlockStm(new TLBrace("{"),
                                                                                              new AABlock(
                                                                                                  new ArrayList()
                                                                                                      {
                                                                                                          new AValueReturnStm
                                                                                                              (new TReturn(
                                                                                                                   "return"),
                                                                                                               boolConst)
                                                                                                      },
                                                                                                  new TRBrace("}")))),
                                                                 new AExpStm(new TSemicolon(";"), assignment1),
                                                                 new AExpStm(new TSemicolon(";"), assignment2),
                                                                 new AValueReturnStm(new TReturn("return"), binop10)
                                                             },
                                                         new TRBrace("}")));

                AASourceFile sourceFile = Util.GetAncestor<AASourceFile>(node);
                sourceFile.GetDecl().Add(method);

                data.LocalLinks[pointerRef1] =
                    data.LocalLinks[pointerRef2] =
                    data.LocalLinks[pointerRef3] =
                    data.LocalLinks[pointerRef4] =
                    data.LocalLinks[pointerRef5] =
                    data.LocalLinks[pointerRef6] =
                    data.LocalLinks[pointerRef7] = pointerDecl;

                data.LocalLinks[identifierRef1] =
                    data.LocalLinks[identifierRef2] = identifierDecl;

                data.FieldLinks[usedRef] = vars.Used;
                data.FieldLinks[identifierArrayRef] = vars.IdentifierArray;

                data.LvalueTypes[pointerRef1] =
                    data.LvalueTypes[pointerRef2] =
                    data.LvalueTypes[pointerRef3] =
                    data.LvalueTypes[pointerRef4] =
                    data.LvalueTypes[pointerRef5] =
                    data.LvalueTypes[pointerRef6] =
                    data.LvalueTypes[pointerRef7] =
                    data.ExpTypes[pointerRef1Exp] =
                    data.ExpTypes[pointerRef2Exp] =
                    data.ExpTypes[pointerRef4Exp] =
                    data.ExpTypes[pointerRef5Exp] =
                    data.ExpTypes[pointerRef6Exp] =
                    data.ExpTypes[pointerRef7Exp] =
                    data.LvalueTypes[identifierRef1] =
                    data.LvalueTypes[identifierRef2] =
                    data.ExpTypes[identifierRef2Exp] =
                    data.ExpTypes[intConst1] =
                    data.ExpTypes[intConst2] =
                    data.ExpTypes[intConst3] =
                    data.ExpTypes[intConst4] =
                    data.ExpTypes[intConst5] =
                    data.ExpTypes[intConst6] =
                    data.ExpTypes[intConst7] =
                    data.ExpTypes[intConst1] =
                    data.ExpTypes[binop2] =
                    data.ExpTypes[binop3] =
                    data.ExpTypes[binop4] =
                    data.ExpTypes[binop5] =
                    data.ExpTypes[binop6] =
                    data.ExpTypes[binop7] =
                    data.LvalueTypes[arrayLvalue1] =
                    data.LvalueTypes[arrayLvalue2] =
                    data.ExpTypes[arrayLvalue1Exp] =
                    data.ExpTypes[arrayLvalue2Exp] =
                    data.ExpTypes[assignment1] =
                    data.ExpTypes[assignment2] = new ANamedType(new TIdentifier("int"), null);

                data.ExpTypes[binop1] =
                    data.ExpTypes[binop8] =
                    data.ExpTypes[binop9] =
                    data.ExpTypes[binop10] =
                    data.ExpTypes[boolConst] = new ANamedType(new TIdentifier("bool"), null);

                data.LvalueTypes[usedRef] =
                    data.LvalueTypes[identifierArrayRef] =
                    data.ExpTypes[usedRefExp] =
                    data.ExpTypes[identifierArrayRefExp] = vars.IdentifierArray.GetType();

                return method;
            }
Esempio n. 13
0
            private static AMethodDecl CreateNewObjectMethodP(Node node, TIntegerLiteral intLiteral, string prefix, GlobalStructVars vars, SharedData data)
            {
                if (intLiteral == null)
                    return CreateNewObjectMethod(node, data);

                //if (createStructMethod.ContainsKey(structDecl))
                //    return createStructMethod[structDecl];

                /*
                    int CreateStr()
                    {
                        int i = Str_index;
                        while (Str_used[i / 31] & 1 << (i % 31))
                        {
                            i = i + 1;
                            if (i >= 42)
                            {
                                i = 0;
                            }
                            if (i == Str_index)
                            {
                                UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("Error: Unable to allocate more than 42 dynamic Str types"));
                                IntToString(1/0);
                            }
                        }
                        Str_used[i / 31] = Str_used[i / 31] + Power2(i % 31);
                        Str_index = i;
                        <<if it is being compared with null at any point in time>>
                            <<usedBits := floor(log2(42))+1>>
                            <<bitsLeft := 31 - usedBits>>
                            <<biggestIdentifier := 2^(bitsLeft + 1) - 1>>
                            identifierArray[i] = identifierNext;
                            i = (i << bitsLeft) + identifierNext;
                            identifierNext = identifierNext%biggestIdentifier + 1;
                        return i;
                    }
                 */

                AASourceFile file = Util.GetAncestor<AASourceFile>(node);

                AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral(intLiteral.Text));
                AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("0"));
                AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("0"));
                AIntConstExp intConst8 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst9 = new AIntConstExp(new TIntegerLiteral("31"));
                AIntConstExp intConst10 = new AIntConstExp(new TIntegerLiteral("31"));

                AIntConstExp intConst11 = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp intConst12 = new AIntConstExp(new TIntegerLiteral("1"));

                AFieldLvalue strIndexRef1 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text));
                AFieldLvalue strIndexRef2 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text));
                AFieldLvalue strIndexRef3 = new AFieldLvalue(new TIdentifier(vars.Index.GetName().Text));
                ALvalueExp strIndexRef1Exp = new ALvalueExp(strIndexRef1);
                ALvalueExp strIndexRef2Exp = new ALvalueExp(strIndexRef2);

                AFieldLvalue strUsedRef1 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text));
                AFieldLvalue strUsedRef2 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text));
                AFieldLvalue strUsedRef3 = new AFieldLvalue(new TIdentifier(vars.Used.GetName().Text));
                ALvalueExp strUsedRef1Exp = new ALvalueExp(strUsedRef1);
                ALvalueExp strUsedRef2Exp = new ALvalueExp(strUsedRef2);
                ALvalueExp strUsedRef3Exp = new ALvalueExp(strUsedRef3);

                AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null),
                                                    new TIdentifier("i"), strIndexRef1Exp);
                ALocalLvalue iRef1 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef2 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef3 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef4 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef5 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef6 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef7 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef8 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef9 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef10 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef11 = new ALocalLvalue(new TIdentifier("i"));
                ALocalLvalue iRef12 = new ALocalLvalue(new TIdentifier("i"));
                ALvalueExp iRef1Exp = new ALvalueExp(iRef1);
                ALvalueExp iRef2Exp = new ALvalueExp(iRef2);
                ALvalueExp iRef4Exp = new ALvalueExp(iRef4);
                ALvalueExp iRef5Exp = new ALvalueExp(iRef5);
                ALvalueExp iRef7Exp = new ALvalueExp(iRef7);
                ALvalueExp iRef8Exp = new ALvalueExp(iRef8);
                ALvalueExp iRef9Exp = new ALvalueExp(iRef9);
                ALvalueExp iRef10Exp = new ALvalueExp(iRef10);
                ALvalueExp iRef11Exp = new ALvalueExp(iRef11);
                ALvalueExp iRef12Exp = new ALvalueExp(iRef12);

                ABinopExp binop1 = new ABinopExp(iRef1Exp, new ADivideBinop(new TDiv("/")), intConst1);
                ABinopExp binop2 = new ABinopExp(iRef2Exp, new AModuloBinop(new TMod("%")), intConst2);
                ABinopExp binop3 = new ABinopExp(null, new AAndBinop(new TAnd("&")), null);
                ABinopExp binop4 = new ABinopExp(iRef4Exp, new APlusBinop(new TPlus("+")), intConst3);
                ABinopExp binop5 = new ABinopExp(iRef5Exp, new AGeBinop(new TGteq(">=")), intConst4);
                ABinopExp binop6 = new ABinopExp(iRef7Exp, new AEqBinop(new TEq("==")), strIndexRef2Exp);
                ABinopExp binop7 = new ABinopExp(intConst6, new ADivideBinop(new TDiv("/")), intConst7);
                ABinopExp binop8 = new ABinopExp(iRef8Exp, new ADivideBinop(new TDiv("/")), intConst8);
                ABinopExp binop9 = new ABinopExp(iRef9Exp, new ADivideBinop(new TDiv("/")), intConst9);
                ABinopExp binop10 = new ABinopExp(iRef10Exp, new AModuloBinop(new TMod("%")), intConst10);
                ABinopExp binop11 = new ABinopExp(null, new APlusBinop(new TPlus("+")), null);

                ABinopExp binop12 = new ABinopExp(intConst11, new ALBitShiftBinop(new TLBitShift("<<")), binop2);
                ABinopExp binop13 = new ABinopExp(intConst12, new ALBitShiftBinop(new TLBitShift("<<")), binop10);
                binop3.SetRight(binop12);
                binop11.SetRight(binop13);

                //ASimpleInvokeExp power2Invoke1 = new ASimpleInvokeExp(new TIdentifier("Power2"), new ArrayList() { binop2 });
                //ASimpleInvokeExp power2Invoke2 = new ASimpleInvokeExp(new TIdentifier("Power2"), new ArrayList() { binop10 });
                //binop3.SetRight(power2Invoke1);
                //binop11.SetRight(power2Invoke2);

                AArrayLvalue arrayIndex1 = new AArrayLvalue(new TLBracket("["), strUsedRef1Exp, binop1);
                AArrayLvalue arrayIndex2 = new AArrayLvalue(new TLBracket("["), strUsedRef2Exp, binop8);
                AArrayLvalue arrayIndex3 = new AArrayLvalue(new TLBracket("["), strUsedRef3Exp, binop9);
                ALvalueExp arrayIndex1Exp = new ALvalueExp(arrayIndex1);
                ALvalueExp arrayIndex3Exp = new ALvalueExp(arrayIndex3);
                binop3.SetLeft(arrayIndex1Exp);
                binop11.SetLeft(arrayIndex3Exp);

                AAssignmentExp assignement1 = new AAssignmentExp(new TAssign("="), iRef3, binop4);
                AAssignmentExp assignement2 = new AAssignmentExp(new TAssign("="), iRef6, intConst5);
                AAssignmentExp assignement3 = new AAssignmentExp(new TAssign("="), arrayIndex2, binop11);
                AAssignmentExp assignement4 = new AAssignmentExp(new TAssign("="), strIndexRef3, iRef11Exp);

                ASimpleInvokeExp playerGroupAllInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList());
                AFieldLvalue messageAreaDebugRef = new AFieldLvalue(new TIdentifier("c_messageAreaDebug"));
                ALvalueExp messageAreaDebugRefExp = new ALvalueExp(messageAreaDebugRef);
                AStringConstExp stringConst =
                    new AStringConstExp(
                        new TStringLiteral("\"Galaxy++ Error: Unable to allocate more than " + intLiteral.Text +
                                           " dynamic " + prefix + " types.\""));
                ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() { stringConst });
                ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"),
                                                                             new ArrayList()
                                                                                 {
                                                                                     playerGroupAllInvoke,
                                                                                     messageAreaDebugRefExp,
                                                                                     stringToTextInvoke
                                                                                 });
                ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                          new ArrayList() { binop7 });

                AABlock methodBlock = new AABlock(
                    new ArrayList()
                        {
                            new ALocalDeclStm(new TSemicolon(";"), iDecl),
                            new AWhileStm(new TLParen("("), binop3,
                                          new ABlockStm(new TLBrace("{"),
                                                        new AABlock(
                                                            new ArrayList()
                                                                {
                                                                    new AExpStm
                                                                        (new TSemicolon
                                                                             (";"),
                                                                         assignement1),
                                                                    new AIfThenStm
                                                                        (new TLParen
                                                                             ("("),
                                                                         binop5,
                                                                         new ABlockStm
                                                                             (new TLBrace
                                                                                  ("{"),
                                                                              new AABlock
                                                                                  (new ArrayList
                                                                                       ()
                                                                                       {
                                                                                           new AExpStm
                                                                                               (new TSemicolon
                                                                                                    (";"),
                                                                                                assignement2)
                                                                                       },
                                                                                   new TRBrace
                                                                                       ("}")))),
                                                                    new AIfThenStm
                                                                        (new TLParen
                                                                             ("("),
                                                                         binop6,
                                                                         new ABlockStm
                                                                             (new TLBrace
                                                                                  ("{"),
                                                                              new AABlock
                                                                                  (new ArrayList
                                                                                       ()
                                                                                       {
                                                                                           new AExpStm
                                                                                               (new TSemicolon
                                                                                                    (";"),
                                                                                                displayMessageInvoke),
                                                                                           new AExpStm
                                                                                               (new TSemicolon
                                                                                                    (";"),
                                                                                                intToStringInvoke)
                                                                                       },
                                                                                   new TRBrace
                                                                                       ("}"))))
                                                                },
                                                            new TRBrace(
                                                                "}")))),
                            new AExpStm(new TSemicolon(";"), assignement3),
                            new AExpStm(new TSemicolon(";"), assignement4)
                        },
                    new TRBrace("}"));

                if (vars.IdentifierArray != null)
                {
                    /*
                        <<if it is being compared with null at any point in time>>
                            <<usedBits := floor(log2(42))+1>>
                            <<bitsLeft := 31 - usedBits>>
                            <<biggestIdentifier := 2^(bitsLeft + 1) - 1>>
                            identifierArray[i] = identifierNext;
                            i = (i << bitsLeft) + identifierNext;
                            identifierNext = identifierNext%biggestIdentifier + 1;
                    */
                    int usedLimit = int.Parse(intLiteral.Text);
                    int usedBits = usedLimit == 0 ? 0 : ((int)Math.Floor(Math.Log(usedLimit, 2)) + 1);
                    int bitsLeft = 31 - usedBits;
                    int biggestIdentifier = (1 << (bitsLeft + 1)) - 1;

                    AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                    AIntConstExp biggestIdentifierConst = new AIntConstExp(new TIntegerLiteral(biggestIdentifier.ToString()));
                    AIntConstExp oneIntConst = new AIntConstExp(new TIntegerLiteral("1"));
                    ALocalLvalue secondIRef1 = new ALocalLvalue(new TIdentifier("i"));
                    ALocalLvalue secondIRef2 = new ALocalLvalue(new TIdentifier("i"));
                    ALocalLvalue secondIRef3 = new ALocalLvalue(new TIdentifier("i"));
                    ALvalueExp secondIRef2Exp = new ALvalueExp(secondIRef2);
                    ALvalueExp secondIRef3Exp = new ALvalueExp(secondIRef3);
                    AFieldLvalue identierNExtRef1 = new AFieldLvalue(new TIdentifier("identiferNext"));
                    AFieldLvalue identierNExtRef2 = new AFieldLvalue(new TIdentifier("identiferNext"));
                    AFieldLvalue identierNExtRef3 = new AFieldLvalue(new TIdentifier("identiferNext"));
                    AFieldLvalue identierNExtRef4 = new AFieldLvalue(new TIdentifier("identiferNext"));
                    ALvalueExp identierNExtRef1Exp = new ALvalueExp(identierNExtRef1);
                    ALvalueExp identierNExtRef3Exp = new ALvalueExp(identierNExtRef3);
                    ALvalueExp identierNExtRef4Exp = new ALvalueExp(identierNExtRef4);
                    AFieldLvalue identifierArrayRef = new AFieldLvalue(new TIdentifier("identifierArray"));
                    ALvalueExp identifierArrayRefExp = new ALvalueExp(identifierArrayRef);

                    AArrayLvalue arrayLvalue = new AArrayLvalue(new TLBracket("["), identifierArrayRefExp, secondIRef3Exp);

                    AAssignmentExp secondAssignment3 = new AAssignmentExp(new TAssign("="), arrayLvalue, identierNExtRef4Exp);

                    methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment3));

                    ABinopExp secondBinop1 = new ABinopExp(secondIRef2Exp, new ALBitShiftBinop(new TLBitShift("<<")), bitsLeftConst);
                    ABinopExp secondBinop2 = new ABinopExp(secondBinop1, new APlusBinop(new TPlus("+")), identierNExtRef1Exp);

                    AAssignmentExp secondAssignment1 = new AAssignmentExp(new TAssign("="), secondIRef1, secondBinop2);

                    methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment1));

                    ABinopExp secondBinop3 = new ABinopExp(identierNExtRef3Exp, new AModuloBinop(new TMod("%")), biggestIdentifierConst);
                    ABinopExp secondBinop4 = new ABinopExp(secondBinop3, new APlusBinop(new TPlus("+")), oneIntConst);

                    AAssignmentExp secondAssignment2 = new AAssignmentExp(new TAssign("="), identierNExtRef2, secondBinop4);

                    methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), secondAssignment2));

                    data.LvalueTypes[secondIRef1] =
                        data.LvalueTypes[secondIRef2] =
                        data.ExpTypes[bitsLeftConst] =
                        data.ExpTypes[biggestIdentifierConst] =
                        data.ExpTypes[oneIntConst] =
                        data.ExpTypes[secondIRef2Exp] =
                        data.LvalueTypes[identierNExtRef1] =
                        data.LvalueTypes[identierNExtRef2] =
                        data.LvalueTypes[identierNExtRef3] =
                        data.ExpTypes[identierNExtRef1Exp] =
                        data.ExpTypes[identierNExtRef3Exp] =
                        data.ExpTypes[secondBinop1] =
                        data.ExpTypes[secondBinop2] =
                        data.ExpTypes[secondAssignment1] =
                        data.ExpTypes[secondBinop3] =
                        data.ExpTypes[secondBinop4] =
                        data.ExpTypes[secondAssignment2] =
                        data.LvalueTypes[secondIRef3] =
                        data.LvalueTypes[identierNExtRef4] =
                        data.ExpTypes[secondIRef3Exp] =
                        data.ExpTypes[identierNExtRef4Exp] =
                        data.LvalueTypes[arrayLvalue] =
                        data.ExpTypes[secondAssignment3] = new ANamedType(new TIdentifier("int"), null);

                    data.LvalueTypes[identifierArrayRef] =
                        data.ExpTypes[identifierArrayRefExp] = vars.IdentifierArray.GetType();

                    data.LocalLinks[secondIRef1] =
                        data.LocalLinks[secondIRef2] =
                        data.LocalLinks[secondIRef3] = iDecl;

                    data.FieldLinks[identierNExtRef1] =
                        data.FieldLinks[identierNExtRef2] =
                        data.FieldLinks[identierNExtRef3] =
                        data.FieldLinks[identierNExtRef4] = vars.IdentifierNext;

                    data.FieldLinks[identifierArrayRef] = vars.IdentifierArray;
                }

                methodBlock.GetStatements().Add(new AValueReturnStm(new TReturn("return"), iRef12Exp));

                AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                                 new ANamedType(new TIdentifier("int"), null),
                                                                 new TIdentifier("Create" + prefix, data.LineCounts[file] + 18, 0),
                                                                 new ArrayList(),
                                                                 methodBlock);

                file.GetDecl().Add(method);
                data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(file, method));

                data.LocalLinks[iRef1] =
                    data.LocalLinks[iRef2] =
                    data.LocalLinks[iRef3] =
                    data.LocalLinks[iRef4] =
                    data.LocalLinks[iRef5] =
                    data.LocalLinks[iRef6] =
                    data.LocalLinks[iRef7] =
                    data.LocalLinks[iRef8] =
                    data.LocalLinks[iRef9] =
                    data.LocalLinks[iRef10] =
                    data.LocalLinks[iRef11] =
                    data.LocalLinks[iRef12] = iDecl;
                data.FieldLinks[strUsedRef1] =
                    data.FieldLinks[strUsedRef2] =
                    data.FieldLinks[strUsedRef3] = vars.Used;
                data.FieldLinks[strIndexRef1] =
                    data.FieldLinks[strIndexRef2] =
                    data.FieldLinks[strIndexRef3] = vars.Index;
                //data.SimpleMethodLinks[power2Invoke1] =
                //    data.SimpleMethodLinks[power2Invoke2] = CreatePower2Method(node, data);
                data.FieldLinks[messageAreaDebugRef] =
                    data.Libraries.Fields.First(f => f.GetName().Text == messageAreaDebugRef.GetName().Text);
                data.SimpleMethodLinks[displayMessageInvoke] =
                    data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text);
                data.SimpleMethodLinks[playerGroupAllInvoke] =
                    data.Libraries.Methods.First(m => m.GetName().Text == playerGroupAllInvoke.GetName().Text);
                data.SimpleMethodLinks[stringToTextInvoke] =
                    data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text);
                data.SimpleMethodLinks[intToStringInvoke] =
                    data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);

                data.ExpTypes[intConst1] =
                    data.ExpTypes[intConst2] =
                    data.ExpTypes[intConst3] =
                    data.ExpTypes[intConst4] =
                    data.ExpTypes[intConst5] =
                    data.ExpTypes[intConst6] =
                    data.ExpTypes[intConst7] =
                    data.ExpTypes[intConst8] =
                    data.ExpTypes[intConst9] =
                    data.ExpTypes[intConst10] =
                    data.ExpTypes[intConst11] =
                    data.ExpTypes[intConst12] =
                    data.LvalueTypes[strIndexRef1] =
                    data.LvalueTypes[strIndexRef2] =
                    data.LvalueTypes[strIndexRef3] =
                    data.ExpTypes[strIndexRef1Exp] =
                    data.ExpTypes[strIndexRef2Exp] =
                    data.LvalueTypes[iRef1] =
                    data.LvalueTypes[iRef2] =
                    data.LvalueTypes[iRef3] =
                    data.LvalueTypes[iRef4] =
                    data.LvalueTypes[iRef5] =
                    data.LvalueTypes[iRef6] =
                    data.LvalueTypes[iRef7] =
                    data.LvalueTypes[iRef8] =
                    data.LvalueTypes[iRef9] =
                    data.LvalueTypes[iRef10] =
                    data.LvalueTypes[iRef11] =
                    data.LvalueTypes[iRef12] =
                    data.ExpTypes[iRef1Exp] =
                    data.ExpTypes[iRef2Exp] =
                    data.ExpTypes[iRef4Exp] =
                    data.ExpTypes[iRef5Exp] =
                    data.ExpTypes[iRef7Exp] =
                    data.ExpTypes[iRef8Exp] =
                    data.ExpTypes[iRef9Exp] =
                    data.ExpTypes[iRef10Exp] =
                    data.ExpTypes[iRef11Exp] =
                    data.ExpTypes[iRef12Exp] =
                    data.LvalueTypes[arrayIndex1] =
                    data.LvalueTypes[arrayIndex2] =
                    data.LvalueTypes[arrayIndex3] =
                    data.ExpTypes[arrayIndex1Exp] =
                    data.ExpTypes[arrayIndex3Exp] =
                    data.ExpTypes[binop1] =
                    data.ExpTypes[binop2] =
                    data.ExpTypes[binop3] =
                    data.ExpTypes[binop4] =
                    data.ExpTypes[binop7] =
                    data.ExpTypes[binop8] =
                    data.ExpTypes[binop9] =
                    data.ExpTypes[binop10] =
                    data.ExpTypes[binop11] =
                    data.ExpTypes[binop12] =
                    data.ExpTypes[binop13] =
                    //data.ExpTypes[power2Invoke1] =
                    //data.ExpTypes[power2Invoke2] =
                    data.ExpTypes[intToStringInvoke] =
                    data.ExpTypes[assignement1] =
                    data.ExpTypes[assignement2] =
                    data.ExpTypes[assignement3] =
                    data.ExpTypes[assignement4] =
                    data.LvalueTypes[messageAreaDebugRef] =
                    data.ExpTypes[messageAreaDebugRefExp] = new ANamedType(new TIdentifier("int"), null);

                data.LvalueTypes[strUsedRef1] =
                    data.LvalueTypes[strUsedRef2] =
                    data.LvalueTypes[strUsedRef3] =
                    data.ExpTypes[strUsedRef1Exp] =
                    data.ExpTypes[strUsedRef2Exp] =
                    data.ExpTypes[strUsedRef3Exp] = vars.Used.GetType();

                data.ExpTypes[binop5] =
                    data.ExpTypes[binop6] = new ANamedType(new TIdentifier("int"), null);

                data.ExpTypes[stringConst] =
                    data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);

                data.ExpTypes[stringToTextInvoke] = new ANamedType(new TIdentifier("text"), null);

                data.ExpTypes[playerGroupAllInvoke] = new ANamedType(new TIdentifier("playergroup"), null);

                data.ExpTypes[displayMessageInvoke] = new AVoidType(new TVoid("void"));

                return method;
            }
Esempio n. 14
0
            public override void CaseAPointerLvalue(APointerLvalue node)
            {
                hadPointer = false;
                nameExp = null;
                base.CaseAPointerLvalue(node);
                if (Util.IsIntPointer(node, data.LvalueTypes[node], data))
                {
                    if (nameExp != null)
                    {
                        //Create a data table get string exp
                        nameExp = CreateDynaicGetStm("int");
                    }
                    else
                    {
                        nameExp = node.GetBase();
                    }
                    //Replace by str_Array[<base>];
                    //If this is a compared pointer, replace it by str_Array[<base> >> <<bitsLeft>>]
                    GlobalStructVars vars;
                    int allocateLimit;
                    if (data.EnrichmentTypeLinks.ContainsKey(data.LvalueTypes[node]))
                    {
                        AEnrichmentDecl enrichmentDecl = data.EnrichmentTypeLinks[data.LvalueTypes[node]];
                        vars = CreateEnrichmentFields(node, enrichmentDecl, data);
                        allocateLimit = int.Parse(enrichmentDecl.GetIntDim().Text);
                    }
                    else
                    {
                        AStructDecl structDecl = data.StructTypeLinks[(ANamedType)data.LvalueTypes[node]];
                        vars = CreateStructFields(node, structDecl, data);
                        allocateLimit = int.Parse(structDecl.GetIntDim().Text);
                    }

                    AFieldLvalue array = new AFieldLvalue(new TIdentifier(vars.Array.GetName().Text));
                    ALvalueExp arrayExp = new ALvalueExp(array);

                    PExp arrayIndex = nameExp;
                    if (vars.IdentifierArray != null)
                    {
                        int usedBits = allocateLimit == 0 ? 0 : ((int)Math.Floor(Math.Log(allocateLimit, 2)) + 1);
                        int bitsLeft = 31 - usedBits;
                        int biggestIdentifier = (1 << (bitsLeft + 1)) - 1;

                        AIntConstExp bitsLeftConst = new AIntConstExp(new TIntegerLiteral(bitsLeft.ToString()));
                        arrayIndex = new ABinopExp(arrayIndex, new ARBitShiftBinop(new TRBitShift(">>")), bitsLeftConst);

                        data.ExpTypes[bitsLeftConst] =
                            data.ExpTypes[arrayIndex] = new ANamedType(new TIdentifier("int"), null);
                    }

                    AArrayLvalue replacer = new AArrayLvalue(new TLBracket("["), arrayExp, arrayIndex);
                    node.ReplaceBy(replacer);

                    data.FieldLinks[array] = vars.Array;
                    data.LvalueTypes[array] = data.ExpTypes[arrayExp] = vars.Array.GetType();
                    data.LvalueTypes[replacer] = ((AArrayTempType) vars.Array.GetType()).GetType();

                    hadPointer = false;
                    nameExp = null;
                    return;
                }
                //if (Util.GetAncestor<AAssignmentExp>(node) != null || Util.GetAncestor<APArrayLengthLvalue>(node) != null)
                {
                    if (nameExp != null)
                    {
                        //Create a data table get string exp

                        nameExp = CreateDynaicGetStm("string");
                    }
                    else
                    {
                        nameExp = node.GetBase();
                    }
                }
                hadPointer = true;
                CheckDynamicLvalue(node);
                //Todo: Insert runtime check that the pointer is actually not null / points to a delete object
                //(unless the same pointer was checked before, and it has not been assigned to since, and there has not been a method call since.
            }
Esempio n. 15
0
            public override void CaseAFieldDecl(AFieldDecl node)
            {
                PType type = node.GetType();
                if (node.GetInit() is ANullExp &&
                    type is APointerType &&
                    Util.IsIntPointer(node, ((APointerType)type).GetType(), data))
                {
                    AIntConstExp replacer = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[replacer] = new ANamedType(new TIdentifier("int"), null);
                    node.GetInit().ReplaceBy(replacer);
                }

                base.CaseAFieldDecl(node);
            }
 public override void OutAIntConstExp(AIntConstExp node)
 {
     int i = int.Parse(node.GetIntegerLiteral().Text);
     data.ExpTypes[node] = new ANamedType(new TIdentifier(i < 256 && i >= 0 ? "byte" : "int"), null);
     base.OutAIntConstExp(node);
 }
        private List<PStm> AssignDefault(PLvalue lvalue)
        {
            List<PStm> returner = new List<PStm>();
            PType type = data.LvalueTypes[lvalue];
            PExp rightSide = null;
            if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                if (aType.IsPrimitive("string"))//aType.GetName().Text == "string")
                {
                    rightSide = new AStringConstExp(new TStringLiteral("\"\""));
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null);
                }
                else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text))
                {
                    rightSide = new ANullExp();
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null);
                }
                else if (aType.IsPrimitive(new []{"int", "byte", "fixed"}))
                    /*aType.GetName().Text == "int" ||
                    aType.GetName().Text == "byte" ||
                    aType.GetName().Text == "fixed")*/
                {
                    rightSide = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool")
                {
                    rightSide = new ABooleanConstExp(new AFalseBool());
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color")
                {
                    PExp arg1 = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp arg2 = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp arg3 = new AIntConstExp(new TIntegerLiteral("0"));
                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList() { arg1, arg2, arg3 });
                    rightSide = invoke;
                    data.ExpTypes[rightSide] = type;
                    data.ExpTypes[arg1] =
                        data.ExpTypes[arg2] =
                        data.ExpTypes[arg3] = new ANamedType(new TIdentifier("int"), null);
                    data.SimpleMethodLinks[invoke] =
                        data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text);
                }
                else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char")
                {
                    //Dunno?!
                    rightSide = new ACharConstExp(new TCharLiteral("'\0'"));
                    data.ExpTypes[rightSide] = type;
                }
                else //Struct
                {
                    AStructDecl str = data.StructTypeLinks[aType];
                    foreach (AALocalDecl localDecl in str.GetLocals())
                    {
                        ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data));
                        AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text));
                        data.StructFieldLinks[newLvalue] = localDecl;
                        data.ExpTypes[reciever] = type;
                        data.LvalueTypes[newLvalue] = localDecl.GetType();
                        returner.AddRange(AssignDefault(newLvalue));
                    }
                    return returner;
                }
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide);
                data.ExpTypes[assignment] = type;
                return new List<PStm>() { new AExpStm(new TSemicolon(";"), assignment) };
            }
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    ALvalueExp reciever = new ALvalueExp(Util.MakeClone(lvalue, data));
                    AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString())));
                    data.ExpTypes[reciever] = type;
                    data.LvalueTypes[newLvalue] = aType.GetType();
                    data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null);
                    returner.AddRange(AssignDefault(newLvalue));
                }
                return returner;
            }

            throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type);
        }
Esempio n. 18
0
        public override void CaseADelegateInvokeExp(ADelegateInvokeExp node)
        {
            //Build a list of the possible methods
            AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node);
            List<AMethodDecl> methods = new List<AMethodDecl>();
            ANamedType type = (ANamedType) finalTrans.data.ExpTypes[node.GetReceiver()];
            AMethodDecl delegateMethod = finalTrans.data.DelegateTypeLinks[type];
            foreach (KeyValuePair<ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod)
            {
                if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type))
                {
                    if (!methods.Contains(delegateCreationPair.Value))
                        methods.Add(delegateCreationPair.Value);
                }
            }
            MoveMethodDeclsOut mover;
            if (methods.Count == 0)
            {
                //Can only remove it if the return value is unused
                if (!(node.Parent() is AExpStm))
                {
                    finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                    currentFile,
                                                                    LocRM.GetString("Delegates_Text1")));
                    throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp");
                }

                mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
                foreach (Node arg in node.GetArgs())
                {
                    arg.Apply(mover);
                }
                node.Parent().Parent().RemoveChild(node.Parent());
                foreach (PStm stm in mover.NewStatements)
                {
                    stm.Apply(this);
                }
                return;
            }
            if (methods.Count == 1)
            {
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList());
                while (node.GetArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetArgs()[0]);
                }

                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0])))
                {
                    AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key;
                    AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList(){node.GetReceiver()});
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = methods[0];
                finalTrans.data.ExpTypes[invoke] = methods[0].GetReturnType();
                node.ReplaceBy(invoke);
                return;
            }
            //Multiple methods. Make
            /*
             * <Methods moved out from reciever>
             * string delegate = GetMethodPart(<reciever>);
             * if (delegate == "...")
             * {
             *    Foo(...);
             * }
             * else if (delegate == "...")
             * {
             *    Bar(..., GetPointerPart(<reciever>);
             * }
             * else if(...)
             * ...
             * else
             * {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate."));
             *     int i = 1/0;
             *     return;
             * }
             *
             */
            AABlock block = new AABlock(new ArrayList(), new TRBrace("}"));
            mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
            node.GetReceiver().Apply(mover);
            AMethodDecl methodPartMethod = GetMethodMethod();
            ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"),
                                                                     new ArrayList()
                                                                         {
                                                                             Util.MakeClone(node.GetReceiver(),
                                                                                            finalTrans.data)
                                                                         });
            finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod;
            finalTrans.data.ExpTypes[methodPartInvoke] = methodPartMethod.GetReturnType();
            AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("methodPart"), methodPartInvoke);

            block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl));
            //If the invoke's return value is used, get the lvalue
            PLvalue leftSide;
            if (node.Parent() is AALocalDecl)
            {
                leftSide = new ALocalLvalue(new TIdentifier("renameMe"));
                finalTrans.data.LocalLinks[(ALocalLvalue) leftSide] = (AALocalDecl) node.Parent();
                finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null);
                PStm pStm = Util.GetAncestor<PStm>(node);
                AABlock pBlock = (AABlock) pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block));
                node.Parent().RemoveChild(node);
            }
            else if (node.Parent() is AAssignmentExp)
            {
                AAssignmentExp assignExp = (AAssignmentExp) node.Parent();
                leftSide = assignExp.GetLvalue();
                leftSide.Apply(mover);

                PStm pStm = Util.GetAncestor<PStm>(node);
                pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else if (node.Parent() is AExpStm)
            {
                //No assignments needed
                leftSide = null;
                node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else
            {
                //Create a new local
                AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                           Util.MakeClone(delegateMethod.GetReturnType(),
                                                                          finalTrans.data),
                                                           new TIdentifier("delegateVar"), null);
                ALocalLvalue leftSideLink = new ALocalLvalue(new TIdentifier("delegateVar"));
                ALvalueExp leftSideLinkExp = new ALvalueExp(leftSideLink);

                PStm pStm = Util.GetAncestor<PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block));

                node.ReplaceBy(leftSideLinkExp);

                finalTrans.data.LocalLinks[leftSideLink] = leftSideDecl;
                finalTrans.data.LvalueTypes[leftSideLink] =
                    finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType();

                leftSide = leftSideLink;
                block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl));
            }

            ABlockStm elseBranch;
            //Make final else branch
            /* {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate."));
             *     IntToString(1/0);
             *     return;
             * }
             */
            {
                AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp playerGroupInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList());
                AFieldLvalue messageAreaLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug"));
                ALvalueExp messageAreaLinkExp = new ALvalueExp(messageAreaLink);
                AStringConstExp stringConst =
                    new AStringConstExp(
                        new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" +
                                           node.GetToken().Line + ", " + node.GetToken().Pos +
                                           "]: Got a null delegate.\""));
                ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"),
                                                                           new ArrayList() {stringConst});
                ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"),
                                                                             new ArrayList()
                                                                                 {
                                                                                     playerGroupInvoke,
                                                                                     messageAreaLinkExp,
                                                                                     stringToTextInvoke
                                                                                 });

                AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("0"));
                ABinopExp binop = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2);
                ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                          new ArrayList() {binop});

                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke));
                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke));
                //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return")));

                elseBranch = new ABlockStm(new TLBrace("{"), innerBlock);

                finalTrans.data.SimpleMethodLinks[playerGroupInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[stringToTextInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[displayMessageInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                finalTrans.data.FieldLinks[messageAreaLink] =
                    finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text);

                finalTrans.data.ExpTypes[playerGroupInvoke] =
                    finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType();
                finalTrans.data.LvalueTypes[messageAreaLink] =
                    finalTrans.data.ExpTypes[messageAreaLinkExp] =
                    finalTrans.data.FieldLinks[messageAreaLink].GetType();
                finalTrans.data.ExpTypes[stringToTextInvoke] =
                    finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType();
                finalTrans.data.ExpTypes[stringConst] =
                    finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                finalTrans.data.ExpTypes[displayMessageInvoke] = new AVoidType();

                finalTrans.data.ExpTypes[intConst1] =
                    finalTrans.data.ExpTypes[intConst2] =
                    finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null);
            }

            foreach (AMethodDecl method in methods)
            {
             /*  * if (delegate == "...")
                 * {
                 *    Foo(...);
                 * }
                 * else if (delegate == "...")
                 * {
                 *    Bar(..., GetPointerPart(<reciever>);
                 * }
                 * else if(...)
                 * ...
                 */
                AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList());
                for (int i = 0; i < node.GetArgs().Count; i++)
                {
                    PExp arg = (PExp) node.GetArgs()[i];
                    invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data));
                }
                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method)))
                {
                    AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key;
                    AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { Util.MakeClone(node.GetReceiver(), data) });
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = method;
                finalTrans.data.ExpTypes[invoke] = method.GetReturnType();

                if (leftSide == null)
                {
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));
                }
                else
                {
                    AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke);
                    finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke];
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));
                }
                ALocalLvalue methodPartLink = new ALocalLvalue(new TIdentifier("methodPart"));
                ALvalueExp methodPartLinkExp = new ALvalueExp(methodPartLink);
                AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\""));
                finalTrans.data.LocalLinks[methodPartLink] = methodPartDecl;
                finalTrans.data.LvalueTypes[methodPartLink] =
                    finalTrans.data.ExpTypes[methodPartLinkExp] =
                    finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);

                ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst);
                finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null);

                AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch);

                elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList() { ifThenElse }, new TRBrace("}")));
            }

            block.GetStatements().Add(elseBranch);
        }
 public override void CaseAIntConstExp(AIntConstExp node)
 {
     containsLiteral = true;
 }
Esempio n. 20
0
        private AMethodDecl GetMethodMethod()
        {
            if (GetMethodPartMethod != null)
                return GetMethodPartMethod;
            /*
                string GetMethodPart(string delegate)
                {
                    int i = StringFind(delegate, ":", false);
                    if (i == -1)
                    {
                        return delegate;
                    }
                    return StringSub(delegate, 1, i - 1);
                }
             */
            AASourceFile sourceFile = Util.GetAncestor<AASourceFile>(finalTrans.mainEntry);

            AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool());
            AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("-1"));
            AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1"));

            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"),
                                                                     new ArrayList()
                                                                         {delegateRef1Exp, stringConst, booleanConst});
            AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null),
                                                new TIdentifier("i"), stringFindInvoke);
            ALocalLvalue iRef1 = new ALocalLvalue(new TIdentifier("i"));
            ALocalLvalue iRef2 = new ALocalLvalue(new TIdentifier("i"));
            ALvalueExp iRef1Exp = new ALvalueExp(iRef1);
            ALvalueExp iRef2Exp = new ALvalueExp(iRef2);

            ABinopExp binop1 = new ABinopExp(iRef1Exp, new AEqBinop(new TEq("==")), intConst1);
            AIfThenStm ifThen = new AIfThenStm(new TLParen("("), binop1,
                                               new ABlockStm(new TLBrace("{"),
                                                             new AABlock(
                                                                 new ArrayList()
                                                                     {
                                                                         new AValueReturnStm(new TReturn("return"),
                                                                                             delegateRef2Exp)
                                                                     },
                                                                 new TRBrace("}"))));

            ABinopExp binop2 = new ABinopExp(iRef2Exp, new AMinusBinop(new TMinus("-")), intConst3);
            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                                    new ArrayList() {delegateRef3Exp, intConst2, binop2});

            GetMethodPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                  new ANamedType(new TIdentifier("string"), null),
                                                  new TIdentifier("GetMethodPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() { delegateFormal },
                                                  new AABlock(
                                                      new ArrayList()
                                                          {
                                                              new ALocalDeclStm(new TSemicolon(";"), iDecl),
                                                              ifThen,
                                                              new AValueReturnStm(new TReturn("return"), stringSubInvoke)
                                                          },
                                                      new TRBrace("}")));
            sourceFile.GetDecl().Add(GetMethodPartMethod);

            finalTrans.data.LocalLinks[delegateRef1] =
                finalTrans.data.LocalLinks[delegateRef2] =
                finalTrans.data.LocalLinks[delegateRef3] = delegateFormal;
            finalTrans.data.LocalLinks[iRef1] =
                finalTrans.data.LocalLinks[iRef2] = iDecl;
            finalTrans.data.LvalueTypes[delegateRef1] =
                finalTrans.data.LvalueTypes[delegateRef2] =
                finalTrans.data.LvalueTypes[delegateRef3] =
                finalTrans.data.ExpTypes[delegateRef1Exp] =
                finalTrans.data.ExpTypes[delegateRef2Exp] =
                finalTrans.data.ExpTypes[delegateRef3Exp] =
                finalTrans.data.ExpTypes[stringConst] =
                finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.LvalueTypes[iRef1] =
                finalTrans.data.LvalueTypes[iRef2] =
                finalTrans.data.ExpTypes[iRef1Exp] =
                finalTrans.data.ExpTypes[iRef2Exp] =
                finalTrans.data.ExpTypes[stringFindInvoke] =
                finalTrans.data.ExpTypes[intConst1] =
                finalTrans.data.ExpTypes[intConst2] =
                finalTrans.data.ExpTypes[intConst3] =
                finalTrans.data.ExpTypes[binop2] = new ANamedType(new TIdentifier("int"), null);
            finalTrans.data.ExpTypes[booleanConst] =
                finalTrans.data.ExpTypes[binop1] = new ANamedType(new TIdentifier("bool"), null);

            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return GetMethodPartMethod;
        }
Esempio n. 21
0
        private AMethodDecl GetStringPointerMethod()
        {
            if (GetStringPointerPartMethod != null)
                return GetStringPointerPartMethod;
            /*
             *  string GetPointerPart(string delegate)
             *  {
             *      return StringSub(delegate, StringFind(delegate, ":", false) + 1, StringLength(delegate));
             *  }
             */

            AASourceFile sourceFile = Util.GetAncestor<AASourceFile>(finalTrans.mainEntry);
            AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1 = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2 = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3 = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool());
            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList(){delegateRef2Exp, stringConst, booleanConst});
            AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop = new ABinopExp(stringFindInvoke, new APlusBinop(new TPlus("+")), intConst);

            ASimpleInvokeExp stringLengthInvoke = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList() { delegateRef3Exp });

            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList(){delegateRef1Exp, binop, stringLengthInvoke});

            GetStringPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                 new ANamedType(new TIdentifier("string"), null),
                                                 new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList() {delegateFormal},
                                                 new AABlock(
                                                     new ArrayList()
                                                         {new AValueReturnStm(new TReturn("return"), stringSubInvoke)},
                                                     new TRBrace("}")));
            sourceFile.GetDecl().Add(GetStringPointerPartMethod);
            data.Methods.Add(new SharedData.DeclItem<AMethodDecl>(sourceFile, GetStringPointerPartMethod));

            finalTrans.data.LocalLinks[delegateRef1] =
                finalTrans.data.LocalLinks[delegateRef2] =
                finalTrans.data.LocalLinks[delegateRef3] = delegateFormal;
            finalTrans.data.LvalueTypes[delegateRef1] =
                finalTrans.data.LvalueTypes[delegateRef2] =
                finalTrans.data.LvalueTypes[delegateRef3] =
                finalTrans.data.ExpTypes[delegateRef1Exp] =
                finalTrans.data.ExpTypes[delegateRef2Exp] =
                finalTrans.data.ExpTypes[delegateRef3Exp] =
                finalTrans.data.ExpTypes[stringConst] =
                finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.ExpTypes[booleanConst] = new ANamedType(new TIdentifier("bool"), null);
            finalTrans.data.ExpTypes[intConst] =
                finalTrans.data.ExpTypes[binop] =
                finalTrans.data.ExpTypes[stringFindInvoke] =
                finalTrans.data.ExpTypes[stringLengthInvoke] = new ANamedType(new TIdentifier("int"), null);

            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringLengthInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringLengthInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return GetStringPointerPartMethod;
        }
Esempio n. 22
0
 ArrayList New327()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     TIntegerLiteral tintegerliteralNode2 = (TIntegerLiteral)nodeArrayList1[0];
     AIntConstExp pexpNode1 = new AIntConstExp (
       tintegerliteralNode2
     );
     nodeList.Add(pexpNode1);
     return nodeList;
 }
Esempio n. 23
0
 public virtual void InAIntConstExp(AIntConstExp node)
 {
     DefaultIn(node);
 }
 public override void OutAIntConstExp(AIntConstExp node)
 {
     if (foldIntegerConstants)
     {
         integerConstant = int.Parse(node.GetIntegerLiteral().Text);
     }
 }
Esempio n. 25
0
 public virtual void OutAIntConstExp(AIntConstExp node)
 {
     DefaultOut(node);
 }
 public override void CaseAIntConstExp(AIntConstExp node)
 {
     Write(node.GetIntegerLiteral().Text);
 }
        public override void OutAAProgram(AAProgram node)
        {
            foreach (KeyValuePair<AArrayLengthLvalue, AArrayTempType> pair in data.ArrayLengthTypes)
            {
                AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral(pair.Value.GetIntDim().Text));
                data.ExpTypes[intConst] = new ANamedType(new TIdentifier("int"), null);
                ALvalueExp exp = Util.GetAncestor<ALvalueExp>(pair.Key);
                exp.ReplaceBy(intConst);
            }

            base.OutAAProgram(node);
        }
        public override void OutACastExp(ACastExp node)
        {
            string toType = ((AAName)((ANamedType) node.GetType()).GetName()).AsString();
            string fromType;
            PType fromPType = data.ExpTypes[node.GetExp()];
            AStructDecl toEnum = null;
            AStructDecl fromEnum = null;

            if (data.StructTypeLinks.ContainsKey((ANamedType)node.GetType()))
            {
                AStructDecl str = data.StructTypeLinks[(ANamedType)node.GetType()];
                if (data.Enums.ContainsKey(str))
                    toEnum = str;
            }
            if (fromPType is ANamedType)
            {
                fromType = ((AAName)((ANamedType)fromPType).GetName()).AsString();
                //Namespace ignored
                if (data.StructTypeLinks.ContainsKey((ANamedType) fromPType))
                {
                    AStructDecl str = data.StructTypeLinks[(ANamedType) fromPType];
                    if (data.Enums.ContainsKey(str))
                        fromEnum = str;
                }
            }
            else
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText121")));
                throw new ParserException(node.GetToken(), "Invalid cast");
            }

            if (toEnum != null && (fromType == "int" || fromType == "byte"))
            {
                ANamedType type = new ANamedType(new TIdentifier(toEnum.GetName().Text), null);
                data.StructTypeLinks[type] = toEnum;
                data.ExpTypes[node.GetExp()] = type;
                node.ReplaceBy(node.GetExp());
                return;
            }

            if (fromEnum != null && (toType == "int" || toType == "byte"))
            {
                int enumDefinitions = 0;
                foreach (PLocalDecl local in fromEnum.GetLocals())
                {
                    if (local is AALocalDecl)
                        enumDefinitions++;
                }
                string typeName = enumDefinitions > 255 ? "int" : "byte";
                ANamedType type = new ANamedType(new TIdentifier(typeName), null);
                data.ExpTypes[node.GetExp()] = new ANamedType(new TIdentifier(typeName), null);
                node.ReplaceBy(node.GetExp());
                return;
            }

            if (fromEnum != null && toType == "string")
            {
                AMethodDecl targetMethod = data.StructMethods[fromEnum][0];
                ASimpleInvokeExp invokeExp = new ASimpleInvokeExp(new TIdentifier("toString"), new ArrayList(){node.GetExp()});
                data.SimpleMethodLinks[invokeExp] = targetMethod;
                data.ExpTypes[invokeExp] = targetMethod.GetReturnType();
                node.ReplaceBy(invokeExp);
                return;
            }

            ASimpleInvokeExp replacementMethod = null;
            switch (toType)
            {
                case "string":
                    switch (fromType)
                    {
                        case "wave":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("AIWaveToString"), new ArrayList{node.GetExp()});
                            break;
                        case "fixed"://Implicit
                            AFieldLvalue precisionArg = new AFieldLvalue(new TIdentifier("c_fixedPrecisionAny"));
                            ALvalueExp exp = new ALvalueExp(precisionArg);
                            data.FieldLinks[precisionArg] =
                                data.Libraries.Fields.First(field => field.GetName().Text == precisionArg.GetName().Text);
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToString"), new ArrayList { node.GetExp(), exp});
                            break;
                        case "int"://Implicit
                        case "byte"://Implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList { node.GetExp()});
                            break;
                        case "bool"://Implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertBooleanToString"), new ArrayList { node.GetExp() });
                            break;
                        case "color"://Implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertColorToString"), new ArrayList { node.GetExp() });
                            break;
                    }
                    break;
                case "text":
                    switch (fromType)
                    {
                        case "wave":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("AIWaveToText"), new ArrayList { node.GetExp() });
                            break;
                        case "fixed"://Implicit
                            AFieldLvalue precisionArg = new AFieldLvalue(new TIdentifier("c_fixedPrecisionAny"));
                            ALvalueExp exp = new ALvalueExp(precisionArg);
                            data.FieldLinks[precisionArg] =
                                data.Libraries.Fields.First(field => field.GetName().Text == precisionArg.GetName().Text);
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToText"), new ArrayList { node.GetExp(), exp });
                            break;
                        case "int"://Implicit
                        case "byte":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToText"), new ArrayList { node.GetExp() });
                            break;
                        case "bool"://Implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("libNtve_gf_ConvertBooleanToText"), new ArrayList { node.GetExp() });
                            break;
                        case "string"://Implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList { node.GetExp() });
                            break;
                    }
                    break;
                case "int":
                    switch (fromType)
                    {
                        case "bool":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("BoolToInt"), new ArrayList {node.GetExp()});
                            break;
                        case "fixed":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("FixedToInt"), new ArrayList { node.GetExp() });
                            break;
                        case "string":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToInt"), new ArrayList { node.GetExp() });
                            break;
                    }
                    break;
                case "fixed":
                    switch (fromType)
                    {
                        case "int"://Already implicit
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("IntToFixed"), new ArrayList { node.GetExp() });
                            break;
                        case "string":
                            replacementMethod = new ASimpleInvokeExp(new TIdentifier("StringToFixed"), new ArrayList { node.GetExp() });
                            break;
                    }
                    break;
                case "bool":
                    switch (fromType)
                    {
                        case "int":
                        case "byte":
                        case "fixed":
                            //Replace by
                            //exp != 0
                            AIntConstExp zero = new AIntConstExp(new TIntegerLiteral("0"));
                            ABinopExp binop = new ABinopExp(node.GetExp(), new ANeBinop(new TNeq("!=")), zero);
                            node.ReplaceBy(binop);

                            binop.Apply(this);
                            return;
                    }
                    break;
            }

            if (replacementMethod == null)
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), currentSourceFile, LocRM.GetString("ErrorText122") + fromType + LocRM.GetString("ErrorText123") + toType));
                throw new ParserException(node.GetToken(), LocRM.GetString("ErrorText121"));
            }

            data.SimpleMethodLinks[replacementMethod] =
                data.Libraries.Methods.First(method => method.GetName().Text == replacementMethod.GetName().Text);
            data.ExpTypes[replacementMethod] = data.SimpleMethodLinks[replacementMethod].GetReturnType();
            node.ReplaceBy(replacementMethod);
            for (int i = 1; i < replacementMethod.GetArgs().Count; i++)
            {
                ((Node)replacementMethod.GetArgs()[i]).Apply(this);
            }
        }
        private AMethodDecl CreateStringDeobfuscator()
        {
            AASourceFile file = (AASourceFile) finalTrans.mainEntry.Parent();

            //Create fields for the string constants
            AStringConstExp emptyStringConst = new AStringConstExp(new TStringLiteral("\"\""));
            AFieldDecl emptyStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("fOobar"), emptyStringConst);
            file.GetDecl().Add(emptyStringField);
            data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, emptyStringField));
            AFieldLvalue emptyStringRef1 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            AFieldLvalue emptyStringRef2 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            AFieldLvalue emptyStringRef3 = new AFieldLvalue(new TIdentifier(emptyStringField.GetName().Text));
            ALvalueExp emptyStringRef1Exp = new ALvalueExp(emptyStringRef1);
            ALvalueExp emptyStringRef2Exp = new ALvalueExp(emptyStringRef2);
            ALvalueExp emptyStringRef3Exp = new ALvalueExp(emptyStringRef3);

            AStringConstExp colonStringConst = new AStringConstExp(new TStringLiteral("\":\""));
            AFieldDecl colonStringField = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const"),
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("foObar"), colonStringConst);
            file.GetDecl().Add(colonStringField);
            data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, colonStringField));
            AFieldLvalue colonStringRef = new AFieldLvalue(new TIdentifier(colonStringField.GetName().Text));
            ALvalueExp colonStringRefExp = new ALvalueExp(colonStringRef);

            /*
                string output = "";
                string ch;
                int length = StringLength(s);
                int phase1 = (length - 1)%3;
                int phase2 = (length - 1)%2;
             */

            AALocalDecl stringParam = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                      new ANamedType(new TIdentifier("string"), null),
                                                      new TIdentifier("fo0bar"), emptyStringRef1Exp);
            ALocalLvalue stringParamRef1 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef2 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef3 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef4 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef5 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef6 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef7 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALocalLvalue stringParamRef8 = new ALocalLvalue(new TIdentifier(stringParam.GetName().Text));
            ALvalueExp stringParamRef1Exp = new ALvalueExp(stringParamRef1);
            ALvalueExp stringParamRef2Exp = new ALvalueExp(stringParamRef2);
            ALvalueExp stringParamRef4Exp = new ALvalueExp(stringParamRef4);
            ALvalueExp stringParamRef5Exp = new ALvalueExp(stringParamRef5);
            ALvalueExp stringParamRef7Exp = new ALvalueExp(stringParamRef7);
            ALvalueExp stringParamRef8Exp = new ALvalueExp(stringParamRef8);

            AABlock methodBlock = new AABlock(new ArrayList(), new TRBrace("}"));

            AALocalDecl outputDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("string"), null),
                                                     new TIdentifier("foobar"), emptyStringRef1Exp);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), outputDecl));
            ALocalLvalue outputRef1 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef2 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef3 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef4 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef5 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef6 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef7 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef8 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef9 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef10 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef11 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef12 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALocalLvalue outputRef13 = new ALocalLvalue(new TIdentifier(outputDecl.GetName().Text));
            ALvalueExp outputRef2Exp = new ALvalueExp(outputRef2);
            ALvalueExp outputRef4Exp = new ALvalueExp(outputRef4);
            ALvalueExp outputRef5Exp = new ALvalueExp(outputRef5);
            ALvalueExp outputRef6Exp = new ALvalueExp(outputRef6);
            ALvalueExp outputRef7Exp = new ALvalueExp(outputRef7);
            ALvalueExp outputRef8Exp = new ALvalueExp(outputRef8);
            ALvalueExp outputRef10Exp = new ALvalueExp(outputRef10);
            ALvalueExp outputRef11Exp = new ALvalueExp(outputRef11);
            ALvalueExp outputRef12Exp = new ALvalueExp(outputRef12);
            ALvalueExp outputRef13Exp = new ALvalueExp(outputRef13);

            AALocalDecl chDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("string"), null),
                                                     new TIdentifier("f0obar"), null);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), chDecl));
            ALocalLvalue chRef1 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef2 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef3 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef4 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALocalLvalue chRef5 = new ALocalLvalue(new TIdentifier(chDecl.GetName().Text));
            ALvalueExp chRef3Exp = new ALvalueExp(chRef3);
            ALvalueExp chRef4Exp = new ALvalueExp(chRef4);
            ALvalueExp chRef5Exp = new ALvalueExp(chRef5);

            ASimpleInvokeExp stringLengthInvoke1 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { stringParamRef1Exp });
            AALocalDecl lengthDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("f0Obar"), stringLengthInvoke1);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), lengthDecl));
            ALocalLvalue lengthRef1 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef2 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef3 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef4 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef5 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef6 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef7 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALocalLvalue lengthRef8 = new ALocalLvalue(new TIdentifier(lengthDecl.GetName().Text));
            ALvalueExp lengthRef1Exp = new ALvalueExp(lengthRef1);
            ALvalueExp lengthRef2Exp = new ALvalueExp(lengthRef2);
            ALvalueExp lengthRef3Exp = new ALvalueExp(lengthRef3);
            ALvalueExp lengthRef4Exp = new ALvalueExp(lengthRef4);
            ALvalueExp lengthRef5Exp = new ALvalueExp(lengthRef5);
            ALvalueExp lengthRef6Exp = new ALvalueExp(lengthRef6);
            ALvalueExp lengthRef7Exp = new ALvalueExp(lengthRef7);

            AIntConstExp intConstp1Init1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConstp1Init2 = new AIntConstExp(new TIntegerLiteral("3"));
            ABinopExp binopExpP1InitMinus = new ABinopExp(lengthRef1Exp, new AMinusBinop(new TMinus("-")), intConstp1Init1);
            ABinopExp binopExpP1InitMod = new ABinopExp(binopExpP1InitMinus, new AModuloBinop(new TMod("%")), intConstp1Init2);

            AALocalDecl phase1Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("fO0bar"), binopExpP1InitMod);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase1Decl));
            ALocalLvalue phase1Ref1 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref2 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref3 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref4 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref5 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref6 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref7 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref8 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref9 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref10 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALocalLvalue phase1Ref11 = new ALocalLvalue(new TIdentifier(phase1Decl.GetName().Text));
            ALvalueExp phase1Ref1Exp = new ALvalueExp(phase1Ref1);
            ALvalueExp phase1Ref2Exp = new ALvalueExp(phase1Ref2);
            ALvalueExp phase1Ref4Exp = new ALvalueExp(phase1Ref4);
            ALvalueExp phase1Ref5Exp = new ALvalueExp(phase1Ref5);
            ALvalueExp phase1Ref7Exp = new ALvalueExp(phase1Ref7);
            ALvalueExp phase1Ref9Exp = new ALvalueExp(phase1Ref9);
            ALvalueExp phase1Ref10Exp = new ALvalueExp(phase1Ref10);
            ALvalueExp phase1Ref11Exp = new ALvalueExp(phase1Ref11);

            AIntConstExp intConstp2Init1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConstp2Init2 = new AIntConstExp(new TIntegerLiteral("2"));
            ABinopExp binopExpP2InitMinus = new ABinopExp(lengthRef2Exp, new AMinusBinop(new TMinus("-")), intConstp2Init1);
            ABinopExp binopExpP2InitMod = new ABinopExp(binopExpP2InitMinus, new AModuloBinop(new TMod("%")), intConstp2Init2);

            AALocalDecl phase2Decl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                     new ANamedType(new TIdentifier("int"), null),
                                                     new TIdentifier("carl"), binopExpP2InitMod);
            methodBlock.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), phase2Decl));
            ALocalLvalue phase2Ref1 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref2 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref3 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref4 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref5 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref6 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref7 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref8 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALocalLvalue phase2Ref9 = new ALocalLvalue(new TIdentifier(phase2Decl.GetName().Text));
            ALvalueExp phase2Ref1Exp = new ALvalueExp(phase2Ref1);
            ALvalueExp phase2Ref2Exp = new ALvalueExp(phase2Ref2);
            ALvalueExp phase2Ref4Exp = new ALvalueExp(phase2Ref4);
            ALvalueExp phase2Ref5Exp = new ALvalueExp(phase2Ref5);
            ALvalueExp phase2Ref7Exp = new ALvalueExp(phase2Ref7);
            ALvalueExp phase2Ref9Exp = new ALvalueExp(phase2Ref9);

            /*
                while(length > 0)
                {
                    if(phase2 == 0)
                    {
                        ch = StringSub(s, 1, 1);
                        s = StringReplace(s, "", 1, 1);
                    }
                    else
                    {
                        if(phase2 == 1)
                        {
                            ch = StringSub(s, length, length);
                            s = StringReplace(s, "", length, length);
                        }
                    }

                    if(phase1 == 0)
                    {
                        output = ch + output;
                    }
                    else
                    {
                        if(phase1 == 1)
                        {
                            output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output));
                        }
                        else
                        {
                            output = output + ch;
                        }
                    }
                    phase1 = phase1 - 1;
                    if(phase1 < 0)
                    {
                        phase1 = phase1 + 3;
                    }
                    phase2 = phase2 - 1;
                    if(phase2 < 0)
                    {
                        phase2 = phase2 + 2;
                    }
                    length = StringLength(s);
                }
             */

            AABlock whileBlock = new AABlock(new ArrayList(), new TRBrace("}"));
            AIntConstExp intConstWhileCond = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binopWhileCond = new ABinopExp(lengthRef3Exp, new AGtBinop(new TGt(">")), intConstWhileCond);
            methodBlock.GetStatements().Add(new AWhileStm(new TLParen("("), binopWhileCond,
                                                          new ABlockStm(new TLBrace("{"), whileBlock)));

            /*
                    if(phase2 == 0)
                    {
                        ch = StringSub(s, 1, 1);
                        s = StringReplace(s, "", 1, 1);
                    }
                    else
                    {
                        if(phase2 == 1)
                        {
                            ch = StringSub(s, length, length);
                            s = StringReplace(s, "", length, length);
                        }
                    }
             */
            AIntConstExp intConstIf1Cond = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binopIf1Cond = new ABinopExp(phase2Ref1Exp, new AEqBinop(new TEq("==")), intConstIf1Cond);
            AABlock thenBlock = new AABlock();
            AABlock elseBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binopIf1Cond,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //ch = StringSub(s, 1, 1);
            AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("1"));
            ASimpleInvokeExp invokeStringSub1 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() {stringParamRef2Exp, intConst1, intConst2});
            AAssignmentExp assignment1 = new AAssignmentExp(new TAssign("="), chRef1, invokeStringSub1);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment1));

            //s = StringReplace(s, "", 1, 1);
            AIntConstExp intConst3 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst4 = new AIntConstExp(new TIntegerLiteral("1"));
            ASimpleInvokeExp invokeStringReplace1 = new ASimpleInvokeExp(new TIdentifier("StringReplace"),
                                                           new ArrayList() { stringParamRef4Exp, emptyStringRef2Exp, intConst3, intConst4 });
            AAssignmentExp assignment2 = new AAssignmentExp(new TAssign("="), stringParamRef3, invokeStringReplace1);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment2));

            //if(phase2 == 1)
            AIntConstExp intConst5 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop1 = new ABinopExp(phase2Ref2Exp, new AEqBinop(new TEq("==")), intConst5);
            thenBlock = new AABlock();
            elseBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop1,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //ch = StringSub(s, length, length);
            ASimpleInvokeExp invokeStringSub2 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { stringParamRef5Exp, lengthRef3Exp, lengthRef4Exp });
            AAssignmentExp assignment3 = new AAssignmentExp(new TAssign("="), chRef2, invokeStringSub2);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment3));

            //s = StringReplace(s, "", length, length);
            ASimpleInvokeExp invokeStringReplace2 = new ASimpleInvokeExp(new TIdentifier("StringReplace"),
                                                           new ArrayList() { stringParamRef7Exp, emptyStringRef3Exp, lengthRef5Exp, lengthRef6Exp });
            AAssignmentExp assignment4 = new AAssignmentExp(new TAssign("="), stringParamRef6, invokeStringReplace2);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment4));

            //if(phase1 == 0)
            AIntConstExp intConst6 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop2 = new ABinopExp(phase1Ref1Exp, new AEqBinop(new TEq("==")), intConst6);
            thenBlock = new AABlock();
            elseBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop2,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //output = ch + output;
            ABinopExp binop3 = new ABinopExp(chRef3Exp, new APlusBinop(new TPlus("+")), outputRef2Exp);
            AAssignmentExp assignment5 = new AAssignmentExp(new TAssign("="), outputRef1, binop3);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment5));

            //if(phase1 == 1)
            AABlock cBlock = elseBlock;
            AIntConstExp intConst7 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop4 = new ABinopExp(phase1Ref2Exp, new AEqBinop(new TEq("==")), intConst7);
            thenBlock = new AABlock();
            elseBlock = new AABlock();
            cBlock.GetStatements().Add(new AIfThenElseStm(new TLParen("("), binop4,
                                                              new ABlockStm(new TLBrace("{"), thenBlock),
                                                              new ABlockStm(new TLBrace("{"), elseBlock)));

            //output = StringSub(output, 1, (StringLength(output) + 1)/2) + ch + StringSub(output, (StringLength(output) + 1)/2 + 1, StringLength(output));
            AIntConstExp intConst8 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst9 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst10 = new AIntConstExp(new TIntegerLiteral("2"));
            AIntConstExp intConst11 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst12 = new AIntConstExp(new TIntegerLiteral("2"));
            AIntConstExp intConst13 = new AIntConstExp(new TIntegerLiteral("1"));

            ASimpleInvokeExp invokeStringLength1 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() {outputRef5Exp});
            ABinopExp binop5 = new ABinopExp(invokeStringLength1, new APlusBinop(new TPlus("+")), intConst9);
            ABinopExp binop6 = new ABinopExp(binop5, new ADivideBinop(new TDiv("/")), intConst10);

            ASimpleInvokeExp invokeStringSub3 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef4Exp, intConst8, binop6});

            ABinopExp binop7 = new ABinopExp(invokeStringSub3, new APlusBinop(new TPlus("+")), chRef4Exp);

            ASimpleInvokeExp invokeStringLength2 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { outputRef7Exp });
            ABinopExp binop8 = new ABinopExp(invokeStringLength2, new APlusBinop(new TPlus("+")), intConst11);
            ABinopExp binop9 = new ABinopExp(binop8, new ADivideBinop(new TDiv("/")), intConst12);
            ABinopExp binop10 = new ABinopExp(binop9, new APlusBinop(new TPlus("+")), intConst13);

            ASimpleInvokeExp invokeStringLength3 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { outputRef8Exp });

            ASimpleInvokeExp invokeStringSub4 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef6Exp, binop10, invokeStringLength3 });

            ABinopExp binop11 = new ABinopExp(binop7, new APlusBinop(new TPlus("+")), invokeStringSub4);

            AAssignmentExp assignment6 = new AAssignmentExp(new TAssign("="), outputRef3, binop11);

            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment6));

            //output = output + ch;
            ABinopExp binop12 = new ABinopExp(outputRef10Exp, new APlusBinop(new TPlus("+")), chRef5Exp);
            AAssignmentExp assignment7 = new AAssignmentExp(new TAssign("="), outputRef9, binop12);
            elseBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment7));

            //phase1 = phase1 - 1;
            AIntConstExp intConst14 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop13 = new ABinopExp(phase1Ref4Exp, new AMinusBinop(new TMinus("-")), intConst14);
            AAssignmentExp assignment8 = new AAssignmentExp(new TAssign("="), phase1Ref3, binop13);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment8));

            //if(phase1 < 0)
            AIntConstExp intConst15 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop14 = new ABinopExp(phase1Ref5Exp, new ALtBinop(new TLt("<")), intConst15);
            thenBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop14,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //phase1 = phase1 + 3;
            AIntConstExp intConst16 = new AIntConstExp(new TIntegerLiteral("3"));
            ABinopExp binop15 = new ABinopExp(phase1Ref7Exp, new APlusBinop(new TPlus("+")), intConst16);
            AAssignmentExp assignment9 = new AAssignmentExp(new TAssign("="), phase1Ref6, binop15);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment9));

            //phase2 = phase2 - 1;
            AIntConstExp intConst17 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop16 = new ABinopExp(phase2Ref4Exp, new AMinusBinop(new TMinus("-")), intConst17);
            AAssignmentExp assignment10 = new AAssignmentExp(new TAssign("="), phase2Ref3, binop16);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment10));

            //if(phase2 < 0)
            AIntConstExp intConst18 = new AIntConstExp(new TIntegerLiteral("0"));
            ABinopExp binop17 = new ABinopExp(phase2Ref5Exp, new ALtBinop(new TLt("<")), intConst18);
            thenBlock = new AABlock();
            whileBlock.GetStatements().Add(new AIfThenStm(new TLParen("("), binop17,
                                                         new ABlockStm(new TLBrace("{"), thenBlock)));

            //phase2 = phase2 + 2;
            AIntConstExp intConst19 = new AIntConstExp(new TIntegerLiteral("2"));
            ABinopExp binop18 = new ABinopExp(phase2Ref7Exp, new APlusBinop(new TPlus("+")), intConst19);
            AAssignmentExp assignment11 = new AAssignmentExp(new TAssign("="), phase2Ref6, binop18);
            thenBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment11));

            //length = StringLength(s);
            ASimpleInvokeExp invokeStringLength4 = new ASimpleInvokeExp(new TIdentifier("StringLength"),
                                                                        new ArrayList() { stringParamRef8Exp });
            AAssignmentExp assignment12 = new AAssignmentExp(new TAssign("="), lengthRef8, invokeStringLength4);
            whileBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment12));

            /*
                phase1 = StringFind(output, ":", false);
                phase2 = StringToInt(StringSub(output, 1, phase1 - 1));
                return StringSub(output, phase1 + 1, phase2 + phase1);
             */

            ABooleanConstExp boolConst1 = new ABooleanConstExp(new AFalseBool());
            ASimpleInvokeExp invokeStringFind = new ASimpleInvokeExp(new TIdentifier("StringFind"),
                                                                     new ArrayList()
                                                                         {outputRef11Exp, colonStringRefExp, boolConst1});
            AAssignmentExp assignment13 = new AAssignmentExp(new TAssign("="), phase1Ref8, invokeStringFind);
            methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment13));

            //phase2 = StringToInt(StringSub(output, 1, phase1 - 1));
            AIntConstExp intConst20 = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp intConst21 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop19 = new ABinopExp(phase1Ref9Exp, new AMinusBinop(new TMinus("-")), intConst21);
            ASimpleInvokeExp invokeStringSub5 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef12Exp, intConst20, binop19});
            ASimpleInvokeExp invokeStringToInt = new ASimpleInvokeExp(new TIdentifier("StringToInt"),
                                                                      new ArrayList() { invokeStringSub5 });
            AAssignmentExp assignment14 = new AAssignmentExp(new TAssign("="), phase2Ref8, invokeStringToInt);
            methodBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment14));

            //return StringSub(output, phase1 + 1, phase2 + phase1);
            AIntConstExp intConst22 = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp binop20 = new ABinopExp(phase1Ref10Exp, new APlusBinop(new TPlus("+")), intConst22);
            ABinopExp binop21 = new ABinopExp(phase2Ref9Exp, new APlusBinop(new TPlus("+")), phase1Ref11Exp);
            ASimpleInvokeExp invokeStringSub6 = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                           new ArrayList() { outputRef12Exp, binop20, binop21 });
            methodBlock.GetStatements().Add(new AValueReturnStm(new TReturn("return"), invokeStringSub6));

            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                 new ANamedType(new TIdentifier("string"), null),
                                                 new TIdentifier("Galaxypp_Deobfuscate"), new ArrayList() {stringParam},
                                                 methodBlock);

            //Fix data refferences.. I got tired here.

            return method;
        }
Esempio n. 30
0
            public override void CaseAAssignmentExp(AAssignmentExp node)
            {
                if (node.GetExp() is ANewExp)
                {
                    node.GetExp().Apply(this);
                }

                bool rightIsDynamic = false;
                PExp rightString = null;
                bool rightWasSet = assignmentRightSideSet;
                assignmentRightSideSet = false;
                if (rightWasSet)
                {
                    rightIsDynamic = hadPointer;
                    rightString = nameExp;
                }

                hadPointer = false;
                nameExp = null;
                node.GetLvalue().Apply(this);
                bool leftIsDynamic = hadPointer;
                PExp leftString = nameExp;

                if (!rightWasSet)
                {
                    hadPointer = false;
                    nameExp = null;
                    node.GetExp().Apply(this);
                    rightIsDynamic = hadPointer;
                    rightString = nameExp;
                }

                PType type = data.ExpTypes[node];
                if (node.GetExp() is ANullExp &&
                            type is APointerType &&
                            Util.IsIntPointer(node, ((APointerType)type).GetType(), data))
                {
                    AIntConstExp replacer = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[replacer] = new ANamedType(new TIdentifier("int"), null);
                    node.GetExp().ReplaceBy(replacer);
                }
                /* Cases to handle
                 *
                 * left dynamic, right dynamic, type simple / left dynamic, right normal, type simple
                 * left dynamic, right dynamic, type bulk
                 * left dynamic, right simple, type bulk
                 * left simple, right dynamic, type bulk
                 *
                 *
                 */
                /*if (!Util.IsBulkCopy(type))
                {//left dynamic, right dynamic, type simple / left dynamic, right normal, type simple
                    if (leftIsDynamic)
                    {
                        string typeString = "string";
                        if (type is ANamedType)
                        {
                            typeString = ((ANamedType) type).GetName().Text;
                        }
                        PExp exp = CreateDynaicSetStm(typeString, leftString, node.GetExp());

                        node.ReplaceBy(exp);
                    }
                }
                else*/
                {
                    //throw new ParserException(null, "Not implemented");
                    if (type is ANamedType)
                    {

                    }
                    else if (type is AArrayTempType || type is ADynamicArrayType)
                    {

                    }
                    PStm pStm = Util.GetAncestor<PStm>(node);
                    AABlock pBlock = (AABlock) pStm.Parent();
                    int index = pBlock.GetStatements().IndexOf(pStm) + 1;
                    if (leftIsDynamic)
                    {

                        if (rightIsDynamic)
                        {//left dynamic, right dynamic, type bulk
                            MakeAssignmentBothDynamic(leftString, rightString, type, pBlock, ref index);
                            pStm.Parent().RemoveChild(pStm);
                        }
                        else
                        {//left dynamic, right simple, type bulk
                            MakeAssignmentLeftDynamic(leftString, node.GetExp(), type, pBlock, ref index);
                            pStm.Parent().RemoveChild(pStm);
                        }
                    }
                    else if (rightIsDynamic)
                    {//left simple, right dynamic, type bulk
                        MakeAssignmentRightDynamic(node.GetLvalue(), rightString, type, pBlock, ref index);
                        pStm.Parent().RemoveChild(pStm);
                    }
                }
            }