Morse theory for Hamiltonian systems