Example #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
        }
Example #2
0
 public AstType visit(AstNewObj n)
 {
     return null;
 }
Example #3
0
        /* throws ParseException */
        /* final */
        public static AstExp astExp()
        {
            AstType t;
            AstToken n;
            AstRelop.OP relop;
            AstBinop.OP binop;
            AstUnop.OP unop;
            AstExp e, e1, e2;
            AstExpList el;

            jj_consume_token(AstRegExpId.kw56);
            switch ((jj_ntk == AstRegExpId.UNDEFINED)?jj_ntk_fn():jj_ntk) {
            case AstRegExpId.kwArrayElm:
              jj_consume_token(AstRegExpId.kwArrayElm);
              e1 = astExp();
              e2 = astExp();
                                  e = new AstArrayElm(e1,e2);
              break;
            case AstRegExpId.kwArrayLen:
              jj_consume_token(AstRegExpId.kwArrayLen);
              e1 = astExp();
                                  e = new AstArrayLen(e1);
              break;
            case AstRegExpId.kwBinop:
              jj_consume_token(AstRegExpId.kwBinop);
              binop = binOp();
              e1 = astExp();
              e2 = astExp();
                                  e = new AstBinop(binop,e1,e2);
              break;
            case AstRegExpId.kwBoolVal:
              jj_consume_token(AstRegExpId.kwBoolVal);
              switch ((jj_ntk == AstRegExpId.UNDEFINED)?jj_ntk_fn():jj_ntk) {
              case AstRegExpId.kwTrue:
            jj_consume_token(AstRegExpId.kwTrue);
                                  e = new AstBoolVal(true);
            break;
              case AstRegExpId.kwFalse:
            jj_consume_token(AstRegExpId.kwFalse);
                                  e = new AstBoolVal(false);
            break;
              default:
            jj_la1[12] = jj_gen;
            jj_consume_token(AstRegExpId.UNDEFINED);
            throw new AstParseException();
              }
              break;
            case AstRegExpId.kwCall:
              jj_consume_token(AstRegExpId.kwCall);
              e1 = astExp();
              e2 = astExp();
              el = astExpList();
                                  e = new AstCall(e1,(AstId)e2,el);
              break;
            case AstRegExpId.kwId:
              jj_consume_token(AstRegExpId.kwId);
              n = jj_consume_token(AstRegExpId.ID);
                                  e = new AstId(n.image);
              break;
            case AstRegExpId.kwIntVal:
              jj_consume_token(AstRegExpId.kwIntVal);
              n = jj_consume_token(AstRegExpId.INTVAL);
                                  e = new AstIntVal(int.Parse(n.image));
              break;
            case AstRegExpId.kwField:
              jj_consume_token(AstRegExpId.kwField);
              e1 = astExp();
              e2 = astExp();
                                  e = new AstField(e1,(AstId)e2);
              break;
            case AstRegExpId.kwNewArray:
              jj_consume_token(AstRegExpId.kwNewArray);
              t = astType();
              n = jj_consume_token(AstRegExpId.INTVAL);
              e = new AstNewArray((AstType)t, int.Parse(n.image));
              break;
            case AstRegExpId.kwNewObj:
              jj_consume_token(AstRegExpId.kwNewObj);
              e1 = astExp();
              el = astExpList();
                                  e = new AstNewObj((AstId)e1,el);
              break;
            case AstRegExpId.kwRelop:
              jj_consume_token(AstRegExpId.kwRelop);
              relop = relOp();
              e1 = astExp();
              e2 = astExp();
              e = new AstRelop(relop, e1, e2);
              break;
            case AstRegExpId.kwStrVal:
              jj_consume_token(AstRegExpId.kwStrVal);
              n = jj_consume_token(AstRegExpId.STRVAL);
                                  String s = n.image;
                                  e = new AstStrVal(s.Substring(1, s.Length-2));
              break;
            case AstRegExpId.kwThis:
              jj_consume_token(AstRegExpId.kwThis);
                                  e = new AstThis();
              break;
            case AstRegExpId.kwUnop:
              jj_consume_token(AstRegExpId.kwUnop);
              unop = unOp();
              e1 = astExp();
                                   e = new AstUnop(unop,e1);
              break;
            case AstRegExpId.kwNullExp:
              jj_consume_token(AstRegExpId.kwNullExp);
                                  e = null;
              break;

            default:
              jj_la1[13] = jj_gen;
              jj_consume_token(AstRegExpId.UNDEFINED);
              throw new AstParseException();
            }
            jj_consume_token(AstRegExpId.kw57);
            {if (true) return e;}
            throw new Error("Missing return statement in function");
        }
Example #4
0
 /* throws Exception */
 // NewObj ---
 // Id cid;
 // ExpList args;
 public AstType visit(AstNewObj n)
 {
     symTable.GetClass(n.cid);
     return new AstObjType(n.cid);
 }