Not all speech is protected, but yes writing code is protected, it is executing that code for nefarious purposes that is not. Just like writing down invention designs for a killer robot is protected, but building that robot and using it on people is not.