New version page

UK CS 541 - Programming Assignment 5 CSX Code Generator

Upgrade to remove ads
Upgrade to remove ads
Unformatted text preview:

CS 541 — Spring 2014Programming Assignment 5CSX Code GeneratorYour final assignment is to extend the AST node classes to generate JVM assembler code for CSX programs. Your main program calls the CSX parser. If the parse is successful, it calls the type checker. If the program contains no type errors, it calls the code generator. Your program takes the CSX source program to be compiled the command line, writes error messages to standard output, and places generated JVM code in file name.j, where name is the identifier that names the CSX class. Skeletons for the code generator may be found in ~raphael/courses/cs541/public/proj5/startup.The Code GeneratorYour program generates assembler code for the Java Virtual Machine (JVM), which is the same target machine that Java compilers assume. You then assemble the symbolic JVM instructions your compiler generates using the Jasmin assembler. Jasmin documentation is available on its homepage, which is linked to the class homepage (under “Useful Programming Tools”). The JVM instruction set (often called “bytecode”) is also described in the Jasmin documentation. Jasmin produces a standard format “.class” file, which can be executed using java, just as compiled Java programs are.Initiate code generation by calling the member functionboolean codeGen(PrintStream asmfile)in the root of your AST (which must be a ProgramNode). The parameter is the file into which JVM instructions are to be written. codeGen should store the PrintStream in an internal variable or field called aFile. It then calls the member function cg(), which traverses the AST, generating JVM code into aFile.Your code generator need only handle type-correct programs; don’t worry about translating type-incorrect programs. If it detects any errors during code generation, codeGen should return false; the contents of aFile need not be valid. If it detects no errors, it returns true, and the contents of aFile should be a valid JVM assembly program that can be assembled using jasmin. Consider the following simple CSX program:class simple{ void main() {int a; read(a); print("Answer = ", 2*a+1, '\n');} // main()} // class simple This program might translate into the following JVM assembler code: .class public simple ; This is a public class named simple.super java/lang/Object ; The super class is Object; JVM interpreters start execution at main(String[]).method public static main([Ljava/lang/String;)Vinvokestatic simple/main()V ; call main()return ; then return.limit stack 2 ; Max stack depth needed.end method ; End of body of main(String[]).method public static main()V ; Beginning of main().limit locals 1 ; Number of local variables usedinvokestatic CSXLib/readInt()I ; Call CSXLib.readInt()istore 0 ; Store int read into local 0 (a)ldc "Answer = " ; Push string literal onto stack; Call CSXLib.printString(String)invokestatic CSXLib/printString(Ljava/lang/String;)Vldc 2 ; Push 2 onto stackiload 0 ; Push local 0 (a) onto stackimul ; Multiply top two stack valuesldc 1 ; Push 1 onto stackiadd ; Add top two stack valuesinvokestatic CSXLib/printInt(I)V ; Call CSXLib.printInt(int)ldc 10 ; Push 10 ('\n') onto stackinvokestatic CSXLib/printChar(C)V ; Call CSXLib.printChar(char)return ; return from main().limit stack 25 ; Max stack depth needed(overestimate).end method ; End of body of main() Your generator stores this program in file simple.j, since the name of the CSX class is simple. The following command assembles the program into simple.class: jasmin simple.j You would then execute simple.class using the commandjava simpleTranslating AST NodesThe following table outlines what your code generator is expected to do for each kind of AST node. Kind of AST Node Code Generator ActionProgramNodeGenerate beginning of class; generate body of main(String[]);translate members. MemberDeclsNodeTranslate fields, then methods.FieldDeclsNodeTranslate thisField, then moreFields.MethodDeclsNodeTranslate thisMethod, then moreMethods.VarDeclNodeAllocate a field or local variable index for varName. If initValue is non-null, translate it and generate code to store initValue into varName. ConstDeclNodeAllocate a field or local variable index for constName; translate constValue; generate code to store constValue into constName. ArrayDeclNodeAllocate a field or local variable index for arrayName; generate code to allocate an array of type elementType whose size is arraySize; generate code to store a reference to the array in arrayName’s field or local variable.MethodDeclNodeGenerate the method’s prologue; translate args; translate decls; translate stmts; generate the method’s epilogue. ArgDeclsNodeTranslate thisDecl, then moreDecls.ValArgDeclNodeAllocate a local variable index to hold the value of a scalar parameter.RefArrayDeclNodeAllocate a local variable index to hold a reference to an array parameter. StmtsNodeTranslate thisStmt, then moreStmts.AsgNodeIf source is an array, generate code to clone it and save a reference to the clone in target. If source is a string literal, generate code to convert it to a character array and save a reference to the array in target. If target is an indexed array, generate code to push a ref erence to the array (using varName), then translate target.subscriptVal. Translate source; generate code to store source’s value in target.IfThenNodeTranslate condition; generate code to conditionally branch around thenPart; translate thenPart; generate a jump past elsePart; translate elsePart. WhileLoopNodeCreate assembler labels for head-of-loop and loop-exit. If label is non-null store head-of-loop and loop-exit in label’s symbol table entry. Generate head-of-loop label; translate condition; generate a conditional branch to loop-exit label; translate loopBody; generate a jump to head-of-loop; generate loop-exit label.ReadNodeGenerate a call to CSXLib.readInt() or CSXLib.read Char() depending on the type of targetVar; generate a store into targetVar; translate moreReads.PrintNodeTranslate outputValue; generate a call to CSXLib.printString(String) or CSX Lib.printInt(int) or CSXLib.printChar(char) or CSXLib.printBool(bool) or CSXLib.print CharArray(char[]), depending on the type of out putValue; translate morePrints. CallNodeTranslate procArgs; generate a static call to procName.ReturnNodeIf returnVal is non-null then translate it and generate an ireturn; otherwise generate a return.BreakNodeGenerate a jump to the loop-exit


View Full Document
Download Programming Assignment 5 CSX Code Generator
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming Assignment 5 CSX Code Generator and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming Assignment 5 CSX Code Generator 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?