package node.exprNode.prefixNode; 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 PreIncNode extends PrefixVarNode { public PreIncNode( VariableNode right ) { super( right ); operator = "++"; } public Type checkType() { Type rightType = right.checkType(); if ( ! right.addressable() ) throw new Error( "Operand for ++ not addressable" ); if ( ! ( rightType instanceof IntType ) && ! ( rightType instanceof CharType ) && ! ( rightType instanceof PtrType ) ) throw new Error( "Operand of ++ operator must be an int, char or pointer" ); opCode = "addq"; type = rightType; return type; } public RunValue eval( RunEnv runEnv ) { PtrValue rightData = right.evalAddr( runEnv ); RunValue rightValue = rightData.getValue(); if ( type instanceof IntType ) rightValue = new IntValue( rightValue.intValue() + 1 ); else if ( type instanceof CharType ) rightValue = new CharValue( ( char ) ( rightValue.charValue() + 1 ) ); else if ( type instanceof PtrType ) { PtrType ptrType = ( PtrType ) type; int size = ptrType.subType().size().interp(); rightValue = rightValue.addressValue().elementAt( size ); } rightData.setValue( rightValue ); return rightValue; } public void evalCode( Node sibling ) { super.evalCode( sibling ); Register valueReg = obtainReg( type, usage().plus( address() ) ); Address address = right.loadAddress( usage().plus( valueReg ) ); Code.load( type, valueReg, address ); Code.inc( type, valueReg, valueReg ); Code.store( type, valueReg, address ); setAddress( valueReg ); } }