Beispiel #1
0
        internal bool translate(DirectTranslation translation)
        {
            Symbol main = globalTable.LocalSearch("main");

            if (main is null)
            {
                Console.Error.WriteLine("No main function");
                return(false);
            }
            foreach (var item in globalTable.LocalScope)
            {
                if (!item.Value.IsFunction) // global variable
                {
                }
            }
            main.FunctionDefinition.Generate(translation);
            globalTable.LocalScope.Remove("main");
            foreach (var item in globalTable.LocalScope)
            {
                if (item.Value.IsFunction)
                {
                    item.Value.FunctionDefinition.Generate(translation);
                }
            }
            return(true);
        }
Beispiel #2
0
        internal override string Generate(DirectTranslation translation)
        {
            string tempVar = ".S" + VariableCounter;

            LiteralHelper(translation, tempVar, i);
            return(tempVar);
        }
Beispiel #3
0
 internal override string Generate(DirectTranslation translation)
 {
     translation.AddFunctionLabel(functionName, this);
     translation.AddPrologue(this);
     statementList.Generate(translation);
     return(null);
 }
Beispiel #4
0
        internal override string Generate(DirectTranslation translation)
        {
            string tempVar = ".S" + VariableCounter;
            int    i       = BitConverter.SingleToInt32Bits(f);

            LiteralHelper(translation, tempVar, i);
            return(tempVar);
        }
Beispiel #5
0
 internal override string Generate(DirectTranslation trans)
 {
     foreach (var item in list)
     {
         item.Generate(trans);
     }
     return(null);
 }
Beispiel #6
0
        protected static void LiteralHelper(DirectTranslation translation, string tempScalar, int i)
        {
            ushort high = (ushort)(i >> 16);
            ushort low  = (ushort)i;

            if (i == 0)
            {
                translation.AddAssembly("s_setzero", tempScalar);
            }
            else
            {
                translation.AddAssembly("s_write_low", tempScalar, (low).ToString());
                translation.AddAssembly("s_write_high", tempScalar, (high).ToString());
            }
        }
Beispiel #7
0
        internal static string ItoFHelper(DirectTranslation translation, string temp)
        {
            string tempF;

            if (temp[2] == '.') // temp is not temporary
            {
                tempF = $".{temp[1]}{VariableCounter}";
            }
            else
            {
                tempF = temp;
            }
            translation.AddAssembly("s_itof", tempF, temp);
            return(tempF);
        }
Beispiel #8
0
        internal override string Generate(DirectTranslation translation)
        {
            switch (type)
            {
            case Type.BREAK:
                translation.AddBranch("jmp", CurrentLoopEndLabel);
                break;

            case Type.CONTINUE:
                translation.AddBranch("jmp", CurrentLoopBeginLabel);
                break;

            default:
                break;
            }
            return(null);
        }
Beispiel #9
0
        internal override string Generate(DirectTranslation translation)
        {
            string tempScalar = ".S" + VariableCounter;
            string tempVar    = ".V" + VariableCounter;
            int    i;

            i = BitConverter.SingleToInt32Bits(vector.X);
            LiteralHelper(translation, tempScalar, i);
            translation.AddAssembly("v_get_from_s_d", tempVar, tempScalar, "0");
            i = BitConverter.SingleToInt32Bits(vector.Y);
            LiteralHelper(translation, tempScalar, i);
            translation.AddAssembly("v_get_from_s", tempVar, tempScalar, "1");
            i = BitConverter.SingleToInt32Bits(vector.Z);
            LiteralHelper(translation, tempScalar, i);
            translation.AddAssembly("v_get_from_s", tempVar, tempScalar, "2");
            i = BitConverter.SingleToInt32Bits(vector.W);
            LiteralHelper(translation, tempScalar, i);
            translation.AddAssembly("v_get_from_s", tempVar, tempScalar, "3");
            return(tempVar);
        }
Beispiel #10
0
 internal override string Generate(DirectTranslation translation)
 {
     if (arraySize <= 0)
     {
         if (initializer is not null)
         {
             string tempVar = initializer.Generate(translation);
             if (type == Type.VECTOR)
             {
                 translation.AddAssembly("v_mov", scopedIdentifier, tempVar);
             }
             else
             {
                 translation.AddAssembly("s_mov", scopedIdentifier, tempVar);
             }
         }
     }
     else // array
     {
         translation.AddAssembly("ii_addi", scopedIdentifier, "RS28", offset.ToString());
     }
     return(null);
 }
