예제 #1
0
        // *** partial implementation, will be fixed in proj2 ****
        // NewObj ---
        // Id cid;
        public IrExp visit(AstNewObj n)
        {
            // A NewObj node should be translated into a CALL to "malloc" with a single argument representing the size
            // of the object, followed by a sequence of statements for initializing the object’s class variables.
            // A special note: the initialization expressions saved in the the symbol table entry for this purpose are
            // AST expressions, and need to be translated into IR expressions. Furthermore, this translation should be
            // conducted in the proper scope environment — the IrgenVisitor variable currClass needs to point to the
            // object’s class.

            IrStmtList stmts = new IrStmtList();

            // Calculate the size of the object
            ClassRec classRec = symTable.GetClass(n.cid);              // <get the object's ClassRec from symbol table>;
            int numVars = GetNumObjVars(classRec);

            // Construct malloc call node
            IrExp size = new IrBinop(IrBinop.OP.MUL, new IrConst(numVars > 0 ? numVars : 1), cWordSize);
            IrCall call = new IrCall(new IrName("malloc"), new IrExpList(size));

            IrTemp tmp = new IrTemp();                // This TEMP will contain the location of the object variables
            stmts.add(new IrMove(tmp, call));       // Move the results of the malloc call into the temp

            GetInitStmts(classRec, stmts, tmp);   // Recursively get object's variable initialization statements

            return new IrEseq(stmts, tmp);          // Construct resulting statement
        }
예제 #2
0
        /* throws ParseException */
        public static IrExp EXP()
        {
            IrToken t;
            int n;
            String str;
            IrStmt s;
            IrExp e, e2;
            IrExpList el = new IrExpList();
            jj_consume_token(RegExpId.kw32);

            switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
            {
                case RegExpId.kwESEQ:

                    jj_consume_token(RegExpId.kwESEQ);
                    s = STMT();
                    e = EXP();
                    e = new IrEseq(s,e);
                    break;

                case RegExpId.kwMEM:

                    jj_consume_token(RegExpId.kwMEM);
                    e = EXP();
                    e = new IrMem(e);
                    break;

                case RegExpId.kwBINOP:

                    jj_consume_token(RegExpId.kwBINOP);
                    IrBinop.OP op = binopCode();
                    e = EXP();
                    e2 = EXP();
                    e = new IrBinop(op,e,e2);
                    break;

                case RegExpId.kwCALL:

                    jj_consume_token(RegExpId.kwCALL);
                    e = EXP();
                    jj_consume_token(RegExpId.kw32);
                    // label_4:
                    while (true)
                    {
                        switch ((jj_ntk == RegExpId.UNDEFINED) ? jj_ntk_fn() : jj_ntk)
                        {
                            case RegExpId.kw32:
                                break;

                            default:
                                jj_la1[7] = jj_gen;
                                goto label_4a; // break label_4;
                        }
                        e2 = EXP();
                        el.add(e2);
                    }
                    label_4a:

                    jj_consume_token(RegExpId.kw35);
                    e = new IrCall((IrName)e,el);
                    break;

                case RegExpId.kwTEMP:

                    jj_consume_token(RegExpId.kwTEMP);
                    n = INT();
                    e = new IrTemp(n);
                    break;

                case RegExpId.kwNAME:

                    jj_consume_token(RegExpId.kwNAME);
                    t = jj_consume_token(RegExpId.ID);
                    e = new IrName(t.image);
                    break;

                case RegExpId.kwFIELD:

                    jj_consume_token(RegExpId.kwFIELD);
                    e = EXP();
                    n = INT();
                    e = new IrField(e,n);
                    break;

                case RegExpId.kwPARAM:

                    jj_consume_token(RegExpId.kwPARAM);
                    n = INT();
                    e = new IrParam(n);
                    break;

                case RegExpId.kwVAR:

                    jj_consume_token(RegExpId.kwVAR);
                    n = INT();
                    e = new IrVar(n);
                    break;

                case RegExpId.kwCONST:

                    jj_consume_token(RegExpId.kwCONST);
                    n = INT();
                    e = new IrConst(n);
                    break;

                case RegExpId.kwSTRING:

                    jj_consume_token(RegExpId.kwSTRING);
                    str = STR();
                    e = new IrString(str);
                    break;

                default:

                    jj_la1[8] = jj_gen;
                    jj_consume_token(RegExpId.UNDEFINED);
                    throw new IrParseException();
            }

            jj_consume_token(RegExpId.kw35);
            {if (true) return e;}
            throw new Error("Missing return statement in function");
        }
예제 #3
0
        public int visit(IrCall s)
        {
            if (s.func.id.Equals("malloc")) {

            IrExp arg0 = s.args.elementAt(0);
            int val = arg0.accept(this);

            hp -= val;
            retVal=hp;

            } else {

            //– find the FUNC node corresponding to the routine from the program’s funcs list, and switch there
            IrFunc func = findFunc(s.func.id);

            //push parameters onto the stack (at stack[sp+1], stack[sp+2], etc.);

            for (int i = 0; i < s.args.size(); ++i) {
                stack[sp + i + 1] = s.args.elementAt(i).accept(this);
            }

            // adjust the frame pointer (save the current fp in stack[sp], and set sp to be the new fp);
            stack[sp] = fp;

            fp = sp;

            //to execute (by invoking accept() on the FUNC node);
            func.accept(this);

            // After control returns, restore the saved frame pointer.
            sp = fp;
            fp = stack[sp];
            }

            return retVal;
        }
예제 #4
0
파일: Canon.cs 프로젝트: robin5/DemiTasse
 public IrExp visit(IrCall t)
 {
     IrExp args = t.args.accept(this);
     IrStmt s = getStmt(args);
     if (t.func.id != "malloc")
     {
         IrTemp tmp = new IrTemp();
         IrStmt s1 = new IrMove(tmp, new IrCall(t.func, (IrExpList)getExp(args)));
         return new IrEseq(mergeStmts(s, s1), tmp);
     }
     else if (s != null)
     {
         return new IrEseq(s, new IrCall(t.func, (IrExpList)getExp(args)));
     }
     else
     {
         return t;
     }
 }