CSE 401, Winter 2014
Robert R. Henry
Assignment 10
Doubles
Due via dropbox Wednesday March 12 by the end of the day (midnight).
Goals
You will be able to execute MJ programs that manipulate doubles. This work has you revisit much of your compiler, and make little extensions here and there as needed. In fact, you’ve already done lexing and parsing for doubles, so it is just semantic analysis, code generation and library code to do.
Things to Do
- You must implement basic support for double precision floating point numbers, using IEEE-754 64-bit floating point numbers, corresponding to the “double” type in Java. You will need to implement double precision literals, the “double” keyword, double arrays, evaluate expressions involving double arithmetic, assign to doubles, pass double arguments to functions, and return doubles from functions.
- Do NOT use the obsolete x87 instruction set for doing floating point.
- Study what gcc does.
- Let the SSE hardware do the heavy lifting.
- You do NOT need to implement explicit or implicit type conversions from ints to doubles; doubles are essentially an island unto themselves.
- You do NOT need to implement % (remainder) from here http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3, as it is tricky, and there’s no direct analog in C or C++ for you to study. In fact, you should change the type checker to reject application of % on doubles.
- Generating code to compare doubles is tricky, due to bizarre asymmetry in the x86_64 SSE instruction set. Instead, call a library function to do the work, and rely on gcc to use the right set of tricks. You’ll need 6 library functions which you should put into boot.c.
- You must pass doubles as arguments, and return a double from a function, using the standard x86_64 ABI conventions. Note that doubles go into their own register bank. You’ll encounter a little trickiness to keep track of storage locations for double parameters, especially when they are interleaved with ints (longs) or references.
- There is a function in boot.c named “put_double” which prints a double precision number, using the Java rules here http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#toString(double). Your type checker will have to determine how to implement the now overloaded function System.out.println, either to print a 64-bit int, or print a 64-bit double.
- You do not need to understand what happens in “put_double”, because it is very tricky to write (and read) floating point numbers accurately without loss of precision. The code in “put_double” should produce identical results to what Java does for all numbers you are likely to encounter in practice; it is “off” for very small denormalized floating point numbers, as well puts trailing 0’s on numbers near 0.001. (This bug was in the Java Run time for over 8 years.)
What to turn in
- As always, a complete tar file into the drop box.
- Output convincing us that your test program(s) for doubles are doing the right thing. You may have to extend the test program(s) you wrote back in week 1 based on what you now know.