Beispiel #11
0
        internal override string Generate(DirectTranslation translation)
        {
            string id = LinkedSymbol.Identifier;

            if (id[0] == '.')
            {
                return(id); // scoped identifier
            }
            else
            {
                string tempScalar = $".S{VariableCounter}";
                if (id[1] == 'V')
                {
                    string tempVar     = $".V{VariableCounter}";
                    string tempScalar2 = $".S{VariableCounter}";
                    translation.AddAssembly("s_write_low", tempScalar, "0");
                    translation.AddAssembly("s_write_high", tempScalar, "8192");
                    translation.AddAssembly("s_load_4byte", tempScalar2, tempScalar, LinkedSymbol.Offset.ToString());
                    translation.AddAssembly("v_get_from_s_d", tempVar, tempScalar2, "0");
                    translation.AddAssembly("s_load_4byte", tempScalar2, tempScalar, (LinkedSymbol.Offset + 4).ToString());
                    translation.AddAssembly("v_get_from_s", tempVar, tempScalar2, "1");
                    translation.AddAssembly("s_load_4byte", tempScalar2, tempScalar, (LinkedSymbol.Offset + 8).ToString());
                    translation.AddAssembly("v_get_from_s", tempVar, tempScalar2, "2");
                    translation.AddAssembly("s_load_4byte", tempScalar2, tempScalar, (LinkedSymbol.Offset + 12).ToString());
                    translation.AddAssembly("v_get_from_s", tempVar, tempScalar2, "3");
                    // translation.AddAssembly("v_load_16byte", tempVar, tempScalar, LinkedSymbol.Offset.ToString());
                    return(tempVar);
                }
                else
                {
                    translation.AddAssembly("s_write_low", tempScalar, "0");
                    translation.AddAssembly("s_write_high", tempScalar, "8192");
                    translation.AddAssembly("s_load_4byte", tempScalar, tempScalar, LinkedSymbol.Offset.ToString());
                }
                return(tempScalar);
            }
        }
Beispiel #12
0
        internal override string Generate(DirectTranslation translation)
        {
            string begin     = "L" + LabelCounter;
            string end       = "L" + LabelCounter;
            string realBegin = "L" + LabelCounter;

            NewLoop(begin, end);
            if (initialStatement is not null)
            {
                initialStatement.Generate(translation);
            }
            conditionHelper(translation, end, realBegin, true);
            translation.AddLabel(realBegin);
            loopBody.Generate(translation);
            translation.AddLabel(begin);
            if (iterateStatement is not null)
            {
                iterateStatement.Generate(translation);
            }
            conditionHelper(translation, end, realBegin, false);
            translation.AddLabel(end);
            EndLoop();
            return(null);
        }
Beispiel #13
0
        internal override string Generate(DirectTranslation translation)
        {
            string tempVar = ".V" + VariableCounter;
            string tempScalar;

            for (int i = 0; i < 4; i++)
            {
                tempScalar = expressions[i].Generate(translation);
                if (types[i] == Type.INT)
                {
                    tempScalar = BinaryExpression.ItoFHelper(translation, tempScalar);
                }
                // translation.AddAssembly("s_itof", tempScalar, tempScalar);
                if (i == 0)
                {
                    translation.AddAssembly("v_get_from_s_d", tempVar, tempScalar, i.ToString());
                }
                else
                {
                    translation.AddAssembly("v_get_from_s", tempVar, tempScalar, i.ToString());
                }
            }
            return(tempVar);
        }
Beispiel #14
0
 internal override string Generate(DirectTranslation translation)
 {
     if (function.functionName == "main")
     {
         translation.AddAssembly("Fin");
         return(null);
     }
     if (expression is not null)
     {
         string tempVar = expression.Generate(translation);
         if (function.returnType == Type.VECTOR)
         {
             translation.AddAssembly("v_mov", "RV1", tempVar);
         }
         else
         {
             translation.AddAssembly("s_mov", "RS1", tempVar);
         }
     }
     translation.AddEpilogue(function);
     translation.AddReturn("ret", function.returnType);
     // translation.List[^1].returnType = function.returnType;
     return(null);
 }
Beispiel #15
0
 internal override string Generate(DirectTranslation list)
 {
     return(statementList.Generate(list));
 }
