package node.exprNode.postfixNode; import node.*; import node.exprNode.*; import node.exprNode.variableNode.*; import env.*; import type.*; import type.basicType.*; import runEnv.*; import runEnv.basicValue.*; import code.*; public class PostIncNode extends PostfixVarNode { public PostIncNode( VariableNode left ) { super( left ); operator = "++"; } public Type checkType() { Type leftType = left.checkType(); if ( ! left.addressable() ) throw new Error( "Operand for ++ not addressable" ); if ( ! ( leftType instanceof IntType ) && ! ( leftType instanceof CharType ) && ! ( leftType instanceof PtrType ) ) throw new Error( "Operand of ++ operator must be an int, char or pointer" ); opCode = "addq"; type = leftType; return type; } public RunValue eval( RunEnv runEnv ) { PtrValue leftData = left.evalAddr( runEnv ); RunValue leftValue = leftData.getValue(); if ( type instanceof IntType ) leftData.setValue( new IntValue( leftValue.intValue() + 1 ) ); else if ( type instanceof CharType ) leftData.setValue( new CharValue( ( char ) ( leftValue.charValue() + 1 ) ) ); else if ( type instanceof PtrType ) { PtrType ptrType = ( PtrType ) type; int size = ptrType.subType().size().interp(); leftData.setValue( leftValue.addressValue().elementAt( size ) ); } return leftValue; } public void evalCode( Node sibling ) { super.evalCode( sibling ); Address address = left.loadAddress( usage() ); Register preValueReg = obtainReg( type, usage().plus( address ) ); Register postValueReg = obtainReg( type, usage().plus( address ).plus( preValueReg ) ); Code.load( type, preValueReg, address ); Code.inc( type, preValueReg, postValueReg ); Code.store( type, postValueReg, address ); setAddress( preValueReg ); } }