Verildi

IA32 Code Generation in C

The code you need to modify is hw8.gen.codes.c with fprintf statement. I need this by midnight this Tuesday, 04/09/2013.

In this homework, we are going to do the Intel IA32 codes generation by processing the abstract syntax tree constructed from the parsing process.

To make a function call, one way is to follow the instructions given here under "Calling Functions". Arguments are passed by pushl, followed by a call to the function, then discarding the arguments by changing the stack pointer %esp using addl, and finally retrieve the function result from register %eax. In this homework, we suggest you to use the above method. However, gcc (or, gcc 3.x) uses a more efficient way. You can run gcc3 -S to see the assembly codes generated for calling a function. We will explain the method used by gcc's in class. Next, consider the function call:

c = f(a,b);

We need to push the argument b first, then push the argument a, followed by calling function f, saved the value of %eax to c. In order to generate the pushes for the arguments in the right order, when processing an ast node of the type AST_ARG_LIST, the correct logic is to process the right subtree before the left subtree.

In processing an AST_ID node, if the node's entry is a local array variable, you need to load the beginning address of the array into a temporary variable as in

fprintf(fp, " leal %d(%%ebp), %%eax\n", node->entry->offset);

What should be the logic for referencing a non-array ID, or an array ID when the array is passed as a function parameter?

In processing an AST_ARRAY_REF node, there are two cases. If the array is a local variable, the loading of the array reference into register %eax can be achieved as follows:

fprintf(fp, " movl %d(%%ebp), %%eax\n", node->left->entry->offset);

fprintf(fp, " movl %d(%%ebp,%%eax,4), %%eax\n", node->entry->offset);

On the other hand, if the array is a function parameter, the loading of the array reference into register %eax requires indirection, which can be achieved as follows:

fprintf(fp, " movl %d(%%ebp), %%eax\n", node->left->entry->offset);

fprintf(fp, " leal 0(,%%eax,4), %%edx\n");

fprintf(fp, " movl %d(%%ebp), %%eax\n", node->entry->offset);

fprintf(fp, " movl (%%edx,%%eax), %%eax\n");

Note that we still have to save the value in register %eax back to a temporary (local) variable.

In processing an AST_ASSIGN node, the logic for an assignment to a simple variable is straight-forward, while the logic for assigning to an array entry is more complicated. Assigning to a local array entry can be achieved as follows:

fprintf(fp, " movl %d(%%ebp), %%eax\n", node->left->left->entry->offset);

fprintf(fp, " movl %d(%%ebp), %%edx\n", node->right->entry->offset);

fprintf(fp, " movl %%edx, %d(%%ebp,%%eax,4)\n",node->left->entry->offset);

What should be the logic for assigning to an array entry when the array is passed as a function parameter?

Beceriler: C Programlama

Daha fazlasını görün: what is syntax in programming, what is in c programming, what is c programming used for, what is a variable in programming, what is a tree node, what is an array in programming, what is an argument in programming, what is a method in programming, what is a function in programming, what is a class in programming, variable in programming, tree in order, syntax programming, syntax in programming, stack programming, programming what is a class, programming syntax, programming statement, programming in assembly, pointer programming, note value tree, node programming, node of a tree, node in tree, need assembly instructions

İşveren Hakkında:
( 2 değerlendirme ) Albuquerque, United States

Proje NO: #4408207

1 freelancer is bidding on average $100 for this job

hddh

I can do it.

in 2 gün içinde100$ USD
(11 Değerlendirme)
3.9