Code Generation: Generating JVM Code for Simple Expressions and Assignments, Study notes of Computer Science

An overview of the code generation process for simple expressions and assignments in the context of java virtual machine (jvm) code. It covers the generation of code for various types of expressions, including conditionals, variable declarations, and assignments. The document also includes examples of jvm code for simple arithmetic operations and conditional statements.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-m9f
koofers-user-m9f 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Simple Code Generation
pf3
pf4
pf5
pf8

Partial preview of the text

Download Code Generation: Generating JVM Code for Simple Expressions and Assignments and more Study notes Computer Science in PDF only on Docsity!

Simple Code Generation

“Translation”

1.Evaluate

(^) Exp .

2.If (^) Exp (^) is zero go to step 5.

3.Evaluate

(^) ans (^) ← (^) Exp .

5.Evaluate4.Go to step 6.

(^) ans (^) ← (^) Exp .

6.Use the value computed in

(^) ans (^) as the result of the conditional expression.

Exp

Exp

ConditionalExprNode

Exp

Exp

Exp

Exp

Code Generation Support Routines

GenLoadLiteral(Result, Type, Value)GenLoadLocal(Result, Type, Offset),GenLoadGlobal(Result, Type, Label)FreeTemp(Address1, Adress2, ...) GetTemp(Type)

  1. IdentifierNode.CodeGen ( ) if Address.AccessMode = Global

then Result

(^) GenLoadGlobal(Result, Type, Address.Label)

elsif Address.AccessMode = Local

then Result

(^) GenLoadLocal(Result, Type, Address.Offset)

elsif Address.AccessMode = Literal

then if Result

(^) ≠ (^) null

then Result

(^) GenLoadLiteral(Result, Type, Address.Value)

else Result

(^) AddressNode(Literal, Address.Value)

CodeGen Example

  1. PlusNode.CodeGen ( ) LeftOperand.CodeGen()

RightOperand.CodeGen()

Result (^) ← (^) GenAdd(Result, Type, LeftOperand.Result, RightOperand.Result)

  1. FreeTemp(LeftOperand.Result, RightOperand.Result)

Following

(^) is (^) JVM (^) code (^) for (^) A+B+

(^) where (^) A is a global integer variable

with a label of

L

(^) in class (^) C and (^) B is a local integer variable assigned a local index

(an offset within the frame) of 4:

JVM Code

Comments

Generated By

getstatic

(^) C/L (^) I stackPush static integer field onto

GenLoadGlobal

iload (^4)

stackPush integer local 4 onto

GenLoadLocal

iadd

Add top two stack locations

(^) GenAdd

iconst_

stackPush integer literal 3 onto

GenLoadLiteral

iadd

Add top two stack locations

(^) GenAdd

Assignment

  1. AsgNode.CodeGen ( ) Source.CodeGen()

if Source.Result.AccessMode

{JumpIfTrue, JumpIfFalse}

then Result

(^) ConvertFromJumpCode (Result, Source.Result.AccessMode, Source.Result.Label)

else Result

(^) Source.Result

if Target.Address.AccessMode = Global

then GenStoreGlobal(Result, Type, Target.Address.Label, IsExpr)

else GenStoreLocal(Result, Type, Target.Address.Offset, IsExpr)

Target

Source

AsgNode

Exp

IdentifierNode

IsExpr

Assignment Example

Consider

(^) A = (^) B (^) = C (^) + (^1) where (^) A, (^) B and (^) C are local integers with frame indices of

1, 2 and 3 respectively. First,

C

(^) is evaluated. Since

B

= C

(^1) is used as an

expression, the value of

C

(^) is duplicated (and thereby preserved) so that it can

also be assigned to

(^) A. The JVM code generatde is:

iload (^) 3 ; (^) Push (^) local

(^) # (^) (C) (^) onto (^) the (^) stack

iconst_1 ;

(^) Push (^1) (^) onto (^) the (^) stack

iadd

Compute

(^) C (^) +

dup

Duplicate

C

(^) for (^) second

(^) store

istore 2 ;

(^) Store

(^) top (^) of (^) stack

(^) into (^) local

(^) # (^) (B)

istore 1 ;

(^) Store

(^) top

(^) of (^) stack

(^) into

(^) local

(^) # (^) (A)