Exemple #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
        }
Exemple #2
0
 // NewArray ---
 // int size;
 public IrExp visit(AstNewArray n)
 {
     IrExp size = new IrConst(n.size);
     IrStmtList sl = new IrStmtList();
     IrName top = new IrName();
     IrTemp array = new IrTemp();
     IrTemp cnt = new IrTemp();
     IrExp bound = new IrBinop(IrBinop.OP.MUL, new IrConst(n.size + 1), cWordSize);
     sl.add(new IrMove(array, new IrCall(new IrName("malloc"), new IrExpList(bound))));
     sl.add(new IrMove(new IrMem(array), size));
     sl.add(new IrMove(cnt, new IrBinop(IrBinop.OP.ADD, array,
                    new IrBinop(IrBinop.OP.MUL, size, cWordSize))));
     sl.add(new IrLabel(top));
     sl.add(new IrMove(new IrMem(cnt), cZero));
     sl.add(new IrMove(cnt, new IrBinop(IrBinop.OP.SUB, cnt, cWordSize)));
     sl.add(new IrCJump(IrCJump.OP.GT, cnt, array, top));
     return new IrEseq(sl, array);
 }
Exemple #3
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");
        }
Exemple #4
0
        public int visit(IrBinop e)
        {
            // evaluate both operands to lval and rval
            int lval = e.left.accept(this);
            int rval = e.right.accept(this);

            // evaluate expression value
            switch (e.op) {
            case IrBinop.OP.ADD: return lval + rval;
            case IrBinop.OP.SUB: return lval - rval;
            case IrBinop.OP.MUL: return lval * rval;
            case IrBinop.OP.DIV: return lval / rval;
            case IrBinop.OP.AND: return (2 == (lval + rval)) ? 1 : 0;
            case IrBinop.OP.OR: return (0 < (lval + rval)) ? 1 : 0;
            default: throw new Exception("Encountered undefined BINOP value: " + e.op);
            }
        }
Exemple #5
0
 public IrExp visit(IrBinop t)
 {
     IrExp left = t.left.accept(this);
     IrExp right = t.right.accept(this);
     IrStmt s = mergeStmts(getStmt(left), getStmt(right));
     if (s != null)
         return new IrEseq(s, new IrBinop(t.op, getExp(left), getExp(right)));
     return t;
 }