示例#1
0
        public void Generate(List<FourExp> fourExpList, VarTable varTable, List<AssemblerIns> cmdList)
        {
            //为变量分配内存,并对符号的活跃信息域进行初始化
            List<string> varNameList = varTable.GetNames();
            initVarTable(varTable, fourExpList, varNameList);

            //生成数据段
            genDataIns(varNameList, varTable, cmdList);

            int count = 0;

            foreach (FourExp f in fourExpList)
            {
                foreach (string varName in varNameList)
                {
                    //从符号表的后续引用信息域和活跃域中去除无用信息
                    if (varTable.GetPeekRefeInfo(varName) == count)
                    {
                        //varTable.PopRefeInfo(varName);
                        varTable.PopActInfo(varName);
                    }
                    count++;
                }
                convert(f, varTable, cmdList);
                optimize();
            }
        }
示例#2
0
 /// <summary>
 /// 遍历Ast树,生成四元式列表
 /// </summary>
 /// <param name="ast">Ast树</param>
 /// <param name="varTable">变量表</param>
 /// <returns></returns>
 public static List<FourExp> Translate(Ast ast, VarTable varTable)
 {
     List<FourExp> fourExpList = new List<FourExp>();
     LabelStack labelStack = new LabelStack();
     foreach (Statement s in ast.Statements)
     {
         s.Translate(varTable, labelStack, fourExpList);
     }
     return fourExpList;
 }
示例#3
0
 /// <summary>
 /// 跳转之后的调整
 /// </summary>
 /// <param name="f">四元式</param>
 /// <param name="reg1">寄存器1</param>
 /// <param name="reg2">寄存器2</param>
 /// <param name="varTable">变量表</param>
 /// <param name="cmdList">汇编指令列表</param>
 private void adjustAfterJump(FourExp f, string reg1, string reg2, VarTable varTable, List<AssemblerIns> cmdList)
 {
     if (varTable.GetPeekActInfo(f.Arg1) == false)
     {
         cmdList.Add(AssemblerFac.GenSW(reg1, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
         varTable.SetAddrInfo(f.Arg1, "");
         regUseTable.GetContent(reg1).Remove(f.Arg1);
     }
     if (varTable.GetPeekActInfo(f.Arg2) == false)
     {
         cmdList.Add(AssemblerFac.GenSW(reg2, varTable.GetAddr(f.Arg2).ToString(), "$ZERO"));
         varTable.SetAddrInfo(f.Arg2, "");
         regUseTable.GetContent(reg2).Remove(f.Arg2);
     }
 }
示例#4
0
 public override string GetValue(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     return "";
 }
示例#5
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     if (statement1 == null)
     {
         string label = labelStack.NewLabel();
         string condition = expression.GetValue(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJe(condition, 0+"", label));
         statement1.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenLabel(label));
     }
     else
     {
         string label1 = labelStack.NewLabel();
         string label2 = labelStack.NewLabel();
         string condition = expression.GetValue(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJe(condition, 0 + "", label1));
         statement1.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenJmp(label2));
         fourExpList.Add(FourExpFac.GenLabel(label1));
         statement2.Translate(varTable, labelStack, fourExpList);
         fourExpList.Add(FourExpFac.GenLabel(label2));
     }
 }
示例#6
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     int value = 0;
     varTable.Add(this.name, this.type, value);
 }
示例#7
0
 public virtual string GetValue(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     return "";
 }
示例#8
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     string label = labelStack.NewLabel();
     fourExpList.Add(FourExpFac.GenLabel(label));
     statement.Translate(varTable, labelStack, fourExpList);
     string condition = expression.GetValue(varTable, labelStack, fourExpList);
     fourExpList.Add(FourExpFac.GenJne(condition, 0 + "", label));
 }