Beispiel #16
0
 internal override string Generate(DirectTranslation list)
 {
     return(declarationList.Generate(list));
 }
Beispiel #17
0
        internal override string Generate(DirectTranslation translation)
        {
            string trueLabel = "L" + LabelCounter;
            string skipLabel = "L" + LabelCounter;

            if (condition is BinaryExpression)
            {
                BinaryExpression be = condition as BinaryExpression;
                if (be.type < BinaryExpression.Type.EQ || be.type > BinaryExpression.Type.LE)
                {
                    string cond = condition.Generate(translation);
                    translation.AddAssembly("cmp_i", cond, "RS0");
                    translation.AddBranch("bne", trueLabel);
                }
                else
                {
                    string temp1 = be.exp1.Generate(translation);
                    string temp2 = be.exp2.Generate(translation);
                    if (be.type1 == Type.INT && be.type2 == Type.INT)
                    {
                        translation.AddAssembly("cmp_i", temp1, temp2);
                    }
                    else
                    {
                        if (be.type1 == Type.INT)
                        {
                            temp1 = BinaryExpression.ItoFHelper(translation, temp1);
                        }
                        // translation.AddAssembly("s_itof", temp1, temp1);
                        if (be.type2 == Type.INT)
                        {
                            temp2 = BinaryExpression.ItoFHelper(translation, temp2);
                        }
                        // translation.AddAssembly("s_itof", temp2, temp2);
                        translation.AddAssembly("cmp_f", temp1, temp2);
                    }
                    switch (be.type)
                    {
                    case BinaryExpression.Type.EQ:
                        translation.AddBranch("be", trueLabel);
                        break;

                    case BinaryExpression.Type.NE:
                        translation.AddBranch("bne", trueLabel);
                        break;

                    case BinaryExpression.Type.GT:
                        translation.AddBranch("bg", trueLabel);
                        break;

                    case BinaryExpression.Type.GE:
                        translation.AddBranch("bge", trueLabel);
                        break;

                    case BinaryExpression.Type.LT:
                        translation.AddBranch("bl", trueLabel);
                        break;

                    case BinaryExpression.Type.LE:
                        translation.AddBranch("ble", trueLabel);
                        break;
                    }
                }
            }
            else
            {
                string cond = condition.Generate(translation);
                translation.AddAssembly("cmp_i", cond, "RS0");
                translation.AddBranch("bne", trueLabel);
            }
            if (elseStatement is not null)
            {
                elseStatement.Generate(translation);
            }
            translation.AddBranch("jmp", skipLabel);
            translation.AddLabel(trueLabel);
            statement.Generate(translation);
            translation.AddLabel(skipLabel);
            return(null);
        }
Beispiel #18
0
        private void conditionHelper(DirectTranslation translation, string end, string realBegin, bool fisrt)
        {
            if (condition is null)
            {
                if (!fisrt)
                {
                    translation.AddBranch("jmp", realBegin);
                }
            }
            else
            {
                if (condition is BinaryExpression)
                {
                    BinaryExpression be = condition as BinaryExpression;
                    if (be.type < BinaryExpression.Type.EQ || be.type > BinaryExpression.Type.LE)
                    {
                        string cond = condition.Generate(translation);
                        translation.AddAssembly("cmp_i", cond, "RS0");
                        if (fisrt)
                        {
                            translation.AddBranch("be", end);
                        }
                        else
                        {
                            translation.AddBranch("bne", realBegin);
                        }
                    }
                    else
                    {
                        string temp1 = be.exp1.Generate(translation);
                        string temp2 = be.exp2.Generate(translation);
                        if (be.type1 == Type.INT && be.type2 == Type.INT)
                        {
                            translation.AddAssembly("cmp_i", temp1, temp2);
                        }
                        else
                        {
                            if (be.type1 == Type.INT)
                            {
                                temp1 = BinaryExpression.ItoFHelper(translation, temp1);
                            }
                            // translation.AddAssembly("s_itof", temp1, temp1);
                            if (be.type2 == Type.INT)
                            {
                                temp2 = BinaryExpression.ItoFHelper(translation, temp2);
                            }
                            // translation.AddAssembly("s_itof", temp2, temp2);
                            translation.AddAssembly("cmp_f", temp1, temp2);
                        }
                        switch (be.type)
                        {
                        case BinaryExpression.Type.EQ:
                            if (fisrt)
                            {
                                translation.AddBranch("bne", end);
                            }
                            else
                            {
                                translation.AddBranch("be", realBegin);
                            }
                            break;

                        case BinaryExpression.Type.NE:
                            if (fisrt)
                            {
                                translation.AddBranch("be", end);
                            }
                            else
                            {
                                translation.AddBranch("bne", realBegin);
                            }
                            break;

                        case BinaryExpression.Type.GT:
                            if (fisrt)
                            {
                                translation.AddBranch("ble", end);
                            }
                            else
                            {
                                translation.AddBranch("bg", realBegin);
                            }
                            break;

                        case BinaryExpression.Type.GE:
                            if (fisrt)
                            {
                                translation.AddBranch("bl", end);
                            }
                            else
                            {
                                translation.AddBranch("bge", realBegin);
                            }
                            break;

                        case BinaryExpression.Type.LT:
                            if (fisrt)
                            {
                                translation.AddBranch("bge", end);
                            }
                            else
                            {
                                translation.AddBranch("bl", realBegin);
                            }
                            break;

                        case BinaryExpression.Type.LE:
                            if (fisrt)
                            {
                                translation.AddBranch("bg", end);
                            }
                            else
                            {
                                translation.AddBranch("ble", realBegin);
                            }
                            break;
                        }
                    }
                }
                else
                {
                    string cond = condition.Generate(translation);
                    translation.AddAssembly("cmp_i", cond, "RS0");
                    if (fisrt)
                    {
                        translation.AddBranch("be", end);
                    }
                    else
                    {
                        translation.AddBranch("bne", realBegin);
                    }
                }
            }
        }
