Read Groovy Compile process(Groovy Benkyoukai 2013)

  • View
    2.351

  • Download
    0

Embed Size (px)

DESCRIPTION

Outline of groovy compilation process

Transcript

  • 1. Groovy Groovy Groovy 2013/3/9 @NTT Grails Slide # 1 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339

2. (@uehaj) NTTGrails JGGUG(Grails/Groovy) : GROOVY() Grails() G*Magazine Vol 6 Gr GroovyServ, LispBuilder, GVM(Groovy JVM), StaticalizerSlide # Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 3. GroovyANTLRASMSlide # 3 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 4. GroovySlide # 4 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 5. Groovysrc/main builder typehandlinggroovy expr wrappers beans stmt syntax grape toolstools inspect classgen ast io asm gse lang indy javac securitysc shell timecli util transform control xml ui customizerstransform util builder sc logging io transformers xml messages stcorg plugin utilapachereflection vmplugin commons stdclassesv5cliruntime v6codehaus callsitev7groovy dgmimpl antlr arrays java m12n parser memoize treewalker metaclass ast powerassertSlide # 5 GroovyCopyright(C) 2013 NTT Software Corporation All rights reserved.1339 6. JavaJava javac .class java JVM Java Slide # 6 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 7. GroovyJavaGroovy javac groovyc .class.class java groovy JVMGroovy () JavaJava Java Slide # 7 Groovy 2011 Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 8. Groovy Groovy groovyc .classgroovy JVMGroovy ()Java Java Slide # 8 6 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 9. Groovygroovyc groovy groovysh groovy.ui.GroovyMain o.c.g.tools.FileSgroovyConsole ystemCompilergroovy.lang.GroovyShell groovy.util.Groo vyScriptEnginegroovy.lang.GroovyClass Loader org.codehaus.groovy.controll.CompilationUnitSlide # 9 GroovyCopyright(C) 2013 NTT Software Corporation All rights reserved.1339 10. CompilationUnit org.codehaus.groovy.controll.CompilationUnit CompilationUnit Slide # 10 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 11. JavaAwareCompilationUnit extends CompilationUnit()JavaAwareCompilationUnit extends CompilationUnit (-j,--jointCompilation)GroovyJava-j.javaGroovy/(!!) Slide # 11Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved. 1339 12. CompilationUnit#compile() public void compile() .. { compile(Phases.ALL); // } public void compile(int throughPhase) .. { // : }Slide # 12 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 13. ?CompilationUnit INITIALIZATION(1) // PARSING(2) //ANTLRAST CONVERSION(3) //CSTAST SEMANTIC_ANALYSIS(4) //AST CANONICALIZATION(5) //AST INSTRUCTION_SELECTION(6) //(1) CLASS_GENERATION(7) //(2) OUTPUT(8) // FINALIZATION(9) // ALL(9) //Slide # 13 GroovyCopyright(C) 2013 NTT Software Corporation All rights reserved.1339 14. () AST? ASTAbstract Syntax Tree, CST(Concrete Syntax Tree, ) a.foo(b,c) CST . AST. a foo afoo () , b c b cSlide # 14 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 15. GroovyConsoleGroovy AST BrowserSlide # 15 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 16. CompilationUnitphaseOperation() CompilationUnit#addPhaseOperation() AST CompilerCustomizerASTSlide # 16 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 17. ()ASTASTAST INITIALIZATION - PARSING- CONVERSION @Grab SEMANTIC_ANALYSIS@Field, @Log, @PackageScope, CANONICALIZATION AST INSTRUCTION_SELECTIO @TypeChecked, @CompileStatic CategoryASTTransformation CLASS_GENERATION N- DelegateASTTransformation OUTPUT - FINALIZATION -ImmutableASTTransformationSlide # 17 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 18. phaseOperationsPARSING: AnnotationCollectorTransform SourceUnit#parse()StaticVerier AntlrParserPlugin#parseCST()//InnerClassCompletionVisitor CSTEnumCompletionVisitorCONVERSION: CANONICALIZATION: convert(SourceUnit#convert() // compileCompleteCheck Generates an AST from the CST),CLASS_GENERATION: AntlrParserPlugin.buildAST()// classgen(OptimizerVisitor,Generics ANTLR ASTGroovyAST Visitor, LabelVerier, EnumVisitor ClassCompletionVerier,SEMANTIC_ANALYSIS: ExtendedVerier, ClassVisitor, resolve(VariableScopeVisitor, AsmClassGenerator) ResolveVisitor)OUTPUT: staticImport(StaticImportVisitor) output InnerClassVisitorSlide # 18 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 19. phaseOperationsPARSING: AnnotationCollectorTransform SourceUnit#parse()StaticVerier AntlrParserPlugin#parseCST()//InnerClassCompletionVisitor CSTEnumCompletionVisitorCONVERSION: CANONICALIZATION: convert(SourceUnit#convert() // compileCompleteCheck Generates an AST from the CST),CLASS_GENERATION: AntlrParserPlugin.buildAST()// classgen(OptimizerVisitor,Generics ANTLR ASTGroovyAST Visitor, LabelVerier, EnumVisitor ClassCompletionVerier,SEMANTIC_ANALYSIS: ExtendedVerier, ClassVisitor, resolve(VariableScopeVisitor,AsmClassGenerator) ResolveVisitor) OUTPUT: staticImport(StaticImportVisitor) output InnerClassVisitorSlide # 18 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 20. ANTLR Slide # 19 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 21. ANTLR ANTLR? WikipediaANTLRANother Tool for Language RecognitionLL(*)3.xLL(*)2.xLL(k)PCCTSPurdue Compiler Construction Tool Set1989 Terence Parr ANTLR LR"ANT(i)-LR"LR[]ANTLR EBNF Groovy 2.1ANTLR 2.7.7 34Slide # 20 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 22. AntlrParserPlugin.java() parseCST() CST ANTLRAST o.c.g.antlr.parser.GroovyLexer o.c.g.antlr.parser.GroovyRecognizer buildAST() ANTLRASTGroovyASTSlide # 21 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 23. GroovyLexer.java, GroovyRecognizer.javagroovy.gGroovyGroovytarget/generated-sourcesJavagroovy.gGroovyLexer.java(Groovy) ANTLR GroovyRecognizer.javaSlide # 22 GroovyCopyright(C) 2013 NTT Software Corporation All rights reserved.1339 24. groovy.gANTLR 2.7Groovy 4324groovy./src/main/org/codehaus/groovy/runtime/14239DefaultGroovyMethods.java./src/main/org/codehaus/groovy/antlr/4323groovy.g./subprojects/groovy-sql/src/main/java/groovy/sql/ 4229Sql.java./src/main/org/codehaus/groovy/runtime/3865StringGroovyMethods.javaSlide # 23 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 25. groovy.gANTLRCompilationUnit // Compilation Unit: Groovy // compilationUnit : // "#!" (SH_COMMENT!)? // nls! // compilation unitopitional ( (annotationsOpt "package")=> packageDefinition | (statement[EOF])? ) // // and/or ( sep! (statement[sepToken])? )* EOF! ;Slide # 24 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 26. do-while? /*OBS* no do-while statement in Groovy (too ambiguous) // do-while statement | "do"^ statement "while"! LPAREN! strictContextExpression RPAREN! SEMI! *OBS*/Slide # 25 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 27. ()AntlrPerserPluginCompilerlerCongration import org.codehaus.groovy.control.* import org.codehaus.groovy.antlr.* import org.codehaus.groovy.syntax.* class Pascalizer extends ParserPluginFactory { ParserPlugin createParserPlugin() { new AntlrParserPlugin() { Reduction parseCST(SourceUnit sourceUnit, Reader reader) { def s = reader.text.replaceAll(begin, {).replaceAll(end, }) s = s.replaceAll(/(*/, /*).replaceAll(/*)/, */) super.parseCST(sourceUnit, new StringReader(s)) } } } } def conf = new CompilerConfiguration(pluginFactory: new Pascalizer()) new GroovyShell(binding, conf).evaluate(""" def foo(arg) begin (* *) println arg end foo("hello pascal!") """)Slide # 26 Groovy : http://groovyconsole.appspot.com/script/3Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 28. ASM Slide # 27 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 29. ASM?WikipediaObjectWeb ASMThe ASM library is a project of the ObjectWeb consortium.It provides a simple API for decomposing, modifying, andrecomposing binary Java classes (i.e. bytecode). Theproject was originally conceived and developed by EricBruneton. ASM is Java-centric at present, and does notcurrently have a backend that exposes other bytecodeimplementations (such as .NET bytecode,Pythonbytecode, etc.). Groovy 2.1ASM 4.0Slide # 28 Groovy Copyright(C) 2013 NTT Software Corporation All rights reserved.1339 30. CompilatoinUnit#classgenCLASS_GENERATIONphaseOperation new OptimizerVisitor(this).visitClass(classNode, source) new GenericsVisitor(source).visitClass(classNode) new Verier().visitClass(classNode) new LabelVerier(source).visitClass(classNode) new ClassCompletionVerier(source).visitClass(classNode) new ExtendedVerier(source).visitClass(classNode) ClassVisitor visitor = createClassVisitor() new AsmClassGenerator(source, context, visitor, sourceName).visitClass(classNode) byte[] bytes = visitor.toByteArray(); generatedClasses.add(new GroovyClass(classNode.getName(), bytes)) // bytesGroovy // output Slide # 29 GroovyCopyright(C) 2013 NTT Software Corporation All rights reserved.1339 31. AsmClassGeneratorVisitor visitSynchronizedStatement() visitClass()visitThrowStatement() visitClass()visitReturnStatement() visitGenericType()visitExpressionStatement() visitConstructor()visitTernaryExpression() visitMethod() visitDeclarationExpression() visitField()visitBinaryExpression() visitProperty() visitPostxExpression() visitCatchStatement() visitPrexExpression() visitBlockStatement() visitClosureExpression() visitForLoop()visitConstantExpression() visitWhileLoop()visitSpreadExpression() visitDoWhileLoop()visitSpreadMapExpression() visitIfElse() visitMethodPointerExpression() visitAssertStatement()visitUnaryMinus