示例#9
0
 /// <summary>
 /// 调整变量表和寄存器表中的相关域
 /// </summary>
 /// <param name="regName">计算器名</param>
 /// <param name="varTable">变量表</param>
 /// <param name="cmdList">汇编指令列表</param>
 private void doAdjust(string regName, VarTable varTable, List<AssemblerIns> cmdList)
 {
     foreach (string varName in regUseTable.GetContent(regName))
     {
         cmdList.Add(AssemblerFac.GenSW(regName, varTable.GetAddr(varName).ToString(), "$ZERO"));
         varTable.SetAddrInfo(varName, "");
     }
     regUseTable.Clear(regName);
 }
示例#10
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
 }
示例#11
0
        /// <summary>
        /// 将四元式翻译为汇编指令
        /// </summary>
        /// <param name="f">四元式</param>
        /// <param name="varTable">变量表</param>
        /// <param name="cmdList">汇编指令列表</param>
        private void convert(FourExp f, VarTable varTable, List<AssemblerIns> cmdList)
        {
            #region Label FourExp
            if (f.Op == FourExpOperation.label)
            {
                cmdList.Add(AssemblerFac.GenLABEL(f.LabelName));
            }
            #endregion

            #region Jump Operation
            if (f.Op >= FourExpOperation.jmp && f.Op <= FourExpOperation.jle)
            {
                string operation = "";
                switch (f.Op)
                {
                    case FourExpOperation.jmp:
                        operation = Mnemonic.J.ToString();
                        cmdList.Add(AssemblerFac.GenJ(f.TargetLabel));
                        break;
                    case FourExpOperation.je:
                        operation = Mnemonic.BEQ.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    case FourExpOperation.jne:
                        operation = Mnemonic.BNE.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    case FourExpOperation.jg:
                        operation = Mnemonic.BGTZ.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    case FourExpOperation.jge:
                        operation = Mnemonic.BGEZ.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    case FourExpOperation.jl:
                        operation = Mnemonic.BLTZ.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    case FourExpOperation.jle:
                        operation = Mnemonic.BLEZ.ToString();
                        doJump(f, operation, varTable, cmdList);
                        break;
                    default:
                        //错误处理
                        break;
                }

            }
            #endregion

            #region Move Operation
            else if (f.Op == FourExpOperation.mov)
            {
                string regB = "";
                if (isNumber(f.Arg1))
                {
                    regB = getReg(f, varTable, false, cmdList);
                    int value = Convert.ToInt32(f.Arg1);
                    if (value <= 32767 && value >= -32768)
                    {
                        cmdList.Add(AssemblerFac.GenLUI(regB, value.ToString()));
                        cmdList.Add(AssemblerFac.GenSRL(regB, regB, 16 + ""));
                    }
                    else
                    {
                        short high = Convert.ToInt16(value >> 16);
                        cmdList.Add(AssemblerFac.GenLUI(regB, high + ""));
                        short low = Convert.ToInt16(value & 0xffff);
                        cmdList.Add(AssemblerFac.GenORI(regB, regB, low + ""));
                    }
                }
                else
                {
                    if (varTable.GetAddrInfo(f.Arg1) == "")
                    {
                        regB = getReg(f, varTable, false, cmdList);
                        cmdList.Add(AssemblerFac.GenLW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                        regUseTable.GetContent(regB).Add(f.Arg1);
                        varTable.SetAddrInfo(f.Arg1, regB);
                    }
                    else
                    {
                        regB = varTable.GetAddrInfo(f.Arg1);
                    }
                }
                regUseTable.GetContent(regB).Add(f.Result);
                varTable.SetAddrInfo(f.Result, regB);

                if(varTable.Contains(f.Arg1) && varTable.GetPeekActInfo(f.Arg1) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Arg1, "");
                    regUseTable.GetContent(regB).Remove(f.Arg1);
                }

                if (varTable.Contains(f.Result) && varTable.GetPeekActInfo(f.Result) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(f.Result).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Result, "");
                    regUseTable.GetContent(regB).Remove(f.Result);
                }
            }
            #endregion

            #region Arithmetical or Logical Operation
            else if (f.Op <= FourExpOperation.or)//数学或逻辑运算
            {
                //获取第一个参数的寄存器
                string regA, regB, regC;
                if (isNumber(f.Arg1))
                {
                    regB = getReg(f, varTable, false, cmdList);
                    int value = Convert.ToInt32(f.Arg1);
                    if (value <= 32767 && value >= -32768)
                    {
                        cmdList.Add(AssemblerFac.GenLUI(regB, value.ToString()));
                        cmdList.Add(AssemblerFac.GenSRL(regB, regB, 16 + ""));
                    }
                    else
                    {
                        short high = Convert.ToInt16(value >> 16);
                        cmdList.Add(AssemblerFac.GenLUI(regB, high + ""));
                        short low = Convert.ToInt16(value & 0xffff);
                        cmdList.Add(AssemblerFac.GenORI(regB, regB, low + ""));
                    }
                }
                else
                {
                    if (varTable.GetAddrInfo(f.Arg1) == "")
                    {
                        regB = getReg(f, varTable, false, cmdList);
                        varTable.SetAddrInfo(f.Arg1, regB);
                        regUseTable.Add(regB, f.Arg1);
                        cmdList.Add(AssemblerFac.GenLW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                    }
                    else
                    {
                        regB = varTable.GetAddrInfo(f.Arg1);
                    }
                }
                //获取第二个参数的寄存器
                if (isNumber(f.Arg1))
                {
                    regC = getReg(f, varTable, false, cmdList);
                    int value = Convert.ToInt32(f.Arg2);
                    if (value <= 32767 && value >= -32768)
                    {
                        cmdList.Add(AssemblerFac.GenLUI(regC, value.ToString()));
                        cmdList.Add(AssemblerFac.GenSRL(regC, regC, 16 + ""));
                    }
                    else
                    {
                        short high = Convert.ToInt16(value >> 16);
                        cmdList.Add(AssemblerFac.GenLUI(regC, high + ""));
                        short low = Convert.ToInt16(value & 0xffff);
                        cmdList.Add(AssemblerFac.GenORI(regC, regC, low + ""));
                    }
                }
                else
                {
                    if (varTable.GetAddrInfo(f.Arg2) == "")
                    {
                        regC = getReg(f, varTable, false, cmdList);
                        varTable.SetAddrInfo(f.Arg2, regC);
                        regUseTable.Add(regC, f.Arg2);
                        cmdList.Add(AssemblerFac.GenLW(regC, varTable.GetAddr(f.Arg2).ToString(), "$ZERO"));
                    }
                    else
                    {
                        regC = varTable.GetAddrInfo(f.Arg2);
                    }
                }

                //获取计算结果的寄存器
                regA = getReg(f, varTable, true, cmdList);
                varTable.SetAddrInfo(f.Result, regA);
                regUseTable.Add(regA, f.Result);

                if (regA == regB)
                {
                    foreach (string var in regUseTable.GetContent(regB))
                    {
                        cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(var).ToString(), "$ZERO"));
                        varTable.SetAddrInfo(var, "");
                        regUseTable.GetContent(regB).Remove(var);
                    }
                }
                if (regA == regC)
                {
                    foreach (string var in regUseTable.GetContent(regC))
                    {
                        cmdList.Add(AssemblerFac.GenSW(regC, varTable.GetAddr(var).ToString(), "$ZERO"));
                        varTable.SetAddrInfo(var, "");
                        regUseTable.GetContent(regC).Remove(var);
                    }
                }
                string operation = "";
                switch (f.Op)
                {
                    case FourExpOperation.add:
                        operation = Mnemonic.ADD.ToString();
                        break;
                    case FourExpOperation.sub:
                        operation = Mnemonic.SUB.ToString();
                        break;
                    //case FourExpOperation.mul:

                    //    break;
                    //case FourExpOperation.div:

                    //    break;
                    case FourExpOperation.and:
                        operation = Mnemonic.AND.ToString();
                        break;
                    case FourExpOperation.or:
                        operation = Mnemonic.OR.ToString();
                        break;
                    case FourExpOperation.xor:
                        operation = Mnemonic.XOR.ToString();
                        break;
                    case FourExpOperation.nor:
                        operation = Mnemonic.NOR.ToString();
                        break;
                    default:
                        //错误处理
                        break;
                }
                cmdList.Add(AssemblerFac.GenMathOrLog(operation, regA, regB, regC));
                if (varTable.Contains(f.Arg1) && (varTable.GetAddrInfo(f.Arg1) != null) && (varTable.GetPeekActInfo(f.Arg1) == false))
                {
                    cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Arg1, "");
                    regUseTable.GetContent(regB).Remove(f.Arg1);
                }
                if (varTable.Contains(f.Arg2) && (varTable.GetAddrInfo(f.Arg2) != null) && (varTable.GetPeekActInfo(f.Arg2) == false))
                {
                    cmdList.Add(AssemblerFac.GenSW(regC, varTable.GetAddr(f.Arg2).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Arg2, "");
                    regUseTable.GetContent(regC).Remove(f.Arg2);
                }
                if (varTable.Contains(f.Result) && varTable.GetPeekActInfo(f.Result) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regA, varTable.GetAddr(f.Result).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Result, "");
                    regUseTable.GetContent(regA).Remove(f.Result);
                }
            }
            #endregion

            #region Not or Neg Operation
            else if (f.Op == FourExpOperation.neg)
            {
                string regB;
                if (isNumber(f.Arg1))
                {
                    regB = getReg(f, varTable, false, cmdList);
                    int value = Convert.ToInt32(f.Arg1);
                    if (value <= 32767 && value >= -32768)
                    {
                        cmdList.Add(AssemblerFac.GenLUI(regB, value.ToString()));
                        cmdList.Add(AssemblerFac.GenSRL(regB, regB, 16 + ""));
                    }
                    else
                    {
                        short high = Convert.ToInt16(value >> 16);
                        cmdList.Add(AssemblerFac.GenLUI(regB, high + ""));
                        short low = Convert.ToInt16(value & 0xffff);
                        cmdList.Add(AssemblerFac.GenORI(regB, regB, low + ""));
                    }
                }
                else
                {
                    regB = varTable.GetAddrInfo(f.Arg1);
                    if (regB == "")
                    {
                        regB = getReg(f, varTable, false, cmdList);
                        cmdList.Add(AssemblerFac.GenLW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                        varTable.SetAddrInfo(f.Arg1, regB);
                        regUseTable.GetContent(regB).Add(f.Arg1);
                    }
                }

                string regA = varTable.GetAddrInfo(f.Result);
                if (regA == "")
                {
                    regA = getReg(f, varTable, true, cmdList);
                    varTable.SetAddrInfo(f.Result, regA);
                    regUseTable.GetContent(regA).Add(f.Result);
                }
                cmdList.Add(AssemblerFac.GenSUB(regA, "$ZERO", regB));
                if (varTable.Contains(f.Arg1) && varTable.GetPeekActInfo(f.Arg1) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Arg1, "");
                    regUseTable.GetContent(regB).Remove(f.Arg1);
                }
                if (varTable.Contains(f.Arg1) && varTable.GetPeekActInfo(f.Result) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regA, varTable.GetAddr(f.Result).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Result, "");
                    regUseTable.GetContent(regA).Remove(f.Result);
                }
            }
            else if (f.Op == FourExpOperation.not)
            {
                string regB;
                if (isNumber(f.Arg1))
                {
                    regB = getReg(f, varTable, false, cmdList);
                    int value = Convert.ToInt32(f.Arg1);
                    if (value <= 32767 && value >= -32768)
                    {
                        cmdList.Add(AssemblerFac.GenLUI(regB, value.ToString()));
                        cmdList.Add(AssemblerFac.GenSRL(regB, regB, 16 + ""));
                    }
                    else
                    {
                        short high = Convert.ToInt16(value >> 16);
                        cmdList.Add(AssemblerFac.GenLUI(regB, high + ""));
                        short low = Convert.ToInt16(value & 0xffff);
                        cmdList.Add(AssemblerFac.GenORI(regB, regB, low + ""));
                    }
                }
                else
                {
                    regB = varTable.GetAddrInfo(f.Arg1);
                    if (regB == "")
                    {
                        regB = getReg(f, varTable, false, cmdList);
                        cmdList.Add(AssemblerFac.GenLW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                        varTable.SetAddrInfo(f.Arg1, regB);
                        regUseTable.GetContent(regB).Add(f.Arg1);
                    }
                }

                string regA = varTable.GetAddrInfo(f.Result);
                if (regA == "")
                {
                    regA = getReg(f, varTable, true, cmdList);
                    varTable.SetAddrInfo(f.Result, regA);
                    regUseTable.GetContent(regA).Add(f.Result);
                }
                cmdList.Add(AssemblerFac.GenXORI(regA, regB, Convert.ToString(1)));//a = NOT b => a = b xor 1
                if (varTable.Contains(f.Arg1) && varTable.GetPeekActInfo(f.Arg1) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regB, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Arg1, "");
                    regUseTable.GetContent(regB).Remove(f.Arg1);
                }
                if (varTable.Contains(f.Result) && varTable.GetPeekActInfo(f.Result) == false)
                {
                    cmdList.Add(AssemblerFac.GenSW(regA, varTable.GetAddr(f.Result).ToString(), "$ZERO"));
                    varTable.SetAddrInfo(f.Result, "");
                    regUseTable.GetContent(regA).Remove(f.Result);
                }
            }
            #endregion

            #region Rightshift or Leftshift
            else if (f.Op == FourExpOperation.leftshift || f.Op == FourExpOperation.rightshift)
            {

            }
            #endregion

            else
            {
                //错误处理
            }
        }
