1 / 18

Decision Structures – Code Generation

Decision Structures – Code Generation. Lecture 24 Mon, Apr 18, 2005. Conditional Branches. Branching on a conditional expression will be done in two parts. Evaluate the expression, leaving either true (1) or false (0) on the stack. Test the value on the stack and branch on true.

yen
Download Presentation

Decision Structures – Code Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005

  2. Conditional Branches • Branching on a conditional expression will be done in two parts. • Evaluate the expression, leaving either true (1) or false (0) on the stack. • Test the value on the stack and branch on true. • It would be more efficient to test the original expression and branch immediately. • However, by separating the two tests, it will be simpler to write the code.

  3. Conditional Branches • Recall that the if statement is of the form IF (expr)stmt where expr is either zero (false) or nonzero (true). • Thus, we must compare expr to 0. • First, we generate code for a CMPNE tree that will leave 1 on the stack if expr is nonzero.

  4. CMPNE INT CMPNE DOUBLE NUM 0 e INT CAST DOUBLE e DOUBLE NUM 0 The CMPNE Tree Integer Expression Floating-point Expression

  5. Integer Comparisons • To compare two integers, we must • Pop them off the stack into registers. • Compare the registers. • Push 0 or 1 onto the stack. • There is only one compare instruction: cmp. • It sets various flags, depending on the result of the comparison.

  6. Flags CF, ZF, SF, and OF • The relevant flags are • CF – carry flag • ZF – zero flag • SF – sign flag • OF – overflow flag

  7. Conditional Jumps • We then do a condition jump. • A conditional jump checks the flags to decide whether or not to jump.

  8. Integer Comparison • The code for the integer comparison !=. mov $1,%ecx # Move true to ecx pop %eax # Pop right operand pop %edx # Pop left operand cmp %eax,%edx # Compare operands jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

  9. Floating-Point Comparisons • When we compare two floating-point numbers, they are already on the FPU stack, in st(0) and st(1). • The instruction fucompp • Compares st(0) to st(1). • Sets condition codes C0 and C3 in the FPU status word. • Pops both operands off the stack.

  10. Flags C0 and C3 • The result of the comparison is determined by C0 and C3.

  11. Floating-Point Comparisons • To perform a conditional jump based on a floating-point comparison, we must first move the FPU status word to the EFLAGS register. • The instruction fnstsw %ax stores the FPU status word in ax (16 bits). • The instruction sahf stores ah in the EFLAGS regsiter.

  12. Floating-Point Comparisons • The sahf instruction maps • C0  CF. • C3  ZF. • Thus, we want to use conditional jumps that check CF and ZF.

  13. Conditional Jumps

  14. Floating-Point Comparisons • The code for the floating-point comparison !=. mov $1,%ecx # Move true to ecx fucompp # Pop left operand fnstsw %ax # Store status word sahf # Move ah to eflags jne L0n # Jump if left != right dec %ecx # Change ecx to false L0n: push %ecx # Push T/F result

  15. Floating-Point Comparisons • One little complication is that fucompp compares st(0) and st(1) in the reverse order from what we would expect. • Therefore, the meanings if < and > are reversed and the meanings of <= and >= are reversed. • So we reverse the roles of ja and jb, and jae and jbe.

  16. JUMP JUMPT BLABEL 6 BLABEL 6 CMPNE e.mode NUM 0 e e.mode Jump Trees • There are JUMP trees and JUMPT trees.

  17. jmp B6 jmp L8 JUMP Trees • A JUMP tree generates the following code.

  18. pop %eax cmp $1,%eax je B6 pop %eax cmp $1,%eax je L6 JUMPT Trees • A JUMPT tree must pop a boolean value off the stack and jump only if it is 1.

More Related