Beispiel #19
0
        internal override string Generate(DirectTranslation translation)
        {
            string rhsVar = right.Generate(translation);

            if (left is IdentifierExpression)
            {
                string id = (left as IdentifierExpression).LinkedSymbol.Identifier;
                // if (id[0] != '.')
                // {
                //     Symbol s = (left as IdentifierExpression).LinkedSymbol;
                //     string tempScalar = $".S{VariableCounter}";
                //     translation.AddAssembly("s_write_low", tempScalar, "0");
                //     translation.AddAssembly("s_write_high", tempScalar, "8192");
                //     if (leftType == Type.VECTOR)
                //         translation.AddAssembly("v_store_16byte", rhsVar, tempScalar, s.Offset.ToString());
                //     else
                //         translation.AddAssembly("s_store_4byte", rhsVar, tempScalar, s.Offset.ToString());
                // }
                // else
                // {
                if (leftType == Type.VECTOR)
                {
                    translation.AddAssembly("v_mov", id, rhsVar);
                }
                else
                {
                    translation.AddAssembly("s_mov", id, rhsVar);
                }
                // }
            }
            else
            {
                IndexExpression ie = (left as IndexExpression);
                string          pointer = ie.expression.Generate(translation);
                string          index = ie.indexExpression.Generate(translation);
                string          indexVar, pointerVar;
                if (index[2] == '.') // index is not temporary
                {
                    indexVar = $".{index[1]}{VariableCounter}";
                }
                else
                {
                    indexVar = index;
                }
                if (pointer[2] == '.') // index is not temporary
                {
                    pointerVar = $".{pointer[1]}{VariableCounter}";
                }
                else
                {
                    pointerVar = pointer;
                }
                if (ie.valueType == Type.VECTOR)
                {
                    translation.AddAssembly("v_get_from_s", pointer, rhsVar,
                                            (ie.indexExpression as IntLiteralExpression).i.ToString());
                }
                else if (ie.valueType == Type.VECTOR_POINTER)
                {
                    translation.AddAssembly("ii_muli", indexVar, index, "16");
                    translation.AddAssembly("ii_add", pointerVar, indexVar, pointer);
                    translation.AddAssembly("v_store_16byte", rhsVar, pointerVar, "0");
                }
                else
                {
                    translation.AddAssembly("ii_muli", indexVar, index, "4");
                    translation.AddAssembly("ii_add", pointerVar, indexVar, pointer);
                    translation.AddAssembly("s_store_4byte", rhsVar, pointerVar, "0");
                }
            }
            return(null);
        }
Beispiel #20
0
 internal override string Generate(DirectTranslation translation)
 {
     return(expression.Generate(translation));
 }
Beispiel #21
0
 abstract internal string Generate(DirectTranslation translation);