




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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.
GenLoadLiteral(Result, Type, Value)GenLoadLocal(Result, Type, Offset),GenLoadGlobal(Result, Type, Label)FreeTemp(Address1, Adress2, ...) GetTemp(Type)
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)
RightOperand.CodeGen()
Result (^) ← (^) GenAdd(Result, Type, LeftOperand.Result, RightOperand.Result)
Following
(^) is (^) JVM (^) code (^) for (^) A+B+
(^) where (^) A is a global integer variable
with a label of
(^) 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
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
Consider
(^) A = (^) B (^) = C (^) + (^1) where (^) A, (^) B and (^) C are local integers with frame indices of
1, 2 and 3 respectively. First,
(^) is evaluated. Since
(^1) is used as an
expression, the value of
(^) 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
(^) for (^) second
(^) store
istore 2 ;
(^) Store
(^) top (^) of (^) stack
(^) into (^) local
(^) # (^) (B)
istore 1 ;
(^) Store
(^) top
(^) of (^) stack
(^) into
(^) local
(^) # (^) (A)