CS代写 Final assignment

Final assignment
Complete the rest of the compiler. Remaining features

o Each Procedure is an inner class that implements the Runnable interface

Copyright By PowCoder代写 加微信 powcoder

o Variables defined in the procedure are fields of the class.

o The body of the procedure is the run method.

o Referencing a variable means loading a reference to the defining object and using

GETFIELD or PUTFIELD INSTRUCTIONS.

o Calling a method involves instantiating an instance of the class and invoking its run

o Instances of non-static inner classes contain an implicit reference to enclosing class.

This is typically called this$N (this$0, this$1, etc.) where N is the nesting level of the

enclosing class. The value is passed to the method. Referencing non-local

variables requires following the chain of this$N references to find the containing class.

Calling a procedure thus requires following the chain of this$N references to find an

instance of the enclosing class. Use nesting levels stored by the Scope visitor to handle

o Since we will be creating multiple classes, instead of returning a byte[], we need to

return a List of (classname, byte[]) pairs.

o The input command should invoke one of a set of provided methods.

VAR a,b,c;
b := “hello”;
c := TRUE;

Corresponding Java program for Example 1
package edu.ufl.cise.plpfa22.codeGenSamples;

public class Var1 implements Runnable{
boolean c;

public Var1() {

public void run() {

b = “hello”;
System.out.println(a);
System.out.println(b);
System.out.println(c);

public static void main(String[] args) {
new Var1().run();

Previously, we put everything in the run method. We need to rearrange a bit.

Generated code
public class edu/ufl/cise/plpfa22/prog implements java/lang/Runnable {

// access flags 0x1
public I a

// access flags 0x1
public Ljava/lang/String; b

// access flags 0x1
public Z c

// access flags 0x1
public ()V
INVOKESPECIAL java/lang/Object. ()V
MAXSTACK = 1
MAXLOCALS = 1

// access flags 0x9
public static main([Ljava/lang/String;)V
NEW edu/ufl/cise/plpfa22/prog
INVOKESPECIAL edu/ufl/cise/plpfa22/prog. ()V
INVOKEVIRTUAL edu/ufl/cise/plpfa22/prog.run ()V
MAXSTACK = 2
MAXLOCALS = 1

// access flags 0x1
public run()V
PUTFIELD edu/ufl/cise/plpfa22/prog.a : I
LDC “hello”
PUTFIELD edu/ufl/cise/plpfa22/prog.b : Ljava/lang/String;

PUTFIELD edu/ufl/cise/plpfa22/prog.c : Z
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog.a : I
INVOKEVIRTUAL java/io/PrintStream.println (I)V
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog.b : Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog.c : Z
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
MAXSTACK = 2
MAXLOCALS = 1

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.TypePath;
public class Var1Dump implements Opcodes {

public static byte[] dump () throws Exception {

ClassWriter classWriter = new ClassWriter(0);
FieldVisitor fieldVisitor;
RecordComponentVisitor recordComponentVisitor;
MethodVisitor methodVisitor;
AnnotationVisitor annotationVisitor0;

classWriter.visit(V18, ACC_PUBLIC | ACC_SUPER,
“edu/ufl/cise/plpfa22/codeGenSamples/Var1”, null, “java/lang/Object”, new String[] {
“java/lang/Runnable” });

classWriter.visitSource(“Var1.java”, null);

fieldVisitor = classWriter.visitField(0, “a”, “I”, null, null);
fieldVisitor.visitEnd();

fieldVisitor = classWriter.visitField(0, “b”, “Ljava/lang/String;”, null, null);
fieldVisitor.visitEnd();
fieldVisitor = classWriter.visitField(0, “c”, “Z”, null, null);
fieldVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, ““, “()V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(9, label0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL, “java/lang/Object”, ““, “()V”,
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(10, label1);
methodVisitor.visitInsn(RETURN);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLocalVariable(“this”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var1;”, null, label0, label2, 0);
methodVisitor.visitMaxs(1, 1);
methodVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, “run”, “()V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(14, label0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitIntInsn(BIPUSH, 42);
methodVisitor.visitFieldInsn(PUTFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“a”, “I”);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(15, label1);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitLdcInsn(“hello”);
methodVisitor.visitFieldInsn(PUTFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“b”, “Ljava/lang/String;”);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLineNumber(16, label2);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitInsn(ICONST_1);
methodVisitor.visitFieldInsn(PUTFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“c”, “Z”);
Label label3 = new Label();
methodVisitor.visitLabel(label3);
methodVisitor.visitLineNumber(17, label3);

methodVisitor.visitFieldInsn(GETSTATIC, “java/lang/System”, “out”,
“Ljava/io/PrintStream;”);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“a”, “I”);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, “java/io/PrintStream”, “println”,
“(I)V”, false);
Label label4 = new Label();
methodVisitor.visitLabel(label4);
methodVisitor.visitLineNumber(18, label4);
methodVisitor.visitFieldInsn(GETSTATIC, “java/lang/System”, “out”,
“Ljava/io/PrintStream;”);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“b”, “Ljava/lang/String;”);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, “java/io/PrintStream”, “println”,
“(Ljava/lang/String;)V”, false);
Label label5 = new Label();
methodVisitor.visitLabel(label5);
methodVisitor.visitLineNumber(19, label5);
methodVisitor.visitFieldInsn(GETSTATIC, “java/lang/System”, “out”,
“Ljava/io/PrintStream;”);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”,
“c”, “Z”);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, “java/io/PrintStream”, “println”,
“(Z)V”, false);
Label label6 = new Label();
methodVisitor.visitLabel(label6);
methodVisitor.visitLineNumber(20, label6);
methodVisitor.visitInsn(RETURN);
Label label7 = new Label();
methodVisitor.visitLabel(label7);
methodVisitor.visitLocalVariable(“this”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var1;”, null, label0, label7, 0);
methodVisitor.visitMaxs(2, 1);
methodVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, “main”,
“([Ljava/lang/String;)V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(23, label0);
methodVisitor.visitTypeInsn(NEW, “edu/ufl/cise/plpfa22/codeGenSamples/Var1”);
methodVisitor.visitInsn(DUP);
methodVisitor.visitMethodInsn(INVOKESPECIAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var1”, ““, “()V”, false);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var1”, “run”, “()V”, false);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(24, label1);
methodVisitor.visitInsn(RETURN);

Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLocalVariable(“args”, “[Ljava/lang/String;”, null, label0, label2,
methodVisitor.visitMaxs(2, 1);
methodVisitor.visitEnd();
classWriter.visitEnd();

return classWriter.toByteArray();

VAR a,b,c;
PROCEDURE p;

b := “hello”;
c := TRUE;

Corresponding Java program

package edu.ufl.cise.plpfa22.codeGenSamples;

public class Var2 implements Runnable {
boolean c;

public Var2() {

class p implements Runnable {

public void run() {
b = “hello”;
System.out.println(a);

System.out.println(b);
System.out.println(c);

public static void main(String[] args) {
new Var2().run();

public void run() {
new p().run();

Generated Code

public class edu/ufl/cise/plpfa22/prog implements java/lang/Runnable {

NESTMEMBER edu/ufl/cise/plpfa22/prog$p

// access flags 0x1
public I a

// access flags 0x1
public Ljava/lang/String; b

// access flags 0x1
public Z c

// access flags 0x1
public ()V
INVOKESPECIAL java/lang/Object. ()V
MAXSTACK = 1
MAXLOCALS = 1

// access flags 0x9
public static main([Ljava/lang/String;)V
NEW edu/ufl/cise/plpfa22/prog
INVOKESPECIAL edu/ufl/cise/plpfa22/prog. ()V
INVOKEVIRTUAL edu/ufl/cise/plpfa22/prog.run ()V
MAXSTACK = 2
MAXLOCALS = 1

// access flags 0x1
public run()V
NEW edu/ufl/cise/plpfa22/prog$p

INVOKESPECIAL edu/ufl/cise/plpfa22/prog$p. (Ledu/ufl/cise/plpfa22/prog;)V
INVOKEVIRTUAL edu/ufl/cise/plpfa22/prog$p.run ()V
MAXSTACK = 3
MAXLOCALS = 1

edu/ufl/cise/plpfa22/prog$p
// class version 62.0 (62)
// access flags 0x1
public class edu/ufl/cise/plpfa22/prog$p implements java/lang/Runnable {

NESTHOST edu/ufl/cise/plpfa22/prog

// access flags 0x1010
final synthetic Ledu/ufl/cise/plpfa22/prog; this$0

// access flags 0x0
(Ledu/ufl/cise/plpfa22/prog;)V
PUTFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
INVOKESPECIAL java/lang/Object. ()V
MAXSTACK = 2
MAXLOCALS = 2

// access flags 0x1
public run()V
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
PUTFIELD edu/ufl/cise/plpfa22/prog.a : I
LDC “hello”
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
PUTFIELD edu/ufl/cise/plpfa22/prog.b : Ljava/lang/String;
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
PUTFIELD edu/ufl/cise/plpfa22/prog.c : Z
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
GETFIELD edu/ufl/cise/plpfa22/prog.a : I
INVOKEVIRTUAL java/io/PrintStream.println (I)V
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
GETFIELD edu/ufl/cise/plpfa22/prog.b : Ljava/lang/String;
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V

GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
GETFIELD edu/ufl/cise/plpfa22/prog$p.this$0 : Ledu/ufl/cise/plpfa22/prog;
GETFIELD edu/ufl/cise/plpfa22/prog.c : Z
INVOKEVIRTUAL java/io/PrintStream.println (Z)V
MAXSTACK = 2
MAXLOCALS = 1

ASMifier output for Var2
package asm.edu.ufl.cise.plpfa22.codeGenSamples;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.TypePath;
public class Var2Dump implements Opcodes {

public static byte[] dump () throws Exception {

ClassWriter classWriter = new ClassWriter(0);
FieldVisitor fieldVisitor;
RecordComponentVisitor recordComponentVisitor;
MethodVisitor methodVisitor;
AnnotationVisitor annotationVisitor0;

classWriter.visit(V18, ACC_PUBLIC | ACC_SUPER,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2”, null, “java/lang/Object”, new String[] {
“java/lang/Runnable” });

classWriter.visitSource(“Var2.java”, null);

classWriter.visitNestMember(“edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”);

classWriter.visitInnerClass(“edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2”, “p”, 0);

fieldVisitor = classWriter.visitField(0, “a”, “I”, null, null);
fieldVisitor.visitEnd();
fieldVisitor = classWriter.visitField(0, “b”, “Ljava/lang/String;”, null, null);
fieldVisitor.visitEnd();

fieldVisitor = classWriter.visitField(0, “c”, “Z”, null, null);
fieldVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, ““, “()V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(9, label0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL, “java/lang/Object”, ““, “()V”,
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(10, label1);
methodVisitor.visitInsn(RETURN);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLocalVariable(“this”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;”, null, label0, label2, 0);
methodVisitor.visitMaxs(1, 1);
methodVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, “main”,
“([Ljava/lang/String;)V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(26, label0);
methodVisitor.visitTypeInsn(NEW, “edu/ufl/cise/plpfa22/codeGenSamples/Var2”);
methodVisitor.visitInsn(DUP);
methodVisitor.visitMethodInsn(INVOKESPECIAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2”, ““, “()V”, false);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2”, “run”, “()V”, false);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(27, label1);
methodVisitor.visitInsn(RETURN);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLocalVariable(“args”, “[Ljava/lang/String;”, null, label0, label2,
methodVisitor.visitMaxs(2, 1);
methodVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, “run”, “()V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(31, label0);

methodVisitor.visitTypeInsn(NEW, “edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”);
methodVisitor.visitInsn(DUP);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”, ““,
“(Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;)V”, false);
methodVisitor.visitMethodInsn(INVOKEVIRTUAL,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”, “run”, “()V”, false);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLineNumber(32, label1);
methodVisitor.visitInsn(RETURN);
Label label2 = new Label();
methodVisitor.visitLabel(label2);
methodVisitor.visitLocalVariable(“this”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;”, null, label0, label2, 0);
methodVisitor.visitMaxs(3, 1);
methodVisitor.visitEnd();
classWriter.visitEnd();

return classWriter.toByteArray();

ASMifier for Var2$p

package asm.edu.ufl.cise.plpfa22.codeGenSamples;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ConstantDynamic;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.TypePath;
public class Var2$pDump implements Opcodes {

public static byte[] dump () throws Exception {

ClassWriter classWriter = new ClassWriter(0);
FieldVisitor fieldVisitor;
RecordComponentVisitor recordComponentVisitor;
MethodVisitor methodVisitor;
AnnotationVisitor annotationVisitor0;

classWriter.visit(V18, ACC_SUPER, “edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”, null,
“java/lang/Object”, new String[] { “java/lang/Runnable” });

classWriter.visitSource(“Var2.java”, null);

classWriter.visitNestHost(“edu/ufl/cise/plpfa22/codeGenSamples/Var2”);

classWriter.visitInnerClass(“edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”,
“edu/ufl/cise/plpfa22/codeGenSamples/Var2”, “p”, 0);

fieldVisitor = classWriter.visitField(ACC_FINAL | ACC_SYNTHETIC, “this$0”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;”, null, null);
fieldVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(0, ““,
“(Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;)V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(12, label0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitVarInsn(ALOAD, 1);
methodVisitor.visitFieldInsn(PUTFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”,
“this$0”, “Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;”);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitMethodInsn(INVOKESPECIAL, “java/lang/Object”, ““, “()V”,
methodVisitor.visitInsn(RETURN);
Label label1 = new Label();
methodVisitor.visitLabel(label1);
methodVisitor.visitLocalVariable(“this”,
“Ledu/ufl/cise/plpfa22/codeGenSamples/Var2$p;”, null, label0, label1, 0);
methodVisitor.visitMaxs(2, 2);
methodVisitor.visitEnd();
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, “run”, “()V”, null, null);
methodVisitor.visitCode();
Label label0 = new Label();
methodVisitor.visitLabel(label0);
methodVisitor.visitLineNumber(16, label0);
methodVisitor.visitVarInsn(ALOAD, 0);
methodVisitor.visitFieldInsn(GETFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var2$p”,
“this$0”, “Ledu/ufl/cise/plpfa22/codeGenSamples/Var2;”);
methodVisitor.visitIntInsn(BIPUSH, 42);
methodVisitor.visitFieldInsn(PUTFIELD, “edu/ufl/cise/plpfa22/codeGenSamples/Var2”,
“a”, “I”);
Label label1 = new Label();

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com