예제 #1
0
        /// <summary>
        /// Inserts a procedure with all necessary information
        /// </summary>
        private void insertProcedure()
        {
            token = getNextToken(); // proc
            token = getNextToken(); // proc name

            SymTab.entry ptr = st.lookUp(token.Trim(' '));

            emit(token + "\t PROC");
            emit("\tpush bp ");                               //push bp
            mov("bp", "sp");                                  // mov bp, sp
            SymTab.entry.function fptr = ptr as SymTab.entry.function;
            emit("\tsub sp, " + fptr.sizeOfLocal.ToString()); // sub sp, size of locals from symtab

            //TRANSLATED CODE
            emit("");
            if (token != "endp")
            {
                token = getNextToken(); // get L
            }
            transCode();


            //Last part of template
            emit("\tadd sp, " + fptr.sizeOfLocal.ToString());
            emit("\tpop bp");
            emit("\tret " + fptr.sizeOfParams);

            token = getNextToken(); // proc name
            emit(token + "\t endp");
        }
예제 #2
0
        /// <summary>
        /// Insert all variables at depth 1 in in the .asm
        /// </summary>
        public void buildDataSeg()
        {
            for (int i = 0; i < SymTab.tableSize; i++)
            {
                SymTab.entry temp = SymTab.hashTable[i].ElementAt(0);


                while (temp.Next != null)
                {
                    if (temp.typeOfEntry == SymTab.entryType.literalEntry)
                    {
                        SymTab.entry.literal lptr = temp as SymTab.entry.literal;
                        emit(temp.lexeme + "\t" + "db\t" + lptr.lit + ",\"$\"");
                    }

                    if (temp.depth == 1)
                    {
                        if (temp.typeOfEntry == SymTab.entryType.varEntry)
                        {
                            emit("_" + temp.lexeme + "\t" + "dw\t" + "?");
                        }
                    }
                    if (temp.Next != null)
                    {
                        temp = temp.Next;
                    }
                }
                if (temp.depth == 1)
                {
                    if (temp.typeOfEntry == SymTab.entryType.varEntry)
                    {
                        emit("_" + temp.lexeme + "\t" + "dw\t" + "?");
                    }
                }
                else
                {
                    if (temp.typeOfEntry == SymTab.entryType.literalEntry)
                    {
                        SymTab.entry.literal lptr = temp as SymTab.entry.literal;
                        emit(temp.lexeme + "\t" + "db\t" + lptr.lit + ",\"$\"");
                    }
                }
            }
        }//End build dataseg
예제 #3
0
        private void L()
        {
            string l;

            switch (token[0])
            {
            //BP
            case ('_'):
                l     = token;
                token = getNextToken();     // get =
                R(l);
                emit("");

                break;

            case ('@'):

                l     = token;
                token = getNextToken();     // get =
                R(l);

                emit("");

                break;



            default:

                switch (token)
                {
                case ("wri"):

                    token = getNextToken();

                    //If pushing a number
                    if (char.IsDigit(token[0]))
                    {
                        mov("ax", token);
                        // emit("\tcall writeint");
                    }
                    //Else a variable
                    else
                    {
                        //De ref
                        if (token[0] == '@')
                        {
                            token = trim(token);

                            if (token[0] == '_')
                            {
                                mov("bx", "[" + trim(token) + "]");
                            }
                            else
                            {
                                mov("bx", "[" + token + "]");
                            }
                        }
                        else
                        {
                            if (token[0] == '_')
                            {
                                mov("ax", "[" + trim(token) + "]");
                            }
                            else
                            {
                                mov("ax", "[" + token + "]");
                            }
                        }


                        //emit("\tcall writeint");
                    }
                    emit("\tcall writeint");

                    token = getNextToken();

                    break;

                case ("wrs"):

                    token = getNextToken();
                    mov("dx", " offset " + token);
                    emit("\tcall writestr");

                    token = getNextToken();

                    break;

                case ("wrln"):
                    emit("\tcall writeln");
                    token = getNextToken();

                    break;

                case ("rdi"):
                    emit("\tcall readint");
                    //mov();
                    token = getNextToken();

                    if (token[0] == '@')
                    {
                        token = trim(token);
                        if (token[0] == '_')
                        {
                            token = trim(token);
                        }

                        mov("[" + token + "]", "bx");
                        //mov("dx", "[" + token + "]");
                    }
                    else
                    {
                        if (token[0] == '_')
                        {
                            token = trim(token);
                        }

                        mov("[" + token + "]", "bx");
                    }



                    token = getNextToken();

                    break;

                case ("rds"):
                    break;

                case ("push"):
                    token = getNextToken();

                    if (token[0] == '@')         // De reference?
                    {
                        token = trim(token);

                        if (token[0] == '_')

                        {
                            token = trim(token);
                            if (token[0] == '_')
                            {
                                mov("ax", "offset " + token);
                                // mov("ax", "offset " + token);
                            }
                            else
                            {
                                mov("ax", token);
                            }
                        }



                        emit("\tpush ax \n");
                    }
                    else
                    {
                        if (token[0] == '_')
                        {
                            token = trim(token);
                            mov("ax", token);
                        }
                        else
                        {
                            mov("ax", "offset " + token);
                        }


                        emit("\tpush ax \n");

                        //token = getNextToken();

                        //Console.WriteLine(token);
                        //token = getNextToken();
                    }

                    token = getNextToken();
                    break;

                case ("call"):

                    token = getNextToken();
                    emit("\tcall " + token);

                    token = getNextToken();
                    break;

                case ("proc"):
                    break;

                case ("endp"):
                    break;

                default:
                    SymTab.entry tmp = st.lookUp(token);

                    if (tmp.token == lexicalScanner.SYMBOL.unkownt)
                    {
                        token = getNextToken();
                    }
                    else
                    {
                        l     = token;
                        token = getNextToken();         // get =

                        R(l);
                        emit("");
                        //Console.WriteLine("Second switch -- variable?");
                        //  if(token != "proc" || token != "endp")
                    }
                    break;
                }
                break;
            }
        }
예제 #4
0
        private void rTail()
        {
            //ch = getNextChar();
            switch (token[0])
            {
            case ('+'):


                token = getNextToken();

                SymTab.entry ptr = st.lookUp(token);
                char         c   = token[0];
                if (char.IsDigit(c))
                {
                }
                else if (ptr.token == lexicalScanner.SYMBOL.unkownt)
                {
                    if (token[0] == '@')
                    {
                        if (token[0] == '_')
                        {
                            emit("\tadd ax, " + "[" + trim(trim(token)) + "]");
                        }
                        else
                        {
                            emit("\tadd ax, " + "[" + token + "]");
                        }
                    }
                    else
                    {
                        if (token[0] == '_')
                        {
                            emit("\tadd ax," + "[" + trim(token) + "]");
                        }
                        else
                        {
                            emit("\tadd ax, " + "[" + token + "]");
                        }
                    }
                }
                else
                {
                    if (token[0] == '@')
                    {
                        if (token[0] == '_')
                        {
                            emit("\tadd ax, " + trim(trim(token)));
                        }
                        else
                        {
                            emit("\tadd ax, " + token);
                        }
                    }
                    else
                    {
                        if (token[0] == '_')
                        {
                            emit("\tadd ax," + trim(token));
                        }
                        else
                        {
                            emit("\tadd ax, " + token);
                        }
                    }
                }

                token = getNextToken();
                break;

            case ('*'):

                token = getNextToken();

                mov("bx ", "[" + trim(token) + "]");
                emit("\timul " + "bx");
                token = getNextToken();
                break;

            default:
                break;
            }
        }