[Solved] CS451651 Project1- Supporting Simple Operations

$25

File Name: CS451651_Project1__Supporting_Simple_Operations.zip
File Size: 442.74 KB

SKU: [Solved] CS451651 Project1- Supporting Simple Operations Category: Tag:
5/5 - (1 vote)
  1. Become familiar with the CLEmitter, an abstraction for generating JVM bytecode (see Appendix D of our text).
  2. Extend the base j language by adding some basic Java operations (on primitive integers) to the language. Supporting these operations requires studying the j compiler in its entirety, if only cursorily, and then making slight modifications to it. Notice that many of the operations have different levels of precedence, just as * has a different level of precedence in j than does +. These levels of precedence are captured in the Java grammar (see appendix at the end); for example, the parser uses one method to parse expressions involving * and /, and another to parse expressions involving + and -. Download and Test the j

Download and unzip the base j compiler under some directory (well refer to this directory as $j). See Appendix A for information on whats in the j distribution.

Run the following command inside the $j directory to compile the j compiler.

$ ant clean compile jar

Run the following command to compile a j program P.java using the j compiler, which produces the JVM target program

P.class.

$ sh $j/j/bin/j P.java

Run the following command to run P.class.

$ java P

Problem 1. (Using CLEmitter) Consider the following program IsPrime.java that receives an integer n as command-line argument and prints whether or not n is a prime number.

// IsPrime.javapublic class IsPrime {// Returns true if n is prime, and false otherwise. private static boolean isPrime(int n) { if (n < 2) {return false;}for (int i = 2; i <= n / i; i++) { if (n % i == 0) { return false;}}return true;}// Entry point.public static void main(String[] args) { int n = Integer.parseInt(args[0]); boolean result = isPrime(n); if (result) {System.out.println(n + is a prime number);}else {System.out.println(n + is not a prime number);}}}

Using the annotated program GenFactorial.java under $j/j/tests/clemitter as a model, complete the implementation of the program GenIsPrime.java that uses the CLEmitter interface to programmatically generate IsPrime.class, ie, the JVM bytecode for the program IsPrime.java above.

$ javac -cp .:$j/j/lib/j.jar GenIsPrime.java

$ java -cp .:$j/j/lib/j.jar GenIsPrime

$ java IsPrime 42

42 is not a prime number

$ java IsPrime 31

31 is a prime number

Hints: There are two ways to approach this problem, the first being more intellectually rewarding.

  1. The bytecode for main() is similar to the bytecode for GenFactorial.main(). Here are some hints for generating bytecode for the isPrime() method:
if n >= 2 goto A: return falseA: i = 2D: if i > n / i goto B: if n % i != 0 goto C: return falseC: increment i by 1 goto D:B: return True
  1. Compile java using javac, and decompile (using javap) IsPrime.class to get the bytecode javac generated and mimic the same in GenIsPrime.

Problem 2. (Division Operation) Follow the process outlined in Section 1.5 of our text to implement the Java division operator /.

$ $j/j/bin/j tests/Division.java

$ java Division 42 6

7

Problem 3. (Remainder Operation) Implement the Java remainder operator %.

$ $j/j/bin/j tests/Remainder.java

$ java Remainder 42 13

3

Problem 4. (Shift Operations) Implement the Java shift operators: arithmetic left shift <<, arithmetic right shift >>, logical right shift >>>.

$ $j/j/bin/j tests/ArithmeticLeftShift.java

$ java ArithmeticLeftShift 1 5

32

$ $j/j/bin/j tests/ArithmeticRightShift.java

$ java ArithmeticRightShift 32 5

1

$ java ArithmeticRightShift -32 5 -1

$ $j/j/bin/j tests/LogicalRightShift.java

$ java LogicalRightShift 32 5

1

$ java LogicalRightShift -32 5

134217727

Problem 5. (Bitwise Operations) Implement the Java bitwise operators: unary complement ~, inclusive or |, exclusive or ^, and &. Note: there are JVM instructions for |, ^, and &, but not for ~, which must be computed as the exclusive or of the operand and -1.

$ $j/j/bin/j tests/BitwiseNot.java

$ java BitwiseNot 42

-43

$ $j/j/bin/j tests/BitwiseInclusiveOr.java

$ java BitwiseInclusiveOr 3 5 7

$ $j/j/bin/j tests/BitwiseExclusiveOr.java

$ java BitwiseExclusiveOr 3 5 6

$ $j/j/bin/j tests/BitwiseAnd.java

$ java BitwiseAnd 3 5

1

Problem 6. (Unary Plus Operation) Implement the Java unary plus operaor +.

$ $j/j/bin/j tests/UnaryPlus.java

$ java UnaryPlus -42

-42

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS451651 Project1- Supporting Simple Operations[Solved] CS451651 Project1- Supporting Simple Operations
$25