IUCompilerCourse / Essentials-of-Compilation

A book about compiling Racket and Python to x86-64 assembly
1.27k stars 137 forks source link

Correct instruction selection #137

Closed AndrewTolmach closed 1 year ago

AndrewTolmach commented 1 year ago

p. 25 para 2 says "There are also cases that require special care to avoid generating needlessly complicated code." In fact, special care is required just to generate correct code in some cases, including this example! That is, if we use the initial scheme on x = y + x, we'll compute x +x by mistake. So the current wording is a bit misleading. (And subtraction is thornier still, although maybe that is best kept as a nasty surprise for the student.)

jsiek commented 1 year ago

Yes, that is a bit misleading. I've revised to clarify, first introducing a fully general 3-instruction sequence, and then saying how we can do better with either a single instruction for with 2-instructions depending on whether the first argument is the same as the LHS or not.