示例#12
0
 /// <summary>
 /// 初始化变量表
 /// </summary>
 /// <param name="varTable">变量表</param>
 /// <param name="fourExpList">四元式列表</param>
 /// <param name="varNameList">变量表中变量名的列表</param>
 private void initVarTable(VarTable varTable, List<FourExp> fourExpList, List<string> varNameList)
 {
     short address = 0x0000;
     foreach (string varName in varNameList)
     {
         //初始化变量表中的变量地址和活跃信息域
         varTable.SetAddr(varName, address);
         address += 4;
         //varTable.ClearRefeInfo(varName);
         varTable.ClearActInfo(varName);
         varTable.PushActInfo(varName, false);
         //varTable.PushRefeInfo(varName, -1);
     }
     //扫描四元式表,在变量表中填入相关信息
     int count = fourExpList.Count;
     int length = count;
     for (int i = length; i != 0; i--)
     {
         string A = fourExpList[i].Result;
         string B = fourExpList[i].Arg1;
         string C = fourExpList[i].Arg2;
         if (A != "")
         {
             //varTable.PushRefeInfo(A, -1);
             varTable.PushActInfo(A, false);
         }
         if (B != "")
         {
             //varTable.PushRefeInfo(B, count);
             varTable.PushActInfo(B, true);
         }
         if (C != "")
         {
             //varTable.PushRefeInfo(C, count);
             varTable.PushActInfo(C, true);
         }
         count--;
     }
 }
