compilers
play

Compilers Stack Machines Alex Aiken Stack Machines Only storage - PowerPoint PPT Presentation

Compilers Stack Machines Alex Aiken Stack Machines Only storage is a stack An instruction r = F(a 1 ,a n ): Pops n operands from the stack Computes the operation F using the operands Pushes the result r on the stack Alex


  1. Compilers Stack Machines Alex Aiken

  2. Stack Machines • Only storage is a stack • An instruction r = F(a 1 ,…a n ): – Pops n operands from the stack – Computes the operation F using the operands – Pushes the result r on the stack Alex Aiken

  3. Stack Machines Alex Aiken

  4. Stack Machines • Consider two instructions – push i - push integer i on the stack – add - add two integers – A program: push 7 push 5 add Alex Aiken

  5. Stack Machines • Stack machines are a very simple machine model – Leads to a simple, small compiler – But not necessarily one that produces very fast code Alex Aiken

  6. Stack Machines • Location of the operands/result is not explicitly stated – Always the top of the stack • In contrast to a register machine – add instead of add r 1 , r 2 , r 3 – More compact programs • One reason that Java bytecode uses stack evaluation Alex Aiken

  7. Stack Machines • There is an intermediate point between a pure stack machine and a pure register machine • An n-register stack machine – Conceptually, keep the top n locations of the pure stack machine’s stack in registers • Consider a 1-register stack machine – The register is called the accumulator Alex Aiken

  8. Stack Machines • In a pure stack machine – An add does 3 memory operations – Two reads and one write to the stack • In a 1-register stack machine the add does acc  acc + top_of_stack Alex Aiken

  9. Stack Machines • Consider an expression op(e 1 ,…,e n ) – Note e 1 ,…,e n are subexpressions • For each e i (0 < i < n) – Compute e i – Push result on the stack • Pop n-1 values from the stack, compute op • Store result in the accumulator Alex Aiken

  10. Stack Machines Alex Aiken

  11. Stack Machines After evaluating an expression e, the accumulator holds the value of e and the stack is unchanged. Expression evaluation preserves the stack. Alex Aiken

  12. Stack Machines Code Acc Stack acc  3 3 <init> push acc 3 3, <init> acc  7 7 3, <init> push acc 7 7, 3, <init> acc  5 5 7, 3, <init> acc  acc + top_of_stack 12 7, 3, <init> pop 12 3, <init> acc  acc + top_of_stack 15 3, <init> pop 15 <init> Alex Aiken

  13. Stack Machines Given the current state of the stack and accumulator, what is the next line of Current: code to generate for the code fragment Acc : 5 (2 * 3) + 5? Stack: 6,<init> push acc pop acc  6 acc  acc + top_of_stack

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend