Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2. This question is about code generation. (a) When translating conditionals and loops, the generated assembly code (e.g. MIPS, x86 or ARM) contains jumps. For
2. This question is about code generation. (a) When translating conditionals and loops, the generated assembly code (e.g. MIPS, x86 or ARM) contains jumps. For the CPU to be able to execute a jump, the target of a jump must be a valid memory address. However, code generators that emit assembly code typically generate jumps to symbolic addresses. Here are two examples in MIPS assembly: b exit int j beq loop_body Answer the following questions about symbolic addresses. i. What are the advantages of using symbolic addresses? ii. Which program is responsible for translating such symbolic addresses to actual memory addresses? ii. How does the code generator create symbolic addresses? [10 marks) (b) Consider the following program fragment in a Java-like language: class A { Af (int i) { i+1; A a = new A (); return a; } } Can the local variable a in the method f be allocated in the activation record (for invocations) of f? If you think it can, sketch an appropriate layout for the activation record. If you think that's not possible, explain why not, and where a should be held instead. [10 marks] (C) In the lectures we used (a variant of) the following simple programming language to explain code-generation. E = n + E + E| E - E P = for r = E to E do PP;P 2:=E Here z ranges over variables, and n over integers. Design code generator for the simple language above. The target machine architecture for your code generator is the stack machine (as introduced in the lectures). Make sure to explain your design appropriately, including any auxiliary procedures you might be using. Note that your code generator should translate both programs (ranged over by P) and expressions (ranged over by E). For your help, the commands of the machine language for the stack machine are listed below. [30 marks) Command Meaning Nop Does nothing Pop x Removes the top of the stack and stores it in x PushAbs x Pushes the content of the variable x on stack Pushlmm n Pushes the number n on stack CompGreater Than Pops the top two elements off the stack. If the first one popped is bigger than the second one, pushes a 1 onto the stack, otherwise pushes a 0. CompEq Pops the top two elements off the stack. If both are equal, pushes a 1 onto the stack, otherwise pushes a 0. Jump Jumps to Jump True Jumps to address/labell if the top of the stack is not 0. Top element of stack is removed. Plus Adds the top two elements of the stack, and puts result on stack. Both arguments are removed from stack Minus Subtracts the second element of the stack from the top element, and pushes the result on the after popping the top two elements from the stack Times Multiplies the top two elements of the stack, and puts result on stack. Both arguments are removed from stack Divide Divides the top element of the stack by the second element on the stack, and puts result on stack. Both arguments are removed from stack Negate Negates the top element of the stack
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started