示例#13
0
 /// <summary>
 /// 获取寄存器,isResult为true,则说明需要返回的是存放结果的寄存器
 /// </summary>
 /// <param name="f">四元式</param>
 /// <param name="varTable">变量表</param>
 /// <param name="isResult">指定获取的寄存器是否是用来存放计算结果</param>
 /// <param name="cmdList">汇编指令列表</param>
 /// <returns>寄存器名</returns>
 private string getReg(FourExp f, VarTable varTable, bool isResult, List<AssemblerIns> cmdList)
 {
     //返回B或者C所在的寄存器
     if (isResult)
     {
         if ((f.Arg1 != "") && (varTable.GetAddrInfo(f.Arg1) != ""))
         {
             string regB = varTable.GetAddrInfo(f.Arg1);
             if ((varTable.GetPeekActInfo(f.Arg1) == false) || f.Arg1 == f.Result || regUseTable.GetContent(regB).Count == 1)
             {
                 return regB;
             }
         }
         if ((f.Arg2 != "") && (varTable.GetAddrInfo(f.Arg2) != ""))
         {
             string regC = varTable.GetAddrInfo(f.Arg1);
             if ((varTable.GetPeekActInfo(f.Arg2) == false) || f.Arg2 == f.Result || regUseTable.GetContent(regC).Count == 1)
             {
                 return regC;
             }
         }
     }
     //返回未占用寄存器
     foreach (string regName in registers)
     {
         if (regUseTable.GetContent(regName) == null)
         {
             return regName;
         }
     }
     //随机返回一个已占用的寄存器
     Random r = new Random();
     while (true)
     {
         int i = r.Next(registers.Length);
         string reg = registers[i];
         List<string> varList = new List<string>() { f.Arg1, f.Arg2, f.Result };
         if (!regUseTable.Contains(reg, varList))
         {
             //调整变量表和寄存器表中的相关域
             doAdjust(reg, varTable, cmdList);
             return reg;
         }
     }
 }
