Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Please submit your answers    

In this homework, we are going to generate Java Virtual Machine codes by processing the abstract syntax tree constructed from the parsing process.

We are going to support all features of C-- language for integer data type. That is, for the purpose of this homework, float data type is not supported.

Please refer to the following documents about Java Virtual Machine codes:

  • Instructions Set (limited to instructions that are related to integers only)
  • Compiling Examples

The template for codes generation program is similar to that given for hw7. Also, click here for an executable for hw8.

One can obtain the Java Virtual Machine codes for the test data as follows: Take the example of data04. We can present it as a Java program as follows:

public class data04 {

public static

int

Factorial(int v)

{

   int limit = 7;

   if (v < 0 || v > limit) return -1;

   {

       int c = 0, fact = 1;

       /* Loop invariant: fact == c! */

       while (c < v) {

          c = c+1; fact = fact*c;

       }

       return fact;

   }

}

}

The commands are:

  javac data04.java

  javap -c data04 > data04.java.out

The command javap will produce the Java Virtual Machine Codes in readable format.

You can also compare the output data04.java.out by javap with the output of hw8 by running

  hw8 < data04 > data04.out

NOTE: The codes produced by hw8 may differ from that by the Java compiler. But the codes are close. Sometimes, hw8 has a few more unnecessary branching statements which can be cleaned up by a simple pass over the generated codes.

Programming hints:

Previously, traverse_ast does not return any value. You may want to extend the function traverse_ast to return the number of lines of codes generated. In order that traverse_ast can generate the right line numbers for the Java Virtual Machine codes, the function traverse_ast takes one extra parameter next_line_number which is the line number of the first instruction generated in the processing of the subtree rooted at node.

The (unsophisticated) codes for AST_ADD in traverse_ast can be very simple as follows:

case AST_ADD:

             size  = traverse_ast( node->left  , next_line_number        );

             size += traverse_ast( node->right , next_line_number + size );

             printf("%4d:   iadd\n", next_line_number+size);

             size++;

             return size;

You need to modify your symtbl module so that each ID symbol has an entry with a field local_var_index. Note that we do not make use of any temp variable for codes generation in this homework. We need a global variable local_var_num, which use is similar to that of temp_num, to keep track of the largest variable number that is currently in use. The function symtbl_dump_entry needs to be revised to print also the local_var_index associated with an identifier.

Also, modify hw5parser.y so that float data type is no longer supported.

type_spec   : INT

              { data_type = TYPE_INT; }

            | FLOAT

              { yyerror("float unsupported\n"); exit(-1); }

            ;

Java uses special comparison instructions when one of the operands is 0. They are ifeq, ifne, iflt, ifge, ifgt, ifle. For your first attempt, you can just use the general comparison instructions if_icmpeq, if_icmpne, if_icmplt, if_icmpge, if_icmpgt, if_icmple so that you can avoid testing if one of the operands is 0.

NOTE: the codes that your program generates do not have to match the quality of the codes generated by the Java (javac) compiler, or my hw8.

NOTE: You can assume that each integer constant in a C-- program is of small value, and can be represented in one byte so that there is no need to use the runtime constant pool. Also, you can assume that the number of local variables is small such that the index into the local variable array can be achieved in one byte.

Java, Programming

  • Category:- Java
  • Reference No.:- M9525177

Have any Question?


Related Questions in Java

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As