示例#14
0
 /// <summary>
 /// 生成数据段
 /// </summary>
 /// <param name="varNameList">变量名列表</param>
 /// <param name="varTable">变量表</param>
 /// <param name="cmdList">汇编指令列表</param>
 private void genDataIns(List<string> varNameList, VarTable varTable, List<AssemblerIns> cmdList)
 {
     foreach (string varName in varNameList)
     {
         if (varTable.GetType(varName) == VariableType.INT || varTable.GetType(varName) == VariableType.CHAR || varTable.GetType(varName) == VariableType.BOOL)
         {
             short varValue = (short)varTable.GetValue(varName);
             short varAddr = varTable.GetAddr(varName);
             cmdList.Add(AssemblerFac.GenLUI("$T0", varValue.ToString()));
             cmdList.Add(AssemblerFac.GenSRL("$T0", "$T0", Convert.ToString(16)));
             cmdList.Add(AssemblerFac.GenSW("$T0", varAddr.ToString(), "$ZERO"));
         }
         else
         {
             int value = varTable.GetValue(varName);
             short high = (short)(value>>16);
             short varAddr = varTable.GetAddr(varName);
             cmdList.Add(AssemblerFac.GenLUI("$T0", high.ToString()));
             short low = (short)(value & 0xffff);
             cmdList.Add(AssemblerFac.GenORI("$T0", "$T0", low.ToString()));
             cmdList.Add(AssemblerFac.GenSW("$T0", varAddr.ToString(), "$ZERO"));
         }
     }
 }
示例#15
0
 /// <summary>
 /// 翻译跳转指令
 /// </summary>
 /// <param name="f">四元式</param>
 /// <param name="operation">汇编指令的操作助记符</param>
 /// <param name="varTable">变量表</param>
 /// <param name="cmdList">汇编指令列表</param>
 private void doJump(FourExp f, string operation, VarTable varTable, List<AssemblerIns> cmdList)
 {
     string reg1 = "", reg2 = "";
     reg1 = varTable.GetAddrInfo(f.Arg1);
     reg2 = varTable.GetAddrInfo(f.Arg2);
     if (reg1 == "")
     {
         reg1 = getReg(f, varTable, false, cmdList);
         cmdList.Add(AssemblerFac.GenLW(reg1, varTable.GetAddr(f.Arg1).ToString(), "$ZERO"));
         regUseTable.GetContent(reg1).Add(f.Arg1);
         varTable.SetAddrInfo(f.Arg1, reg1);
     }
     if (reg2 == "")
     {
         reg2 = getReg(f, varTable, false, cmdList);
         cmdList.Add(AssemblerFac.GenLW(reg2, varTable.GetAddr(f.Arg2).ToString(), "$ZERO"));
         regUseTable.GetContent(reg2).Add(f.Arg2);
         varTable.SetAddrInfo(f.Arg2, reg2);
     }
     cmdList.Add(AssemblerFac.GenSLT("$T0", reg1, reg2));
     cmdList.Add(AssemblerFac.GenJUMP(operation, "$T0", f.TargetLabel.ToString()));
     adjustAfterJump(f, reg1, reg2, varTable, cmdList);
 }
示例#16
0
 public override string GetValue(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     return this.value.ToString();
 }
示例#17
0
        public override string GetValue(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
        {
            string returnValue = "";
            //加、减、乘、除、按位与、按位或
            if (this.op.Type == OperatorType.add || this.op.Type == OperatorType.sub || this.op.Type == OperatorType.mul || this.op.Type == OperatorType.div || this.op.Type == OperatorType.bitand || this.op.Type == OperatorType.bitor)
            {
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                string t = varTable.NewTemp(arg1, arg2);
                switch (this.op.Type)
                {
                    case OperatorType.add:          //+
                        fourExpList.Add(FourExpFac.GenAdd(arg1, arg2, t));
                        break;
                    case OperatorType.sub:          //-
                        fourExpList.Add(FourExpFac.GenSub(arg1, arg2, t));
                        break;
                    case OperatorType.mul:          //*
                        fourExpList.Add(FourExpFac.GenMul(arg1, arg2, t));
                        break;
                    case OperatorType.div:          ///
                        fourExpList.Add(FourExpFac.GenDiv(arg1, arg2, t));
                        break;
                    case OperatorType.bitand:       //按位与
                        fourExpList.Add(FourExpFac.GenAnd(arg1, arg2, t));
                        break;
                    case OperatorType.bitor:        //按位或
                        fourExpList.Add(FourExpFac.GenOr(arg1, arg2, t));
                        break;
                    default:
                        break;
                }
                returnValue = t;
            }
            //与、或
            else if (this.op.Type == OperatorType.and || this.op.Type == OperatorType.or)
            {
                string label1 = labelStack.NewLabel();
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenJe(arg1, 0 + "", label1));
                string t1 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenMov(1 + "", t1));
                arg1 = t1;
                fourExpList.Add(FourExpFac.GenLabel(label1));

                string label2 = labelStack.NewLabel();
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                fourExpList.Add(FourExpFac.GenJe(arg2, 0 + "", label2));
                string t2 = varTable.NewTemp(VariableType.BOOL);
                fourExpList.Add(FourExpFac.GenMov(1 + "", t2));
                arg2 = t2;
                fourExpList.Add(FourExpFac.GenLabel(label2));

                string t3 = varTable.NewTemp(VariableType.BOOL);
                switch (this.op.Type)
                {
                    case OperatorType.and:      //与
                        fourExpList.Add(FourExpFac.GenAnd(arg1, arg2, t3));
                        break;
                    case OperatorType.or:       //或
                        fourExpList.Add(FourExpFac.GenOr(arg1, arg2, t3));
                        break;
                }
                returnValue = t3;
            }
            //左移、右移
            else if (this.op.Type == OperatorType.leftmove || this.op.Type == OperatorType.rightmove)
            {
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                string t = varTable.NewTemp(arg1);
                switch (this.op.Type)
                {
                    case OperatorType.leftmove:             //左移
                        fourExpList.Add(FourExpFac.GenLeftshift(arg1, arg2, t));
                        break;
                    case OperatorType.rightmove:            //右移
                        fourExpList.Add(FourExpFac.GenRightshift(arg1, arg2, t));
                        break;
                    default:
                        break;
                }
                returnValue = t;
            }
            //小于、小于等于、大于、大于等于、等于、不等于
            else if (this.op.Type == OperatorType.less || this.op.Type == OperatorType.lessequal || this.op.Type == OperatorType.greater || this.op.Type == OperatorType.greatereuqal || this.op.Type == OperatorType.equal || this.op.Type == OperatorType.notequal)
            {
                string label1 = labelStack.NewLabel();
                string label2 = labelStack.NewLabel();
                string t = varTable.NewTemp(VariableType.BOOL);
                string arg1 = expression1.GetValue(varTable, labelStack, fourExpList);
                string arg2 = expression2.GetValue(varTable, labelStack, fourExpList);
                switch (this.op.Type)
                {
                    case OperatorType.less:         //<
                        fourExpList.Add(FourExpFac.GenJl(arg1, arg2, label1));
                        break;
                    case OperatorType.lessequal:    //<=
                        fourExpList.Add(FourExpFac.GenJle(arg1, arg2, label1));
                        break;
                    case OperatorType.greater:      //>
                        fourExpList.Add(FourExpFac.GenJg(arg1, arg2, label1));
                        break;
                    case OperatorType.greatereuqal: //>=
                        fourExpList.Add(FourExpFac.GenJge(arg1, arg2, label1));
                        break;
                    case OperatorType.equal:        //==
                        fourExpList.Add(FourExpFac.GenJe(arg1, arg2, label1));
                        break;
                    case OperatorType.notequal:     //!=
                        fourExpList.Add(FourExpFac.GenJne(arg1, arg2, label1));
                        break;
                    default:
                        //
                        break;
                }
                fourExpList.Add(FourExpFac.GenMov(0 + "", t));
                fourExpList.Add(FourExpFac.GenJmp(label2));
                fourExpList.Add(FourExpFac.GenLabel(label1));
                fourExpList.Add(FourExpFac.GenMov(1 + "", t));
                fourExpList.Add(FourExpFac.GenLabel(label2));
                returnValue = t;
            }
            else
            {
                //错误处理
            }
            return returnValue;
        }
示例#18
0
 public virtual void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
 }
示例#19
0
 public override string GetValue(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     string returnValue = "";
     string label, t1, t2, var;
     switch (this.op.Type)
     {
         case OperatorType.not:          //非
             label = labelStack.NewLabel();
             var = expression.GetValue(varTable, labelStack, fourExpList);
             fourExpList.Add(FourExpFac.GenJe(var, 0 + "", label));
             t1 = varTable.NewTemp(VariableType.BOOL);
             fourExpList.Add(FourExpFac.GenMov(1 + "", t1));
             var = t1;
             fourExpList.Add(FourExpFac.GenLabel(label));
             t2 = varTable.NewTemp(VariableType.BOOL);
             fourExpList.Add(FourExpFac.GenNot(t1, t2));
             returnValue = t2;
             break;
         case OperatorType.selfaddhead:      //自增前置
             var = expression.GetValue(varTable, labelStack, fourExpList);
             fourExpList.Add(FourExpFac.GenAdd(var, 1 + "", var));
             returnValue = var;
             break;
         case OperatorType.selfsubhead:      //自减前置
             var = expression.GetValue(varTable, labelStack, fourExpList);
             fourExpList.Add(FourExpFac.GenSub(var, 1 + "", var));
             returnValue = var;
             break;
         case OperatorType.selfaddtail:      //自增后置
             var = expression.GetValue(varTable, labelStack, fourExpList);
             t1 = varTable.NewTemp(var);
             fourExpList.Add(FourExpFac.GenAdd(var, 1 + "", var));
             returnValue = t1;
             break;
         case OperatorType.selfsubtail:      //自减后置
             var = expression.GetValue(varTable, labelStack, fourExpList);
             t1 = varTable.NewTemp(var);
             fourExpList.Add(FourExpFac.GenSub(var, 1 + "", var));
             returnValue = t1;
             break;
         case OperatorType.bitnot:       //按位非
             var = expression.GetValue(varTable, labelStack, fourExpList);
             t1 = varTable.NewTemp(var);
             fourExpList.Add(FourExpFac.GenNot(var, t1));
             returnValue = t1;
             break;
         case OperatorType.neg:          //取反
             var = expression.GetValue(varTable, labelStack, fourExpList);
             t1 = varTable.NewTemp(var);
             fourExpList.Add(FourExpFac.GenNeg(var, t1));
             returnValue = t1;
             break;
     }
     return returnValue;
 }
示例#20
0
 public override void Translate(VarTable varTable, LabelStack labelStack, List<FourExp> fourExpList)
 {
     string value = expression.GetValue(varTable, labelStack, fourExpList);
     varTable.SetValue(this.identify, value);
 }